All files / lib/serializers lnd-serializer.ts

85.71% Statements 12/14
72.73% Branches 8/11
90% Functions 9/10
83.33% Lines 10/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69                        1x   1x 2x 1x                   3x       5x           8x             2x                         6x 5x                      
import { Address } from "@node-lightning/wire";
import { Channel } from "../channel";
import { ChannelSettings } from "../channel-settings";
import { Graph } from "../graph";
import { Node } from "../node";
 
/**
 * Performs JSON serialization of the graph in the same format
 * as used by LND and defined in LND API documentation:
 *
 * https://api.lightning.community/#simple-rpc-33
 */
export class LndSerializer {
    public toObject(g: Graph) {
        return {
            nodes: Array.from(g.nodes.values()).map(node => this.serializeNode(node)),
            edges: Array.from(g.channels.values()).map(chan => this.serializeChannel(chan)),
        };
    }
 
    public toJSON(g: Graph, format: boolean = true) {
        const obj = this.toObject(g);
        return format ? JSON.stringify(obj, null, 2) : JSON.stringify(obj);
    }
 
    public serializeNode(node: Node) {
        return {
            last_update: node.lastUpdate,
            pub_key: node.nodeId.toString("hex"),
            alias: node.aliasString,
            addresses: node.addresses.filter(p => p).map(address => this.serializeAddress(address)),
            color: node.rgbColorString,
        };
    }
 
    public serializeAddress(address: Address) {
        return {
            network: "tcp",
            addr: address.toString(),
        };
    }
 
    public serializeChannel(chan: Channel) {
        return {
            channel_id: chan.shortChannelId.toNumber().toString(),
            chan_point: chan.channelPoint && chan.channelPoint.toString(),
            last_update: chan.lastUpdate,
            node1_pub: chan.nodeId1.toString("hex"),
            node2_pub: chan.nodeId2.toString("hex"),
            capacity: chan.capacity && chan.capacity.toString(),
            node1_policy: this.serializeRoutingPolicy(chan.node1Settings),
            node2_policy: this.serializeRoutingPolicy(chan.node2Settings),
        };
    }
 
    public serializeRoutingPolicy(policy: ChannelSettings) {
        if (!policy) return null;
        return {
            time_lock_delta: policy.cltvExpiryDelta,
            min_htlc: policy.htlcMinimumMsat.toString(),
            fee_base_msat: policy.feeBaseMsat.toString(),
            fee_rate_milli_msat: policy.feeProportionalMillionths.toString(),
            disabled: policy.disabled,
            max_htlc_msat: policy.htlcMaximumMsat ? policy.htlcMaximumMsat.toString() : "0",
            last_update: policy.timestamp,
        };
    }
}