Options
All
  • Public
  • Public/Protected
  • All
Menu

A DHT implementation modeled after Kademlia with S/Kademlia modifications. Original implementation in go: https://github.com/libp2p/go-libp2p-kad-dht.

Hierarchy

  • EventEmitter
    • KadDHT

Index

Constructors

constructor

  • new KadDHT(__namedParameters: { clientMode: boolean; concurrency: number; datastore: Datastore; dialer: any; forceProtocolLegacy: undefined | boolean; kBucketSize: number; libp2p: any; onPut: undefined | ((arg0: Record, arg1: PeerId) => void); onRemove: undefined | ((arg0: Record) => void); peerId: PeerId; peerStore: any; protocolPrefix: undefined | string; randomWalk: RandomWalkOptions; registrar: any; selectors: object; validators: object }): KadDHT
  • Create a new KadDHT.

    Parameters

    • __namedParameters: { clientMode: boolean; concurrency: number; datastore: Datastore; dialer: any; forceProtocolLegacy: undefined | boolean; kBucketSize: number; libp2p: any; onPut: undefined | ((arg0: Record, arg1: PeerId) => void); onRemove: undefined | ((arg0: Record) => void); peerId: PeerId; peerStore: any; protocolPrefix: undefined | string; randomWalk: RandomWalkOptions; registrar: any; selectors: object; validators: object }
      • clientMode: boolean
      • concurrency: number
      • datastore: Datastore
      • dialer: any
      • forceProtocolLegacy: undefined | boolean
      • kBucketSize: number
      • libp2p: any
      • onPut: undefined | ((arg0: Record, arg1: PeerId) => void)
      • onRemove: undefined | ((arg0: Record) => void)
      • peerId: PeerId
      • peerStore: any
      • protocolPrefix: undefined | string
      • randomWalk: RandomWalkOptions
      • registrar: any
      • selectors: object
      • validators: object

    Returns KadDHT

Properties

_clientMode

_clientMode: boolean

_log

_log: Debugger & { error: Debugger }

_queryManager

_queryManager: QueryManager

_running

_running: boolean

concurrency

concurrency: number

contentFetching

contentFetching: { _putLocal: any; get: any; getMany: any; put: any }

Type declaration

  • _putLocal: function
    • _putLocal(key: Uint8Array, rec: Uint8Array): Promise<void>
    • Store the given key/value pair locally, in the datastore.

      Parameters

      • key: Uint8Array
      • rec: Uint8Array

        encoded record

      Returns Promise<void>

  • get: function
    • get(key: Uint8Array, options?: undefined | { timeout: undefined | number }): Promise<Uint8Array>
    • Get the value to the given key. Times out after 1 minute by default.

      Parameters

      • key: Uint8Array
      • options: undefined | { timeout: undefined | number } = {}

      Returns Promise<Uint8Array>

  • getMany: function
    • getMany(key: Uint8Array, nvals: number, options?: undefined | { timeout: undefined | number }): Promise<{ from: PeerId; val: Uint8Array }[]>
    • Get the n values to the given key without sorting.

      Parameters

      • key: Uint8Array
      • nvals: number
      • options: undefined | { timeout: undefined | number } = {}

      Returns Promise<{ from: PeerId; val: Uint8Array }[]>

  • put: function
    • put(key: Uint8Array, value: Uint8Array, options?: undefined | { minPeers: undefined | number }): Promise<void>
    • Store the given key/value pair in the DHT.

      Parameters

      • key: Uint8Array
      • value: Uint8Array
      • options: undefined | { minPeers: undefined | number } = {}

      Returns Promise<void>

contentRouting

contentRouting: { findProviders: any; provide: any }

Type declaration

  • findProviders: function
    • findProviders(key: CID, options?: undefined | { maxNumProviders: undefined | number; timeout: undefined | number }): AsyncIterable<{ id: PeerId; multiaddrs: Multiaddr[] }>
    • Search the dht for up to K providers of the given CID.

      Parameters

      • key: CID
      • options: undefined | { maxNumProviders: undefined | number; timeout: undefined | number } = ...

      Returns AsyncIterable<{ id: PeerId; multiaddrs: Multiaddr[] }>

      }

  • provide: function
    • provide(key: CID): Promise<void>
    • Announce to the network that we can provide the value for a given key

      Parameters

      • key: CID

      Returns Promise<void>

datastore

datastore: Datastore

dialer

dialer: any

disjointPaths

disjointPaths: number

kBucketSize

kBucketSize: number

libp2p

libp2p: any

network

network: Network

onPut

onPut: (arg0: Record, arg1: PeerId) => void

Type declaration

    • (arg0: Record, arg1: PeerId): void
    • Parameters

      • arg0: Record
      • arg1: PeerId

      Returns void

onRemove

onRemove: (arg0: Record) => void

Type declaration

    • (arg0: Record): void
    • Parameters

      • arg0: Record

      Returns void

peerId

peerId: PeerId

peerRouting

peerRouting: { _findPeerSingle: any; findPeer: any; getClosestPeers: any; getPublicKey: any }

Type declaration

  • _findPeerSingle: function
    • _findPeerSingle(peer: PeerId, target: PeerId): Promise<Message>
    • Ask peer peer if they know where the peer with id target is.

      Parameters

      • peer: PeerId
      • target: PeerId

      Returns Promise<Message>

  • findPeer: function
    • findPeer(id: PeerId, options?: undefined | { timeout: undefined | number }): Promise<{ id: PeerId; multiaddrs: Multiaddr[] }>
    • Search for a peer with the given ID.

      Parameters

      • id: PeerId
      • options: undefined | { timeout: undefined | number } = ...

      Returns Promise<{ id: PeerId; multiaddrs: Multiaddr[] }>

      }

  • getClosestPeers: function
    • getClosestPeers(key: Uint8Array, options?: undefined | { shallow: undefined | boolean }): AsyncIterable<PeerId>
    • Kademlia 'node lookup' operation.

      Parameters

      • key: Uint8Array
      • options: undefined | { shallow: undefined | boolean } = ...

      Returns AsyncIterable<PeerId>

  • getPublicKey: function
    • getPublicKey(peer: PeerId): Promise<PublicKey>

peerStore

peerStore: any

protocol

protocol: string

providers

providers: Providers

randomWalk

randomWalk: RandomWalk

registrar

registrar: any

routingTable

routingTable: RoutingTable

selectors

selectors: { pk: (k: Uint8Array, records: Uint8Array[]) => number }

Type declaration

  • pk: (k: Uint8Array, records: Uint8Array[]) => number
      • (k: Uint8Array, records: Uint8Array[]): number
      • Parameters

        • k: Uint8Array
        • records: Uint8Array[]

        Returns number

validators

validators: { pk: { func: (key: Uint8Array, publicKey: Uint8Array) => Promise<void>; sign: boolean } }

Type declaration

  • pk: { func: (key: Uint8Array, publicKey: Uint8Array) => Promise<void>; sign: boolean }
    • func: (key: Uint8Array, publicKey: Uint8Array) => Promise<void>
        • (key: Uint8Array, publicKey: Uint8Array): Promise<void>
        • Parameters

          • key: Uint8Array
          • publicKey: Uint8Array

          Returns Promise<void>

    • sign: boolean

Static Readonly captureRejectionSymbol

captureRejectionSymbol: typeof captureRejectionSymbol

Static captureRejections

captureRejections: boolean

Sets or gets the default captureRejection value for all emitters.

Static defaultMaxListeners

defaultMaxListeners: number

Static Readonly errorMonitor

errorMonitor: typeof errorMonitor

This symbol shall be used to install a listener for only monitoring 'error' events. Listeners installed using this symbol are called before the regular 'error' listeners are called.

Installing a listener using this symbol does not change the behavior once an 'error' event is emitted, therefore the process will still crash if no regular 'error' listener is installed.

Static multicodec

multicodec: string

Accessors

isStarted

  • get isStarted(): boolean

Methods

_add

  • _add(peerId: PeerId): Promise<void>
  • Add the peer to the routing table and update it in the peerStore.

    Parameters

    • peerId: PeerId

    Returns Promise<void>

