-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
1266 lines (1262 loc) · 68.7 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://bitcoincore.tech/apps/bitcoinjs-ui/lib/bitcoinjs-lib.js"></script>
<title>Zaplocker</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Ubuntu+Mono&display=swap" rel="stylesheet">
<script src="https://supertestnet.github.io/bitcoin-chess/js/bolt11.js"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://unpkg.com/@cmdcode/tapscript"></script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://bundle.run/[email protected]"></script>
<script>var Buffer = buffer.Buffer;</script>
<script src="https://bundle.run/[email protected]"></script>
<script src="https://bundle.run/[email protected]"></script>
<style>
* {
box-sizing: border-box;
font-size: 1.15rem;
font-family: Arial, sans-serif;
}
html {
max-width: 70ch;
padding: 3rem 1rem;
margin: auto;
line-height: 1.25;
}
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
input {
line-height: 1.25;
width: 100%;
height: 1.8rem;
font-size: 1.15rem;
border: 1px solid grey;
}
.hidden {
display: none;
}
.crossed_out {
text-decoration: line-through;
}
.header_wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
.log_in, .log_out {
height: 2rem;
}
.desired_username {
text-align: right;;
}
.bad_form {
outline: 3px solid red;
background-color: pink;
outline-offset: 3px;
}
.black-bg {
display: none;
width: 100%;
position: fixed;
top: 0;
left: 0;
background-color: black;
opacity: .5;
width: 100vw;
height: 100vh;
}
.modal {
display: none;
position: fixed;
box-sizing: border-box;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100%;
max-width: 560px;
background-color: white;
border-radius: 1rem;
padding: 20px;
color: black;
text-align: center;
word-wrap: break-word;
}
.modal * {
color: black;
}
.pending_pmt {
margin-bottom: 1rem;
padding: 1rem;
border: 1px solid black;
border-radius: 1rem;
word-wrap: break-word;
}
.loaded_paycode {
word-wrap: break-word;
}
.tan {
background-color: tan;
padding: 1rem;
}
.black_stripes, .black_stripes_bottom {
background: repeating-linear-gradient(
0deg,
white,
white 1rem,
black 1rem,
black 2rem
);
height: 8rem;
display: flex;
justify-content: center;
align-items: flex-end;
}
.black_stripes_bottom {
align-items: center;
height: 9rem;
}
.black_stripes span {
font-family: 'Anton', sans-serif;
font-weight: bold;
font-size: 200%;
letter-spacing: 10px;
color: red;
padding: 1rem 20rem;
background-image: linear-gradient(to right, rgba(0,0,0,0) 10%, rgba(255,255,255,1) 35%, rgba(255,255,255,1) 64%, rgba(0,0,0,0) 90%);
}
.black_stripes_bottom span {
padding: 1rem 10rem;
background-image: linear-gradient(to right, rgba(0,0,0,0) 10%, rgba(255,255,255,1) 35%, rgba(255,255,255,1) 64%, rgba(0,0,0,0) 90%);
}
.black_stripes_bottom span button {
position: relative;
font-family: 'Ubuntu Mono', monospace;
font-weight: bold;
font-size: 120%;
letter-spacing: 3px;
color: white;
background-image: linear-gradient(to bottom, rgba(255,255,255,1) .5%, rgba(255,0,0,1) 35%, rgba(255,0,0,1) 65%, rgba(255,255,255,1) 99.5%);
border: 2px solid darkred;
border-radius: 1rem;
padding: .4rem 1rem;
cursor: pointer;
}
.black_stripes_bottom span button:active {
top: 2px;
left: 2px;
}
.big_btns {
display: flex;
justify-content: space-between;
}
.big_btns * {
font-family: 'Ubuntu Mono', monospace;
}
.big_btn {
text-align: center;
}
.big_logo {
width: 100% !important;
background-size: contain;
background-repeat: no-repeat;
background-position: center center;
height: 6rem;
width: 4rem;
}
.big_num {
color: red;
font-size: 150%;
margin-right: .2rem;
}
.log_in_logo {
background-image: url( 'https://swapservice.xyz/nostr-icon-purple-on-white%201.png' );
}
.choose_logo {
background-size: 80%;
background-image: url( 'https://swapservice.xyz/head%201.png' );
}
.receive_logo {
background-image: url( 'https://swapservice.xyz/head-2%201.png' );
}
@media screen and (max-width: 600px) {
}
@media screen and (max-width: 400px) {
.big_btns {
display: block;
}
.big_btn {
margin: 3rem 0;
}
.choose_logo {
background-size: 50%;
}
}
</style>
<script>
var $ = document.querySelector.bind( document );
var $$ = document.querySelectorAll.bind( document );
var url_params = new URLSearchParams( window.location.search );
var url_keys = url_params.keys();
var $_GET = {}
for ( var key of url_keys ) $_GET[ key ] = url_params.get( key );
</script>
<script>
var { getSharedSecret, schnorr, utils } = nobleSecp256k1;
var crypto = window.crypto;
var getRand = size => crypto.getRandomValues(new Uint8Array(size));
var sha256 = bitcoinjs.crypto.sha256;
var keypair = bitcoinjs.ECPair.makeRandom();
var privKey = keypair.privateKey.toString( "hex" );
var pubKey = keypair.publicKey.toString( "hex" );
pubKey = pubKey.substring( 2 );
console.log( pubKey );
</script>
<script>
var v2 = false;
var user_pubkey = null;
function modalVanish() {
$( ".black-bg" ).style.display = "none";
$( ".modal" ).style.display = "none";
}
function showModal( content, block_til_clear ) {
if ( block_til_clear ) var fn = `modalVanish();sessionStorage[ 'modal_cleared' ] = true;`; else var fn = `modalVanish();`;
$( ".modal" ).innerHTML = `<div style="position: absolute;right: 1rem;top: 0.5rem;font-size: 2rem; cursor: pointer; color: black;" onclick="${fn}">×</div>`;
$( ".modal" ).innerHTML += `<div style="overflow-y: auto; max-height: 80vh; margin-top: 1.5rem;">${content}</div>`;
$( ".black-bg" ).style.display = "block";
$( ".modal" ).style.display = "block";
}
function hexToBytes( hex ) {
return Uint8Array.from( hex.match( /.{1,2}/g ).map( ( byte ) => parseInt( byte, 16 ) ) );
}
function textToHex( text ) {
var encoder = new TextEncoder().encode( text );
return [...new Uint8Array(encoder)]
.map( x => x.toString( 16 ).padStart( 2, "0" ) )
.join( "" );
}
var username_is_good = async name => {
var port = "";
if ( window.location.port ) port = `:${window.location.port}`;
var username_is_good = await getData( `${window.location.protocol + "//" + window.location.hostname + port}/test_username/?username=${name}` );
username_is_good = username_is_good.includes( "error" ) ? false : true;
return username_is_good;
}
function pubkeyToNpub( hex ) {
return bech32.bech32.encode( "npub", bech32.bech32.toWords( hexToBytes( hex, "hex" ) ) );
}
function getData( url ) {
return new Promise( async function( resolve, reject ) {
function inner_get( url ) {
var xhttp = new XMLHttpRequest();
xhttp.open( "GET", url, true );
xhttp.send();
return xhttp;
}
var data = inner_get( url );
data.onerror = function( e ) {
resolve( "error" );
}
async function isResponseReady() {
return new Promise( function( resolve2, reject ) {
if ( !data.responseText || data.readyState != 4 ) {
setTimeout( async function() {
var msg = await isResponseReady();
resolve2( msg );
}, 1 );
} else {
resolve2( data.responseText );
}
});
}
var returnable = await isResponseReady();
resolve( returnable );
});
}
async function postData( url, json, content_type = "", apikey = "" ) {
var rtext = "";
function inner_post( url, json, content_type = "", apikey = "" ) {
var xhttp = new XMLHttpRequest();
xhttp.open( "POST", url, true );
if ( content_type ) {
xhttp.setRequestHeader( `Content-Type`, content_type );
}
if ( apikey ) {
xhttp.setRequestHeader( `X-Api-Key`, apikey );
}
xhttp.send( json );
return xhttp;
}
var data = inner_post( url, json, content_type, apikey );
data.onerror = function( e ) {
rtext = "error";
}
async function isResponseReady() {
return new Promise( function( resolve, reject ) {
if ( rtext == "error" ) {
resolve( rtext );
}
if ( !data.responseText || data.readyState != 4 ) {
setTimeout( async function() {
var msg = await isResponseReady();
resolve( msg );
}, 50 );
} else {
resolve( data.responseText );
}
});
}
var returnable = await isResponseReady();
return returnable;
}
function pubkeyFromNpub( npub ) {
return bytesToHex( bech32.bech32.fromWords( bech32.bech32.decode( npub ).words ) );
}
function bytesToHex( bytes ) {
return bytes.reduce( ( str, byte ) => str + byte.toString( 16 ).padStart( 2, "0" ), "" );
}
var isValidNpub = npub => {
try {
var hex = pubkeyFromNpub( npub );
if ( hex.length == 64 && isValidHex( hex ) ) return true;
} catch( e ) {
return;
}
return;
}
function isValidHex( h ) {
if ( !h ) return;
var length = h.length;
if ( length % 2 ) return;
try {
var a = BigInt( "0x" + h, "hex" );
} catch( e ) {
return;
}
var unpadded = a.toString( 16 );
var padding = [];
var i; for ( i=0; i<length; i++ ) padding.push( 0 );
padding = padding.join( "" );
padding = padding + unpadded.toString();
padding = padding.slice( -Math.abs( length ) );
return ( padding === h );
}
async function getBlockheight( network ) {
var data = await getData( "https://blockstream.info/" + network + "api/blocks/tip/height" );
return Number( data );
}
function generateHtlc(serverPubkey, userPubkey, pmthash, timelock) {
return bitcoinjs.script.fromASM(
`
OP_SIZE
${bitcoinjs.script.number.encode(32).toString('hex')}
OP_EQUALVERIFY
OP_SHA256
${pmthash}
OP_EQUAL
OP_IF
${userPubkey}
OP_ELSE
${bitcoinjs.script.number.encode(timelock).toString("hex")}
OP_CHECKLOCKTIMEVERIFY
OP_DROP
${serverPubkey}
OP_ENDIF
OP_CHECKSIG
`
.trim()
.replace(/\s+/g, " ")
);
}
//this function returns true if the address received money or false if it did not
function addressOnceHadMoney(address) {
return new Promise(function (resolve, reject) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (
this.readyState == 4 &&
this.status > 199 &&
this.status < 300
) {
var json = JSON.parse(xhttp.responseText);
if (
json["chain_stats"]["funded_txo_count"] > 0 ||
json["mempool_stats"]["funded_txo_count"] > 0
) {
resolve(true);
}
resolve(false);
}
};
xhttp.open(
"GET",
"https://mempool.space/api/address/" + address,
true
);
xhttp.send();
});
}
//this function waits until money arrives in an address, checking every five seconds, and then returns true -- it never returns false but it can hang forever
async function waitForMoneyToArriveInAddress(address) {
async function isAddressFundedYet(address) {
var address_received_money = await addressOnceHadMoney(address);
return new Promise(function (resolve, reject) {
if (!address_received_money) {
setTimeout(async function () {
var msg = await isAddressFundedYet(address);
resolve(msg);
}, 5000);
} else {
resolve(address_received_money);
}
});
}
async function getTimeoutData() {
var address_received_money = await isAddressFundedYet(address);
return address_received_money;
}
var returnable = await getTimeoutData();
return returnable;
}
async function addressReceivedMoneyInThisTx(address) {
let txid;
let vout;
let amt;
let nonjson = await getData("https://mempool.space/api/address/" + address + "/txs");
let json = JSON.parse(nonjson);
json.forEach(function (tx) {
tx["vout"].forEach(function (output, index) {
if (output["scriptpubkey_address"] == address) {
txid = tx["txid"];
vout = index;
amt = output["value"];
}
});
});
return [txid, vout, amt];
}
function sweepingHTLC( txid, txindex, original_quantity_of_sats, new_quantity_of_sats, userPrivkey, serverPubkey, preimage, timelock, useraddress, userPubkey ) {
console.log(
"serverPubkey:",
serverPubkey,
"userPubkey:",
userPubkey,
"preimage:",
preimage,
"timelock:",
timelock
);
var pmthash = bitcoinjs.crypto
.sha256(Buffer.from(preimage, "hex"))
.toString("hex");
var witnessscript = generateHtlc(
serverPubkey,
userPubkey,
pmthash,
timelock
);
console.log("witness script:", witnessscript.toString("hex"));
var outputscript =
"00" + bitcoinjs.crypto.sha256(witnessscript).toString("hex");
// var useraddress = "tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt";
var psbt = new bitcoinjs.Psbt({ network: bitcoinjs.networks.mainnet });
// psbt.setLocktime( timelock );
psbt.addInput({
hash: txid,
index: txindex,
sequence: 0xfffffffe,
witnessUtxo: {
script: Buffer.from(
"0020" +
bitcoinjs.crypto
.sha256(Buffer.from(witnessscript, "hex"))
.toString("hex"),
"hex"
),
value: original_quantity_of_sats,
},
witnessScript: Buffer.from(witnessscript, "hex"),
});
psbt.addOutput({
address: useraddress,
value: new_quantity_of_sats,
});
psbt.signInput(
0,
bitcoinjs.ECPair.fromPrivateKey(Buffer.from(userPrivkey, "hex"))
);
var getFinalScripts = (txindex, input, script) => {
// Step 1: Check to make sure the meaningful locking script matches what you expect.
var decompiled = bitcoinjs.script.decompile(script);
if (!decompiled || decompiled[0] !== bitcoinjs.opcodes.OP_SIZE) {
throw new Error(`Can not finalize input #${txindex}`);
}
// Step 2: Create final scripts
var witnessStackClaimBranch = bitcoinjs.payments.p2wsh({
redeem: {
network: bitcoinjs.networks.mainnet,
output: script,
input: bitcoinjs.script.compile([
input.partialSig[0].signature,
Buffer.from(preimage, "hex"),
]),
},
network: bitcoinjs.networks.mainnet,
});
console.log("First branch witness stack:");
console.log(
witnessStackClaimBranch.witness.map((x) => x.toString("hex"))
);
return {
finalScriptWitness: witnessStackToScriptWitness(
witnessStackClaimBranch.witness
),
};
};
psbt.finalizeInput(0, getFinalScripts);
return psbt.extractTransaction().toHex();
}
async function getMinFeeRate() {
var fees = await getData( "https://mempool.space/api/v1/fees/recommended" );
fees = JSON.parse( fees );
if ( !( "hourFee" in fees ) ) return "error -- site down";
var minfee = fees[ "hourFee" ];
return minfee;
}
function witnessStackToScriptWitness(witness) {
let buffer2 = buffer.Buffer.allocUnsafe(0);
function writeSlice(slice) {
buffer2 = buffer.Buffer.concat([buffer2, buffer.Buffer.from(slice)]);
}
function writeVarInt(i) {
const currentLen = buffer2.length;
const varintLen = varuintBitcoin.encodingLength(i);
buffer2 = buffer.Buffer.concat([buffer2, buffer.Buffer.allocUnsafe(varintLen)]);
varuintBitcoin.encode(i, buffer2, currentLen);
}
function writeVarSlice(slice) {
writeVarInt(slice.length);
writeSlice(slice);
}
function writeVector(vector) {
writeVarInt(vector.length);
vector.forEach(writeVarSlice);
}
writeVector(witness);
return buffer2;
}
async function pushBTCpmt(rawtx) {
var txid = await postData( "https://mempool.space/api/tx", rawtx );
return txid;
}
async function getNote( item ) {
async function isNoteSetYet( note_i_seek ) {
return new Promise( function( resolve, reject ) {
if ( !note_i_seek ) {
setTimeout( async function() {
var msg = await isNoteSetYet( sessionStorage[ item ] );
resolve( msg );
}, 100 );
} else {
resolve( note_i_seek );
}
});
}
async function getTimeoutData() {
var note_i_seek = await isNoteSetYet( sessionStorage[ item ] );
return note_i_seek;
}
var returnable = await getTimeoutData();
return returnable;
}
var loadUser = async ( user_pubkey, port, user_data ) => {
var port = "";
if ( window.location.port ) port = `:${window.location.port}`;
user_data = JSON.parse( user_data );
$( '.loaded_username' ).innerText = user_data[ "username" ];
$( '.loaded_lnaddress' ).innerText = user_data[ "username" ] + "@" + window.location.hostname + port;
$( '.loaded_paycode' ).innerText = bech32.bech32.encode( "lnurl", bech32.bech32.toWords( hexToBytes( textToHex( window.location.protocol + "//" + window.location.hostname + port + "/.well-known/lnurlp/" + user_data[ "username" ] ) ) ), 10000 ).toUpperCase();
if ( user_data[ "relays_array" ] ) {
v2 = true;
$( '.v2_info' ).classList.remove( "hidden" );
user_data[ "relays_array" ].forEach( relay => {
var li = document.createElement( "li" );
li.innerText = relay;
$( '.user_relays' ).append( li );
});
$( '.relays_sig' ).innerText = user_data[ "relays_sig" ];
var user_pubkey = await window.nostr.getPublicKey();
var sig_is_good = await nobleSecp256k1.schnorr.verify( user_data[ "relays_sig" ], sha256( JSON.stringify( user_data[ "relays_array" ] ) ).toString( "hex" ), user_pubkey );
//console.log( "sig is good, right?", sig_is_good );
if ( !sig_is_good ) return alert( "Warning, this website is trying to scam you! It is telling you you use a certain set of relays that you did not sign! Beware, and do not trust this site any further!" );
}
$( '.onboarder' ).classList.add( "hidden" );
$( '.onboarded' ).classList.remove( "hidden" );
var preimages = await window.nostr.nip04.decrypt( user_pubkey, user_data[ "ciphertext" ] );
if ( !user_data[ "pending" ].length ) return;
var html = ``;
user_data[ "pending" ].forEach( async ( pending_pmt, index ) => {
var pmthash = pending_pmt[ "pmthash" ];
var matching_preimage;
var i; for ( i=0; i<preimages.match(/.{1,64}/g).length; i++ ) {
var preimage = preimages.match(/.{1,64}/g)[ i ];
hash = bytesToHex( sha256( hexToBytes( preimage ) ) );
if ( hash === pmthash ) {
matching_preimage = preimage;
break;
}
}
pending_pmt[ "preimage" ] = matching_preimage;
user_data[ "pending" ][ index ] = pending_pmt;
var current_blockheight = await getBlockheight( "" );
console.log( "blockheight:", current_blockheight );
var blocks_til_expiry = pending_pmt[ "expires" ] - current_blockheight;
var feerate = await getMinFeeRate();
var single_mining_fee = ( feerate * 200 );
var mining_fee = ( feerate * 200 ) * 2;
var amount_expected_in_swap_address = pending_pmt[ "amount" ] - pending_pmt[ "swap_fee" ] - single_mining_fee;
var amount_expected = pending_pmt[ "amount" ] - pending_pmt[ "swap_fee" ] - mining_fee;
var amount_expected_ln = pending_pmt[ "amount" ] - pending_pmt[ "swap_fee" ];
html += `
<div class="pending_pmt">
Amount sent: ${pending_pmt[ "amount" ]} sats<br>
Server fee: ${pending_pmt[ "swap_fee" ]} sats<br>
Mining fee (estimate -- only on the base layer): ${mining_fee} sats<br>
Amount you'll get after fees (on the base layer): ${amount_expected} sats<br>
Amount you'll get after fees (over lightning): ${amount_expected + mining_fee} sats<br>
Expires: ~${blocks_til_expiry * 10} minutes<br>
<div class="attestation_loader">Loading attestations...</div>
<div class="attestation_info hidden" data-pmthash="${pending_pmt[ "pmthash" ]}" data-pending_amt="${pending_pmt[ "amount" ]}" data-swap_fee="${pending_pmt[ "swap_fee" ]}" >
Number of attestations: <span class="att_num">0</span><br>
What this means: <span class="att_meaning"></span><br>
</div>
<p><button class="settle_bl" data-preimage="${pending_pmt[ "preimage" ]}" data-serverpub="${pending_pmt[ "serverPubkey" ]}">Settle on base layer</button></p>
<p><button class="settle_ln" data-preimage="${pending_pmt[ "preimage" ]}" data-serverpub="${pending_pmt[ "serverPubkey" ]}">Settle over lightning</button></p>
</div>
`;
if ( index === user_data[ "pending" ].length - 1 ) {
$( '.loaded_pending' ).innerText = ``;
var div1 = document.createElement( "div" );
div1.innerHTML = html;
$( '.loaded_pending' ).append( div1 );
$$( '.settle_bl' ).forEach( button => {
button.onclick = async b => {
var useraddress = prompt( "Please enter a bitcoin address where you want your money to go" );
if ( !isValidAddress( useraddress ) ) return alert( "Please try again with a valid bitcoin address" );
if ( useraddress.startsWith( "tb1p" ) ) return alert( "Please try again. That was a taproot address and taproot addresses are not supported yet." );
var swap_privkey = bytesToHex( nobleSecp256k1.utils.randomBytes() );
var swap_pubkey = nobleSecp256k1.getPublicKey( swap_privkey, true );
var current_blockheight = await getBlockheight( "" );
console.log( "blockheight:", current_blockheight );
var preimage = b.target.getAttribute( "data-preimage" );
var pmthash = bytesToHex( sha256( hexToBytes( preimage ) ) );
var serverPubkey = b.target.getAttribute( "data-serverpub" );
var timelock = current_blockheight + 10;
var witness_script = generateHtlc(
serverPubkey,
swap_pubkey,
pmthash,
timelock
);
var htlcObject = bitcoinjs.payments.p2wsh({
redeem: {
output: witness_script,
network: bitcoinjs.networks.mainnet,
},
network: bitcoinjs.networks.mainnet,
});
//send the swap address to the server along with
//your swap pubkey and the payment hash. Then have
//the server recreate the swap address and, if it
//matches the one you sent, they should send the
//amount at issue to the swap address. Then you
//should check if they sent the right amount and
//sweep it. Then they should get your preimage and
//settle the invoice that came to them.
var port = "";
if ( window.location.port ) port = `:${window.location.port}`;
var url = window.location.protocol + "//" + window.location.hostname + port + `/start_swap/?swap_pubkey=${swap_pubkey}&htlc_address=${htlcObject.address}&pmthash=${pmthash}`;
getData( url );
showModal( `Waiting for server to send your money...` );
var waitIsOver = await waitForMoneyToArriveInAddress(htlcObject.address);
if ( waitIsOver == "failure" ) return;
var tx_array = await addressReceivedMoneyInThisTx(htlcObject.address);
var txid = tx_array[0];
var txindex = tx_array[1];
var amount_received = tx_array[2];
if ( Number( amount_received ) < Number( amount_expected_in_swap_address ) && Math.abs( Number( amount_received ) - Number( amount_expected_in_swap_address ) ) / Number( amount_expected_in_swap_address ) > .02 ) return showModal( "Server tried to scam you, aborting trade! Amount received: " + Number( amount_received ) + " Amount expected: " + Number( amount_expected_in_swap_address ) + " Equality: " + Number( amount_received ) === Number( amount_expected_in_swap_address ) );
console.log( "Amount received: " + Number( amount_received ) + " Amount expected: " + Number( amount_expected_in_swap_address ) + " Equality: " + Number( amount_received ) === Number( amount_expected_in_swap_address ) );
var original_quantity_of_sats = amount_received;
var feerate = await getMinFeeRate();
var new_quantity_of_sats = amount_received - ( feerate * 200 );
if ( new_quantity_of_sats < 546 ) new_quantity_of_sats = amount_received - 200;
var userPrivkey = swap_privkey;
var tx_hex = sweepingHTLC(
txid,
txindex,
original_quantity_of_sats,
new_quantity_of_sats,
userPrivkey,
serverPubkey,
preimage,
timelock,
useraddress,
swap_pubkey
);
console.log( tx_hex );
sessionStorage.removeItem( "modal_cleared" );
showModal( `You're almost done! Just X out of this popup when the following transaction has 1 confirmation: <a href="https://mempool.space/tx/${txid}" target="_blank">https://mempool.space/tx/${txid}</a>`, true );
await getNote( "modal_cleared" );
var sweep_txid = await pushBTCpmt(tx_hex);
sessionStorage.removeItem( "modal_cleared" );
showModal( `Your transaction was a success! Here is your txid: <a href="https://mempool.space/tx/${sweep_txid}" target="_blank">https://mempool.space/tx/${sweep_txid}</a>`, true );
await getNote( "modal_cleared" );
window.location.reload();
}
});
$$( '.settle_ln' ).forEach( button => {
button.onclick = async b => {
var preimage = b.target.getAttribute( "data-preimage" );
var content = `
<p>Enter an invoice with the following amount</p>
<div class="tan">${amount_expected_ln}</div>
<p>And the following preimage</p>
<div class="tan">${preimage}</div>
<p><input class="user_invoice" placeholder="Enter your invoice here"></p>
<p><button class="invoice_submitter">Submit</button></p>
`;
showModal( content );
$( '.invoice_submitter' ).onclick = async () => {
var port = "";
if ( window.location.port ) port = `:${window.location.port}`;
var invoice = $( '.user_invoice' ).value;
var status = await getData( window.location.protocol + "//" + window.location.hostname + port + `/pay_invoice/?invoice=${invoice}` );
console.log( "status:", status );
if ( status.includes( "success" ) ) {
showModal( `Success, your invoice was settled. Now go in peace.` );
} else if ( status.includes( "undefined" ) ) {
showModal( `<p>It looks like the invoice you created has the wrong preimage. Did you create it with a custom preimage? Most wallets don't let you but LND does. On the command line, you can create an image with a custom preimage like this:</p><code style="font-family: monospace;">lncli addinvoice --preimage ${preimage} --amt ${amount_expected_ln}</code>` );
} else {
showModal( `Oh no, we couldn't find a route to you! Consider using the base layer to settle or try again later.` );
}
}
}
});
if ( v2 ) {
$$( '.attestation_info' ).forEach( async att_div => {
var ptag = nobleSecp256k1.getPublicKey( att_div.getAttribute( "data-pmthash" ), true ).substring( 2 );
var relay_to_query_for_attestations = $( '.user_relays li' ).innerText;
var attestations = await getNostrNotesByKindAndPtag( 55869, ptag, relay_to_query_for_attestations );
if ( attestations == "time is up" ) attestations = [];
attestations = removeDuplicates( attestations );
attestations = removeInvalidInvoices( attestations );
console.log( "lsp_keyhash:", user_data[ "lsp_keyhash" ] );
var user_pubkey = await window.nostr.getPublicKey();
var sig_is_good = await schnorr.verify( user_data[ "lsp_keyhash_sig" ], user_data[ "lsp_keyhash" ], user_pubkey );
//console.log( "sig is good, right?", sig_is_good );
var lsp_pubkey = await getData( window.location.protocol + "//" + window.location.hostname + port + "/get_lsp_pubkey" );
var real_keyhash = sha256( hexToBytes( lsp_pubkey ) ).toString( "hex" );
if ( real_keyhash != user_data[ "lsp_keyhash" ] || !sig_is_good ) return alert( "Warning, this website is trying to scam you! It is telling you you signed a statement saying the relay's pubkey is one thing when you really did not. Beware, and do not trust this site any further!" );
attestations = removeInvoicesWithWrongPubkeys( attestations, lsp_pubkey );
attestations = removeInvoicesWithWrongPmthash( attestations, att_div.getAttribute( "data-pmthash" ) );
var pending_amt = att_div.getAttribute( "data-pending_amt" );
pending_amt = Number( pending_amt );
var swap_fee = att_div.getAttribute( "data-swap_fee" );
swap_fee = Number( swap_fee );
var amts_match = null;
if ( attestations.length ) amts_match = pending_amt == getInvoiceAmount( attestations[ 0 ] ) ? true : false;
var num_of_attestations = attestations.length;
var att_meaning = "";
if ( !num_of_attestations ) att_meaning = `The sender did not say how much money you are suppose to receive, therefore there is no way to validate that this website is forwarding the correct amount to you. Do not settle this payment unless you trust this website to forward you the correct amount.`;
if ( num_of_attestations == 1 && amts_match ) att_meaning = `There is only one known sender and they seemed to confirm the amount you are supposed to receive. But there is no way to validate this information. This website could have falsified that notification, and there could be other, undetected senders who your LSP is waiting to steal from as soon as you settle this payment. However, this situation is the best this software can do. Proceed and settle the payment but only if you trust that your LSP is not falsifying attestations or waiting to steal from undetected senders.`;
if ( num_of_attestations > 1 ) att_meaning = `There are multiple senders who independently attest (and have proof) that your LSP is trying to cheat you by taking money that ought to come to you. Do not proceed! Instead, warn everyone not to use this LSP anymore by broadcasting the following proof that they tried to cheat you: ZAPLOCKER FRAUD PROOF -- LSP scam invoice 1: ${attestations[ 0 ]} -- LSP scam invoice 2: ${attestations[ 1 ]}`;
att_div.getElementsByClassName( "att_num" )[ 0 ].innerText = num_of_attestations;
att_div.getElementsByClassName( "att_meaning" )[ 0 ].innerText = att_meaning;
console.log( "attestation info:", ptag, relay_to_query_for_attestations, attestations );
att_div.classList.remove( "hidden" );
$( '.attestation_loader' ).classList.add( "hidden" );
if ( !num_of_attestations ) {
var event = {
"content": "",
"created_at": Math.floor( Date.now() / 1000 ),
"kind": 55869,
"tags": [ [ "p", ptag ] ],
"pubkey": user_pubkey
}
var signed_event = await window.nostr.signEvent( event );
var was_seen = await eventWasReplayedTilSeen( signed_event, relay_to_query_for_attestations );
}
});
}
}
});
}
function removeDuplicates(arr) {
return arr.filter((item,index) => arr.indexOf(item) === index);
}
function isValidInvoice( invoice ) {
try{
return ( typeof( bolt11.decode( invoice ) ) == "object" );
} catch( e ) {
return;
}
}
function removeInvalidInvoices( arr ) {
var new_arr = JSON.parse( JSON.stringify( arr ) );
new_arr.forEach( (inv,idx) => {
if ( !isValidInvoice( inv ) ) new_arr.splice( idx, 1 );
});
return new_arr;
}
function getInvoiceAmount( invoice ) {
var decoded = bolt11.decode( invoice );
var amount = decoded[ "satoshis" ].toString();
return Number( amount );
}
function getInvoicePubkey( invoice ) {
var decoded = bolt11.decode( invoice );
return decoded.payeeNodeKey;
}
function getInvoicePmthash( invoice ) {
var decoded = bolt11.decode( invoice );
var i; for ( i=0; i<decoded[ "tags" ].length; i++ ) {
if ( decoded[ "tags" ][ i ][ "tagName" ] == "payment_hash" ) {
var pmthash = decoded[ "tags" ][ i ][ "data" ].toString();
}
}
return pmthash;
}
function removeInvoicesWithWrongPubkeys( arr, right_pubkey ) {
var new_arr = JSON.parse( JSON.stringify( arr ) );
new_arr.forEach( (inv,idx) => {
if ( getInvoicePubkey( inv ) != right_pubkey ) new_arr.splice( idx, 1 );
});
return new_arr;
}
function removeInvoicesWithWrongPmthash( arr, right_pmthash ) {
var new_arr = JSON.parse( JSON.stringify( arr ) );
new_arr.forEach( (inv,idx) => {
if ( getInvoicePmthash( inv ) != right_pmthash ) new_arr.splice( idx, 1 );
});
return new_arr;
}
var eventWasReplayedTilSeen = async ( event, the_relay, num ) => {
if ( !num ) num = 0;
var note = await getNostrNote( event.id, the_relay );
if ( note != "time is up" ) return true;
console.log( "replaying this event:", event.id, "at this relay:", the_relay );
num = num + 1;
await setNote( event, null, the_relay );
var was_seen = false;
if ( num < 6 ) was_seen = await eventWasReplayedTilSeen( event, the_relay, num );
return was_seen;
}
async function getSignedEvent( event, privateKey ) {
var eventData = JSON.stringify([
0,
event['pubkey'],
event['created_at'],
event['kind'],
event['tags'],
event['content']
]);
event.id = bitcoinjs.crypto.sha256( eventData ).toString( "hex" );
event.sig = await nobleSecp256k1.schnorr.sign( event.id, privateKey );
return event;
}
var makeEvent = async ( note, recipientpubkey ) => {
var now = Math.floor( ( new Date().getTime() ) / 1000 );
if ( recipientpubkey ) {
note = encrypt( privKey, recipientpubkey, note );
var newevent = [
0,
pubKey,
now,
4,
[['p', recipientpubkey]],
note
];
} else {
var newevent = [
0,
pubKey,
now,
1,
[],
note
];
}
var message = JSON.stringify( newevent );
var msghash = bytesToHex( sha256( message ) );
var sig = await nobleSecp256k1.schnorr.sign( msghash, privKey );
var fullevent = {
"id": msghash,
"pubkey": pubKey,
"created_at": now,
"kind": recipientpubkey ? 4 : 1,
"tags": recipientpubkey ? [['p', recipientpubkey]] : [],
"content": note,
"sig": sig
}
return fullevent;
}
async function getNostrNote( id, the_relay ) {
var started_waiting_time = Math.floor( Date.now() / 1000 );
var note = "";
var relays = [the_relay];
var i; for ( i=0; i<relays.length; i++ ) {
var myrelay = relays[ i ];
socket = new WebSocket( myrelay );
socket.addEventListener( 'message', async function( event ) {
var event = JSON.parse( event.data );
if ( !event[ 2 ] ) return;
// console.log( "got an event!", event );
var sig = event[ 2 ].sig;
var eventData = JSON.stringify([
0, // Reserved for future use
event[ 2 ]['pubkey'], // The sender's public key
event[ 2 ]['created_at'], // Unix timestamp
event[ 2 ]['kind'], // Message “kind” or type
event[ 2 ]['tags'], // Tags identify replies/recipients
event[ 2 ]['content'] // Your note contents
]);
var id = sha256( eventData ).toString( 'hex' );
var pubKeyPlusOne = ( BigInt( "0x" + pubKey ) + BigInt( "0x" + pubKey ) ).toString( 16 ).substring( 0, 64 );
var valid = await nobleSecp256k1.schnorr.verify( sig, id, event[ 2 ].pubkey );
if ( valid ) note = event[ 2 ].content;
});
socket.addEventListener( 'open', function open() {
var randomid = bitcoinjs.ECPair.makeRandom().privateKey.toString( "hex" ).substring( 0, 16 );
var filter = {
"ids": [
id
]
}
var subscription = [ "REQ", randomid, filter ];
subscription = JSON.stringify( subscription );
var chaser = [ "CLOSE", randomid ];
chaser = JSON.stringify( chaser );
socket.send( subscription );
setTimeout( function() {socket.send( chaser );}, 1000 );
setTimeout( function() {socket.close();}, 2000 );
});
async function isNoteSetYet( note_i_seek ) {
return new Promise( function( resolve, reject ) {
if ( !note_i_seek ) {
var current_time = Math.floor( Date.now() / 1000 );
if ( started_waiting_time + 5 < current_time ) {
resolve( "time is up" );
}
setTimeout( async function() {
var msg = await isNoteSetYet( note );
resolve( msg );
}, 100 );
} else {
resolve( note_i_seek );
}
});
}
async function getTimeoutData() {
var note_i_seek = await isNoteSetYet( note );
return note_i_seek;
}
var returnable = await getTimeoutData();
return returnable;
}
}
var setNote = async ( note_or_event, recipient, relay ) => {
if ( typeof note_or_event == "string" ) var event = await makeEvent( note_or_event, recipient );
else var event = note_or_event;
var mysocket = new WebSocket( relay );
mysocket.addEventListener( "open", () => mysocket.send( JSON.stringify( ["EVENT", event ] ) ) );
}
async function getNostrNotesByKindAndPtag( kind, ptag, the_relay ) {
var started_waiting_time = Math.floor( Date.now() / 1000 );
var notes = [];
var relays = [the_relay];
var i; for ( i=0; i<relays.length; i++ ) {
var myrelay = relays[ i ];
socket = new WebSocket( myrelay );
socket.addEventListener( 'message', async function( event ) {
var event = JSON.parse( event.data );
if ( !event[ 2 ] ) return;
// console.log( "got an event!", event );
var sig = event[ 2 ].sig;