Skip to content

Commit

Permalink
Remove push_bytes and use push_data instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Vitae committed Feb 1, 2024
1 parent c6ea8fa commit 52aaa16
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 71 deletions.
6 changes: 3 additions & 3 deletions mm2src/coins/qrc20/script_pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ pub fn generate_contract_call_script_pubkey(

Ok(ScriptBuilder::default()
.push_opcode(Opcode::OP_4)
.push_bytes(&gas_limit)
.push_bytes(&gas_price)
.push_data(&gas_limit)
.push_data(&gas_price)
.push_data(function_call)
.push_bytes(contract_address)
.push_data(contract_address)
.push_opcode(Opcode::OP_CALL)
.into_script())
}
Expand Down
50 changes: 25 additions & 25 deletions mm2src/coins/utxo/swap_proto_v2_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@ pub fn taker_funding_script(
) -> Script {
let mut builder = Builder::default()
.push_opcode(Opcode::OP_IF)
.push_bytes(&time_lock.to_le_bytes())
.push_data(&time_lock.to_le_bytes())
.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
.push_opcode(Opcode::OP_DROP)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_IF)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIGVERIFY)
.push_bytes(maker_pub)
.push_data(maker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_SIZE)
.push_bytes(&[32])
.push_data(&[32])
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_HASH160);

if taker_secret_hash.len() == 32 {
builder = builder.push_bytes(ripemd160(taker_secret_hash).as_slice());
builder = builder.push_data(ripemd160(taker_secret_hash).as_slice());
} else {
builder = builder.push_bytes(taker_secret_hash);
builder = builder.push_data(taker_secret_hash);
}

builder
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ENDIF)
.push_opcode(Opcode::OP_ENDIF)
Expand All @@ -54,28 +54,28 @@ pub fn taker_payment_script(
) -> Script {
let mut builder = Builder::default()
.push_opcode(Opcode::OP_IF)
.push_bytes(&time_lock.to_le_bytes())
.push_data(&time_lock.to_le_bytes())
.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
.push_opcode(Opcode::OP_DROP)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_SIZE)
.push_bytes(&[32])
.push_data(&[32])
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_HASH160);

if maker_secret_hash.len() == 32 {
builder = builder.push_bytes(ripemd160(maker_secret_hash).as_slice());
builder = builder.push_data(ripemd160(maker_secret_hash).as_slice());
} else {
builder = builder.push_bytes(maker_secret_hash);
builder = builder.push_data(maker_secret_hash);
}

builder
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIGVERIFY)
.push_bytes(maker_pub)
.push_data(maker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ENDIF)
.into_script()
Expand All @@ -91,43 +91,43 @@ pub fn maker_payment_script(
) -> Script {
let mut builder = Builder::default()
.push_opcode(Opcode::OP_IF)
.push_bytes(&time_lock.to_le_bytes())
.push_data(&time_lock.to_le_bytes())
.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
.push_opcode(Opcode::OP_DROP)
.push_bytes(maker_pub)
.push_data(maker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_IF)
.push_opcode(Opcode::OP_SIZE)
.push_bytes(&[32])
.push_data(&[32])
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_HASH160);

if maker_secret_hash.len() == 32 {
builder = builder.push_bytes(ripemd160(maker_secret_hash).as_slice());
builder = builder.push_data(ripemd160(maker_secret_hash).as_slice());
} else {
builder = builder.push_bytes(maker_secret_hash);
builder = builder.push_data(maker_secret_hash);
}

builder = builder
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_bytes(taker_pub)
.push_data(taker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_SIZE)
.push_bytes(&[32])
.push_data(&[32])
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_HASH160);

if taker_secret_hash.len() == 32 {
builder = builder.push_bytes(ripemd160(taker_secret_hash).as_slice());
builder = builder.push_data(ripemd160(taker_secret_hash).as_slice());
} else {
builder = builder.push_bytes(taker_secret_hash);
builder = builder.push_data(taker_secret_hash);
}

