Bitcoin-Core

從 python 模組訪問 bitcoind 遇到錯誤

  • May 11, 2021

我正在對區塊鏈進行一些試驗,特別是嘗試使用python-OP_RETURN 模組將一些字節放入區塊鏈 op_return 欄位。要使用這個模組,我需要一個正在執行的比特幣核心實例,所以我使用下載的 .tar.gz 安裝了 bitcoind(而不是通過引發一些系統問題 iirc 的 ppa)。無論如何,我執行了 bitcoind 守護程序,並且通過編寫一個 bitcoin.conf 文件,我似乎可以通過使用 prune 參數來避免為此投入 200+GB。我還在 bitcoin.conf 中設置了以下內容:

rpcuser=myuser
rpcpassword=mypassword
testnet=0
rpcport=8332
rpcallowip=127.0.0.1
rpcallowip=1.2.3.4
server=1

現在想嘗試將一些字節放入區塊鏈:

jeremy@instance-1:~$ python3 python-OP_RETURN/store-OP_RETURN.py 'This example stores 47 bytes in the blockchain.'
testnet False  [**this is debug info I printed]
url http://127.0.0.1:8332/ req {'id': '1620597001.3218298-441058', 'method': 'getinfo', 'params': ()}  [**this is also debug info I printed]
Traceback (most recent call last):
 File "python-OP_RETURN/store-OP_RETURN.py", line 47, in <module>
   result=OP_RETURN_store(data, testnet)
 File "/home/jeremy/python-OP_RETURN/OP_RETURN.py", line 116, in OP_RETURN_store
   if not OP_RETURN_bitcoin_check(testnet):
 File "/home/jeremy/python-OP_RETURN/OP_RETURN.py", line 410, in OP_RETURN_bitcoin_check
   info=OP_RETURN_bitcoin_cmd('getinfo', testnet)
 File "/home/jeremy/python-OP_RETURN/OP_RETURN.py", line 477, in OP_RETURN_bitcoin_cmd
   raw_result=urlopen(url, json.dumps(request).encode('utf-8'), OP_RETURN_NET_TIMEOUT).read()
 File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
   return opener.open(url, data, timeout)
 File "/usr/lib/python3.7/urllib/request.py", line 531, in open
   response = meth(req, response)
 File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
   'http', request, response, code, msg, hdrs)
 File "/usr/lib/python3.7/urllib/request.py", line 563, in error
   result = self._call_chain(*args)
 File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
   result = func(*args)
 File "/usr/lib/python3.7/urllib/request.py", line 1034, in http_error_401
   url, req, headers)
 File "/usr/lib/python3.7/urllib/request.py", line 987, in http_error_auth_reqed
   return self.retry_http_basic_auth(host, req, realm)
 File "/usr/lib/python3.7/urllib/request.py", line 997, in retry_http_basic_auth
   return self.parent.open(req, timeout=req.timeout)
 File "/usr/lib/python3.7/urllib/request.py", line 531, in open
   response = meth(req, response)
 File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
   'http', request, response, code, msg, hdrs)
 File "/usr/lib/python3.7/urllib/request.py", line 569, in error
   return self._call_chain(*args)
 File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
   result = func(*args)
 File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default
   raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 404: Not Found

所以我的問題是我將如何避免 404 ,和/或它為什麼會發生。第 477 行的 url 和請求是

<http://127.0.0.1:8332/> 和 {‘id’: ‘1620597001.3218298-441058’, ‘method’: ‘getinfo’, ‘params’: ()} 分別。我應該使用 telnet 來檢查 host:port 是否有響應?

即使我在使用python-OP_RETURN時遇到了奇怪的錯誤,這個儲存庫中的最後一次送出是在 2017 年,所以也許我們需要對程式碼進行一些更改。

如果你想試驗 OP_RETURN,可以使用 Bitcoin Core 來完成。例子:

將字元串轉換為十六進制:https ://codebeautify.org/string-hex-converter

createrawtransaction '[]' '{"data":"616c69616268617474"}'
fundrawtransaction 02000000000100000000000000000b6a09616c6961626861747400000000
signrawtransactionwithwallet 020000000ce2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ad02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5c202000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5c502000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ab02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5fc02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5b902000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ce02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5d402000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5f002000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5db02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b58e01000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5bd02000000feffffff02d0400f00000000001600145449e855d30333134c39495b33626cd186437ee900000000000000000b6a09616c6961626861747400000000
sendrawtransaction 0200000000010ce2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ad02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5c202000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5c502000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ab02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5fc02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5b902000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5ce02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5d402000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5f002000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5db02000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b58e01000000feffffffe2788d8d5fec762bc775e599bd8d39a985dbd456f3fb950702204420ddc6c6b5bd02000000feffffff02d0400f00000000001600145449e855d30333134c39495b33626cd186437ee900000000000000000b6a09616c6961626861747402473044022021c93f175288c463cc259cbfd57cfc06c1f9f2e452db40d3be3e37da8550bbb602207e827c8049fabe1c0938eecf677477ffbb91de161303bfb1943f9e22ca39e471012102485de55fe20ab9f624395a7166fe03e9bfdc45e0fb446c8f6e538efcc460c03a0247304402200d96c5aa0ff214417a17a840d0e3f86a99ffda3db29e46ea9d0c05ee57c7c6be02200d8ecc119b8608d54fbed9329a2dabada1401745a34d990cd6db832a20a435b6012102d14be8536f23ba51e38dc7d161783927c84373b4d5878a67ba265b5315b0d0d4024730440220358e4461c682b69177bb39e1b6ab0ff515d1bc575de76b133ae461aac709ca3902201a74ad58948901fa2139656dc2449c258b12845101ed4f82b6c20bb7b2fb5ef70121027136f163dbbda7ff9cf9078f14e1d469b3de2026d2799f5ccc9c63ccb52ec8c90247304402200adf5fd7264d38f3834b64077c4e59eddbce02ecc8c4dcc083b49681d051c2fd02206f8eb03573eacd4d64eaeeef4abda72f1c8fcf368fd01c63d91fcf739c913d86012103b4377ce01cfc8f3439782a33ac68217db727af4eeb6bf9f34f8af47814d763ff02473044022075c012b72b2714fb8cc9bf1dc77293662a28cede4648f45ae308fc80d9c917f902201e2db0baf2313afe0d424555af1c804960a419324bb50f76192327ce275b006e012102103849cb029ae85ab65a704cc8d5fe25165a6cd5f42e7f2af6c6b465fea899c702473044022078dd7fe6d83d50ad661772ce361d0346415d822f44fb48ad28b59d11158e80f90220332099875fc180e71d48937cc202cd336c031cbb6f3224356cd6a31027561f11012102138fc6473695745c0287d9fb6635d18eb033bcc339a5db3e11599365958ddba1024730440220353586fa57d54c4941346aabe64957c4a229f04d3233815887c353cadca47e5a02204b02a709eb477c5b8fc5e2e11edf98282c0e82a7085807d18ae7363a0396002f012102565aec22cc4f52b9baaea17f8d155b780ff1057c151f53cf15281a0fd5cd4e9502473044022028dd4a470864194af0d40b3c3c6207e1941a370e004edcc5e3ce1ba5746eae4e02200a0f5a30c61710b00cb1e5d9e210fb5095d03a55fff6512a1620bf7210437d96012102cf7c83739aef76ee1e1fe9378b9b8e4f974b00761ef87a636001ee492e3d636302473044022000d180174a0afefa16ace0052153cd4c7724f8bc0978ccfbd899d9343a60dbf802201bb8ba343ebe92480a26aa6be26c045ba790291a4de42e83a8a519b640a39edc012103bbff0e340ee040337ded4f12b9ab6d15380d3521bbfcd81bb7b65c1f672a6b140247304402205e0a52240c5513500b21f476ff04b773dd628e794502b86e1ab7e2ee80f2cfae02200732b2204562472dd4b1a98a8cd9b4fe6a994e18747d16b9c08f86e1dcd2cab501210331bd88d561124fbc4659abd293a26dfe4da4046de95c73d4dccbc5676134a5ca02473044022068ebe1f6293f05c200949a9e10244b63dcce392fc9349daebd3714f371d018cf0220055ec9c8cc47a8386d280d153bbe39c4761ba731ef79622943195f3d14ba8da7012102972a779477874901cde66d4dcf41a93d8b5976766391eedf056ffaafd201574602473044022022809f4ca1d18ec2db0f6d5aeb49e18e64516e2df1d4002dabfea4a478840157022038523b3d98d6de3c3c3f7fabeb81d415046ea3c13e6f75ef510b6eec397a83f7012102ab6642990c6e1810fae0844dfaa8a7d2aa7dda248a3b6f202e2211c092e60e5800000000

操作返回範例-TX

                              操作返回詳細資訊

發送:d3ba03164365d8a644dfc0fa221d367ff083463d67bfc5fcbbdc2643c3623bc5

使用AuthServiceProxy Python JSON-RPC 介面實現相同目的的 Python 程式碼:

from bitcoinrpc.authproxy import AuthServiceProxy

rpc_port = 18444
rpc_user = 'user3'
rpc_password = 'password3'

rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:%s"%(rpc_user,rpc_password,rpc_port))

OP_RETURN_HEX = "aliabhatt".encode("utf-8").hex()
print(OP_RETURN_HEX)

create_tx = rpc_connection.createrawtransaction([],{"data":OP_RETURN_HEX})
print(create_tx)

fund_tx = rpc_connection.fundrawtransaction(create_tx)
print(fund_tx)

sign_tx = rpc_connection.fundrawtransaction(fund_tx['hex'])
print(sign_tx)

send_tx = rpc_connection.fundrawtransaction(sign_tx['hex'])
print(send_tx)

引用自:https://bitcoin.stackexchange.com/questions/106187