forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.c
736 lines (648 loc) · 19.6 KB
/
script.c
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include "address.h"
#include "locktime.h"
#include "preimage.h"
#include "pubkey.h"
#include "script.h"
#include <assert.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/endian/endian.h>
#include <ccan/mem/mem.h>
/* Some standard ops */
#define OP_0 0x00
#define OP_PUSHBYTES(val) (val)
#define OP_PUSHDATA1 0x4C
#define OP_PUSHDATA2 0x4D
#define OP_PUSHDATA4 0x4E
#define OP_NOP 0x61
#define OP_IF 0x63
#define OP_NOTIF 0x64
#define OP_ELSE 0x67
#define OP_ENDIF 0x68
#define OP_RETURN 0x6a
#define OP_2DROP 0x6d
#define OP_DEPTH 0x74
#define OP_DROP 0x75
#define OP_DUP 0x76
#define OP_SWAP 0x7c
#define OP_EQUAL 0x87
#define OP_EQUALVERIFY 0x88
#define OP_SIZE 0x82
#define OP_1SUB 0x8C
#define OP_ADD 0x93
#define OP_CHECKSIG 0xAC
#define OP_CHECKMULTISIG 0xAE
#define OP_HASH160 0xA9
#define OP_CHECKSEQUENCEVERIFY 0xB2
#define OP_CHECKLOCKTIMEVERIFY 0xB1
/* Bitcoin's OP_HASH160 is RIPEMD(SHA256()) */
static void hash160(struct ripemd160 *redeemhash, const void *mem, size_t len)
{
struct sha256 h;
sha256(&h, mem, len);
ripemd160(redeemhash, h.u.u8, sizeof(h));
}
static void add(u8 **scriptp, const void *mem, size_t len)
{
size_t oldlen = tal_count(*scriptp);
tal_resize(scriptp, oldlen + len);
memcpy(*scriptp + oldlen, mem, len);
}
static void add_op(u8 **scriptp, u8 op)
{
add(scriptp, &op, 1);
}
static void add_push_bytes(u8 **scriptp, const void *mem, size_t len)
{
if (len < 76)
add_op(scriptp, OP_PUSHBYTES(len));
else if (len < 256) {
char c = len;
add_op(scriptp, OP_PUSHDATA1);
add(scriptp, &c, 1);
} else if (len < 65536) {
le16 v = cpu_to_le16(len);
add_op(scriptp, OP_PUSHDATA2);
add(scriptp, &v, 2);
} else {
le32 v = cpu_to_le32(len);
add_op(scriptp, OP_PUSHDATA4);
add(scriptp, &v, 4);
}
add(scriptp, memcheck(mem, len), len);
}
static void add_number(u8 **script, u32 num)
{
if (num == 0)
add_op(script, 0);
else if (num <= 16)
add_op(script, 0x50 + num);
else {
le64 n = cpu_to_le64(num);
/* Beware: encoding is signed! */
if (num <= 0x0000007F)
add_push_bytes(script, &n, 1);
else if (num <= 0x00007FFF)
add_push_bytes(script, &n, 2);
else if (num <= 0x007FFFFF)
add_push_bytes(script, &n, 3);
else if (num <= 0x7FFFFFFF)
add_push_bytes(script, &n, 4);
else
add_push_bytes(script, &n, 5);
}
}
static void add_push_key(u8 **scriptp, const struct pubkey *key)
{
u8 der[PUBKEY_CMPR_LEN];
pubkey_to_der(der, key);
add_push_bytes(scriptp, der, sizeof(der));
}
static void add_push_sig(u8 **scriptp, const struct bitcoin_signature *sig)
{
u8 der[73];
size_t len = signature_to_der(der, sig);
add_push_bytes(scriptp, der, len);
}
static u8 *stack_key(const tal_t *ctx, const struct pubkey *key)
{
u8 der[PUBKEY_CMPR_LEN];
pubkey_to_der(der, key);
return tal_dup_arr(ctx, u8, der, sizeof(der), 0);
}
/* Bitcoin wants DER encoding. */
static u8 *stack_sig(const tal_t *ctx, const struct bitcoin_signature *sig)
{
u8 der[73];
size_t len = signature_to_der(der, sig);
return tal_dup_arr(ctx, u8, der, len, 0);
}
static u8 *stack_preimage(const tal_t *ctx, const struct preimage *preimage)
{
return tal_dup_arr(ctx, u8, preimage->r, sizeof(preimage->r), 0);
}
/* Bitcoin script stack values are a special, special snowflake.
*
* They're little endian values, but 0 is an empty value. We only
* handle single byte values here. */
static u8 *stack_number(const tal_t *ctx, unsigned int num)
{
u8 val;
if (num == 0)
return tal_arr(ctx, u8, 0);
val = num;
assert(val == num);
/* We use tal_dup_arr since we want tal_count() to work */
return tal_dup_arr(ctx, u8, &val, 1, 0);
}
/* tal_count() gives the length of the script. */
u8 *bitcoin_redeem_2of2(const tal_t *ctx,
const struct pubkey *key1,
const struct pubkey *key2)
{
u8 *script = tal_arr(ctx, u8, 0);
add_number(&script, 2);
if (pubkey_cmp(key1, key2) < 0) {
add_push_key(&script, key1);
add_push_key(&script, key2);
} else {
add_push_key(&script, key2);
add_push_key(&script, key1);
}
add_number(&script, 2);
add_op(&script, OP_CHECKMULTISIG);
return script;
}
u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash)
{
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_HASH160);
add_push_bytes(&script, redeemhash->u.u8, sizeof(redeemhash->u.u8));
add_op(&script, OP_EQUAL);
assert(tal_count(script) == BITCOIN_SCRIPTPUBKEY_P2SH_LEN);
return script;
}
/* Create p2sh for this redeem script. */
u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript)
{
struct ripemd160 redeemhash;
hash160(&redeemhash, redeemscript, tal_count(redeemscript));
return scriptpubkey_p2sh_hash(ctx, &redeemhash);
}
/* Create an output script using p2pkh */
u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr)
{
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
add_push_bytes(&script, &addr->addr, sizeof(addr->addr));
add_op(&script, OP_EQUALVERIFY);
add_op(&script, OP_CHECKSIG);
assert(tal_count(script) == BITCOIN_SCRIPTPUBKEY_P2PKH_LEN);
return script;
}
u8 *scriptpubkey_opreturn(const tal_t *ctx)
{
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_RETURN);
return script;
}
/* Create an input script which spends p2pkh */
u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey,
const struct bitcoin_signature *sig)
{
u8 *script = tal_arr(ctx, u8, 0);
add_push_sig(&script, sig);
add_push_key(&script, pubkey);
return script;
}
/* Create the redeemscript for a P2SH + P2WPKH (for signing tx) */
u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key)
{
struct ripemd160 keyhash;
u8 *script = tal_arr(ctx, u8, 0);
/* BIP141: BIP16 redeemScript pushed in the scriptSig is exactly a
* push of a version byte plus a push of a witness program. */
add_number(&script, 0);
pubkey_to_hash160(key, &keyhash);
add_push_bytes(&script, &keyhash, sizeof(keyhash));
assert(tal_count(script) == BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN);
return script;
}
u8 *bitcoin_scriptsig_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key)
{
u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(ctx, key), *script;
/* BIP141: The scriptSig must be exactly a push of the BIP16
* redeemScript or validation fails. */
script = tal_arr(ctx, u8, 0);
add_push_bytes(&script, redeemscript, tal_count(redeemscript));
tal_free(redeemscript);
return script;
}
u8 **bitcoin_witness_p2wpkh(const tal_t *ctx,
const struct bitcoin_signature *sig,
const struct pubkey *key)
{
u8 **witness;
/* BIP141: The witness must consist of exactly 2 items (≤ 520
* bytes each). The first one a signature, and the second one
* a public key. */
witness = tal_arr(ctx, u8 *, 2);
witness[0] = stack_sig(witness, sig);
witness[1] = stack_key(witness, key);
return witness;
}
/* Create an output script for a 32-byte witness. */
u8 *scriptpubkey_p2wsh(const tal_t *ctx, const u8 *witnessscript)
{
struct sha256 h;
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_0);
sha256(&h, witnessscript, tal_count(witnessscript));
add_push_bytes(&script, h.u.u8, sizeof(h.u.u8));
assert(tal_count(script) == BITCOIN_SCRIPTPUBKEY_P2WSH_LEN);
return script;
}
/* Create an output script for a 20-byte witness. */
u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key)
{
struct ripemd160 h;
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_0);
pubkey_to_hash160(key, &h);
add_push_bytes(&script, &h, sizeof(h));
return script;
}
u8 *scriptpubkey_p2wpkh_derkey(const tal_t *ctx, const u8 der[33])
{
u8 *script = tal_arr(ctx, u8, 0);
struct ripemd160 h;
add_op(&script, OP_0);
hash160(&h, der, PUBKEY_CMPR_LEN);
add_push_bytes(&script, &h, sizeof(h));
return script;
}
u8 *scriptpubkey_witness_raw(const tal_t *ctx, u8 version,
const u8 *wprog, size_t wprog_size)
{
u8 *script = tal_arr(ctx, u8, 0);
add_number(&script, version);
add_push_bytes(&script, wprog, wprog_size);
return script;
}
/* Create a witness which spends the 2of2. */
u8 **bitcoin_witness_2of2(const tal_t *ctx,
const struct bitcoin_signature *sig1,
const struct bitcoin_signature *sig2,
const struct pubkey *key1,
const struct pubkey *key2)
{
u8 **witness = tal_arr(ctx, u8 *, 4);
/* OP_CHECKMULTISIG has an out-by-one bug, which MBZ */
witness[0] = stack_number(witness, 0);
/* sig order should match key order. */
if (pubkey_cmp(key1, key2) < 0) {
witness[1] = stack_sig(witness, sig1);
witness[2] = stack_sig(witness, sig2);
} else {
witness[1] = stack_sig(witness, sig2);
witness[2] = stack_sig(witness, sig1);
}
witness[3] = bitcoin_redeem_2of2(witness, key1, key2);
return witness;
}
/* Create scriptcode (fake witness, basically) for P2WPKH */
u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key)
{
struct ripemd160 pkhash;
u8 *script = tal_arr(ctx, u8, 0);
pubkey_to_hash160(key, &pkhash);
/* BIP143:
*
* For P2WPKH witness program, the scriptCode is
* 0x1976a914{20-byte-pubkey-hash}88ac.
*/
/* PUSH(25): OP_DUP OP_HASH160 PUSH(20) 20-byte-pubkey-hash
* OP_EQUALVERIFY OP_CHECKSIG */
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
add_push_bytes(&script, &pkhash, sizeof(pkhash));
add_op(&script, OP_EQUALVERIFY);
add_op(&script, OP_CHECKSIG);
return script;
}
bool is_p2pkh(const u8 *script, struct bitcoin_address *addr)
{
size_t script_len = tal_count(script);
if (script_len != BITCOIN_SCRIPTPUBKEY_P2PKH_LEN)
return false;
if (script[0] != OP_DUP)
return false;
if (script[1] != OP_HASH160)
return false;
if (script[2] != OP_PUSHBYTES(20))
return false;
if (script[23] != OP_EQUALVERIFY)
return false;
if (script[24] != OP_CHECKSIG)
return false;
if (addr)
memcpy(addr, script+3, 20);
return true;
}
bool is_p2sh(const u8 *script, struct ripemd160 *addr)
{
size_t script_len = tal_count(script);
if (script_len != BITCOIN_SCRIPTPUBKEY_P2SH_LEN)
return false;
if (script[0] != OP_HASH160)
return false;
if (script[1] != OP_PUSHBYTES(20))
return false;
if (script[22] != OP_EQUAL)
return false;
if (addr)
memcpy(addr, script+2, 20);
return true;
}
bool is_p2wsh(const u8 *script, struct sha256 *addr)
{
size_t script_len = tal_count(script);
if (script_len != BITCOIN_SCRIPTPUBKEY_P2WSH_LEN)
return false;
if (script[0] != OP_0)
return false;
if (script[1] != OP_PUSHBYTES(sizeof(struct sha256)))
return false;
if (addr)
memcpy(addr, script+2, sizeof(struct sha256));
return true;
}
bool is_p2wpkh(const u8 *script, struct bitcoin_address *addr)
{
size_t script_len = tal_count(script);
if (script_len != BITCOIN_SCRIPTPUBKEY_P2WPKH_LEN)
return false;
if (script[0] != OP_0)
return false;
if (script[1] != OP_PUSHBYTES(sizeof(struct ripemd160)))
return false;
if (addr)
memcpy(addr, script+2, sizeof(*addr));
return true;
}
u8 **bitcoin_witness_sig_and_element(const tal_t *ctx,
const struct bitcoin_signature *sig,
const void *elem, size_t elemsize,
const u8 *witnessscript)
{
u8 **witness = tal_arr(ctx, u8 *, 3);
witness[0] = stack_sig(witness, sig);
witness[1] = tal_dup_arr(witness, u8, elem, elemsize, 0);
witness[2] = tal_dup_arr(witness, u8,
witnessscript, tal_count(witnessscript), 0);
return witness;
}
/* BOLT #3:
*
* This output sends funds back to the owner of this commitment transaction and
* thus must be timelocked using `OP_CHECKSEQUENCEVERIFY`. It can be claimed, without delay,
* by the other party if they know the revocation private key. The output is a
* version-0 P2WSH, with a witness script:
*
* OP_IF
* # Penalty transaction
* <revocationpubkey>
* OP_ELSE
* `to_self_delay`
* OP_CHECKSEQUENCEVERIFY
* OP_DROP
* <local_delayedpubkey>
* OP_ENDIF
* OP_CHECKSIG
*/
u8 *bitcoin_wscript_to_local(const tal_t *ctx, u16 to_self_delay,
const struct pubkey *revocation_pubkey,
const struct pubkey *local_delayedkey)
{
u8 *script = tal_arr(ctx, u8, 0);
add_op(&script, OP_IF);
add_push_key(&script, revocation_pubkey);
add_op(&script, OP_ELSE);
add_number(&script, to_self_delay);
add_op(&script, OP_CHECKSEQUENCEVERIFY);
add_op(&script, OP_DROP);
add_push_key(&script, local_delayedkey);
add_op(&script, OP_ENDIF);
add_op(&script, OP_CHECKSIG);
return script;
}
/* BOLT #3:
*
* #### Offered HTLC Outputs
*
* This output sends funds to either an HTLC-timeout transaction after the
* HTLC-timeout or to the remote node using the payment preimage or the
* revocation key. The output is a P2WSH, with a witness script:
*
* # To remote node with revocation key
* OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
* OP_IF
* OP_CHECKSIG
* OP_ELSE
* <remote_htlcpubkey> OP_SWAP OP_SIZE 32 OP_EQUAL
* OP_NOTIF
* # To local node via HTLC-timeout transaction (timelocked).
* OP_DROP 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
* OP_ELSE
* # To remote node with preimage.
* OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
* OP_CHECKSIG
* OP_ENDIF
* OP_ENDIF
*/
u8 *bitcoin_wscript_htlc_offer_ripemd160(const tal_t *ctx,
const struct pubkey *localhtlckey,
const struct pubkey *remotehtlckey,
const struct ripemd160 *payment_ripemd,
const struct pubkey *revocationkey)
{
u8 *script = tal_arr(ctx, u8, 0);
struct ripemd160 ripemd;
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
pubkey_to_hash160(revocationkey, &ripemd);
add_push_bytes(&script, &ripemd, sizeof(ripemd));
add_op(&script, OP_EQUAL);
add_op(&script, OP_IF);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ELSE);
add_push_key(&script, remotehtlckey);
add_op(&script, OP_SWAP);
add_op(&script, OP_SIZE);
add_number(&script, 32);
add_op(&script, OP_EQUAL);
add_op(&script, OP_NOTIF);
add_op(&script, OP_DROP);
add_number(&script, 2);
add_op(&script, OP_SWAP);
add_push_key(&script, localhtlckey);
add_number(&script, 2);
add_op(&script, OP_CHECKMULTISIG);
add_op(&script, OP_ELSE);
add_op(&script, OP_HASH160);
add_push_bytes(&script,
payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8));
add_op(&script, OP_EQUALVERIFY);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ENDIF);
add_op(&script, OP_ENDIF);
return script;
}
u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx,
const struct pubkey *localhtlckey,
const struct pubkey *remotehtlckey,
const struct sha256 *payment_hash,
const struct pubkey *revocationkey)
{
struct ripemd160 ripemd;
ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u));
return bitcoin_wscript_htlc_offer_ripemd160(ctx, localhtlckey,
remotehtlckey,
&ripemd, revocationkey);
}
/* BOLT #3:
*
* #### Received HTLC Outputs
*
* This output sends funds to either the remote node after the HTLC-timeout or
* using the revocation key, or to an HTLC-success transaction with a
* successful payment preimage. The output is a P2WSH, with a witness script:
*
* # To remote node with revocation key
* OP_DUP OP_HASH160 <RIPEMD160(SHA256(revocationpubkey))> OP_EQUAL
* OP_IF
* OP_CHECKSIG
* OP_ELSE
* <remote_htlcpubkey> OP_SWAP
* OP_SIZE 32 OP_EQUAL
* OP_IF
* # To local node via HTLC-success transaction.
* OP_HASH160 <RIPEMD160(payment_hash)> OP_EQUALVERIFY
* 2 OP_SWAP <local_htlcpubkey> 2 OP_CHECKMULTISIG
* OP_ELSE
* # To remote node after timeout.
* OP_DROP <cltv_expiry> OP_CHECKLOCKTIMEVERIFY OP_DROP
* OP_CHECKSIG
* OP_ENDIF
* OP_ENDIF
*/
u8 *bitcoin_wscript_htlc_receive_ripemd(const tal_t *ctx,
const struct abs_locktime *htlc_abstimeout,
const struct pubkey *localhtlckey,
const struct pubkey *remotehtlckey,
const struct ripemd160 *payment_ripemd,
const struct pubkey *revocationkey)
{
u8 *script = tal_arr(ctx, u8, 0);
struct ripemd160 ripemd;
add_op(&script, OP_DUP);
add_op(&script, OP_HASH160);
pubkey_to_hash160(revocationkey, &ripemd);
add_push_bytes(&script, &ripemd, sizeof(ripemd));
add_op(&script, OP_EQUAL);
add_op(&script, OP_IF);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ELSE);
add_push_key(&script, remotehtlckey);
add_op(&script, OP_SWAP);
add_op(&script, OP_SIZE);
add_number(&script, 32);
add_op(&script, OP_EQUAL);
add_op(&script, OP_IF);
add_op(&script, OP_HASH160);
add_push_bytes(&script,
payment_ripemd->u.u8, sizeof(payment_ripemd->u.u8));
add_op(&script, OP_EQUALVERIFY);
add_number(&script, 2);
add_op(&script, OP_SWAP);
add_push_key(&script, localhtlckey);
add_number(&script, 2);
add_op(&script, OP_CHECKMULTISIG);
add_op(&script, OP_ELSE);
add_op(&script, OP_DROP);
add_number(&script, htlc_abstimeout->locktime);
add_op(&script, OP_CHECKLOCKTIMEVERIFY);
add_op(&script, OP_DROP);
add_op(&script, OP_CHECKSIG);
add_op(&script, OP_ENDIF);
add_op(&script, OP_ENDIF);
return script;
}
u8 *bitcoin_wscript_htlc_receive(const tal_t *ctx,
const struct abs_locktime *htlc_abstimeout,
const struct pubkey *localhtlckey,
const struct pubkey *remotehtlckey,
const struct sha256 *payment_hash,
const struct pubkey *revocationkey)
{
struct ripemd160 ripemd;
ripemd160(&ripemd, payment_hash->u.u8, sizeof(payment_hash->u));
return bitcoin_wscript_htlc_receive_ripemd(ctx, htlc_abstimeout,
localhtlckey, remotehtlckey,
&ripemd, revocationkey);
}
/* BOLT #3:
*
* ## HTLC-Timeout and HTLC-Success Transactions
*
*...
* * `txin[0]` witness stack: `0 <remotehtlcsig> <localhtlcsig> <payment_preimage>` for HTLC-success, `0 <remotehtlcsig> <localhtlcsig> 0` for HTLC-timeout
*/
u8 **bitcoin_witness_htlc_timeout_tx(const tal_t *ctx,
const struct bitcoin_signature *localhtlcsig,
const struct bitcoin_signature *remotehtlcsig,
const u8 *wscript)
{
u8 **witness = tal_arr(ctx, u8 *, 5);
witness[0] = stack_number(witness, 0);
witness[1] = stack_sig(witness, remotehtlcsig);
witness[2] = stack_sig(witness, localhtlcsig);
witness[3] = stack_number(witness, 0);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_count(wscript), 0);
return witness;
}
u8 **bitcoin_witness_htlc_success_tx(const tal_t *ctx,
const struct bitcoin_signature *localhtlcsig,
const struct bitcoin_signature *remotesig,
const struct preimage *preimage,
const u8 *wscript)
{
u8 **witness = tal_arr(ctx, u8 *, 5);
witness[0] = stack_number(witness, 0);
witness[1] = stack_sig(witness, remotesig);
witness[2] = stack_sig(witness, localhtlcsig);
witness[3] = stack_preimage(witness, preimage);
witness[4] = tal_dup_arr(witness, u8, wscript, tal_count(wscript), 0);
return witness;
}
u8 *bitcoin_wscript_htlc_tx(const tal_t *ctx,
u16 to_self_delay,
const struct pubkey *revocation_pubkey,
const struct pubkey *local_delayedkey)
{
u8 *script = tal_arr(ctx, u8, 0);
/* BOLT #3:
*
* The witness script for the output is:
*
* OP_IF
* # Penalty transaction
* <revocationpubkey>
* OP_ELSE
* `to_self_delay`
* OP_CHECKSEQUENCEVERIFY
* OP_DROP
* <local_delayedpubkey>
* OP_ENDIF
* OP_CHECKSIG
*/
add_op(&script, OP_IF);
add_push_key(&script, revocation_pubkey);
add_op(&script, OP_ELSE);
add_number(&script, to_self_delay);
add_op(&script, OP_CHECKSEQUENCEVERIFY);
add_op(&script, OP_DROP);
add_push_key(&script, local_delayedkey);
add_op(&script, OP_ENDIF);
add_op(&script, OP_CHECKSIG);
return script;
}
bool scripteq(const u8 *s1, const u8 *s2)
{
memcheck(s1, tal_count(s1));
memcheck(s2, tal_count(s2));
if (tal_count(s1) != tal_count(s2))
return false;
return memcmp(s1, s2, tal_count(s1)) == 0;
}