-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.html
1539 lines (1523 loc) · 81.6 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<script src="https://unpkg.com/@cmdcode/tapscript@1.4.0"></script>
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script src="https://unpkg.com/@dashincubator/ripemd160/ripemd160.js"></script>
<script src="https://supertestnet.github.io/bitvm-workshop/combinations.js"></script>
<script>
var $ = document.querySelector.bind( document );
var $$ = document.querySelectorAll.bind( document );
var hexToBytes = hex => Uint8Array.from( hex.match( /.{1,2}/g ).map( byte => parseInt( byte, 16 ) ) );
var bytesToHex = bytes => bytes.reduce( ( str, byte ) => str + byte.toString( 16 ).padStart( 2, "0" ), "" );
var rmd160 = s => {
if ( typeof s == "string" ) s = new TextEncoder().encode( s );
var hash = RIPEMD160.create();
hash.update( new Uint8Array( s ) );
return bytesToHex( hash.digest() );
}
var waitASec=num=>new Promise(res=>setTimeout(res,num*1000));
var waitForMove = async () => {
if ( !num ) await waitASec( 1 );
return num || waitForMove();
}
</script>
<script>
var contract = [];
var hashes = [];
var fraud_occurred = false;
var victory_achieved = false;
var alice = {
privkey: "ab".repeat( 32 ),
scripts: [],
trees: [],
preimages: [],
hashes: [],
accept_preimages: [],
accept_hashes: [],
bobs_moves: [],
bobs_squares: [],
bobs_acks: [],
}
alice[ "pubkey" ] = nobleSecp256k1.getPublicKey( alice[ "privkey" ], true ).substring( 2 );
var funding_address = tapscript.Address.p2tr.fromPubKey( alice[ "pubkey" ], 'testnet' );
var alices_preimage;
var alices_hash;
var alices_move;
var bob = {
privkey: "ba".repeat( 32 ),
scripts: [],
trees: [],
preimages: [],
hashes: [],
accept_preimages: [],
accept_hashes: [],
alices_moves: [],
alices_squares: [],
alices_acks: [],
}
bob[ "pubkey" ] = nobleSecp256k1.getPublicKey( bob[ "privkey" ], true ).substring( 2 );
var bobs_preimage;
var bobs_hash;
var bobs_move;
</script>
<script>
var i; for ( i=0; i<45; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
alice[ "preimages" ].push( preimage );
alice[ "hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*4; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
bob[ "preimages" ].push( preimage );
bob[ "hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*4; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
alice[ "accept_preimages" ].push( preimage );
alice[ "accept_hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*5; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
bob[ "accept_preimages" ].push( preimage );
bob[ "accept_hashes" ].push( hash );
}
</script>
<style>
html,input{line-height:1.25}*{box-sizing:border-box;font-size:1.15rem;font-family:Arial,sans-serif}html{max-width:800px;padding:0;margin:auto}body{margin:3rem 1rem}h1{font-size:2rem}h2{font-size:1.5rem}input{width:100%;height:1.8rem;font-size:1.15rem;border:1px solid grey}.tictacbox{display:flex;width:100%;justify-content:center}.gameboard{display:flex;width:15rem}.square{height:5rem;width:5rem;cursor:pointer;display:flex;justify-content:center;align-items:center}.col2 div{border-right:1px solid #000;border-left:1px solid #000}.square4,.square5,.square6{border-top:1px solid #000;border-bottom:1px solid #000}
</style>
</head>
<body>
<h1>Welcome to bit tac toe</h1>
<p><button onclick="init()">Play</button></p>
<div class="tictacbox">
<div class="gameboard">
<div class="col1">
<div class="square square1"></div>
<div class="square square4"></div>
<div class="square square7"></div>
</div>
<div class="col2">
<div class="square square2"></div>
<div class="square square5"></div>
<div class="square square8"></div>
</div>
<div class="col3">
<div class="square square3"></div>
<div class="square square6"></div>
<div class="square square9"></div>
</div>
</div>
</div>
<script>
var type = 1;var num;var game_on;
$$( '.square' ).forEach( item => {item.onclick = () => {
if ( !game_on ) return;
var conf = true;
if ( item.innerHTML ) conf = confirm( `This is an illegal move! Your opponent will be able to take your money. Click ok to do it anyway` );
if ( !conf ) return;
var symbol = `<span style="font-size: 200%;">×</span>`
if ( !type ) symbol = `◯`;
item.innerHTML = symbol;
type = !type;
var a = item.classList[ 1 ];
num = Number( a.substring( a.length - 1, a.length ) );
}});
</script>
<script>
var contract = [];
var hashes = [];
var fraud_occurred = false;
var victory_achieved = false;
var alice = {
privkey: "ab".repeat( 32 ),
scripts: [],
trees: [],
preimages: [],
hashes: [],
accept_preimages: [],
accept_hashes: [],
bobs_moves: [],
bobs_squares: [],
bobs_acks: [],
}
alice[ "pubkey" ] = nobleSecp256k1.getPublicKey( alice[ "privkey" ], true ).substring( 2 );
var funding_address = tapscript.Address.p2tr.fromPubKey( alice[ "pubkey" ], 'testnet' );
var alices_preimage;
var alices_hash;
var alices_move;
var bob = {
privkey: "ba".repeat( 32 ),
scripts: [],
trees: [],
preimages: [],
hashes: [],
accept_preimages: [],
accept_hashes: [],
alices_moves: [],
alices_squares: [],
alices_acks: [],
}
bob[ "pubkey" ] = nobleSecp256k1.getPublicKey( bob[ "privkey" ], true ).substring( 2 );
var bobs_preimage;
var bobs_hash;
var bobs_move;
</script>
<script>
var i; for ( i=0; i<9*5; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
alice[ "preimages" ].push( preimage );
alice[ "hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*4; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
bob[ "preimages" ].push( preimage );
bob[ "hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*4; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
alice[ "accept_preimages" ].push( preimage );
alice[ "accept_hashes" ].push( hash );
}
</script>
<script>
var i; for ( i=0; i<9*5; i++ ) {
var preimage = nobleSecp256k1.utils.randomPrivateKey();
preimage = Array.from( preimage );
preimage.splice( 0, 12 );
preimage = new Uint8Array( preimage );
var hash = rmd160( preimage );
preimage = bytesToHex( preimage );
bob[ "accept_preimages" ].push( preimage );
bob[ "accept_hashes" ].push( hash );
}
</script>
<script>
sessionStorage[ "cell_0" ] = `___|`;
sessionStorage[ "cell_1" ] = `___|`;
sessionStorage[ "cell_2" ] = `___`;
sessionStorage[ "cell_3" ] = `___|`;
sessionStorage[ "cell_4" ] = `___|`;
sessionStorage[ "cell_5" ] = `___`;
sessionStorage[ "cell_6" ] = `___|`;
sessionStorage[ "cell_7" ] = `___|`;
sessionStorage[ "cell_8" ] = `___`;
console.log( sessionStorage[ "cell_0" ] + sessionStorage[ "cell_1" ] + sessionStorage[ "cell_2" ] );
console.log( sessionStorage[ "cell_3" ] + sessionStorage[ "cell_4" ] + sessionStorage[ "cell_5" ] );
console.log( sessionStorage[ "cell_6" ] + sessionStorage[ "cell_7" ] + sessionStorage[ "cell_8" ] );
</script>
<script>
var alicesTurn = turn => {
var original_turn = turn;
turn = turn * 9;
// The first two scripts are simple -- one is the 'happy
// path' whereby Alice and Bob cooperate, the other lets
// Bob takes Alice's money if Alice stops playing
var alices_turn_scripts = [
[ 0, alice[ "pubkey" ], "OP_CHECKSIGADD", bob[ "pubkey" ], "OP_CHECKSIGADD", 2, "OP_EQUAL" ],
[ 10, "OP_CHECKSEQUENCEVERIFY", bob[ "pubkey" ], "OP_CHECKSIG" ],
];
// this third script is more complex -- it forces Alice
// to reveal one of 9 preimages to represent marking an
// X in one of 9 squares of a tic tac toe board
var i; for ( i=turn; i<turn + 9; i++ ) {
if ( turn === 0 ) {
var initial_tapleaf_preimage = "";
var initial_tapleaf_guard = "9c1185a5c5e9fc54612808977ee8f548b2258d31";
var tapleaf_guard = initial_tapleaf_guard;
var initial_accept_preimage = "";
var initial_accept_guard = "9c1185a5c5e9fc54612808977ee8f548b2258d31";
var accept_guard = initial_accept_guard;
} else {
var tapleaf_guard = bob[ "hashes" ][i-9];
var accept_guard = alice[ "accept_hashes" ][i-9];
}
var alices_turn_third_script = [
"OP_RIPEMD160",
tapleaf_guard,
"OP_EQUALVERIFY",
"OP_RIPEMD160",
accept_guard,
"OP_EQUALVERIFY",
"OP_RIPEMD160",
"OP_DUP",
alice[ "hashes" ][ turn ],
"OP_EQUAL",
"OP_SWAP",
];
var j; for ( j=turn + 1; j<turn + 9; j++ ) {
alices_turn_third_script.push( ...[
"OP_DUP",
alice[ "hashes" ][ j ],
"OP_EQUAL",
"OP_ROT",
"OP_ADD",
"OP_SWAP",
]);
}
alices_turn_third_script.push( ...[
"OP_DROP",
"OP_VERIFY",
0, alice[ "pubkey" ], "OP_CHECKSIGADD", bob[ "pubkey" ], "OP_CHECKSIGADD", 2, "OP_EQUAL",
]);
alices_turn_scripts.push( alices_turn_third_script );
}
var i; for ( i=1; i<5; i++ ) {
var combinations = [];
var hashes = [ ...bob[ "hashes" ] ];
hashes = hashes.splice( ( 9 * i ) - 9, 9 );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
combinations.forEach( combination => {
alices_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
combination[ 0 ], "OP_EQUALVERIFY",
combination[ 1 ], "OP_EQUALVERIFY",
alice[ "pubkey" ],
"OP_CHECKSIG"
]);
});
}
var i; for ( i=0; i<9; i++ ) {
var combinations = [];var hashes = [];
hashes.push( bob[ "hashes" ][ i ], bob[ "hashes" ][ i + 9 ], bob[ "hashes" ][ i + 18 ], bob[ "hashes" ][ i + 27 ] );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
combinations.forEach( combination => {
alices_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
combination[ 0 ], "OP_EQUALVERIFY",
combination[ 1 ], "OP_EQUALVERIFY",
alice[ "pubkey" ],
"OP_CHECKSIG"
]);
});
}
var accept_hashes = [ ...bob[ "accept_hashes" ] ];
var reveal_hashes = [ ...bob[ "hashes" ] ];
accept_hashes.forEach( ( item, index ) => {
var i; for ( i=0; i<4; i++ ) {
if ( i==0 ) var hashnum = index % 9;
if ( i==1 ) var hashnum = ( index % 9 ) + 9;
if ( i==2 ) var hashnum = ( index % 9 ) + 18;
if ( i==3 ) var hashnum = ( index % 9 ) + 27;
alices_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item, "OP_EQUALVERIFY",
reveal_hashes[ hashnum ], "OP_EQUALVERIFY",
alice[ "pubkey" ], "OP_CHECKSIG"
]);
}
});
var combinations = [];
var i; for ( i=0; i<9; i++ ) {
/*row1*/ if ( i == 0 ) var hashes = [0,9,18,27,1,10,19,28];
if ( i == 1 ) var hashes = [0,9,18,27,2,11,20,29];
if ( i == 2 ) var hashes = [1,10,19,28,2,11,20,29];
/*row2*/ if ( i == 3 ) var hashes = [3,12,21,30,4,13,22,31];
if ( i == 4 ) var hashes = [3,12,21,30,5,14,23,32];
if ( i == 5 ) var hashes = [4,13,22,31,5,14,23,32];
/*row3*/ if ( i == 6 ) var hashes = [6,15,24,33,7,16,25,34];
if ( i == 7 ) var hashes = [6,15,24,33,8,17,26,35];
if ( i == 8 ) var hashes = [7,16,25,34,8,17,26,35];
for ( var comb of G.combination( hashes, 2 ) ) {var comb2 = JSON.parse( JSON.stringify( comb ) ); comb2.sort((a,b)=>a-b).reverse(); if ( ( comb2[ 0 ] - comb2[ 1 ] ) % 9 && comb2[ 0 ] - comb2[ 1 ] >= 3 ) combinations.push( Array.from( comb ) );}
}
combinations.forEach( ( item, index ) => {
combinations.splice( index * 5, 1, ...JSON.parse( JSON.stringify( [combinations[ index * 5 ]].flatMap(i => Array(5).fill(i))) ) );
});
combinations.forEach( ( item, index ) => {
if ( item.length > 2 ) return;
if ( index < 180 ) {if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {if ( index % 5 == 0 ) item.push( 2 );if ( index % 5 == 1 ) item.push( 11 );if ( index % 5 == 2 ) item.push( 20 );if ( index % 5 == 3 ) item.push( 29 );if ( index % 5 == 4 ) item.push( 38 );}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 1 );if ( index % 5 == 1 ) item.push( 10 );if ( index % 5 == 2 ) item.push( 19 );if ( index % 5 == 3 ) item.push( 28 );if ( index % 5 == 4 ) item.push( 37 );}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 0 );if ( index % 5 == 1 ) item.push( 9 );if ( index % 5 == 2 ) item.push( 18 );if ( index % 5 == 3 ) item.push( 27 );if ( index % 5 == 4 ) item.push( 36 );}
}
if ( index >= 180 && index < 360 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {if ( index % 5 == 0 ) item.push( 5 );if ( index % 5 == 1 ) item.push( 14 );if ( index % 5 == 2 ) item.push( 23 );if ( index % 5 == 3 ) item.push( 32 );if ( index % 5 == 4 ) item.push( 41 );}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 4 );if ( index % 5 == 1 ) item.push( 13 );if ( index % 5 == 2 ) item.push( 22 );if ( index % 5 == 3 ) item.push( 31 );if ( index % 5 == 4 ) item.push( 40 );}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 3 );if ( index % 5 == 1 ) item.push( 12 );if ( index % 5 == 2 ) item.push( 21 );if ( index % 5 == 3 ) item.push( 30 );if ( index % 5 == 4 ) item.push( 39 );}
}
if ( index >= 360 && index < 540 ) {if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {if ( index % 5 == 0 ) item.push( 8 );if ( index % 5 == 1 ) item.push( 17 );if ( index % 5 == 2 ) item.push( 26 );if ( index % 5 == 3 ) item.push( 35 );if ( index % 5 == 4 ) item.push( 44 );}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 7 );if ( index % 5 == 1 ) item.push( 16 );if ( index % 5 == 2 ) item.push( 25 );if ( index % 5 == 3 ) item.push( 34 );if ( index % 5 == 4 ) item.push( 43 );}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {if ( index % 5 == 0 ) item.push( 6 );if ( index % 5 == 1 ) item.push( 15 );if ( index % 5 == 2 ) item.push( 24 );if ( index % 5 == 3 ) item.push( 33 );if ( index % 5 == 4 ) item.push( 42 );}
}
});
combinations.forEach( combination => {
alices_turn_scripts.push([
"OP_RIPEMD160", "OP_SWAP", "OP_RIPEMD160",
2, "OP_ROLL",
"OP_RIPEMD160",
"OP_SWAP",
2, "OP_ROLL",
alice[ "hashes" ][ combination[ 2 ] ],
"OP_EQUALVERIFY",
bob[ "accept_hashes" ][ combination[ 1 ] ],
"OP_EQUALVERIFY",
bob[ "accept_hashes" ][ combination[ 0 ] ],
"OP_EQUALVERIFY",
//TODO: turn this into a multisig
alice[ "pubkey" ], "OP_CHECKSIG"
]);
});
alice[ "trees" ].push( alices_turn_scripts.map( s => tapscript.Tap.encodeScript( s ) ) );
alice[ "scripts" ].push( alices_turn_scripts );
var pubkey = "ab".repeat( 32 );
var [ tpubkey ] = tapscript.Tap.getPubKey( pubkey, { tree: alice[ "trees" ][ original_turn ] });
var alices_turn_address = tapscript.Address.p2tr.fromPubKey( tpubkey, 'testnet' );
return alices_turn_address;
}
</script>
<script>
var bobsTurn = turn => {
var original_turn = turn;
turn = turn * 9;
var bobs_turn_scripts = [
[ 0, alice[ "pubkey" ], "OP_CHECKSIGADD", bob[ "pubkey" ], "OP_CHECKSIGADD", 2, "OP_EQUAL" ],
[ 10, "OP_CHECKSEQUENCEVERIFY", alice[ "pubkey" ], "OP_CHECKSIG" ],
];
var i; for ( i=turn; i<turn + 9; i++ ) {
var tapleaf_guard = alice[ "hashes" ][ i ];
var accept_guard = bob[ "accept_hashes" ][ i ];
var bobs_turn_third_script = [
"OP_RIPEMD160",
tapleaf_guard,
"OP_EQUALVERIFY",
"OP_RIPEMD160",
accept_guard,
"OP_EQUALVERIFY",
"OP_RIPEMD160",
"OP_DUP",
bob[ "hashes" ][ turn ],
"OP_EQUAL",
"OP_SWAP",
];
var j; for ( j=turn + 1; j<turn + 9; j++ ) {
bobs_turn_third_script.push( ...[
"OP_DUP",
bob[ "hashes"][ j ],
"OP_EQUAL",
"OP_ROT",
"OP_ADD",
"OP_SWAP",
]);
}
bobs_turn_third_script.push( ...[
"OP_DROP",
"OP_VERIFY",
0,
alice[ "pubkey" ],
"OP_CHECKSIGADD",
bob[ "pubkey" ],
"OP_CHECKSIGADD",
2,
"OP_EQUAL",
]);
bobs_turn_scripts.push( bobs_turn_third_script );
}
bob[ "scripts" ].push( bobs_turn_scripts );
//allow Bob to prove fraud if Alice reveals two preimages for any of her turns
var i; for ( i=1; i<6; i++ ) {
var combinations = [];
var hashes = [ ...alice[ "hashes" ] ];
hashes = hashes.splice( ( 9 * i ) - 9, 9 );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
combinations.forEach( combination => {
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
combination[ 0 ],
"OP_EQUALVERIFY",
combination[ 1 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
});
}
var i; for ( i=0; i<9; i++ ) {
var combinations = [];
var hashes = [];
hashes.push( alice[ "hashes" ][ i ], alice[ "hashes" ][ i + 9 ], alice[ "hashes" ][ i + 18 ], alice[ "hashes" ][ i + 27 ], alice[ "hashes" ][ i + 36 ] );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
combinations.forEach( combination => {
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
combination[ 0 ],
"OP_EQUALVERIFY",
combination[ 1 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
});
}
var accept_hashes = [ ...alice[ "accept_hashes" ] ];
var reveal_hashes = [ ...alice[ "hashes" ] ];
accept_hashes.forEach( ( item, index ) => {
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item,
"OP_EQUALVERIFY",
reveal_hashes[ index % 9 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item,
"OP_EQUALVERIFY",
reveal_hashes[ ( index % 9 ) + 9 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item,
"OP_EQUALVERIFY",
reveal_hashes[ ( index % 9 ) + 9 + 9 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item,
"OP_EQUALVERIFY",
reveal_hashes[ ( index % 9 ) + 9 + 9 + 9 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
item,
"OP_EQUALVERIFY",
reveal_hashes[ ( index % 9 ) + 9 + 9 + 9 + 9 ],
"OP_EQUALVERIFY",
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
});
var combinations = [];
var i; for ( i=0; i<9; i++ ) {
//row 1
if ( i == 0 ) var hashes = [0, 9, 18, 27, 1, 10, 19, 28];
if ( i == 1 ) var hashes = [0, 9, 18, 27, 2, 11, 20, 29];
if ( i == 2 ) var hashes = [1, 10, 19, 28, 2, 11, 20, 29];
//row 2
if ( i == 3 ) var hashes = [3, 12, 21, 30, 4, 13, 22, 31];
if ( i == 4 ) var hashes = [3, 12, 21, 30, 5, 14, 23, 32];
if ( i == 5 ) var hashes = [4, 13, 22, 31, 5, 14, 23, 32];
//row 3
if ( i == 6 ) var hashes = [6, 15, 24, 33, 7, 16, 25, 34];
if ( i == 7 ) var hashes = [6, 15, 24, 33, 8, 17, 26, 35];
if ( i == 8 ) var hashes = [7, 16, 25, 34, 8, 17, 26, 35];
for ( var comb of G.combination( hashes, 2 ) ) {var comb2 = JSON.parse( JSON.stringify( comb ) ); comb2.sort((a,b)=>a-b).reverse(); if ( ( comb2[ 0 ] - comb2[ 1 ] ) % 9 && comb2[ 0 ] - comb2[ 1 ] >= 3 ) combinations.push( Array.from( comb ) );}
}
combinations.forEach( ( item, index ) => {
combinations.splice( index * 4, 1, ...JSON.parse( JSON.stringify( [combinations[ index * 4 ]].flatMap(i => Array(4).fill(i))) ) );
});
combinations.forEach( ( item, index ) => {
if ( item.length > 2 ) return;
//row 1
//Alice had 180 here because there were originally 108 combinations and I multiplied them by 5 for each of her 5 turns. Then I divided the result, 540, by 3 to work on rows 1, 2, and 3. But Bob only has 4 turns so he only gets 108*4 = 432 combinations, meaning 180 for him should be 432/3 = 144
if ( index < 144 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 4 == 0 ) item.push( 2 );
if ( index % 4 == 1 ) item.push( 11 );
if ( index % 4 == 2 ) item.push( 20 );
if ( index % 4 == 3 ) item.push( 29 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 1 );
if ( index % 4 == 1 ) item.push( 10 );
if ( index % 4 == 2 ) item.push( 19 );
if ( index % 4 == 3 ) item.push( 28 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 0 );
if ( index % 4 == 1 ) item.push( 9 );
if ( index % 4 == 2 ) item.push( 18 );
if ( index % 4 == 3 ) item.push( 27 );
}
}
//row 2
if ( index >= 144 && index < 288 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 4 == 0 ) item.push( 5 );
if ( index % 4 == 1 ) item.push( 14 );
if ( index % 4 == 2 ) item.push( 23 );
if ( index % 4 == 3 ) item.push( 32 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 4 );
if ( index % 4 == 1 ) item.push( 13 );
if ( index % 4 == 2 ) item.push( 22 );
if ( index % 4 == 3 ) item.push( 31 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 3 );
if ( index % 4 == 1 ) item.push( 12 );
if ( index % 4 == 2 ) item.push( 21 );
if ( index % 4 == 3 ) item.push( 30 );
}
}
//row 3
if ( index >= 288 && index < 432 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 4 == 0 ) item.push( 8 );
if ( index % 4 == 1 ) item.push( 17 );
if ( index % 4 == 2 ) item.push( 26 );
if ( index % 4 == 3 ) item.push( 35 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 7 );
if ( index % 4 == 1 ) item.push( 16 );
if ( index % 4 == 2 ) item.push( 25 );
if ( index % 4 == 3 ) item.push( 34 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 4 == 0 ) item.push( 6 );
if ( index % 4 == 1 ) item.push( 15 );
if ( index % 4 == 2 ) item.push( 24 );
if ( index % 4 == 3 ) item.push( 33 );
}
}
});
combinations.forEach( combination => {
bobs_turn_scripts.push([
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
2,
"OP_ROLL",
"OP_RIPEMD160",
"OP_SWAP",
2,
"OP_ROLL",
bob[ "hashes" ][ combination[ 2 ] ],
"OP_EQUALVERIFY",
alice[ "accept_hashes" ][ combination[ 1 ] ],
"OP_EQUALVERIFY",
alice[ "accept_hashes" ][ combination[ 0 ] ],
"OP_EQUALVERIFY",
//todo: ensure this is a multisig path that
//sends the money to an address where Bob
//has an opportunity to prove alice cheated
bob[ "pubkey" ],
"OP_CHECKSIG"
]);
});
bob[ "trees" ].push( bobs_turn_scripts.map( s => tapscript.Tap.encodeScript( s ) ) );
var pubkey = "ab".repeat( 32 );
var [ tpubkey ] = tapscript.Tap.getPubKey( pubkey, { tree: bob[ "trees" ][ original_turn ] });
var bobs_turn_address = tapscript.Address.p2tr.fromPubKey( tpubkey, 'testnet' );
return bobs_turn_address;
}
</script>
<script>
var proveDoubleTurn = ( p1, p2, opponent, self ) => {
var h1 = rmd160( hexToBytes( p1 ) );
var h2 = rmd160( hexToBytes( p2 ) );
var combinations = [];
var i; for ( i=1; i<5; i++ ) {
var hashes = [ ...opponent[ "hashes" ] ];
hashes = hashes.splice( ( 9 * i ) - 9, 9 );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
}
var possibles = [];
combinations.forEach( item => {
if ( item.includes( h1 ) ) possibles.push( item );
});
var actual;
possibles.forEach( item => {
if ( item.includes( h2 ) ) actual = item;
});
if ( !actual ) return;
fraud_occurred = true;
var script = [
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
actual[ 0 ], "OP_EQUALVERIFY",
actual[ 1 ], "OP_EQUALVERIFY",
self[ "pubkey" ], "OP_CHECKSIG"
];
var target = tapscript.Tap.encodeScript( script );
return [ target, script ];
}
var proveOverwriteSelf = ( p1, p2, opponent, self ) => {
var h1 = rmd160( hexToBytes( p1 ) );
var h2 = rmd160( hexToBytes( p2 ) );
var actual;
var i; for ( i=0; i<9; i++ ) {
var combinations = [];var hashes = [];
hashes.push( opponent[ "hashes" ][ i ], opponent[ "hashes" ][ i + 9 ], opponent[ "hashes" ][ i + 18 ], opponent[ "hashes" ][ i + 27 ] );
for ( var comb of G.combination( hashes, 2 ) ) combinations.push( Array.from( comb ) );
var possibles = [];
combinations.forEach( item => {if ( item.includes( h1 ) ) possibles.push( item );});
possibles.forEach( item => {if ( item.includes( h2 ) ) actual = item;});
}
if ( !actual ) return;fraud_occurred = true;
var script = [
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
actual[ 0 ],
"OP_EQUALVERIFY",
actual[ 1 ],
"OP_EQUALVERIFY",
self[ "pubkey" ],
"OP_CHECKSIG"
];
var target = tapscript.Tap.encodeScript( script );
return [ target, script ];
}
</script>
<script>
var proveOverwriteOpponent = ( p1, p2, opponent, self ) => {
var h1 = rmd160( hexToBytes( p1 ) );
var h2 = rmd160( hexToBytes( p2 ) );
var actual;
var accept_hashes = [ ...opponent[ "accept_hashes" ] ];
var reveal_hashes = [ ...opponent[ "hashes" ] ];
accept_hashes.forEach( ( item, index ) => {
if ( ( item == h1 || item == h2 ) && ( reveal_hashes[ index % 9 ] == h1 || reveal_hashes[ index % 9 ] == h2 ) ) actual = [ item, reveal_hashes[index%9] ];
if ( ( item == h1 || item == h2 ) && ( reveal_hashes[ ( index % 9 ) + 9 ] == h1 || reveal_hashes[ ( index % 9 ) + 9 ] == h2 ) ) actual = [ item, reveal_hashes[(index%9) + 9 ] ];
if ( ( item == h1 || item == h2 ) && ( reveal_hashes[ ( index % 9 ) + 18 ] == h1 || reveal_hashes[ ( index % 9 ) + 9 + 9 ] == h2 ) ) actual = [ item, reveal_hashes[(index%9) + 18 ] ];
if ( ( item == h1 || item == h2 ) && ( reveal_hashes[ ( index % 9 ) + 27 ] == h1 || reveal_hashes[ ( index % 9 ) + 9 + 9 + 9 ] == h2 ) ) actual = [ item, reveal_hashes[(index%9) + 27 ] ];
});
if ( !actual ) return;
fraud_occurred = true;
var script = [
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
actual[ 0 ],
"OP_EQUALVERIFY",
actual[ 1 ],
"OP_EQUALVERIFY",
self[ "pubkey" ],
"OP_CHECKSIG"
];
var target = tapscript.Tap.encodeScript( script );
return [ target, script ];
}
</script>
<script>
var proveVictory = ( p1, p2, p3, opponent, self ) => {
var h1 = rmd160( hexToBytes( p1 ) );
var h2 = rmd160( hexToBytes( p2 ) );
var h3 = rmd160( hexToBytes( p3 ) );
var actual;
var combinations = [];
var i; for ( i=0; i<9; i++ ) {
//row 1
if ( i == 0 ) var hashes = [0, 9, 18, 27, 1, 10, 19, 28];
if ( i == 1 ) var hashes = [0, 9, 18, 27, 2, 11, 20, 29];
if ( i == 2 ) var hashes = [1, 10, 19, 28, 2, 11, 20, 29];
//row 2
if ( i == 3 ) var hashes = [3, 12, 21, 30, 4, 13, 22, 31];
if ( i == 4 ) var hashes = [3, 12, 21, 30, 5, 14, 23, 32];
if ( i == 5 ) var hashes = [4, 13, 22, 31, 5, 14, 23, 32];
//row 3
if ( i == 6 ) var hashes = [6, 15, 24, 33, 7, 16, 25, 34];
if ( i == 7 ) var hashes = [6, 15, 24, 33, 8, 17, 26, 35];
if ( i == 8 ) var hashes = [7, 16, 25, 34, 8, 17, 26, 35];
for ( var comb of G.combination( hashes, 2 ) ) {var comb2 = JSON.parse( JSON.stringify( comb ) ); comb2.sort((a,b)=>a-b).reverse(); if ( ( comb2[ 0 ] - comb2[ 1 ] ) % 9 && comb2[ 0 ] - comb2[ 1 ] >= 3 ) combinations.push( Array.from( comb ) );}
}
combinations.forEach( ( item, index ) => {
combinations.splice( index * 5, 1, ...JSON.parse( JSON.stringify( [combinations[ index * 5 ]].flatMap(i => Array(5).fill(i))) ) );
});
combinations.forEach( ( item, index ) => {
if ( item.length > 2 ) return;
//row 1
if ( index < 180 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 5 == 0 ) item.push( 2 );
if ( index % 5 == 1 ) item.push( 11 );
if ( index % 5 == 2 ) item.push( 20 );
if ( index % 5 == 3 ) item.push( 29 );
if ( index % 5 == 4 ) item.push( 38 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 1 );
if ( index % 5 == 1 ) item.push( 10 );
if ( index % 5 == 2 ) item.push( 19 );
if ( index % 5 == 3 ) item.push( 28 );
if ( index % 5 == 4 ) item.push( 37 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 0 );
if ( index % 5 == 1 ) item.push( 9 );
if ( index % 5 == 2 ) item.push( 18 );
if ( index % 5 == 3 ) item.push( 27 );
if ( index % 5 == 4 ) item.push( 36 );
}
}
//row 2
if ( index >= 180 && index < 360 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 5 == 0 ) item.push( 5 );
if ( index % 5 == 1 ) item.push( 14 );
if ( index % 5 == 2 ) item.push( 23 );
if ( index % 5 == 3 ) item.push( 32 );
if ( index % 5 == 4 ) item.push( 41 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 4 );
if ( index % 5 == 1 ) item.push( 13 );
if ( index % 5 == 2 ) item.push( 22 );
if ( index % 5 == 3 ) item.push( 31 );
if ( index % 5 == 4 ) item.push( 40 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 3 );
if ( index % 5 == 1 ) item.push( 12 );
if ( index % 5 == 2 ) item.push( 21 );
if ( index % 5 == 3 ) item.push( 30 );
if ( index % 5 == 4 ) item.push( 39 );
}
}
//row 3
if ( index >= 360 && index < 540 ) {
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 1 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 1 ) {
if ( index % 5 == 0 ) item.push( 8 );
if ( index % 5 == 1 ) item.push( 17 );
if ( index % 5 == 2 ) item.push( 26 );
if ( index % 5 == 3 ) item.push( 35 );
if ( index % 5 == 4 ) item.push( 44 );
}
if ( item[ 0 ] % 3 == 0 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 0 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 7 );
if ( index % 5 == 1 ) item.push( 16 );
if ( index % 5 == 2 ) item.push( 25 );
if ( index % 5 == 3 ) item.push( 34 );
if ( index % 5 == 4 ) item.push( 43 );
}
if ( item[ 0 ] % 3 == 1 && item[ 1 ] % 3 == 2 || item[ 1 ] % 3 == 1 && item[ 0 ] % 3 == 2 ) {
if ( index % 5 == 0 ) item.push( 6 );
if ( index % 5 == 1 ) item.push( 15 );
if ( index % 5 == 2 ) item.push( 24 );
if ( index % 5 == 3 ) item.push( 33 );
if ( index % 5 == 4 ) item.push( 42 );
}
}
});
combinations.forEach( ( item, index ) => {
combinations[ index ][ 0 ] = opponent[ "accept_hashes" ][ item[ 0 ] ];
combinations[ index ][ 1 ] = opponent[ "accept_hashes" ][ item[ 1 ] ];
combinations[ index ][ 2 ] = self[ "hashes" ][ item[ 2 ] ];
});
var possibles = [];
combinations.forEach( item => {
if ( item.includes( h1 ) && item.includes( h2 ) ) possibles.push( item );
});
possibles.forEach( item => {
if ( item.includes( h3 ) ) actual = item;
});
if ( !actual ) return;
actual.reverse();
victory_achieved = true;
var script = [
"OP_RIPEMD160",
"OP_SWAP",
"OP_RIPEMD160",
2,
"OP_ROLL",
"OP_RIPEMD160",
"OP_SWAP",
2,
"OP_ROLL",
actual[ 0 ],
"OP_EQUALVERIFY",
actual[ 1 ],
"OP_EQUALVERIFY",
actual[ 2 ],
"OP_EQUALVERIFY",
//todo: ensure this is a multisig path that
//sends the money to an address where Bob
//has an opportunity to prove alice cheated
self[ "pubkey" ],
"OP_CHECKSIG"
];
var target = tapscript.Tap.encodeScript( script );
return [ target, script ];
}
</script>
<script>
var aliceChecksFraud = ( step, turn ) => {
var bobs_preimage_2;
// bobs_preimage_2 = prompt( `Enter bob's second preimage` );
if ( !bobs_preimage_2 ) bobs_preimage_2 = "ab";
var is_double_turn = proveDoubleTurn( alice[ "bobs_moves" ][ turn - 1 ], bobs_preimage_2, bob, alice );
if ( is_double_turn ) {
var txdata = tapscript.Tx.create({
vin: [{
txid: contract[ step ][ "txid" ],
vout: contract[ step ][ "vout" ],
prevout: {
value: contract[ step ][ "amt" ],
scriptPubKey: tapscript.Address.toScriptPubKey( contract[ step ][ "funding_address" ] )
},
}],
vout: [{
value: contract[ step ][ "amt" ] - 500,
scriptPubKey: tapscript.Address.toScriptPubKey( "tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v" )
}]
});
var target = is_double_turn[ 0 ];
var script = is_double_turn[ 1 ];
var sig = tapscript.Signer.taproot.sign( alice[ "privkey" ], txdata, 0, { extension: target });
var [ tpubkey, cblock ] = tapscript.Tap.getPubKey( "ab".repeat( 32 ), { tree: alice[ "trees" ][ turn ], target });
var bobs_preimage_1 = alice[ "bobs_moves" ][turn-1];
txdata.vin[ 0 ].witness = [ sig, bobs_preimage_1, bobs_preimage_2, script, cblock ];
var txhex = tapscript.Tx.encode( txdata ).hex;
console.log( "broadcast this to take bob's money:", txhex );
txdata.vin[ 0 ].witness = [ sig, bobs_preimage_2, bobs_preimage_1, script, cblock ];
var txhex = tapscript.Tx.encode( txdata ).hex;
console.log( "or this:", txhex );
}
var hash_of_bobs_preimage = rmd160( hexToBytes( alice[ "bobs_moves" ][ turn - 1 ] ) );
var index_of_bobs_preimage = bob[ "hashes" ].indexOf( hash_of_bobs_preimage );
var square_bobs_preimage_belongs_to = index_of_bobs_preimage % 9;
if ( !alice[ "bobs_squares" ][ square_bobs_preimage_belongs_to ] ) {
alice[ "bobs_squares" ][ square_bobs_preimage_belongs_to ] = alice["bobs_moves"][turn-1];
} else {
var bobs_preimage_1 = alice["bobs_moves"][turn-1];
var bobs_preimage_2 = alice["bobs_squares"][ square_bobs_preimage_belongs_to ];
var overwrites_self = proveOverwriteSelf( bobs_preimage_1, bobs_preimage_2, bob, alice );
if ( overwrites_self ) {
var txdata = tapscript.Tx.create({
vin: [{
txid: contract[ step ][ "txid" ],
vout: contract[ step ][ "vout" ],
prevout: {
value: contract[ step ][ "amt" ],
scriptPubKey: tapscript.Address.toScriptPubKey( contract[ step ][ "funding_address" ] )
},
}],
vout: [{
value: contract[ step ][ "amt" ] - 500,
scriptPubKey: tapscript.Address.toScriptPubKey( "tb1qd28npep0s8frcm3y7dxqajkcy2m40eysplyr9v" )
}]
});
var target = overwrites_self[ 0 ];
var script = overwrites_self[ 1 ];
var sig = tapscript.Signer.taproot.sign( alice[ "privkey" ], txdata, 0, { extension: target });
var [ tpubkey, cblock ] = tapscript.Tap.getPubKey( "ab".repeat( 32 ), { tree: alice[ "trees" ][ turn ], target });
var bobs_preimage_1 = alice["bobs_moves"][turn-1]
txdata.vin[ 0 ].witness = [ sig, bobs_preimage_1, bobs_preimage_2, script, cblock ];
var txhex = tapscript.Tx.encode( txdata ).hex;
console.log( "broadcast this to take bob's money:", txhex );txdata.vin[ 0 ].witness = [ sig, bobs_preimage_2, bobs_preimage_1, script, cblock ];
var txhex = tapscript.Tx.encode( txdata ).hex;
console.log( "or this:", txhex );
}
}
var hash_of_bobs_preimage = rmd160( hexToBytes( alice[ "bobs_moves" ][ turn - 1 ] ) );
var index_of_bobs_preimage = bob[ "hashes" ].indexOf( hash_of_bobs_preimage );
var square_bobs_preimage_belongs_to = index_of_bobs_preimage % 9;
if ( alice[ "bobs_acks" ][ square_bobs_preimage_belongs_to ] ) {
var bobs_preimage_1 = alice[ "bobs_moves" ][turn-1];
var bobs_preimage_2 = alice[ "bobs_acks" ][ square_bobs_preimage_belongs_to ];
var overwrites_opponent = proveOverwriteOpponent( bobs_preimage_1, bobs_preimage_2, bob, alice );
if ( overwrites_opponent ) {
var txdata = tapscript.Tx.create({
vin: [{
txid: contract[ step ][ "txid" ],
vout: contract[ step ][ "vout" ],
prevout: {