-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
208 lines (206 loc) · 9.03 KB
/
index.html
File metadata and controls
208 lines (206 loc) · 9.03 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://supertestnet.github.io/blind-sig-js/blindSigJS.js"></script>
<script src="https://cdn.jsdelivr.net/gh/6502/sha256@main/sha256.js"></script>
<script>var SHA256 = string_or_uint8array => bytesToHex( sha256( string_or_uint8array ) );</script>
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script>
var isValidHex = hex => {
if ( !hex ) return;
var length = hex.length;
if ( length % 2 ) return;
try {
var bigint = BigInt( "0x" + hex, "hex" );
} catch( e ) {
return;
}
var prepad = bigint.toString( 16 );
var i; for ( i=0; i<length; i++ ) prepad = "0" + prepad;
var padding = prepad.slice( -Math.abs( length ) );
return ( padding === hex );
}
</script>
<script>
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 hexToText = hex => {
var bytes = new Uint8Array(Math.ceil(hex.length / 2));
for (var i = 0; i < hex.length; i++) bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
var text = new TextDecoder().decode( bytes );
return text;
}
</script>
<script>
var waitASec=num=>new Promise(res=>setTimeout(res,num*1000));
</script>
<script>
var decomposeAmount = amount_to_decompose => {
var decomposed = [];
var getBaseLog = ( x, y ) => Math.log(y) / Math.log(x);
var inner_fn = amt => {
var exponent = Math.floor( getBaseLog( 2, amt ) );
decomposed.push( 2 ** exponent );
amount_to_decompose = amt - 2 ** exponent;
if ( amount_to_decompose ) inner_fn(amount_to_decompose);
}
inner_fn( amount_to_decompose );
return decomposed;
}
</script>
</head>
<body>
<center><h2 class="balance">0</h2></center></h1>
<center>
<button class="send">Send</button>
<button class="receive">Receive</button>
</center>
<script>
var utxos = [];
var $ = document.querySelector.bind( document );
var $$ = document.querySelectorAll.bind( document );
(async () => {
// Step 1 Create a 32 byte secret
var secret_for_msg = bytesToHex(blindSigJS.getRand(32));
console.log( "secret_for_msg:", secret_for_msg );
// Step 2 Pick one of the mint's keysets
var endpoint = `https://testnut.cashu.space`;
sessionStorage[ "endpoint" ] = endpoint;
var keysets = await fetch( `${endpoint}/v1/keysets` );
keysets = await keysets.json();
keysets = keysets[ "keysets" ];
console.log( "keysets:", keysets );
var keyset;
keysets.every( item => {if ( isValidHex( item.id ) && item.active ) {keyset = item.id;return;} return true;});
console.log( "keyset:", keyset );
// Step 3 Pick an amount and get its pubkey
var pubkeys = await fetch(`${endpoint}/v1/keys/${keyset}`);
pubkeys = await pubkeys.json();
pubkeys = pubkeys[ "keysets" ][ 0 ][ "keys" ];
console.log( "pubkeys:", pubkeys );
var amount_i_want = 256;
var amt_pubkey = pubkeys[ amount_i_want ];
console.log( "amt_pubkey:", amt_pubkey );
// Step 4 Get an LN invoice for that amount
var post_data = {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({"amount": amount_i_want, "unit": "sat"}),
}
var invoice_data = await fetch( `${endpoint}/v1/mint/quote/bolt11`, post_data );
invoice_data = await invoice_data.json();
console.log( "invoice_data:", invoice_data );
// Step 5 Pay that invoice (but not on testnut)
console.log( `pay this: ${invoice_data[ "request" ]}` );
var isPaid = async invoice => {
await waitASec( 5 );
var is_paid_info = await fetch( `${endpoint}/v1/mint/quote/bolt11/${invoice_data[ "quote" ]}` );
is_paid_info = await is_paid_info.json();
var is_paid = is_paid_info[ "paid" ];
console.log( "it is paid, right?", is_paid );
return is_paid || isPaid( invoice );
}
await isPaid( invoice_data[ "request" ] );
// Step 6 Prepare a sig request
var message = new blindSigJS.bsjMsg();
var B_ = await message.createBlindedMessageFromString( secret_for_msg );
var B_hex = blindSigJS.ecPointToHex( B_ );
var sig_request = {"quote": invoice_data[ "quote" ], "outputs": [{
"amount": amount_i_want,
"id": keyset,
"B_": B_hex,
}]}
console.log( "sig_request:", sig_request );
// Step 7 Get a blinded sig
var post_data = {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify(sig_request),
}
var blinded_sigs = await fetch( `${endpoint}/v1/mint/bolt11`, post_data );
blinded_sigs = await blinded_sigs.json();
var blinded_sig = blinded_sigs[ "signatures" ][ 0 ][ "C_" ];
console.log( "blinded_sig:", blinded_sig );
// Step 8 Unblind the sig and create a "utxo"
var C_ = nobleSecp256k1.Point.fromCompressedHex( hexToBytes( blinded_sig ) );
amt_pubkey = nobleSecp256k1.Point.fromCompressedHex( hexToBytes( amt_pubkey ) );
var {C} = message.unblindSignature(C_, amt_pubkey);
var compressed_C = nobleSecp256k1.Point.fromHex( blindSigJS.ecPointToHex( C ) ).toHex( true );
var utxo = {
id: keyset,
amount: amount_i_want,
secret: secret_for_msg,
C: compressed_C,
}
console.log( "utxo:", utxo );
utxos.push( utxo );
var bal = 0;
utxos.forEach( item => bal = bal + item[ "amount" ] );
$( '.balance' ).innerText = bal;
})();
$( '.send' ).onclick = async () => {
var nut = {
mint: `https://testnut.cashu.space`,
proofs: [],
}
utxos.forEach( item => nut[ "proofs" ].push( item ) );
nut = "cashuA" + btoa( JSON.stringify( {token: [nut]} ) );
console.log( nut );
utxos = [];
$( '.balance' ).innerText = 0;
}
$( '.receive' ).onclick = async () => {
var token = prompt( `Enter a cashu token` );
token = token.substring( token.indexOf( "cashuA" ) + 6 );
token = JSON.parse( atob( token ) );
token.token[0]["proofs"].forEach(item => utxos.push(item));
var bal = 0;
utxos.forEach( item => bal = bal + item[ "amount" ] );
var secret_for_msg = bytesToHex( blindSigJS.getRand( 32 ) );
var message = new blindSigJS.bsjMsg();
var B_ = await message.createBlindedMessageFromString( secret_for_msg );
var B_hex = blindSigJS.ecPointToHex( B_ );
var new_utxo = {
"amount": utxos[ utxos.length - 1 ][ "amount" ],
"id": utxos[ utxos.length - 1 ][ "id" ],
"B_": B_hex,
}
var swap_data = {
"inputs": [ utxos[ utxos.length - 1 ] ],
"outputs": [ new_utxo ],
}
var post_data = {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify( swap_data ),
}
var endpoint = sessionStorage[ "endpoint" ];
var blinded_sigs = await fetch( `${endpoint}/v1/swap`, post_data );
blinded_sigs = await blinded_sigs.json();
var blinded_sig = blinded_sigs[ "signatures" ][ 0 ][ "C_" ];
console.log( "blinded_sig:", blinded_sig );
var keyset = utxos[ utxos.length - 1 ][ "id" ];
var pubkeys = await fetch(`${endpoint}/v1/keys/${keyset}`);
pubkeys = await pubkeys.json();
pubkeys = pubkeys[ "keysets" ][ 0 ][ "keys" ];
var amount_i_want = utxos[ utxos.length - 1 ][ "amount" ];
var amt_pubkey = pubkeys[ amount_i_want ];
console.log( "amt_pubkey:", amt_pubkey );
amt_pubkey = nobleSecp256k1.Point.fromCompressedHex( hexToBytes( amt_pubkey ) );
var new_C_ = nobleSecp256k1.Point.fromCompressedHex( hexToBytes( blinded_sig ) );
var {C} = message.unblindSignature( new_C_, amt_pubkey );
var compressed_C = nobleSecp256k1.Point.fromHex( blindSigJS.ecPointToHex( C ) ).toHex( true );
var new_utxo = {
id: utxos[ utxos.length - 1 ][ "id" ],
amount: utxos[ utxos.length - 1 ][ "amount" ],
secret: secret_for_msg,
C: compressed_C,
}
utxos.splice( utxos[ utxos.length - 1 ][ "id" ], 1 );
utxos.push( new_utxo );
$( '.balance' ).innerText = bal;
}
</script>
</body>
</html>