_betterPeersToQuery

  • _betterPeersToQuery(msg: Message, peerId: PeerId): Promise<{ id: PeerId; multiaddrs: Multiaddr[] }[]>
  • Get the nearest peers to the given query, but iff closer than self.

    Parameters

    • msg: Message
    • peerId: PeerId

    Returns Promise<{ id: PeerId; multiaddrs: Multiaddr[] }[]>

_checkLocalDatastore

  • _checkLocalDatastore(key: Uint8Array): Promise<undefined | Record>
  • Try to fetch a given record by from the local datastore. Returns the record iff it is still valid, meaning

    • it was either authored by this node, or
    • it was received less than MAX_RECORD_AGE ago.

    Parameters

    • key: Uint8Array

    Returns Promise<undefined | Record>

_getValueOrPeers

  • _getValueOrPeers(peer: PeerId, key: Uint8Array): Promise<{ peers: PeerData[]; record: Record } | { peers: PeerData[]; record: undefined }>
  • Query a particular peer for the value for the given key. It will either return the value or a list of closer peers.

    Note: The peerStore is updated with new addresses found for the given peer.

    Parameters

    • peer: PeerId
    • key: Uint8Array

    Returns Promise<{ peers: PeerData[]; record: Record } | { peers: PeerData[]; record: undefined }>

_getValueSingle

  • _getValueSingle(peer: PeerId, key: Uint8Array): Promise<Message>
  • Get a value via rpc call for the given parameters.

    Parameters

    • peer: PeerId
    • key: Uint8Array

    Returns Promise<Message>

_isSelf

  • _isSelf(other: PeerId): boolean
  • Is the given peer id our PeerId?

    Parameters

    • other: PeerId

    Returns boolean

_nearestPeersToQuery

  • _nearestPeersToQuery(msg: Message): Promise<{ id: PeerId; multiaddrs: Multiaddr[] }[]>
  • Returns the routing tables closest peers, for the key of the message.

    Parameters

    • msg: Message

    Returns Promise<{ id: PeerId; multiaddrs: Multiaddr[] }[]>

_peerDiscovered

  • _peerDiscovered(peerId: PeerId, multiaddrs: Multiaddr[]): void
  • Parameters

    • peerId: PeerId
    • multiaddrs: Multiaddr[]

    Returns void

_putLocal

  • _putLocal(key: Uint8Array, value: Uint8Array): Promise<void>
  • Parameters

    • key: Uint8Array
    • value: Uint8Array

    Returns Promise<void>

_putValueToPeer

  • _putValueToPeer(key: Uint8Array, rec: Uint8Array, target: PeerId): Promise<void>
  • Store the given key/value pair at the peer target.

    Parameters

    • key: Uint8Array
    • rec: Uint8Array

      encoded record

    • target: PeerId

    Returns Promise<void>

_verifyRecordLocally

  • _verifyRecordLocally(record: Record): Promise<void>
  • Verify a record without searching the DHT.

    Parameters

    • record: Record

    Returns Promise<void>

_verifyRecordOnline

  • _verifyRecordOnline(record: Record): Promise<void>
  • Verify a record, fetching missing public keys from the network. Calls back with an error if the record is invalid.

    Parameters

    • record: Record

    Returns Promise<void>

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): (string | symbol)[]
  • Returns (string | symbol)[]

findPeer

  • findPeer(id: PeerId, options?: undefined | { timeout: undefined | number }): Promise<{ id: PeerId; multiaddrs: Multiaddr[] }>
  • Search for a peer with the given ID.

    Parameters

    • id: PeerId
    • options: undefined | { timeout: undefined | number } = ...

    Returns Promise<{ id: PeerId; multiaddrs: Multiaddr[] }>

    }

findProviders

  • findProviders(key: CID, options?: undefined | { maxNumProviders: undefined | number; timeout: undefined | number }): AsyncIterable<{ id: PeerId; multiaddrs: Multiaddr[] }>
  • Search the dht for up to K providers of the given CID.

    Parameters

    • key: CID
    • options: undefined | { maxNumProviders: undefined | number; timeout: undefined | number } = ...

    Returns AsyncIterable<{ id: PeerId; multiaddrs: Multiaddr[] }>

    }

