/
scriptutil.js
365 lines (304 loc) · 9.28 KB
/
scriptutil.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
'use strict';
var bcoin = require('bcoin');
var bn = bcoin.bn;
var constants = bcoin.constants;
var utils = require('bcoin/lib/utils/utils');
var crypto = require('bcoin/lib/crypto/crypto');
var assert = utils.assert;
var opcodes = constants.opcodes;
var hashType = constants.hashType;
var util = exports;
util.toWitnessScripthash = function(redeem) {
return bcoin.script.fromProgram(0, redeem.sha256());
};
util.toMultisig = function(k1, k2) {
var script = new bcoin.script();
var k;
// Note: It looks like lnd orders these in reverse.
if (utils.cmp(k1, k2) < 0) {
k = k1;
k1 = k2;
k2 = k;
}
script.push(opcodes.OP_2);
script.push(k1);
script.push(k2);
script.push(opcodes.OP_2);
script.push(opcodes.OP_CHECKMULTISIG);
script.compile();
return script;
};
util.fundingRedeem = function(k1, k2, value) {
var redeem = util.toMultisig(k1, k2);
var output = new bcoin.output();
output.script = util.toWitnessScripthash(redeem);
output.value = value;
assert(value > 0);
return {
redeem: redeem,
output: output
};
};
util.spendMultisig = function(redeem, k1, s1, k2, s2) {
var witness = new bcoin.witness();
witness.push(new Buffer(0));
// Note: It looks like lnd orders these in reverse.
if (utils.cmp(k1, k2) < 0) {
witness.push(s2);
witness.push(s1);
} else {
witness.push(s1);
witness.push(s2);
}
witness.push(redeem.toRaw());
witness.compile();
return witness;
};
util.findOutput = function(tx, script) {
var i, output;
for (i = 0; i < tx.outputs.length; i++) {
output = tx.outputs[i];
if (utils.equal(output.script.toRaw(), script.toRaw()))
return i;
}
return -1;
};
util.createSenderHTLC = function(absTimeout, relTimeout, senderKey, recKey, revHash, payHash) {
var script = new bcoin.script();
script.push(opcodes.OP_IF);
script.push(opcodes.OP_IF);
script.push(revHash);
script.push(opcodes.OP_ELSE);
script.push(opcodes.OP_SIZE);
script.push(new bn(32));
script.push(opcodes.OP_EQUALVERIFY);
script.push(payHash);
script.push(opcodes.OP_ENDIF);
script.push(opcodes.OP_SWAP);
script.push(opcodes.OP_SHA256);
script.push(opcodes.OP_EQUALVERIFY);
script.push(recKey);
script.push(opcodes.OP_CHECKSIG);
script.push(opcodes.OP_ELSE);
script.push(new bn(absTimeout));
script.push(opcodes.OP_CHECKLOCKTIMEVERIFY);
script.push(new bn(relTimeout));
script.push(opcodes.OP_CHECKSEQUENCEVERIFY);
script.push(opcodes.OP_2DROP);
script.push(senderKey);
script.push(opcodes.OP_CHECKSIG);
script.push(opcodes.OP_ENDIF);
script.compile();
return script;
};
util.senderSpendRedeem = function(commitScript, recKey, sweep, payImage) {
var sig = sweep.signature(0, commitScript, recKey, hashType.ALL, 1);
var witness = new bcoin.witness();
witness.push(sig);
witness.push(payImage);
witness.push(new bn(0));
witness.push(new bn(1));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.senderSpendRevoke = function(commitScript, recKey, sweep, revImage) {
var sig = sweep.signature(0, commitScript, recKey, hashType.ALL, 1);
var witness = new bcoin.witness();
witness.push(sig);
witness.push(revImage);
witness.push(new bn(1));
witness.push(new bn(1));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.senderSpendTimeout = function(commitScript, senderKey, sweep, absTimeout, relTime) {
var sig, witness;
sweep.setSequence(0, relTime);
sweep.setLocktime(absTimeout);
sig = sweep.signature(0, commitScript, senderKey, hashType.ALL, 1);
witness = new bcoin.witness();
witness.push(sig);
witness.push(new bn(0));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.createReceiverHTLC = function(absTimeout, relTimeout, senderKey, recKey, revHash, payHash) {
var script = new bcoin.script();
script.push(opcodes.OP_IF);
script.push(opcodes.OP_SIZE);
script.push(new bn(32));
script.push(opcodes.OP_EQUALVERIFY);
script.push(opcodes.OP_SHA256);
script.push(payHash);
script.push(opcodes.OP_EQUALVERIFY);
script.push(new bn(relTimeout));
script.push(opcodes.OP_CHECKSEQUENCEVERIFY);
script.push(opcodes.OP_DROP);
script.push(recKey);
script.push(opcodes.OP_CHECKSIG);
script.push(opcodes.OP_ELSE);
script.push(opcodes.OP_IF);
script.push(opcodes.OP_SHA256);
script.push(revHash);
script.push(opcodes.OP_EQUALVERIFY);
script.push(opcodes.OP_ELSE);
script.push(new bn(absTimeout));
script.push(opcodes.OP_CHECKLOCKTIMEVERIFY);
script.push(opcodes.OP_DROP);
script.push(opcodes.OP_ENDIF);
script.push(senderKey);
script.push(opcodes.OP_CHECKSIG);
script.push(opcodes.OP_ENDIF);
script.compile();
return script;
};
util.recSpendRedeem = function(commitScript, recKey, sweep, payImage, relTime) {
var sig, witness;
sweep.setSequence(0, relTime);
sig = sweep.signature(0, commitScript, recKey, hashType.ALL, 1);
witness = new bcoin.witness();
witness.push(sig);
witness.push(payImage);
witness.push(new bn(1));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.recSpendRevoke = function(commitScript, senderKey, sweep, revImage) {
var sig = sweep.signature(0, commitScript, senderKey, hashType.ALL, 1);
var witness = new bcoin.witness();
witness.push(sig);
witness.push(revImage);
witness.push(new bn(1));
witness.push(new bn(0));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.recSpendTimeout = function(commitScript, senderKey, sweep, absTimeout) {
var sig, witness;
sweep.setLocktime(absTimeout);
sig = sweep.signature(0, commitScript, senderKey, hashType.ALL, 1);
witness = new bcoin.witness();
witness.push(sig);
witness.push(new bn(0));
witness.push(new bn(0));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.commitSelf = function commitSelf(csvTime, selfKey, revKey) {
var script = new bcoin.script();
script.push(opcodes.OP_IF);
script.push(revKey);
script.push(opcodes.OP_CHECKSIG);
script.push(opcodes.OP_ELSE);
script.push(selfKey);
script.push(opcodes.OP_CHECKSIGVERIFY);
script.push(new bn(csvTime));
script.push(opcodes.OP_CHECKSEQUENCEVERIFY);
script.push(opcodes.OP_ENDIF);
script.compile();
return script;
};
util.commitUnencumbered = function commitUnencumbered(key) {
return bcoin.script.fromProgram(0, crypto.hash160(key));
};
util.commitSpendTimeout = function commitSpendTimeout(commitScript, blockTimeout, selfKey, sweep) {
var sig, witness;
sweep.setSequence(0, blockTimeout);
sig = sweep.signature(0, commitScript, selfKey, hashType.ALL, 1);
witness = new bcoin.witness();
witness.push(sig);
witness.push(new bn(0));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.commitSpendRevoke = function commitSpendRevoke(commitScript, revPriv, sweep) {
var sig = sweep.signature(0, commitScript, revPriv, hashType.ALL, 1);
var witness = new bcoin.witness();
witness.push(sig);
witness.push(new bn(1));
witness.push(commitScript.toRaw());
witness.compile();
return witness;
};
util.commitSpendNoDelay = function commitSpendNoDelay(commitScript, commitPriv, sweep) {
var pkh = bcoin.script.fromPubkeyhash(commitScript.get(1));
var sig = sweep.signature(0, pkh, commitPriv, hashType.ALL, 1);
var witness = new bcoin.witness();
witness.push(sig);
witness.push(bcoin.ec.publicKeyCreate(commitPriv, true));
witness.compile();
return witness;
};
util.deriveRevPub = function(commitPub, revImage) {
return bcoin.ec.publicKeyTweakAdd(commitPub, revImage, true);
};
util.deriveRevPriv = function(commitPriv, revImage) {
return bcoin.ec.privateKeyTweakAdd(commitPriv, revImage);
};
util.deriveElkremRoot = function(localKey, remoteKey) {
var secret = localKey; // private
var salt = remoteKey; // public
var info = new Buffer('elkrem', 'ascii');
var prk = crypto.hkdfExtract(secret, salt, 'sha256');
var root = crypto.hkdfExpand(prk, info, 32, 'sha256');
return root;
};
util.createCommitTX = function(
fundingOutput, selfKey, theirKey, revKey,
csvTimeout, valueToSelf, valueToThem
) {
var ourRedeem = util.commitSelf(csvTimeout, selfKey, revKey);
var payToUs = util.toWitnessScripthash(ourRedeem);
var payToThem = util.commitUnencumbered(theirKey);
var tx = new bcoin.mtx();
var output;
tx.version = 2;
tx.addInput(fundingOutput);
if (valueToSelf > 0) {
output = new bcoin.output();
output.value = valueToSelf;
output.script = payToUs;
tx.addOutput(output);
}
if (valueToThem > 0) {
output = new bcoin.output();
output.value = valueToThem;
output.script = payToThem;
tx.addOutput(output);
}
return tx;
};
util.createCooperativeClose = function createCooperativeClose(
fundingInput, ourBalance, theirBalance,
ourDeliveryScript, theirDeliveryScript,
initiator
) {
var tx = new bcoin.mtx();
var output;
tx.addInput(fundingInput);
if (initiator)
ourBalance -= 5000;
else
theirBalance -= 5000;
if (ourBalance > 0) {
output = new bcoin.output();
output.script = ourDeliveryScript;
output.value = ourBalance;
tx.addOutput(output);
}
if (theirBalance > 0) {
output = new bcoin.output();
output.script = theirDeliveryScript;
output.value = theirBalance;
tx.addOutput(output);
}
tx.sortMembers();
return tx;
};