diff --git a/src/constants.rs b/src/constants.rs index 1946d20..fe6c6d2 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -5,7 +5,6 @@ pub const MAX_METADATA_BYTES: usize = 800; pub const TX_HASH_LENGTH: usize = 32; /*------- ADDRESS CONSTANTS -------*/ -pub const V0_ADDRESS_LENGTH: usize = 16; pub const STANDARD_ADDRESS_LENGTH: usize = 64; // Prepending character for a P2SH address pub const P2SH_PREPEND: u8 = b'H'; @@ -14,11 +13,6 @@ pub const P2SH_PREPEND: u8 = b'H'; // Current network version: Always bump immediately after a version is deployed. pub const NETWORK_VERSION: u32 = 6; pub const NETWORK_VERSION_SERIALIZED: &[u8] = b"6"; -// Network version 0 -pub const NETWORK_VERSION_V0: u64 = 0; -// Network version to support temporary address structure on wallet -// TODO: Deprecate after addresses retire -pub const NETWORK_VERSION_TEMP: u64 = 99999; /*------- VALUE HANDLING CONSTANTS --------*/ // Number of decimal places to divide to in display @@ -229,8 +223,6 @@ pub const OPWITHIN_DESC: &str = "Substitutes the three numbers on top of the the // crypto pub const OPSHA3: &str = "OP_SHA3"; pub const OPHASH256: &str = "OP_HASH256"; -pub const OPHASH256V0: &str = "OP_HASH256_V0"; -pub const OPHASH256TEMP: &str = "OP_HASH256_TEMP"; pub const OPCHECKSIG: &str = "OP_CHECKSIG"; pub const OPCHECKSIGVERIFY: &str = "OP_CHECKSIGVERIFY"; pub const OPCHECKMULTISIG: &str = "OP_CHECKMULTISIG"; @@ -239,10 +231,6 @@ pub const OPCHECKMULTISIGVERIFY: &str = "OP_CHECKMULTISIGVERIFY"; pub const OPSHA3_DESC: &str = "Hashes the top item on the stack using SHA3-256"; pub const OPHASH256_DESC: &str = "Creates standard address from public key and pushes it onto the stack"; -pub const OPHASH256V0_DESC: &str = - "Creates v0 address from public key and pushes it onto the stack"; -pub const OPHASH256TEMP_DESC: &str = - "Creates temporary address from public key and pushes it onto the stack"; pub const OPCHECKSIG_DESC: &str = "Pushes ONE onto the stack if the signature is valid, ZERO otherwise"; pub const OPCHECKSIGVERIFY_DESC: &str = "Runs OP_CHECKSIG and OP_VERIFY in sequence"; diff --git a/src/primitives/transaction.rs b/src/primitives/transaction.rs index fe697e0..165be6f 100644 --- a/src/primitives/transaction.rs +++ b/src/primitives/transaction.rs @@ -35,7 +35,6 @@ pub struct TxConstructor { pub previous_out: OutPoint, pub signatures: Vec, pub pub_keys: Vec, - pub address_version: Option, } /// An outpoint - a combination of a transaction hash and an index n into its vout diff --git a/src/script/interface_ops.rs b/src/script/interface_ops.rs index 7b9875b..a6222e2 100644 --- a/src/script/interface_ops.rs +++ b/src/script/interface_ops.rs @@ -8,9 +8,7 @@ use crate::primitives::transaction::*; use crate::script::lang::{ConditionStack, Script, Stack}; use crate::script::{OpCodes, StackEntry}; use crate::utils::error_utils::*; -use crate::utils::transaction_utils::{ - construct_address, construct_address_temp, construct_address_v0, -}; +use crate::utils::transaction_utils::construct_address; use bincode::de; use bincode::serialize; use bytes::Bytes; @@ -601,7 +599,7 @@ pub fn op_cat(stack: &mut Stack) -> bool { error_item_size(op); return false; } - let cat = [s1, s2].join(""); + let cat = [s1, s2].concat(); stack.push(StackEntry::Bytes(cat)) } @@ -648,6 +646,10 @@ pub fn op_substr(stack: &mut Stack) -> bool { return false; } }; + // TODO: As this was previously a hex string, the indices don't exactly correspond to what + // they did originally. However, I don't think there are any existing transactions + // on the chain which actually use this opcode, so I'm fairly confident it won't + // matter. Double-check that this is the case before merging! if n1 >= s.len() { error_item_index(op); return false; @@ -660,7 +662,7 @@ pub fn op_substr(stack: &mut Stack) -> bool { error_item_index(op); return false; } - let substr = s[n1..n1 + n2].to_string(); + let substr = s[n1..n1 + n2].to_vec(); stack.push(StackEntry::Bytes(substr)) } @@ -700,7 +702,11 @@ pub fn op_left(stack: &mut Stack) -> bool { if n >= s.len() { stack.push(StackEntry::Bytes(s)) } else { - let left = s[..n].to_string(); + // TODO: As this was previously a hex string, the indices don't exactly correspond to what + // they did originally. However, I don't think there are any existing transactions + // on the chain which actually use this opcode, so I'm fairly confident it won't + // matter. Double-check that this is the case before merging! + let left = s[..n].to_vec(); stack.push(StackEntry::Bytes(left)) } } @@ -739,9 +745,13 @@ pub fn op_right(stack: &mut Stack) -> bool { } }; if n >= s.len() { - stack.push(StackEntry::Bytes("".to_string())) + stack.push(StackEntry::Bytes(Vec::new())) } else { - let right = s[n..].to_string(); + // TODO: As this was previously a hex string, the indices don't exactly correspond to what + // they did originally. However, I don't think there are any existing transactions + // on the chain which actually use this opcode, so I'm fairly confident it won't + // matter. Double-check that this is the case before merging! + let right = s[n..].to_vec(); stack.push(StackEntry::Bytes(right)) } } @@ -756,8 +766,8 @@ pub fn op_right(stack: &mut Stack) -> bool { pub fn op_size(stack: &mut Stack) -> bool { let (op, desc) = (OPSIZE, OPSIZE_DESC); trace(op, desc); - let s = match stack.last() { - Some(StackEntry::Bytes(s)) => s, + let len = match stack.last() { + Some(StackEntry::Bytes(s)) => s.len(), Some(_) => { error_item_type(op); return false; @@ -767,7 +777,11 @@ pub fn op_size(stack: &mut Stack) -> bool { return false; } }; - stack.push(StackEntry::Num(s.len())) + // TODO: As this was previously a hex string, the length doesn't exactly correspond to what + // it did originally. However, I don't think there are any existing transactions + // on the chain which actually use this opcode, so I'm fairly confident it won't + // matter. Double-check that this is the case before merging! + stack.push(StackEntry::Num(len)) } /*---- BITWISE LOGIC OPS ----*/ @@ -1924,7 +1938,11 @@ pub fn op_sha3(stack: &mut Stack) -> bool { let data = match stack.pop() { Some(StackEntry::Signature(sig)) => sig.as_ref().to_owned(), Some(StackEntry::PubKey(pk)) => pk.as_ref().to_owned(), - Some(StackEntry::Bytes(s)) => s.as_bytes().to_owned(), + Some(StackEntry::Bytes(s)) => { + // For legacy reasons, the hashed data is the hex representation of the data rather than + // the data itself. + hex::encode(&s).as_bytes().to_owned() + }, Some(_) => { error_item_type(op); return false; @@ -1934,7 +1952,8 @@ pub fn op_sha3(stack: &mut Stack) -> bool { return false; } }; - let hash = hex::encode(sha3_256::digest(&data)); + let hash = sha3_256::digest(&data).to_vec(); + // TODO: Originally, the hash was converted back to hex! stack.push(StackEntry::Bytes(hash)) } @@ -1960,65 +1979,7 @@ pub fn op_hash256(stack: &mut Stack) -> bool { } }; let addr = construct_address(&pk); - stack.push(StackEntry::Bytes(addr)) -} - -/// OP_HASH256_V0: Creates v0 address from public key and pushes it onto the stack -/// -/// Example: OP_HASH256_V0([pk]) -> [addr_v0] -/// -/// Info: Support for old 32-byte addresses -/// -/// TODO: Deprecate after addresses retire -/// -/// ### Arguments -/// -/// * `stack` - mutable reference to the stack -pub fn op_hash256_v0(stack: &mut Stack) -> bool { - let (op, desc) = (OPHASH256V0, OPHASH256V0_DESC); - trace(op, desc); - let pk = match stack.pop() { - Some(StackEntry::PubKey(pk)) => pk, - Some(_) => { - error_item_type(op); - return false; - } - _ => { - error_num_items(op); - return false; - } - }; - let addr_v0 = construct_address_v0(&pk); - stack.push(StackEntry::Bytes(addr_v0)) -} - -/// OP_HASH256_TEMP: Creates temporary address from public key and pushes it onto the stack -/// -/// Example: OP_HASH256_TEMP([pk]) -> [addr_temp] -/// -/// Info: Support for temporary address scheme used in wallet -/// -/// TODO: Deprecate after addresses retire -/// -/// ### Arguments -/// -/// * `stack` - mutable reference to the stack -pub fn op_hash256_temp(stack: &mut Stack) -> bool { - let (op, desc) = (OPHASH256TEMP, OPHASH256TEMP_DESC); - trace(op, desc); - let pk = match stack.pop() { - Some(StackEntry::PubKey(pk)) => pk, - Some(_) => { - error_item_type(op); - return false; - } - _ => { - error_num_items(op); - return false; - } - }; - let addr_temp = construct_address_temp(&pk); - stack.push(StackEntry::Bytes(addr_temp)) + stack.push(StackEntry::Bytes(hex::decode(addr).unwrap())) } /// OP_CHECKSIG: Pushes ONE onto the stack if the signature is valid, ZERO otherwise @@ -2067,8 +2028,13 @@ pub fn op_checksig(stack: &mut Stack) -> bool { return false; } }; - trace!("Signature: {:?}", hex::encode(sig)); - if (!sign::verify_detached(&sig, msg.as_bytes(), &pk)) { + + // For legacy reasons, the signed message is the hex representation of the message rather than + // the message itself. + let msg_hex = hex::encode(msg); + + trace!("Signature: {:?}", msg_hex); + if (!sign::verify_detached(&sig, msg_hex.as_bytes(), &pk)) { trace!("Signature verification failed"); stack.push(StackEntry::Num(ZERO)) } else { @@ -2121,8 +2087,13 @@ pub fn op_checksigverify(stack: &mut Stack) -> bool { return false; } }; - trace!("Signature: {:?}", hex::encode(sig)); - if (!sign::verify_detached(&sig, msg.as_bytes(), &pk)) { + + // For legacy reasons, the signed message is the hex representation of the message rather than + // the message itself. + let msg_hex = hex::encode(msg); + + trace!("Signature: {:?}", msg_hex); + if (!sign::verify_detached(&sig, msg_hex.as_bytes(), &pk)) { trace!("Signature verification failed"); error_invalid_signature(op); return false; @@ -2298,13 +2269,17 @@ pub fn op_checkmultisigverify(stack: &mut Stack) -> bool { /// * `sigs` - signatures to verify /// * `msg` - data to verify against /// * `pks` - public keys to match against -fn verify_multisig(sigs: &[Signature], msg: &String, pks: &mut Vec) -> bool { +fn verify_multisig(sigs: &[Signature], msg: &[u8], pks: &mut Vec) -> bool { + // For legacy reasons, the signed message is the hex representation of the message rather than + // the message itself. + let msg_hex = hex::encode(msg); + let mut num_valid_sigs = ZERO; for sig in sigs { if let Some((index, _)) = pks .iter() .enumerate() - .find(|(_, pk)| sign::verify_detached(sig, msg.as_bytes(), pk)) + .find(|(_, pk)| sign::verify_detached(sig, msg_hex.as_bytes(), pk)) { num_valid_sigs += ONE; pks.remove(index); diff --git a/src/script/lang.rs b/src/script/lang.rs index 259465e..c784f4b 100644 --- a/src/script/lang.rs +++ b/src/script/lang.rs @@ -7,10 +7,9 @@ use crate::crypto::sign_ed25519::{ use crate::script::interface_ops::*; use crate::script::{OpCodes, StackEntry}; use crate::utils::error_utils::*; -use crate::utils::transaction_utils::{construct_address, construct_address_for}; +use crate::utils::transaction_utils::construct_address; use bincode::serialize; use bytes::Bytes; -use hex::encode; use serde::{Deserialize, Serialize}; use tracing::{error, warn}; @@ -312,8 +311,6 @@ impl Script { // crypto OpCodes::OP_SHA3 => test_for_return &= op_sha3(&mut stack), OpCodes::OP_HASH256 => test_for_return &= op_hash256(&mut stack), - OpCodes::OP_HASH256_V0 => test_for_return &= op_hash256_v0(&mut stack), - OpCodes::OP_HASH256_TEMP => test_for_return &= op_hash256_temp(&mut stack), OpCodes::OP_CHECKSIG => test_for_return &= op_checksig(&mut stack), OpCodes::OP_CHECKSIGVERIFY => { test_for_return &= op_checksigverify(&mut stack) @@ -375,7 +372,7 @@ impl Script { StackEntry::Op(OpCodes::OP_CREATE), StackEntry::Num(block_number as usize), StackEntry::Op(OpCodes::OP_DROP), - StackEntry::Bytes(asset_hash), + StackEntry::Bytes(hex::decode(asset_hash).expect("asset_hash contains non-hex characters")), StackEntry::Signature(signature), StackEntry::PubKey(pub_key), StackEntry::Op(OpCodes::OP_CHECKSIG), @@ -391,23 +388,18 @@ impl Script { /// * `signature` - Signature of check data /// * `pub_key` - Public key of the payer pub fn pay2pkh( - check_data: String, + check_data: Vec, signature: Signature, pub_key: PublicKey, - address_version: Option, ) -> Self { - let op_hash_256 = match address_version { - Some(NETWORK_VERSION_V0) => OpCodes::OP_HASH256_V0, - Some(NETWORK_VERSION_TEMP) => OpCodes::OP_HASH256_TEMP, - _ => OpCodes::OP_HASH256, - }; let stack = vec![ StackEntry::Bytes(check_data), StackEntry::Signature(signature), StackEntry::PubKey(pub_key), StackEntry::Op(OpCodes::OP_DUP), - StackEntry::Op(op_hash_256), - StackEntry::Bytes(construct_address_for(&pub_key, address_version)), + StackEntry::Op(OpCodes::OP_HASH256), + StackEntry::Bytes(hex::decode(construct_address(&pub_key)) + .expect("address contains non-hex characters?")), StackEntry::Op(OpCodes::OP_EQUALVERIFY), StackEntry::Op(OpCodes::OP_CHECKSIG), ]; @@ -421,7 +413,7 @@ impl Script { /// * `check_data` - Data to be signed for verification /// * `pub_key` - Public key of this party /// * `signature` - Signature of this party - pub fn member_multisig(check_data: String, pub_key: PublicKey, signature: Signature) -> Self { + pub fn member_multisig(check_data: Vec, pub_key: PublicKey, signature: Signature) -> Self { let stack = vec![ StackEntry::Bytes(check_data), StackEntry::Signature(signature), @@ -439,7 +431,7 @@ impl Script { /// * `n` - Number of valid signatures total /// * `check_data` - Data to have checked against signatures /// * `pub_keys` - The constituent public keys - pub fn multisig_lock(m: usize, n: usize, check_data: String, pub_keys: Vec) -> Self { + pub fn multisig_lock(m: usize, n: usize, check_data: Vec, pub_keys: Vec) -> Self { let mut stack = vec![StackEntry::Bytes(check_data), StackEntry::Num(m)]; stack.append(&mut pub_keys.iter().map(|e| StackEntry::PubKey(*e)).collect()); stack.push(StackEntry::Num(n)); @@ -453,7 +445,7 @@ impl Script { /// /// * `check_data` - Data to have signed /// * `signatures` - Signatures to unlock with - pub fn multisig_unlock(check_data: String, signatures: Vec) -> Self { + pub fn multisig_unlock(check_data: Vec, signatures: Vec) -> Self { let mut stack = vec![StackEntry::Bytes(check_data)]; stack.append( &mut signatures @@ -475,7 +467,7 @@ impl Script { pub fn multisig_validation( m: usize, n: usize, - check_data: String, + check_data: Vec, signatures: Vec, pub_keys: Vec, ) -> Self { diff --git a/src/script/mod.rs b/src/script/mod.rs index 3063a46..aa3223b 100644 --- a/src/script/mod.rs +++ b/src/script/mod.rs @@ -12,8 +12,9 @@ pub enum StackEntry { Op(OpCodes), Signature(Signature), PubKey(PublicKey), + // TODO: This should probably be u64, as usize doesn't have a consistent range on all platforms Num(usize), - Bytes(String), + Bytes(Vec), } /// Opcodes enum @@ -108,8 +109,6 @@ pub enum OpCodes { // crypto OP_SHA3 = 0x90, OP_HASH256 = 0x91, - OP_HASH256_V0 = 0x92, - OP_HASH256_TEMP = 0x93, OP_CHECKSIG = 0x94, OP_CHECKSIGVERIFY = 0x95, OP_CHECKMULTISIG = 0x96, @@ -127,6 +126,8 @@ pub enum OpCodes { OP_NOP8 = 0xb7, OP_NOP9 = 0xb8, OP_NOP10 = 0xb9, + OP_NOP11 = 0x92, // Formerly OP_HASH256_V0 + OP_NOP12 = 0x93, // Formerly OP_HASH256_TEMP } impl OpCodes { diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 12e150a..b6a7510 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -49,3 +49,105 @@ pub fn add_btreemap( }); m1 } + +/// A trait which indicates that it is possible to acquire a "placeholder" value +/// of a type, which can be used for test purposes. +#[cfg(test)] +pub trait Placeholder : Sized { + /// Gets a placeholder value of this type which can be used for test purposes. + fn placeholder() -> Self; + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array() -> [Self; N] { + core::array::from_fn(|_| Self::placeholder()) + } +} + +/// A trait which indicates that it is possible to acquire a "placeholder" value +/// of a type, which can be used for test purposes. These placeholder values are consistent +/// across program runs. +#[cfg(test)] +pub trait PlaceholderSeed: Sized + PartialEq { + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `seed_parts` - the parts of the seed for the placeholder value to obtain. Two placeholder + /// values generated from the same seed are guaranteed to be equal (even + /// across multiple test runs, so long as the value format doesn't change). + fn placeholder_seed_parts<'a>(seed_parts: impl IntoIterator) -> Self; + + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `seed` - the seed for the placeholder value to obtain. Two placeholder + /// values generated from the same seed are guaranteed to be equal (even + /// across multiple test runs, so long as the value format doesn't change). + fn placeholder_seed(seed: impl AsRef<[u8]>) -> Self { + Self::placeholder_seed_parts([ seed.as_ref() ]) + } + + /// Gets a dummy valid of this type which can be used for test purposes. + /// + /// This allows acquiring multiple distinct placeholder values which are still consistent + /// between runs. + /// + /// ### Arguments + /// + /// * `index` - the index of the placeholder value to obtain. Two placeholder values generated + /// from the same index are guaranteed to be equal (even across multiple test runs, + /// so long as the value format doesn't change). + fn placeholder_indexed(index: u64) -> Self { + Self::placeholder_seed_parts([ index.to_le_bytes().as_slice() ]) + } + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array_seed(seed: impl AsRef<[u8]>) -> [Self; N] { + core::array::from_fn(|n| Self::placeholder_seed_parts( + [ seed.as_ref(), &(n as u64).to_le_bytes() ] + )) + } + + /// Gets an array of placeholder values of this type which can be used for test purposes. + fn placeholder_array_indexed(base_index: u64) -> [Self; N] { + Self::placeholder_array_seed(base_index.to_le_bytes()) + } +} + +#[cfg(test)] +impl Placeholder for T { + fn placeholder() -> Self { + ::placeholder_seed_parts([]) + } +} + +/// Generates the given number of pseudorandom bytes based on the given seed. +/// +/// This is intended to be used in tests, where random but reproducible placeholder values are often +/// required. +/// +/// ### Arguments +/// +/// * `seed_parts` - the parts of the seed, which will be concatenated to form the RNG seed +#[cfg(test)] +pub fn placeholder_bytes<'a, const N: usize>( + seed_parts: impl IntoIterator +) -> [u8; N] { + // Use Shake-256 to generate an arbitrarily large number of random bytes based on the given seed. + let mut shake256 = sha3::Shake256::default(); + for slice in seed_parts { + sha3::digest::Update::update(&mut shake256, slice); + } + let mut reader = sha3::digest::ExtendableOutput::finalize_xof(shake256); + + let mut res = [0u8; N]; + sha3::digest::XofReader::read(&mut reader, &mut res); + res +} diff --git a/src/utils/script_utils.rs b/src/utils/script_utils.rs index 786ac0b..8ff008f 100644 --- a/src/utils/script_utils.rs +++ b/src/utils/script_utils.rs @@ -217,7 +217,11 @@ pub fn tx_has_valid_create_script(script: &Script, asset: &Asset) -> bool { it.next(), it.next(), ) { - if b == &asset_hash && script.interpret() { + // For legacy reasons, the hashed data is the hex representation of the data rather than + // the data itself. + let b_hex = hex::encode(b); + + if b_hex == asset_hash && script.interpret() { return true; } } @@ -233,6 +237,7 @@ pub fn tx_has_valid_create_script(script: &Script, asset: &Asset) -> bool { /// * `script` - Script to validate /// * `outpoint_hash` - Hash of the corresponding outpoint /// * `tx_out_pub_key` - Public key of the previous tx_out +// TODO: The last two operands should be converted to the corresponding types fn tx_has_valid_p2pkh_sig(script: &Script, outpoint_hash: &str, tx_out_pub_key: &str) -> bool { let mut it = script.stack.iter(); @@ -243,9 +248,7 @@ fn tx_has_valid_p2pkh_sig(script: &Script, outpoint_hash: &str, tx_out_pub_key: Some(StackEntry::Signature(_)), Some(StackEntry::PubKey(_)), Some(StackEntry::Op(OpCodes::OP_DUP)), - Some(StackEntry::Op( - OpCodes::OP_HASH256 | OpCodes::OP_HASH256_V0 | OpCodes::OP_HASH256_TEMP, - )), + Some(StackEntry::Op(OpCodes::OP_HASH256)), Some(StackEntry::Bytes(h)), Some(StackEntry::Op(OpCodes::OP_EQUALVERIFY)), Some(StackEntry::Op(OpCodes::OP_CHECKSIG)), @@ -262,7 +265,13 @@ fn tx_has_valid_p2pkh_sig(script: &Script, outpoint_hash: &str, tx_out_pub_key: it.next(), ) { debug!("b: {:?}, h: {:?}", b, h); - if h == tx_out_pub_key && b == outpoint_hash && script.interpret() { + + // For legacy reasons, the hashed data is the hex representation of the data rather than + // the data itself. + let h_hex = hex::encode(h); + let b_hex = hex::encode(b); + + if h_hex == tx_out_pub_key && b_hex == outpoint_hash && script.interpret() { return true; } } @@ -383,7 +392,7 @@ mod tests { assert_eq!(cond_stack.first_false_pos, Some(0)); /// error item type let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(String::new())); + stack.push(StackEntry::Bytes(Vec::new())); let mut cond_stack = ConditionStack::new(); let b = op_if(&mut stack, &mut cond_stack); assert!(!b); @@ -428,7 +437,7 @@ mod tests { assert_eq!(cond_stack.first_false_pos, Some(0)); /// error item type let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(String::new())); + stack.push(StackEntry::Bytes(Vec::new())); let mut cond_stack = ConditionStack::new(); let b = op_notif(&mut stack, &mut cond_stack); assert!(!b); @@ -871,7 +880,7 @@ mod tests { /// op_pick([1,"hello"]) -> fail let mut stack = Stack::new(); stack.push(StackEntry::Num(1)); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let b = op_pick(&mut stack); assert!(!b); /// op_pick([1,1]) -> fail @@ -919,7 +928,7 @@ mod tests { /// op_roll([1,"hello"]) -> fail let mut stack = Stack::new(); stack.push(StackEntry::Num(1)); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let b = op_roll(&mut stack); assert!(!b); /// op_roll([1,1]) -> fail @@ -1001,36 +1010,36 @@ mod tests { fn test_cat() { /// op_cat(["hello","world"]) -> ["helloworld"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); - stack.push(StackEntry::Bytes("world".to_string())); - let mut v: Vec = vec![StackEntry::Bytes("helloworld".to_string())]; + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); + stack.push(StackEntry::Bytes("world".as_bytes().to_vec())); + let mut v: Vec = vec![StackEntry::Bytes("helloworld".as_bytes().to_vec())]; op_cat(&mut stack); assert_eq!(stack.main_stack, v); /// op_cat(["hello",""]) -> ["hello"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); - stack.push(StackEntry::Bytes("".to_string())); - let mut v: Vec = vec![StackEntry::Bytes("hello".to_string())]; + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); + stack.push(StackEntry::Bytes("".as_bytes().to_vec())); + let mut v: Vec = vec![StackEntry::Bytes("hello".as_bytes().to_vec())]; op_cat(&mut stack); assert_eq!(stack.main_stack, v); /// op_cat(["a","a"*MAX_SCRIPT_ITEM_SIZE]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("a".to_string())); + stack.push(StackEntry::Bytes("a".as_bytes().to_vec())); let mut s = String::new(); for i in 1..=MAX_SCRIPT_ITEM_SIZE { s.push('a'); } - stack.push(StackEntry::Bytes(s.to_string())); + stack.push(StackEntry::Bytes(s.as_bytes().to_vec())); let b = op_cat(&mut stack); assert!(!b); /// op_cat(["hello"]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let b = op_cat(&mut stack); assert!(!b); /// op_cat(["hello", 1]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(1)); let b = op_cat(&mut stack); assert!(!b) @@ -1041,62 +1050,62 @@ mod tests { fn test_substr() { /// op_substr(["hello",1,2]) -> ["el"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); for i in 1..=2 { stack.push(StackEntry::Num(i)); } - let mut v: Vec = vec![StackEntry::Bytes("el".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("el".as_bytes().to_vec())]; op_substr(&mut stack); assert_eq!(stack.main_stack, v); /// op_substr(["hello",0,0]) -> [""] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); for i in 1..=2 { stack.push(StackEntry::Num(0)); } - let mut v: Vec = vec![StackEntry::Bytes("".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("".as_bytes().to_vec())]; op_substr(&mut stack); assert_eq!(stack.main_stack, v); /// op_substr(["hello",0,5]) -> ["hello"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(0)); stack.push(StackEntry::Num(5)); - let mut v: Vec = vec![StackEntry::Bytes("hello".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("hello".as_bytes().to_vec())]; op_substr(&mut stack); assert_eq!(stack.main_stack, v); /// op_substr(["hello",5,0]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(5)); stack.push(StackEntry::Num(0)); let b = op_substr(&mut stack); assert!(!b); /// op_substr(["hello",1,5]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(1)); stack.push(StackEntry::Num(5)); let b = op_substr(&mut stack); assert!(!b); /// op_substr(["hello",1]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(1)); let b = op_substr(&mut stack); assert!(!b); /// op_substr(["hello",1,usize::MAX]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(1)); stack.push(StackEntry::Num(usize::MAX)); let b = op_substr(&mut stack); assert!(!b); /// op_substr(["hello",1,""]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(1)); - stack.push(StackEntry::Bytes("".to_string())); + stack.push(StackEntry::Bytes("".as_bytes().to_vec())); let b = op_substr(&mut stack); assert!(!b) } @@ -1106,34 +1115,34 @@ mod tests { fn test_left() { /// op_left(["hello",2]) -> ["he"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(2)); - let mut v: Vec = vec![StackEntry::Bytes("he".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("he".as_bytes().to_vec())]; op_left(&mut stack); assert_eq!(stack.main_stack, v); /// op_left(["hello",0]) -> [""] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(0)); - let mut v: Vec = vec![StackEntry::Bytes("".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("".as_bytes().to_vec())]; op_left(&mut stack); assert_eq!(stack.main_stack, v); /// op_left(["hello",5]) -> ["hello"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(5)); - let mut v: Vec = vec![StackEntry::Bytes("hello".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("hello".as_bytes().to_vec())]; op_left(&mut stack); assert_eq!(stack.main_stack, v); /// op_left(["hello",""]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); - stack.push(StackEntry::Bytes("".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); + stack.push(StackEntry::Bytes("".as_bytes().to_vec())); let b = op_left(&mut stack); assert!(!b); /// op_left(["hello"]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let b = op_left(&mut stack); assert!(!b) } @@ -1143,34 +1152,34 @@ mod tests { fn test_right() { /// op_right(["hello",0]) -> ["hello"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(0)); - let mut v: Vec = vec![StackEntry::Bytes("hello".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("hello".as_bytes().to_vec())]; op_right(&mut stack); assert_eq!(stack.main_stack, v); /// op_right(["hello",2]) -> ["llo"] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(2)); - let mut v: Vec = vec![StackEntry::Bytes("llo".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("llo".as_bytes().to_vec())]; op_right(&mut stack); assert_eq!(stack.main_stack, v); /// op_right(["hello",5]) -> [""] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); stack.push(StackEntry::Num(5)); - let mut v: Vec = vec![StackEntry::Bytes("".to_string())]; + let mut v: Vec = vec![StackEntry::Bytes("".as_bytes().to_vec())]; op_right(&mut stack); assert_eq!(stack.main_stack, v); /// op_right(["hello",""]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); - stack.push(StackEntry::Bytes("".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); + stack.push(StackEntry::Bytes("".as_bytes().to_vec())); let b = op_right(&mut stack); assert!(!b); /// op_right(["hello"]) -> fail let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let b = op_right(&mut stack); assert!(!b) } @@ -1180,15 +1189,15 @@ mod tests { fn test_size() { /// op_size(["hello"]) -> ["hello",5] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); let mut v: Vec = - vec![StackEntry::Bytes("hello".to_string()), StackEntry::Num(5)]; + vec![StackEntry::Bytes("hello".as_bytes().to_vec()), StackEntry::Num(5)]; op_size(&mut stack); assert_eq!(stack.main_stack, v); /// op_size([""]) -> ["",0] let mut stack = Stack::new(); - stack.push(StackEntry::Bytes("".to_string())); - let mut v: Vec = vec![StackEntry::Bytes("".to_string()), StackEntry::Num(0)]; + stack.push(StackEntry::Bytes("".as_bytes().to_vec())); + let mut v: Vec = vec![StackEntry::Bytes("".as_bytes().to_vec()), StackEntry::Num(0)]; op_size(&mut stack); assert_eq!(stack.main_stack, v); /// op_size([1]) -> fail @@ -1287,7 +1296,7 @@ mod tests { /// op_equal(["hello","hello"]) -> [1] let mut stack = Stack::new(); for i in 1..=2 { - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); } let mut v: Vec = vec![StackEntry::Num(1)]; op_equal(&mut stack); @@ -1313,7 +1322,7 @@ mod tests { /// op_equalverify(["hello","hello"]) -> [] let mut stack = Stack::new(); for i in 1..=2 { - stack.push(StackEntry::Bytes("hello".to_string())); + stack.push(StackEntry::Bytes("hello".as_bytes().to_vec())); } let mut v: Vec = vec![]; op_equalverify(&mut stack); @@ -1922,22 +1931,22 @@ mod tests { let (pk, sk) = sign::gen_keypair(); let msg = hex::encode(vec![0, 0, 0]); let sig = sign::sign_detached(msg.as_bytes(), &sk); - let h = hex::encode(sha3_256::digest(sig.as_ref())); + let h = sha3_256::digest(sig.as_ref()).to_vec(); let mut stack = Stack::new(); stack.push(StackEntry::Signature(sig)); let mut v: Vec = vec![StackEntry::Bytes(h)]; op_sha3(&mut stack); assert_eq!(stack.main_stack, v); /// op_sha3([pk]) -> [sha3_256(pk)] - let h = hex::encode(sha3_256::digest(pk.as_ref())); + let h = sha3_256::digest(pk.as_ref()).to_vec(); let mut stack = Stack::new(); stack.push(StackEntry::PubKey(pk)); let mut v: Vec = vec![StackEntry::Bytes(h)]; op_sha3(&mut stack); assert_eq!(stack.main_stack, v); /// op_sha3(["hello"]) -> [sha3_256("hello")] - let s = "hello".to_string(); - let h = hex::encode(sha3_256::digest(s.as_bytes())); + let s = "hello".as_bytes().to_vec(); + let h = sha3_256::digest(hex::encode(&s).as_bytes()).to_vec(); let mut stack = Stack::new(); stack.push(StackEntry::Bytes(s)); let mut v: Vec = vec![StackEntry::Bytes(h)]; @@ -1961,7 +1970,7 @@ mod tests { let (pk, sk) = sign::gen_keypair(); let mut stack = Stack::new(); stack.push(StackEntry::PubKey(pk)); - let mut v: Vec = vec![StackEntry::Bytes(construct_address(&pk))]; + let mut v: Vec = vec![StackEntry::Bytes(hex::decode(construct_address(&pk)).unwrap())]; op_hash256(&mut stack); assert_eq!(stack.main_stack, v); /// op_hash256([]) -> fail @@ -1970,38 +1979,6 @@ mod tests { assert!(!b) } - #[test] - /// Test OP_HASH256_V0 - fn test_hash256_v0() { - /// op_hash256_v0([pk]) -> [addr_v0] - let (pk, sk) = sign::gen_keypair(); - let mut stack = Stack::new(); - stack.push(StackEntry::PubKey(pk)); - let mut v: Vec = vec![StackEntry::Bytes(construct_address_v0(&pk))]; - op_hash256_v0(&mut stack); - assert_eq!(stack.main_stack, v); - /// op_hash256([]) -> fail - let mut stack = Stack::new(); - let b = op_hash256_v0(&mut stack); - assert!(!b) - } - - #[test] - /// Test OP_HASH256_TEMP - fn test_hash256_temp() { - /// op_hash256_temp([pk]) -> [addr_temp] - let (pk, sk) = sign::gen_keypair(); - let mut stack = Stack::new(); - stack.push(StackEntry::PubKey(pk)); - let mut v: Vec = vec![StackEntry::Bytes(construct_address_temp(&pk))]; - op_hash256_temp(&mut stack); - assert_eq!(stack.main_stack, v); - /// op_hash256([]) -> fail - let mut stack = Stack::new(); - let b = op_hash256_temp(&mut stack); - assert!(!b) - } - #[test] /// Test OP_CHECKSIG fn test_checksig() { @@ -2010,7 +1987,7 @@ mod tests { let msg = hex::encode(vec![0, 0, 0]); let sig = sign::sign_detached(msg.as_bytes(), &sk); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let mut v: Vec = vec![StackEntry::Num(1)]; @@ -2020,7 +1997,7 @@ mod tests { /// op_checksig([msg',sig,pk]) -> [0] let msg = hex::encode(vec![0, 0, 1]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let mut v: Vec = vec![StackEntry::Num(0)]; @@ -2031,7 +2008,7 @@ mod tests { let (pk, sk) = sign::gen_keypair(); let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let mut v: Vec = vec![StackEntry::Num(0)]; @@ -2054,7 +2031,7 @@ mod tests { let msg = hex::encode(vec![0, 0, 0]); let sig = sign::sign_detached(msg.as_bytes(), &sk); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let mut v: Vec = vec![]; @@ -2064,7 +2041,7 @@ mod tests { /// op_checksigverify([msg',sig,pk]) -> fail let msg = hex::encode(vec![0, 0, 1]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let b = op_checksigverify(&mut stack); @@ -2074,7 +2051,7 @@ mod tests { let (pk, sk) = sign::gen_keypair(); let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig)); stack.push(StackEntry::PubKey(pk)); let b = op_checksigverify(&mut stack); @@ -2100,7 +2077,7 @@ mod tests { let sig1 = sign::sign_detached(msg.as_bytes(), &sk1); let sig2 = sign::sign_detached(msg.as_bytes(), &sk2); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig2)); stack.push(StackEntry::Num(2)); @@ -2115,7 +2092,7 @@ mod tests { /// op_checkmultisig([msg,0,pk1,pk2,pk3,3]) -> [1] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Num(0)); stack.push(StackEntry::PubKey(pk1)); stack.push(StackEntry::PubKey(pk2)); @@ -2128,7 +2105,7 @@ mod tests { /// op_checkmultisig([msg,0,0]) -> [1] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Num(0)); stack.push(StackEntry::Num(0)); let mut v: Vec = vec![StackEntry::Num(1)]; @@ -2138,7 +2115,7 @@ mod tests { /// op_checkmultisig([msg,sig1,1,pk1,1]) -> [1] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(1)); stack.push(StackEntry::PubKey(pk1)); @@ -2151,7 +2128,7 @@ mod tests { let msg = hex::encode(vec![0, 0, 0]); let sig3 = sign::sign_detached(msg.as_bytes(), &sk3); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig3)); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(2)); @@ -2166,7 +2143,7 @@ mod tests { /// op_checkmultisig([msg',sig1,sig2,2,pk1,pk2,pk3,3]) -> [0] let msg = hex::encode(vec![0, 0, 1]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig2)); stack.push(StackEntry::Num(2)); @@ -2181,7 +2158,7 @@ mod tests { /// op_checkmultisig([msg,sig1,sig1,2,pk1,pk2,pk3,3]) -> [0] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(2)); @@ -2253,7 +2230,7 @@ mod tests { let sig1 = sign::sign_detached(msg.as_bytes(), &sk1); let sig2 = sign::sign_detached(msg.as_bytes(), &sk2); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig2)); stack.push(StackEntry::Num(2)); @@ -2268,7 +2245,7 @@ mod tests { /// op_checkmultisigverify([msg,0,pk1,pk2,pk3,3]) -> [] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Num(0)); stack.push(StackEntry::PubKey(pk1)); stack.push(StackEntry::PubKey(pk2)); @@ -2281,7 +2258,7 @@ mod tests { /// op_checkmultisig([msg,0,0]) -> [] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Num(0)); stack.push(StackEntry::Num(0)); let mut v: Vec = vec![]; @@ -2291,7 +2268,7 @@ mod tests { /// op_checkmultisigverify([msg,sig1,1,pk1,1]) -> [] let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(1)); stack.push(StackEntry::PubKey(pk1)); @@ -2304,7 +2281,7 @@ mod tests { let msg = hex::encode(vec![0, 0, 0]); let sig3 = sign::sign_detached(msg.as_bytes(), &sk3); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig3)); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(2)); @@ -2319,7 +2296,7 @@ mod tests { /// op_checkmultisigverify([msg',sig1,sig2,2,pk1,pk2,pk3,3]) -> fail let msg = hex::encode(vec![0, 0, 1]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig2)); stack.push(StackEntry::Num(2)); @@ -2333,7 +2310,7 @@ mod tests { /// op_checkmultisigverify([msg,sig1,sig1,2,pk1,pk2,pk3,3]) -> fail let msg = hex::encode(vec![0, 0, 0]); let mut stack = Stack::new(); - stack.push(StackEntry::Bytes(msg)); + stack.push(StackEntry::Bytes(hex::decode(msg).unwrap())); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Signature(sig1)); stack.push(StackEntry::Num(2)); @@ -2399,11 +2376,11 @@ mod tests { let script = Script::from(v); assert!(script.is_valid()); // script length <= 10000 bytes - let v = vec![StackEntry::Bytes("a".repeat(500)); 20]; + let v = vec![StackEntry::Bytes("a".repeat(500).as_bytes().to_vec()); 20]; let script = Script::from(v); assert!(script.is_valid()); // script length > 10000 bytes - let v = vec![StackEntry::Bytes("a".repeat(500)); 21]; + let v = vec![StackEntry::Bytes("a".repeat(500).as_bytes().to_vec()); 21]; let script = Script::from(v); assert!(!script.is_valid()); // # opcodes <= 201 @@ -2457,11 +2434,11 @@ mod tests { let script = Script::from(v); assert!(script.interpret()); // script length <= 10000 bytes - let v = vec![StackEntry::Bytes("a".repeat(500)); 20]; + let v = vec![StackEntry::Bytes("a".repeat(500).as_bytes().to_vec()); 20]; let script = Script::from(v); assert!(script.interpret()); // script length > 10000 bytes - let v = vec![StackEntry::Bytes("a".repeat(500)); 21]; + let v = vec![StackEntry::Bytes("a".repeat(500).as_bytes().to_vec()); 21]; let script = Script::from(v); assert!(!script.interpret()); // # opcodes <= 201 @@ -2666,7 +2643,7 @@ mod tests { new_tx_in.script_signature = Script::multisig_validation( m, entry.pub_keys.len(), - entry.previous_out.t_hash.clone(), + hex::decode(&entry.previous_out.t_hash).unwrap(), entry.signatures, entry.pub_keys, ); @@ -2685,7 +2662,7 @@ mod tests { for entry in tx_values { let mut new_tx_in = TxIn::new(); new_tx_in.script_signature = Script::member_multisig( - entry.previous_out.t_hash.clone(), + hex::decode(&entry.previous_out.t_hash).unwrap(), entry.pub_keys[0], entry.signatures[0], ); @@ -2736,22 +2713,6 @@ mod tests { #[test] /// Checks that correct member multisig scripts are validated as such fn test_pass_member_multisig_valid() { - test_pass_member_multisig_valid_common(None); - } - - #[test] - /// Checks that correct member multisig scripts are validated as such - fn test_pass_member_multisig_valid_v0() { - test_pass_member_multisig_valid_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that correct member multisig scripts are validated as such - fn test_pass_member_multisig_valid_temp() { - test_pass_member_multisig_valid_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_pass_member_multisig_valid_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let t_hash = hex::encode(vec![0, 0, 0]); let signature = sign::sign_detached(t_hash.as_bytes(), &sk); @@ -2760,7 +2721,6 @@ mod tests { previous_out: OutPoint::new(t_hash, 0), signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = create_multisig_member_tx_ins(vec![tx_const]); @@ -2771,22 +2731,6 @@ mod tests { #[test] /// Checks that incorrect member multisig scripts are validated as such fn test_fail_member_multisig_invalid() { - test_fail_member_multisig_invalid_common(None); - } - - #[test] - /// Checks that incorrect member multisig scripts are validated as such - fn test_fail_member_multisig_invalid_v0() { - test_fail_member_multisig_invalid_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that incorrect member multisig scripts are validated as such - fn test_fail_member_multisig_invalid_temp() { - test_fail_member_multisig_invalid_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_fail_member_multisig_invalid_common(address_version: Option) { let (_pk, sk) = sign::gen_keypair(); let (pk, _sk) = sign::gen_keypair(); let t_hash = hex::encode(vec![0, 0, 0]); @@ -2796,7 +2740,6 @@ mod tests { previous_out: OutPoint::new(t_hash, 0), signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = create_multisig_member_tx_ins(vec![tx_const]); @@ -2807,10 +2750,6 @@ mod tests { #[test] /// Checks that correct p2pkh transaction signatures are validated as such fn test_pass_p2pkh_sig_valid() { - test_pass_p2pkh_sig_valid_common(None); - } - - fn test_pass_p2pkh_sig_valid_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let outpoint = OutPoint { t_hash: hex::encode(vec![0, 0, 0]), @@ -2823,7 +2762,6 @@ mod tests { previous_out: outpoint, signatures: vec![], pub_keys: vec![pk], - address_version, }; let tx_outs = vec![]; @@ -2831,7 +2769,7 @@ mod tests { tx_ins = update_input_signatures(&tx_ins, &tx_outs, &key_material); let hash_to_sign = construct_tx_in_out_signable_hash(&tx_ins[0], &tx_outs); - let tx_out_pk = construct_address_for(&pk, address_version); + let tx_out_pk = construct_address(&pk); assert!(tx_has_valid_p2pkh_sig( &tx_ins[0].script_signature, @@ -2843,16 +2781,6 @@ mod tests { #[test] /// Checks that invalid p2pkh transaction signatures are validated as such fn test_fail_p2pkh_sig_invalid() { - test_fail_p2pkh_sig_invalid_common(None); - } - - #[test] - /// Checks that invalid p2pkh transaction signatures are validated as such - fn test_fail_p2pkh_sig_invalid_v0() { - test_fail_p2pkh_sig_invalid_common(Some(NETWORK_VERSION_V0)); - } - - fn test_fail_p2pkh_sig_invalid_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let (second_pk, _s) = sign::gen_keypair(); let outpoint = OutPoint { @@ -2867,7 +2795,6 @@ mod tests { previous_out: outpoint, signatures: vec![signature], pub_keys: vec![second_pk], - address_version, }; let tx_ins = construct_payment_tx_ins(vec![tx_const]); @@ -2883,22 +2810,6 @@ mod tests { #[test] /// Checks that invalid p2pkh transaction signatures are validated as such fn test_fail_p2pkh_sig_script_empty() { - test_fail_p2pkh_sig_script_empty_common(None); - } - - #[test] - /// Checks that invalid p2pkh transaction signatures are validated as such - fn test_fail_p2pkh_sig_script_empty_v0() { - test_fail_p2pkh_sig_script_empty_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that invalid p2pkh transaction signatures are validated as such - fn test_fail_p2pkh_sig_script_empty_temp() { - test_fail_p2pkh_sig_script_empty_common(Some(NETWORK_VERSION_V0)); - } - - fn test_fail_p2pkh_sig_script_empty_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let outpoint = OutPoint { t_hash: hex::encode(vec![0, 0, 0]), @@ -2912,7 +2823,6 @@ mod tests { previous_out: outpoint, signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let mut tx_ins = Vec::new(); @@ -2937,22 +2847,6 @@ mod tests { #[test] /// Checks that invalid p2pkh transaction signatures are validated as such fn test_fail_p2pkh_sig_script_invalid_struct() { - test_fail_p2pkh_sig_script_invalid_struct_common(None); - } - - #[test] - /// Checks that invalid p2pkh transaction signatures are validated as such - fn test_fail_p2pkh_sig_script_invalid_struct_v0() { - test_fail_p2pkh_sig_script_invalid_struct_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that invalid p2pkh transaction signatures are validated as such - fn test_fail_p2pkh_sig_script_invalid_struct_temp() { - test_fail_p2pkh_sig_script_invalid_struct_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_fail_p2pkh_sig_script_invalid_struct_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let outpoint = OutPoint { t_hash: hex::encode(vec![0, 0, 0]), @@ -2966,7 +2860,6 @@ mod tests { previous_out: outpoint, signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let mut tx_ins = Vec::new(); @@ -2977,7 +2870,7 @@ mod tests { new_tx_in .script_signature .stack - .push(StackEntry::Bytes("".to_string())); + .push(StackEntry::Bytes("".as_bytes().to_vec())); new_tx_in.previous_out = Some(entry.previous_out); tx_ins.push(new_tx_in); @@ -2995,22 +2888,6 @@ mod tests { #[test] /// Checks that correct multisig validation signatures are validated as such fn test_pass_multisig_validation_valid() { - test_pass_multisig_validation_valid_common(None); - } - - #[test] - /// Checks that correct multisig validation signatures are validated as such - fn test_pass_multisig_validation_valid_v0() { - test_pass_multisig_validation_valid_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that correct multisig validation signatures are validated as such - fn test_pass_multisig_validation_valid_temp() { - test_pass_multisig_validation_valid_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_pass_multisig_validation_valid_common(address_version: Option) { let (first_pk, first_sk) = sign::gen_keypair(); let (second_pk, second_sk) = sign::gen_keypair(); let (third_pk, third_sk) = sign::gen_keypair(); @@ -3024,7 +2901,6 @@ mod tests { previous_out: OutPoint::new(check_data, 0), signatures: vec![first_sig, second_sig], pub_keys: vec![first_pk, second_pk, third_pk], - address_version, }; let tx_ins = create_multisig_tx_ins(vec![tx_const], m); @@ -3035,29 +2911,7 @@ mod tests { #[test] /// Validate tx_is_valid for multiple TxIn configurations fn test_tx_is_valid() { - test_tx_is_valid_common(None, OpCodes::OP_HASH256, None, false); - } - - #[test] - /// Validate tx_is_valid for multiple TxIn configurations - fn test_tx_is_valid_v0() { - test_tx_is_valid_common( - Some(NETWORK_VERSION_V0), - OpCodes::OP_HASH256_V0, - None, - false, - ); - } - - #[test] - /// Validate tx_is_valid for multiple TxIn configurations - fn test_tx_is_valid_temp() { - test_tx_is_valid_common( - Some(NETWORK_VERSION_TEMP), - OpCodes::OP_HASH256_TEMP, - None, - false, - ); + test_tx_is_valid_common(OpCodes::OP_HASH256, None, false); } #[test] @@ -3081,19 +2935,18 @@ mod tests { /// Validate tx_is_valid for locktime fn test_tx_is_valid_locktime() { assert!( - test_tx_is_valid_common(None, OpCodes::OP_HASH256, Some(99), false) - && !test_tx_is_valid_common(None, OpCodes::OP_HASH256, Some(1000000000), false) + test_tx_is_valid_common(OpCodes::OP_HASH256, Some(99), false) + && !test_tx_is_valid_common(OpCodes::OP_HASH256, Some(1000000000), false) ); } #[test] /// Validate tx_is_valid for fees fn test_tx_is_valid_fees() { - test_tx_is_valid_common(None, OpCodes::OP_HASH256, None, true); + test_tx_is_valid_common(OpCodes::OP_HASH256, None, true); } fn test_tx_is_valid_common( - address_version: Option, op_hash256: OpCodes, locktime: Option, with_fees: bool, @@ -3104,7 +2957,7 @@ mod tests { let (pk, sk) = sign::gen_keypair(); let tx_hash = hex::encode(vec![0, 0, 0]); let tx_outpoint = OutPoint::new(tx_hash, 0); - let script_public_key = construct_address_for(&pk, address_version); + let script_public_key = construct_address(&pk); let tx_in_previous_out = TxOut::new_token_amount(script_public_key.clone(), TokenAmount(5), locktime); let ongoing_tx_outs = vec![tx_in_previous_out.clone()]; @@ -3121,19 +2974,19 @@ mod tests { // 0. Happy case: valid test ( vec![ - StackEntry::Bytes(valid_bytes), + StackEntry::Bytes(hex::decode(valid_bytes).unwrap()), StackEntry::Signature(valid_sig), StackEntry::PubKey(pk), StackEntry::Op(OpCodes::OP_DUP), StackEntry::Op(op_hash256), - StackEntry::Bytes(script_public_key), + StackEntry::Bytes(hex::decode(script_public_key).unwrap()), StackEntry::Op(OpCodes::OP_EQUALVERIFY), StackEntry::Op(OpCodes::OP_CHECKSIG), ], true, ), // 2. Empty script - (vec![StackEntry::Bytes("".to_string())], false), + (vec![StackEntry::Bytes("".as_bytes().to_vec())], false), ]; // @@ -3324,22 +3177,6 @@ mod tests { #[test] /// Checks that incorrect member interpret scripts are validated as such fn test_fail_interpret_valid() { - test_fail_interpret_valid_common(None); - } - - #[test] - /// Checks that incorrect member interpret scripts are validated as such - fn test_fail_interpret_valid_v0() { - test_fail_interpret_valid_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that incorrect member interpret scripts are validated as such - fn test_fail_interpret_valid_temp() { - test_fail_interpret_valid_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_fail_interpret_valid_common(address_version: Option) { let (_pk, sk) = sign::gen_keypair(); let (pk, _sk) = sign::gen_keypair(); let t_hash = hex::encode(vec![0, 0, 0]); @@ -3349,7 +3186,6 @@ mod tests { previous_out: OutPoint::new(t_hash, 0), signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = create_multisig_member_tx_ins(vec![tx_const]); @@ -3360,22 +3196,6 @@ mod tests { #[test] /// Checks that interpret scripts are validated as such fn test_pass_interpret_valid() { - test_pass_interpret_valid_common(None); - } - - #[test] - /// Checks that interpret scripts are validated as such - fn test_pass_interpret_valid_v0() { - test_pass_interpret_valid_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - /// Checks that interpret scripts are validated as such - fn test_pass_interpret_valid_temp() { - test_pass_interpret_valid_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_pass_interpret_valid_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let t_hash = hex::encode(vec![0, 0, 0]); let signature = sign::sign_detached(t_hash.as_bytes(), &sk); @@ -3384,7 +3204,6 @@ mod tests { previous_out: OutPoint::new(t_hash, 0), signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = create_multisig_member_tx_ins(vec![tx_const]); diff --git a/src/utils/test_utils.rs b/src/utils/test_utils.rs index 0c8fcef..e7139f8 100644 --- a/src/utils/test_utils.rs +++ b/src/utils/test_utils.rs @@ -66,7 +66,7 @@ pub fn generate_tx_with_ins_and_outs_assets( let signature = sign::sign_detached(signable_hash.as_bytes(), &sk); let tx_in = TxIn::new_from_input( tx_previous_out.clone(), - Script::pay2pkh(signable_hash, signature, pk, None), + Script::pay2pkh(hex::decode(&signable_hash).unwrap(), signature, pk), ); utxo_set.insert(tx_previous_out, tx_in_previous_out); tx.inputs.push(tx_in); diff --git a/src/utils/transaction_utils.rs b/src/utils/transaction_utils.rs index eee4332..7c4f7c5 100644 --- a/src/utils/transaction_utils.rs +++ b/src/utils/transaction_utils.rs @@ -31,20 +31,6 @@ pub fn construct_p2sh_address(script: &Script) -> String { addr } -/// Builds an address from a public key and a specified network version -/// -/// ### Arguments -/// -/// * `pub_key` - A public key to build an address from -/// * `address_version` - Network version to use for the address -pub fn construct_address_for(pub_key: &PublicKey, address_version: Option) -> String { - match address_version { - Some(NETWORK_VERSION_V0) => construct_address_v0(pub_key), - Some(NETWORK_VERSION_TEMP) => construct_address_temp(pub_key), - _ => construct_address(pub_key), - } -} - /// Builds an address from a public key /// /// ### Arguments @@ -54,58 +40,6 @@ pub fn construct_address(pub_key: &PublicKey) -> String { hex::encode(sha3_256::digest(pub_key.as_ref())) } -/// Builds an old (network version 0) address from a public key -/// -/// ### Arguments -/// -/// * `pub_key` - A public key to build an address from -pub fn construct_address_v0(pub_key: &PublicKey) -> String { - let first_pubkey_bytes = { - // We used sodiumoxide serialization before with a 64 bit length prefix. - // Make clear what we are using as this was not intended. - let mut v = vec![32, 0, 0, 0, 0, 0, 0, 0]; - v.extend_from_slice(pub_key.as_ref()); - v - }; - let mut first_hash = sha3_256::digest(&first_pubkey_bytes).to_vec(); - first_hash.truncate(V0_ADDRESS_LENGTH); - hex::encode(first_hash) -} - -/// Builds an address from a public key using the -/// temporary address scheme present on the wallet -/// -/// TODO: Deprecate after addresses retire -/// -/// ### Arguments -/// -/// * `pub_key` - A public key to build an address from -pub fn construct_address_temp(pub_key: &PublicKey) -> String { - let base64_encoding = base64::encode(pub_key.as_ref()); - let hex_decoded = decode_base64_as_hex(&base64_encoding); - hex::encode(sha3_256::digest(&hex_decoded)) -} - -/// Decodes a base64 encoded string as hex, invalid character pairs are decoded up to the -/// first character. If the decoding up to the first character fails, a default value of 0 -/// is used. -/// -/// TODO: Deprecate after addresses retire -/// -/// ### Arguments -/// -/// * `s` - Base64 encoded string -pub fn decode_base64_as_hex(s: &str) -> Vec { - (ZERO..s.len()) - .step_by(TWO) - .map(|i| { - u8::from_str_radix(&s[i..i + TWO], SIXTEEN as u32) - .or_else(|_| u8::from_str_radix(&s[i..i + ONE], SIXTEEN as u32)) - .unwrap_or_default() - }) - .collect() -} - /// Constructs signable string for OutPoint /// /// ### Arguments @@ -162,7 +96,7 @@ pub fn get_stack_entry_signable_string(entry: &StackEntry) -> String { } StackEntry::PubKey(pub_key) => format!("PubKey:{}", hex::encode(pub_key.as_ref())), StackEntry::Num(num) => format!("Num:{num}"), - StackEntry::Bytes(bytes) => format!("Bytes:{bytes}"), + StackEntry::Bytes(bytes) => format!("Bytes:{}", hex::encode(bytes)), } } @@ -592,10 +526,9 @@ pub fn update_input_signatures( let sk = &key_material.get(&previous_out.unwrap()).unwrap().1; let script_signature = Script::pay2pkh( - signable_hash.clone(), + hex::decode(&signable_hash).unwrap(), sign_detached(signable_hash.as_bytes(), sk), pk, - None, ); tx_in.script_signature = script_signature; @@ -754,26 +687,7 @@ mod tests { use crate::script::OpCodes; use crate::utils::script_utils::{tx_has_valid_p2sh_script, tx_outs_are_valid}; - #[test] - // Creates a valid payment transaction - fn test_construct_a_valid_payment_tx() { - test_construct_a_valid_payment_tx_common(None); - } - - #[test] - // Creates a valid payment transaction - fn test_construct_a_valid_payment_tx_v0() { - test_construct_a_valid_payment_tx_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - // Creates a valid payment transaction - fn test_construct_a_valid_payment_tx_temp() { - test_construct_a_valid_payment_tx_common(Some(NETWORK_VERSION_TEMP)); - } - fn test_construct_valid_inputs( - address_version: Option, ) -> ( Vec, String, @@ -793,7 +707,6 @@ mod tests { previous_out: prev_out, signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = construct_payment_tx_ins(vec![tx_const]); @@ -804,8 +717,7 @@ mod tests { #[test] fn test_construct_a_valid_p2sh_tx() { let token_amount = TokenAmount(400000); - let (tx_ins, _drs_block_hash, key_material) = - test_construct_valid_inputs(Some(NETWORK_VERSION_V0)); + let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(); let mut script = Script::new_for_coinbase(10); script.stack.push(StackEntry::Op(OpCodes::OP_DROP)); @@ -824,7 +736,6 @@ mod tests { previous_out: OutPoint::new(spending_tx_hash, 0), signatures: vec![], pub_keys: vec![], - address_version: Some(NETWORK_VERSION_V0), }; let redeeming_tx_ins = construct_p2sh_redeem_tx_ins(tx_const, script.clone()); @@ -854,8 +765,7 @@ mod tests { #[test] fn test_construct_a_valid_burn_tx() { let token_amount = TokenAmount(400000); - let (tx_ins, _drs_block_hash, key_material) = - test_construct_valid_inputs(Some(NETWORK_VERSION_V0)); + let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(); let burn_tx = construct_burn_tx(tx_ins, None, &key_material); @@ -865,7 +775,6 @@ mod tests { previous_out: OutPoint::new(spending_tx_hash, 0), signatures: vec![], pub_keys: vec![], - address_version: Some(NETWORK_VERSION_V0), }; let s = vec![StackEntry::Op(OpCodes::OP_BURN)]; @@ -896,8 +805,9 @@ mod tests { // TODO: Add assertion for full tx validity } - fn test_construct_a_valid_payment_tx_common(address_version: Option) { - let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(address_version); + #[test] + fn test_construct_a_valid_payment_tx() { + let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(); let token_amount = TokenAmount(400000); let payment_tx = construct_payment_tx( @@ -920,7 +830,7 @@ mod tests { #[test] /// Creates a valid payment transaction including fees fn test_construct_valid_payment_tx_with_fees() { - let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(None); + let (tx_ins, _drs_block_hash, key_material) = test_construct_valid_inputs(); let token_amount = TokenAmount(400000); let fee_amount = TokenAmount(1000); @@ -958,7 +868,6 @@ mod tests { previous_out: prev_out.clone(), signatures: vec![signature], pub_keys: vec![pk], - address_version: Some(2), }; let tx_ins = construct_payment_tx_ins(vec![tx_const]); @@ -1004,7 +913,6 @@ mod tests { previous_out: prev_out.clone(), signatures: vec![signature], pub_keys: vec![pk], - address_version: Some(2), }; let drs_tx_hash = "item_tx_hash".to_string(); @@ -1054,7 +962,6 @@ mod tests { previous_out: prev_out.clone(), signatures: vec![signature], pub_keys: vec![pk], - address_version: Some(2), }; let genesis_hash = "item_tx_hash".to_string(); @@ -1082,22 +989,6 @@ mod tests { #[test] // Creates a valid UTXO set fn test_construct_valid_utxo_set() { - test_construct_valid_utxo_set_common(None); - } - - #[test] - // Creates a valid UTXO set - fn test_construct_valid_utxo_set_v0() { - test_construct_valid_utxo_set_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - // Creates a valid UTXO set - fn test_construct_valid_utxo_set_temp() { - test_construct_valid_utxo_set_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_construct_valid_utxo_set_common(address_version: Option) { let (pk, sk) = sign::gen_keypair(); let t_hash_1 = hex::encode(vec![0, 0, 0]); @@ -1111,7 +1002,6 @@ mod tests { previous_out: OutPoint::new("".to_string(), 0), signatures: vec![signed], pub_keys: vec![pk], - address_version, }; let token_amount = TokenAmount(400000); @@ -1135,7 +1025,6 @@ mod tests { previous_out: tx_1_out_p.clone(), signatures: vec![signed], pub_keys: vec![pk], - address_version, }; let tx_ins_2 = construct_payment_tx_ins(vec![tx_2]); let tx_outs = vec![TxOut::new_token_amount( @@ -1163,22 +1052,6 @@ mod tests { #[test] // Creates a valid DDE transaction fn test_construct_a_valid_dde_tx() { - test_construct_a_valid_dde_tx_common(None); - } - - #[test] - // Creates a valid DDE transaction - fn test_construct_a_valid_dde_tx_v0() { - test_construct_a_valid_dde_tx_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - // Creates a valid DDE transaction - fn test_construct_a_valid_dde_tx_temp() { - test_construct_a_valid_dde_tx_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_construct_a_valid_dde_tx_common(address_version: Option) { let (_pk, sk) = sign::gen_keypair(); let (pk, _sk) = sign::gen_keypair(); let t_hash = hex::encode(vec![0, 0, 0]); @@ -1198,7 +1071,6 @@ mod tests { previous_out: prev_out.clone(), signatures: vec![signature], pub_keys: vec![pk], - address_version, }; let tx_ins = construct_payment_tx_ins(vec![tx_const]); @@ -1349,22 +1221,6 @@ mod tests { #[test] // Test valid address construction; should correlate with test on wallet fn test_construct_valid_addresses() { - test_construct_valid_addresses_common(None); - } - - #[test] - // Test valid address construction; should correlate with test on wallet - fn test_construct_valid_addresses_v0() { - test_construct_valid_addresses_common(Some(NETWORK_VERSION_V0)); - } - - #[test] - // Test valid address construction; should correlate with test on wallet - fn test_construct_valid_addresses_temp() { - test_construct_valid_addresses_common(Some(NETWORK_VERSION_TEMP)); - } - - fn test_construct_valid_addresses_common(address_version: Option) { // // Arrange // @@ -1383,32 +1239,17 @@ mod tests { // let actual_pub_addresses: Vec = pub_keys .iter() - .map(|pub_key| construct_address_for(pub_key, address_version)) + .map(construct_address) .collect(); // // Assert // - let expected_pub_addresses = match address_version { - // Old Address structure - Some(NETWORK_VERSION_V0) => vec![ - "13bd3351b78beb2d0dadf2058dcc926c", - "abc7c0448465c4507faf2ee588728824", - "6ae52e3870884ab66ec49d3bb359c0bf", - ], - // Temporary address structure present on wallet - Some(NETWORK_VERSION_TEMP) => vec![ - "6c6b6e8e9df8c63d22d9eb687b9671dd1ce5d89f195bb2316e1b1444848cd2b3", - "8ac2fdcb0688abb2727d63ed230665b275a1d3a28373baa92a9afa5afd610e9f", - "0becdaaf6a855f04961208ee992651c11df0be91c08629dfc079d05d2915ec22", - ], - // Current address structure - _ => vec![ - "5423e6bd848e0ce5cd794e55235c23138d8833633cd2d7de7f4a10935178457b", - "77516e2d91606250e625546f86702510d2e893e4a27edfc932fdba03c955cc1b", - "4cfd64a6692021fc417368a866d33d94e1c806747f61ac85e0b3935e7d5ed925", - ], - }; + let expected_pub_addresses = vec![ + "5423e6bd848e0ce5cd794e55235c23138d8833633cd2d7de7f4a10935178457b", + "77516e2d91606250e625546f86702510d2e893e4a27edfc932fdba03c955cc1b", + "4cfd64a6692021fc417368a866d33d94e1c806747f61ac85e0b3935e7d5ed925", + ]; assert_eq!(actual_pub_addresses, expected_pub_addresses); } @@ -1509,7 +1350,7 @@ mod tests { Signature::from_slice(hex::decode(signatures[n]).unwrap().as_ref()).unwrap(); let pk = PublicKey::from_slice(hex::decode(pub_keys[n]).unwrap().as_ref()).unwrap(); - let script = Script::pay2pkh(sig_data, sig, pk, None); + let script = Script::pay2pkh(hex::decode(&sig_data).unwrap(), sig, pk); let out_p = previous_out_points[n].clone(); TxIn::new_from_input(out_p, script)