balanceofsatoshis 25.2.2 → 25.3.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.
@@ -1,249 +0,0 @@
1
- const asyncAuto = require('async/auto');
2
- const asyncMapSeries = require('async/mapSeries');
3
- const {componentsOfTransaction} = require('@alexbosworth/blockchain');
4
- const {idForTransaction} = require('@alexbosworth/blockchain');
5
- const {resolutionType} = require('bolt03');
6
- const {returnResult} = require('asyncjs-util');
7
-
8
- const {endpoints} = require('./blockstream');
9
-
10
- const closeSpendsDelayMs = 1000;
11
- const flatten = arr => [].concat(...arr);
12
- const getTxDelayMs = 2000;
13
-
14
- /** Get channel resolution
15
-
16
- {
17
- close_transaction_id: <Close Transaction Id String>
18
- [is_cooperative_close]: <Channel Is Cooperatively Closed Bool>
19
- network: <Network Name String>
20
- request: <Request Function>
21
- transactions: [{
22
- id: <Transaction Id String>
23
- output_addresses: [<Address String>]
24
- transaction: <Raw Transaction Hex String>
25
- }]
26
- }
27
-
28
- @returns via cbk or Promise
29
- {
30
- [resolutions]: [{
31
- type: <Resolution Type String>
32
- value: <Value Number>
33
- }]
34
- }
35
- */
36
- module.exports = (args, cbk) => {
37
- return new Promise((resolve, reject) => {
38
- return asyncAuto({
39
- // Check arguments
40
- validate: cbk => {
41
- if (!args.close_transaction_id) {
42
- return cbk([400, 'ExpectedCloseTransactionIdToGetChanResolution']);
43
- }
44
-
45
- if (!args.network) {
46
- return cbk([400, 'ExpectedNodeNetworkNameToGetChannelResolution']);
47
- }
48
-
49
- if (!args.request) {
50
- return cbk([400, 'ExpectedRequestFunctionToGetChannelResolution']);
51
- }
52
-
53
- return cbk();
54
- },
55
-
56
- // Get commitment transaction
57
- getCommitmentTransaction: ['validate', ({}, cbk) => {
58
- if (!!args.is_cooperative_close) {
59
- return cbk();
60
- }
61
-
62
- const id = args.close_transaction_id;
63
-
64
- const found = args.transactions.find(n => n.id === id);
65
-
66
- // Exit early when the transaction is pre-provided
67
- if (!!found) {
68
- return cbk(null, found.transaction);
69
- }
70
-
71
- return args.request({
72
- url: `${endpoints[args.network]}tx/${id}/hex`
73
- },
74
- (err, r, txHex) => {
75
- if (!!err) {
76
- return cbk([503, 'UnexpectedErrorGettingCommitTxInfo', err]);
77
- }
78
-
79
- if (!r || r.statusCode !== 200) {
80
- return cbk([503, 'UnexpectedResponseCodeWhenGettingCommitTx']);
81
- }
82
-
83
- if (!txHex) {
84
- return cbk([503, 'ExpectedTransactionForCommitTxId']);
85
- }
86
-
87
- return cbk(null, txHex);
88
- });
89
- }],
90
-
91
- // Decode transactions to derive spends
92
- spends: ['validate', ({}, cbk) => {
93
- const spends = args.transactions.map(({id, transaction}) => {
94
- const {inputs} = componentsOfTransaction({transaction});
95
-
96
- return inputs.map((input, index) => {
97
- return {id: input.id, spent_by: id, vin: index};
98
- });
99
- });
100
-
101
- return cbk(null, flatten(spends));
102
- }],
103
-
104
- // Get close tx output spends
105
- getCloseSpends: ['spends', ({spends}, cbk) => {
106
- if (!!args.is_cooperative_close) {
107
- return cbk(null, []);
108
- }
109
-
110
- const closeTxId = args.close_transaction_id;
111
-
112
- const found = args.transactions.find(n => n.id === closeTxId);
113
-
114
- const outspends = spends.filter(n => n.id === closeTxId);
115
-
116
- // Exit early without requesting when all spends are found locally
117
- if (!!found && found.output_addresses.length === outspends.length) {
118
- return cbk(null, outspends.map(outspend => {
119
- return {txid: outspend.spent_by, vin: outspend.vin};
120
- }));
121
- }
122
-
123
- return args.request({
124
- json: true,
125
- url: `${endpoints[args.network]}tx/${closeTxId}/outspends`,
126
- },
127
- (err, r, outspends) => {
128
- if (!!err) {
129
- return cbk([503, 'UnexpectedErrorGettingOutspentsForTx', err]);
130
- }
131
-
132
- if (!r || r.statusCode !== 200) {
133
- return cbk([503, 'UnexpectedResponseCodeWhenGettingOutspends']);
134
- }
135
-
136
- if (!Array.isArray(outspends)) {
137
- return cbk([503, 'ExpectedJsonResultForTransactionOutspents']);
138
- }
139
-
140
- const unexpectedOutspend = outspends.find(n => {
141
- if (!n.spent) {
142
- return false;
143
- }
144
-
145
- return !n.txid || n.vin === undefined;
146
- });
147
-
148
- if (!!unexpectedOutspend) {
149
- return cbk([503, 'UnexpectedResultFromOutspendQuery', outspends]);
150
- }
151
-
152
- return setTimeout(() => {
153
- return cbk(null, outspends.map(outspend => ({
154
- txid: outspend.txid,
155
- vin: outspend.vin,
156
- })));
157
- },
158
- closeSpendsDelayMs);
159
- });
160
- }],
161
-
162
- // Get transactions
163
- getTransactions: ['getCloseSpends', ({getCloseSpends}, cbk) => {
164
- const txs = {};
165
-
166
- return asyncMapSeries(getCloseSpends, ({txid, vin}, cbk) => {
167
- if (!txid) {
168
- return cbk(null, {});
169
- }
170
-
171
- if (!!txs[txid]) {
172
- return cbk(null, {id: txid, transaction: txs[txid]});
173
- }
174
-
175
- const found = args.transactions.find(n => n.id === txid);
176
-
177
- // Exit early when the transaction is pre-provided
178
- if (!!found) {
179
- return cbk(null, {id: txid, transaction: found.transaction});
180
- }
181
-
182
- return args.request({
183
- url: `${endpoints[args.network]}tx/${txid}/hex`
184
- },
185
- (err, r, txHex) => {
186
- if (!!err) {
187
- return cbk([503, 'UnexpectedErrorGettingSpentTxInfo', err]);
188
- }
189
-
190
- if (!r || r.statusCode !== 200) {
191
- return cbk([503, 'UnexpectedResponseCodeWhenGettingTx']);
192
- }
193
-
194
- if (!txHex) {
195
- return cbk([503, 'ExpectedTransactionForSpendTxId']);
196
- }
197
-
198
- txs[txid] = txHex;
199
-
200
- return setTimeout(() => {
201
- return cbk(null, {id: txid, transaction: txHex});
202
- },
203
- getTxDelayMs);
204
- });
205
- },
206
- cbk);
207
- }],
208
-
209
- // Resolutions
210
- resolutions: [
211
- 'getCloseSpends',
212
- 'getCommitmentTransaction',
213
- 'getTransactions',
214
- ({getCloseSpends, getCommitmentTransaction, getTransactions}, cbk) =>
215
- {
216
- // Exit early when the channel was closed cooperatively
217
- if (!!args.is_cooperative_close) {
218
- return cbk(null, {});
219
- }
220
-
221
- const tx = componentsOfTransaction({
222
- transaction: getCommitmentTransaction,
223
- });
224
-
225
- const resolutions = getCloseSpends.map(({txid, vin}, i) => {
226
- const {transaction} = getTransactions.find(n => txid === n.id);
227
- const value = tx.outputs[i].tokens;
228
-
229
- if (!transaction) {
230
- return {value, type: 'unspent'};
231
- }
232
-
233
- return {
234
- value,
235
- transaction_id: idForTransaction({transaction}).id,
236
- type: resolutionType({vin, transaction}).type,
237
- };
238
- });
239
-
240
- if (!resolutions.length) {
241
- return cbk();
242
- }
243
-
244
- return cbk(null, {resolutions});
245
- }],
246
- },
247
- returnResult({reject, resolve, of: 'resolutions'}, cbk));
248
- });
249
- };