-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrpc_route_from_route.js
107 lines (93 loc) · 3.25 KB
/
rpc_route_from_route.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const rpcHopFromHop = require('./rpc_hop_from_hop');
const isNumber = n => !isNaN(n);
/** RPC formatted route from a route
{
fee: <Route Fee Tokens Number>
fee_mtokens: <Route Fee Millitokens String>
hops: [{
channel: <Standard Format Channel Id String>
channel_capacity: <Channel Capacity Tokens Number>
fee: <Fee Number>
fee_mtokens: <Fee Millitokens String>
forward: <Forward Tokens Number>
forward_mtokens: <Forward Millitokens String>
[messages]: [{
type: <Message Type Number String>
value: <Message Raw Value Hex Encoded String>
}]
[public_key]: <Forward Edge Public Key Hex String>
timeout: <Timeout Block Height Number>
}]
[messages]: [{
type: <Message Type Number String>
value: <Message Raw Value Hex Encoded String>
}]
mtokens: <Total Fee-Inclusive Millitokens String>
[payment]: <Payment Identifier Hex String>
timeout: <Timeout Block Height Number>
tokens: <Total Fee-Inclusive Tokens Number>
[total_mtokens]: <Total Millitokens String>
}
@throws
<Error>
@returns
{
hops: [{
amt_to_forward: <Tokens to Forward String>
amt_to_forward_msat: <Millitokens to Forward String>
chan_id: <Numeric Format Channel Id String>
chan_capacity: <Channel Capacity Number>
[custom_records]: {<TLV Type Number String>: <TLV Value Buffer Object>}
expiry: <Timeout Chain Height Number>
fee: <Fee in Tokens Number>
fee_msat: <Fee in Millitokens Number>
[pub_key]: <Next Hop Public Key Hex String>
tlv_payload: <Has Extra TLV Data Bool>
[mpp_record]: {
payment_addr: <Payment Identifier Buffer>
total_amt_msat: <Total Payment Millitokens Amount String>
}
}]
total_amt: <Total Tokens String>
total_amt_msat: <Route Total Millitokens String>
total_fees: <Route Fee Tokens String>
total_fees_msat: <Route Total Fees Millitokens String>
total_time_lock: <Route Total Timelock Number>
}
*/
module.exports = args => {
if (!isNumber(args.fee)) {
throw new Error('ExpectedFeeNumberToMapRouteToRpcRoute');
}
if (!isNumber(args.tokens)) {
throw new Error('ExpectedTokensNumberToMapRouteToRpcRoute');
}
const hops = args.hops.map(hop => rpcHopFromHop(hop));
const finalHopIndex = hops.length - 1;
const payAddress = !args.payment ? null : Buffer.from(args.payment, 'hex');
// Set the payment identifier and total amount in the TLV payload
if (!!args.payment || !!args.total_mtokens) {
hops[finalHopIndex].tlv_payload = true;
hops[finalHopIndex].mpp_record = {
payment_addr: payAddress || undefined,
total_amt_msat: args.total_mtokens || undefined,
};
}
// Set custom TLV payload records on the final hop
if (!!args.messages && !!args.messages.length) {
hops[finalHopIndex].tlv_payload = true;
hops[finalHopIndex].custom_records = args.messages.reduce((tlv, n) => {
tlv[n.type] = Buffer.from(n.value, 'hex');
return tlv;
},
hops[finalHopIndex].custom_records || {});
}
return {
hops,
total_amt: args.tokens.toString(),
total_amt_msat: args.mtokens,
total_fees: args.fee.toString(),
total_fees_msat: args.fee_mtokens,
total_time_lock: args.timeout,
};
};