Skip to content

Commit

Permalink
add InvalidNonScriptData error
Browse files Browse the repository at this point in the history
  • Loading branch information
scrypt committed Oct 24, 2023
1 parent d0af72e commit 6e2c304
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ pub enum BSVErrors {
#[error("Error deserialising TxOut field {0}: {1}")]
DeserialiseTxOut(String, #[source] std::io::Error),

#[error("NonScriptData can only appear after an OP_RETURN that is not in a branch block!")]
InvalidNonScriptData(),

#[error("Error serialising TxOut field {0}: {1}")]
SerialiseTxOut(String, #[source] std::io::Error),

Expand Down
18 changes: 10 additions & 8 deletions src/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,6 @@ impl Script {
fn map_string_to_script_bit(code: &str, is_non_script_data: bool) -> Result<ScriptBit, BSVErrors> {
let code = code.trim();

if is_non_script_data {
if code.starts_with("non-script-data:") {
let non_script_data = hex::decode(code.trim_start_matches("non-script-data:"))?;
return Ok(ScriptBit::NonScriptData(non_script_data));
}
}

// Number OP_CODES
match code {
"0" => return Ok(ScriptBit::OpCode(OpCodes::OP_0)),
Expand Down Expand Up @@ -274,6 +267,15 @@ impl Script {
return Ok(ScriptBit::OpCode(opcode));
}

if code.starts_with("non-script-data:") {
if is_non_script_data {
let non_script_data = hex::decode(code.trim_start_matches("non-script-data:"))?;
return Ok(ScriptBit::NonScriptData(non_script_data));
} else {
return Err(BSVErrors::InvalidNonScriptData());
}
}

// PUSHDATA OP_CODES
let data_bytes = hex::decode(code)?;
let bit = match VarInt::get_pushdata_opcode(data_bytes.len() as u64) {
Expand Down Expand Up @@ -340,7 +342,7 @@ impl Script {
}

/**
* Ordinary ASM, (for example, OP_RETURN 01 01) does not contain ScriptBit::NonScriptData after being converted into ScriptBit.
* Ordinary ASM, (for example, OP_RETURN 01 01) does not contain ScriptBit::NonScriptData after being converted into ScriptBit.
* This function wraps all ScriptBit after OP_RETURN with ScriptBit::NonScriptData.
*/
fn wrap_with_non_script_data(bits_iter: &mut Iter<ScriptBit>, non_script_data_index: usize) -> Vec<ScriptBit> {
Expand Down
13 changes: 11 additions & 2 deletions tests/script.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(test)]
mod script_tests {
use bsv::{Hash, OpCodes, P2PKHAddress, Script, ScriptBit};
use bsv::{BSVErrors, Hash, OpCodes, P2PKHAddress, Script, ScriptBit};
// #[test]
// #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
// fn to_hex_string() {
Expand Down Expand Up @@ -540,7 +540,6 @@ mod script_tests {
assert_eq!(&script.to_hex(), "006a0051");
}


#[test]
fn non_script_data_asm() {
let script = Script::from_asm_string("0 OP_RETURN 01 0102").unwrap();
Expand All @@ -554,4 +553,14 @@ mod script_tests {
assert_eq!(&script.to_hex(), "006a0101020102");
}

#[test]
fn invalid_non_script_data() {
let result = Script::from_asm_string("0 non-script-data:0101020102 01 0102 OP_RETURN");

assert!(matches!(result, Err(BSVErrors::InvalidNonScriptData())));

let result = Script::from_asm_string("OP_1 OP_IF OP_1 OP_RETURN non-script-data:0101020102 01 0102 OP_ENDIF OP_1");

assert!(matches!(result, Err(BSVErrors::InvalidNonScriptData())));
}
}

0 comments on commit 6e2c304

Please sign in to comment.