@@ -56,30 +56,24 @@ function processVoutAddresses(address_list, vout_value, arr_vout, cb) {
5656 }
5757}
5858
59- function decodeP2PKaddress ( encoded_address , cb ) {
60- // first find the descriptor value
61- var uri = base_url + 'getdescriptorinfo?descriptor=' + encodeURIComponent ( 'pkh(' + encoded_address . replace ( ' OP_CHECKSIG' , '' ) + ')' ) ;
62- request ( { uri : uri , json : true } , function ( error , response , body ) {
63- // check if there was an error
64- if ( error == null ) {
65- // decode the address using the output descriptor
66- uri = base_url + 'deriveaddresses?descriptor=' + encodeURIComponent ( body . descriptor ) ;
67- request ( { uri : uri , json : true } , function ( error , response , body ) {
68- // check if there was an error
69- if ( error == null ) {
70- // return decoded address
71- return cb ( body ) ;
59+ function encodeP2PKaddress ( p2pk_descriptor , cb ) {
60+ // find the descriptor value
61+ module . exports . get_descriptorinfo ( p2pk_descriptor , function ( descriptor_info ) {
62+ // check for errors
63+ if ( descriptor_info != null ) {
64+ // encode the address using the output descriptor
65+ module . exports . get_deriveaddresses ( descriptor_info . descriptor , function ( p2pkh_address ) {
66+ // check for errors
67+ if ( p2pkh_address != null ) {
68+ // return P2PKH address
69+ return cb ( p2pkh_address ) ;
7270 } else {
73- // an error occurred
74- console . log ( 'deriveaddresses error: ' + error ) ;
75- // return null
71+ // address could not be encoded
7672 return cb ( null ) ;
7773 }
7874 } ) ;
7975 } else {
80- // an error occurred
81- console . log ( 'getdescriptorinfo error: ' + error ) ;
82- // return null
76+ // address could not be encoded
8377 return cb ( null ) ;
8478 }
8579 } ) ;
@@ -388,7 +382,75 @@ module.exports = {
388382 } ) ;
389383 }
390384 } ,
391-
385+
386+ get_descriptorinfo : function ( descriptor , cb ) {
387+ var cmd_name = 'getdescriptorinfo' ;
388+ // format the descriptor correctly for use in the getdescriptorinfo cmd
389+ descriptor = 'pkh(' + descriptor . replace ( ' OP_CHECKSIG' , '' ) + ')' ;
390+
391+ if ( settings . use_rpc ) {
392+ rpcCommand ( [ { method :cmd_name , parameters : [ descriptor ] } ] , function ( response ) {
393+ // check if there was an error
394+ if ( response != null && response != 'There was an error. Check your console.' ) {
395+ // return the rpc response
396+ return cb ( response ) ;
397+ } else {
398+ // an error occurred
399+ console . log ( cmd_name + ' error: ' + ( response == null ? 'Method not found' : response ) ) ;
400+ // return null
401+ return cb ( null ) ;
402+ }
403+ } ) ;
404+ } else {
405+ var uri = base_url + cmd_name + '?descriptor=' + encodeURIComponent ( descriptor ) ;
406+ request ( { uri : uri , json : true } , function ( error , response , body ) {
407+ // check if there was an error
408+ if ( error == null && ( body . message == null || body . message != 'Method not found' ) ) {
409+ // return the request body
410+ return cb ( body ) ;
411+ } else {
412+ // an error occurred
413+ console . log ( cmd_name + ' error: ' + ( error == null ? body . message : error ) ) ;
414+ // return null
415+ return cb ( null ) ;
416+ }
417+ } ) ;
418+ }
419+ } ,
420+
421+ get_deriveaddresses : function ( descriptor , cb ) {
422+ var cmd_name = 'deriveaddresses' ;
423+
424+ if ( settings . use_rpc ) {
425+ rpcCommand ( [ { method :cmd_name , parameters : [ descriptor ] } ] , function ( response ) {
426+ // check if there was an error
427+ if ( response != null && response != 'There was an error. Check your console.' ) {
428+ // return the rpc response
429+ return cb ( response ) ;
430+ } else {
431+ // an error occurred
432+ console . log ( cmd_name + ' error: ' + ( response == null ? 'Method not found' : response ) ) ;
433+ // return null
434+ return cb ( null ) ;
435+ }
436+ } ) ;
437+ } else {
438+ var uri = base_url + cmd_name + '?descriptor=' + encodeURIComponent ( descriptor ) ;
439+ request ( { uri : uri , json : true } , function ( error , response , body ) {
440+ // check if there was an error
441+ if ( error == null && ( body . message == null || body . message != 'Method not found' ) ) {
442+ // return the request body
443+ return cb ( body ) ;
444+ } else {
445+ // an error occurred
446+ console . log ( cmd_name + ' error: ' + ( error == null ? body . message : error ) ) ;
447+ // return null
448+ return cb ( null ) ;
449+ }
450+ } ) ;
451+ }
452+ } ,
453+
392454 // synchonous loop used to interate through an array,
393455 // avoid use unless absolutely neccessary
394456 syncLoop : function ( iterations , process , exit ) {
@@ -556,19 +618,20 @@ module.exports = {
556618 // check if there are one or more addresses in the vout
557619 if ( address_list == null || address_list . length == 0 ) {
558620 // no addresses defined
559- // try to decode the asm value as P2PK (Pay To Pubkey)
560- decodeP2PKaddress ( vout [ i ] . scriptPubKey . asm , function ( decoded_address ) {
561- // check if the address was decoded properly
562- if ( decoded_address != null ) {
621+ // assume the asm value is a P2PK (Pay To Pubkey) public key that should be encoded as a P2PKH (Pay To Pubkey Hash) address
622+ encodeP2PKaddress ( vout [ i ] . scriptPubKey . asm , function ( p2pkh_address ) {
623+ // check if the address was encoded properly
624+ if ( p2pkh_address != null ) {
563625 // process vout addresses
564- processVoutAddresses ( decoded_address , vout [ i ] . value , arr_vout , function ( vout_array ) {
626+ processVoutAddresses ( p2pkh_address , vout [ i ] . value , arr_vout , function ( vout_array ) {
565627 // save updated array
566628 arr_vout = vout_array ;
567629 // move to next vout
568630 loop . next ( ) ;
569631 } ) ;
570632 } else {
571- // could not decode address, move to next vout
633+ // could not decipher the address, move to next vout
634+ console . log ( 'Failed to find vout address from ' + vout [ i ] . scriptPubKey . asm ) ;
572635 loop . next ( ) ;
573636 }
574637 } ) ;
@@ -629,12 +692,12 @@ module.exports = {
629692 loop . next ( ) ;
630693 } else {
631694 // no addresses defined
632- // try to decode the asm value as P2PK (Pay To Pubkey)
633- decodeP2PKaddress ( tx . vout [ i ] . scriptPubKey . asm , function ( decoded_address ) {
634- // check if the address was decoded properly
635- if ( decoded_address != null ) {
636- // save the decoded address
637- addresses . push ( { hash : decoded_address , amount : tx . vout [ i ] . value } ) ;
695+ // assume the asm value is a P2PK (Pay To Pubkey) public key that should be encoded as a P2PKH (Pay To Pubkey Hash) address
696+ encodeP2PKaddress ( tx . vout [ i ] . scriptPubKey . asm , function ( p2pkh_address ) {
697+ // check if the address was encoded properly
698+ if ( p2pkh_address != null ) {
699+ // save the P2PKH address
700+ addresses . push ( { hash : p2pkh_address , amount : tx . vout [ i ] . value } ) ;
638701 }
639702 loop . break ( true ) ;
640703 loop . next ( ) ;
0 commit comments