Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: SacWheeler on August 09, 2021, 09:57:26 PM



Title: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: SacWheeler on August 09, 2021, 09:57:26 PM
Hi

I am working on a python script using python-bitcoinrpc (https://github.com/jgarzik/python-bitcoinrpc).
Cofiguration is working and rpc.commands get executed.
But I am running into an error with "createrawtransaction".

I try
Code:
createrawtransaction(inputs, outputs, 0, True)
Error: "-1: JSON value is not a string as expected"

for the inputs and outputs I've set correspontig variables as lists, containing a dictionary/dictionaries with the relevant key:value pairs.
inputs: [
    {
         'tixid': 'c1636efe3d85e3bb7e58663cfa976590ed1a9bb6740e7b851cbb34ab63f52ab9',
         'vout': 2,
         'sequence': 4294967293
    }
]

outputs: [
    {
        'bc1q5q5e95h3z0eymfwasm2whx2jrztqa8sy012345': 0.012345
    },
    {
        'bc1q2gphxxpm7aetec0m7jpd04e342c5hree543210': 0.054321
    }
]


I tried to set all values as str() instead of float() or int(). Stille get the error "-1: JSON value is not a string as expected"...

I tried
Code:
createrawtransaction(json.dumps(inputs), json.dumps(outputs), 0, True)
Error: "expected type array, got string"
it seams like "createrawtransaction" does not accept the string value produced by json.dumps()....


As far as I can tell, my json array for the inputs and outputs are formated as described by https://bitcoincore.org/en/doc/0.21.0/rpc/rawtransactions/createrawtransaction/ (https://bitcoincore.org/en/doc/0.21.0/rpc/rawtransactions/createrawtransaction/)

What am I missing??
It almost looks like the python strings in my lists do not appear as strings to the bitcoin-cli.



I am running BitcoinCore 0.21.1, MacOs 11.4, python 3.9.4


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: NotATether on August 10, 2021, 02:10:46 AM
Have you wrapped the entire JSON-RPC call around double quotes? It's not enough to just convert them to strings as there are no quotes transmitted to the Bitcoin Core server.

The JSON-RPC server is kinda dumb and chokes on keys and other strings if you don't wrap them with quotes.


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: SacWheeler on August 10, 2021, 10:44:00 AM
what do you mean by rapping the entire JSON-RPC call around double quotes?

do you mean to represent the strings inside the JSON array with double quotes (instead of single quotes)?
like:
inputs = [
    {
         "tixid": "c1636efe3d85e3bb7e58663cfa976590ed1a9bb6740e7b851cbb34ab63f52ab9",
         "vout": 2,
         "sequence": 4294967293
    }
]


if so, this was my guess too.
But I don't know how to do that in python. because python uses single quotes in lists with dictionaries for strings...
as far as I understand , python does not care if you use double or single quotes for strings. But I assume that the JSON-RPC does care indeed...


so the question would be how to create a valid JSON array (with double quotes) in python for the JSON-RPC..?

Any suggestions?


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: NotATether on August 10, 2021, 10:49:58 AM
do you mean to represent the strings inside the JSON array with double quotes (instead of single quotes)?

Yeah, pretty much.

But I don't know how to do that in python. because python uses single quotes in lists with dictionaries for strings...
as far as I understand , python does not care if you use double or single quotes for strings. But I assume that the JSON-RPC does care indeed...


so the question would be how to create a valid JSON array (with double quotes) in python for the JSON-RPC..?

An old trick up my sleeve - run the dictionary through json.dumps() twice. i.e. json.dumps(json.dumps(dict)).

The outer call will take care of placing the double quotes around the entire string, the inner call will place them around all strings.


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: SacWheeler on August 10, 2021, 11:17:49 AM
well the thing with json.dumps() is, that it converts the mylist or JSON array respectively into a string.
And the rpc does not accepts a string as JSON array.

Error: Expected type array, got string


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: SacWheeler on August 10, 2021, 11:31:33 AM
based on the comment:
Quote
It's not enough to just convert them to strings as there are no quotes transmitted to the Bitcoin Core server.

I tried to add double quotes to the string like this: 
Code:
'"{}"'.format('txid'): '"{}"'.format('[i]mytxid[/i]')
inputs = [
    {
         '"tixid"': '"c1636efe3d85e3bb7e58663cfa976590ed1a9bb6740e7b851cbb34ab63f52ab9"',
         '"vout"': 2,
         '"sequence"': 4294967293
    }
]

but still an error:  JSON value is not a string as expected.

I guess I have to "force" python somehow to use double quotes in stade of single quotes for the strings inside the list...
but how... ???


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: NotATether on August 12, 2021, 05:30:33 AM
I checked and the createrawtransaction function is not in the bitcoinrpc codebase and appears to be part of your script, so it's quite hard to identify the problem if you don't share the code for it, or even better, the whole file.


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: nc50lc on August 12, 2021, 08:46:11 AM
I don't know your code but try to see what went wrong by checking the difference between yours and this simple (working) python-bitcoinrpc script.
for Regtest:
Code:
from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
rpc_connection = AuthServiceProxy("http://%s:%s@127.0.0.1:18443"%("my_username", "my_password"),timeout = 120)

# test
inputs = [
  {
     "txid":"8b2225245836b007a44bd10baa7245ff1f0317f1957c04ef40ff5a3de8fdfe89",
     "vout": 0
   }
]

outputs = [
  {
    "mjtMTytBUHUFgAi3Qqpd4esjQHutdTdJBr": 0.0123
  },
  {
    "bcrt1qrfp5ulc4tzsguqe3utdxyqgv6nappe85nggutz": 0.0987
  }
]

txn = rpc_connection.createrawtransaction (inputs, outputs, 0, True)
print(txn)


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: SacWheeler on August 13, 2021, 04:40:27 PM
I could narrow it down and found the fault in my code.
For inputs, I got a typing error for the key value. I used tixid instead of txid...
sorry guys, I feel kind of dumb... ::) but thanks a lot for bearing with me!!

script is working now. I appreciate the support! You guys are awesome!


Title: Re: getting JSON errors with createrawtransaction using python and python-bitcoinrpc
Post by: nc50lc on August 14, 2021, 04:46:59 AM
I could narrow it down and found the fault in my code.
For inputs, I got a typing error for the key value. I used tixid instead of txid...
sorry guys, I feel kind of dumb... ::) but thanks a lot for bearing with me!!
No worries.
I've noticed that it's written exactly the same in your other thread, I thought it was just a typo when typing that reply.
That's why I didn't mentioned it.