Dark wallet bitcoin card wallet
49 commentsDiy bitcoin mining rig builder
Recently, inspired by Ken Shirriff's and Bryce Neal's low level looks at the Bitcoin protocol, I set about constructing Bitcoin's much talked about multisignature transactions from scratch to understand their capabilities and limitations. The code to do it all in Go is available as go-bitcoin-multsig on GitHub and I'd like to go through how all of this works at the Bitcoin protocol level.
We'll also step through creating and spending a multisig transaction to make it all clearer. In many ways, this is a follow up to Ken's amazing explanation of the Bitcoin protocol and constructing a Pay-to-PubKeyHash P2PKH transaction, so I won't cover things covered there in any great detail.
Please check out his post out first if you're completely new to the Bitcoin protocol. I'll be using go-bitcoin-multisig to generate keys and transactions along the way, explaining each step. If you'd like to follow along and create a multisig transaction yourself, you'll need to follow the simple build instructions for go-bitcoin-multisig.
To spend Bitcoin funds sent to this type of address, the recipient must use the private key associated with the public key hash specified in that address to create a digital signature, which is put into the scriptSig of a spending transaction, unlocking the funds.
This is because P2SH addresses have a version byte prefix of 0x05 , instead of the 0x00 prefix in P2PKH addresses, and these come out as a '3' and '1' after base58check encoding. So what information is encoded in a P2SH address? A specific unspent Bitcoin can actually have a whole range of different spending conditions attached to it, the most common being a typical P2PKH which just requires the recipient to provide a signature matching the public key hash.
The Bitcoin core developers realized that people were looking at the capabilities of Bitcoin's Script language and seeing a whole array of possibilities about what spending conditions you could attach to a Bitcoin output, to create much more elaborate transactions than just P2PKH transactions.
The core developers decided that instead of letting senders put in long scripts into their scriptPubKey where spending conditions usually go , they would let each sender put in a hash of their spending conditions instead. These spending conditions are known as the redeem script , and a P2SH funding transaction simply contains a hash of this redeem script in the scriptPubKey of the funding transaction.
The redeem script itself is only revealed, checked against the redeem script hash, and evaluated during the spending transaction. This puts the responsibility of providing the full redeem script on to the recipient of the P2SH funds.
This has a number of advantages:. All of this will hopefully make more sense as we go ahead and craft a multisignature P2SH transaction. If you'd like to learn more, the Bitcoin developer guide has a full explanation of P2SH transactions.
We will create a 2-of-3 multisignature address, where 2 digital signatures of 3 possible public keys are required to spend funds sent to this address. First we need the hex representations of 3 public keys. Now, we specify that we want a 2-of-3 address and provide our 3 public keys to generate our P2SH address:. Let's breakdown that redeem script since that is where all the magic happens.
A valid multisignature redeem script, according to the Bitcoin protocol , looks like:. It contains a hashed redeem script with our chosen public keys and multisig script, but this will not be revealed publicly until the spending transaction, since it has been hashed. We would at this point pass this address to the sender who is funding our multisig address. To fund our multisig address now, we need a funding source of Bitcoins.
Note that the generated transaction changes slightly each time because of the nonce in the digital signatures and this may change the total size of the transaction slightly each time. Everything else should remain the same. We now have a scriptPubKey of the form:. This is used to compare the redeem script provided in the spending transaction to the hash in the funding transaction.
We'll see how the scriptPubKey here and the scriptSig of the spending transaction come together shortly. At this point, you can broadcast your own funding transaction and have it actually confirmed on the network.
The transaction above was broadcast and confirmed as txid 02be35dce7efa0bfb7f4b79c4cdcd3d. Now we want to be able to spend our P2SH multisig funds. First let's generate another key pair to be our destination where we can send our multisig funds.
Now, we will need 2 of the 3 private keys of the public keys used to generate our P2SH address. We'll use our 1st and 3rd original generated private keys any 2 of 3 would work, of course. Now, this is important: We can obviously skip keys when our M required keys is less than our N possible keys, but they must show up in our signed spending transaction in the same order that they were provided in the redeem script. To create our spending transaction, we need the input txid of the funding transaction, our amount with the remaining balance going to transaction fees and the destination.
We must also provide the original redeem script. Remember, the destination P2SH address is a hash and doesn't reveal our redeem script. Only the recipient who created the P2SH address knows the full redeem script, and in this case, we are that recipient and can provide it:. Again, the transaction will look slightly different each time because of the changing nonce in the digital signature, but everything else should look the same.
Let's look at how the Bitcoin protocol will run through the script here. Combining the spending transaction scriptSig and funding transaction scriptPubKey, we get:.
As stated earlier, the order of signatures matters here and must match the order that the public keys were provided in. A couple of important notes, especially for troubleshooting, on how this raw transaction is created:. We can now broadcast this transaction to spend our multisig P2SH funds. You can see the above transaction confirmed as txid eeab3ef6cbea5fb1bb8babeb7cde10ae5a7d8a3fa57dca I hope all of that was helpful for anyone trying to understand the innards of the Bitcoin protocol or trying to build multisig applications on top of the raw Bitcoin protocol.