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,4 +1,4 @@
use rustc_serialize::hex::{ToHex, FromHex};
use hex::{ToHex, FromHex};
pub type H160 = [u8; 20];
pub type H256 = [u8; 32];

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;