Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async ipnsPublicKey(peer, parsed) {
let raw = peer.toBytes()
// Check if the public key is just embedded in the peer ID.
if (raw.length > 1 && raw[0] == 0x00) {
return keys.unmarshalPublicKey(raw)
}
// Extract the public key from the IPNS record.
return crypto.subtle.digest("SHA-256", parsed.pubKey).then((digest) => {
let mh = multihash.encode(Buffer.from(digest), "sha2-256")
if (!raw.equals(mh)) {
throw new Error("given unexpected public key")
}
return keys.unmarshalPublicKey(parsed.pubKey)
})
}
module.exports.addPubKey = async peerId => {
if (PeerId.isPeerId(peerId) && peerId.pubKey) return peerId
peerId.pubKey = await libp2p_crypto.unmarshalPublicKey(Multihash.decode(peerId.toBytes()).digest)
return peerId
}
serializedPeerInfos.map(async serializedPeerInfo => {
const peerId = PeerId.createFromBytes(serializedPeerInfo[0])
if (serializedPeerInfo.length === 3) {
peerId.pubKey = libp2p_crypto.unmarshalPublicKey(serializedPeerInfo[2])
}
const peerInfo = await PeerInfo.create(peerId)
serializedPeerInfo[1].forEach(multiaddr => peerInfo.multiaddrs.add(Multiaddr(multiaddr)))
peerBook.put(peerInfo)
})
)
return crypto.subtle.digest("SHA-256", parsed.pubKey).then((digest) => {
let mh = multihash.encode(Buffer.from(digest), "sha2-256")
if (!raw.equals(mh)) {
throw new Error("given unexpected public key")
}
return keys.unmarshalPublicKey(parsed.pubKey)
})
}