Skip to content

Instantly share code, notes, and snippets.

@gavinandresen
Last active July 14, 2024 10:15
Show Gist options
  • Save gavinandresen/3966071 to your computer and use it in GitHub Desktop.
Save gavinandresen/3966071 to your computer and use it in GitHub Desktop.
# Raw transaction API example work-through
# Send coins to a 2-of-3 multisig, then spend them.
#
# For this example, I'm using these three keypairs (public/private)
# 0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f86 / 5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU
# 04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec6874 / 5Jb7fCeh1Wtm4yBBg3q3XbT6B525i17kVhy3vMC9AqfR6FH2qGk
# 048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d46213 / 5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw
# First: combine the three keys into a multisig address:
./bitcoind createmultisig 2 '["0491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f86","04865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec6874","048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d46213"]'
{
"address" : "3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC",
"redeemScript" : "52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae"
}
# Next, create a transaction to send funds into that multisig. Transaction d6f72... is
# an unspent transaction in my wallet (which I got from the 'listunspent' RPC call):
./bitcoind createrawtransaction '[{"txid" : "d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389","vout":0}]' '{"3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC":0.01}'
010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d60000000000ffffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000
# ... and sign it:
./bitcoind signrawtransaction 010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d60000000000ffffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000
{
"hex" : "010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d6000000008b483045022100abbc8a73fe2054480bda3f3281da2d0c51e2841391abd4c09f4f908a2034c18d02205bc9e4d68eafb918f3e9662338647a4419c0de1a650ab8983f1d216e2a31d8e30141046f55d7adeff6011c7eac294fe540c57830be80e9355c83869c9260a4b8bf4767a66bacbd70b804dc63d5beeb14180292ad7f3b083372b1d02d7a37dd97ff5c9effffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000",
"complete" : true
}
# Now, create a transaction that will spend that multisig transaction. First, I need the txid
# of the transaction I just created, so:
./bitcoind decoderawtransaction 010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d6000000008b483045022100abbc8a73fe2054480bda3f3281da2d0c51e2841391abd4c09f4f908a2034c18d02205bc9e4d68eafb918f3e9662338647a4419c0de1a650ab8983f1d216e2a31d8e30141046f55d7adeff6011c7eac294fe540c57830be80e9355c83869c9260a4b8bf4767a66bacbd70b804dc63d5beeb14180292ad7f3b083372b1d02d7a37dd97ff5c9effffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000
{
"txid" : "3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac",
... etc, rest omitted to make this shorter
}
# Create the spend-from-multisig transaction. Since the fund-the-multisig transaction
# hasn't been sent yet, I need to give txid, scriptPubKey and redeemScript:
./bitcoind createrawtransaction '[{"txid":"3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac","vout":0,"scriptPubKey":"a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087","redeemScript":"52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae"}]' '{"1GtpSrGhRGY5kkrNz4RykoqRQoJuG2L6DS":0.01}'
0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c0000000000ffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000
# ... Now I can partially sign it using one private key:
./bitcoind signrawtransaction '0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c0000000000ffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000' '[{"txid":"3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac","vout":0,"scriptPubKey":"a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087","redeemScript":"52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae"}]' '["5JaTXbAUmfPYZFRwrYaALK48fN6sFJp4rHqq2QSXs8ucfpE4yQU"]'
{
"hex" : "0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c00000000fd15010048304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78014cc952410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353aeffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000",
"complete" : false
}
# ... and then take the "hex" from that and complete the 2-of-3 signatures using one of
# the other private keys (note the "hex" result getting longer):
./bitcoind signrawtransaction '0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c00000000fd15010048304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78014cc952410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353aeffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000' '[{"txid":"3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac","vout":0,"scriptPubKey":"a914f815b036d9bbbce5e9f2a00abd1bf3dc91e9551087","redeemScript":"52410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353ae"}]' '["5JFjmGo5Fww9p8gvx48qBYDJNAzR9pmH5S389axMtDyPT8ddqmw"]'
{
"hex" : "0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c00000000fd5d010048304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78014730440220795f0f4f5941a77ae032ecb9e33753788d7eb5cb0c78d805575d6b00a1d9bfed02203e1f4ad9332d1416ae01e27038e945bc9db59c732728a383a6f1ed2fb99da7a4014cc952410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353aeffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000",
"complete" : true
}
# And I can send the funding and spending transactions:
./bitcoind sendrawtransaction 010000000189632848f99722915727c5c75da8db2dbf194342a0429828f66ff88fab2af7d6000000008b483045022100abbc8a73fe2054480bda3f3281da2d0c51e2841391abd4c09f4f908a2034c18d02205bc9e4d68eafb918f3e9662338647a4419c0de1a650ab8983f1d216e2a31d8e30141046f55d7adeff6011c7eac294fe540c57830be80e9355c83869c9260a4b8bf4767a66bacbd70b804dc63d5beeb14180292ad7f3b083372b1d02d7a37dd97ff5c9effffffff0140420f000000000017a914f815b036d9bbbce5e9f2a00abd1bf3dc91e955108700000000
3c9018e8d5615c306d72397f8f5eef44308c98fb576a88e030c25456b4f3a7ac
./bitcoind sendrawtransaction 0100000001aca7f3b45654c230e0886a57fb988c3044ef5e8f7f39726d305c61d5e818903c00000000fd5d010048304502200187af928e9d155c4b1ac9c1c9118153239aba76774f775d7c1f9c3e106ff33c0221008822b0f658edec22274d0b6ae9de10ebf2da06b1bbdaaba4e50eb078f39e3d78014730440220795f0f4f5941a77ae032ecb9e33753788d7eb5cb0c78d805575d6b00a1d9bfed02203e1f4ad9332d1416ae01e27038e945bc9db59c732728a383a6f1ed2fb99da7a4014cc952410491bba2510912a5bd37da1fb5b1673010e43d2c6d812c514e91bfa9f2eb129e1c183329db55bd868e209aac2fbc02cb33d98fe74bf23f0c235d6126b1d8334f864104865c40293a680cb9c020e7b1e106d8c1916d3cef99aa431a56d253e69256dac09ef122b1a986818a7cb624532f062c1d1f8722084861c5c3291ccffef4ec687441048d2455d2403e08708fc1f556002f1b6cd83f992d085097f9974ab08a28838f07896fbab08f39495e15fa6fad6edbfb1e754e35fa1c7844c41f322a1863d4621353aeffffffff0140420f00000000001976a914ae56b4db13554d321c402db3961187aed1bbed5b88ac00000000
837dea37ddc8b1e3ce646f1a656e79bbd8cc7f558ac56a169626d649ebe2a3ba
# You can see these transactions at:
# http://blockchain.info/address/3QJmV3qfvL9SuYo34YihAf3sRCW3qSinyC
@phanaster
Copy link

