Skip to content

Instantly share code, notes, and snippets.

@imrehg
Forked from anonymous/2of3.md
Last active January 2, 2016 16:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imrehg/8331382 to your computer and use it in GitHub Desktop.
Save imrehg/8331382 to your computer and use it in GitHub Desktop.

2-of-3 Escrow Example with Electrum

This is gmaxwell's 2-of-2 escrow example, adapted to Electrum and to a lesser extent, GFM.

The Scenario

Archey wants to pay Bertha 0.001 BTC for a Twitter Platinum invite, but prevent Bertha from cheating him. Archey selects Iceland to act as a neutral mediator in case something unexpected happens.

Each party begins by selecting a new Bitcoin address, and then sharing it with the others. These addresses - three in total - are combined to create a three-pronged Bitcoin address. This three-pronged address will require the consent of two of {Archey, Bertha, Iceland} in order for either Archey or Bertha to withdraw funds from it as per the terms of their agreement. This address will visually differ from normal Bitcoin addresses - it will begin with the number '3' unless testnet is used, in which case it will begin with the number '2'.

For simplicity, all public addresses were taken from the same Electrum wallet in the making of this document. The purpose is to demonstrate the Electrum CLI's effectiveness in facilitating safer Bitcoin transactions with multisig.

Steps

  1. Create the multisig address
  2. Fund the multisig address
  3. Withdraw from the multisig address

We're assuming Archey and Bertha have already agreed to the Twitter Platinum invite's terms of sale. By following these instructions, they will both greatly mitigate counterparty risk for little to no cost.

Instructions

Step 1: Create Multisig Address

On Archey:

[live01@Archey ~]$ electrum -w wallet.dat listaddresses
[
    "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
    "16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA", 
    "1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ", 
    "1F5sJKp3oezjkik6zppckE8m8NLdRXUwXH", 
    "16ufMxnGb3wedQcFusQakBh69oZ3r1Htxs", 
    "1Jy3U1Re4cPjYyfckLVWJNGhw6QqDKjNQu"
]

Validateaddress is used to get the pubkey for each address, which is what we need to create the multisig address.

[live01@Archey ~]$ electrum -w wallet.dat validateaddress 1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6
{
    "address": "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
    "isvalid": true, 
}
[live01@Archey ~]$ electrum -w wallet.dat getpubkeys 1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6
{
    "address": "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
    "pubkeys": [ "045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c2415340" ]
}

On Iceland:

[live01@Archey ~]$ electrum -w wallet.dat listaddresses
[
    "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
    "16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA", 
    "1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ", 
    "1F5sJKp3oezjkik6zppckE8m8NLdRXUwXH", 
    "16ufMxnGb3wedQcFusQakBh69oZ3r1Htxs", 
    "1Jy3U1Re4cPjYyfckLVWJNGhw6QqDKjNQu"
]
[escrow01@Iceland ~]$ electrum -w wallet.dat getpubkeys 16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA
{
    "address": "16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA", 
    "pubkeys": [ "04d6d8237913748f4051cd05d435142e5a85d46253b7a432c1560a91b1db3873a6a1bdf9b5fad443aa35654634d0a3cf826c6819514645b6fb60904a63b1415de9" ]
}

On Bertha:

[live01@Bertha ~]$ electrum -w wallet.dat listaddresses
[
    "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
    "16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA", 
    "1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ", 
    "1F5sJKp3oezjkik6zppckE8m8NLdRXUwXH", 
    "16ufMxnGb3wedQcFusQakBh69oZ3r1Htxs", 
    "1Jy3U1Re4cPjYyfckLVWJNGhw6QqDKjNQu"
]
[live01@Bertha ~]$ electrum -w wallet.dat getpubkeys 1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ
{
    "address": "1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ", 
    "pubkeys": [ "04883519de6cc5382063f5d563c737f76c192046e8e5c8ffec758df0a52e9de4b7ce37394d03a8ab7f1e66809c52ef3d1abe910109471d4584a4353e41c73ae478" ]
}

On Archey, Bertha & Iceland:

[live01@*** ~]$ electrum -w wallet.dat createmultisig 2 '["045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c2415340", "04d6d8237913748f4051cd05d435142e5a85d46253b7a432c1560a91b1db3873a6a1bdf9b5fad443aa35654634d0a3cf826c6819514645b6fb60904a63b1415de9", "04883519de6cc5382063f5d563c737f76c192046e8e5c8ffec758df0a52e9de4b7ce37394d03a8ab7f1e66809c52ef3d1abe910109471d4584a4353e41c73ae478"]'
{
    "address": "34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw", 
    "redeemScript": "5241045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c24153404104d6d8237913748f4051cd05d435142e5a85d46253b7a432c1560a91b1db3873a6a1bdf9b5fad443aa35654634d0a3cf826c6819514645b6fb60904a63b1415de94104883519de6cc5382063f5d563c737f76c192046e8e5c8ffec758df0a52e9de4b7ce37394d03a8ab7f1e66809c52ef3d1abe910109471d4584a4353e41c73ae47853ae"
}

