paid-services 3.17.1 → 3.19.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,6 +1,15 @@
1
1
  # Versions
2
2
 
3
- ## Version 3.17.1
3
+ ## Version 3.19.0
4
+
5
+ - `manageSwap`: Add support for inbound peer constraint
6
+ - `manageSwap`: Add support for external sweep address
7
+
8
+ ## Version 3.18.0
9
+
10
+ - `manageGroupJoin`: Add conflict tx for safe funded pending channel deletion
11
+
12
+ ## Version 3.17.2
4
13
 
5
14
  - `manageGroupJoin`: Add method to coordinate or join a channels group
6
15
 
@@ -1,4 +1,5 @@
1
1
  const asyncAuto = require('async/auto');
2
+ const asyncRetry = require('async/retry');
2
3
  const {connectPeer} = require('ln-sync');
3
4
  const {getChainBalance} = require('ln-service');
4
5
  const {getNodeAlias} = require('ln-sync');
@@ -8,9 +9,11 @@ const {getGroupDetails} = require('./p2p');
8
9
 
9
10
  const coordinatorFromJoinCode = n => n.slice(0, 66);
10
11
  const groupIdFromJoinCode = n => n.slice(66);
12
+ const interval = 500;
11
13
  const isCode = n => !!n && n.length === 98;
12
14
  const isPublicKey = n => !!n && /^0[2-3][0-9A-F]{64}$/i.test(n);
13
15
  const niceName = n => `${n.alias} ${n.id}`.trim();
16
+ const times = 2 * 60;
14
17
  const tokensAsBigUnit = tokens => (tokens / 1e8).toFixed(8);
15
18
 
16
19
  /** Ask to confirm joining a group
@@ -73,7 +76,10 @@ module.exports = ({ask, lnd}, cbk) => {
73
76
 
74
77
  // Connect to the coordinator
75
78
  connect: ['group', ({group}, cbk) => {
76
- return connectPeer({lnd, id: group.coordinator}, cbk);
79
+ return asyncRetry({interval, times}, cbk => {
80
+ return connectPeer({lnd, id: group.coordinator}, cbk);
81
+ },
82
+ cbk);
77
83
  }],
78
84
 
79
85
  // Get the coordinator node alias
@@ -78,6 +78,9 @@ module.exports = ({capacity, count, ecp, identity, lnd, rate}) => {
78
78
  return emitter.emit('error', err);
79
79
  };
80
80
 
81
+ // An error was encountered
82
+ coordinator.events.once('error', errored);
83
+
81
84
  // Group members have registered themselves
82
85
  coordinator.events.once('joined', async ({ids}) => {
83
86
  emitter.emit('filled', {ids});
@@ -70,7 +70,9 @@ module.exports = ({ask, lnd, logger}, cbk) => {
70
70
  return logger.info({peering_with: formatNodes(nodes)});
71
71
  });
72
72
 
73
- join.once('publishing', ({signed}) => logger.info({signed}));
73
+ join.once('publishing', ({refund, signed}) => {
74
+ return logger.info({refund, signed});
75
+ });
74
76
 
75
77
  return;
76
78
  }],
@@ -13,6 +13,8 @@ const half = n => n / 2;
13
13
  lnd: <Authenticated LND API Object>
14
14
  to: <Look for Outgoing Channel To Identity Public Key Hex String>
15
15
  }
16
+
17
+ @returns via cbk or Promise
16
18
  */
17
19
  module.exports = ({capacity, from, id, lnd, to}, cbk) => {
18
20
  return new Promise((resolve, reject) => {
@@ -1,18 +1,28 @@
1
+ const {address} = require('bitcoinjs-lib');
1
2
  const asyncAuto = require('async/auto');
2
3
  const asyncRetry = require('async/retry');
4
+ const {createChainAddress} = require('ln-service');
5
+ const {createPsbt} = require('psbt');
3
6
  const {decodePsbt} = require('psbt');
4
7
  const {extendPsbt} = require('psbt');
5
8
  const {fundPendingChannels} = require('ln-service');
9
+ const {getChainFeeRate} = require('ln-service');
10
+ const {getMaxFundAmount} = require('ln-sync');
6
11
  const {getPendingChannels} = require('ln-service');
7
12
  const {partiallySignPsbt} = require('ln-service');
13
+ const {payments} = require('bitcoinjs-lib');
8
14
  const {returnResult} = require('asyncjs-util');
15
+ const {signPsbt} = require('ln-service');
9
16
  const tinysecp = require('tiny-secp256k1');
10
17
  const {Transaction} = require('bitcoinjs-lib');
11
18
  const {unextractTransaction} = require('psbt');
12
19
 
20
+ const bufferAsHex = buffer => buffer.toString('hex');
13
21
  const {concat} = Buffer;
14
22
  const dummySignature = Buffer.alloc(1);
23
+ const format = 'p2wpkh';
15
24
  const {from} = Buffer;
25
+ const {fromBech32} = address;
16
26
  const {fromHex} = Transaction;
17
27
  const hashAll = Transaction.SIGHASH_ALL;
18
28
  const hashDefault = Transaction.SIGHASH_DEFAULT;
@@ -21,6 +31,9 @@ const inputAsOutpoint = n => `${n.transaction_id}:${n.transaction_vout}`;
21
31
  const interval = 10;
22
32
  const {isArray} = Array;
23
33
  const notEmpty = arr => arr.filter(n => !!n);
34
+ const {p2wpkh} = payments;
35
+ const {random} = Math;
36
+ const slowConf = 144;
24
37
  const spendAsOutpoint = n => `${n.hash.reverse().toString('hex')}:${n.index}`;
25
38
  const times = 500;
26
39
 
@@ -49,6 +62,7 @@ const times = 500;
49
62
 
50
63
  @returns via cbk or Promise
51
64
  {
65
+ conflict: <Conflict Transaction Hex String>
52
66
  psbt: <Partially Signed PSBT Hex String>
53
67
  }
54
68
  */
@@ -79,11 +93,94 @@ module.exports = ({id, lnd, psbt, utxos}, cbk) => {
79
93
  return cbk();
80
94
  },
81
95
 
96
+ // Create a conflicting address to refund funds to
97
+ createConflictAddress: ['validate', ({}, cbk) => {
98
+ return createChainAddress({format, lnd}, cbk);
99
+ }],
100
+
101
+ // Get the conflicting tx fee rate
102
+ getRate: ['validate', ({}, cbk) => {
103
+ return getChainFeeRate({lnd, confirmation_target: slowConf}, cbk);
104
+ }],
105
+
106
+ // Find the conflicting amount to send to the refund address
107
+ getConflictAmount: [
108
+ 'createConflictAddress',
109
+ 'getRate',
110
+ ({createConflictAddress, getRate}, cbk) =>
111
+ {
112
+ const [input] = utxos;
113
+
114
+ return getMaxFundAmount({
115
+ lnd,
116
+ addresses: [createConflictAddress.address],
117
+ fee_tokens_per_vbyte: getRate.tokens_per_vbyte,
118
+ inputs: [{
119
+ tokens: input.witness_utxo.tokens,
120
+ transaction_id: input.transaction_id,
121
+ transaction_vout: input.transaction_vout,
122
+ }],
123
+ },
124
+ cbk);
125
+ }],
126
+
127
+ // Create a conflicting PSBT to sign
128
+ conflict: [
129
+ 'createConflictAddress',
130
+ 'ecp',
131
+ 'getConflictAmount',
132
+ ({createConflictAddress, ecp, getConflictAmount}, cbk) =>
133
+ {
134
+ const hash = fromBech32(createConflictAddress.address).data;
135
+ const [input] = utxos;
136
+
137
+ const {psbt} = createPsbt({
138
+ outputs: [{
139
+ script: bufferAsHex(p2wpkh({hash}).output),
140
+ tokens: getConflictAmount.max_tokens,
141
+ }],
142
+ utxos: [{
143
+ id: input.transaction_id,
144
+ vout: input.transaction_vout,
145
+ }],
146
+ });
147
+
148
+ const base = decodePsbt({ecp, psbt});
149
+
150
+ const tx = fromHex(base.unsigned_transaction);
151
+
152
+ const inputs = base.inputs.map((input, vin) => {
153
+ const outpoint = spendAsOutpoint(tx.ins[vin]);
154
+
155
+ // Look for relevant signing instructions
156
+ const utxo = utxos.find(n => inputAsOutpoint(n) === outpoint) || {};
157
+
158
+ return {
159
+ bip32_derivations: utxo.bip32_derivations,
160
+ non_witness_utxo: utxo.non_witness_utxo,
161
+ sighash_type: !!utxo.non_witness_utxo ? hashAll : hashDefault,
162
+ witness_utxo: utxo.witness_utxo,
163
+ };
164
+ });
165
+
166
+ // Extend the base PSBT with relevant signing information
167
+ return cbk(null, extendPsbt({ecp, inputs, psbt}).psbt);
168
+ }],
169
+
170
+ // Decode the PSBT to get the unsigned funding transaction
171
+ funding: ['ecp', 'validate', ({ecp}, cbk) => {
172
+ try {
173
+ return cbk(null, decodePsbt({ecp, psbt}));
174
+ } catch (err) {
175
+ return cbk([400, 'ExpectedValidPsbtToSignAndFundChannel', {err}]);
176
+ }
177
+ }],
178
+
82
179
  // Extend the PSBT with the derivation paths
83
- psbtToSign: ['ecp', 'validate', ({ecp}, cbk) => {
84
- const tx = fromHex(decodePsbt({ecp, psbt}).unsigned_transaction);
180
+ psbtToSign: ['ecp', 'funding', ({ecp, funding}, cbk) => {
181
+ const tx = fromHex(funding.unsigned_transaction);
85
182
 
86
- const inputs = decodePsbt({ecp, psbt}).inputs.map((input, vin) => {
183
+ const inputs = funding.inputs.map((input, vin) => {
87
184
  const outpoint = spendAsOutpoint(tx.ins[vin]);
88
185
 
89
186
  // Look for relevant signing instructions
@@ -101,6 +198,11 @@ module.exports = ({id, lnd, psbt, utxos}, cbk) => {
101
198
  return cbk(null, {psbt: extendPsbt({ecp, inputs, psbt}).psbt});
102
199
  }],
103
200
 
201
+ // Sign and finalize the conflicting PSBT
202
+ signConflict: ['conflict', ({conflict}, cbk) => {
203
+ return signPsbt({lnd, psbt: conflict}, cbk);
204
+ }],
205
+
104
206
  // Partially sign the PSBT that funds the channel open
105
207
  signPsbt: ['psbtToSign', ({psbtToSign}, cbk) => {
106
208
  return partiallySignPsbt({lnd, psbt: psbtToSign.psbt}, cbk);
@@ -151,7 +253,7 @@ module.exports = ({id, lnd, psbt, utxos}, cbk) => {
151
253
  }],
152
254
 
153
255
  // Fund the pending channel with the finalized PSBT
154
- fundChannel: ['finalizePsbt', ({finalizePsbt}, cbk) => {
256
+ fundChannel: ['conflict', 'finalizePsbt', ({finalizePsbt}, cbk) => {
155
257
  return fundPendingChannels({
156
258
  lnd,
157
259
  channels: [id],
@@ -182,7 +284,35 @@ module.exports = ({id, lnd, psbt, utxos}, cbk) => {
182
284
  },
183
285
  cbk);
184
286
  }],
287
+
288
+ // Final group funding transaction resolution
289
+ result: [
290
+ 'ecp',
291
+ 'signConflict',
292
+ 'signPsbt',
293
+ ({ecp, signConflict, signPsbt}, cbk) =>
294
+ {
295
+ const signed = decodePsbt({ecp, psbt: signPsbt.psbt});
296
+
297
+ const extended = extendPsbt({
298
+ ecp,
299
+ psbt,
300
+ inputs: signed.inputs.map(input => {
301
+ return {
302
+ non_witness_utxo: input.non_witness_utxo,
303
+ partial_sig: input.partial_sig,
304
+ taproot_key_spend_sig: input.taproot_key_spend_sig,
305
+ witness_utxo: input.witness_utxo,
306
+ };
307
+ }),
308
+ });
309
+
310
+ return cbk(null, {
311
+ conflict: signConflict.transaction,
312
+ psbt: extended.psbt,
313
+ });
314
+ }],
185
315
  },
186
- returnResult({reject, resolve, of: 'signPsbt'}, cbk));
316
+ returnResult({reject, resolve, of: 'result'}, cbk));
187
317
  });
188
318
  };
@@ -1,8 +1,11 @@
1
1
  const EventEmitter = require('events');
2
2
 
3
3
  const asyncAuto = require('async/auto');
4
+ const asyncReflect = require('async/reflect');
4
5
  const asyncRetry = require('async/retry');
6
+ const {cancelPendingChannel} = require('ln-service');
5
7
  const {decodePsbt} = require('psbt');
8
+ const {deletePendingChannel} = require('ln-service');
6
9
  const {returnResult} = require('asyncjs-util');
7
10
  const tinysecp = require('tiny-secp256k1');
8
11
  const {Transaction} = require('bitcoinjs-lib');
@@ -16,6 +19,7 @@ const {registerPendingOpen} = require('./p2p');
16
19
  const {registerSignedOpen} = require('./p2p');
17
20
 
18
21
  const {fromHex} = Transaction;
22
+ const hexAsBuffer = hex => Buffer.from(hex, 'hex');
19
23
  const interval = 1000;
20
24
  const times = 60 * 10;
21
25
 
@@ -138,10 +142,22 @@ module.exports = ({capacity, coordinator, count, id, lnd, rate}, cbk) => {
138
142
  }],
139
143
 
140
144
  // Decode the unsigned PSBT
141
- transaction: ['ecp', 'register', ({ecp, register}, cbk) => {
145
+ transaction: [
146
+ 'ecp',
147
+ 'propose',
148
+ 'register',
149
+ ({ecp, propose, register}, cbk) =>
150
+ {
151
+ const funding = hexAsBuffer(propose.funding);
142
152
  const psbt = decodePsbt({ecp, psbt: register.psbt});
143
153
 
144
- return cbk(null, {id: fromHex(psbt.unsigned_transaction).getId()});
154
+ const tx = fromHex(psbt.unsigned_transaction);
155
+
156
+ return cbk(null, {
157
+ id: tx.getId(),
158
+ raw: psbt.unsigned_transaction,
159
+ vout: tx.outs.findIndex(n => n.script.equals(funding)),
160
+ });
145
161
  }],
146
162
 
147
163
  // Confirm the incoming channel
@@ -150,7 +166,7 @@ module.exports = ({capacity, coordinator, count, id, lnd, rate}, cbk) => {
150
166
  'partners',
151
167
  'register',
152
168
  'transaction',
153
- ({ecp, partners, register, transaction}, cbk) =>
169
+ asyncReflect(({ecp, partners, register, transaction}, cbk) =>
154
170
  {
155
171
  // Make sure that there is an inbound channel
156
172
  return asyncRetry({interval, times}, cbk => {
@@ -164,12 +180,45 @@ module.exports = ({capacity, coordinator, count, id, lnd, rate}, cbk) => {
164
180
  cbk);
165
181
  },
166
182
  cbk);
183
+ })],
184
+
185
+ // Clean up funding pending channels when the channel group fails
186
+ clean: [
187
+ 'incoming',
188
+ 'propose',
189
+ 'register',
190
+ 'transaction',
191
+ ({incoming, propose, register, transaction}, cbk) =>
192
+ {
193
+ // Exit early when incoming channel is seen
194
+ if (!incoming.error) {
195
+ return cbk();
196
+ }
197
+
198
+ // When there was no incoming channel detected, fail and clean up
199
+ return deletePendingChannel({
200
+ lnd,
201
+ confirmed_transaction: register.conflict,
202
+ pending_transaction: transaction.raw,
203
+ pending_transaction_vout: transaction.vout,
204
+ },
205
+ err => {
206
+ if (!!err) {
207
+ return cbk([503, 'UnexpectedErrorCleaningUpGroupChannel', {err}]);
208
+ }
209
+
210
+ // Pass back the original error
211
+ return cbk(incoming.error);
212
+ });
167
213
  }],
168
214
 
169
215
  // Publish partial signatures to coordinator
170
- reveal: ['incoming', 'register', ({register}, cbk) => {
216
+ reveal: ['clean', 'incoming', 'register', ({register}, cbk) => {
171
217
  // Let listeners know that the signature will be sent to coordinator
172
- emitter.emit('publishing', {signed: register.psbt});
218
+ emitter.emit('publishing', {
219
+ refund: register.conflict,
220
+ signed: register.psbt,
221
+ });
173
222
 
174
223
  return registerSignedOpen({
175
224
  coordinator,
@@ -1,5 +1,7 @@
1
1
  const asyncAuto = require('async/auto');
2
+ const asyncReflect = require('async/reflect');
2
3
  const asyncRetry = require('async/retry');
4
+ const {cancelPendingChannel} = require('ln-service');
3
5
  const {connectPeer} = require('ln-sync');
4
6
  const {decodePsbt} = require('psbt');
5
7
  const {returnResult} = require('asyncjs-util');
@@ -46,7 +48,8 @@ const typeGroupChannelId = '1';
46
48
 
47
49
  @returns via cbk or Promise
48
50
  {
49
- psbt: <Unsigned PSBT Hex String>
51
+ conflict: <Conflict Transaction Hex String>
52
+ psbt: <Partially Signed PSBT Hex String>
50
53
  }
51
54
  */
52
55
  module.exports = (args, cbk) => {
@@ -94,7 +97,7 @@ module.exports = (args, cbk) => {
94
97
  }],
95
98
 
96
99
  // Send connection confirmation request
97
- request: ['connect', ({}, cbk) => {
100
+ request: ['connect', asyncReflect(({}, cbk) => {
98
101
  const {records} = encodePendingProposal({
99
102
  change: args.change,
100
103
  funding: args.funding,
@@ -145,17 +148,34 @@ module.exports = (args, cbk) => {
145
148
  });
146
149
  },
147
150
  cbk);
151
+ })],
152
+
153
+ // Clean up the pending channel if registration fails
154
+ clean: ['request', ({request}, cbk) => {
155
+ // Exit early when there was no error registering the pending channel
156
+ if (!request.error) {
157
+ return cbk();
158
+ }
159
+
160
+ return cancelPendingChannel({id: args.pending, lnd: args.lnd}, err => {
161
+ if (!!err) {
162
+ return cbk([503, 'UnexpectedErrorCleaningPendingChannel', {err}]);
163
+ }
164
+
165
+ // Return the original registration error
166
+ return cbk(request.error);
167
+ });
148
168
  }],
149
169
 
150
170
  // Check the unsigned funding transaction represents the partial open
151
- check: ['ecp', 'request', ({ecp, request}, cbk) => {
171
+ check: ['clean', 'ecp', 'request', ({ecp, request}, cbk) => {
152
172
  try {
153
- decodePsbt({ecp, psbt: request});
173
+ decodePsbt({ecp, psbt: request.value});
154
174
  } catch (err) {
155
175
  return cbk([503, 'ExpectedValidUnsignedResponsePsbt', {err}]);
156
176
  }
157
177
 
158
- const psbt = decodePsbt({ecp, psbt: request});
178
+ const psbt = decodePsbt({ecp, psbt: request.value});
159
179
 
160
180
  const tx = fromHex(psbt.unsigned_transaction);
161
181
 
@@ -207,7 +227,7 @@ module.exports = (args, cbk) => {
207
227
  return signAndFundGroupChannel({
208
228
  id: args.pending,
209
229
  lnd: args.lnd,
210
- psbt: request,
230
+ psbt: request.value,
211
231
  utxos: args.utxos,
212
232
  },
213
233
  cbk);
package/package.json CHANGED
@@ -15,10 +15,10 @@
15
15
  "bolt07": "1.8.2",
16
16
  "ecpair": "2.0.1",
17
17
  "goldengate": "11.2.3",
18
- "invoices": "2.0.7",
18
+ "invoices": "2.1.0",
19
19
  "ln-service": "53.17.4",
20
20
  "ln-sync": "3.13.0",
21
- "psbt": "2.6.0",
21
+ "psbt": "2.7.0",
22
22
  "p2tr": "1.3.1",
23
23
  "tiny-secp256k1": "2.2.1"
24
24
  },
@@ -47,5 +47,5 @@
47
47
  "integration-tests": "tap -j 2 --branches=1 --functions=1 --lines=1 --statements=1 -t 180 test/integration/*.js",
48
48
  "test": "tap --branches=1 --functions=1 --lines=1 --statements=1 -t 60 test/actions/*.js test/balanced/*.js test/capacity/*.js test/client/*.js test/config/*.js test/p2p/*.js test/records/*.js test/respond/*.js test/server/*.js test/server/*.js test/services/*.js test/trades/*.js"
49
49
  },
50
- "version": "3.17.1"
50
+ "version": "3.19.0"
51
51
  }
@@ -90,6 +90,7 @@ const v1AddressWords = key => [].concat(1).concat(bech32m.toWords(key));
90
90
  recovery: <Swap Request Recovery Hex String>
91
91
  [request]: <Request Function>
92
92
  response: <Swap Response Hex String>
93
+ [sweep_address]: <Sweep Chain Address String>
93
94
  }
94
95
 
95
96
  @returns via cbk or Promise
@@ -134,6 +135,11 @@ module.exports = (args, cbk) => {
134
135
 
135
136
  // Create a sweep address
136
137
  createAddress: ['validate', ({}, cbk) => {
138
+ // Exit early when there is no need to create a sweep address
139
+ if (!!args.sweep_address) {
140
+ return cbk(null, {address: args.sweep_address});
141
+ }
142
+
137
143
  return createChainAddress({lnd: args.lnd}, cbk);
138
144
  }],
139
145
 
@@ -204,6 +210,7 @@ module.exports = (args, cbk) => {
204
210
  coop_public_key: decoded.coop_public_key,
205
211
  deposit_mtokens: decoded.deposit_mtokens,
206
212
  deposit_payment: decoded.deposit_payment,
213
+ incoming_peer: decoded.incoming_peer,
207
214
  push: decoded.push,
208
215
  refund_public_key: decoded.refund_public_key,
209
216
  request: decoded.request,
@@ -238,7 +245,10 @@ module.exports = (args, cbk) => {
238
245
  const request = responseDetails.request;
239
246
 
240
247
  if (!!args.is_external_funding) {
241
- args.emitter.emit('update', {pay_to_fund_swap_offchain: request});
248
+ args.emitter.emit('update', {
249
+ external_funding_pay_to_fund_swap_offchain: request,
250
+ must_be_in_through: responseDetails.incoming_peer || undefined,
251
+ });
242
252
 
243
253
  return cbk();
244
254
  }
@@ -247,6 +257,7 @@ module.exports = (args, cbk) => {
247
257
 
248
258
  return pay({
249
259
  request,
260
+ incoming_peer: responseDetails.incoming_peer || undefined,
250
261
  lnd: args.lnd,
251
262
  max_fee: args.max_fee_funding,
252
263
  },
@@ -381,6 +392,7 @@ module.exports = (args, cbk) => {
381
392
  cltv_delta: to.cltv_delta,
382
393
  destination: to.destination,
383
394
  features: to.features,
395
+ incoming_peer: requestDetails.incoming_peer || undefined,
384
396
  lnd: args.lnd,
385
397
  max_fee: args.max_fee_deposit,
386
398
  messages: [{
@@ -412,7 +424,10 @@ module.exports = (args, cbk) => {
412
424
 
413
425
  const address = encodeAddress(prefix, v1AddressWords(key));
414
426
 
415
- args.emitter.emit('update', {waiting_for_chain_funding: address});
427
+ args.emitter.emit('update', {
428
+ waiting_for_chain_funding: address,
429
+ required_confirmations: args.min_confirmations || defaultConfsCount,
430
+ });
416
431
 
417
432
  if (!!args.request) {
418
433
  return findDeposit({
@@ -14,6 +14,7 @@ const {diffieHellmanComputeSecret} = require('ln-service');
14
14
  const {fundPsbt} = require('ln-service');
15
15
  const {getChainFeeRate} = require('ln-service');
16
16
  const {getChainTransactions} = require('ln-service');
17
+ const {getChannels} = require('ln-service');
17
18
  const {getHeight} = require('ln-service');
18
19
  const {getIdentity} = require('ln-service');
19
20
  const {getInvoice} = require('ln-service');
@@ -159,6 +160,7 @@ module.exports = (args, cbk) => {
159
160
  claim_solo_public_key: details.claim_solo_public_key,
160
161
  hash: details.hash,
161
162
  key_index: details.key_index,
163
+ incoming_peer: details.incoming_peer || undefined,
162
164
  refund_coop_private_key: details.refund_coop_private_key,
163
165
  refund_coop_private_key_hash: details.refund_coop_private_key_hash,
164
166
  solo_private_key: details.refund_solo_private_key,
@@ -170,6 +172,19 @@ module.exports = (args, cbk) => {
170
172
  }
171
173
  }],
172
174
 
175
+ // Lookup channel ids with the incoming peer when applicable
176
+ getChannels: ['recoveryDetails', ({recoveryDetails}, cbk) => {
177
+ if (!recoveryDetails.incoming_peer) {
178
+ return cbk();
179
+ }
180
+
181
+ return getChannels({
182
+ lnd: args.lnd,
183
+ partner_public_key: recoveryDetails.incoming_peer,
184
+ },
185
+ cbk);
186
+ }],
187
+
173
188
  // Get the refund key
174
189
  getRefundKey: [
175
190
  'ecp',
@@ -213,6 +228,7 @@ module.exports = (args, cbk) => {
213
228
  return;
214
229
  }
215
230
 
231
+ const channels = invoice.payments.map(n => n.in_channel);
216
232
  const hash = recoveryDetails.claim_coop_public_key_hash;
217
233
  const timeout = min(...invoice.payments.map(n => n.timeout));
218
234
 
@@ -231,7 +247,11 @@ module.exports = (args, cbk) => {
231
247
 
232
248
  args.emitter.emit('update', {execution_payment_held: id});
233
249
 
234
- return cbk(null, {delta, claim_coop_public_key: message.value});
250
+ return cbk(null, {
251
+ channels,
252
+ delta,
253
+ claim_coop_public_key: message.value,
254
+ });
235
255
  });
236
256
  }],
237
257
 
@@ -257,13 +277,14 @@ module.exports = (args, cbk) => {
257
277
 
258
278
  args.emitter.emit('update', {offchain_funding_held: id});
259
279
 
280
+ const channels = invoice.payments.map(n => n.in_channel);
260
281
  const timeout = min(...invoice.payments.map(n => n.timeout));
261
282
 
262
283
  const delta = timeout - recoveryDetails.timeout;
263
284
 
264
285
  sub.removeAllListeners();
265
286
 
266
- return cbk(null, {delta});
287
+ return cbk(null, {channels, delta});
267
288
  });
268
289
 
269
290
  return;
@@ -423,12 +444,52 @@ module.exports = (args, cbk) => {
423
444
  cbk);
424
445
  }],
425
446
 
447
+ // Check incoming peer constraints
448
+ checkIncomingPeer: [
449
+ 'getChannels',
450
+ 'waitForDepositHold',
451
+ 'waitForFundHold',
452
+ ({getChannels, waitForDepositHold, waitForFundHold}, cbk) =>
453
+ {
454
+ // Exit early when there are no incoming peer constraints
455
+ if (!args.incoming_peer) {
456
+ return cbk();
457
+ }
458
+
459
+ const channels = getChannels.channels.map(n => n.id);
460
+
461
+ // The deposit must come in under a channel with the incoming peer
462
+ const invalidDeposit = waitForDepositHold.channels.find(id => {
463
+ return !channels.includes(id);
464
+ });
465
+
466
+ if (!!invalidDeposit) {
467
+ return cbk([503, 'UnexpectedInboundChannelForHeldDeposit']);
468
+ }
469
+
470
+ // The funding must come in under a channel with the incoming peer
471
+ const invalidFunding = waitForFundHold.channels.find(id => {
472
+ return !channels.includes(id);
473
+ });
474
+
475
+ if (!!invalidFunding) {
476
+ return cbk([503, 'UnexpectedInboundChannelForHeldFunding']);
477
+ }
478
+
479
+ return cbk();
480
+ }],
481
+
426
482
  // Check that there are sufficient remaining blocks
427
483
  checkTimeRemaining: [
428
484
  'recoveryDetails',
429
485
  'waitForDepositHold',
430
486
  'waitForFundHold',
431
- ({recoveryDetails, waitForDepositHold, waitForFundHold}, cbk) =>
487
+ ({
488
+ recoveryDetails,
489
+ waitForDepositHold,
490
+ waitForFundHold,
491
+ },
492
+ cbk) =>
432
493
  {
433
494
  if (waitForDepositHold.delta < defaultMinDelta) {
434
495
  return cbk([503, 'InsufficientDepositDeltaBlocksToFundSwap']);
@@ -458,6 +519,7 @@ module.exports = (args, cbk) => {
458
519
 
459
520
  // Lock chain funds to fund the swap with
460
521
  lockFunding: [
522
+ 'checkIncomingPeer',
461
523
  'checkTimeRemaining',
462
524
  'getTransactions',
463
525
  'swap',