Some questions :
If I send the funding transaction long before creating the redeeming transaction do i still need the scriptpubkey,txid and the redeemingscript? or do i only need the txid?

am i able to spend from an adress with another txid than the funding transaction with all privatekeys?

Do i have to call listunspent for txids or is there a function with which i can create new ids?

@cimm
Copy link

cimm commented Feb 6, 2014

There is a typo on line 55. Think you want the private key here, not the public one.

@flybird365
Copy link

What if the multi-parties never claim the coins? Do the sender have the capability to cancel the contract in order to free the coins?

@AndyOfiesh
Copy link

Is there something in bitcoind that would allow me wrap this with P2SH?

@bencxr
Copy link

bencxr commented Apr 24, 2014

flybird365, i don't think so. one way around could be to sign a nlocktime transaction releasing the coins if they are not spent by a certain date.

@microlost
Copy link

how can i import the multisign-addr to the qt wallet,or when the qt wallet will support the multisign-addr transaction?
I got a return:TX rejected (code -22) vwhen sendrawtransaction,what is the matter?

@ShawnKimble
Copy link

How do you obtain 'scriptPubKey' when you are the second party signing the transaction, and may not have known how the MultiSig address was originally created?

Found my answer --> "decoderawtransaction"

@fcracker79
Copy link

Hi there,

thank you for your great article.

I tried to do it step by step, but when submitting the transaction, I receive the following:

bitcoin-cli -regtest sendrawtransaction 0100000001e9c9a5c8d4a7e58e72b97e0d4ffc9dc6ff591af55d6751a6869d112097d6b9a300000000fdfd0000473044022019aabf229a168f5ef9610af1c2386ed4320fa66f78e4d77b0b5bd457484e8d8f02206b5bac92bf85bd03be96ea942d700e4a4c6f8f8dbfdf08c7848357ee14a5f88d01483045022100c6bcfb8d8018870140b48a2cac26ae777979dd403d9b0990b61a81385ddbf5a80220558248484224d9de8afab4fce376c43e59181611020480337254938db9f42983014c69522103cd316868fa25a0da24e75cbafe9e702da6c7171c59b1680464d4f8c1963e50ca21033bcdd9e06ab88647aad2e6d395d3f319d727cd5bd6c559711dbe3d7190b45dce210206c8411fdb01605a2394c7e8a336e6f93a9b28a38368eabf2cd42387ecaeec3253aeffffffff0100c2eb0b000000001976a914fa4ce907b496243ee5c1880a962f597c981ae80d88ac00000000
error: {"code":-25,"message":""}

