Skip to content

Commit

Permalink
Add lock time types
Browse files Browse the repository at this point in the history
Add a `LockTime` type to hold the nLockTime `u32` value. Use it in
`Transaction` for `lock_time` instead of a `u32`. Make it public so this
new type can be used by rust-miniscript and other downstream projects.

Add a `PackedLockTime` type that wraps a raw `u32` and derives `Ord`,
this type is for wrapping a consensus lock time value for nesting in
types that would like to derive `Ord`.
  • Loading branch information
tcharding committed Jul 22, 2022
1 parent 275587d commit 58c1afe
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 27 deletions.
6 changes: 3 additions & 3 deletions examples/ecdsa-psbt.rs
Expand Up @@ -43,8 +43,8 @@ use bitcoin::util::bip32::{
};
use bitcoin::util::psbt::{self, Input, Psbt, PsbtSighashType};
use bitcoin::{
Address, Amount, Network, OutPoint, PrivateKey, PublicKey, Script, Sequence, Transaction, TxIn,
TxOut, Txid, Witness,
Address, Amount, Network, OutPoint, PackedLockTime, PrivateKey, PublicKey, Script, Sequence,
Transaction, TxIn, TxOut, Txid, Witness,
};

use self::psbt_sign::*;
Expand Down Expand Up @@ -207,7 +207,7 @@ impl WatchOnly {

let tx = Transaction {
version: 2,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![TxIn {
previous_output: OutPoint {
txid: Txid::from_hex(INPUT_UTXO_TXID)?,
Expand Down
6 changes: 4 additions & 2 deletions src/blockdata/constants.rs
Expand Up @@ -16,6 +16,7 @@ use crate::hashes::hex::{self, HexIterator};
use crate::hashes::{Hash, sha256d};
use crate::blockdata::opcodes;
use crate::blockdata::script;
use crate::blockdata::locktime::PackedLockTime;
use crate::blockdata::transaction::{OutPoint, Transaction, TxOut, TxIn, Sequence};
use crate::blockdata::block::{Block, BlockHeader};
use crate::blockdata::witness::Witness;
Expand Down Expand Up @@ -73,7 +74,7 @@ fn bitcoin_genesis_tx() -> Transaction {
// Base
let mut ret = Transaction {
version: 1,
lock_time: 0,
lock_time: PackedLockTime::ZERO,
input: vec![],
output: vec![],
};
Expand Down Expand Up @@ -201,6 +202,7 @@ mod test {
use crate::hashes::hex::{ToHex, FromHex};
use crate::network::constants::Network;
use crate::consensus::encode::serialize;
use crate::blockdata::locktime::PackedLockTime;

#[test]
fn bitcoin_genesis_first_transaction() {
Expand All @@ -218,7 +220,7 @@ mod test {
assert_eq!(serialize(&gen.output[0].script_pubkey),
Vec::from_hex("434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac").unwrap());
assert_eq!(gen.output[0].value, 50 * COIN_VALUE);
assert_eq!(gen.lock_time, 0);
assert_eq!(gen.lock_time, PackedLockTime::ZERO);

assert_eq!(gen.wtxid().to_hex(), "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b");
}
Expand Down

0 comments on commit 58c1afe

Please sign in to comment.