Skip to content

Commit bd008ad

Browse files
committed
Merge #31: fix: correct links and mentions to the v0.32.0 bitcoin
586a71c fix: secp256k1 links (Jose Storopoli) 95f1401 fix: some inconsistencies in `tx_taproot.md` (Jose Storopoli) 1b6f20f fix: correct links and mentions to the v0.32.0 bitcoin (Jose Storopoli) Pull request description: In the `Cargo.toml` of the tests we are already using `0.32.0`. Hence, the doc links and mentions from `0.31.1` should be updated. PS: I've added a minor inconsistency fix in [95f1401](95f1401) ACKs for top commit: apoelstra: utACK 586a71c tcharding: ACK 586a71c Tree-SHA512: 290a63d2933995aa4f8dcb8c0cdf122ea9752061bb19430662b472005e32b8b5ccf3b169049c65328e4fddef78c93da3d90c26b792abeffbbc4ee30efc79684e
2 parents c3f0b1c + 586a71c commit bd008ad

File tree

4 files changed

+87
-87
lines changed

4 files changed

+87
-87
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ find it missing please consider contributing an example or raising an issue.
1010

1111
## Current rust-bitcoin version
1212

13-
This cookbook is currently based on [rust-bitcoin `v0.31.1`](https://docs.rs/bitcoin/0.31.1/bitcoin/index.html)
13+
This cookbook is currently based on [rust-bitcoin `v0.32.0`](https://docs.rs/bitcoin/0.32.0/bitcoin/index.html)
1414

1515
## License
1616

cookbook/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Welcome to the `rust-bitcoin` cookbook!
66
This cookbook is designed to provide readers with a
77
comprehensive understanding of `rust-bitcoin` and its key features.
88

9-
Cookbook currently depends on `rust-bitcoin` version [`v0.31.1`](https://docs.rs/bitcoin/0.31.1/bitcoin/index.html).
9+
Cookbook currently depends on `rust-bitcoin` version [`v0.32.0`](https://docs.rs/bitcoin/0.32.0/bitcoin/index.html).
1010
This may not be the [latest available version](https://docs.rs/bitcoin) of `rust-bitcoin`.
1111

1212
## How to contribute

cookbook/src/tx_segwit-v0.md

+39-39
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ In a real application these would be actual secrets[^secp].
6666
We use the `SecretKey::new` method to generate a random private key `sk`.
6767
We then use the `PublicKey::new` method to derive the corresponding public key `pk`.
6868
Finally, we use the `PublicKey::wpubkey_hash` method to derive the corresponding public key hash `wpkh`.
69-
Note that `senders_keys` is generic over the [`Signing`](https://docs.rs/secp256k1/0.27.0/secp256k1/trait.Signing.html) trait.
69+
Note that `senders_keys` is generic over the [`Signing`](https://docs.rs/secp256k1/0.29.0/secp256k1/trait.Signing.html) trait.
7070
This is used to indicate that is an instance of `Secp256k1` and can be used for signing.
7171
We conclude returning the private key `sk` and the public key hash `wpkh` as a tuple.
7272

@@ -110,7 +110,7 @@ fn dummy_unspent_transaction_output(wpkh: &WPubkeyHash) -> (OutPoint, TxOut) {
110110
`dummy_unspent_transaction_output` generates a dummy unspent transaction output (UTXO).
111111
This is a SegWit V0 P2WPKH (`ScriptBuf::new_p2wpkh`) UTXO with a dummy invalid transaction ID (`txid: Txid::all_zeros()`),
112112
and a value of the `const DUMMY_UTXO_AMOUNT` that we defined earlier.
113-
We are using the [`OutPoint`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.OutPoint.html) struct to represent the transaction output.
113+
We are using the [`OutPoint`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.OutPoint.html) struct to represent the transaction output.
114114
Finally, we return the tuple `(out_point, utxo)`.
115115

116116
Now we are ready for our main function that will sign a transaction that spends a `p2wpkh` unspent output:
@@ -228,7 +228,7 @@ Let's go over the main function code block by block.
228228

229229
`let secp = Secp256k1::new();` creates a new `Secp256k1` context with all capabilities.
230230
Since we added the `rand-std` feature to our `Cargo.toml`,
231-
we can use the [`SecretKey::new`](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.Secp256k1.html#method.new) method to generate a random private key `sk`.
231+
we can use the [`SecretKey::new`](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.Secp256k1.html#method.new) method to generate a random private key `sk`.
232232

233233
`let (sk, wpkh) = senders_keys(&secp);` generates a random private key `sk` and derives the corresponding public key hash `wpkh`.
234234
`let address = receivers_address();` generates a receiver's address `address`.
@@ -237,93 +237,93 @@ All of these are helper functions that we defined earlier.
237237

238238
`let script_code = dummy_utxo.script_pubkey.p2wpkh_script_code().expect("valid script");`
239239
creates the script code required to spend a P2WPKH output.
240-
Since `dummy_utxo` is a [`TxOut`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.TxOut.html) type,
241-
we can access the underlying public field `script_pubkey` which, in turn is a [`Script`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.Script.html) type.
242-
We then use the [`p2wpkh_script_code`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.ScriptBuf.html#method.p2wpkh_script_code) method to generate the script code.
240+
Since `dummy_utxo` is a [`TxOut`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.TxOut.html) type,
241+
we can access the underlying public field `script_pubkey` which, in turn is a [`Script`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.Script.html) type.
242+
We then use the [`p2wpkh_script_code`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.ScriptBuf.html#method.p2wpkh_script_code) method to generate the script code.
243243

244244
In `let input = TxIn {...}` we are instantiating the input for the transaction we are constructing
245-
Inside the [`TxIn`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.TxIn.html) struct we are setting the following fields:
245+
Inside the [`TxIn`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.TxIn.html) struct we are setting the following fields:
246246

247-
- `previous_output` is the outpoint of the dummy UTXO we are spending; it is a [`OutPoint`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.OutPoint.html) type.
248-
- `script_sig` is the script code required to spend a P2WPKH output; it is a [`ScriptBuf`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.ScriptBuf.html) type.
247+
- `previous_output` is the outpoint of the dummy UTXO we are spending; it is a [`OutPoint`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.OutPoint.html) type.
248+
- `script_sig` is the script code required to spend a P2WPKH output; it is a [`ScriptBuf`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.ScriptBuf.html) type.
249249
It should be empty. That's why the `ScriptBuf::new()`.
250-
- `sequence` is the sequence number; it is a [`Sequence`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.Sequence.html) type.
251-
We are using the [`ENABLE_RBF_NO_LOCKTIME`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.Sequence.html#associatedconstant.ENABLE_RBF_NO_LOCKTIME) constant.
252-
- `witness` is the witness stack; it is a [`Witness`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/witness/struct.Witness.html) type.
253-
We are using the [`default`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/witness/struct.Witness.html#impl-Default) method to create an empty witness that will be filled in later after signing.
250+
- `sequence` is the sequence number; it is a [`Sequence`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.Sequence.html) type.
251+
We are using the [`ENABLE_RBF_NO_LOCKTIME`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.Sequence.html#associatedconstant.ENABLE_RBF_NO_LOCKTIME) constant.
252+
- `witness` is the witness stack; it is a [`Witness`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/witness/struct.Witness.html) type.
253+
We are using the [`default`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/witness/struct.Witness.html#impl-Default) method to create an empty witness that will be filled in later after signing.
254254
This is possible because `Witness` implements the [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) trait.
255255

256256
In `let spend = TxOut {...}` we are instantiating the spend output.
257-
Inside the [`TxOut`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.TxOut.html) struct we are setting the following fields:
257+
Inside the [`TxOut`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.TxOut.html) struct we are setting the following fields:
258258

259259
- `value` is the amount we are spending; it is a [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) type.
260260
We are using the `const SPEND_AMOUNT` that we defined earlier.
261-
- `script_pubkey` is the script code required to spend a P2WPKH output; it is a [`ScriptBuf`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.ScriptBuf.html) type.
262-
We are using the [`script_pubkey`](https://docs.rs/bitcoin/0.31.1/bitcoin/address/struct.Address.html#method.script_pubkey) method to generate the script pubkey from the receivers address.
261+
- `script_pubkey` is the script code required to spend a P2WPKH output; it is a [`ScriptBuf`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.ScriptBuf.html) type.
262+
We are using the [`script_pubkey`](https://docs.rs/bitcoin/0.32.0/bitcoin/address/struct.Address.html#method.script_pubkey) method to generate the script pubkey from the receivers address.
263263
This will lock the output to the receiver's address.
264264

265265
In `let change = TxOut {...}` we are instantiating the change output.
266266
It is very similar to the `spend` output, but we are now using the `const CHANGE_AMOUNT` that we defined earlier[^spend].
267-
This is done by setting the `script_pubkey` field to [`ScriptBuf::new_p2wpkh(&wpkh)`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.ScriptBuf.html#method.new_p2wpkh),
267+
This is done by setting the `script_pubkey` field to [`ScriptBuf::new_p2wpkh(&wpkh)`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.ScriptBuf.html#method.new_p2wpkh),
268268
which generates P2WPKH-type of script pubkey.
269269

270-
In `let unsigned_tx = Transaction {...}` we are instantiating the transaction we want to sign and broadcast using the [`Transaction`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.Transaction.html) struct.
270+
In `let unsigned_tx = Transaction {...}` we are instantiating the transaction we want to sign and broadcast using the [`Transaction`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.Transaction.html) struct.
271271
We set the following fields:
272272

273273
- `version` is the transaction version; it is a [`i32`](https://doc.rust-lang.org/std/primitive.u32.html) type.
274274
We are using version `2` which means that [BIP68](https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki) applies.
275275
- `lock_time` is the transaction lock time;
276-
it is a [`LockTime`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/locktime/absolute/enum.LockTime.html) enum.
277-
We are using the constant [`ZERO`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/locktime/absolute/enum.LockTime.html#associatedconstant.ZERO)
276+
it is a [`LockTime`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/locktime/absolute/enum.LockTime.html) enum.
277+
We are using the constant [`ZERO`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/locktime/absolute/enum.LockTime.html#associatedconstant.ZERO)
278278
This will make the transaction valid immediately.
279279
- `input` is the input vector; it is a [`Vec<TxIn>`](https://doc.rust-lang.org/std/vec/struct.Vec.html) type.
280280
We are using the `input` variable that we defined earlier wrapped in the [`vec!`](https://doc.rust-lang.org/std/macro.vec.html) macro for convenient initialization.
281281
- `output` is the output vector; it is a [`Vec<TxOut>`](https://doc.rust-lang.org/std/vec/struct.Vec.html) type.
282282
We are using the `spend` and `change` variables that we defined earlier wrapped in the [`vec!`](https://doc.rust-lang.org/std/macro.vec.html) macro for convenient initialization.
283283

284-
In `let mut sighash_cache = SighashCache::new(unsigned_tx);` we are instantiating a [`SighashCache`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/struct.SighashCache.html) struct.
284+
In `let mut sighash_cache = SighashCache::new(unsigned_tx);` we are instantiating a [`SighashCache`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/struct.SighashCache.html) struct.
285285
This is a type that efficiently calculates [signature hash message](https://developer.bitcoin.org/devguide/transactions.html?highlight=sighash_all#signature-hash-types) for legacy, segwit and taproot inputs.
286286
We are using the `new` method to instantiate the struct with the `unsigned_tx` that we defined earlier.
287287
`new` takes any `Borrow<Transaction>` as an argument.
288288
[`Borrow<T>`](https://doc.rust-lang.org/std/borrow/trait.Borrow.html) is a trait that allows us to pass either a reference to a `T` or a `T` itself.
289289
Hence, you can pass a `Transaction` or a `&Transaction` to `new`.
290290

291-
`sighash_cache` is instantiated as mutable because we require a mutable reference when creating the sighash to sign using [`segwit_signature_hash`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/struct.SighashCache.html#method.segwit_signature_hash).
291+
`sighash_cache` is instantiated as mutable because we require a mutable reference when creating the sighash to sign using [`segwit_signature_hash`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/struct.SighashCache.html#method.segwit_signature_hash).
292292
This computes the [BIP143](https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki) sighash for any flag type.
293293
It takes the following arguments:
294294

295295
- `input_index` is the index of the input we are signing; it is a [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) type.
296296
We are using `0` since we only have one input.
297-
- `script_code` is the script code required to spend a P2WPKH output; it is a reference to [`Script`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/script/struct.Script.html) type.
297+
- `script_code` is the script code required to spend a P2WPKH output; it is a reference to [`Script`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/script/struct.Script.html) type.
298298
We are using the `script_code` variable that we defined earlier.
299299
- `value` is the amount of the UTXO we are spending; it is a [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) type.
300300
We are using the `const DUMMY_UTXO_AMOUNT` that we defined earlier.
301-
- `sighash_type` is the type of sighash; it is a [`EcdsaSighashType`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/enum.EcdsaSighashType.html) enum.
302-
We are using the [`All`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/enum.EcdsaSighashType.html#variant.All) variant,
301+
- `sighash_type` is the type of sighash; it is a [`EcdsaSighashType`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/enum.EcdsaSighashType.html) enum.
302+
We are using the [`All`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/enum.EcdsaSighashType.html#variant.All) variant,
303303
which indicates that the sighash will include all the inputs and outputs.
304304

305-
We create the message `msg` by converting the `sighash` to a [`Message`](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.Message.html) type.
305+
We create the message `msg` by converting the `sighash` to a [`Message`](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.Message.html) type.
306306
This is the message that we will sign.
307-
The [Message::from](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.Message.html#impl-From%3C%26%27_%20bitcoin%3A%3Ahashes%3A%3Asha256d%3A%3AHash%3E) method takes anything that implements the promises to be a thirty two byte hash i.e., 32 bytes that came from a cryptographically secure hashing algorithm.
307+
The [Message::from](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.Message.html#impl-From%3C%26%27_%20bitcoin%3A%3Ahashes%3A%3Asha256d%3A%3AHash%3E) method takes anything that implements the promises to be a thirty two byte hash i.e., 32 bytes that came from a cryptographically secure hashing algorithm.
308308

309-
We compute the signature `sig` by using the [`sign_ecdsa`](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.Secp256k1.html#method.sign_ecdsa) method.
310-
It takes a refence to a [`Message`](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.Message.html) and a reference to a [`SecretKey`](https://docs.rs/secp256k1/0.27.0/secp256k1/struct.SecretKey.html) as arguments,
311-
and returns a [`Signature`](https://docs.rs/secp256k1/0.27.0/secp256k1/ecdsa/struct.Signature.html) type.
309+
We compute the signature `sig` by using the [`sign_ecdsa`](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.Secp256k1.html#method.sign_ecdsa) method.
310+
It takes a refence to a [`Message`](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.Message.html) and a reference to a [`SecretKey`](https://docs.rs/secp256k1/0.29.0/secp256k1/struct.SecretKey.html) as arguments,
311+
and returns a [`Signature`](https://docs.rs/secp256k1/0.29.0/secp256k1/ecdsa/struct.Signature.html) type.
312312

313-
In the next step, we update the witness stack for the input we just signed by first converting the `sighash_cache` into a [`Transaction`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/transaction/struct.Transaction.html)
314-
by using the [`into_transaction`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/struct.SighashCache.html#method.into_transaction) method.
313+
In the next step, we update the witness stack for the input we just signed by first converting the `sighash_cache` into a [`Transaction`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/transaction/struct.Transaction.html)
314+
by using the [`into_transaction`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/struct.SighashCache.html#method.into_transaction) method.
315315
We access the witness field of the first input with `tx.input[0].witness`.
316-
It is a [`Witness`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/witness/struct.Witness.html) type.
317-
We use the [`push_bitcoin_signature`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/witness/struct.Witness.html#method.push_bitcoin_signature) method.
316+
It is a [`Witness`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/witness/struct.Witness.html) type.
317+
We use the [`push_bitcoin_signature`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/witness/struct.Witness.html#method.push_bitcoin_signature) method.
318318
It expects two arguments:
319319

320-
1. A reference to a [`SerializedSignature`](https://docs.rs/secp256k1/0.27.0/secp256k1/ecdsa/serialized_signature/struct.SerializedSignature.html) type.
321-
This is accomplished by calling the [`serialize_der`](https://docs.rs/secp256k1/0.27.0/secp256k1/ecdsa/struct.Signature.html#method.serialize_der) method on the `Signature` `sig`,
320+
1. A reference to a [`SerializedSignature`](https://docs.rs/secp256k1/0.29.0/secp256k1/ecdsa/serialized_signature/struct.SerializedSignature.html) type.
321+
This is accomplished by calling the [`serialize_der`](https://docs.rs/secp256k1/0.29.0/secp256k1/ecdsa/struct.Signature.html#method.serialize_der) method on the `Signature` `sig`,
322322
which returns a `SerializedSignature` type.
323-
1. A [`EcdsaSighashType`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/enum.EcdsaSighashType.html) enum.
324-
Again we are using the same [`All`](https://docs.rs/bitcoin/0.31.1/bitcoin/sighash/enum.EcdsaSighashType.html#variant.All) variant that we used earlier.
323+
1. A [`EcdsaSighashType`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/enum.EcdsaSighashType.html) enum.
324+
Again we are using the same [`All`](https://docs.rs/bitcoin/0.32.0/bitcoin/sighash/enum.EcdsaSighashType.html#variant.All) variant that we used earlier.
325325

326-
We repeat the same step as above, but now using the [`push`](https://docs.rs/bitcoin/0.31.1/bitcoin/blockdata/witness/struct.Witness.html#method.push) method
326+
We repeat the same step as above, but now using the [`push`](https://docs.rs/bitcoin/0.32.0/bitcoin/blockdata/witness/struct.Witness.html#method.push) method
327327
to push the serialized public key to the witness stack.
328328
It expects a single argument of type `AsRef<[u8]>` which is a reference to a byte slice.
329329

0 commit comments

Comments
 (0)