ElectrumX 服务器 jsonRPC 认证

ElectrumX server jsonRPC authentication

无法连接到 electrum 服务器

错误: HTTPConnectionPool(host='electrum.eff.ro', port=50002): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [WinError 10061] 无法建立连接,因为目标机器主动拒绝'))

连接在 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()

经常显示这样的错误。

更新:

服务器: url = "http://fortress.qtornado.com:443"

错误:

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

如果连接 HTTPS:

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

错误:

HTTPSConnectionPool(host='fortress.qtornado.com', port=443): 最大重试次数超过 url: / (由 SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED]证书验证失败:自签名证书 (_ssl.c:1051)')))

如何使用ssl证书连接?

问题解决方案示例: https://github.com/cluelessperson/grappler

比特币测试网

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'))