script init

This commit is contained in:
debris 2016-08-19 14:50:22 +02:00
parent e50389b89a
commit ab599d20dd
9 changed files with 82 additions and 8 deletions

View File

@ -50,7 +50,7 @@ impl Block {
#[cfg(test)]
mod tests {
use rustc_serialize::hex::FromHex;
use hex::FromHex;
use reader::deserialize;
use hash::h256_from_str;
use super::Block;

View File

@ -21,7 +21,7 @@ pub fn dhash(input: &[u8]) -> H256 {
#[cfg(test)]
mod tests {
use super::dhash;
use rustc_serialize::hex::FromHex;
use hex::FromHex;
#[test]
fn test_double_hash() {

View File

@ -1,11 +1,11 @@
use rustc_serialize::hex::{ToHex, FromHex};
use hex::{ToHex, FromHex};
pub type H160 = [u8; 20];
pub type H256 = [u8; 32];
pub type H512 = [u8; 64];
pub type H520 = [u8; 65];
/// Reverses the hash. Commonly used to display
/// Reverses the hash. Commonly used to display
#[inline]
pub fn reverse(hash: &H256) -> H256 {
let mut result = hash.clone();

View File

@ -117,7 +117,7 @@ impl From<&'static str> for Address {
#[cfg(test)]
mod tests {
use rustc_serialize::hex::FromHex;
use hex::FromHex;
use network::Network;
use super::{Address, Type};

View File

@ -1,7 +1,7 @@
//! Bitcoin KeyPair
use std::fmt;
use rustc_serialize::hex::ToHex;
use hex::ToHex;
use rcrypto::sha2::Sha256;
use rcrypto::ripemd160::Ripemd160;
use rcrypto::digest::Digest;

View File

@ -22,3 +22,4 @@ pub mod reader;
pub mod stream;
pub mod transaction;
pub use rustc_serialize::hex;

View File

@ -1,3 +1,6 @@
mod opcode;
mod script;
pub use self::opcode::Opcode;
pub use self::script::Script;

70
src/script/script.rs Normal file
View File

@ -0,0 +1,70 @@
//! Serialized script, used inside transaction inputs and outputs.
use script::Opcode;
/// Maximum number of bytes pushable to the stack
const MAX_SCRIPT_ELEMENT_SIZE: u32 = 520;
/// Maximum number of non-push operations per script
const MAX_OPS_PER_SCRIPT: u32 = 201;
/// Maximum number of public keys per multisig
const MAX_PUBKEYS_PER_MULTISIG: u32 = 20;
/// Maximum script length in bytes
const MAX_SCRIPT_SIZE: u32 = 10000;
/// Threshold for nLockTime: below this value it is interpreted as block number,
/// otherwise as UNIX timestamp.
const LOCKTIME_THRESHOLD: u32 = 500000000; // Tue Nov 5 00:53:20 1985 UTC
/// Serialized script, used inside transaction inputs and outputs.
pub struct Script {
data: Vec<u8>,
}
impl Script {
/// Script constructor.
pub fn new(data: Vec<u8>) -> Self {
Script {
data: data,
}
}
/// Extra-fast test for pay-to-script-hash scripts.
pub fn is_pay_to_script_hash(&self) -> bool {
self.data.len() == 23 &&
self.data[0] == Opcode::OP_HASH160 as u8 &&
self.data[1] == 0x14 &&
self.data[22] == Opcode::OP_EQUAL as u8
}
/// Extra-fast test for pay-to-witness-script-hash scripts.
pub fn is_pay_to_witness_script_hash(&self) -> bool {
self.data.len() == 34 &&
self.data[0] == Opcode::OP_0 as u8 &&
self.data[1] == 0x20
}
}
#[cfg(test)]
mod tests {
use hex::FromHex;
use super::Script;
#[test]
fn test_is_pay_to_script_hash() {
let data = "a9143b80842f4ea32806ce5e723a255ddd6490cfd28d87".from_hex().unwrap();
let data2 = "a9143b80842f4ea32806ce5e723a255ddd6490cfd28d88".from_hex().unwrap();
assert!(Script::new(data).is_pay_to_script_hash());
assert!(!Script::new(data2).is_pay_to_script_hash());
}
#[test]
fn test_is_pay_to_witness_script_hash() {
let data = "00203b80842f4ea32806ce5e723a255ddd6490cfd28dac38c58bf9254c0577330693".from_hex().unwrap();
let data2 = "01203b80842f4ea32806ce5e723a255ddd6490cfd28dac38c58bf9254c0577330693".from_hex().unwrap();
assert!(Script::new(data).is_pay_to_witness_script_hash());
assert!(!Script::new(data2).is_pay_to_witness_script_hash());
}
}

View File

@ -148,7 +148,7 @@ impl Transaction {
#[cfg(test)]
mod tests {
use rustc_serialize::hex::FromHex;
use hex::FromHex;
use reader::deserialize;
use hash::h256_from_str;
use super::Transaction;
@ -171,7 +171,7 @@ mod tests {
assert_eq!(tx_output.value, 5000000000);
assert_eq!(tx_output.pk_script, "76a914404371705fa9bd789a2fcd52d2c580b65d35549d88ac".from_hex().unwrap());
}
#[test]
fn test_transaction_hash() {
let encoded_tx = "0100000001a6b97044d03da79c005b20ea9c0e1a6d9dc12d9f7b91a5911c9030a439eed8f5000000004948304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d1090db022100e2ac980643b0b82c0e88ffdfec6b64e3e6ba35e7ba5fdd7d5d6cc8d25c6b241501ffffffff0100f2052a010000001976a914404371705fa9bd789a2fcd52d2c580b65d35549d88ac00000000".from_hex().unwrap();