Merge pull request #198 from sgeisler/safe-opcodes

Safe opcodes 1.14.0
This commit is contained in:
Andrew Poelstra 2018-12-14 16:32:29 +00:00 committed by GitHub
commit 5a5158e120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 810 additions and 401 deletions

View File

@ -78,7 +78,7 @@ fn bitcoin_genesis_tx() -> Transaction {
// Outputs
let out_script = script::Builder::new()
.push_slice(&hex_bytes("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f").unwrap())
.push_opcode(opcodes::All::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
ret.output.push(TxOut {
value: 50 * COIN_VALUE,

File diff suppressed because it is too large Load Diff

View File

@ -58,7 +58,7 @@ impl fmt::Debug for Script {
n as usize
} else {
match opcode {
opcodes::All::OP_PUSHDATA1 => {
opcodes::all::OP_PUSHDATA1 => {
if self.0.len() < index + 1 {
f.write_str("<unexpected end>")?;
break;
@ -68,7 +68,7 @@ impl fmt::Debug for Script {
Err(_) => { f.write_str("<bad length>")?; break; }
}
}
opcodes::All::OP_PUSHDATA2 => {
opcodes::all::OP_PUSHDATA2 => {
if self.0.len() < index + 2 {
f.write_str("<unexpected end>")?;
break;
@ -78,7 +78,7 @@ impl fmt::Debug for Script {
Err(_) => { f.write_str("<bad length>")?; break; }
}
}
opcodes::All::OP_PUSHDATA4 => {
opcodes::all::OP_PUSHDATA4 => {
if self.0.len() < index + 4 {
f.write_str("<unexpected end>")?;
break;
@ -94,7 +94,7 @@ impl fmt::Debug for Script {
if index > 1 { f.write_str(" ")?; }
// Write the opcode
if opcode == opcodes::All::OP_PUSHBYTES_0 {
if opcode == opcodes::all::OP_PUSHBYTES_0 {
f.write_str("OP_0")?;
} else {
write!(f, "{:?}", opcode)?;
@ -304,9 +304,9 @@ impl Script {
/// Compute the P2SH output corresponding to this redeem script
pub fn to_p2sh(&self) -> Script {
Builder::new().push_opcode(opcodes::All::OP_HASH160)
Builder::new().push_opcode(opcodes::all::OP_HASH160)
.push_slice(&Hash160::from_data(&self.0)[..])
.push_opcode(opcodes::All::OP_EQUAL)
.push_opcode(opcodes::all::OP_EQUAL)
.into_script()
}
@ -326,52 +326,52 @@ impl Script {
#[inline]
pub fn is_p2sh(&self) -> bool {
self.0.len() == 23 &&
self.0[0] == opcodes::All::OP_HASH160 as u8 &&
self.0[1] == opcodes::All::OP_PUSHBYTES_20 as u8 &&
self.0[22] == opcodes::All::OP_EQUAL as u8
self.0[0] == opcodes::all::OP_HASH160.into_u8() &&
self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8() &&
self.0[22] == opcodes::all::OP_EQUAL.into_u8()
}
/// Checks whether a script pubkey is a p2pkh output
#[inline]
pub fn is_p2pkh(&self) -> bool {
self.0.len() == 25 &&
self.0[0] == opcodes::All::OP_DUP as u8 &&
self.0[1] == opcodes::All::OP_HASH160 as u8 &&
self.0[2] == opcodes::All::OP_PUSHBYTES_20 as u8 &&
self.0[23] == opcodes::All::OP_EQUALVERIFY as u8 &&
self.0[24] == opcodes::All::OP_CHECKSIG as u8
self.0[0] == opcodes::all::OP_DUP.into_u8() &&
self.0[1] == opcodes::all::OP_HASH160.into_u8() &&
self.0[2] == opcodes::all::OP_PUSHBYTES_20.into_u8() &&
self.0[23] == opcodes::all::OP_EQUALVERIFY.into_u8() &&
self.0[24] == opcodes::all::OP_CHECKSIG.into_u8()
}
/// Checks whether a script pubkey is a p2pkh output
#[inline]
pub fn is_p2pk(&self) -> bool {
(self.0.len() == 67 &&
self.0[0] == opcodes::All::OP_PUSHBYTES_65 as u8 &&
self.0[66] == opcodes::All::OP_CHECKSIG as u8)
self.0[0] == opcodes::all::OP_PUSHBYTES_65.into_u8() &&
self.0[66] == opcodes::all::OP_CHECKSIG.into_u8())
|| (self.0.len() == 35 &&
self.0[0] == opcodes::All::OP_PUSHBYTES_33 as u8 &&
self.0[34] == opcodes::All::OP_CHECKSIG as u8)
self.0[0] == opcodes::all::OP_PUSHBYTES_33.into_u8() &&
self.0[34] == opcodes::all::OP_CHECKSIG.into_u8())
}
/// Checks whether a script pubkey is a p2wsh output
#[inline]
pub fn is_v0_p2wsh(&self) -> bool {
self.0.len() == 34 &&
self.0[0] == opcodes::All::OP_PUSHBYTES_0 as u8 &&
self.0[1] == opcodes::All::OP_PUSHBYTES_32 as u8
self.0[0] == opcodes::all::OP_PUSHBYTES_0.into_u8() &&
self.0[1] == opcodes::all::OP_PUSHBYTES_32.into_u8()
}
/// Checks whether a script pubkey is a p2wpkh output
#[inline]
pub fn is_v0_p2wpkh(&self) -> bool {
self.0.len() == 22 &&
self.0[0] == opcodes::All::OP_PUSHBYTES_0 as u8 &&
self.0[1] == opcodes::All::OP_PUSHBYTES_20 as u8
self.0[0] == opcodes::all::OP_PUSHBYTES_0.into_u8() &&
self.0[1] == opcodes::all::OP_PUSHBYTES_20.into_u8()
}
/// Check if this is an OP_RETURN output
pub fn is_op_return (&self) -> bool {
!self.0.is_empty() && (opcodes::All::from(self.0[0]) == opcodes::All::OP_RETURN)
!self.0.is_empty() && (opcodes::All::from(self.0[0]) == opcodes::all::OP_RETURN)
}
/// Whether a script can be proven to have no satisfying input
@ -549,12 +549,12 @@ impl Builder {
pub fn push_int(mut self, data: i64) -> Builder {
// We can special-case -1, 1-16
if data == -1 || (data >= 1 && data <= 16) {
self.0.push((data - 1 + opcodes::OP_TRUE as i64) as u8);
self.0.push((data - 1 + opcodes::OP_TRUE.into_u8() as i64) as u8);
self
}
// We can also special-case zero
else if data == 0 {
self.0.push(opcodes::OP_FALSE as u8);
self.0.push(opcodes::OP_FALSE.into_u8());
self
}
// Otherwise encode it as data
@ -573,16 +573,16 @@ impl Builder {
match data.len() as u64 {
n if n < opcodes::Ordinary::OP_PUSHDATA1 as u64 => { self.0.push(n as u8); },
n if n < 0x100 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA1 as u8);
self.0.push(opcodes::Ordinary::OP_PUSHDATA1.into_u8());
self.0.push(n as u8);
},
n if n < 0x10000 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA2 as u8);
self.0.push(opcodes::Ordinary::OP_PUSHDATA2.into_u8());
self.0.push((n % 0x100) as u8);
self.0.push((n / 0x100) as u8);
},
n if n < 0x100000000 => {
self.0.push(opcodes::Ordinary::OP_PUSHDATA4 as u8);
self.0.push(opcodes::Ordinary::OP_PUSHDATA4.into_u8());
self.0.push((n % 0x100) as u8);
self.0.push(((n / 0x100) % 0x100) as u8);
self.0.push(((n / 0x10000) % 0x100) as u8);
@ -597,7 +597,7 @@ impl Builder {
/// Adds a single opcode to the script
pub fn push_opcode(mut self, data: opcodes::All) -> Builder {
self.0.push(data as u8);
self.0.push(data.into_u8());
self
}
@ -722,18 +722,18 @@ mod test {
script = script.push_slice("NRA4VR".as_bytes()); comp.extend([6u8, 78, 82, 65, 52, 86, 82].iter().cloned()); assert_eq!(&script[..], &comp[..]);
// opcodes
script = script.push_opcode(opcodes::All::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
script = script.push_opcode(opcodes::All::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
script = script.push_opcode(opcodes::all::OP_CHECKSIG); comp.push(0xACu8); assert_eq!(&script[..], &comp[..]);
}
#[test]
fn script_builder() {
// from txid 3bb5e6434c11fb93f64574af5d116736510717f2c595eb45b52c28e31622dfff which was in my mempool when I wrote the test
let script = Builder::new().push_opcode(opcodes::All::OP_DUP)
.push_opcode(opcodes::All::OP_HASH160)
let script = Builder::new().push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&hex_decode("16e1ae70ff0fa102905d4af297f6912bda6cce19").unwrap())
.push_opcode(opcodes::All::OP_EQUALVERIFY)
.push_opcode(opcodes::All::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
.into_script();
assert_eq!(&format!("{:x}", script), "76a91416e1ae70ff0fa102905d4af297f6912bda6cce1988ac");
}
@ -884,7 +884,7 @@ mod test {
v_min,
vec![
Instruction::PushBytes(&[105]),
Instruction::Op(opcodes::All::OP_NOP3),
Instruction::Op(opcodes::all::OP_NOP3),
]
);
@ -899,7 +899,7 @@ mod test {
v_nonmin_alt,
vec![
Instruction::PushBytes(&[105, 0]),
Instruction::Op(opcodes::All::OP_NOP3),
Instruction::Op(opcodes::all::OP_NOP3),
]
);
@ -913,7 +913,7 @@ mod test {
let script_1 = Builder::new().push_slice(&[1,2,3,4]).into_script();
let script_2 = Builder::new().push_int(10).into_script();
let script_3 = Builder::new().push_int(15).into_script();
let script_4 = Builder::new().push_opcode(opcodes::All::OP_RETURN).into_script();
let script_4 = Builder::new().push_opcode(opcodes::all::OP_RETURN).into_script();
assert!(script_1 < script_2);
assert!(script_2 < script_3);

View File

@ -212,21 +212,21 @@ impl Address {
Payload::Pubkey(ref pk) => {
script::Builder::new()
.push_slice(&pk.serialize_uncompressed()[..])
.push_opcode(opcodes::All::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_CHECKSIG)
},
Payload::PubkeyHash(ref hash) => {
script::Builder::new()
.push_opcode(opcodes::All::OP_DUP)
.push_opcode(opcodes::All::OP_HASH160)
.push_opcode(opcodes::all::OP_DUP)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&hash[..])
.push_opcode(opcodes::All::OP_EQUALVERIFY)
.push_opcode(opcodes::All::OP_CHECKSIG)
.push_opcode(opcodes::all::OP_EQUALVERIFY)
.push_opcode(opcodes::all::OP_CHECKSIG)
},
Payload::ScriptHash(ref hash) => {
script::Builder::new()
.push_opcode(opcodes::All::OP_HASH160)
.push_opcode(opcodes::all::OP_HASH160)
.push_slice(&hash[..])
.push_opcode(opcodes::All::OP_EQUAL)
.push_opcode(opcodes::all::OP_EQUAL)
},
Payload::WitnessProgram(ref witprog) => {
script::Builder::new()