Skip to content

Commit 19d6500

Browse files
edited/updated §9.2
1 parent 09252b7 commit 19d6500

3 files changed

Lines changed: 102 additions & 48 deletions

File tree

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ If you'd like to make your own translation, please see [Contributing](https://gi
7070
* [8.2: Understanding the PSBT](docs/08_2_Understanding_the_PSBT.md)
7171
* [8.3: Using a Partially Signed Bitcoin Transaction](docs/08_3_Using_a_Partially_Signed_Bitcoin_Transaction.md)
7272
* [8.4: Creating Animated QR Codes](docs/08_4_Creating_Animated_QR_Codes.md)
73-
74-
[in progress]
75-
76-
* [8.0: Expanding Bitcoin Transactions in Other Ways](docs/08_0_Expanding_Bitcoin_Transactions_Other.md)
77-
* [8.1: Sending a Transaction with a Locktime](docs/08_1_Sending_a_Transaction_with_a_Locktime.md)
78-
* [8.2: Sending a Transaction with Data](docs/08_2_Sending_a_Transaction_with_Data.md) [**TO UPDATE**]
73+
* [9.0: Expanding Bitcoin Transactions in Other Ways](docs/09_0_Expanding_Bitcoin_Transactions_Other.md)
74+
* [9.1: Sending a Transaction with a Locktime](docs/09_1_Sending_a_Transaction_with_a_Locktime.md)
75+
* [9.2: Sending a Transaction with Data](docs/09_2_Sending_a_Transaction_with_Data.md)
7976

8077
### PART FOUR: IMPORTING & EXPORTING YOUR SECRETS
8178

Lines changed: 96 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
1-
TODO: Talk about big expansion (30.0)
1+
# 9.2: Sending a Transaction with Data
22

3-
# 8.2: Sending a Transaction with Data
3+
The final way to vary how you send a basic transaction is to use the
4+
transaction to send data instead of funds (or really, in addition to
5+
funds). This gives you the ability to embed information in the
6+
blockchain. It is done through a special `OP_RETURN` command.
47

5-
The final way to vary how you send a basic transaction is to use the transaction to send data instead of funds (or really, in addition to funds). This gives you the ability to embed information in the blockchain. It is done through a special `OP_RETURN` command.
6-
7-
The catch? You can only store 80 bytes at a time!
8+
The catch? It's controversial. Many people think you shouldn't put
9+
data onto the Bitcoin chain at all. For a long time, `OP_RETURN` was
10+
limited to 80 bytes to minimize its use, but in recent years it's
11+
become unlimited.
812

913
## Create Your Data
1014

11-
The first thing you need to do is create the 80 bytes (or less) of data that you'll be recording in your `OP_RETURN`. This might be as simple as preparing a message or you might be hashing existing data. For example, `sha256sum` produces 256 bits of data, which is 32 bytes, well under the limits:
15+
The first thing you need to do is create the data that you'll be
16+
recording in your `OP_RETURN`. This might be as simple as preparing a
17+
message or you might be hashing existing data. For example,
18+
`sha256sum` produces 256 bits of data, which you can use to commit to
19+
a document such as a contract.
20+
1221
```
13-
$ sha256sum contract.jpg
14-
b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75 contract.jpg
22+
$ sha256sum contract.pdf
23+
b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75 contract.pdf
1524
$ op_return_data="b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75"
1625
```
17-
> :book: _What is an OP_RETURN?_ All Bitcoin transactions are built upon opcode scripts that we'll meet in the next chapter. The `OP_RETURN` is a simple opcode that defines an OUTPUT as invalid. Convention has resulted in it being used to embed data on the blockchain.
26+
27+
> 📖 _What is an OP_RETURN?_ All Bitcoin transactions are built upon
28+
opcode scripts that we'll meet in later chapters. The `OP_RETURN` is a
29+
simple opcode that defines an OUTPUT as invalid. Convention has
30+
resulted in it being used to embed data on the blockchain.
1831

1932
## Prepare Some Money
2033

21-
Your purpose in creating a data transaction isn't to send money to anyone, it's to put data into the blockchain. However, you _must_ send money to do so. You just need to use a change address as your _only_ recipient. Then you can identify a UTXO and send that to your change address, minus a transaction fee, while also using the same transaction to create an OP_RETURN.
34+
Your purpose in creating a data transaction isn't to send money to
35+
anyone, it's to put data into the blockchain. However, you _must_ send
36+
money to do so. You just need to use a change address as your _only_
37+
recipient. Then you can identify a UTXO and send that to your change
38+
address, minus a transaction fee, while also using the same
39+
transaction to create an OP_RETURN.
2240

2341
Here's the standard setup:
2442
```
2543
$ bitcoin-cli listunspent
2644
[
2745
{
28-
"txid": "854a833b667049ac811b4cf1cad40fa7f8dce8b0f4c1018a58b84559b6e05f42",
29-
"vout": 0,
30-
"address": "tb1q6kgsjxuqwj3rwhkenpdfcjccalk06st9z0k0kh",
31-
"scriptPubKey": "0014d591091b8074a2375ed9985a9c4b18efecfd4165",
32-
"amount": 0.01463400,
33-
"confirmations": 1392,
46+
"txid": "9c1f8e701638cf58c8430f8ffd30c5235948268ecabb6af07d0dc378fff29d9e",
47+
"vout": 478,
48+
"address": "tb1qqgep7ahfekud8fg4nu9h80llxm6a33g00w3k6h",
49+
"label": "",
50+
"scriptPubKey": "001402321f76e9cdb8d3a5159f0b73bfff36f5d8c50f",
51+
"amount": 0.00233627,
52+
"confirmations": 5,
3453
"spendable": true,
3554
"solvable": true,
36-
"desc": "wpkh([d6043800/0'/1'/12']02883bb5463e37d55252d8b3d5c2141b007b37c8a7db6211f75c955acc5ea325eb)#cjr03mru",
55+
"desc": "wpkh([d2d90038/84h/1h/0h/0/0]02323fe5e590877ec4308c3f19017498b5c1dcb39b4072a3204ddea0aad1b8d7f4)#7ln5e0qc",
56+
"parent_descs": [
57+
"wpkh([d2d90038/84h/1h/0h]tpubDD6bEYfJAjaN62Ndw8h39GLSkJ4JhWw8DBMKjJezKfB95A7JcWr2DZSRix5YrNwbU8fcjgDLy2ecNp6QT6E6h4QK9TFpqnJ6qkuUwyqfDhL/0/*)#kjjtltgy"
58+
],
3759
"safe": true
3860
}
3961
]
@@ -45,31 +67,35 @@ $ changeaddress=$(bitcoin-cli getrawchangeaddress)
4567

4668
## Write A Raw Transaction
4769

48-
You can now write a new rawtransaction with two outputs: one is your change address to get back (most of) your money, the other is a data address, which is the `bitcoin-cli` term for an OP_RETURN.
70+
You can now write a new rawtransaction with two outputs: one is your
71+
change address to get back (most of) your money, the other is a data
72+
address, which is the `bitcoin-cli` term for an OP_RETURN.
73+
4974
```
50-
rawtxhex=$(bitcoin-cli -named createrawtransaction inputs='''[ { "txid": "'$utxo_txid'", "vout": '$utxo_vout' } ]''' outputs='''{ "data": "'$op_return_data'", "'$changeaddress'": 0.0146 }''')
75+
rawtxhex=$(bitcoin-cli -named createrawtransaction inputs='''[ { "txid": "'$utxo_txid'", "vout": '$utxo_vout' } ]''' outputs='''{ "data": "'$op_return_data'", "'$changeaddress'": 0.00233 }''')
5176
```
5277

5378

5479
Here's what that transaction actually looks like:
5580
```
81+
$ bitcoin-cli -named decoderawtransaction hexstring=$rawtxhex
5682
{
57-
"txid": "a600148ac3b05f0c774b8687a71c545077ea5dfb9677e5c6d708215053d892e8",
58-
"hash": "a600148ac3b05f0c774b8687a71c545077ea5dfb9677e5c6d708215053d892e8",
83+
"txid": "56af5070c97995ac30ad69770ed0f2afbc25daedb12fe20a79216f50801618b1",
84+
"hash": "56af5070c97995ac30ad69770ed0f2afbc25daedb12fe20a79216f50801618b1",
5985
"version": 2,
6086
"size": 125,
6187
"vsize": 125,
6288
"weight": 500,
6389
"locktime": 0,
6490
"vin": [
6591
{
66-
"txid": "854a833b667049ac811b4cf1cad40fa7f8dce8b0f4c1018a58b84559b6e05f42",
67-
"vout": 0,
92+
"txid": "9c1f8e701638cf58c8430f8ffd30c5235948268ecabb6af07d0dc378fff29d9e",
93+
"vout": 478,
6894
"scriptSig": {
6995
"asm": "",
7096
"hex": ""
7197
},
72-
"sequence": 4294967295
98+
"sequence": 4294967293
7399
}
74100
],
75101
"vout": [
@@ -78,48 +104,76 @@ Here's what that transaction actually looks like:
78104
"n": 0,
79105
"scriptPubKey": {
80106
"asm": "OP_RETURN b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75",
107+
"desc": "raw(6a20b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75)#nvcr5fqt",
81108
"hex": "6a20b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75",
82109
"type": "nulldata"
83110
}
84111
},
85112
{
86-
"value": 0.01460000,
113+
"value": 0.00233000,
87114
"n": 1,
88115
"scriptPubKey": {
89-
"asm": "0 998a9b0ed076bbdec1d88da4f475b9dde75e3620",
90-
"hex": "0014998a9b0ed076bbdec1d88da4f475b9dde75e3620",
91-
"reqSigs": 1,
92-
"type": "witness_v0_keyhash",
93-
"addresses": [
94-
"tb1qnx9fkrksw6aaaswc3kj0gademhn4ud3q7cz4fm"
95-
]
116+
"asm": "0 b6dee3adaf8e3a7fd51645d0c76ae5c914fb2604",
117+
"desc": "addr(tb1qkm0w8td03ca8l4gkghgvw6h9ey20kfsyreqwny)#53rfascv",
118+
"hex": "0014b6dee3adaf8e3a7fd51645d0c76ae5c914fb2604",
119+
"address": "tb1qkm0w8td03ca8l4gkghgvw6h9ey20kfsyreqwny",
120+
"type": "witness_v0_keyhash"
96121
}
97122
}
98123
]
99124
}
100125
101126
```
102-
As you can see, this sends the majority of the money straight back to the change address (`tb1qnx9fkrksw6aaaswc3kj0gademhn4ud3q7cz4fm`) minus a small transaction fee. More importantly, the first output shows an OP_RETURN with the data (`b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75`) right after it.
127+
128+
As you can see, this sends the majority of the money straight back to
129+
the change address (`tb1qkm0w8td03ca8l4gkghgvw6h9ey20kfsyreqwny`)
130+
minus a small transaction fee. More importantly, the first output
131+
shows an OP_RETURN with the data
132+
(`b9f81a8919e5aba39aeb86145c684010e6e559b580a85003ae25d78237a12e75`)
133+
right after it.
103134

104135
## Send A Raw Transaction
105136

106-
Sign your raw transaction and send it, and soon that OP_RETURN will be embedded in the blockchain!
137+
Sign your raw transaction and send it, and soon that OP_RETURN will be
138+
embedded in the blockchain!
139+
140+
```
141+
signedtx=$(bitcoin-cli -named signrawtransactionwithwallet hexstring=$rawtxhex | jq -r \
142+
'.hex')
143+
$ bitcoin-cli -named sendrawtransaction hexstring=$signedtx
144+
56af5070c97995ac30ad69770ed0f2afbc25daedb12fe20a79216f50801618b1
145+
```
107146

108147
## Check Your OP_RETURN
109148

110149
Again, remember that you can look at this transaction using a blockchain explorer:
111-
[https://live.blockcypher.com/btc-testnet/tx/a600148ac3b05f0c774b8687a71c545077ea5dfb9677e5c6d708215053d892e8/](https://live.blockcypher.com/btc-testnet/tx/a600148ac3b05f0c774b8687a71c545077ea5dfb9677e5c6d708215053d892e8/)
150+
[https://mempool.space/signet/tx/56af5070c97995ac30ad69770ed0f2afbc25daedb12fe20a79216f50801618b1?showDetails=true](https://mempool.space/signet/tx/56af5070c97995ac30ad69770ed0f2afbc25daedb12fe20a79216f50801618b1?showDetails=true).
112151

113-
You may note a warning about the data being in an "unknown protocol". If you were designing some regular use of `OP_RETURN` data, you'd probably mark it with a special prefix, to mark that protocol. Then, the actual OP_RETURN data might be something like "CONTRACTS3b110a164aa18d3a5ab064ba93fdce62". This example didn't use a prefix to avoid muddying the data space.
152+
Some explors may note that the data is in an "unknown protocol". If
153+
you were designing some regular use of `OP_RETURN` data, you'd
154+
probably mark it with a special prefix, to mark that protocol. Then,
155+
the actual OP_RETURN data might be something like
156+
"CONTRACTS3b110a164aa18d3a5ab064ba93fdce62". This example didn't use a
157+
prefix to avoid muddying the data space.
114158

115159
## Summary: Sending a Transaction with Data
116160

117-
You can use an `OP_RETURN` opcode to store up to 80 bytes of data on the blockchain. You do this with the `data` codeword for a `vout`. You still have to send money along too, but you just send it back to a change address, minus a transaction fee.
118-
119-
> :fire: _What is the Power of OP_RETURN?_ The OP_RETURN opens up whole new possibilities for the blockchain, because you can embed data that proves that certain things happened at certain times. Various organizations have used OP_RETURNs for proof of existence, for copyright, for colored coins, and [for other purposes](https://en.bitcoin.it/wiki/OP_RETURN). Though 80 bytes might not seem a lot, it can be quite effective if OP_RETURNs are used to store hashes of the actual data. Then, you can prove the existence of your digital data by demonstrating that the hash of it matches the hash on the blockchain.
120-
121-
Note that there is some controversy over using the Bitcoin blockchain in this way.
161+
You can use an `OP_RETURN` opcode to store up to 80 bytes of data on
162+
the blockchain. You do this with the `data` codeword for a `vout`. You
163+
still have to send money along too, but you just send it back to a
164+
change address, minus a transaction fee.
165+
166+
> 🔥 _What is the Power of OP_RETURN?_ The OP_RETURN opens up whole
167+
new possibilities for the blockchain, because you can embed data that
168+
proves that certain things happened at certain times. Various
169+
organizations have used OP_RETURNs for proof of existence, for
170+
copyright, for colored coins, and for other purposes. But beware of
171+
the possibility for pushback when you use OP_RETURN, and so if you use
172+
it, do so responsibly, ideally embedding small amounts of data that
173+
can be used as commitments rather than larger data dumps. This will
174+
still support proof of existence and timestamping.
122175

123176
## What's Next?
124177

125-
Move on to "Bitcoin Scripting" with [Chapter Nine: Introducing Bitcoin Scripts](09_0_Introducing_Bitcoin_Scripts.md).
178+
Move on to "Importing & Exporting Your Secrets" with [Chapter Ten:
179+
Creating Bitcoin Secrets](10_0_Creating_Bitcoin_Secrets.md)

docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ complete state.</i>
5353
* [8.2: Understanding the PSBT](08_2_Understanding_the_PSBT.md)
5454
* [8.3: Using a Partially Signed Bitcoin Transaction](08_3_Using_a_Partially_Signed_Bitcoin_Transaction.md)
5555
* [8.4: Creating Animated QR Codes](08_4_Creating_Animated_QR_Codes.md)
56+
* [**9.0: Expanding Bitcoin Transactions in Other Ways**](docs/09_0_Expanding_Bitcoin_Transactions_Other.md)
57+
* [9.1: Sending a Transaction with a Locktime](docs/09_1_Sending_a_Transaction_with_a_Locktime.md)
58+
* [9.2: Sending a Transaction with Data](docs/09_2_Sending_a_Transaction_with_Data.md)
5659

5760
---
5861

0 commit comments

Comments
 (0)