-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
515 lines (514 loc) · 24.2 KB
/
Copy pathindex.html
File metadata and controls
515 lines (514 loc) · 24.2 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<script src="https://supertestnet.github.io/bankify/super_nostr.js"></script>
<script src="https://supertestnet.github.io/nwcjs/nwcjs.js"></script>
<script src="https://bundle.run/noble-secp256k1@1.2.14"></script>
<script src="https://bundle.run/bech32@2.0.0"></script>
<script>
//nsec1jhd5tlmnelfjv9gqnx0hdr48gsqttyyypky5z78v82ja0wptuvnqpqvzpl
var dominus = e => e.target.parentElement.remove();
var doplus = init => {
if ( $( '.plus_button' ) ) {
$( '.plus_button' ).classList.add( "minus_button" );
$( '.plus_button' ).innerHTML = 'Remove item';
$( '.plus_button' ).onclick = dominus;
$( '.plus_button' ).classList.remove( "plus_button" );
// if ( init ) $( '.products_option' ).click();
}
var html = `
<div class="product">
<p>Product title</p>
<input class="product_title">
<p>Price (in sats)</p>
<input class="price">
<p>Location</p>
<input class="location">
<p>Affiliate discount (percentage)</p>
<input class="discount">
<p>Short description</p>
<input class="short_desc">
<p>Long description</p>
<textarea class="long_desc"></textarea>
<p>Categories (comma separated)</p>
<input class="categories">
<p>Image</p>
<input class="product_image">
<button class="plus_button">Add item</button>
</div>
`;
var div = document.createElement( "div" );
div.innerHTML = html;
div.getElementsByClassName( "plus_button" )[ 0 ].onclick = doplus;
$( '.products' ).append( div.firstElementChild );
}
var hexToText = hex => {
var bytes = new Uint8Array( Math.ceil( hex.length / 2 ) );
var i; for ( i=0; i<hex.length; i++ ) bytes[ i ] = parseInt( hex.substr( i * 2, 2 ), 16 );
var text = new TextDecoder().decode( bytes );
return text;
}
var textToHex = text => {
var encoded = new TextEncoder().encode( text );
return Array.from( encoded )
.map( x => x.toString( 16 ).padStart( 2, "0" ) )
.join( "" );
}
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 extractTagFromNostrEvent = ( event, tag ) => {
var data = null;
event.tags.every( item => {
if ( item[ 0 ] === tag ) {
data = item[ 1 ];
return;
}
return true;
});
if ( data ) return data;
return "no data";
}
var extractHashtagsFromNostrEvent = event => {
var hashtags = [];
event.tags.forEach( item => {
if ( item[ 0 ] === "t" ) hashtags.push( item[ 1 ] );
});
if ( hashtags ) return hashtags;
return "no hashtags";
}
var waitSomeTime = num => new Promise( resolve => setTimeout( resolve, num ) );
var affiliatePlus = init => {
if ( $( '.affiliate_plus_button' ) ) {
$( '.affiliate_plus_button' ).classList.add( "affiliate_minus_button" );
$( '.affiliate_plus_button' ).innerHTML = '-';
$( '.affiliate_plus_button' ).onclick = dominus;
$( '.affiliate_plus_button' ).classList.remove( "affiliate_plus_button" );
// if ( init ) $( '.affiliates_option' ).click();
}
var html = `
<div class="affiliate">
<p>Affiliate npub</p>
<input class="affiliate_nprofile">
<span class="affiliate_plus_button">+</span>
</div>
`;
var div = document.createElement( "div" );
div.innerHTML = html;
div.getElementsByClassName( "affiliate_plus_button" )[ 0 ].onclick = affiliatePlus;
$( '.affiliates' ).append( div.firstElementChild );
}
var convertNEvent = nevent => {
var arr = bech32.bech32.fromWords( bech32.bech32.decode( nevent, 100_000 ).words );
var hex = bytesToHex( arr );
if ( !hex.startsWith( "0020" ) ) var event_id = hex.substring( hex.length - 64 );
else var event_id = hex.substring( 4, 68 );
if ( !hex.startsWith( "0020" ) ) hex = hex.substring( 0, hex.length - 64 );
else hex = hex.substring( 68 );
var relays = [];
var loop = () => {
if ( hex.startsWith( "01" ) ) {
var relay_length = parseInt( hex.substring( 2, 4 ), 16 );
relays.push( hexToText( hex.substring( 4, 4 + relay_length * 2 ) ) );
hex = hex.substring( 4 + relay_length * 2 );
loop();
}
}
loop();
return [ event_id, relays ];
}
var convertPubkeyAndRelaysToNprofile = ( prefix, pubkey, relays ) => {
var relays_str = "";
relays.forEach( relay => {
var relay_str = textToHex( relay );
var len = ( relay_str.length / 2 ).toString( 16 );
if ( len.length % 2 ) len = "0" + len;
relays_str = relays_str + "01" + len + relay_str;
});
var hex = relays_str + "0020" + pubkey;
var bytes = hexToBytes( hex );
var nevent = bech32.bech32.encode( prefix, bech32.bech32.toWords( bytes ), 100_000 );
return nevent;
}
var pubkeyToNpub = hex => bech32.bech32.encode( "npub", bech32.bech32.toWords( hexToBytes( hex ) ) );
var pubkeyFromNpub = npub => super_nostr.bytesToHex( bech32.bech32.fromWords( bech32.bech32.decode( npub, 10_000 ).words ) );
</script>
<style>
* {
box-sizing: border-box;
font-size: 1.15rem;
font-family: Arial, sans-serif;
}
html {
max-width: 800px;
padding: 3rem 1rem;
margin: auto;
line-height: 1.25;
padding: 0;
}
body {
margin: 3rem 1rem;
word-wrap: break-word;
}
h1 {
font-size: 2rem;
}
h2 {
font-size: 1.5rem;
}
input, textarea {
line-height: 1.25;
width: 100%;
height: 1.8rem;
font-size: 1.15rem;
border: 1px solid grey;
}
textarea {
height: auto;
}
.hidden {
display: none !important;
}
.option {
margin-bottom: 1rem;
padding: 1rem;
border: 1px solid black;
border-radius: 1rem;
background-color: #cccccc;
color: white;
font-weight: bold;
cursor: pointer;
}
.affiliates {
background-color: white;
color: black;
padding: 1rem;
border: 1px solid black;
border-radius: 1rem;
margin-top: 1rem;
cursor: auto;
}
.product {
padding: 1rem;
border: 1px solid black;
border-radius: 1rem;
margin-top: 1rem;
background-color: white;
color: black;
cursor: auto;
}
.affiliate:first-child {
margin-top: -.5rem;
}
.submit_div button {
margin-top: 1rem;
width: 100%;
}
@media screen and (max-width: 600px) {
}
</style>
<style>
.plus_button, .minus_button {
margin-top: 1.5rem;
width: 100%;
}
.affiliate_nprofile {
width: calc( 100% - 2.5rem );
}
.affiliate_plus_button, .affiliate_minus_button {
display: inline-block;
border: 1px solid grey;
padding: 0px 5px;
cursor: pointer;
width: 2rem;
text-align: center;
vertical-align: top;
height: 1.8rem;
line-height: 1.8rem;
}
</style>
<style>
.black-bg {
width: 100%;
position: fixed;
top: 0;
left: 0;
background-color: black;
opacity: .5;
width: 100vw;
height: 100vh;
}
.modal {
position: fixed;
box-sizing: border-box;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 90%;
max-width: 560px;
background-color: white;
border-radius: 1rem;
padding: 20px;
color: black;
text-align: center;
word-wrap: break-word;
}
.modal * {
color: black;
}
.modal input, .modal textarea {
max-width: 90%;
}
.hidden {
display: none !important;
}
</style>
<script>
var modalVanish = () => {
$( ".black-bg" ).classList.add( "hidden" );
$( ".modal" ).classList.add( "hidden" );
}
var showModal = content => {
$( ".modal" ).innerHTML = `<div class="x_modal" style="position: absolute;right: 1rem;top: 0.5rem;font-size: 2rem; cursor: pointer; color: black;" onclick="modalVanish()">×</div>`;
$( ".modal" ).innerHTML += `<div style="overflow-y: auto; max-height: 80vh; margin-top: 1.5rem;">${content}</div>`;
$( ".black-bg" ).classList.remove( "hidden" );
$( ".modal" ).classList.remove( "hidden" );
}
</script>
<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 );
var hash_arr = window.location.href.substring( window.location.href.indexOf( "#" ) ).split( "#" );
hash_arr.splice( 0, 1 );
var params = {}
hash_arr.forEach( item => {
var vals = item.split( "=" );
params[ vals[ 0 ] ] = vals[ 1 ];
});
if ( sessionStorage.privkey ) {
window.onload = () => {
$( '.ensure_login' ).classList.add( "hidden" );
// $( '.loading' ).classList.remove( "hidden" );
}
}
if ( params.privkey ) {
window.onload = () => {
$( '.ensure_login' ).classList.add( "hidden" );
// $( '.options' ).classList.remove( "hidden" );
}
var privkey = super_nostr.bytesToHex( bech32.bech32.fromWords( bech32.bech32.decode( params.privkey ).words ) );
//TODO: uncomment the line below
// window.location.href = window.location.href.substring( 0, window.location.href.indexOf( "#" ) );
params.privkey = privkey;
var pubkey = super_nostr.getPubkey( privkey );
sessionStorage.privkey = privkey;
sessionStorage.pubkey = pubkey;
}
var loadEvents = async () => {
var privkey = sessionStorage.privkey;
var pubkey = sessionStorage.pubkey;
events = await super_nostr.getEvents('wss://relay.damus.io', null, [ pubkey ], [30402, 13167]);
events.reverse();
var i; for ( i=0; i<events.length; i++ ) {
var event = events[ i ];
if ( event.kind === 13167 ) {
parse13167( event );
continue;
}
var parsedEvent = {
title: extractTagFromNostrEvent( event, "title" ),
price: extractTagFromNostrEvent( event, "price" ),
location: extractTagFromNostrEvent( event, "location" ),
discount: extractTagFromNostrEvent( event, "discount" ),
short_desc: extractTagFromNostrEvent( event, "summary" ),
long_desc: event.content,
categories: extractHashtagsFromNostrEvent( event ),
product_image: extractTagFromNostrEvent( event, "image" ),
}
var product = $$( '.product' )[ $$( '.product' ).length - 1 ];
product.getElementsByClassName( "product_title" )[ 0 ].value = parsedEvent.title;
product.getElementsByClassName( "price" )[ 0 ].value = parsedEvent.price;
product.getElementsByClassName( "location" )[ 0 ].value = parsedEvent.location;
product.getElementsByClassName( "discount" )[ 0 ].value = parsedEvent.discount;
product.getElementsByClassName( "short_desc" )[ 0 ].value = parsedEvent.short_desc;
product.getElementsByClassName( "long_desc" )[ 0 ].value = parsedEvent.long_desc;
product.getElementsByClassName( "categories" )[ 0 ].value = parsedEvent.categories.join( ", " );
product.getElementsByClassName( "product_image" )[ 0 ].value = parsedEvent.product_image;
$( '.plus_button' ).click();
await waitSomeTime( 100 );
}
await waitSomeTime( 1000 );
params.privkey = sessionStorage.privkey;
sessionStorage.removeItem( "privkey" );
sessionStorage.removeItem( "pubkey" );
}
var parse13167 = async event => {
event.tags.reverse();
var i; for ( i=0; i<event.tags.length; i++ ) {
var tag = event.tags[ i ];
var npub_form = $$( '.affiliate_nprofile' )[ $$( '.affiliate_nprofile' ).length - 1 ];
if ( !tag[ 1 ] ) continue;
npub_form.value = tag[ 1 ];
$( '.affiliate_plus_button' ).click();
await waitSomeTime( 100 );
}
}
loadEvents();
</script>
</head>
<body>
<h1>Welcome to lightlinks admin</h1>
<div class="loading">loading...</div>
<div class="ensure_login">
You need to be logged in. Go to lightlinks.io
</div>
<div class="lud16_entry_form hidden">
<p>Enter a lightning address where your users can send money when they buy your products</p>
<p><input class="lud16_form"></p>
<p><button class="submit_lud16_form" onclick="submitLud16();">Submit</button></form>
</div>
<div class="options hidden">
<p>Please create a product</p>
<div class="option products_option">
<div class="label">Products</div>
<div class="sometimes_invisible products hidden"></div>
<div class="submit_div hidden"><button onclick="submitProducts();">Submit</button></div>
</div>
<div class="option affiliates_option">
<div class="label">Affiliates</div>
<div class="sometimes_invisible affiliates hidden"></div>
<div class="submit_div hidden"><button onclick="submitAffiliates();">Submit</button></div>
</div>
</div>
<script>
var events = null;
var relays = [ "wss://relay.damus.io" ];
$$( '.option' ).forEach( item => item.onclick = e => {
var findParent = ( node, num_of_ups = 0 ) => {
if ( !node.parentElement ) return;
if ( node.parentElement.classList.contains( "sometimes_invisible" ) ) return;
if ( node.parentElement.classList.contains( "option" ) ) return num_of_ups;
num_of_ups = num_of_ups + 1;
return findParent( node.parentElement, num_of_ups );
}
var num_of_ups = findParent( e.target );
var target = e.target;
var i; for ( i=0; i<num_of_ups + 1; i++ ) {
target = target[ "parentElement" ];
}
var target = target.getElementsByClassName( "sometimes_invisible" )[ 0 ];
if ( !target ) return;
if ( target.classList.contains( "hidden" ) ) {
target.classList.remove( "hidden" );
target.parentElement.getElementsByClassName( "submit_div" )[ 0 ].classList.remove( "hidden" );
} else {
target.classList.add( "hidden" );
target.parentElement.getElementsByClassName( "submit_div" )[ 0 ].classList.add( "hidden" );
}
});
var submitProducts = async () => {
var privkey = params.privkey;
var products = [];
$$( '.product' ).forEach( item => {
products.push({
title: item.getElementsByClassName( "product_title" )[ 0 ].value,
price: Number( item.getElementsByClassName( "price" )[ 0 ].value ),
location: item.getElementsByClassName( "location" )[ 0 ].value,
discount: Number( item.getElementsByClassName( "discount" )[ 0 ].value ),
short_desc: item.getElementsByClassName( "short_desc" )[ 0 ].value,
long_desc: item.getElementsByClassName( "long_desc" )[ 0 ].value,
categories: item.getElementsByClassName( "categories" )[ 0 ].value.split( "," ),
product_image: item.getElementsByClassName( "product_image" )[ 0 ].value,
});
});
products.forEach( ( item, index ) => {
item.categories.forEach( ( cat, idx ) => {
if ( cat.startsWith( " " ) ) products[ index ].categories[ idx ] = cat.substring( 1 );
});
});
console.log( products );
products.forEach( async product => {
var msg = product.long_desc;
var tags = [
[ "title", product.title ],
[ "published_at", String( Math.floor( Date.now() / 1000 ) ) ],
[ "image", product.product_image ],
[ "summary", product.short_desc ],
[ "location", product.location ],
[ "price", `${product.price}`, "sats", "never", "active" ],
[ "discount", `${product.discount}`, "percent" ],
];
product.categories.forEach( cat => tags.push( [ "t", cat ] ) );
var event = await super_nostr.prepEvent( privkey, msg, 30402, tags );
super_nostr.sendEvent( event, relays[ 0 ] );
console.log( event );
});
}
var submitAffiliates = async () => {
var privkey = params.privkey;
//list of nprofiles:
//nprofile1qqszrqlfgavys8g0zf8mmy79dn92ghn723wwawx49py0nqjn7jtmjagppamhxue69uhkummnw3ezumt0d5qs6amnwvaz7tmwdaejumr0dssraqwh
//nprofile1qqsv6sy2d8xxcumu5xnkalpl5frudjjnajq87mnuj46pvsty09lgzcspz3mhxue69uhhyetvv9ujuerpd46hxtnfduqs6amnwvaz7tmwdaejumr0dsqkvamnwvaz7tmxd9k8getj9ehx7um5wgh8w6twv5hkuur4vgck2dr3vu6nvamkvsek26r9vajrserdxaexcem28p3k6wfe8pkhjufsv95rsefews6h5et3ddnnwaphwvunxufhx5c8qdek8a38ymmpv33kzum58468yat9qyt8wumn8ghj7un9d3shjtnwdaehgu3wvfskueqpp4mhxue69uhhjctzw5hx6egpypmhxue69uhhyetvv9uj66ns9ehx7um5wgh8w6tjv4jxuet59e48q904jph
var nprofiles = [];
$$( '.affiliate_nprofile' ).forEach( item => nprofiles.push( [ "affiliate", item.value ] ) );
var event = {
"kind": 13167,
"created_at": Math.floor( Date.now() / 1000 ),
"content": "",
"tags": nprofiles,
}
var event = await super_nostr.prepEvent( privkey, "", 13167, nprofiles );
super_nostr.sendEvent( event, relays[ 0 ] );
console.log( event );
}
var submitLud16 = async () => {
var lnaddy = $( '.lud16_form' ).value;
var msg = JSON.stringify({"lud16":`${lnaddy}`});
var privkey = params.privkey;
if ( !privkey ) privkey = sessionStorage.privkey;
var event = await super_nostr.prepEvent( privkey, msg, 0, [] );
console.log( 'sent!' );
console.log( event );
super_nostr.sendEvent( event, relays[ 0 ] );
showModal( 'success! Refresh your page' );
}
var getZapReceipt = async checking_id => {
var events = await nwcjs.getEvents( relays[ 0 ], null, [ 9735 ], null, null, 1, [ checking_id ], null, 3 );
if ( !events.length ) return;
return events[ 0 ];
}
(async()=>{
doplus( "init" );
affiliatePlus( "init" );
$( '.lud16_form' ).value = '';
var pubkey = sessionStorage.pubkey;
if ( params.pubkey ) pubkey = params.pubkey;
if ( !pubkey ) return;
//TODO: remove this pubkey
// var pubkey = `2183e94758481d0f124fbd93c56ccaa45e7e545ceeb8d52848f98253f497b975`;
var events = await super_nostr.getEvents( relays[ 0 ], null, [ pubkey ], [ 0 ], null, null, 1 );
if ( !events.length ) {
$( '.lud16_entry_form' ).classList.remove( "hidden" );
$( '.options' ).classList.add( "hidden" );
$( '.loading' ).classList.add( "hidden" );
return;
} else {
$( '.options' ).classList.remove( "hidden" );
$( '.loading' ).classList.add( "hidden" );
}
var lnaddy = JSON.parse( events[ 0 ].content ).lud16;
var [ invoice, checking_id ] = await nwcjs.getZapRequest( lnaddy, 1000, relays );
console.log( invoice, checking_id );
var receipt = await getZapReceipt( checking_id );
console.log( receipt );
})();
</script>
<div class="black-bg hidden" onclick="modalVanish();"></div>
<div class="modal hidden"></div>
</body>
</html>