get

  • get(key: Uint8Array, options?: undefined | { timeout: undefined | number }): Promise<Uint8Array>
  • Get the value to the given key. Times out after 1 minute by default.

    Parameters

    • key: Uint8Array
    • options: undefined | { timeout: undefined | number } = {}

    Returns Promise<Uint8Array>

getClosestPeers

  • getClosestPeers(key: Uint8Array, options?: undefined | { shallow: undefined | boolean }): AsyncGenerator<PeerId, void, undefined>
  • Kademlia 'node lookup' operation.

    Parameters

    • key: Uint8Array
    • options: undefined | { shallow: undefined | boolean } = ...

    Returns AsyncGenerator<PeerId, void, undefined>

getMany

  • getMany(key: Uint8Array, nvals: number, options?: undefined | { timeout: undefined | number }): Promise<{ from: PeerId; val: Uint8Array }[]>
  • Get the n values to the given key without sorting.

    Parameters

    • key: Uint8Array
    • nvals: number
    • options: undefined | { timeout: undefined | number } = {}

    Returns Promise<{ from: PeerId; val: Uint8Array }[]>

getMaxListeners

  • getMaxListeners(): number
  • Returns number

getPublicKey

  • getPublicKey(peer: PeerId): Promise<PublicKey>
  • Get the public key for the given peer id.

    Parameters

    • peer: PeerId

    Returns Promise<PublicKey>

listenerCount

  • listenerCount(event: string | symbol): number
  • Parameters

    • event: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

off

  • off(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

on

  • on(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

once

  • once(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

provide

  • provide(key: CID): Promise<void>
  • Announce to the network that we can provide given key's value.

    Parameters

    • key: CID

    Returns Promise<void>

put

  • put(key: Uint8Array, value: Uint8Array, options?: undefined | { minPeers: undefined | number }): Promise<void>
  • Store the given key/value pair in the DHT.

    Parameters

    • key: Uint8Array
    • value: Uint8Array
    • options: undefined | { minPeers: undefined | number } = {}

    Returns Promise<void>

rawListeners

  • rawListeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): KadDHT
  • Parameters

    • Optional event: string | symbol

    Returns KadDHT

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): KadDHT
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns KadDHT

removeLocal

  • removeLocal(key: Uint8Array): Promise<undefined>
  • Remove the given key from the local datastore.

    Parameters

    • key: Uint8Array

    Returns Promise<undefined>

setMaxListeners

  • setMaxListeners(n: number): KadDHT
  • Parameters

    • n: number

    Returns KadDHT

start

  • start(): void
  • Start listening to incoming connections.

    Returns void

stop

  • stop(): void
  • Stop accepting incoming connections and sending outgoing messages.

    Returns void

Static getEventListener

  • getEventListener(emitter: DOMEventTarget | EventEmitter, name: string | symbol): Function[]
  • Returns a list listener for a specific emitter event name.

    Parameters

    • emitter: DOMEventTarget | EventEmitter
    • name: string | symbol

    Returns Function[]

Static listenerCount

  • listenerCount(emitter: EventEmitter, event: string | symbol): number
  • deprecated

    since v4.0.0

    Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

Static on

  • on(emitter: EventEmitter, event: string, options?: StaticEventEmitterOptions): AsyncIterableIterator<any>
  • Parameters

    • emitter: EventEmitter
    • event: string
    • Optional options: StaticEventEmitterOptions

    Returns AsyncIterableIterator<any>

Static once

  • once(emitter: NodeEventTarget, event: string | symbol, options?: StaticEventEmitterOptions): Promise<any[]>
  • once(emitter: DOMEventTarget, event: string, options?: StaticEventEmitterOptions): Promise<any[]>
  • Parameters

    • emitter: NodeEventTarget
    • event: string | symbol
    • Optional options: StaticEventEmitterOptions

    Returns Promise<any[]>

  • Parameters

    • emitter: DOMEventTarget
    • event: string
    • Optional options: StaticEventEmitterOptions

    Returns Promise<any[]>