How to use the it-length-prefixed.decodeFromReader function in it-length-prefixed

To help you get started, we’ve selected a few it-length-prefixed examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libp2p / js-libp2p / src / crypto / plaintext.js View on Github external
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()

it-length-prefixed

Streaming length prefixed buffers with async iterables

Apache-2.0 OR MIT
Latest version published 4 months ago

Package Health Score

71 / 100
Full package analysis