forked from alexbosworth/ln-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_from_route_hint.js
80 lines (66 loc) · 2.35 KB
/
route_from_route_hint.js
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
70
71
72
73
74
75
76
77
78
79
80
const {chanFormat} = require('bolt07');
const {isArray} = Array;
/** Get a route from lnd raw route hints
{
destination: <Destination Public Key Hex String>
hop_hints: [{
fee_base_msat: <Hop Base Fee Millitokens>
channel: <Numeric Format Hop Channel Id String>
cltv_delta: <Hop CLTV Delta Number>
fee_rate: <Hop Fee Rate Number>
node_id: <Hop Public Key Hex String>
}]
}
@throws
<ExpectedPaymentRequestDestinationToCalculateRoute Error>
<ExpectedRouteHopBaseFeeInRouteHint Error>
<ExpectedRouteHopChannelIdInRouteHint Error>
<ExpectedRouteHopCltvExpiryDeltaInRouteHint Error>
<ExpectedRouteHopFeeRateInRouteHint Error>
<ExpectedRouteHopHints Error>
<ExpectedRouteHopHintsInRoute Error>
<ExpectedRouteHopPublicKeyInRouteHint Error>
@returns
[{
[base_fee_mtokens]: <Base Routing Fee In Millitokens String>
[channel]: <Standard Format Channel Id String>
[cltv_delta]: <CLTV Blocks Delta Number>
[fee_rate]: <Fee Rate In Millitokens Per Million Number>
public_key: <Forward Edge Public Key Hex String>
}]
*/
module.exports = args => {
if (!args.destination) {
throw new Error('ExpectedPaymentRequestDestinationToCalculateRoute');
}
if (!isArray(args.hop_hints) || !args.hop_hints.length) {
throw new Error('ExpectedRouteHopHints');
}
const [firstHint] = args.hop_hints;
const lastHop = {node_id: args.destination};
const lastHops = args.hop_hints.map((hop, i, hops) => {
if (!hop.chan_id) {
throw new Error('ExpectedRouteHopChannelIdInRouteHint');
}
if (hop.cltv_expiry_delta === undefined) {
throw new Error('ExpectedRouteHopCltvExpiryDeltaInRouteHint');
}
if (hop.fee_base_msat === undefined) {
throw new Error('ExpectedRouteHopBaseFeeInRouteHint');
}
if (hop.fee_proportional_millionths === undefined) {
throw new Error('ExpectedRouteHopFeeRateInRouteHint');
}
if (!hop.node_id) {
throw new Error('ExpectedRouteHopPublicKeyInRouteHint');
}
return {
base_fee_mtokens: hop.fee_base_msat.toString(),
channel: chanFormat({number: hop.chan_id}).channel,
cltv_delta: hop.cltv_expiry_delta,
fee_rate: hop.fee_proportional_millionths,
public_key: (hops[(i + [hop].length)] || lastHop).node_id,
};
});
return [].concat([{public_key: firstHint.node_id}]).concat(lastHops);
};