Skip to content

Commit

Permalink
P2PK transaction improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoes123 committed Mar 3, 2021
1 parent ca62816 commit 96aaeb0
Showing 1 changed file with 95 additions and 32 deletions.
127 changes: 95 additions & 32 deletions lib/explorer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,24 @@ function processVoutAddresses(address_list, vout_value, arr_vout, cb) {
}
}

function decodeP2PKaddress(encoded_address, cb) {
// first find the descriptor value
var uri = base_url + 'getdescriptorinfo?descriptor=' + encodeURIComponent('pkh(' + encoded_address.replace(' OP_CHECKSIG', '') + ')');
request({uri: uri, json: true}, function (error, response, body) {
// check if there was an error
if (error == null) {
// decode the address using the output descriptor
uri = base_url + 'deriveaddresses?descriptor=' + encodeURIComponent(body.descriptor);
request({uri: uri, json: true}, function (error, response, body) {
// check if there was an error
if (error == null) {
// return decoded address
return cb(body);
function encodeP2PKaddress(p2pk_descriptor, cb) {
// find the descriptor value
module.exports.get_descriptorinfo(p2pk_descriptor, function(descriptor_info) {
// check for errors
if (descriptor_info != null) {
// encode the address using the output descriptor
module.exports.get_deriveaddresses(descriptor_info.descriptor, function(p2pkh_address) {
// check for errors
if (p2pkh_address != null) {
// return P2PKH address
return cb(p2pkh_address);
} else {
// an error occurred
console.log('deriveaddresses error: ' + error);
// return null
// address could not be encoded
return cb(null);
}
});
} else {
// an error occurred
console.log('getdescriptorinfo error: ' + error);
// return null
// address could not be encoded
return cb(null);
}
});
Expand Down Expand Up @@ -388,7 +382,75 @@ module.exports = {
});
}
},


get_descriptorinfo: function(descriptor, cb) {
var cmd_name = 'getdescriptorinfo';
// format the descriptor correctly for use in the getdescriptorinfo cmd
descriptor = 'pkh(' + descriptor.replace(' OP_CHECKSIG', '') + ')';

if (settings.use_rpc) {
rpcCommand([{method:cmd_name, parameters: [descriptor]}], function(response){
// check if there was an error
if (response != null && response != 'There was an error. Check your console.') {
// return the rpc response
return cb(response);
} else {
// an error occurred
console.log(cmd_name + ' error: ' + (response == null ? 'Method not found' : response));
// return null
return cb(null);
}
});
} else {
var uri = base_url + cmd_name + '?descriptor=' + encodeURIComponent(descriptor);
request({uri: uri, json: true}, function (error, response, body) {
// check if there was an error
if (error == null && (body.message == null || body.message != 'Method not found')) {
// return the request body
return cb(body);
} else {
// an error occurred
console.log(cmd_name + ' error: ' + (error == null ? body.message : error));
// return null
return cb(null);
}
});
}
},

get_deriveaddresses: function(descriptor, cb) {
var cmd_name = 'deriveaddresses';

if (settings.use_rpc) {
rpcCommand([{method:cmd_name, parameters: [descriptor]}], function(response){
// check if there was an error
if (response != null && response != 'There was an error. Check your console.') {
// return the rpc response
return cb(response);
} else {
// an error occurred
console.log(cmd_name + ' error: ' + (response == null ? 'Method not found' : response));
// return null
return cb(null);
}
});
} else {
var uri = base_url + cmd_name + '?descriptor=' + encodeURIComponent(descriptor);
request({uri: uri, json: true}, function (error, response, body) {
// check if there was an error
if (error == null && (body.message == null || body.message != 'Method not found')) {
// return the request body
return cb(body);
} else {
// an error occurred
console.log(cmd_name + ' error: ' + (error == null ? body.message : error));
// return null
return cb(null);
}
});
}
},

// synchonous loop used to interate through an array,
// avoid use unless absolutely neccessary
syncLoop: function(iterations, process, exit){
Expand Down Expand Up @@ -556,19 +618,20 @@ module.exports = {
// check if there are one or more addresses in the vout
if (address_list == null || address_list.length == 0) {
// no addresses defined
// try to decode the asm value as P2PK (Pay To Pubkey)
decodeP2PKaddress(vout[i].scriptPubKey.asm, function(decoded_address) {
// check if the address was decoded properly
if (decoded_address != null) {
// assume the asm value is a P2PK (Pay To Pubkey) public key that should be encoded as a P2PKH (Pay To Pubkey Hash) address
encodeP2PKaddress(vout[i].scriptPubKey.asm, function(p2pkh_address) {
// check if the address was encoded properly
if (p2pkh_address != null) {
// process vout addresses
processVoutAddresses(decoded_address, vout[i].value, arr_vout, function(vout_array) {
processVoutAddresses(p2pkh_address, vout[i].value, arr_vout, function(vout_array) {
// save updated array
arr_vout = vout_array;
// move to next vout
loop.next();
});
} else {
// could not decode address, move to next vout
// could not decipher the address, move to next vout
console.log('Failed to find vout address from ' + vout[i].scriptPubKey.asm);
loop.next();
}
});
Expand Down Expand Up @@ -629,12 +692,12 @@ module.exports = {
loop.next();
} else {
// no addresses defined
// try to decode the asm value as P2PK (Pay To Pubkey)
decodeP2PKaddress(tx.vout[i].scriptPubKey.asm, function(decoded_address) {
// check if the address was decoded properly
if (decoded_address != null) {
// save the decoded address
addresses.push({hash: decoded_address, amount: tx.vout[i].value});
// assume the asm value is a P2PK (Pay To Pubkey) public key that should be encoded as a P2PKH (Pay To Pubkey Hash) address
encodeP2PKaddress(tx.vout[i].scriptPubKey.asm, function(p2pkh_address) {
// check if the address was encoded properly
if (p2pkh_address != null) {
// save the P2PKH address
addresses.push({hash: p2pkh_address, amount: tx.vout[i].value});
}
loop.break(true);
loop.next();
Expand Down

0 comments on commit 96aaeb0

Please sign in to comment.