Author

Topic: getting JSON errors with createrawtransaction using python and python-bitcoinrpc (Read 132 times)

legendary
Activity: 2338
Merit: 5297
Self-proclaimed Genius
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... Roll Eyes 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.
newbie
Activity: 13
Merit: 16
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... Roll Eyes but thanks a lot for bearing with me!!

script is working now. I appreciate the support! You guys are awesome!
legendary
Activity: 2338
Merit: 5297
Self-proclaimed Genius
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:%[email protected]: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)
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
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.
newbie
Activity: 13
Merit: 16
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... Huh
newbie
Activity: 13
Merit: 16
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
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
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.
newbie
Activity: 13
Merit: 16
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?
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
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.
newbie
Activity: 13
Merit: 16
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/

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
Jump to: