ScriptHash from bitcoin address with BitcoinJS

0

I need to interact with Electrum server API, in order to get UTXOs of a bech32 address.

Electrum doesn't support bech32 but supports searching by ScriptHash, which is be obtainable from an address.

I think that the name ScriptHash is a little ambiguous here (i think locking script hash can make more sense)

So how can i get the scripthash from the address as in the following example using bitcoinjs :

       {'priv': 'L8g5V8kFFeg2WbecahRSdobARbHz2w2STH9S8ePHVSY4fmia7Rsj',
        'pub': '03e9f948421aaa89415dc5f281a61b60dde12aae3181b3a76cd2d849b164fc6d0b',
        'address': 'bc1qqmpt7u5e9hfznljta5gnvhyvfd2kdd0r90hwue',
        'minikey': False,
        'txin_type': 'p2wpkh',
        'compressed': True,
        'addr_encoding': 'bech32',
        'scripthash': '1929acaaef3a208c715228e9f1ca0318e3a6b9394ab53c8d026137f847ecf97b'}

youssef

Posted 2018-01-15T00:58:01.503

Reputation: 136

Answers

1

Got the answer via github

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'))

Note that -for some reason- Electrum uses the reverse scripthash

youssef

Posted 2018-01-15T00:58:01.503

Reputation: 136

can you post the result of the output please?knocte 2019-01-14T07:09:45.030