diff --git a/script/src/builder.rs b/script/src/builder.rs index 5b363899..33f6c052 100644 --- a/script/src/builder.rs +++ b/script/src/builder.rs @@ -31,6 +31,14 @@ impl Builder { .into_script() } + /// Builds op_return script + pub fn build_nulldata(bytes: &[u8]) -> Script { + Builder::default() + .push_opcode(Opcode::OP_RETURN) + .push_bytes(bytes) + .into_script() + } + /// Pushes opcode to the end of script pub fn push_opcode(mut self, opcode: Opcode) -> Self { self.data.push(opcode as u8); @@ -94,7 +102,16 @@ impl Builder { /// Appends `OP_RETURN` operation to the end of script pub fn return_bytes(mut self, bytes: &[u8]) -> Self { + let len = bytes.len(); + if len < 1 || len > 75 { + panic!(format!("Canot 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::OP_RETURN as u8); + self.data.push(opcode as u8); self.data.extend_from_slice(bytes); self }