From 3921899c65333f673ed4410daa915d4fa8458b11 Mon Sep 17 00:00:00 2001 From: Tamas Blummer Date: Thu, 17 May 2018 13:11:28 +0200 Subject: [PATCH] add is_coin_base add data access as copy --- src/blockdata/script.rs | 3 +++ src/blockdata/transaction.rs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index ea1eea7c..c8298a71 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -291,6 +291,9 @@ impl Script { /// Convert the script into a byte vector pub fn into_vec(self) -> Vec { self.0.into_vec() } + /// returns a copy of the script data + pub fn data (&self) -> Vec { self.0.clone().into_vec() } + /// Compute the P2SH output corresponding to this redeem script pub fn to_p2sh(&self) -> Script { Builder::new().push_opcode(opcodes::All::OP_HASH160) diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index ab37108a..d441d53b 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -272,6 +272,11 @@ impl Transaction { Err(script::Error::SerializationError) } } + + /// is this a coin base transaction? + pub fn is_coin_base (&self) -> bool { + self.input.len() == 1 && self.input[0].prev_index == 0xFFFFFFFF && self.input [0].prev_hash == Sha256dHash::default() + } } impl BitcoinHash for Transaction { @@ -453,6 +458,18 @@ mod tests { assert!(txin.is_ok()); } + #[test] + fn test_is_coinbase () { + use network::constants::Network; + use blockdata::constants; + + let genesis = constants::genesis_block(Network::Bitcoin); + assert! (genesis.txdata[0].is_coin_base()); + let hex_tx = hex_bytes("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap(); + let tx: Transaction = deserialize(&hex_tx).unwrap(); + assert!(!tx.is_coin_base()); + } + #[test] fn test_transaction() { let hex_tx = hex_bytes("0100000001a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece010000006c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52ffffffff0100e1f505000000001976a9140389035a9225b3839e2bbf32d826a1e222031fd888ac00000000").unwrap();