However,
bitcoin-cli -regtest signrawtransaction '0100000001e9c9a5c8d4a7e58e72b97e0d4ffc9dc6ff591af55d6751a6869d112097d6b9a300000000fdfd0000473044022019aabf229a168f5ef9610af1c2386ed4320fa66f78e4d77b0b5bd457484e8d8f02206b5bac92bf85bd03be96ea942d700e4a4c6f8f8dbfdf08c7848357ee14a5f88d01483045022100c6bcfb8d8018870140b48a2cac26ae777979dd403d9b0990b61a81385ddbf5a80220558248484224d9de8afab4fce376c43e59181611020480337254938db9f42983014c69522103cd316868fa25a0da24e75cbafe9e702da6c7171c59b1680464d4f8c1963e50ca21033bcdd9e06ab88647aad2e6d395d3f319d727cd5bd6c559711dbe3d7190b45dce210206c8411fdb01605a2394c7e8a336e6f93a9b28a38368eabf2cd42387ecaeec3253aeffffffff0100c2eb0b000000001976a914fa4ce907b496243ee5c1880a962f597c981ae80d88ac00000000' '[{"txid":"a3b9d69720119d86a651675df51a59ffc69dfc4f0d7eb9728ee5a7d4c8a5c9e9","vout":0,"scriptPubKey":"a914e08d76f2616103e174d0c3184f5b7a22f55e6f9987","redeemScript":"522103cd316868fa25a0da24e75cbafe9e702da6c7171c59b1680464d4f8c1963e50ca21033bcdd9e06ab88647aad2e6d395d3f319d727cd5bd6c559711dbe3d7190b45dce210206c8411fdb01605a2394c7e8a336e6f93a9b28a38368eabf2cd42387ecaeec3253ae"}]' '[]'
{
"hex" : "0100000001e9c9a5c8d4a7e58e72b97e0d4ffc9dc6ff591af55d6751a6869d112097d6b9a300000000fdfd0000473044022019aabf229a168f5ef9610af1c2386ed4320fa66f78e4d77b0b5bd457484e8d8f02206b5bac92bf85bd03be96ea942d700e4a4c6f8f8dbfdf08c7848357ee14a5f88d01483045022100c6bcfb8d8018870140b48a2cac26ae777979dd403d9b0990b61a81385ddbf5a80220558248484224d9de8afab4fce376c43e59181611020480337254938db9f42983014c69522103cd316868fa25a0da24e75cbafe9e702da6c7171c59b1680464d4f8c1963e50ca21033bcdd9e06ab88647aad2e6d395d3f319d727cd5bd6c559711dbe3d7190b45dce210206c8411fdb01605a2394c7e8a336e6f93a9b28a38368eabf2cd42387ecaeec3253aeffffffff0100c2eb0b000000001976a914fa4ce907b496243ee5c1880a962f597c981ae80d88ac00000000",
"complete" : true
}

That is, the transaction seems to be valid.
Please would you mind providing some kind of help?

Thank you very much

@init-zz-max
Copy link

Hello,

How can I create RAW TX and sign it with a few addresses

multisigAddress1
multisigAddress2

Any example ?

@ShenthilkumarCK
Copy link

Hi, Before decoderawtransaction you need to sendrawtransaction and use the output for decoderawtransaction. It solves the error: {"code":-26,"message":"18: bad-txns-inputs-spent"} error.

@7sedam7
Copy link

7sedam7 commented Sep 12, 2017

Hi,

the above example does not work anymore. Can you please help.

  1. multisig addresses when created do not start with 3 anymore

  2. I can not sign the transaction no matter what I do

Please help, thanks.

@lfcook11
Copy link

As part of a class assignment I created a multi sig address and sent bitcoin to it but now I cannot find the transaction or the coin. Obvious novice error but it was not on test so ....Any help would be appreciated.

@fiatjaf
Copy link

fiatjaf commented Jun 4, 2019

@lfcook11 try searching your bash history for the commands that created the address and redo them. You should get the same address.

@ademidun
Copy link

@lfcook11 (and other people coming here in the future) If you remember the date and the amount you sent, you can use a block explorer search engine.

I Googled "search bitcoin transaction by date" and found this Stack Overflow answer:

Suppose I want to search for a transaction of exactly 445 BTC in the date range 27 July 2013 and Aug 3rd 2013 ... is there an (online?) tool that has this kind of search capabilities?

Blockchair allow you to query the bitcoin blockchain with lots of parameter such as date, BTC output amount, fee amount etc...

Here is your request: https://blockchair.com/bitcoin/transactions?q=time(2013-07-27..2013-08-03),output_total(44500000000)#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment