You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need to convert the address to scripthash to use electrumx. Is there a library or method for this? Or do I have examples for reference? thank you very much
I tried to use the following method to convert the address into scripthash, but the balance of my address could not be checked in electrumx.
No.. that's not how it works. The scripthash is actually the hash of the locking script that appears on-chain for a paricular address. A locking script for p2pkh has the form: OP_DUP OP_HASH160 <rmd160_pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG. (For segwit addresses or the new bc1 style addresses it has a completely different form.)
You need to take the single sha256 hash of the bytes of that locking script, not of the pubkey hash.
In your case, for p2pkh only, you would need to prepend the bytes: [0x76, 0xa9, 0x14]. Then also append at the end the bytes: [0x88, 0xac] ... prepend/append these to the pubkey hash bytes you obtained. Then take the sha256 of that whole thing.
It's confusing, I know.. hopefully this helps.
If you wanted to have this done for you, look in electrumx/lib/coins.py and other places for code you can import/steal to do this.
I need to convert the address to scripthash to use electrumx. Is there a library or method for this? Or do I have examples for reference? thank you very much
I tried to use the following method to convert the address into scripthash, but the balance of my address could not be checked in electrumx.
import hashlib
import base58
def p2pkh_to_scripthash(address):
decoded = base58.b58decode_check(address)
pubkey_hash = decoded[1:]
sha256_hash = hashlib.sha256(pubkey_hash).digest()
return sha256_hash[::-1].hex()
P2PKH
p2pkh_address = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
scripthash = p2pkh_to_scripthash(p2pkh_address)
print(f"Scripthash for P2PKH address {p2pkh_address}: {scripthash}")
bc1pvks94hj8p6z9mjvxwua8xhsvfkvjfj8vlll3nkv5aq3mmeglgcasm4eyav
1NXJGRGj7FSwHEK4oMy2vkiFV8sVTLgy5F
bc1q8f8nj8yeatul9ryxmn3dw03f5ual74d4rrklul
The text was updated successfully, but these errors were encountered: