Author

Topic: Electrumx - get balance of BECH32 addresses (Read 306 times)

legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
April 17, 2021, 01:44:32 AM
#9
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.
newbie
Activity: 3
Merit: 0
Hi ilib,

could you share your full code customization ? how you add bech32 address for electrmx ?
legendary
Activity: 2128
Merit: 1293
There is trouble abrewing
December 01, 2019, 09:48:31 AM
#7
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))))
where "Script()" is the other OP codes you add such as 0x00, 0x76, 0x88,...

eg. P2WPKH:
Code:
SHA256(0x00 | 0x14 | RIPEMD160(SHA256(Pubkey)))

P2PKH:
Code:
SHA256(0x76 | 0xa9 | 0x14 | RIPEMD160(SHA256(Pubkey)) | 0x88 | 0xac)
newbie
Activity: 19
Merit: 12
December 01, 2019, 06:33:34 AM
#6

this should be:
Code:
SHA256(00201ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4)
where 0x00 is OP_0, 0x20 is the size of the data (hash) to push and 1ff... to the end is the 32 byte hash.
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))
the server says that the hash is invalid.
If I exclude RIPEMD160 - just like in your example but the size is replaced to 0x14 - everything works.
for example this address bc1qa24tsgchvuxsaccp8vrnkfd85hrcpafg20kmjw shows the correct balance.

pubkeyhash: eaaab82317670d0ee3013b073b25a7a5c780f528
Code:
hash = SHA256(0014eaaab82317670d0ee3013b073b25a7a5c780f528)) => e9576bdfa6b6ad206e2ff012b4584d8eaed15c49e3b53e461af43a0189bcfbaf

Just wonder what's wrong with ripemd160?

legendary
Activity: 2128
Merit: 1293
There is trouble abrewing
November 30, 2019, 09:11:26 PM
#5
Hash = SHA256(HEX2BIN(Pubkeyhash)) = 4899cf970298cf69b162d33b16db0c917935d11ce5908d63c7fba1893b23b581
this should be:
Code:
SHA256(00201ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4)
where 0x00 is OP_0, 0x20 is the size of the data (hash) to push and 1ff... to the end is the 32 byte hash.
and the result of this hash is
Code:
32afd5ba9b3dd392109682fb4f84ea5b32bc17f9dab5afca9d06b137b6dd8490

Quote
No error message but something is wrong with the hashes I guess
it returns zero while this address for sure has some positive balance
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.
HCP
legendary
Activity: 2086
Merit: 4361
November 30, 2019, 05:47:44 PM
#4
I enquire the balance of P2WSH like this:
Address: bc1qrlam06agczxvm4fzlaumcp8q20gh3xfy28r3qg9xlr5rrc6e48jq4ghzey
Pubkeyhash: 1ffbb7eba8c08ccdd522ff79bc04e053d178992451c71020a6f8e831e359a9e4
Are you hashing the pubkey or the script for your bech32 address?

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>
where hash is the SHA256(script)
...
newbie
Activity: 19
Merit: 12
November 30, 2019, 04:28:41 PM
#3
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>
where hash is the result of RIPEMD160(SHA256(pubkey))

and if your bech32 address is a P2WSH then your script is:
Code:
OP_0 <32 bytes hash>
where hash is the SHA256(script)

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







legendary
Activity: 2128
Merit: 1293
There is trouble abrewing
November 30, 2019, 07:57:38 AM
#2
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>
where hash is the result of RIPEMD160(SHA256(pubkey))

and if your bech32 address is a P2WSH then your script is:
Code:
OP_0 <32 bytes hash>
where hash is the SHA256(script)

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.
newbie
Activity: 19
Merit: 12
November 30, 2019, 05:23:10 AM
#1
Who knows how to enquire electrumx to get balance of Segwit addresses - p2wsh or p2wpkh?

In case of legacy addresses - it is described here

So, how to build 'scripthash' from bech32 address to send this request?
Code:
blockchain.scripthash.get_balance(scripthash)
Jump to: