Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: CIYAM on November 20, 2012, 08:49:06 AM



Title: Couple of questions about the raw tx API commands
Post by: CIYAM on November 20, 2012, 08:49:06 AM
I have just started playing the the listunspent/createrawtransaction/signrawtransaction/sendrawtransaction RPC commands which work great, however, there are a couple of things I am wondering about.

1) Why doesn't the listunspent include a bitcoin "address" as well as the public key in the JSON output (as I can't easily tell what is what when I'm looking at that without jumping into blockchain.info to look up the tx)?

2) If I want to send to more than one output address with createrawtransaction what is the exact syntax (an example somewhere in the docco would be nice)?


Title: Re: Couple of questions about the raw tx API commands
Post by: kjj on November 20, 2012, 09:51:46 PM
I have just started playing the the listunspent/createrawtransaction/signrawtransaction/sendrawtransaction RPC commands which work great, however, there are a couple of things I am wondering about.

1) Why doesn't the listunspent include a bitcoin "address" as well as the public key in the JSON output (as I can't easily tell what is what when I'm looking at that without jumping into blockchain.info to look up the tx)?

2) If I want to send to more than one output address with createrawtransaction what is the exact syntax (an example somewhere in the docco would be nice)?

1) No idea.  I've never felt like it was "missing" as such, and I've done quite a bit with raw transactions.  It is trivial to figure out the address, when one exists.  A normal sendtoaddress transaction will start with 76a914 and end with 88ac * (both in hex), with the pubkey in between, and from there it is just a matter of hashing and base58check encoding.  A P2SH address will have a different scheme, as will a generate transaction.

2) the output section of createrawtransaction is a list, {"address1":"amount1","address2":"amount2","address3":"amount3"...}.

*  In case anyone was wondering, 76a914 is OP_DUP OP_HASH160 PUSH_20BYTES and 88ac is OP_EQUALVERIFY OP_CHECKSIG.


Title: Re: Couple of questions about the raw tx API commands
Post by: CIYAM on November 21, 2012, 01:33:38 AM
1) No idea.  I've never felt like it was "missing" as such, and I've done quite a bit with raw transactions.  It is trivial to figure out the address, when one exists.  A normal sendtoaddress transaction will start with 76a914 and end with 88ac * (both in hex), with the pubkey in between, and from there it is just a matter of hashing and base58check encoding.  A P2SH address will have a different scheme, as will a generate transaction.

Sounds like you are a lot more familiar with the inner workings of Bitcoin than me - but how exactly (say from a bash shell) do I get the hash of the public key and base58 encode it (which is why I thought bitcoind should do this for me)?

2) the output section of createrawtransaction is a list, {"address1":"amount1","address2":"amount2","address3":"amount3"...}.

Thanks for that.


Title: Re: Couple of questions about the raw tx API commands
Post by: kjj on November 21, 2012, 01:51:19 AM
1) No idea.  I've never felt like it was "missing" as such, and I've done quite a bit with raw transactions.  It is trivial to figure out the address, when one exists.  A normal sendtoaddress transaction will start with 76a914 and end with 88ac * (both in hex), with the pubkey in between, and from there it is just a matter of hashing and base58check encoding.  A P2SH address will have a different scheme, as will a generate transaction.

Sounds like you are a lot more familiar with the inner workings of Bitcoin than me - but how exactly (say from a bash shell) do I get the hash of the public key and base58 encode it (which is why I thought bitcoind should do this for me)?

Well, I do most of my stuff in PHP.  Here is some code, intended more as an example than as something to use.  You'll need some support functions to use it, etc, etc.

Code: (PHP)
<?php

// takes $pubkey as a hex string, returns an address
function gen_addr($pubkey){
 
$h=hash("sha256",hex2bin($pubkey),TRUE);
 
$h2=hash("ripemd160",$h,TRUE);
 
$h3="\x00".$h2;
 
$h4=hash("sha256",$h3,TRUE);
 
$h5=hash("sha256",$h4,TRUE);
 
$chk=substr($h5,0,4);
 
$addr_bin=$h3.$chk;
 
$addr=encodeBase58($addr_bin);
 return 
$addr;
}

?>



Title: Re: Couple of questions about the raw tx API commands
Post by: CIYAM on November 21, 2012, 02:36:19 AM
Okay - after mulling over your hex hints I worked out that from:

Code:
"scriptPubKey" : "76a91406c1991c6466a550b66931ea48970cfd17ff51b388ac",

if I remove the 76a914 prefix and the 88ac suffix then I get 06c1991c6466a550b66931ea48970cfd17ff51b3 which if convert to base58 using brainwallet.html (as there doesn't seem to be any standard Linux program to do this conversion and I don't use PHP) I finally get 1ciyam3htJit1feGa26p2wQ4aw6KFTejU.

That is really a PITA - I guess I could write my own program to do this, however, I really hope one of the devs is reading this and might consider adding the following to the listunspent outout:

Code:
"scriptPubKey" : "76a91406c1991c6466a550b66931ea48970cfd17ff51b388ac",
"address" : "1ciyam3htJit1feGa26p2wQ4aw6KFTejU",


Title: Re: Couple of questions about the raw tx API commands
Post by: Gavin Andresen on November 21, 2012, 02:54:08 AM
listunspent output is designed to be read by machines, not people, and its output is designed to go directly into createrawtransaction.

If you need all the human-friendly details of a transaction, use getrawtransaction <txid> 1

I'm against adding "address" to the listunspent output, because most of the time it will be unused and sometimes it would just not appear at all (because not all scriptPubKeys are bitcoin addresses).


Title: Re: Couple of questions about the raw tx API commands
Post by: CIYAM on November 21, 2012, 03:41:02 AM
listunspent output is designed to be read by machines, not people, and its output is designed to go directly into createrawtransaction.

If you need all the human-friendly details of a transaction, use getrawtransaction <txid> 1

Thanks Gavin - I hadn't realised I could do that (although it is one extra step it should be easy enough) - understand that I need to know the address (if present) as my raw transaction needs to include that as the "change address" (yes - am not wanting to be anonymous at all with this use case) so indeed I am wanting to automate this but the "machine" still needs the address.

I'm against adding "address" to the listunspent output, because most of the time it will be unused and sometimes it would just not appear at all (because not all scriptPubKeys are bitcoin addresses).

I understand - but it would make my job as a developer easier if I could just make one RPC call rather than several (as I need to find a specific address that I am going to create a raw tx to send coins to another address with change being sent back to itself).