Module @libp2p/interface-connection-encrypter - v4.0.1

@libp2p/interface-connection-encrypter

libp2p.io Discuss codecov CI

Connection Encrypter interface for libp2p

Table of contents

Install

$ npm i @libp2p/interface-connection-encrypter

Modules that implement the interface

API

  • Crypto
    • protocol<string>: The protocol id of the crypto module.
    • secureInbound<function(PeerId, duplex)>: Secures inbound connections.
    • secureOutbound<function(PeerId, duplex, PeerId)>: Secures outbound connections.

Secure Inbound

  • const { conn, remotePeer } = await crypto.secureInbound(localPeer, duplex, [remotePeer])

Secures an inbound streaming iterable duplex connection. It returns an encrypted streaming iterable duplex, as well as the PeerId of the remote peer.

Parameters

  • localPeer is the PeerId of the receiving peer.
  • duplex is the streaming iterable duplex that will be encryption.
  • remotePeer is the optional PeerId of the initiating peer, if known. This may only exist during transport upgrades.

Return Value

Secure Outbound

  • const { conn, remotePeer } = await crypto.secureOutbound(localPeer, duplex, remotePeer)

Secures an outbound streaming iterable duplex connection. It returns an encrypted streaming iterable duplex, as well as the PeerId of the remote peer.

Parameters

  • localPeer is the PeerId of the receiving peer.
  • duplex is the streaming iterable duplex that will be encrypted.
  • remotePeer is the PeerId of the remote peer. If provided, implementations should use this to validate the integrity of the remote peer.

Return Value

  • <object>
    • conn<duplex>: An encrypted streaming iterable duplex.
    • remotePeer<PeerId>: The PeerId of the remote peer. This should match the remotePeer parameter, and implementations should enforce this.

Crypto Errors

Common crypto errors come with the interface, and can be imported directly. All Errors take an optional message.

const {
InvalidCryptoExchangeError,
InvalidCryptoTransmissionError,
UnexpectedPeerError
} = require('libp2p-interfaces/src/crypto/errors')

const error = new UnexpectedPeerError('a custom error message')
console.log(error.code === UnexpectedPeerError.code) // true

Error Types

  • InvalidCryptoExchangeError - Should be thrown when a peer provides data that is insufficient to finish the crypto exchange.
  • InvalidCryptoTransmissionError - Should be thrown when an error occurs during encryption/decryption.
  • UnexpectedPeerError - Should be thrown when the expected peer id does not match the peer id determined via the crypto exchange.

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Index

Modules

Interfaces