Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: DonnyBoy on November 24, 2019, 01:44:36 PM



Title: Trying to create a raw transaction - noob troubles
Post by: DonnyBoy on November 24, 2019, 01:44:36 PM
Hi, I've been working on improving my computer skills and thought bitcoin would be a good avenue to learn.

After a long battle, I finally managed to achieve synchronization of my node and have been trying some commands. I tried sending the simplest transaction I could, and much to my amazement, it worked:

bitcoin-cli createrawtransaction "[{\"txid\":\"5894211487d94fs5484a0f64cb56edf8a4fea5a82s1848782e7d55b45cd12472\",\"vout\":0}]" "[{\"1KNt61bXLPTvs5PhL3m4fp71ndqJWBSQ14\":0.0002}]"

The transaction above was easy because it was a single input and required no change. However, I am trying to spice things up first by including a transaction with change, and then one involving multiple inputs. However, I've run into some minor issues when I tried to make things more interesting.

However when I tried to add a change address and value into a similar transaction.

bitcoin-cli createrawtransaction "[{\"txid\":\"37d53f65g0568e3fd1e4auu7f30b4i0758e69o257f649de62b352931e402g7654\",\"vout\":0}]" "[{\"1EmGo9SAoYpoXmYH3Hm4LX8mMsyDxyDCkH \":0.0003, \"3L4kHNBgRPf5u875UhTd6N57sw4gc7vLa \":0.00005}]"

This returned an error:

error code: -8
error message:
Invalid parameter, key-value pair must contain exactly one key


Not sure where I am going wrong because it doesn't seem like a substantial leap from the first transaction to the second...? If anyone can see where I might be going wrong here I would appreciate the input! Also can anyone recommend where I might find some examples of some raw transactions? Doesn't seem to be too many around that I can find :-/

Thanks in advance everyone. I appreciate the community's help as always!


Title: Re: Trying to create a raw transaction - noob troubles
Post by: Coding Enthusiast on November 24, 2019, 02:17:54 PM
It seems like a JSON parsing problem to me. You are giving the interpreter a JSON array (an array of outputs) so it should look like:
Code:
[
    {"FooKey": FooValue},
    {"BarKey": BarValue}
]
where keys are addresses and values are decimal amounts.


Title: Re: Trying to create a raw transaction - noob troubles
Post by: Carlton Banks on November 24, 2019, 02:18:11 PM
"[{\"1EmGo9SAoYpoXmYH3Hm4LX8mMsyDxyDCkH \":0.0003,
there's a space character here                          ^

\":0.0003, \"3L4kHNBgRPf5u875UhTd6N57sw4gc7vLa \":0.00005}]"
and here ^

\"3L4kHNBgRPf5u875UhTd6N57sw4gc7vLa \":0.00005}]"
and here                                                   ^

"[{\"txid\":\"37d53f65g0568e3fd1e4auu7f30b4i0758e69o257f649de62b352931e402g7654\",\"vout\":0}]" "[{\"1EmGo9SAoYpoXmYH3Hm4LX8mMsyDxyDCkH \":0.0003,
not to mention                                                                                                                   here ^  :)

try removing those spaces, see whether the errors can be exorcised.

or try putting single quotes '  ' around all of the JSON data.



my true advice would be: stop doing it like this

the actual direct use of the CLI combined with the JSON data is not a user-friendly way to use the bitcoin-cli tool. If you prepare some files containing the JSON data, the CLI (probably bash in this case) will not directly interpret the space characters, and you can happily leave them in where they're needed. You can also make the JSON slightly more readable, as the \ escape isn't necessary for the " character.

so you'd do bitcoin-cli createrawtransaction $(cat json.data) for your command.

and json.data would look like
Code:
[{"txid":"37d53f65g0568e3fd1e4auu7f30b4i0758e69o257f649de62b352931e402g7654","vout":0}] [{"1EmGo9SAoYpoXmYH3Hm4LX8mMsyDxyDCkH":0.0003,"3L4kHNBgRPf5u875UhTd6N57sw4gc7vLa":0.00005}]


Title: Re: Trying to create a raw transaction - noob troubles
Post by: aa7356 on November 24, 2019, 02:54:27 PM
I do some kind of volunteer work at the Computer Science Museum, as a computer science student I think software as a computational abstraction representation of a real machinery state in a given moment.

Turns out that this post reminds me of "punch cards" ( Hollerith cards ) so I decided to find out when the "space/blank" character was mentioned in the ISO C standard ...
googling on that I end up at stackoverflow comment on '/0'

Quote
' '      space
'\t'     horizontal tab
'\n'     newline
'\v'     vertical tab
'\f'     feed
'\r'     carriage return   

Quote
Use isspace standard library function from ctype.h if you want to check for any of these white-spaces.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/ctype.h.html

I'm still curious


Title: Re: Trying to create a raw transaction - noob troubles
Post by: HCP on November 27, 2019, 03:47:55 AM
However when I tried to add a change address and value into a similar transaction.

bitcoin-cli createrawtransaction "[{\"txid\":\"37d53f65g0568e3fd1e4auu7f30b4i0758e69o257f649de62b352931e402g7654\",\"vout\":0}]" "[{\"1EmGo9SAoYpoXmYH3Hm4LX8mMsyDxyDCkH \":0.0003, \"3L4kHNBgRPf5u875UhTd6N57sw4gc7vLa \":0.00005}]"

This returned an error:

error code: -8
error message:
Invalid parameter, key-value pair must contain exactly one key
You seem to have the exact same issue as this: https://bitcoin.stackexchange.com/questions/80905/bitcoin-cli-createrawtransaction-with-3-outputs-example


Also can anyone recommend where I might find some examples of some raw transactions? Doesn't seem to be too many around that I can find :-/
Coincidentally... I found that above StackExchange question while searching for: "bitcoin cli examples" :P


Title: Re: Trying to create a raw transaction - noob troubles
Post by: pooya87 on November 27, 2019, 04:38:40 AM
Also can anyone recommend where I might find some examples of some raw transactions? Doesn't seem to be too many around that I can find :-/

you can look at the blockchain itself! there are millions of transactions from genesis block up to the current height. for that you could use a block explorer that lets you see the raw transaction hex, usually they all do but blockcypher may be the most convenient as it has a clickable button on each tx called "API call" that shows you  both the hex and deserialized format.
example:
https://live.blockcypher.com/btc-testnet/tx/0e6931173d6bc63457b63d90fd8fc470d9b06c0bae79e354110466e47a062428/
=>
https://api.blockcypher.com/v1/btc/test3/txs/0e6931173d6bc63457b63d90fd8fc470d9b06c0bae79e354110466e47a062428?limit=50&includeHex=true

also i'd recommend using testnet, the coins don't have value so you can create transactions more freely without worry. and it is for "testing"!