0

I am experimenting with the blockchain a bit, specifically trying to use the python-OP_RETURN module to put a few bytes into the blockchain op_return field. To use this module I blv I need a running bitcoin core instance so I installed bitcoind using a downloaded .tar.gz (and not thru the ppa which raised some system issues iirc). Anyway, I have the bitcoind daemon running and by writing a bitcoin.conf file it seems I can avoid devoting 200+GB to this by using the prune paramter. I also set the following in bitcoin.conf:

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

and now wanted to try putting some bytes into the blockchain :

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

So my question is how I would avoid the 404 , and/or why its happening. The url and request from line 477 are

http://127.0.0.1:8332/ and {'id': '1620597001.3218298-441058', 'method': 'getinfo', 'params': ()} respectively. Should I use telnet maybe to check if the host:port is responding?

1

Even I am getting weird errors using python-OP_RETURN, last commit in this repository was in 2017 so maybe we need to make few changes in the code.

If you want to experiment with OP_RETURN, it can be done using Bitcoin Core. Example:

Convert string to hex: https://codebeautify.org/string-hex-converter

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

op-return-example-tx

                              op-return-details

Tx: d3ba03164365d8a644dfc0fa221d367ff083463d67bfc5fcbbdc2643c3623bc5

Python code to achieve the same thing using AuthServiceProxy Python JSON-RPC interface:

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)
8
  • ok thanks - it does seem to be old sw no longer in sync with bitcoin core, i'll see if i can patch it up May 10 at 16:48
  • If you want to use python for creating OP_RETURN transaction, I can update the answer with a different approach.
    – Prayank
    May 11 at 0:09
  • Yes, that's what I'm after. May 11 at 6:53
  • @jeremy_rutman I have added python code in the answer
    – Prayank
    May 11 at 11:20
  • I did pip install bitcoinrpc and wind up with a module version 0.4.0 that doesnt seem to have an authproxy method like you use. My bitconrpc has bitcoin_rpc and BitcoinRPC objects, the first of which has methods like BestBlockHash and the latter of which has methods like getblock May 11 at 11:36

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.