lightning 4.13.3 → 4.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Versions
2
2
 
3
+ ## 4.14.0
4
+
5
+ - `getPayment`, `subscribeToPastPayment`: Add `pending` for pending payment details
6
+
3
7
  ## 4.13.3
4
8
 
5
9
  - Add `sendMessageToPeer` to send a peer to peer message to a connected peer
@@ -1,5 +1,6 @@
1
1
  const {confirmedFromPayment} = require('./../../lnd_responses');
2
2
  const {failureFromPayment} = require('./../../lnd_responses');
3
+ const {pendingFromPayment} = require('./../../lnd_responses');
3
4
  const {states} = require('./payment_states');
4
5
 
5
6
  /** Emit payment from payment event
@@ -21,7 +22,7 @@ module.exports = ({data, emitter}) => {
21
22
  return emitter.emit('failed', failureFromPayment(data));
22
23
 
23
24
  case states.paying:
24
- return emitter.emit('paying', {});
25
+ return emitter.emit('paying', pendingFromPayment(data));
25
26
 
26
27
  default:
27
28
  return;
@@ -105,6 +105,60 @@ export type GetPaymentResult = {
105
105
  /** Total Tokens Paid */
106
106
  tokens: number;
107
107
  };
108
+ pending?: {
109
+ /** Created at ISO-8601 Date */
110
+ created_at: string;
111
+ /** Payment Destination Public Key Hex */
112
+ destination: string;
113
+ /** Payment Hash Hex */
114
+ id: string;
115
+ /** Total Millitokens Pending */
116
+ mtokens: string;
117
+ paths: {
118
+ /** Total Path Fee Tokens */
119
+ fee: number;
120
+ /** Total Path Fee Millitokens */
121
+ fee_mtokens: string;
122
+ hops: {
123
+ /** Standard Format Channel Id */
124
+ channel: string;
125
+ /** Channel Capacity Tokens */
126
+ channel_capacity: number;
127
+ /** Routing Fee Tokens */
128
+ fee: number;
129
+ /** Fee Millitokens */
130
+ fee_mtokens: string;
131
+ /** Forwarded Tokens */
132
+ forward: number;
133
+ /** Forward Millitokens */
134
+ forward_mtokens: string;
135
+ /** Public Key Hex */
136
+ public_key: string;
137
+ /** Timeout Block Height */
138
+ timeout: number;
139
+ }[];
140
+ /** Total Path Millitokens Paid */
141
+ mtokens: string;
142
+ /** MPP Payment Identifying Nonce */
143
+ payment: string;
144
+ /** Expiration Block Height */
145
+ timeout: number;
146
+ /** Path Tokens Paid */
147
+ tokens: number;
148
+ /** Total Millitokens Pending */
149
+ total_mtokens: string;
150
+ }[]
151
+ /** BOLT 11 Payment Request */
152
+ request?: string;
153
+ /** Payment Tokens Rounded Up */
154
+ safe_tokens: number;
155
+ /** Payment Preimage Hex */
156
+ secret: string;
157
+ /** Expiration Block Height */
158
+ timeout?: number;
159
+ /** Total Tokens Pending */
160
+ tokens: number;
161
+ };
108
162
  };
109
163
 
110
164
  /**
@@ -67,6 +67,30 @@ const type = 'router';
67
67
  timeout: <Expiration Block Height Number>
68
68
  tokens: <Total Tokens Paid Number>
69
69
  }
70
+ [pending]: {
71
+ created_at: <Payment Created At ISO 8601 Date String>
72
+ destination: <Payment Destination Hex String>
73
+ id: <Payment Hash Hex String>
74
+ mtokens: <Total Millitokens Pending String>
75
+ paths: [{
76
+ fee_mtokens: <Total Fee Millitokens Paid String>
77
+ hops: [{
78
+ channel: <Standard Format Channel Id String>
79
+ channel_capacity: <Channel Capacity Tokens Number>
80
+ fee: <Fee Tokens Rounded Down Number>
81
+ fee_mtokens: <Fee Millitokens String>
82
+ forward: <Forwarded Tokens Number>
83
+ forward_mtokens: <Forward Millitokens String>
84
+ public_key: <Public Key Hex String>
85
+ timeout: <Timeout Block Height Number>
86
+ }]
87
+ mtokens: <Total Millitokens Pending String>
88
+ }]
89
+ [request]: <BOLT 11 Encoded Payment Request String>
90
+ safe_tokens: <Payment Tokens Rounded Up Number>
91
+ [timeout]: <Expiration Block Height Number>
92
+ tokens: <Total Tokens Pending Number>
93
+ }
70
94
  }
71
95
  */
72
96
  module.exports = ({id, lnd}, cbk) => {
@@ -102,6 +126,7 @@ module.exports = ({id, lnd}, cbk) => {
102
126
  is_failed: !!res.failed,
103
127
  is_pending: !res.payment && !res.failed,
104
128
  payment: res.payment || undefined,
129
+ pending: res.pending || undefined,
105
130
  });
106
131
  };
107
132
 
@@ -109,7 +134,7 @@ module.exports = ({id, lnd}, cbk) => {
109
134
  sub.once('end', () => cbk([503, 'UnknownStatusOfPayment']));
110
135
  sub.once('error', err => finished(err));
111
136
  sub.once('failed', failed => finished(null, {failed}));
112
- sub.once('paying', () => finished(null, {}));
137
+ sub.once('paying', pending => finished(null, {pending}));
113
138
 
114
139
  return;
115
140
  }],
@@ -82,7 +82,34 @@ const unknownServiceErr = 'unknown service verrpc.Versioner';
82
82
  }
83
83
 
84
84
  @event 'paying'
85
- {}
85
+ {
86
+ created_at: <Payment Created At ISO 8601 Date String>
87
+ destination: <Payment Destination Hex String>
88
+ id: <Payment Hash Hex String>
89
+ mtokens: <Total Millitokens Pending String>
90
+ paths: [{
91
+ fee: <Total Fee Tokens Pending Number>
92
+ fee_mtokens: <Total Fee Millitokens Pending String>
93
+ hops: [{
94
+ channel: <Standard Format Channel Id String>
95
+ channel_capacity: <Channel Capacity Tokens Number>
96
+ fee: <Fee Tokens Rounded Down Number>
97
+ fee_mtokens: <Fee Millitokens String>
98
+ forward: <Forward Tokens Number>
99
+ forward_mtokens: <Forward Millitokens String>
100
+ public_key: <Public Key Hex String>
101
+ timeout: <Timeout Block Height Number>
102
+ }]
103
+ mtokens: <Total Millitokens Pending String>
104
+ safe_fee: <Total Fee Tokens Pending Rounded Up Number>
105
+ safe_tokens: <Total Tokens Pending, Rounded Up Number>
106
+ timeout: <Expiration Block Height Number>
107
+ }]
108
+ [request]: <BOLT 11 Encoded Payment Request String>
109
+ safe_tokens: <Total Tokens Pending, Rounded Up Number>
110
+ [timeout]: <Expiration Block Height Number>
111
+ tokens: <Total Tokens Pending Rounded Down Number>
112
+ }
86
113
  */
87
114
  module.exports = args => {
88
115
  if (!isHash(args.id)) {
@@ -11,6 +11,7 @@ const nodeInfoAsNode = require('./node_info_as_node');
11
11
  const paymentFailure = require('./payment_failure');
12
12
  const paymentRequestDetails = require('./payment_request_details');
13
13
  const pendingAsPendingChannels = require('./pending_as_pending_channels');
14
+ const pendingFromPayment = require('./pending_from_payment');
14
15
  const policyFromChannelUpdate = require('./policy_from_channel_update');
15
16
  const routesFromQueryRoutes = require('./routes_from_query_routes');
16
17
  const rpcAttemptHtlcAsAttempt = require('./rpc_attempt_htlc_as_attempt');
@@ -53,6 +54,7 @@ module.exports = {
53
54
  paymentFailure,
54
55
  paymentRequestDetails,
55
56
  pendingAsPendingChannels,
57
+ pendingFromPayment,
56
58
  policyFromChannelUpdate,
57
59
  routesFromQueryRoutes,
58
60
  rpcAttemptHtlcAsAttempt,
@@ -0,0 +1,145 @@
1
+ const {attemptStates} = require('./constants');
2
+ const rpcAttemptHtlcAsAttempt = require('./rpc_attempt_htlc_as_attempt');
3
+ const {safeTokens} = require('./../bolt00');
4
+
5
+ const {isArray} = Array;
6
+ const is256Hex = n => !!n && /^[0-9A-F]{64}$/i.test(n);
7
+ const {max} = Math;
8
+ const mtokensAsTokens = mtokens => safeTokens({mtokens}).tokens;
9
+ const nsAsDate = ns => new Date(Number(BigInt(ns) / BigInt(1e6)));
10
+
11
+ /** Calculate total payment details from RPC payment HTLC elements
12
+
13
+ The `route` attribute only returns the first route, there may be more due to
14
+ payment splitting
15
+
16
+ {
17
+ creation_date: <Creation Date Epoch Time Seconds String>
18
+ creation_time_ns: <Creation Date Epoch Time Nanoseconds String>
19
+ failure_reason: <Payment Failure Reason String>
20
+ fee_msat: <Fee Paid in Millitokens String>
21
+ fee_sat: <Fee Paid in Tokens String>
22
+ htlcs: [{
23
+ attempt_time_ns: <HTLC Sent At Epoch Time Nanoseconds String>
24
+ resolve_time_ns: <HTLC Resolved At Epoch Time Nanoseconds String>
25
+ route: {
26
+ hops: [{
27
+ amt_to_forward: <Tokens to Forward String>
28
+ amt_to_forward_msat: <Millitokens to Forward String>
29
+ chan_id: <Numeric Format Channel Id String>
30
+ chan_capacity: <Channel Capacity Tokens String>
31
+ custom_records: {
32
+ <UInt64 String>: <Record Data Buffer>
33
+ }
34
+ expiry: <Timeout Chain Height Number>
35
+ fee: <Fee in Tokens String>
36
+ fee_msat: <Fee in Millitokens String>
37
+ [mpp_record]: {
38
+ payment_addr: <Payment Identifier Buffer>
39
+ total_amt_msat: <Total Payment Millitokens Amount String>
40
+ }
41
+ [pub_key]: <Next Hop Public Key Hex String>
42
+ tlv_payload: <Has Extra TLV Data Bool>
43
+ }]
44
+ total_amt: <Total Tokens String>
45
+ total_amt_msat: <Route Total Millitokens String>
46
+ total_fees: <Route Fee Tokens String>
47
+ total_fees_msat: <Route Total Fees Millitokens String>
48
+ total_time_lock: <Route Total Timelock Number>
49
+ }
50
+ status: <HTLC Status String>
51
+ }]
52
+ path: [<Hop Public Key Hex String>]
53
+ payment_hash: <Preimage SHA256 Hash Hex String>
54
+ payment_index: <Payment Index String>
55
+ payment_preimage: <Payment Secret Preimage Hex String>
56
+ payment_request: <BOLT 11 Payment Request String>
57
+ status: <Payment State String>
58
+ value: <Tokens String>
59
+ value_msat: <Paid Tokens Without Routing Fees Millitokens String>
60
+ value_sat: <Paid Tokens Without Routing Fees String>
61
+ }
62
+
63
+ @throws
64
+ <Error>
65
+
66
+ @returns
67
+ {
68
+ created_at: <Payment Created At ISO 8601 Date String>
69
+ destination: <Payment Destination Public Key Hex String>
70
+ id: <Payment Hash Hex String>
71
+ mtokens: <Total Millitokens Pending String>
72
+ paths: [{
73
+ fee: <Total Fee Tokens Pending Number>
74
+ fee_mtokens: <Total Fee Millitokens Pending String>
75
+ hops: [{
76
+ channel: <Standard Format Channel Id String>
77
+ channel_capacity: <Channel Capacity Tokens Number>
78
+ fee: <Fee Tokens Rounded Down Number>
79
+ fee_mtokens: <Fee Millitokens String>
80
+ forward: <Forward Tokens Number>
81
+ forward_mtokens: <Forward Millitokens String>
82
+ public_key: <Public Key Hex String>
83
+ timeout: <Timeout Block Height Number>
84
+ }]
85
+ mtokens: <Total Millitokens Paid String>
86
+ safe_fee: <Total Fee Tokens Paid Rounded Up Number>
87
+ safe_tokens: <Total Tokens Paid, Rounded Up Number>
88
+ timeout: <Expiration Block Height Number>
89
+ }]
90
+ [request]: <BOLT 11 Encoded Payment Request String>
91
+ safe_tokens: <Total Tokens Pending, Rounded Up Number>
92
+ [timeout]: <Expiration Block Height Number>
93
+ tokens: <Total Tokens Pending Rounded Down Number>
94
+ }
95
+ */
96
+ module.exports = payment => {
97
+ if (!payment) {
98
+ throw new Error('ExpectedPendingPaymentToDerivePendingDetails');
99
+ }
100
+
101
+ if (!payment.creation_time_ns) {
102
+ throw new Error('ExpectedPaymentCreationDateToDerivePendingDetails');
103
+ }
104
+
105
+ if (!payment.fee_msat) {
106
+ throw new Error('ExpectedPaymentFeeMillitokensAmountForPendingPayment');
107
+ }
108
+
109
+ if (!isArray(payment.htlcs)) {
110
+ throw new Error('ExpectedArrayOfPaymentHtlcsInPendingPayment');
111
+ }
112
+
113
+ if (!payment.htlcs.find(n => n.status === attemptStates.pending)) {
114
+ throw new Error('ExpectedPendingHtlcInPendingPayment');
115
+ }
116
+
117
+ if (!is256Hex(payment.payment_hash)) {
118
+ throw new Error('ExpectedPaymentHashForPaymentAsPendingPayment');
119
+ }
120
+
121
+ if (mtokensAsTokens(payment.value_msat) !== Number(payment.value_sat)) {
122
+ throw new Error('ExpectedValueOfTokensAndMillitokensToBeConsistent');
123
+ }
124
+
125
+ const attempts = payment.htlcs.map(htlc => rpcAttemptHtlcAsAttempt(htlc));
126
+ const mtokens = BigInt(payment.value_msat) + BigInt(payment.fee_msat);
127
+
128
+ const pending = attempts.filter(n => n.is_pending);
129
+
130
+ const [first] = pending;
131
+
132
+ const [destination] = first.route.hops.map(n => n.public_key).reverse();
133
+
134
+ return {
135
+ destination,
136
+ created_at: nsAsDate(payment.creation_time_ns).toISOString(),
137
+ id: payment.payment_hash,
138
+ mtokens: mtokens.toString(),
139
+ paths: pending.map(n => n.route),
140
+ request: payment.payment_request || undefined,
141
+ safe_tokens: safeTokens({mtokens: mtokens.toString()}).safe,
142
+ timeout: max(...pending.map(n => n.route.timeout).filter(n => !!n)),
143
+ tokens: safeTokens({mtokens: mtokens.toString()}).tokens,
144
+ };
145
+ };
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "body-parser": "1.19.0",
21
21
  "bolt07": "1.7.4",
22
22
  "bolt09": "0.2.0",
23
- "cbor": "8.0.2",
23
+ "cbor": "8.1.0",
24
24
  "express": "4.17.1",
25
25
  "invoices": "2.0.1",
26
26
  "psbt": "1.1.10"
@@ -56,5 +56,5 @@
56
56
  "directory": "test/typescript"
57
57
  },
58
58
  "types": "index.d.ts",
59
- "version": "4.13.3"
59
+ "version": "4.14.0"
60
60
  }
@@ -8,7 +8,48 @@ const makeLnd = args => {
8
8
  return {
9
9
  router: {
10
10
  trackPaymentV2: ({}) => {
11
- const data = args.data || {status: 'IN_FLIGHT'};
11
+ const data = args.data || {
12
+ creation_date: '1',
13
+ creation_time_ns: '1',
14
+ failure_reason: 'FAILURE_REASON_NONE',
15
+ fee: '1',
16
+ fee_msat: '1000',
17
+ fee_sat: '1',
18
+ htlcs: [{
19
+ attempt_time_ns: '1',
20
+ status: 'IN_FLIGHT',
21
+ resolve_time_ns: '1',
22
+ route: {
23
+ hops: [{
24
+ amt_to_forward: '1',
25
+ amt_to_forward_msat: '1000',
26
+ chan_capacity: '1',
27
+ chan_id: '1',
28
+ custom_records: {'1': Buffer.alloc(1)},
29
+ expiry: 1,
30
+ fee: '1',
31
+ fee_msat: '1000',
32
+ mpp_record: {payment_addr: Buffer.alloc(32), total_amt_msat: '1000'},
33
+ pub_key: Buffer.alloc(33).toString('hex'),
34
+ tlv_payload: true,
35
+ }],
36
+ total_amt: '1',
37
+ total_amt_msat: '1000',
38
+ total_time_lock: 1,
39
+ total_fees: '1',
40
+ total_fees_msat: '1000',
41
+ },
42
+ }],
43
+ path: [Buffer.alloc(33).toString('hex')],
44
+ payment_hash: Buffer.alloc(32).toString('hex'),
45
+ payment_index: '1',
46
+ payment_preimage: Buffer.alloc(32).toString('hex'),
47
+ payment_request: '',
48
+ status: 'IN_FLIGHT',
49
+ value: '1',
50
+ value_msat: '1000',
51
+ value_sat: '1',
52
+ };
12
53
  const emitter = new EventEmitter();
13
54
 
14
55
  if (!!args.is_end) {
@@ -146,6 +187,35 @@ const tests = [
146
187
  is_failed: false,
147
188
  is_pending: true,
148
189
  payment: undefined,
190
+ pending: {
191
+ created_at: '1970-01-01T00:00:00.000Z',
192
+ destination: Buffer.alloc(33).toString('hex'),
193
+ id: Buffer.alloc(32).toString('hex'),
194
+ mtokens: '2000',
195
+ paths: [{
196
+ fee: 1,
197
+ fee_mtokens: '1000',
198
+ hops: [{
199
+ channel: '0x0x1',
200
+ channel_capacity: 1,
201
+ fee: 1,
202
+ fee_mtokens: '1000',
203
+ forward: 1,
204
+ forward_mtokens: '1000',
205
+ public_key: Buffer.alloc(33).toString('hex'),
206
+ timeout: 1,
207
+ }],
208
+ mtokens: '1000',
209
+ payment: '0000000000000000000000000000000000000000000000000000000000000000',
210
+ timeout: 1,
211
+ tokens: 1,
212
+ total_mtokens: '1000',
213
+ }],
214
+ request: undefined,
215
+ safe_tokens: 2,
216
+ timeout: 1,
217
+ tokens: 2,
218
+ },
149
219
  },
150
220
  },
151
221
  },
@@ -0,0 +1,143 @@
1
+ const {test} = require('@alexbosworth/tap');
2
+
3
+ const {pendingFromPayment} = require('./../../lnd_responses');
4
+
5
+ const makeArgs = overrides => {
6
+ const args = {
7
+ creation_date: '1',
8
+ creation_time_ns: '1',
9
+ failure_reason: 'FAILURE_REASON_NONE',
10
+ fee: '1',
11
+ fee_msat: '1000',
12
+ fee_sat: '1',
13
+ htlcs: [{
14
+ attempt_time_ns: '1',
15
+ status: 'IN_FLIGHT',
16
+ resolve_time_ns: '1',
17
+ route: {
18
+ hops: [{
19
+ amt_to_forward: '1',
20
+ amt_to_forward_msat: '1000',
21
+ chan_capacity: '1',
22
+ chan_id: '1',
23
+ custom_records: {'1': Buffer.alloc(1)},
24
+ expiry: 1,
25
+ fee: '1',
26
+ fee_msat: '1000',
27
+ mpp_record: {payment_addr: Buffer.alloc(32), total_amt_msat: '1000'},
28
+ pub_key: Buffer.alloc(33).toString('hex'),
29
+ tlv_payload: true,
30
+ }],
31
+ total_amt: '1',
32
+ total_amt_msat: '1000',
33
+ total_time_lock: 1,
34
+ total_fees: '1',
35
+ total_fees_msat: '1000',
36
+ },
37
+ }],
38
+ path: [Buffer.alloc(33).toString('hex')],
39
+ payment_hash: Buffer.alloc(32).toString('hex'),
40
+ payment_index: '1',
41
+ payment_preimage: Buffer.alloc(32).toString('hex'),
42
+ payment_request: '',
43
+ status: 'IN_FLIGHT',
44
+ value: '1',
45
+ value_msat: '1000',
46
+ value_sat: '1',
47
+ };
48
+
49
+ Object.keys(overrides).forEach(k => args[k] = overrides[k]);
50
+
51
+ return args;
52
+ };
53
+
54
+ const makeExpected = overrides => {
55
+ const expected = {
56
+ created_at: '1970-01-01T00:00:00.000Z',
57
+ destination: Buffer.alloc(33).toString('hex'),
58
+ id: Buffer.alloc(32).toString('hex'),
59
+ mtokens: '2000',
60
+ paths: [{
61
+ fee: 1,
62
+ fee_mtokens: '1000',
63
+ hops: [{
64
+ channel: '0x0x1',
65
+ channel_capacity: 1,
66
+ fee: 1,
67
+ fee_mtokens: '1000',
68
+ forward: 1,
69
+ forward_mtokens: '1000',
70
+ public_key: Buffer.alloc(33).toString('hex'),
71
+ timeout: 1,
72
+ }],
73
+ mtokens: '1000',
74
+ payment: '0000000000000000000000000000000000000000000000000000000000000000',
75
+ timeout: 1,
76
+ tokens: 1,
77
+ total_mtokens: '1000',
78
+ }],
79
+ request: undefined,
80
+ safe_tokens: 2,
81
+ timeout: 1,
82
+ tokens: 2,
83
+ };
84
+
85
+ Object.keys(overrides).forEach(k => expected[k] = overrides[k]);
86
+
87
+ return expected;
88
+ };
89
+
90
+ const tests = [
91
+ {
92
+ args: undefined,
93
+ description: 'A payment is expected',
94
+ error: 'ExpectedPendingPaymentToDerivePendingDetails',
95
+ },
96
+ {
97
+ args: makeArgs({creation_time_ns: undefined}),
98
+ description: 'Creation time is expected',
99
+ error: 'ExpectedPaymentCreationDateToDerivePendingDetails',
100
+ },
101
+ {
102
+ args: makeArgs({fee_msat: undefined}),
103
+ description: 'Fee millitokens are expected',
104
+ error: 'ExpectedPaymentFeeMillitokensAmountForPendingPayment',
105
+ },
106
+ {
107
+ args: makeArgs({htlcs: undefined}),
108
+ description: 'HTLCs are expected',
109
+ error: 'ExpectedArrayOfPaymentHtlcsInPendingPayment',
110
+ },
111
+ {
112
+ args: makeArgs({htlcs: [{}]}),
113
+ description: 'A successful HTLC is expected',
114
+ error: 'ExpectedPendingHtlcInPendingPayment',
115
+ },
116
+ {
117
+ args: makeArgs({payment_hash: undefined}),
118
+ description: 'A payment hash is expected',
119
+ error: 'ExpectedPaymentHashForPaymentAsPendingPayment',
120
+ },
121
+ {
122
+ args: makeArgs({value_sat: '1', value_msat: '0'}),
123
+ description: 'Values are expected to agree',
124
+ error: 'ExpectedValueOfTokensAndMillitokensToBeConsistent',
125
+ },
126
+ {
127
+ args: makeArgs({}),
128
+ description: 'Payment is mapped to confirmed payment details',
129
+ expected: makeExpected({}),
130
+ },
131
+ ];
132
+
133
+ tests.forEach(({args, description, error, expected}) => {
134
+ return test(description, ({end, strictSame, throws}) => {
135
+ if (!!error) {
136
+ throws(() => pendingFromPayment(args), new Error(error), 'Err');
137
+ } else {
138
+ strictSame(pendingFromPayment(args), expected, 'Mapped');
139
+ }
140
+
141
+ return end();
142
+ });
143
+ });