From 4ae49eb3512cfc2d4ee54f3fb4e5df4a5a091cb2 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Tue, 30 Jul 2024 01:10:01 +0530 Subject: [PATCH 1/8] Implement OP_SWAP & OP_2SWAP opcodes --- .vscode/settings.json | 8 +++ src/compiler.cairo | 2 + src/opcodes/opcodes.cairo | 24 ++++++- src/opcodes/tests/test_opcodes.cairo | 102 +++++++++++++++++++++++++++ 4 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..24b37d15 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "editor.defaultFormatter": null, + "cairo1.scarbPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", + "cairo1.corelibPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", + "cairo1.languageServerPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", + "cairo.cairoFormatPath": "./Scarb.toml", + "cairo1.enableLanguageServer": true +} diff --git a/src/compiler.cairo b/src/compiler.cairo index 2a2aa5d4..d6f54e89 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -41,7 +41,9 @@ pub impl CompilerTraitImpl of CompilerTrait { opcodes.insert('OP_ELSE', Opcode::OP_ELSE); opcodes.insert('OP_ENDIF', Opcode::OP_ENDIF); opcodes.insert('OP_FROMALTSTACK', Opcode::OP_FROMALTSTACK); + opcodes.insert('OP_2SWAP',Opcode::OP_2SWAP); opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH); + opcodes.insert('OP_SWAP',Opcode::OP_SWAP); opcodes.insert('OP_1ADD', Opcode::OP_1ADD); opcodes.insert('OP_1SUB', Opcode::OP_1SUB); opcodes.insert('OP_NEGATE', Opcode::OP_NEGATE); diff --git a/src/opcodes/opcodes.cairo b/src/opcodes/opcodes.cairo index 4ebb4375..a72b90e6 100644 --- a/src/opcodes/opcodes.cairo +++ b/src/opcodes/opcodes.cairo @@ -22,7 +22,9 @@ pub mod Opcode { pub const OP_ELSE: u8 = 103; pub const OP_ENDIF: u8 = 104; pub const OP_FROMALTSTACK: u8 = 108; + pub const OP_2SWAP: u8 = 114; pub const OP_DEPTH: u8 = 116; + pub const OP_SWAP: u8 = 124; pub const OP_1ADD: u8 = 139; pub const OP_1SUB: u8 = 140; pub const OP_NEGATE: u8 = 143; @@ -155,7 +157,7 @@ pub mod Opcode { 111 => not_implemented(ref engine), 112 => not_implemented(ref engine), 113 => not_implemented(ref engine), - 114 => not_implemented(ref engine), + 114 => opcode_2swap(ref engine), 115 => not_implemented(ref engine), 116 => opcode_depth(ref engine), 117 => not_implemented(ref engine), @@ -165,7 +167,7 @@ pub mod Opcode { 121 => not_implemented(ref engine), 122 => not_implemented(ref engine), 123 => not_implemented(ref engine), - 124 => not_implemented(ref engine), + 124 => opcode_swap(ref engine), 125 => not_implemented(ref engine), 126 => not_implemented(ref engine), 127 => not_implemented(ref engine), @@ -308,6 +310,24 @@ pub mod Opcode { engine.dstack.push_int(depth); } + fn opcode_swap(ref engine: Engine) { + let a = engine.dstack.pop_int(); + let b = engine.dstack.pop_int(); + engine.dstack.push_int(a); + engine.dstack.push_int(b); + } + + fn opcode_2swap(ref engine: Engine) { + let a = engine.dstack.pop_int(); + let b = engine.dstack.pop_int(); + let c = engine.dstack.pop_int(); + let d = engine.dstack.pop_int(); + engine.dstack.push_int(b); + engine.dstack.push_int(a); + engine.dstack.push_int(d); + engine.dstack.push_int(c); + } + fn opcode_1add(ref engine: Engine) { let value = engine.dstack.pop_int(); let result = value + 1; diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 3728a511..3c2e712e 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -362,6 +362,108 @@ fn test_op_depth_empty_stack() { assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack"); } +#[test] +fn test_op_swap() { + let program = "OP_1 OP_2 OP_3 OP_SWAP"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let res = engine.step(); + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 3, "Stack length is not 1"); + + let expected_stack = array!["\x02", "\x03", "\x01"]; + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); +} + +#[test] +fn test_op_swap_mid() { + let program = "OP_1 OP_2 OP_3 OP_SWAP OP_4 OP_5"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let res = engine.step(); + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 5, "Stack length is not 1"); + + let expected_stack = array!["\x05", "\x04", "\x02","\x03","\x01"]; + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); +} + + +#[test] +fn test_op_2swap() { + let program = "OP_1 OP_2 OP_3 OP_4 OP_2SWAP"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let _ = engine.step(); //push 3 + let _ = engine.step(); //push 4 + let dstack = engine.get_dstack(); + let expected_stack = array!["\x04", "\x03", "\x02", "\x01"]; + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); + + + let res = engine.step(); //execute op_2swap + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 4, "Stack length is not 1"); + + let expected_stack = array!["\x02","\x01" ,"\x04", "\x03"]; + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); +} + +#[test] +fn test_op_2swap_mid() { + let program = "OP_1 OP_2 OP_3 OP_4 OP_2SWAP OP_5 OP_6"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let _ = engine.step(); //push 3 + let _ = engine.step(); //push 4 + let _ = engine.step(); //execute op_2swap + let _ = engine.step(); //push 5 + let res = engine.step(); //push 6 + let dstack = engine.get_dstack(); + + assert!(res, "Execution of step failed"); + assert_eq!(dstack.len(), 6, "Stack length is not 1"); + + let expected_stack = array!["\x06", "\x05", "\x02", "\x01", "\x04", "\x03"]; + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); +} + +#[test] +fn test_op_2swap_underflow() { + let program = "OP_1 OP_2 OP_3 OP_2SWAP"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let res = engine.step(); //push 3 + + assert!(res, "pop_byte_array: stack underflow"); +} + #[test] fn test_op_not() { let program = "OP_1 OP_NOT"; From a614790ffaa733a04387a5f1a3b9120705705cf5 Mon Sep 17 00:00:00 2001 From: Varun Doshi <61531351+varun-doshi@users.noreply.github.com> Date: Tue, 30 Jul 2024 01:16:55 +0530 Subject: [PATCH 2/8] Update --- .vscode/settings.json | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 24b37d15..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "editor.defaultFormatter": null, - "cairo1.scarbPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo1.corelibPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo1.languageServerPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo.cairoFormatPath": "./Scarb.toml", - "cairo1.enableLanguageServer": true -} From 57d5b056670910c3570bee6130856e32bd5a5f62 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Tue, 30 Jul 2024 01:22:39 +0530 Subject: [PATCH 3/8] scarb fmt --check successful --- .vscode/settings.json | 8 ------ src/compiler.cairo | 4 +-- src/opcodes/tests/test_opcodes.cairo | 43 ++++++++++++++-------------- 3 files changed, 23 insertions(+), 32 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 24b37d15..00000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "editor.defaultFormatter": null, - "cairo1.scarbPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo1.corelibPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo1.languageServerPath": "/home/varunx/.local/share/scarb-install/latest/bin/scarb", - "cairo.cairoFormatPath": "./Scarb.toml", - "cairo1.enableLanguageServer": true -} diff --git a/src/compiler.cairo b/src/compiler.cairo index d6f54e89..b1740c43 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -41,9 +41,9 @@ pub impl CompilerTraitImpl of CompilerTrait { opcodes.insert('OP_ELSE', Opcode::OP_ELSE); opcodes.insert('OP_ENDIF', Opcode::OP_ENDIF); opcodes.insert('OP_FROMALTSTACK', Opcode::OP_FROMALTSTACK); - opcodes.insert('OP_2SWAP',Opcode::OP_2SWAP); + opcodes.insert('OP_2SWAP', Opcode::OP_2SWAP); opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH); - opcodes.insert('OP_SWAP',Opcode::OP_SWAP); + opcodes.insert('OP_SWAP', Opcode::OP_SWAP); opcodes.insert('OP_1ADD', Opcode::OP_1ADD); opcodes.insert('OP_1SUB', Opcode::OP_1SUB); opcodes.insert('OP_NEGATE', Opcode::OP_NEGATE); diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 3c2e712e..42e60c17 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -398,7 +398,7 @@ fn test_op_swap_mid() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 5, "Stack length is not 1"); - let expected_stack = array!["\x05", "\x04", "\x02","\x03","\x01"]; + let expected_stack = array!["\x05", "\x04", "\x02", "\x03", "\x01"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } @@ -409,22 +409,21 @@ fn test_op_2swap() { let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); let mut engine = EngineTraitImpl::new(bytecode); - let _ = engine.step(); //push 1 - let _ = engine.step(); //push 2 - let _ = engine.step(); //push 3 - let _ = engine.step(); //push 4 + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let _ = engine.step(); //push 3 + let _ = engine.step(); //push 4 let dstack = engine.get_dstack(); let expected_stack = array!["\x04", "\x03", "\x02", "\x01"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); - - - let res = engine.step(); //execute op_2swap + + let res = engine.step(); //execute op_2swap assert!(res, "Execution of step failed"); - + let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 4, "Stack length is not 1"); - let expected_stack = array!["\x02","\x01" ,"\x04", "\x03"]; + let expected_stack = array!["\x02", "\x01", "\x04", "\x03"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } @@ -434,13 +433,13 @@ fn test_op_2swap_mid() { let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); let mut engine = EngineTraitImpl::new(bytecode); - let _ = engine.step(); //push 1 - let _ = engine.step(); //push 2 - let _ = engine.step(); //push 3 - let _ = engine.step(); //push 4 - let _ = engine.step(); //execute op_2swap - let _ = engine.step(); //push 5 - let res = engine.step(); //push 6 + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let _ = engine.step(); //push 3 + let _ = engine.step(); //push 4 + let _ = engine.step(); //execute op_2swap + let _ = engine.step(); //push 5 + let res = engine.step(); //push 6 let dstack = engine.get_dstack(); assert!(res, "Execution of step failed"); @@ -456,12 +455,12 @@ fn test_op_2swap_underflow() { let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); let mut engine = EngineTraitImpl::new(bytecode); - - let _ = engine.step(); //push 1 - let _ = engine.step(); //push 2 - let res = engine.step(); //push 3 - assert!(res, "pop_byte_array: stack underflow"); + let _ = engine.step(); //push 1 + let _ = engine.step(); //push 2 + let res = engine.step(); //push 3 + + assert!(res, "pop_byte_array: stack underflow"); } #[test] From 58581a9e84d1dcd4c4ee5b2f89683ac6eb37f710 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Wed, 31 Jul 2024 06:15:17 +0530 Subject: [PATCH 4/8] update test to use should_panic macro --- src/opcodes/tests/test_opcodes.cairo | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 42e60c17..ef7ce51d 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -450,6 +450,7 @@ fn test_op_2swap_mid() { } #[test] +#[should_panic] fn test_op_2swap_underflow() { let program = "OP_1 OP_2 OP_3 OP_2SWAP"; let mut compiler = CompilerTraitImpl::new(); @@ -458,9 +459,9 @@ fn test_op_2swap_underflow() { let _ = engine.step(); //push 1 let _ = engine.step(); //push 2 - let res = engine.step(); //push 3 + let _ = engine.step(); //push 3 + let res = engine.step(); //OP_2SWAP - assert!(res, "pop_byte_array: stack underflow"); } #[test] From cec4923918d8b0978518432b6cd40d7913706989 Mon Sep 17 00:00:00 2001 From: Varun Doshi Date: Wed, 31 Jul 2024 06:27:28 +0530 Subject: [PATCH 5/8] scarb fmt --- src/opcodes/tests/test_opcodes.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 0091d4ed..51daface 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -543,7 +543,6 @@ fn test_op_2swap_underflow() { let _ = engine.step(); //push 2 let _ = engine.step(); //push 3 let res = engine.step(); //OP_2SWAP - } #[test] From 786262d67ae7338ddb9d3205a8f2e9ddfe8785f2 Mon Sep 17 00:00:00 2001 From: Varun Doshi Date: Wed, 31 Jul 2024 07:05:21 +0530 Subject: [PATCH 6/8] refactored test after stack_to_span change --- src/opcodes/tests/test_opcodes.cairo | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 51daface..09dae4a7 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -459,10 +459,11 @@ fn test_op_swap() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 3, "Stack length is not 1"); - let expected_stack = array!["\x02", "\x03", "\x01"]; + let expected_stack = array!["\x01", "\x03", "\x02",]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } + #[test] fn test_op_swap_mid() { let program = "OP_1 OP_2 OP_3 OP_SWAP OP_4 OP_5"; @@ -480,7 +481,7 @@ fn test_op_swap_mid() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 5, "Stack length is not 1"); - let expected_stack = array!["\x05", "\x04", "\x02", "\x03", "\x01"]; + let expected_stack = array!["\x01", "\x03", "\x02", "\x04", "\x05"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } @@ -496,7 +497,7 @@ fn test_op_2swap() { let _ = engine.step(); //push 3 let _ = engine.step(); //push 4 let dstack = engine.get_dstack(); - let expected_stack = array!["\x04", "\x03", "\x02", "\x01"]; + let expected_stack = array!["\x01", "\x02", "\x03", "\x04"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); let res = engine.step(); //execute op_2swap @@ -505,7 +506,7 @@ fn test_op_2swap() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 4, "Stack length is not 1"); - let expected_stack = array!["\x02", "\x01", "\x04", "\x03"]; + let expected_stack = array!["\x03", "\x04", "\x01", "\x02"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } @@ -527,7 +528,7 @@ fn test_op_2swap_mid() { assert!(res, "Execution of step failed"); assert_eq!(dstack.len(), 6, "Stack length is not 1"); - let expected_stack = array!["\x06", "\x05", "\x02", "\x01", "\x04", "\x03"]; + let expected_stack = array!["\x03", "\x04", "\x01", "\x02", "\x05", "\x06"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } From b4225843821cf046de5656a9a952b29783f2df96 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Mon, 25 Nov 2024 16:10:04 +0530 Subject: [PATCH 7/8] resolved test 4 fails --- packages/tests/src/tests/test_p2sh.cairo | 5 ++--- packages/tests/src/validate.cairo | 15 +++++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/tests/src/tests/test_p2sh.cairo b/packages/tests/src/tests/test_p2sh.cairo index 94a02ddc..692df419 100644 --- a/packages/tests/src/tests/test_p2sh.cairo +++ b/packages/tests/src/tests/test_p2sh.cairo @@ -66,8 +66,7 @@ fn test_p2sh_transaction_3() { assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err()); } -// TODO: Fix this test -#[ignore] + #[test] fn test_p2sh_transaction_4() { //https://learnmeabitcoin.com/explorer/tx/cc11ca9e9dc188663c41eb23b15370f68eded56b7ec54dd5bc4f2d2ae93addb2 @@ -84,7 +83,7 @@ fn test_p2sh_transaction_4() { let utxo_hints = array![prev_out]; - let res = validate_p2sh(@transaction, 0, utxo_hints, 0); + let res = validate_p2sh(@transaction, 1, utxo_hints, 0); assert!(res.is_ok(), "P2SH failed!:{:?}", res.unwrap_err()); } diff --git a/packages/tests/src/validate.cairo b/packages/tests/src/validate.cairo index ead62ef9..3ab6c17c 100644 --- a/packages/tests/src/validate.cairo +++ b/packages/tests/src/validate.cairo @@ -164,14 +164,21 @@ pub fn validate_p2sh( let mut redeem_script_size = 0; if scriptSig_bytes[0] == 0 || scriptSig_bytes[0] == 1 || scriptSig_bytes[0] == 2 { //OP_0 OP_PushData OP_PushData Standard locking scripts - redeem_Script_start_index = (2 + scriptSig_bytes[1] + 1).into(); + if(flags==0){ + redeem_Script_start_index = (2 + scriptSig_bytes[1] + 1).into(); + }else if(flags==1){ + + redeem_Script_start_index = (1+1 + scriptSig_bytes[1] + 1+ + scriptSig_bytes[(1+1 + scriptSig_bytes[1]).into()]+ + scriptSig_bytes[(1+1 + scriptSig_bytes[1]+scriptSig_bytes[(1+1 + scriptSig_bytes[1]).into()]).into()]+1).into(); + } redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index; } else { // non-standard locking script containing a mathematical puzzle redeem_Script_start_index = find_last_index(scriptSig_bytes.clone()); redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index; } - + let redeem_script = sub_byte_array( @scriptSig_bytes, ref redeem_Script_start_index, redeem_script_size ); @@ -181,11 +188,11 @@ pub fn validate_p2sh( if redeem_script.len() > 520 { return Result::Err('P2SH: Redeem Script size > 520'); } - + let hashed_redeem_script: ByteArray = ripemd160::ripemd160_hash( @sha256_byte_array(@redeem_script) ) - .into(); + .into(); let script_pubkey = utxo_hints[0].pubkey_script; let mut script_hash_start_index = 2; From 7fc3ffa626586dde3b86d82133bb751cfff363b4 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Mon, 25 Nov 2024 16:13:42 +0530 Subject: [PATCH 8/8] fmt --- packages/tests/src/validate.cairo | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/tests/src/validate.cairo b/packages/tests/src/validate.cairo index 3ab6c17c..6ffa514e 100644 --- a/packages/tests/src/validate.cairo +++ b/packages/tests/src/validate.cairo @@ -164,13 +164,22 @@ pub fn validate_p2sh( let mut redeem_script_size = 0; if scriptSig_bytes[0] == 0 || scriptSig_bytes[0] == 1 || scriptSig_bytes[0] == 2 { //OP_0 OP_PushData OP_PushData Standard locking scripts - if(flags==0){ + if (flags == 0) { redeem_Script_start_index = (2 + scriptSig_bytes[1] + 1).into(); - }else if(flags==1){ - - redeem_Script_start_index = (1+1 + scriptSig_bytes[1] + 1+ - scriptSig_bytes[(1+1 + scriptSig_bytes[1]).into()]+ - scriptSig_bytes[(1+1 + scriptSig_bytes[1]+scriptSig_bytes[(1+1 + scriptSig_bytes[1]).into()]).into()]+1).into(); + } else if (flags == 1) { + redeem_Script_start_index = + (1 + + 1 + + scriptSig_bytes[1] + + 1 + + scriptSig_bytes[(1 + 1 + scriptSig_bytes[1]).into()] + + scriptSig_bytes[(1 + + 1 + + scriptSig_bytes[1] + + scriptSig_bytes[(1 + 1 + scriptSig_bytes[1]).into()]) + .into()] + + 1) + .into(); } redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index; } else { @@ -178,7 +187,7 @@ pub fn validate_p2sh( redeem_Script_start_index = find_last_index(scriptSig_bytes.clone()); redeem_script_size = (scriptSig_bytes.len()) - redeem_Script_start_index; } - + let redeem_script = sub_byte_array( @scriptSig_bytes, ref redeem_Script_start_index, redeem_script_size ); @@ -188,11 +197,11 @@ pub fn validate_p2sh( if redeem_script.len() > 520 { return Result::Err('P2SH: Redeem Script size > 520'); } - + let hashed_redeem_script: ByteArray = ripemd160::ripemd160_hash( @sha256_byte_array(@redeem_script) ) - .into(); + .into(); let script_pubkey = utxo_hints[0].pubkey_script; let mut script_hash_start_index = 2;