The above command produces the same output for Archey, Bertha and Iceland. The multisig address 34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw has been created.

The three prongs of this multisig address are:

  • Archey: 1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6
  • Bertha: 1DHYNS2G7kYAz3CjFU9ajLTnu48kNWCLZQ
  • Iceland: 16mp8yTH6mRuDQaHrLgZDaoCQyjiZ1SgeA

Step 2: Fund the Multisig Address

Now Archey sends 0.001 BTC to the multisig address 34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw.

On Archey:

[live01@Archey ~]$ electrum -w wallet.dat deseed
Warning: you are going to create a seedless wallet'
It will be saved in 'wallet.dat.seedless'
Are you sure you want to continue? (y/n) y
Done.
[live01@Archey ~]$ electrum -w wallet.dat.seedless listunspent
[
    {
        "address": "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
        "index": 0, 
        "raw_output_script": "76a9146d078f704c400f1840d02f4239ea8f40ce5a847488ac", 
        "tx_hash": "cfed2aac04b2823a679d5d5956462b086ac8d47252d0a5506f9b8d5172d99f75", 
        "value": "4"
    }
]
[live01@Archey ~]$ electrum -w wallet.dat.seedless createrawtransaction '[{"txid" : "cfed2aac04b2823a679d5d5956462b086ac8d47252d0a5506f9b8d5172d99f75","vout":0}]' '{"34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw":0.001,"1MbbbLkwLUvP4hXiDLE1Ma42sz9T3J89TG":3.9988}'
{
    "complete": false, 
    "hex": "0100000001759fd972518d9b6f50a5d05272d4c86a082b4656595d9d673a82b204ac2aedcf0000000000ffffffff02a08601000000000017a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed8740afd517000000001976a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac00000000", 
    "input_info": "[{\"redeemScript\":null,\"signatures\":null,\"vout\":0,\"txid\":\"cfed2aac04b2823a679d5d5956462b086ac8d47252d0a5506f9b8d5172d99f75\",\"KeyID\":null,\"pubkeys\":null,\"scriptPubKey\":null}]"
}
[live01@Archey ~]$ electrum -w wallet.dat signrawtransaction '0100000001759fd972518d9b6f50a5d05272d4c86a082b4656595d9d673a82b204ac2aedcf0000000000ffffffff02a08601000000000017a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed8740afd517000000001976a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac00000000'
Password:
{
    "complete": true, 
    "hex": "0100000001759fd972518d9b6f50a5d05272d4c86a082b4656595d9d673a82b204ac2aedcf000000008b483045022100a8065915b3ad1fa3e41db8d06cecdc11a938671369707e2ee390c1452eb930aa02202dde6b18a6c9f54497ef719784feb9e0a8cb5fc4007ac4f40c5dc73bb3cb6a130141045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c2415340ffffffff02a08601000000000017a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed8740afd517000000001976a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac00000000"
}
[live01@Archey ~]$ electrum -w wallet.dat.seedless sendrawtransaction 0100000001759fd972518d9b6f50a5d05272d4c86a082b4656595d9d673a82b204ac2aedcf000000008b483045022100a8065915b3ad1fa3e41db8d06cecdc11a938671369707e2ee390c1452eb930aa02202dde6b18a6c9f54497ef719784feb9e0a8cb5fc4007ac4f40c5dc73bb3cb6a130141045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c2415340ffffffff02a08601000000000017a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed8740afd517000000001976a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac00000000
Connected to electrum.pdmc.net:50002
"be96b4426ca9ecff46a45c90f2ffbd12af5ea42c590180d309a5958a1327fa50"

Funds have now been sent into the multisig address.

On Bertha:

Bertha can check on the status of the multisig funds; e.g. she may check https://blockchain.info/address/34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw.

Normally, Bertha would wait until the payment into the escrow had several confirmations, then she'd give Archey the Twitter Platinum invite. If she trusts Archey, she may deliver the invite the moment Bitcoin funds enter the pending stage.

Step 3: Withdraw from the Multisig Address

