<p>Well, it's been a long time, but I've been able to find a way by myself.</p>
<p>The python function for getting <em>script hash</em> is this:</p>
<pre><code>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()
</code></pre>
<p>And the full code to check the balance of a public address from a local electrum server is:</p>
<pre><code>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 = &quot;&quot;
    while True:
        data = sock.recv(1024)
        if not data:
            break
        res += data.decode()
    print(res)
    balance = eval(res)['result']
    print(f'{addr} has {balance[&quot;confirmed&quot;] / 100000000} confirmed bitcoins')
    print(f'{addr} has {balance[&quot;unconfirmed&quot;] / 100000000} unconfirmed bitcoins')
    sock.close()


if __name__ == &quot;__main__&quot;:
    addr = &quot;1x8cQ1gKJ5dwBRr1mjsYfsJbWBHJDnxg9&quot;
    script_hash = get_script_hash(addr)
    content = {
        &quot;method&quot;: &quot;blockchain.scripthash.get_balance&quot;,
        &quot;params&quot;: [script_hash],
        &quot;id&quot;: 0}
    electrumx(host, port, content)
</code></pre>
<p>Thank you.</p>

CC BY-SA 4.0
2021-03-17T09:09:15.357Z
2021-03-17T09:09:15.357Z
Nand0san
<p>Reading the ElectrumX documentation, found that the method for getting balance of any public address was: <code>blockchain.address.get_balance</code>, but was removed. Now, I understand that <code>blockchain.scripthash.get_balance</code> is the way for getting balances for any public address, but it requires a &quot;scriphash&quot;.</p>
<p>Checking <a href="https://electrumx.readthedocs.io/en/latest/protocol-basics.html#script-hashes" rel="nofollow noreferrer">documentation for Script Hashes</a> shows stepts for hashing the address to get script hash.</p>
<p>With this python code, I can get the electrum server data:</p>
<pre><code>import json
from time import sleep

port = 50001
host = '127.0.0.1'

content = {
    &quot;method&quot;: &quot;blockchain.scripthash.get_balance&quot;,
    &quot;params&quot;: [&quot;af6df685465a42b753bf72dfdf36c43ea3c2931025411940609da6f2404b00c4&quot;], # this is a tx_hash
    &quot;id&quot;: 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 = &quot;&quot;
    while True:
        data = sock.recv(1024)
        if (not data):
            break
        res += data.decode()
    print(res)
    sock.close()

electrumx(host, port, content)
</code></pre>
<p>With a transaction hash, it throws some results, not exactly the balance of any address involved:</p>
<pre><code>{&quot;id&quot;:0,&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;result&quot;:{&quot;confirmed&quot;:0,&quot;unconfirmed&quot;:0}}
</code></pre>
<p>My questions are:</p>
<ol>
<li>What means &quot;<strong>script hash</strong>&quot; of a public address?</li>
<li>Is required to use a transaction hash, like in the code?</li>
<li>Why when using tx hash always shows <code>&quot;result&quot;:{&quot;confirmed&quot;:0,&quot;unconfirmed&quot;:0}</code>
even if the address has funds.</li>
<li>Can the script hash be obtained just from a public address?</li>
</ol>
<p>Thank you very much.</p>

<electrum><script>
ElectrumX method blockchain.address.get_balance removed, how to get the script hash of a public addres and get balance?
Nand0san
# ElectrumX method blockchain.address.get_balance removed, how to get the script hash of a public addres and get balance?

(Asked by: username_0 on 2021-03-04)

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](https://electrumx.readthedocs.io/en/latest/protocol-basics.html#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.

**Tags:** electrum, script


---

## 1 answers

### Answer by username_0 on 2021-03-17. Score: 2 | Accepted answer

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.


---

