diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index b2c80e3a..aa970c0b 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -267,6 +267,14 @@ impl Script { .into_script() } + /// Generates OP_RETURN-type of scriptPubkey for a given data + pub fn with_op_return(data: &Vec) -> Self { + Builder::new() + .push_opcode(opcodes::all::OP_RETURN) + .push_slice(&data) + .into_script() + } + /// The length in bytes of the script pub fn len(&self) -> usize { self.0.len() } @@ -821,7 +829,7 @@ mod test { use super::*; use super::build_scriptint; - use hashes::hex::FromHex; + use hashes::hex::{FromHex, ToHex}; use consensus::encode::{deserialize, serialize}; use blockdata::opcodes; use util::key::PublicKey; @@ -898,6 +906,13 @@ mod test { let p2wsh = Script::with_v0_wsh(&wscript_hash); assert!(p2wsh.is_v0_p2wsh()); assert_eq!(script.to_v0_p2wsh(), p2wsh); + + // Test data are taken from the second output of + // 2ccb3a1f745eb4eefcf29391460250adda5fab78aaddb902d25d3cd97d9d8e61 transaction + let data = Vec::::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]