Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: jon_smark on August 18, 2012, 11:47:46 AM



Title: Using the raw transactions API to build a simple transaction
Post by: jon_smark on August 18, 2012, 11:47:46 AM
I'm trying to get a good understanding of how Bitcoin works, and the combination of testnet + raw transactions is a wonderful platform for this!  I do have some questions concerning the basics, so I'll describe how I reckon a simple transaction can be built, and I hope you'll correct me and/or answer some of my doubts.

Suppose that a past transaction with txid=aaaaaaaa gave 100 BTC to address m1111 on output vout=1.  I now want to take these 100 BTC and give 90 BTC to address m2222, 9 BTC to address m3333, and pay the remaining 1 BTC as a transaction fee.  These are the steps I think I need to do (in pseudo-code):

First I would need to create a raw transaction:

   
    hex = createrawtransaction [{txid:aaaaaaaa,vout=1}] {m2222=90,m3333=9}
   


Now I need to sign the transaction.  This is where I have a couple of doubts. First, in this simple example, the second parameter of signrawtransaction can be just an empty list, right? Second, what should the sighash parameter normally be?

   
    priv = dumpprivkey m1111
    hex2 = signrawtransaction hex [] priv ALL
   


Finally I can send the transaction:

   
    txid = sendrawtransaction hex2
   


Is this all or am I missing something?
(Thank you for your input!)


Title: Re: Using the raw transactions API to build a simple transaction
Post by: gmaxwell on August 18, 2012, 09:40:19 PM
I'm trying to get a good understanding of how Bitcoin works, and the combination of testnet + raw transactions is a wonderful platform for this!  I do have some questions concerning the basics, so I'll describe how I reckon a simple transaction can be built, and I hope you'll correct me and/or answer some of my doubts.
Suppose that a past transaction with txid=aaaaaaaa gave 100 BTC to address m1111 on output vout=1.  I now want to take these 100 BTC and give 90 BTC to address m2222, 9 BTC to address m3333, and pay the remaining 1 BTC as a transaction fee.  These are the steps I think I need to do (in pseudo-code):
[...]
Now I need to sign the transaction.  This is where I have a couple of doubts. First, in this simple example, the second parameter of signrawtransaction can be just an empty list, right? Second, what should the sighash parameter normally be?

You've basically got it. You don't need to dump or provide the private key: Those parameters are optional— they're used for cases where the relevant keys aren't in your local wallet.

Here are some quick walk-throughs I'm working on that use this api:

https://people.xiph.org/~greg/escrowexample.txt
https://people.xiph.org/~greg/signdemo.txt


Title: Re: Using the raw transactions API to build a simple transaction
Post by: jon_smark on August 19, 2012, 02:40:51 PM
You've basically got it. You don't need to dump or provide the private key: Those parameters are optional— they're used for cases where the relevant keys aren't in your local wallet.

Thanks for the reply!  Indeed, it seems it's even simpler than I thought at first!


Title: Re: Using the raw transactions API to build a simple transaction
Post by: Jouke on January 28, 2013, 10:21:42 PM
my gratitude to both :) Very helpful topic posts :)


Title: Re: Using the raw transactions API to build a simple transaction
Post by: rlh on October 23, 2013, 06:42:25 PM
Hey guys, I have a quick question.  When executing "hex = createrawtransaction [{txid:aaaaaaaa,vout=1}] {m2222=90,m3333=9}", what is the correct value of vout?  Is it always 1? 

How do you build a rawtransaction from multiple inputs?


Title: Re: Using the raw transactions API to build a simple transaction
Post by: dserrano5 on October 23, 2013, 07:27:30 PM
Hey guys, I have a quick question.  When executing "hex = createrawtransaction [{txid:aaaaaaaa,vout=1}] {m2222=90,m3333=9}", what is the correct value of vout?  Is it always 1? 

How do you build a rawtransaction from multiple inputs?

Transactions can (and usually do) have more than one output, and you need to specify the specific output you want to redeem in a given transaction. vout serves that purpose. It's 0-based.


Title: Re: Using the raw transactions API to build a simple transaction
Post by: Abdussamad on October 23, 2013, 07:30:10 PM
vout depends on the input transaction. The first vout of a transaction is 0, the second 1 and so. You'll have to refer to the input transaction on blockexplorer.com to see which vout went to which address.

You can refer to multiple inputs. Just separate them with commas:

createrawtransaction [{txid:aaaaaaaa,vout=1},{txid:aaaaaaaa,vout=2}] {m2222=90,m3333=9}

https://en.bitcoin.it/wiki/Raw_Transactions#createrawtransaction_.5B.7B.22txid.22:txid.2C.22vout.22:n.7D.2C....5D_.7Baddress:amount.2C....7D


Title: Re: Using the raw transactions API to build a simple transaction
Post by: rlh on October 23, 2013, 07:36:59 PM
Thanks guys.  This helps.  However, I keep getting nasty "Error Parsing JSON" errors that don't tell me anything.  Is there s specific format for the raw transaction details that I need to follow.

In my case, I'm using the bitcoind.exe file-- not a linux or Mac build.


Title: Re: Using the raw transactions API to build a simple transaction
Post by: Abdussamad on October 23, 2013, 07:46:43 PM
edit: nvm. Just see this thread for examples from an expert DannyHamilton:

https://bitcointalk.org/index.php?topic=218447.0;all


Title: Re: Using the raw transactions API to build a simple transaction
Post by: rlh on October 23, 2013, 08:47:39 PM
edit: nvm. Just see this thread for examples from an expert DannyHamilton:

https://bitcointalk.org/index.php?topic=218447.0;all

Yes, thank you.  I had just found that post (and hadn't read it) when I responded.


Title: Re: Using the raw transactions API to build a simple transaction
Post by: DannyHamilton on October 24, 2013, 12:02:07 AM
edit: nvm. Just see this thread for examples from an expert DannyHamilton:

https://bitcointalk.org/index.php?topic=218447.0;all

I'm an expert?

That's news to me.   ;D

Let me know if I can help at all.