Closed youssefgh closed 6 years ago
Sorry to bother you with opening an issue here as a question
Not a problem! I don't have time to peruse the stackexchange for related Q&A; your issue here is useful to others, so don't hesitate to open questions.
How can i get the scripthash from the address ?
I think electrum
uses a similar indexing scheme to https://github.com/bitcoinjs/indexd, that is, SHA256(script)
.
If so, then the following should be what you need:
let script = bitcoin.address.toOutputScript(address)
let hash = bitcoin.crypto.sha256(script)
console.log(address, ' maps to ', hash.toString('hex'))
Thanks a lot Your example worked with just a small modification:
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'))
No idea why Electrum uses the reverse of the hash and not the hash itself Cheers
endianness and Bitcoin ftw
And how can you do that in reverse order? I mean by converting from scripthash to public address?
Assuming we got scripthash from this
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'))
I tried following script but somehow it doesnt work:
const reversedHash = Buffer.from(scripthash, "hex"); //scripthash come from Electrum
const hash = reversedHash.reverse();
const address = bitcoin.address.fromOutputScript(hash);
console.log(address)
Did I something wrong?
@YanDevDe bitcoin.address.fromOutputScript
requires an output script (actually scriptPubKey), you are passing it the hash of the output script.
How can i get the scripthash from the address ? as in the following example :
Sorry to bother you with opening an issue here as a question The original question in stackexchange : ScriptHash from bitcoin address with BitcoinJS