-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate-test-vector.py
More file actions
executable file
·1481 lines (1237 loc) · 57.1 KB
/
Copy pathgenerate-test-vector.py
File metadata and controls
executable file
·1481 lines (1237 loc) · 57.1 KB
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import reference
import json
from secp256k1 import *
from bitcoin_utils import deser_txid, hash160, COutPoint
import bip32
from copy import deepcopy
from importlib import reload
reload(reference)
G = ECKey().set(1).get_pubkey()
sending_test_vectors = []
HRP="sp"
#TODO: use clearly mock signatures, e.g. 010203040506...
def get_key_pair(index, seed=b'deadbeef', derivation='m/0h'):
master = bip32.BIP32.from_seed(seed)
d = ECKey().set(master.get_privkey_from_path(f'{derivation}/{index}'))
P = d.get_pubkey()
return d, P
def add_private_keys(inputs, input_priv_keys):
for x, i in enumerate(inputs):
i['private_key'] = input_priv_keys[x][0].get_bytes().hex()
return inputs
def get_p2pkh_scriptsig(pub_key, priv_key):
msg = hashlib.sha256(b'message').digest()
sig = priv_key.sign_ecdsa(msg, low_s=False, rfc6979=True).hex()
s = len(sig) // 2
if pub_key.compressed:
pubkey_bytes = bytes([0x21]) + pub_key.get_bytes(False)
else:
pubkey_bytes = bytes([0x41]) + pub_key.get_bytes(False)
return f'{s:0x}' + sig + pubkey_bytes.hex()
def get_p2pkh_scriptPubKey(pub_key):
pubkey_bytes = pub_key.get_bytes(False)
return "76a914" + reference.hash160(pubkey_bytes).hex() + "88ac"
def get_p2tr_witness(priv_key):
msg = hashlib.sha256(b'message').digest()
sig = priv_key.sign_schnorr(msg).hex()
return serialize_witness_stack([sig])
def get_p2tr_scriptPubKey(pub_key):
return "5120" + pub_key.get_bytes(True).hex()
def serialize_witness_stack(stack_items):
stack_size = len(stack_items)
result = f'{stack_size:02x}'
for item in stack_items:
size = len(item) // 2
result += f'{size:02x}' + item
return result
def new_test_case():
recipient = {
"given": {
"vin": [],
"outputs": [],
"key_material": {
"spend_priv_key": "hex",
"scan_priv_key": "hex",
},
"labels": [],
},
"expected": {
"addresses": [],
"outputs": [],
}
}
sender = {
"given": {
"vin": [],
"recipients": []
},
"expected": {
"outputs": []
}
}
test_case = {
"comment": "",
"sending": [],
"receiving": [],
}
return sender, recipient, test_case
# In[10]:
def generate_labeled_output_tests():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
test_cases = []
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
]
sender_bip32_seed = 'deadbeef'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
recipient_bip32_seed = 'f00dbabe'
b_scan, b_spend, B_scan, B_spend = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
label_ints = [2, 3, 1001337]
address = reference.encode_silent_payment_address(B_scan, B_spend, hrp=HRP)
labeled_addresses = [
reference.create_labeled_silent_payment_address(b_scan, B_spend, case, hrp=HRP) for case in label_ints
]
recipient_addresses = [address] + labeled_addresses
comments = ["Receiving with labels: label with even parity", "Receiving with labels: label with odd parity", "Receiving with labels: large label integer"]
for i, case in enumerate(label_ints):
sender, recipient, test_case = new_test_case()
address = reference.create_labeled_silent_payment_address(b_scan, B_spend, case, hrp=HRP)
addresses = [(address, 1.0)]
inputs = []
for i, outpoint in enumerate(outpoints):
inputs += [{
'txid': outpoint[0],
'vout': outpoint[1],
'scriptSig': get_p2pkh_scriptsig(input_pub_keys[i], input_priv_keys[i][0]),
'txinwitness': '',
'prevout': {'scriptPubKey': {'hex': get_p2pkh_scriptPubKey(input_pub_keys[i])}},
}]
sender['given']['vin'] = add_private_keys(deepcopy(inputs), input_priv_keys)
sender['given']['recipients'] = addresses
recipient['given']['vin'] = inputs
recipient['given']['key_material']['scan_priv_key'] = b_scan.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = b_spend.get_bytes().hex()
recipient['expected']['addresses'] = recipient_addresses
recipient['given']['labels'] = label_ints
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, addresses, hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [r[0] for r in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
b_scan,
B_spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
labels={(reference.generate_label(b_scan, l)*G).get_bytes(False).hex():reference.generate_label(b_scan, l).hex() for l in label_ints},
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = b_spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
test_case["comment"] = comments[i]
test_cases.append(test_case)
return test_cases
def generate_single_output_outpoint_tests():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
outpoint_test_cases = [
[
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0)
],
[
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0)
],
[
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 3),
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 7)
],
[
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 7),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 3)
],
]
sender_bip32_seed = 'deadbeef'
recipient_bip32_seed = 'f00dbabe'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1)
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
test_cases = []
comments = [
"Simple send: two inputs",
"Simple send: two inputs, order reversed",
"Simple send: two inputs from the same transaction",
"Simple send: two inputs from the same transaction, order reversed"
]
for i, outpoints in enumerate(outpoint_test_cases):
sender, recipient, test_case = new_test_case()
test_case["comment"] = comments[i]
inputs = []
for i, outpoint in enumerate(outpoints):
inputs += [{
'txid': outpoint[0],
'vout': outpoint[1],
'scriptSig': get_p2pkh_scriptsig(input_pub_keys[i], input_priv_keys[i][0]),
'txinwitness': '',
'prevout': {'scriptPubKey': {'hex': get_p2pkh_scriptPubKey(input_pub_keys[i])}},
}]
sender['given']['vin'] = add_private_keys(deepcopy(inputs), input_priv_keys)
recipient['given']['vin'] = inputs
b_scan, b_spend, B_scan, B_spend = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
recipient['given']['key_material']['scan_priv_key'] = b_scan.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = b_spend.get_bytes().hex()
address = reference.encode_silent_payment_address(B_scan, B_spend, hrp=HRP)
sender['given']['recipients'].extend([(address, 1.0)])
recipient['expected']['addresses'].extend([address])
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, [(address, 1.0)], hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
b_scan,
B_spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = b_spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
test_cases.append(test_case)
return test_cases
def generate_multiple_output_tests():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
recipient_test_cases = []
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
]
sender_bip32_seed = 'deadbeef'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
recipient_one_bip32_seed = 'f00dbabe'
recipient_two_bip32_seed = 'decafbad'
scan1, spend1, Scan1, Spend1 = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_one_bip32_seed))
address1 = reference.encode_silent_payment_address(Scan1, Spend1, hrp=HRP)
addresses1 = [(address1, amount) for amount in [2.0, 3.0]]
scan2, spend2, Scan2, Spend2 = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_two_bip32_seed))
address2 = reference.encode_silent_payment_address(Scan2, Spend2, hrp=HRP)
addresses2 = [(address2, amount) for amount in [4.0, 5.0]]
test_cases = []
sender, recipient1, test_case = new_test_case()
sender1 = deepcopy(sender)
recipient2 = deepcopy(recipient1)
test_case2 = deepcopy(test_case)
inputs = []
for i, outpoint in enumerate(outpoints):
inputs += [{
'txid': outpoint[0],
'vout': outpoint[1],
'scriptSig': get_p2pkh_scriptsig(input_pub_keys[i], input_priv_keys[i][0]),
'txinwitness': '',
'prevout': {'scriptPubKey': {'hex': get_p2pkh_scriptPubKey(input_pub_keys[i])}},
}]
sender1['given']['vin'] = sender['given']['vin'] = add_private_keys(deepcopy(inputs), input_priv_keys)
sender['given']['recipients'] = addresses1
recipient1['given']['vin'] = inputs
recipient2['given']['vin'] = inputs
recipient1['given']['key_material']['scan_priv_key'] = scan1.get_bytes().hex()
recipient1['given']['key_material']['spend_priv_key'] = spend1.get_bytes().hex()
recipient1['expected']['addresses'] = [address1]
recipient2['given']['key_material']['scan_priv_key'] = scan2.get_bytes().hex()
recipient2['given']['key_material']['spend_priv_key'] = spend2.get_bytes().hex()
recipient2['expected']['addresses'] = [address2]
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, addresses1, hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient1['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
scan1,
Spend1,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = spend1.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient1['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient1])
test_case["comment"] = "Multiple outputs: multiple outputs, same recipient"
test_cases.append(test_case)
sender1['given']['recipients'] = addresses1 + addresses2
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, addresses1 + addresses2, hrp=HRP)
sender1['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient1['given']['outputs'] = output_pub_keys
recipient2['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
scan2,
Spend2,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = spend2.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient2['expected']['outputs'] = add_to_wallet
test_case2['sending'].extend([sender1])
test_case2['receiving'].extend([recipient1, recipient2])
test_case2["comment"] = "Multiple outputs: multiple outputs, multiple recipients"
test_cases.append(test_case2)
return test_cases
# In[13]:
def generate_paying_to_self_test():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0)
]
sender_bip32_seed = 'deadbeef'
recipient_bip32_seed = 'deadbeef'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1)
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
sender, recipient, test_case = new_test_case()
sender['given']['outpoints'] = outpoints
recipient['given']['outpoints'] = outpoints
sender['given']['input_priv_keys'].extend([i1.get_bytes().hex(), i2.get_bytes().hex()])
recipient['given']['input_pub_keys'].extend([I1.get_bytes(False).hex(), I2.get_bytes(False).hex()])
b_scan, b_spend, B_scan, B_spend = create_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
recipient['given']['bip32_seed'] = recipient_bip32_seed
recipient['given']['scan_priv_key'] = b_scan.get_bytes().hex()
recipient['given']['spend_priv_key'] = b_spend.get_bytes().hex()
address = reference.encode_silent_payment_address(B_scan, B_spend, hrp=HRP)
sender['given']['recipients'].extend([(address, 1.0)])
recipient['expected']['addresses'].extend([address])
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, [(address, 1.0)], hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
b_scan,
B_spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = b_spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
return test_case
def generate_multiple_outputs_with_labels_tests():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
recipient_test_cases = []
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
]
sender_bip32_seed = 'deadbeef'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
recipient_bip32_seed = 'f00dbabe'
scan1, spend1, Scan1, Spend1 = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
address = reference.encode_silent_payment_address(Scan1, Spend1, hrp=HRP)
l1 = 1
l2 = 1337
labels_one = [l1]
labels_three = [l1, l2]
label_address_one = reference.create_labeled_silent_payment_address(scan1, Spend1, m=l1, hrp=HRP)
label_address_two = reference.create_labeled_silent_payment_address(scan1, Spend1, m=l2, hrp=HRP)
addresses1 = [(address, 1.0), (label_address_one, 2.0)]
addresses2 = [(label_address_one, 3.0), (label_address_one, 4.0)]
addresses3 = [(address, 5.0), (label_address_one, 6.0), (label_address_two, 7.0), (label_address_two, 8.0)]
test_cases = []
labels = [labels_one, labels_one, labels_three]
sp_addresses = [[address, label_address_one], [address, label_address_one], [address, label_address_one, label_address_two]]
comments = [
"Multiple outputs with labels: un-labeled and labeled address; same recipient",
"Multiple outputs with labels: multiple outputs for labeled address; same recipient",
"Multiple outputs with labels: un-labeled, labeled, and multiple outputs for labeled address; multiple recipients",
]
for i, addrs in enumerate([addresses1, addresses2, addresses3]):
sender, recipient, test_case = new_test_case()
inputs = []
for i, outpoint in enumerate(outpoints):
inputs += [{
'txid': outpoint[0],
'vout': outpoint[1],
'scriptSig': get_p2pkh_scriptsig(input_pub_keys[i], input_priv_keys[i][0]),
'txinwitness': '',
'prevout': {'scriptPubKey': {'hex': get_p2pkh_scriptPubKey(input_pub_keys[i])}},
}]
sender['given']['vin'] = add_private_keys(deepcopy(inputs), input_priv_keys)
recipient['given']['vin'] = inputs
recipient['given']['key_material']['scan_priv_key'] = scan1.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = spend1.get_bytes().hex()
sender['given']['recipients'] = addrs
recipient['expected']['addresses'] = sp_addresses[i]
recipient['given']['labels'] = labels[i]
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, addrs, hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
scan1,
Spend1,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
labels={(reference.generate_label(scan1, l)*G).get_bytes(False).hex():reference.generate_label(scan1, l).hex() for l in labels[i]},
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = spend1.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
test_case["comment"] = comments[i]
test_cases.append(test_case)
return test_cases
def generate_single_output_input_tests():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0)
]
sender_bip32_seed = 'deadbeef'
recipient_bip32_seed = 'f00dbabe'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
i3, I3 = get_key_pair(2, seed=bytes.fromhex(sender_bip32_seed))
i4, I4 = get_key_pair(3, seed=bytes.fromhex(sender_bip32_seed))
if I1.get_y()%2 != 0:
i1.negate()
I1.negate()
if I2.get_y()%2 != 0:
i2.negate()
I2.negate()
if I3.get_y()%2 == 0:
i3.negate()
I3.negate()
if I4.get_y()%2 == 0:
i4.negate()
I4.negate()
address_reuse = [
[(i1, False), (i1, False)],
[I1, I1]
]
taproot_only = [
[(i1, True), (i2, True)],
[I1, I2]
]
taproot_only_with_odd_y = [
[(i1, True), (i4, True)],
[I1, I4]
]
mixed = [
[(i1, True), (i3, False)],
[I1, I3]
]
mixed_with_odd_y = [
[(i4, True), (i3, False)],
[I4, I3]
]
test_cases = []
comments = [
"Single recipient: multiple UTXOs from the same public key",
"Single recipient: taproot only inputs with even y-values",
"Single recipient: taproot only with mixed even/odd y-values",
"Single recipient: taproot input with even y-value and non-taproot input",
"Single recipient: taproot input with odd y-value and non-taproot input"
]
for i, inputs in enumerate([address_reuse, taproot_only, taproot_only_with_odd_y, mixed, mixed_with_odd_y]):
sender, recipient, test_case = new_test_case()
inp = []
for x, (key, is_taproot) in enumerate(inputs[0]):
pub_key = inputs[1][x]
if is_taproot:
inp += [{
"txid": outpoints[x][0],
"vout": outpoints[x][1],
"scriptSig": "",
"txinwitness": get_p2tr_witness(key),
"prevout": {"scriptPubKey": {"hex": get_p2tr_scriptPubKey(pub_key)}},
}]
else:
inp += [{
"txid": outpoints[x][0],
"vout": outpoints[x][1],
"scriptSig": get_p2pkh_scriptsig(pub_key, key),
"txinwitness": "",
"prevout": {"scriptPubKey": {"hex": get_p2pkh_scriptPubKey(pub_key)}},
}]
priv_keys = []
for (priv_key, is_taproot) in inputs[0]:
priv_keys += [priv_key.get_bytes().hex()]
sender['given']['vin'] = add_private_keys(deepcopy(inp), inputs[0])
recipient['given']['vin'] = inp
b_scan, b_spend, B_scan, B_spend = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
recipient['given']['key_material']['scan_priv_key'] = b_scan.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = b_spend.get_bytes().hex()
address = reference.encode_silent_payment_address(B_scan, B_spend, hrp=HRP)
sender['given']['recipients'].extend([(address, 1.0)])
recipient['expected']['addresses'].extend([address])
A_sum = sum([p if not inputs[0][i][1] or p.get_y()%2==0 else p * -1 for i, p in enumerate(inputs[1])])
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(inputs[0], deterministic_nonce, [(address, 1.0)], hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
b_scan,
B_spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = b_spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
test_case["comment"] = comments[i]
test_cases.append(test_case)
return test_cases
def generate_change_tests():
sender, recipient, test_case = new_test_case()
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
recipient_test_cases = []
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
]
sender_bip32_seed = 'deadbeef'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
input_priv_keys = [(i1, False), (i2, False)]
input_pub_keys = [I1, I2]
scan0, spend0, Scan0, Spend0 = reference.derive_silent_payment_key_pair(bytes.fromhex(sender_bip32_seed))
sender_address = reference.encode_silent_payment_address(Scan0, Spend0, hrp=HRP)
change_label = 0
change_labels = [change_label]
change_address = reference.create_labeled_silent_payment_address(scan0, Spend0, m=change_label, hrp=HRP)
recipient_bip32_seed = 'f00dbabe'
seeds = [sender_bip32_seed, recipient_bip32_seed]
scan1, spend1, Scan1, Spend1 = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
address = reference.encode_silent_payment_address(Scan1, Spend1, hrp=HRP)
addresses = [(address, 1.0), (change_address, 2.0)]
test_cases = []
sp_recipients = [[address, change_address]]
rec1, rec2 = deepcopy(recipient), deepcopy(recipient)
rec1['given']['key_material']['scan_priv_key'] = scan0.get_bytes().hex()
rec1['given']['key_material']['spend_priv_key'] = spend0.get_bytes().hex()
rec2['given']['key_material']['scan_priv_key'] = scan1.get_bytes().hex()
rec2['given']['key_material']['spend_priv_key'] = spend1.get_bytes().hex()
rec1['expected']['addresses'] = [sender_address, change_address]
rec1['given']['labels'] = change_labels
rec2['expected']['addresses'] = [address]
inputs = []
for i, outpoint in enumerate(outpoints):
inputs += [{
'txid': outpoint[0],
'vout': outpoint[1],
'scriptSig': get_p2pkh_scriptsig(input_pub_keys[i], input_priv_keys[i][0]),
'txinwitness': '',
'prevout': {'scriptPubKey': {'hex': get_p2pkh_scriptPubKey(input_pub_keys[i])}},
}]
sender['given']['vin'] = add_private_keys(deepcopy(inputs), input_priv_keys)
sender['given']['recipients'] = addresses
A_sum = sum(input_pub_keys)
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs(input_priv_keys, deterministic_nonce, addresses, hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
test_case['sending'].extend([sender])
labels = [change_labels, []]
for i, rec in enumerate([rec1, rec2]):
rec['given']['vin'] = inputs
rec['given']['outputs'] = output_pub_keys
scan, spend, Scan, Spend = reference.derive_silent_payment_key_pair(bytes.fromhex(seeds[i]))
add_to_wallet = reference.scanning(
scan,
Spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
labels={(reference.generate_label(scan, l)*G).get_bytes(False).hex():reference.generate_label(scan, l).hex() for l in labels[i]},
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
rec['expected']['outputs'] = add_to_wallet
test_case['receiving'].extend([rec])
test_case["comment"] = "Single recipient: use silent payments for sender change"
test_cases.append(test_case)
return test_cases
# In[17]:
def generate_unknown_segwit_ver_test():
sender, recipient, test_case = new_test_case()
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
]
sender_bip32_seed = 'deadbeef'
recipient_bip32_seed = 'f00dbabe'
scan, spend, Scan, Spend = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
address = reference.encode_silent_payment_address(Scan, Spend, hrp=HRP)
addresses = [(address, 1.0)]
recipient['given']['key_material']['scan_priv_key'] = scan.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = spend.get_bytes().hex()
inputs = []
input_priv_keys = []
input_pub_keys = []
## included
# p2pkh
i = len(inputs)
priv, pub = get_key_pair(i, seed=bytes.fromhex(sender_bip32_seed))
inputs += [{
'prevout': list(outpoints[i]) + [get_p2pkh_scriptsig(pub, priv), ""],
'scriptPubKey': get_p2pkh_scriptPubKey(pub),
}]
input_priv_keys += [(priv, False)]
input_pub_keys += [pub]
# unknown segwit version
i = len(inputs)
priv, pub = get_key_pair(i, seed=bytes.fromhex(sender_bip32_seed))
sig = priv.sign_ecdsa(msg, low_s=False, rfc6979=True).hex()
inputs += [{
'prevout': list(outpoints[i]) + ["", serialize_witness_stack([sig, pub.get_bytes(False).hex()])],
'scriptPubKey': "5214" + hash160(pub.get_bytes(False)),
}]
input_priv_keys += [(priv, False)]
input_pub_keys += [pub]
sender['given']['recipients'] = addresses
sender['given']['inputs'] = add_private_keys(deepcopy(inputs), input_priv_keys)
# TODO: encode failure of sending explicitly
test_case['sending'].extend([sender])
recipient['given']['inputs'] = inputs
# create plausible outputs
# a) using all detected keys
outputs_a = reference.create_outputs(input_priv_keys, reference.hash_outpoints(outpoints), addresses, hrp=HRP)
# b) using only p2pkh input
outputs_b = reference.create_outputs(input_priv_keys[:1], reference.hash_outpoints(outpoints), addresses, hrp=HRP)
recipient['given']['outputs'] = [outputs_a[0][0], outputs_b[0][0]]
test_case['receiving'].extend([recipient])
test_case["comment"] = "Skipped tx: unknown segwit version input"
return [test_case]
def generate_taproot_with_nums_point_test():
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()
G = ECKey().set(1).get_pubkey()
outpoints = [
("f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 0),
("a1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d", 1)
]
sender_bip32_seed = 'deadbeef'
recipient_bip32_seed = 'f00dbabe'
i1, I1 = get_key_pair(0, seed=bytes.fromhex(sender_bip32_seed))
i2, I2 = get_key_pair(1, seed=bytes.fromhex(sender_bip32_seed))
i3, I3 = get_key_pair(2, seed=bytes.fromhex(sender_bip32_seed))
if I1.get_y()%2 != 0:
i1.negate()
I1.negate()
if I2.get_y()%2 != 0:
i2.negate()
I2.negate()
if I3.get_y()%2 == 0:
i3.negate()
I3.negate()
nums_point = [
[(i1, NUMS_H, True), (i2, None, False), (i3, NUMS_H, False)],
[I1, I2, I3]
]
test_cases = []
comments = [
"Single receipient: taproot input with NUMS point"
]
included_keys = []
included_pubkeys = []
for i, inputs in enumerate([nums_point]):
sender, recipient, test_case = new_test_case()
inp = []
for x, (key, internal_key, add_annex) in enumerate(inputs[0]):
pub_key = inputs[1][x]
if (internal_key == None):
inp += [{
"txid": outpoints[x][0],
"vout": outpoints[x][1],
"scriptSig": "",
"txinwitness": get_p2tr_witness(key),
"prevout": {"scriptPubKey": {"hex": get_p2tr_scriptPubKey(pub_key)}},
}]
included_keys += [(key, True)]
included_pubkeys += [pub_key]
else:
script = "20" + pub_key.get_bytes(True).hex() + "ac"
leaf_version = "c0"
annex = "50"
internal_key_bytes = internal_key.to_bytes(32, 'big')
leaf_hash = TaggedHash("TapLeaf", bytes.fromhex(leaf_version + f'{len(script)//2:0x}' + script))
tap_tweak = TaggedHash("TapTweak", internal_key_bytes + leaf_hash)
tweaked_key = ECPubKey().set(internal_key_bytes).tweak_add(tap_tweak)
control_block = "c1" + internal_key_bytes.hex()
sig = key.sign_schnorr(msg).hex()
stack = [sig, script, control_block]
if (add_annex):
stack.append(annex)
inp += [{
"txid": outpoints[x][0],
"vout": outpoints[x][1],
"scriptSig": "",
"txinwitness": serialize_witness_stack(stack),
"prevout": {"scriptPubKey": {"hex": get_p2tr_scriptPubKey(tweaked_key)}},
}]
# Notice that the keys are not added to included list because they should be skipped since they use NUMS_POINT
sender['given']['vin'] = add_private_keys(deepcopy(inp), inputs[0])
recipient['given']['vin'] = inp
b_scan, b_spend, B_scan, B_spend = reference.derive_silent_payment_key_pair(bytes.fromhex(recipient_bip32_seed))
recipient['given']['key_material']['scan_priv_key'] = b_scan.get_bytes().hex()
recipient['given']['key_material']['spend_priv_key'] = b_spend.get_bytes().hex()
address = reference.encode_silent_payment_address(B_scan, B_spend, hrp=HRP)
sender['given']['recipients'].extend([(address, 1.0)])
recipient['expected']['addresses'].extend([address])
A_sum = sum([p if p.get_y()%2==0 else p * -1 for p in included_pubkeys])
deterministic_nonce = reference.get_input_hash([COutPoint(deser_txid(o[0]), o[1]) for o in outpoints], A_sum)
outputs = reference.create_outputs([(inp[0], True) for inp in included_keys], deterministic_nonce, [(address, 1.0)], hrp=HRP)
sender['expected']['outputs'] = outputs
output_pub_keys = [recipient[0] for recipient in outputs]
recipient['given']['outputs'] = output_pub_keys
add_to_wallet = reference.scanning(
b_scan,
B_spend,
A_sum,
deterministic_nonce,
[ECPubKey().set(bytes.fromhex(pub)) for pub in output_pub_keys],
)
for o in add_to_wallet:
pubkey = ECPubKey().set(bytes.fromhex(o['pub_key']))
full_private_key = b_spend.add(
bytes.fromhex(o['priv_key_tweak'])
)
if full_private_key.get_pubkey().get_y()%2 != 0:
full_private_key.negate()
sig = full_private_key.sign_schnorr(msg, aux)
assert pubkey.verify_schnorr(sig, msg)
o['signature'] = sig.hex()
recipient['expected']['outputs'] = add_to_wallet
test_case['sending'].extend([sender])
test_case['receiving'].extend([recipient])
test_case["comment"] = comments[i]
test_cases.append(test_case)
return test_cases
def generate_malleated_p2pkh_test():
sender, recipient, test_case = new_test_case()
msg = hashlib.sha256(b'message').digest()
aux = hashlib.sha256(b'random auxiliary data').digest()