Search code examples
pythonserverbitcoinjson-rpc

ElectrumX server jsonRPC authentication


Unable to connect to electrum server

Error: HTTPConnectionPool(host='electrum.eff.ro', port=50002): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

Connection is done in python:

electrum wallet jsonRPC authentication

import requests
import json


def main():
    url = "http://electrum.eff.ro:50002"
    payload = json.dumps(
        {
            "id": 0,
            "method": "server.version",
            "params": ["1.9.5", "0.6"]
        }
    )
    headers = {'content-type': "application/json", 'cache-control': "no-cache"}

    try:
        response = requests.request("POST", url, data=payload, headers=headers, auth=(rpc_user, rpc_password))
        return json.loads(response.text)
    except requests.exceptions.RequestException as e:
        print(e)
    except:
        print('No response from Wallet, check Bitcoin is running on this machine')


rpc_user = 'foo'
rpc_password = 'bar'

if __name__ == "__main__":
    answer = main()

Such an error is constantly displayed.

UPD:

Server: url = "http://fortress.qtornado.com:443"

error:

('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

if connect HTTPS:

url = "https://fortress.qtornado.com:443"

error:

HTTPSConnectionPool(host='fortress.qtornado.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1051)')))

How to connect using ssl certificate?


Solution

  • An example of a solution to a problem: https://github.com/cluelessperson/grappler

    Bitcoin Testnet

    import grappler
    from base58 import b58decode_check
    from binascii import hexlify
    from hashlib import sha256
    import codecs
    
    
    a = grappler.ElectrumXConnector(
        # host="fortress.qtornado.com", # bitcoin mainnet
        host='tn.not.fyi',
        port=55002,
        # port=443,
        ssl=True,
        timeout=5
    )
    
    
    OP_DUP = b'76'
    OP_HASH160 = b'a9'
    BYTES_TO_PUSH = b'14'
    OP_EQUALVERIFY = b'88'
    OP_CHECKSIG = b'ac'
    DATA_TO_PUSH = lambda address: hexlify(b58decode_check(address)[1:])
    sig_script_raw = lambda address: b''.join((OP_DUP, OP_HASH160, BYTES_TO_PUSH, DATA_TO_PUSH(address), OP_EQUALVERIFY, OP_CHECKSIG))
    script_hash = lambda address: sha256(codecs.decode(sig_script_raw(address), 'hex_codec')).digest()[::-1].hex()
    
    
    a.send("server.version")
    a.send("server.banner")
    a.send('blockchain.scripthash.get_balance', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))
    a.send('blockchain.scripthash.get_mempool', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))
    a.send('blockchain.scripthash.subscribe', script_hash('mksHkTDsauAP1L79rLZUQA3u36J3ntLtJx'))