Bitcoin Forum
May 04, 2024, 06:37:31 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Electrumx - get balance of BECH32 addresses  (Read 280 times)
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
November 30, 2019, 10:23:10 AM
Last edit: November 30, 2019, 10:40:34 AM by ilib
Merited by OmegaStarScream (2), BrewMaster (2)
 #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)
1714804651
Hero Member
*
Offline Offline

Posts: 1714804651

View Profile Personal Message (Offline)

Ignore
1714804651
Reply with quote  #2

1714804651
Report to moderator
1714804651
Hero Member
*
Offline Offline

Posts: 1714804651

View Profile Personal Message (Offline)

Ignore
1714804651
Reply with quote  #2

1714804651
Report to moderator
In order to achieve higher forum ranks, you need both activity points and merit points.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714804651
Hero Member
*
Offline Offline

Posts: 1714804651

View Profile Personal Message (Offline)

Ignore
1714804651
Reply with quote  #2

1714804651
Report to moderator
BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
November 30, 2019, 12:57:38 PM
 #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.

There is a FOMO brewing...
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
November 30, 2019, 09: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







HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
November 30, 2019, 10: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)
...

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
December 01, 2019, 02:11:26 AM
Merited by HCP (2), Heisenberg_Hunter (1)
 #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.

There is a FOMO brewing...
ilib (OP)
Newbie
*
Offline Offline

Activity: 19
Merit: 12


View Profile
December 01, 2019, 11: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?

BrewMaster
Legendary
*
Offline Offline

Activity: 2114
Merit: 1292


There is trouble abrewing


View Profile
December 01, 2019, 02:48:31 PM
Merited by HCP (2), Heisenberg_Hunter (1)
 #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)

There is a FOMO brewing...
cnodom
Newbie
*
Offline Offline

Activity: 3
Merit: 0


View Profile
April 17, 2021, 05:01:18 AM
 #8

Hi ilib,

could you share your full code customization ? how you add bech32 address for electrmx ?
NotATether
Legendary
*
Online Online

Activity: 1596
Merit: 6727


bitcoincleanup.com / bitmixlist.org


View Profile WWW
April 17, 2021, 06: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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!