Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function encrypt (localId, conn, remoteId) {
const { reader, writer, rest } = handshake(conn)
writer.push(lpEncodeExchange({
id: localId.toBytes(),
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.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()