Now we assume the deal is complete to everyone's satisfaction and Bertha wants to take the coins out of escrow.

On Bertha:

Bertha drafts a transaction withdrawing the BTC from the multisig address into an address of her choice. I mistakenly used Archey's address here instead of Bertha's address.

[live01@Bertha ~]$ electrum -w wallet.dat.seedless decoderawtransaction 0100000001759fd972518d9b6f50a5d05272d4c86a082b4656595d9d673a82b204ac2aedcf000000008b483045022100a8065915b3ad1fa3e41db8d06cecdc11a938671369707e2ee390c1452eb930aa02202dde6b18a6c9f54497ef719784feb9e0a8cb5fc4007ac4f40c5dc73bb3cb6a130141045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c2415340ffffffff02a08601000000000017a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed8740afd517000000001976a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac00000000
{
    "inputs": [
        {
            "address": "1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6", 
            "prevout_hash": "cfed2aac04b2823a679d5d5956462b086ac8d47252d0a5506f9b8d5172d99f75", 
            "prevout_n": 0, 
            "sequence": 4294967295, 
            "signatures": null
        }
    ], 
    "lockTime": 0, 
    "outputs": [
        {
            "address": "34PTJeM3gtGSk3fqAJ4iWcEsbip46cbDVw", 
            "index": 0, 
            "raw_output_script": "a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed87", 
            "value": 100000
        }, 
        {
            "address": "1MbbbLkwLUvP4hXiDLE1Ma42sz9T3J89TG", 
            "index": 1, 
            "raw_output_script": "76a914e1ed88cdd3ff3eead6ebffca4493465b44db983a88ac", 
            "value": 399880000
        }
    ], 
    "version": 1
}
[live01@Bertha ~]$ electrum -w wallet.dat.seedless createrawtransaction '[{"txid":"be96b4426ca9ecff46a45c90f2ffbd12af5ea42c590180d309a5958a1327fa50","vout":0,"scriptPubKey":"a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed87","redeemScript":"5241045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c24153404104d6d8237913748f4051cd05d435142e5a85d46253b7a432c1560a91b1db3873a6a1bdf9b5fad443aa35654634d0a3cf826c6819514645b6fb60904a63b1415de94104883519de6cc5382063f5d563c737f76c192046e8e5c8ffec758df0a52e9de4b7ce37394d03a8ab7f1e66809c52ef3d1abe910109471d4584a4353e41c73ae47853ae"}]' '{"1AwViD7ewnVVrzt58ffQdheyTxMGhAshJ6":0.0008}'
{
    "complete": false, 
    "hex": "010000000150fa27138a95a509d38001592ca45eaf12bdfff2905ca446ffeca96c42b496be0000000000ffffffff0180380100000000001976a9146d078f704c400f1840d02f4239ea8f40ce5a847488ac00000000", 
    "input_info": "[{\"redeemScript\":\"5241045ce62c5a778c083f623e720e99e00e56235578d22c4b7cebec5c42d4cf86d1b8aa5af0f1f5846e899c98605cbad41a4161d200ce929db5e867977912c24153404104d6d8237913748f4051cd05d435142e5a85d46253b7a432c1560a91b1db3873a6a1bdf9b5fad443aa35654634d0a3cf826c6819514645b6fb60904a63b1415de94104883519de6cc5382063f5d563c737f76c192046e8e5c8ffec758df0a52e9de4b7ce37394d03a8ab7f1e66809c52ef3d1abe910109471d4584a4353e41c73ae47853ae\",\"signatures\":null,\"vout\":0,\"txid\":\"be96b4426ca9ecff46a45c90f2ffbd12af5ea42c590180d309a5958a1327fa50\",\"KeyID\":null,\"pubkeys\":null,\"scriptPubKey\":null}]"
}

Now Bertha partially signs it using her private key. From this point on, for scriptPubKey, I used the OP_HASH160 'asm' value of 1d9613bb16091ab2876c829e6bd5f56bed14b9ed instead of a9141d9613bb16091ab2876c829e6bd5f56bed14b9ed87. It made no difference, but a914... should've been used.

[live01@Bertha ~]$ electrum -w wallet.dat signrawtransaction '010000000150fa27138a95a509d38001592ca45eaf12bdfff2905ca446ffeca96c42b496be0000000000ffffffff0180380100000000001976a9146d078f704c400f1840d02f4239ea8f40ce5a847488ac00000000' '[{"txid":"be96b4426ca9ecff46a45c90f2ffbd12af5ea42c590180d309a5958a1327fa50","vout":0,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment