Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Peer

Represents a remote peer and manages a TCP socket and incoming/outgoing communication with that peer.

Hierarchy

  • EventEmitter
    • Peer

Index

Constructors

Properties

Accessors

Methods

Constructors

constructor

Properties

Private Optional _alias

_alias: undefined | string

Private Optional _nodePubKey

_nodePubKey: undefined | string

The node pub key of this peer.

Private Optional _version

_version: undefined | string

The version of xud this peer is using.

active

active: boolean = false

Whether the peer is included in the p2p pool list of peers and will receive broadcasted packets.

Private activeCurrencies

activeCurrencies: Set<string> = new Set<string>()

Currencies that we have verified we can swap with this peer.

Private activePairs

activePairs: Set<string> = new Set<string>()

Trading pairs advertised by this peer which we have verified that we can swap.

address

address: Address

The socket address for the connection to this peer.

Private Optional checkPairsTimer

checkPairsTimer: NodeJS.Timer

Private connectTime

connectTime: number

The epoch time in ms when we connected to this peer.

Private connectionRetriesRevoked

connectionRetriesRevoked: boolean = false

disabledCurrencies

disabledCurrencies: Set<string> = new Set<string>()

Currencies that we cannot swap because we are missing a swap client identifier or because the peer's token identifier for this currency does not match ours - for example this may happen because a peer is using a different token contract address for a currency than we are.

Optional discoverTimer

discoverTimer: NodeJS.Timer

Timer to periodically call getNodes #402

Optional expectedNodePubKey

expectedNodePubKey: undefined | string

Private Readonly framer

framer: Framer

inbound

inbound: boolean

Private logger

logger: Logger

Private Readonly network

network: Network

Private Optional nodeState

nodeState: NodeState

Private Optional outEncryptionKey

outEncryptionKey: Buffer

Private Readonly parser

parser: Parser

Private Optional pingTimer

pingTimer: NodeJS.Timer

Optional recvDisconnectionReason

recvDisconnectionReason: DisconnectionReason

The reason this peer disconnected from us.

Private Readonly responseMap

responseMap: Map<string, PendingResponseEntry> = new Map()

Private Optional retryConnectionTimer

retryConnectionTimer: NodeJS.Timer

Timer to retry connection to peer after the previous attempt failed.

Optional sentDisconnectionReason

sentDisconnectionReason: DisconnectionReason

The reason we told this peer we disconnected from it.

Private Optional sessionInitPacket

sessionInitPacket: packets.SessionInitPacket

Private Optional socket

socket: Socket

Private Optional stallTimer

stallTimer: NodeJS.Timer

Private status

status: PeerStatus = PeerStatus.New

Static Private Readonly CHECK_PAIRS_INTERVAL

CHECK_PAIRS_INTERVAL: 60000 = 60000

Interval for checking if we can reactivate any inactive pairs with peers.

Static Private Readonly CONNECTION_RETRIES_MAX_DELAY

CONNECTION_RETRIES_MAX_DELAY: 300000 = 300000

Connection retries max delay.

Static Private Readonly CONNECTION_RETRIES_MAX_PERIOD

CONNECTION_RETRIES_MAX_PERIOD: 604800000 = 604800000

Connection retries max period.

Static Private Readonly CONNECTION_RETRIES_MIN_DELAY

CONNECTION_RETRIES_MIN_DELAY: 5000 = 5000

Connection retries min delay.

Static Private Readonly PING_INTERVAL

PING_INTERVAL: 30000 = 30000

Interval for pinging peers.

Static Private Readonly RESPONSE_TIMEOUT

RESPONSE_TIMEOUT: 10000 = 10000

Response timeout for response packets.

Static Readonly STALL_INTERVAL

STALL_INTERVAL: 5000 = 5000

Interval to check required responses from peer.

Static defaultMaxListeners

defaultMaxListeners: number

Static Readonly errorMonitor

errorMonitor: keyof symbol

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.

Accessors

addresses

  • get addresses(): Address[] | undefined

advertisedPairs

  • get advertisedPairs(): string[]
  • Returns a list of trading pairs advertised by this peer.

    Returns string[]

alias

  • get alias(): string | undefined

connected

  • get connected(): boolean

connextIdentifier

  • get connextIdentifier(): string | undefined

info

label

  • get label(): string

nodePubKey

  • get nodePubKey(): string | undefined
  • set nodePubKey(nodePubKey: string | undefined): void
  • The hex-encoded node public key for this peer, or undefined if it is still not known.

    Returns string | undefined

  • The hex-encoded node public key for this peer, or undefined if it is still not known.

    Parameters

    • nodePubKey: string | undefined

    Returns void

version

  • get version(): string
  • The version of xud this peer is using, or an empty string if it is still not known.

    Returns string

Methods

Private ackSession

activateCurrency

  • activateCurrency(currency: string): Set<string>

activatePair

  • activatePair(pairId: string): Promise<void>
  • Activates a trading pair with this peer.

    Parameters

    • pairId: string

    Returns Promise<void>

addListener

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

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

          • Rest ...args: any[]

          Returns void

    Returns this

Private addResponseTimeout

  • addResponseTimeout(reqId: string, resType: ResponseType, timeout: number): void

Private authenticateSessionInit

  • authenticateSessionInit(packet: SessionInitPacket, nodePubKey: string, expectedNodePubKey?: undefined | string): Promise<void>
  • Authenticates the identity of a peer with a SessionInitPacket and sets the peer's node state. Throws an error and closes the peer if authentication fails.

    Parameters

    • packet: SessionInitPacket

      the session init packet

    • nodePubKey: string

      our node pub key

    • Optional expectedNodePubKey: undefined | string

      the expected node pub key of the sender of the init packet

    Returns Promise<void>

Private beginHandshake

beginOpen

  • beginOpen(__namedParameters: { expectedNodePubKey: undefined | string; ownNodeKey: NodeKey; ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }; ownVersion: string; retryConnecting: boolean; torport: number }): Promise<SessionInitPacket>
  • Prepares a peer for use by establishing a socket connection and beginning the handshake.

    Parameters

    • __namedParameters: { expectedNodePubKey: undefined | string; ownNodeKey: NodeKey; ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }; ownVersion: string; retryConnecting: boolean; torport: number }
      • expectedNodePubKey: undefined | string
      • ownNodeKey: NodeKey
      • ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }
        • addresses: Address[]

          This node's listening external socket addresses to advertise to peers.

        • connextIdentifier: string
        • lndPubKeys: {}

          An object mapping currency symbols to lnd pub keys.

          • [currency: string]: string | undefined
        • lndUris: {}

          An object mapping currency symbols to lnd listening uris

          • [currency: string]: string[] | undefined
        • pairs: string[]
        • tokenIdentifiers: {}

          An object mapping currency symbols to token identifiers such as lnd chains or token contract addresses.

          • [currency: string]: string | undefined
      • ownVersion: string
      • retryConnecting: boolean
      • torport: number

    Returns Promise<SessionInitPacket>

    the session init packet from beginning the handshake

Private bindParser

  • bindParser(parser: Parser): void

Private bindSocket

  • bindSocket(): void
  • Binds listeners to a newly connected socket for error, close, and data events.

    Returns void

Private checkTimeout

  • checkTimeout(): Promise<void>
  • Potentially timeout peer if it hasn't responded.

    Returns Promise<void>

close

  • Close a peer by ensuring the socket is destroyed and terminating all timers.

    Parameters

    Returns Promise<void>

Private completeHandshake

completeOpen

  • Finishes opening a peer for use by marking the peer as opened, completing the handshake, and setting up the ping packet timer.

    Parameters

    • ownNodeState: NodeState

      our node state data to send to the peer

    • ownNodeKey: NodeKey

      our identity node key

    • ownVersion: string

      the version of xud we are running

    • sessionInit: SessionInitPacket

      the session init packet we received when beginning the handshake

    Returns Promise<void>

Private createSessionInitPacket

  • createSessionInitPacket(__namedParameters: { ephemeralPubKey: string; expectedNodePubKey: string; ownNodeKey: NodeKey; ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }; ownVersion: string }): SessionInitPacket
  • Parameters

    • __namedParameters: { ephemeralPubKey: string; expectedNodePubKey: string; ownNodeKey: NodeKey; ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }; ownVersion: string }
      • ephemeralPubKey: string
      • expectedNodePubKey: string
      • ownNodeKey: NodeKey
      • ownNodeState: { addresses: Address[]; connextIdentifier: string; lndPubKeys: {}; lndUris: {}; pairs: string[]; tokenIdentifiers: {} }
        • addresses: Address[]

          This node's listening external socket addresses to advertise to peers.

        • connextIdentifier: string
        • lndPubKeys: {}

          An object mapping currency symbols to lnd pub keys.

          • [currency: string]: string | undefined
        • lndUris: {}

          An object mapping currency symbols to lnd listening uris

          • [currency: string]: string[] | undefined
        • pairs: string[]
        • tokenIdentifiers: {}

          An object mapping currency symbols to token identifiers such as lnd chains or token contract addresses.

          • [currency: string]: string | undefined
      • ownVersion: string

    Returns SessionInitPacket

deactivateCurrency

  • deactivateCurrency(currency: string): void

deactivatePair

  • deactivatePair(pairId: string): void
  • Deactivates a trading pair with this peer.

    Parameters

    • pairId: string

    Returns void

disableCurrency

  • disableCurrency(currency: string): void

discoverNodes

  • discoverNodes(): Promise<number>

emit

  • emit(event: "connect"): boolean
  • emit(event: "reputation", reputationEvent: ReputationEvent): boolean
  • emit(event: "close"): boolean
  • emit(event: "packet", packet: Packet): boolean
  • emit(event: "verifyPairs"): boolean
  • emit(event: "pairDropped", pairId: string): boolean
  • emit(event: "nodeStateUpdate"): boolean
  • Parameters

    • event: "connect"

    Returns boolean

  • Parameters

    Returns boolean

  • Parameters

    • event: "close"

    Returns boolean

  • Parameters

    • event: "packet"
    • packet: Packet

    Returns boolean

  • Notifies listeners that the peer's advertised but inactive pairs should be verified.

    Parameters

    • event: "verifyPairs"

    Returns boolean

  • Notifies listeners that a previously active pair was dropped by the peer or deactivated.

    Parameters

    • event: "pairDropped"
    • pairId: string

    Returns boolean

  • Parameters

    • event: "nodeStateUpdate"

    Returns boolean

enableCurrency

  • enableCurrency(currency: string): void

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

Private fulfillResponseEntry

  • fulfillResponseEntry(packet: Packet): boolean
  • Fulfill a pending response entry for solicited responses, penalize unsolicited responses.

    Parameters

    Returns boolean

    false if no pending response entry exists for the provided key, otherwise true

getAdvertisedCurrencies

  • getAdvertisedCurrencies(): Set<string>

getIdentifier

  • getIdentifier(clientType: SwapClientType, currency?: undefined | string): string | undefined

getLndPubKey

  • getLndPubKey(currency?: undefined | string): string | undefined
  • Parameters

    • Optional currency: undefined | string

    Returns string | undefined

getLndUris

  • getLndUris(currency: string): string[] | undefined
  • Gets lnd client's listening uris for the provided currency.

    Parameters

    • currency: string

    Returns string[] | undefined

getMaxListeners

  • getMaxListeners(): number

Private getOrAddPendingResponseEntry

getStatus

  • getStatus(): string

getTokenIdentifier

  • getTokenIdentifier(currency: string): string | undefined

Private handleDisconnecting

Private handleNodeStateUpdate

Private handlePacket

  • handlePacket(packet: Packet): Promise<void>

Private handlePing

Private handleSessionInit

Private initConnection

  • initConnection(retry?: boolean, torport: number): Promise<unknown>
  • Ensure we are connected (for inbound connections) or listen for the connect socket event (for outbound connections) and set the connectTime timestamp. If an outbound connection attempt errors or times out, throw an error.

    Parameters

    • Default value retry: boolean = false
    • torport: number

    Returns Promise<unknown>

Private initSession

  • initSession(ownNodeState: NodeState, ownNodeKey: NodeKey, ownVersion: string, expectedNodePubKey: string): Promise<void>

Private initStall

  • initStall(): void

isCurrencyActive

  • isCurrencyActive(currency: string): boolean

Private isPacketSolicited

  • isPacketSolicited(packet: Packet): Promise<boolean>
  • Checks if a given packet is solicited and fulfills the pending response entry if it's a response.

    Parameters

    Returns Promise<boolean>

isPairActive

  • isPairActive(pairId: string): boolean

listenerCount

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

    • type: 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): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: "packet", listener: (packet: Packet) => void): this
  • on(event: "reputation", listener: (event: ReputationEvent) => void): this
  • on(event: "verifyPairs", listener: () => void): this
  • on(event: "pairDropped", listener: (pairId: string) => void): this
  • on(event: "nodeStateUpdate", listener: () => void): this
  • Parameters

    • event: "packet"
    • listener: (packet: Packet) => void
        • Parameters

          Returns void

    Returns this

  • Parameters

    Returns this

  • Adds a listener to be called when the peer's advertised but inactive pairs should be verified.

    Parameters

    • event: "verifyPairs"
    • listener: () => void
        • (): void
        • Returns void

    Returns this

  • Adds a listener to be called when a previously active pair is dropped by the peer or deactivated.

    Parameters

    • event: "pairDropped"
    • listener: (pairId: string) => void
        • (pairId: string): void
        • Parameters

          • pairId: string

          Returns void

    Returns this

  • Parameters

    • event: "nodeStateUpdate"
    • listener: () => void
        • (): void
        • Returns void

    Returns this

once

  • once(event: "close", listener: () => void): this

prependListener

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

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

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

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

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

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

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

    • event: string | symbol

    Returns Function[]

removeAllListeners

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

    • Optional event: string | symbol

    Returns this

removeListener

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

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

          • Rest ...args: any[]

          Returns void

    Returns this

revokeConnectionRetries

  • revokeConnectionRetries(): void

sendGetNodes

sendNodes

sendOrders

  • sendOrders(orders: OutgoingOrder[], reqId: string): Promise<void>

sendPacket

  • sendPacket(packet: Packet): Promise<void>

Private sendPing

  • sendPing(): Promise<void>

Private sendPong

  • sendPong(pingId: string): Promise<void>

setIdentifiers

  • setIdentifiers(nodePubKey: string): void
  • Sets public key and alias for this node together so that they are always in sync.

    Parameters

    • nodePubKey: string

    Returns void

Private setInEncryption

  • setInEncryption(key: Buffer): void

setMaxListeners

  • setMaxListeners(n: number): this
  • Parameters

    • n: number

    Returns this

Private setOutEncryption

  • setOutEncryption(key: Buffer): void

wait

  • wait(reqId: string, resType: ResponseType, timeout?: undefined | number, cb?: undefined | ((packet: Packet) => void)): Promise<Packet>
  • Waits for a packet to be received from peer.

    Parameters

    • reqId: string
    • resType: ResponseType
    • Optional timeout: undefined | number
    • Optional cb: undefined | ((packet: Packet) => void)

    Returns Promise<Packet>

    A promise that is resolved once the packet is received or rejects on timeout.

Private waitSessionInit

Static fromInbound

Static listenerCount

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

    since v4.0.0

    Parameters

    • emitter: EventEmitter
    • event: string | symbol

    Returns number

Generated using TypeDoc