OP_RETURN script initializer

This commit is contained in:
Dr Maxim Orlovsky 2020-09-03 23:48:25 +02:00
parent 9143fd3888
commit e33cdcca14
1 changed files with 16 additions and 1 deletions

View File

@ -267,6 +267,14 @@ impl Script {
.into_script() .into_script()
} }
/// Generates OP_RETURN-type of scriptPubkey for a given data
pub fn with_op_return(data: &Vec<u8>) -> Self {
Builder::new()
.push_opcode(opcodes::all::OP_RETURN)
.push_slice(&data)
.into_script()
}
/// The length in bytes of the script /// The length in bytes of the script
pub fn len(&self) -> usize { self.0.len() } pub fn len(&self) -> usize { self.0.len() }
@ -821,7 +829,7 @@ mod test {
use super::*; use super::*;
use super::build_scriptint; use super::build_scriptint;
use hashes::hex::FromHex; use hashes::hex::{FromHex, ToHex};
use consensus::encode::{deserialize, serialize}; use consensus::encode::{deserialize, serialize};
use blockdata::opcodes; use blockdata::opcodes;
use util::key::PublicKey; use util::key::PublicKey;
@ -898,6 +906,13 @@ mod test {
let p2wsh = Script::with_v0_wsh(&wscript_hash); let p2wsh = Script::with_v0_wsh(&wscript_hash);
assert!(p2wsh.is_v0_p2wsh()); assert!(p2wsh.is_v0_p2wsh());
assert_eq!(script.to_v0_p2wsh(), p2wsh); assert_eq!(script.to_v0_p2wsh(), p2wsh);
// Test data are taken from the second output of
// 2ccb3a1f745eb4eefcf29391460250adda5fab78aaddb902d25d3cd97d9d8e61 transaction
let data = Vec::<u8>::from_hex("aa21a9ed20280f53f2d21663cac89e6bd2ad19edbabb048cda08e73ed19e9268d0afea2a").unwrap();
let op_return = Script::with_op_return(&data);
assert!(op_return.is_op_return());
assert_eq!(op_return.to_hex(), "6a24aa21a9ed20280f53f2d21663cac89e6bd2ad19edbabb048cda08e73ed19e9268d0afea2a");
} }
#[test] #[test]