Bitcoin Forum
April 27, 2024, 03:46:29 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: getting JSON errors with createrawtransaction using python and python-bitcoinrpc  (Read 133 times)
SacWheeler (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 16


View Profile
August 09, 2021, 09:57:26 PM
Merited by ABCbits (2), NotATether (2)
 #1

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
1714189589
Hero Member
*
Offline Offline

Posts: 1714189589

View Profile Personal Message (Offline)

Ignore
1714189589
Reply with quote  #2

1714189589
Report to moderator
1714189589
Hero Member
*
Offline Offline

Posts: 1714189589

View Profile Personal Message (Offline)

Ignore
1714189589
Reply with quote  #2

1714189589
Report to moderator
"There should not be any signed int. If you've found a signed int somewhere, please tell me (within the next 25 years please) and I'll change it to unsigned int." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714189589
Hero Member
*
Offline Offline

Posts: 1714189589

View Profile Personal Message (Offline)

Ignore
1714189589
Reply with quote  #2

1714189589
Report to moderator
1714189589
Hero Member
*
Offline Offline

Posts: 1714189589

View Profile Personal Message (Offline)

Ignore
1714189589
Reply with quote  #2

1714189589
Report to moderator
NotATether
Legendary
*
Offline Offline

Activity: 1582
Merit: 6688


bitcoincleanup.com / bitmixlist.org


View Profile WWW
August 10, 2021, 02:10:46 AM
 #2

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
SacWheeler (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 16


View Profile
August 10, 2021, 10:44:00 AM
 #3

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?
NotATether
Legendary
*
Offline Offline

Activity: 1582
Merit: 6688


bitcoincleanup.com / bitmixlist.org


View Profile WWW
August 10, 2021, 10:49:58 AM
 #4

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
SacWheeler (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 16


View Profile
August 10, 2021, 11:17:49 AM
 #5

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
SacWheeler (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 16


View Profile
August 10, 2021, 11:31:33 AM
 #6

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
NotATether
Legendary
*
Offline Offline

Activity: 1582
Merit: 6688


bitcoincleanup.com / bitmixlist.org


View Profile WWW
August 12, 2021, 05:30:33 AM
 #7

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
nc50lc
Legendary
*
Online Online

Activity: 2394
Merit: 5538


Self-proclaimed Genius


View Profile
August 12, 2021, 08:46:11 AM
Merited by SacWheeler (2)
 #8

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)

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
SacWheeler (OP)
Newbie
*
Offline Offline

Activity: 13
Merit: 16


View Profile
August 13, 2021, 04:40:27 PM
 #9

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!
nc50lc
Legendary
*
Online Online

Activity: 2394
Merit: 5538


Self-proclaimed Genius


View Profile
August 14, 2021, 04:46:59 AM
 #10

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.

.
.HUGE.
▄██████████▄▄
▄█████████████████▄
▄█████████████████████▄
▄███████████████████████▄
▄█████████████████████████▄
███████▌██▌▐██▐██▐████▄███
████▐██▐████▌██▌██▌██▌██
█████▀███▀███▀▐██▐██▐█████

▀█████████████████████████▀

▀███████████████████████▀

▀█████████████████████▀

▀█████████████████▀

▀██████████▀▀
█▀▀▀▀











█▄▄▄▄
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
.
CASINSPORTSBOOK
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀▀█











▄▄▄▄█
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!