import { OutPoint } from "@node-lightning/core" ; import { IGossipEmitter , IWireMessage , MessageType } from "@node-lightning/wire" ; import { ChannelAnnouncementMessage } from "@node-lightning/wire" ; import { ChannelUpdateMessage } from "@node-lightning/wire" ; import { NodeAnnouncementMessage } from "@node-lightning/wire" ; import { EventEmitter } from "events" ; import { Channel } from "./channel" ; import { ChannelSettings } from "./channel-settings" ; import { channelFromMessage } from "./deserialize/channel-from-message" ; import { channelSettingsFromMessage } from "./deserialize/channel-settings-from-message" ; import { Graph } from "./graph" ; import { ChannelNotFoundError } from "./graph-error" ; import { GraphError } from "./graph-error" ; import { Node } from "./node" ; export declare interface GraphManager { on ( event , fn ) : this ; on ( event , fn ) : this ; on ( event , fn ) : this ; on ( event , fn ) : this ; } /**
 * GraphManager is a facade around a Graph object. It converts in-bound
 * gossip messages from the wire into a graph representation. Channels
 * can also be removed by monitoring the block chain via a chainmon object.
 */ export class GraphManager extends EventEmitter { public graph : Graph ; public gossipEmitter : IGossipEmitter ; constructor ( gossipManager , graph = new Graph ( ) ) { super ( ) ; this . graph = graph ; this . gossipEmitter = gossipManager ; this . gossipEmitter . on ( "message" , this . _onMessage . bind ( this ) ) ; } param outpoint public removeChannel ( outpoint ) { const outpointStr = outpoint . toString ( ) ; for ( const channel of this . graph . channels . values ( ) ) { if ( outpointStr === channel ?. channelPoint ?. toString ( ) ) { this . graph . removeChannel ( channel ) ; this . emit ( "channel_closed" , channel ) ; return ; } } } private _onMessage ( msg ) { if ( isChannelAnnouncment ( msg ) ) { const channel = channelFromMessage ( msg ) ; if ( this . graph . getChannel ( msg . shortChannelId ) ) { return ; } if ( ! this . graph . getNode ( msg . nodeId1 ) ) { const node1 = new Node ( ) ; node1 . nodeId = msg . nodeId1 ; this . graph . addNode ( node1 ) ; this . emit ( "node" , node1 ) ; } if ( ! this . graph . getNode ( msg . nodeId2 ) ) { const node2 = new Node ( ) ; node2 . nodeId = msg . nodeId2 ; this . graph . addNode ( node2 ) ; this . emit ( "node" , node2 ) ; } this . graph . addChannel ( channel ) ; this . emit ( "channel" , channel ) ; return ; } if ( isChannelUpdate ( msg ) ) { const channel = this . graph . getChannel ( msg . shortChannelId ) ; if ( ! channel ) { this . emit ( "error" , new ChannelNotFoundError ( msg . shortChannelId ) ) ; return ; } const settings = channelSettingsFromMessage ( msg ) ; channel . updateSettings ( settings ) ; this . emit ( "channel_update" , channel , settings ) ; return ; } if ( isNodeAnnouncement ( msg ) ) { let node = this . graph . getNode ( msg . nodeId ) ; if ( ! node ) { node = new Node ( ) ; node . nodeId = msg . nodeId ; this . graph . addNode ( node ) ; } node . features = msg . features ; node . lastUpdate = msg . timestamp ; node . alias = msg . alias ; node . rgbColor = msg . rgbColor ; node . addresses = msg . addresses ; this . emit ( "node" , node ) ; } } } function isChannelAnnouncment ( msg ) { return msg . type === MessageType . ChannelAnnouncement ; } function isChannelUpdate ( msg ) { return msg . type === MessageType . ChannelUpdate ; } function isNodeAnnouncement ( msg ) { return msg . type === MessageType . NodeAnnouncement ; }