Update nwcjs.js

This commit is contained in:
supertestnet authored and GitHub committed 2025-05-05 22:10:18 -06:00
1 parent f1cc987211
commit e8022ac575
1 file changed
+119
+119
View File
@@ -199,6 +199,125 @@ var nwcjs = {
}
return await loop();
},
makeHodlInvoice: async ( nwc_info, amt, payment_hash, expiry, desc, desc_hash, seconds_of_delay_tolerable = 3 ) => {
var msg = {
method: "make_hodl_invoice",
params: {
amount: amt * 1000,
}
}
if ( payment_hash ) msg.params.payment_hash = payment_hash;
if ( expiry ) msg.params.expiry = expiry;
if ( desc ) msg.params.description = desc;
if ( desc_hash ) msg.params.desc_hash = desc_hash;
msg = JSON.stringify( msg );
var emsg = await nwcjs.encrypt( nwc_info[ "app_privkey" ], nwc_info[ "wallet_pubkey" ], msg );
var obj = {
kind: 23194,
content: emsg,
tags: [ [ "p", nwc_info[ "wallet_pubkey" ] ] ],
created_at: Math.floor( Date.now() / 1000 ),
pubkey: nwc_info[ "app_pubkey" ],
}
var event = await nwcjs.getSignedEvent( obj, nwc_info[ "app_privkey" ] );
var id = event.id;
nwcjs.getResponse( nwc_info, id, "make_hodl_invoice", seconds_of_delay_tolerable );
await nwcjs.waitSomeSeconds( 1 );
var relay = nwc_info[ "relay" ];
nwcjs.sendEvent( event, relay );
var loop = async () => {
await nwcjs.waitSomeSeconds( 1 );
if ( !nwcjs.response.length ) return await loop();
var one_i_want = null;
nwcjs.response.every( ( item, index ) => {
if ( item[ "result_type" ] === "make_hodl_invoice" ) {
one_i_want = item;
nwcjs.response.splice( index, 1 );
return;
}
return true;
});
if ( one_i_want ) return one_i_want;
return await loop();
}
return await loop();
},
settleHodlInvoice: async ( nwc_info, preimage, seconds_of_delay_tolerable = 3 ) => {
var msg = JSON.stringify({
method: "settle_hodl_invoice",
params: {
preimage,
}
});
var emsg = await nwcjs.encrypt( nwc_info[ "app_privkey" ], nwc_info[ "wallet_pubkey" ], msg );
var obj = {
kind: 23194,
content: emsg,
tags: [ [ "p", nwc_info[ "wallet_pubkey" ] ] ],
created_at: Math.floor( Date.now() / 1000 ),
pubkey: nwc_info[ "app_pubkey" ],
}
var event = await nwcjs.getSignedEvent( obj, nwc_info[ "app_privkey" ] );
var id = event.id;
nwcjs.getResponse( nwc_info, id, "settle_hodl_invoice", seconds_of_delay_tolerable );
await nwcjs.waitSomeSeconds( 1 );
var relay = nwc_info[ "relay" ];
nwcjs.sendEvent( event, relay );
var loop = async () => {
await nwcjs.waitSomeSeconds( 1 );
if ( !nwcjs.response.length ) return await loop();
var one_i_want = null;
nwcjs.response.every( ( item, index ) => {
if ( item[ "result_type" ] === "settle_hodl_invoice" ) {
one_i_want = item;
nwcjs.response.splice( index, 1 );
return;
}
return true;
});
if ( one_i_want ) return one_i_want;
return await loop();
}
return await loop();
},
cancelHodlInvoice: async ( nwc_info, payment_hash, seconds_of_delay_tolerable = 3 ) => {
var msg = JSON.stringify({
method: "cancel_hodl_invoice",
params: {
payment_hash,
}
});
var emsg = await nwcjs.encrypt( nwc_info[ "app_privkey" ], nwc_info[ "wallet_pubkey" ], msg );
var obj = {
kind: 23194,
content: emsg,
tags: [ [ "p", nwc_info[ "wallet_pubkey" ] ] ],
created_at: Math.floor( Date.now() / 1000 ),
pubkey: nwc_info[ "app_pubkey" ],
}
var event = await nwcjs.getSignedEvent( obj, nwc_info[ "app_privkey" ] );
var id = event.id;
nwcjs.getResponse( nwc_info, id, "cancel_hodl_invoice", seconds_of_delay_tolerable );
await nwcjs.waitSomeSeconds( 1 );
var relay = nwc_info[ "relay" ];
nwcjs.sendEvent( event, relay );
var loop = async () => {
await nwcjs.waitSomeSeconds( 1 );
if ( !nwcjs.response.length ) return await loop();
var one_i_want = null;
nwcjs.response.every( ( item, index ) => {
if ( item[ "result_type" ] === "cancel_hodl_invoice" ) {
one_i_want = item;
nwcjs.response.splice( index, 1 );
return;
}
return true;
});
if ( one_i_want ) return one_i_want;
return await loop();
}
return await loop();
},
checkInvoice: async ( nwc_info, invoice, seconds_of_delay_tolerable = 3 ) => {
var msg = JSON.stringify({
method: "lookup_invoice",