Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate pay_to_address_script from address (ElectrumX API integration) #262

Open
zilveer opened this issue Mar 30, 2023 · 0 comments
Open

Comments

@zilveer
Copy link

zilveer commented Mar 30, 2023

Hi,
I want to be able to calculate "pay_to_address_script" from an address with Coinbin.
This is needed with the lately updated ElectrumX according to its docs.

Someone in bitcoinj-js lib #bitcoinjs/bitcoinjs-lib#990 posted a similiar request and had it solved with the coding:

let script = bitcoin.address.toOutputScript(address)
let hash = bitcoin.crypto.sha256(script)
let reversedHash = new Buffer(hash.reverse())

console.log(address, ' maps to ', reversedHash.toString('hex'))

For ElectrumX reference: https://github.com/kyuupichan/electrumx/blob/4dd226f9c090d968b80360d607368f5213ccaa56/lib/coins.py#L165

this is my attempt so far but I am doing something wrong.
here is ElectrumX guide on how to convert address to script hashes .

var address = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';

var pubkeyHashScript = Crypto.util.bytesToHex( i.pubkeyHash(address).buffer);
//should generate: 76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac
console.log('pubkeyHashScript: ', pubkeyHashScript);

var pubkeyHashScriptSHA256 = Crypto.SHA256(pubkeyHashScript);

//should generate: 6191c3b590bfcfa0475e877c302da1e323497acf3b42c08d8fa28e364edf018b
console.log('pubkeyHashScriptSHA256: ', pubkeyHashScriptSHA256);

var pubkeyHashScriptSHA256Reversed = Crypto.util.bytesToHex(Crypto.util.hexToBytes(pubkeyHashScriptSHA256).reverse());
console.log('pubkeyHashScriptSHA256Reversed: ', pubkeyHashScriptSHA256Reversed);

//should generate: 8b01df4e368ea28f8dc0423bcf7a4923e3a12d307c875e47a0cfbf90b5c39161

any help or advice on this issue would make me happy, Zilveer

@zilveer zilveer changed the title Calculate scripthash from address (ElectrumX API integration) Calculate Public Key Hash from address (ElectrumX API integration) Mar 30, 2023
@zilveer zilveer changed the title Calculate Public Key Hash from address (ElectrumX API integration) Calculate pay_to_address_script from address (ElectrumX API integration) Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant