0

Reading the ElectrumX documentation, found that the method for getting balance of any public address was: blockchain.address.get_balance, but was removed. Now, I understand that blockchain.scripthash.get_balance is the way for getting balances for any public address, but it requires a "scriphash".

Checking documentation for Script Hashes shows stepts for hashing the address to get script hash.

With this python code, I can get the electrum server data:

import json
from time import sleep

port = 50001
host = '127.0.0.1'

content = {
    "method": "blockchain.scripthash.get_balance",
    "params": ["af6df685465a42b753bf72dfdf36c43ea3c2931025411940609da6f2404b00c4"], # this is a tx_hash
    "id": 0
}

def electrumx(host, port, content):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.sendall(json.dumps(content).encode('utf-8')+b'\n')
    sleep(0.5)
    sock.shutdown(socket.SHUT_WR)
    res = ""
    while True:
        data = sock.recv(1024)
        if (not data):
            break
        res += data.decode()
    print(res)
    sock.close()

electrumx(host, port, content)

With a transaction hash, it throws some results, not exactly the balance of any address involved:

{"id":0,"jsonrpc":"2.0","result":{"confirmed":0,"unconfirmed":0}}

My questions are:

  1. What means "script hash" of a public address?
  2. Is required to use a transaction hash, like in the code?
  3. Why when using tx hash always shows "result":{"confirmed":0,"unconfirmed":0} even if the address has funds.
  4. Can the script hash be obtained just from a public address?

Thank you very much.

0

Well, it's been a long time, but I've been able to find a way by myself.

The python function for getting script hash is this:

from pycoin.symbols.btc import network
import hashlib
def get_script_hash(addr):
    script = network.parse.address(addr).script()
    return hashlib.sha256(script).digest()[::-1].hex()

And the full code to check the balance of a public address from a local electrum server is:

import socket
import json
from time import sleep
from pycoin.symbols.btc import network
import hashlib

port = 50001
host = '127.0.0.1'


def get_script_hash(addr):
    script = network.parse.address(addr).script()
    return hashlib.sha256(script).digest()[::-1].hex()


def electrumx(host, port, content):
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.sendall(json.dumps(content).encode('utf-8') + b'\n')
    sleep(0.5)
    sock.shutdown(socket.SHUT_WR)
    res = ""
    while True:
        data = sock.recv(1024)
        if not data:
            break
        res += data.decode()
    print(res)
    balance = eval(res)['result']
    print(f'{addr} has {balance["confirmed"] / 100000000} confirmed bitcoins')
    print(f'{addr} has {balance["unconfirmed"] / 100000000} unconfirmed bitcoins')
    sock.close()


if __name__ == "__main__":
    addr = "1x8cQ1gKJ5dwBRr1mjsYfsJbWBHJDnxg9"
    script_hash = get_script_hash(addr)
    content = {
        "method": "blockchain.scripthash.get_balance",
        "params": [script_hash],
        "id": 0}
    electrumx(host, port, content)

Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.