Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pubkey: {
Type: KeyType.RSA, // TODO: dont hard code
Data: localId.marshalPubKey()
}
}))
log('write pubkey exchange to peer %j', remoteId)
// Get the Exchange message
const response = (await lp.decode.fromReader(shake.reader).next()).value
const id = Exchange.decode(response.slice())
log('read pubkey exchange from peer %j', remoteId)
let peerId
try {
peerId = await PeerId.createFromPubKey(id.pubkey.Data)
} catch (err) {
log.error(err)
throw new InvalidCryptoExchangeError('Remote did not provide its public key')
}
if (remoteId && !peerId.isEqual(remoteId)) {
throw new UnexpectedPeerError()
}
log('plaintext key exchange completed successfully with peer %j', peerId)
shake.rest()
return {
conn: shake.stream,
remotePeer: peerId
}
let message
try {
message = Message.decode(data)
} catch (err) {
throw errCode(err, codes.ERR_INVALID_MESSAGE)
}
let {
publicKey,
listenAddrs,
protocols,
observedAddr
} = message
const id = await PeerId.createFromPubKey(publicKey)
const peerInfo = new PeerInfo(id)
if (connection.remotePeer.toString() !== id.toString()) {
throw errCode(new Error('identified peer does not match the expected peer'), codes.ERR_INVALID_PEER)
}
// Get the observedAddr if there is one
observedAddr = IdentifyService.getCleanMultiaddr(observedAddr)
// Copy the listenAddrs and protocols
IdentifyService.updatePeerAddresses(peerInfo, listenAddrs)
IdentifyService.updatePeerProtocols(peerInfo, protocols)
this.registrar.peerStore.replace(peerInfo)
// TODO: Track our observed address so that we can score it
log('received observed address of %s', observedAddr)
}
collect((err, data) => {
if (err) {
return callback(err)
}
// connection got closed graciously
if (data.length === 0) {
return callback(new Error('conn was closed, did not receive data'))
}
const input = msg.decode(data[0])
PeerId.createFromPubKey(input.publicKey, (err, id) => {
if (err) {
return callback(err)
}
const peerInfo = new PeerInfo(id)
if (expectedPeerInfo && expectedPeerInfo.id.toB58String() !== id.toB58String()) {
return callback(new Error('invalid peer'))
}
try {
input.listenAddrs
.map(multiaddr)
.forEach((ma) => peerInfo.multiaddrs.add(ma))
} catch (err) {
return callback(err)
}
Data: localId.marshalPubKey()
}
}))
log('write pubkey exchange to peer %j', remoteId)
// Get the Exchange message
const response = (await lp.decodeFromReader(reader).next()).value
const id = Exchange.decode(response.slice())
log('read pubkey exchange from peer %j', remoteId)
if (!id || !id.pubkey) {
throw new Error('Remote did not provide their public key')
}
const peerId = await PeerId.createFromPubKey(id.pubkey.Data)
if (remoteId && !peerId.isEqual(remoteId)) {
throw new Error('Remote peer id does not match known target id')
}
log('crypto exchange completed successfully: %j', peerId)
writer.end()
return {
conn: rest,
remotePeer: peerId
}
}
const embedPublicKey = async (publicKey, entry) => {
if (!publicKey || !publicKey.bytes || !entry) {
const error = new Error('one or more of the provided parameters are not defined')
log.error(error)
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
}
// Create a peer id from the public key.
let peerId
try {
peerId = await PeerId.createFromPubKey(publicKey.bytes)
} catch (err) {
throw errCode(err, ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY)
}
// Try to extract the public key from the ID. If we can, no need to embed it
let extractedPublicKey
try {
extractedPublicKey = extractPublicKeyFromId(peerId)
} catch (err) {
log.error(err)
throw errCode(err, ERRORS.ERR_PUBLIC_KEY_FROM_ID)
}
if (extractedPublicKey) {
return null
}
module.exports.pubKeyToPeerId = pubKey => {
if (typeof pubKey === 'string') {
pubKey = Buffer.from(pubKey.replace(/0x/, ''), 'hex')
}
if (!Buffer.isBuffer(pubKey)) throw Error(`Unable to parse public key to desired representation. Got ${pubKey.toString()}.`)
if (pubKey.length != COMPRESSED_PUBLIC_KEY_LENGTH)
throw Error(`Invalid public key. Expected a buffer of size ${COMPRESSED_PUBLIC_KEY_LENGTH} bytes. Got one of ${pubKey.length} bytes.`)
pubKey = new libp2p_crypto.supportedKeys.secp256k1.Secp256k1PublicKey(pubKey)
return PeerId.createFromPubKey(pubKey.bytes)
}
function getIdAndValidate (pub, id, cb) {
Id.createFromPubKey(Buffer.from(pub, 'hex'), (err, _id) => {
if (err) {
return cb(new Error('Crypto error'))
}
if (_id.toB58String() !== id) {
return cb(new Error('Id is not matching'))
}
return cb(null, crypto.keys.unmarshalPublicKey(Buffer.from(pub, 'hex')))
})
}
pull.asyncMap((pubKey, cb) => PeerId.createFromPubKey(pubKey, cb)),
pull.filter(peerId => {
(pubKey, cb) => PeerId.createFromPubKey(pubKey, cb),
(peerId, cb) => {