Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async _getPreviousValue (peerId) {
if (!(PeerId.isPeerId(peerId))) {
throw errcode(new Error('invalid peer ID'), 'ERR_INVALID_PEER_ID')
}
try {
const dsVal = await this._datastore.get(ipns.getLocalKey(peerId.id))
if (!Buffer.isBuffer(dsVal)) {
throw errcode(new Error("found ipns record that we couldn't process"), 'ERR_INVALID_IPNS_RECORD')
}
// unmarshal data
try {
const record = ipns.unmarshal(dsVal)
return record.value
} catch (err) {
get (peerId) {
// TODO: deprecate this and just accept `PeerId` instances
if (PeerId.isPeerId(peerId)) {
peerId = peerId.toB58String()
}
return this.peers.get(peerId)
}
async sendMessage(msg, destination) {
if (!msg) throw Error(`Expecting a non-empty message.`)
if (!destination) throw Error(`Expecting a non-empty destination.`)
if (PeerInfo.isPeerInfo(destination)) destination = destination.id
if (typeof destination === 'string') destination = PeerId.createFromB58String(destination)
if (!PeerId.isPeerId(destination))
throw Error(`Unable to parse given destination to a PeerId instance. Got type ${typeof destination} with value ${destination}.`)
// Let's try to convert input msg to a Buffer in case it isn't already a Buffer
if (!Buffer.isBuffer(msg)) {
switch (typeof msg) {
default:
throw Error(`Invalid input value. Got '${typeof msg}'.`)
case 'number':
msg = msg.toString()
case 'string':
msg = Buffer.from(msg)
}
}
const promises = []
has (peerId) {
// TODO: deprecate this and just accept `PeerId` instances
if (PeerId.isPeerId(peerId)) {
peerId = peerId.toB58String()
}
return this.peers.has(peerId)
}
}
let addr
if (multiaddr.isMultiaddr(peer)) {
addr = peer
try {
peer = PeerId.createFromB58String(peer.getPeerId())
} catch (err) {
throw errCode(
new Error(`${peer} is not a valid peer type`),
'ERR_INVALID_MULTIADDR'
)
}
}
if (PeerId.isPeerId(peer)) {
peer = new PeerInfo(peer)
}
addr && peer.multiaddrs.add(addr)
return peerBook ? peerBook.put(peer) : peer
}
}
}
export const peerIdFromJson = (obj) => new Promise((resolve, reject) => {
PeerId.createFromJSON(obj, (err, peerId) => {
if (err) return reject(err)
resolve(peerId)
})
})
export const peerIdToJson = (peerId) => Object.assign(
{ __ipfsPostMsgProxyType: 'PeerId' },
peerId.toJSON()
)
export const isPeerId = PeerId.isPeerId
export const isPeerIdJson = (obj) => obj && obj.__ipfsPostMsgProxyType === 'PeerId'
export const prePeerIdFromJson = (index) => {
return (...args) => {
if (isPeerIdJson(args[index])) {
args[index] = peerIdFromJson(args[index])
}
return args
}
}
export const prePeerIdToJson = (index) => {
return (...args) => {
if (isPeerId(args[index])) {
args[index] = peerIdToJson(args[index])
}
function getB58Str (peer) {
let b58Str
if (typeof peer === 'string') {
if (peer.startsWith('/')) {
b58Str = Multiaddr(peer).getPeerId()
} else {
b58Str = peer
}
} else if (Buffer.isBuffer(peer)) {
b58Str = bs58.encode(peer).toString()
} else if (PeerId.isPeerId(peer)) {
b58Str = peer.toB58String()
} else if (PeerInfo.isPeerInfo(peer)) {
b58Str = peer.id.toB58String()
} else if (Multiaddr.isMultiaddr(peer)) {
b58Str = peer.getPeerId()
} else {
throw new Error('not valid PeerId or PeerInfo, or B58Str')
}
return b58Str
}
constructor(addrs, id) {
super()
if (Id.isPeerId(id)) {
this.id = id.toB58String()
this._id = id
} else {
this._id = new Id(Buffer.from(id))
this.id = this._id.toB58String()
}
if (PeerInfo.isPeerInfo(addrs)) {
this.pi = addrs
} else {
this.pi = new PeerInfo(this._id)
addrs.map(addr => multiaddr.isMultiaddr(addr) ? addr : multiaddr(addr)).forEach(addr => this.pi.multiaddrs.add(addr))
}
this.addrs = this.pi.multiaddrs.toArray().map(a => a.toString())
log("created peer %s with address(es) %s", this.id, this.addrs.join(", "))
this.zites = {}
this.score = 0