Title: Electrumx - get balance of BECH32 addresses Post by: ilib on November 30, 2019, 10:23:10 AM Who knows how to enquire electrumx to get balance of Segwit addresses - p2wsh or p2wpkh?
In case of legacy addresses - it is described here (https://electrumx.readthedocs.io/en/latest/protocol-basics.html) So, how to build 'scripthash' from bech32 address to send this request? Code: blockchain.scripthash.get_balance(scripthash) Title: Re: Electrumx - get balance of BECH32 addresses Post by: BrewMaster on November 30, 2019, 12:57:38 PM it is the same exact process with only one difference the "script" part. you have to build a P2W{X}H script where {X} is pubkey or script.
if your bech32 address is a P2WPKH equivalent address then your script is going to be this: Code: OP_0 <20 bytes hash> and if your bech32 address is a P2WSH then your script is: Code: OP_0 <32 bytes hash> now that you have your script, hash it using SHA256 just like the example in docs, reverse it and send that to server. i believe this should work. Title: Re: Electrumx - get balance of BECH32 addresses Post by: ilib on November 30, 2019, 09:28:41 PM it is the same exact process with only one difference the "script" part. you have to build a P2W{X}H script where {X} is pubkey or script. if your bech32 address is a P2WPKH equivalent address then your script is going to be this: Code: OP_0 <20 bytes hash> and if your bech32 address is a P2WSH then your script is: Code: OP_0 <32 bytes hash> now that you have your script, hash it using SHA256 just like the example in docs, reverse it and send that to server. i believe this should work. I enquire the balance of P2WSH like this: Address: bc1qrlam06agczxvm4fzlaumcp8q20gh3xfy28r3qg9xlr5rrc6e48jq4ghzey Pubkeyhash: 1ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4 Hash = SHA256(HEX2BIN(Pubkeyhash)) = 4899cf970298cf69b162d33b16db0c917935d11ce5908d63c7fba1893b23b581 Then I flip it to reverse its endianness: 81b5233b89a1fbc7638d90e51cd13579910cdb163bd362b169cf980297cf9948 Then I send it to Electrumx as an argument of blockchain.scripthash.get_balance and it returns zero while this address for sure has some positive balance No error message but something is wrong with the hashes I guess Title: Re: Electrumx - get balance of BECH32 addresses Post by: HCP on November 30, 2019, 10:47:44 PM I enquire the balance of P2WSH like this: Are you hashing the pubkey or the script for your bech32 address?Address: bc1qrlam06agczxvm4fzlaumcp8q20gh3xfy28r3qg9xlr5rrc6e48jq4ghzey Pubkeyhash: 1ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4 As BrewMaster said... For P2WSH, you need to be using the script... NOT the pubkey ... and if your bech32 address is a P2WSH then your script is: Code: OP_0 <32 bytes hash> ... Title: Re: Electrumx - get balance of BECH32 addresses Post by: BrewMaster on December 01, 2019, 02:11:26 AM Hash = SHA256(HEX2BIN(Pubkeyhash)) = 4899cf970298cf69b162d33b16db0c917935d11ce5908d63c7fba1893b23b581 this should be:Code: SHA256(00201ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4) and the result of this hash is Code: 32afd5ba9b3dd392109682fb4f84ea5b32bc17f9dab5afca9d06b137b6dd8490 Quote No error message but something is wrong with the hashes I guess there can't be any error messages because the server receives a hash which corresponds to an entirely different address which is not in its database so it simply returns zero balance.it returns zero while this address for sure has some positive balance Title: Re: Electrumx - get balance of BECH32 addresses Post by: ilib on December 01, 2019, 11:33:34 AM this should be: Code: SHA256(00201ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4) and the result of this hash is Code: 32afd5ba9b3dd392109682fb4f84ea5b32bc17f9dab5afca9d06b137b6dd8490 Thanks a lot! It works! But as for P2WPKH - if I use Code: RIPEMD160(SHA256(0x00 + 0x14 + Pubkeyhash)) If I exclude RIPEMD160 - just like in your example but the size is replaced to 0x14 - everything works. for example this address bc1qa24tsgchvuxsaccp8vrnkfd85hrcpafg20kmjw (https://www.blockchain.com/btc/address/bc1qa24tsgchvuxsaccp8vrnkfd85hrcpafg20kmjw) shows the correct balance. pubkeyhash: eaaab82317670d0ee3013b073b25a7a5c780f528 Code: hash = SHA256(0014eaaab82317670d0ee3013b073b25a7a5c780f528)) => e9576bdfa6b6ad206e2ff012b4584d8eaed15c49e3b53e461af43a0189bcfbaf Just wonder what's wrong with ripemd160? Title: Re: Electrumx - get balance of BECH32 addresses Post by: BrewMaster on December 01, 2019, 02:48:31 PM Just wonder what's wrong with ripemd160? there is nothing wrong with it, it has no place here. you only perform it on "public keys" after you performed SHA256 only when you want to get the "pubkey-hash" that you use inside the scripts. then you hash the entire script using SHA256 so the complete thing looks like this: Code: SHA256(Script(RIPEMD160(SHA256(Pubkey)))) eg. P2WPKH: Code: SHA256(0x00 | 0x14 | RIPEMD160(SHA256(Pubkey))) P2PKH: Code: SHA256(0x76 | 0xa9 | 0x14 | RIPEMD160(SHA256(Pubkey)) | 0x88 | 0xac) Title: Re: Electrumx - get balance of BECH32 addresses Post by: cnodom on April 17, 2021, 05:01:18 AM Hi ilib,
could you share your full code customization ? how you add bech32 address for electrmx ? Title: Re: Electrumx - get balance of BECH32 addresses Post by: NotATether on April 17, 2021, 06:44:32 AM Hi ilib, could you share your full code customization ? how you add bech32 address for electrmx ? ElectrumX code was not modified. As mentioned in the post directly above yours, you must send ElectrumX a SHA256 hash of the P2WPKH script, so it will look like this: Code: SHA256(0x00 | 0x14 | RIPEMD160(SHA256(Pubkey))) This will let you get the balance of bech32 addresses as requested in the OP. It should work for any other API call that reads a script. |