builder
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_bytes(maker_pub)
.push_data(maker_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ENDIF)
.push_opcode(Opcode::OP_ENDIF)
Expand Down
20 changes: 10 additions & 10 deletions mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4868,26 +4868,26 @@ where
pub fn payment_script(time_lock: u32, secret_hash: &[u8], pub_0: &Public, pub_1: &Public) -> Script {
let mut builder = Builder::default()
.push_opcode(Opcode::OP_IF)
.push_bytes(&time_lock.to_le_bytes())
.push_data(&time_lock.to_le_bytes())
.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
.push_opcode(Opcode::OP_DROP)
.push_bytes(pub_0)
.push_data(pub_0)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_opcode(Opcode::OP_SIZE)
.push_bytes(&[32])
.push_data(&[32])
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_HASH160);

if secret_hash.len() == 32 {
builder = builder.push_bytes(ripemd160(secret_hash).as_slice());
builder = builder.push_data(ripemd160(secret_hash).as_slice());
} else {
builder = builder.push_bytes(secret_hash);
builder = builder.push_data(secret_hash);
}

builder
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_bytes(pub_1)
.push_data(pub_1)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ENDIF)
.into_script()
Expand All @@ -4896,16 +4896,16 @@ pub fn payment_script(time_lock: u32, secret_hash: &[u8], pub_0: &Public, pub_1:
pub fn dex_fee_script(uuid: [u8; 16], time_lock: u32, watcher_pub: &Public, sender_pub: &Public) -> Script {
let builder = Builder::default();
builder
.push_bytes(&uuid)
.push_data(&uuid)
.push_opcode(Opcode::OP_DROP)
.push_opcode(Opcode::OP_IF)
.push_bytes(&time_lock.to_le_bytes())
.push_data(&time_lock.to_le_bytes())
.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
.push_opcode(Opcode::OP_DROP)
.push_bytes(sender_pub)
.push_data(sender_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ELSE)
.push_bytes(watcher_pub)
.push_data(watcher_pub)
.push_opcode(Opcode::OP_CHECKSIG)
.push_opcode(Opcode::OP_ENDIF)
.into_script()
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo_signer/src/sign_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) fn p2pk_spend_with_signature(

TransactionInput {
previous_output: unsigned_input.previous_output,
script_sig: Builder::default().push_bytes(&script_sig).into_bytes(),
script_sig: Builder::default().push_data(&script_sig).into_bytes(),
sequence: unsigned_input.sequence,
script_witness: vec![],
}
Expand Down
24 changes: 5 additions & 19 deletions mm2src/mm2_bitcoin/script/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Builder {
Builder::default()
.push_opcode(Opcode::OP_DUP)
.push_opcode(Opcode::OP_HASH160)
.push_bytes(&address.to_vec())
.push_data(&address.to_vec())
.push_opcode(Opcode::OP_EQUALVERIFY)
.push_opcode(Opcode::OP_CHECKSIG)
.into_script()
Expand All @@ -25,7 +25,7 @@ impl Builder {
/// Builds p2pk script pubkey
pub fn build_p2pk(pubkey: &Public) -> Script {
Builder::default()
.push_bytes(pubkey)
.push_data(pubkey)
.push_opcode(Opcode::OP_CHECKSIG)
.into_script()
}
Expand All @@ -34,7 +34,7 @@ impl Builder {
pub fn build_p2sh(address: &AddressHashEnum) -> Script {
Builder::default()
.push_opcode(Opcode::OP_HASH160)
.push_bytes(&address.to_vec())
.push_data(&address.to_vec())
.push_opcode(Opcode::OP_EQUAL)
.into_script()
}
Expand All @@ -43,15 +43,15 @@ impl Builder {
pub fn build_p2witness(address: &AddressHashEnum) -> Script {
Builder::default()
.push_opcode(Opcode::OP_0)
.push_bytes(&address.to_vec())
.push_data(&address.to_vec())
.into_script()
}

/// Builds op_return script
pub fn build_nulldata(bytes: &[u8]) -> Script {
Builder::default()
.push_opcode(Opcode::OP_RETURN)
.push_bytes(bytes)
.push_data(bytes)
.into_script()
}

Expand All @@ -74,20 +74,6 @@ impl Builder {
/// Appends num push operation to the end of script
pub fn push_num(self, num: Num) -> Self { self.push_data(&num.to_bytes()) }

/// Appends bytes push operation to the end od script
pub fn push_bytes(mut self, bytes: &[u8]) -> Self {
let len = bytes.len();
if !(1..=75).contains(&len) {
panic!("Can not push {} bytes", len);
}

let opcode: Opcode = Opcode::from_u8(((Opcode::OP_PUSHBYTES_1 as usize) + len - 1) as u8)
.expect("value is within [OP_PUSHBYTES_1; OP_PUSHBYTES_75] interval; qed");
self.data.push(opcode as u8);
self.data.extend_from_slice(bytes);
self
}

/// Appends data push operation to the end of script
pub fn push_data(mut self, data: &[u8]) -> Self {
let len = data.len();
Expand Down
26 changes: 13 additions & 13 deletions mm2src/mm2_bitcoin/script/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ OP_ADD
let pubkey_bytes = [0; 33];
let address = Public::from_slice(&pubkey_bytes).unwrap().address_hash();
let script = Builder::default()
.push_bytes(&pubkey_bytes)
.push_data(&pubkey_bytes)
.push_opcode(Opcode::OP_CHECKSIG)
.into_script();
assert_eq!(script.script_type(), ScriptType::PubKey);
Expand All @@ -778,7 +778,7 @@ OP_ADD
let pubkey_bytes = [0; 65];
let address = Public::from_slice(&pubkey_bytes).unwrap().address_hash();
let script = Builder::default()
.push_bytes(&pubkey_bytes)
.push_data(&pubkey_bytes)
.push_opcode(Opcode::OP_CHECKSIG)
.into_script();
assert_eq!(script.script_type(), ScriptType::PubKey);
Expand Down Expand Up @@ -854,8 +854,8 @@ OP_ADD
let address2 = Public::from_slice(&pubkey2_bytes).unwrap().address_hash();
let script = Builder::default()
.push_opcode(Opcode::OP_2)
.push_bytes(&pubkey1_bytes)
.push_bytes(&pubkey2_bytes)
.push_data(&pubkey1_bytes)
.push_data(&pubkey2_bytes)
.push_opcode(Opcode::OP_2)
.push_opcode(Opcode::OP_CHECKMULTISIG)
.into_script();
Expand All @@ -873,10 +873,10 @@ OP_ADD
fn test_num_signatures_required() {
let script = Builder::default()
.push_opcode(Opcode::OP_3)
.push_bytes(&[0; 33])
.push_bytes(&[0; 65])
.push_bytes(&[0; 65])
.push_bytes(&[0; 65])
.push_data(&[0; 33])
.push_data(&[0; 65])
.push_data(&[0; 65])
.push_data(&[0; 65])
.push_opcode(Opcode::OP_4)
.push_opcode(Opcode::OP_CHECKMULTISIG)
.into_script();
Expand All @@ -885,7 +885,7 @@ OP_ADD

let script = Builder::default()
.push_opcode(Opcode::OP_HASH160)
.push_bytes(&[0; 20])
.push_data(&[0; 20])
.push_opcode(Opcode::OP_EQUAL)
.into_script();
assert_eq!(script.script_type(), ScriptType::ScriptHash);
Expand All @@ -897,9 +897,9 @@ OP_ADD
// Builder::default()
// .push_opcode(Opcode::OP_4)
// .push_opcode(Opcode::OP_HASH160)
// .push_bytes(&[0; 20])
// .push_data(&[0; 20])
// .push_opcode(Opcode::_F9) // Bad opcode - 0xf9
// .push_bytes(&[1; 20])
// .push_data(&[1; 20])
// .push_opcode(Opcode::OP_EQUAL)
// is the same as following:
let script: Script =
Expand Down Expand Up @@ -933,9 +933,9 @@ OP_ADD
// Builder::default()
// .push_opcode(Opcode::OP_4)
// .push_opcode(Opcode::OP_HASH160)
// .push_bytes(&[0; 20])
// .push_data(&[0; 20])
// .push_opcode(Opcode::_F9) // Bad opcode - 0xf9
// .push_bytes(&[1; 20])
// .push_data(&[1; 20])
// .push_opcode(Opcode::OP_EQUAL)
// is the same as following:
let script: Script =
Expand Down

0 comments on commit 52aaa16

Please sign in to comment.