Skip to content

Commit

Permalink
Feat: Refactoring & Witness Sigs (#260)
Browse files Browse the repository at this point in the history
<!-- enter the gh issue after hash -->

- [ ] issue #
- [ ] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [ ] code change includes tests

<!-- PR description below -->
  • Loading branch information
b-j-roberts authored Oct 16, 2024
1 parent 059d098 commit ce878fe
Show file tree
Hide file tree
Showing 49 changed files with 2,331 additions and 1,242 deletions.
9 changes: 9 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ version = "0.1.0"
dependencies = [
"shinigami_compiler",
"shinigami_engine",
"shinigami_tests",
"shinigami_utils",
]

Expand All @@ -34,7 +35,15 @@ version = "0.1.0"
dependencies = [
"ripemd160",
"sha1",
"shinigami_utils",
]

[[package]]
name = "shinigami_tests"
version = "0.1.0"
dependencies = [
"shinigami_compiler",
"shinigami_engine",
"shinigami_utils",
]

Expand Down
1 change: 1 addition & 0 deletions packages/cmds/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2024_07"
[dependencies]
shinigami_compiler = { path = "../compiler" }
shinigami_engine = { path = "../engine" }
shinigami_tests = { path = "../tests" }
shinigami_utils = { path = "../utils" }

[dev-dependencies]
Expand Down
29 changes: 16 additions & 13 deletions packages/cmds/src/main.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use shinigami_compiler::compiler::CompilerImpl;
use shinigami_engine::engine::{EngineImpl, EngineInternalImpl};
use shinigami_engine::transaction::{TransactionImpl, TransactionTrait};
use shinigami_engine::utxo::UTXO;
use shinigami_engine::validate;
use shinigami_engine::scriptflags;
use shinigami_engine::transaction::{EngineInternalTransactionImpl, EngineInternalTransactionTrait};
use shinigami_engine::flags;
use shinigami_engine::witness;
use shinigami_engine::hash_cache::HashCacheImpl;
use shinigami_utils::byte_array::felt252_to_byte_array;
use shinigami_utils::bytecode::hex_to_bytecode;
use shinigami_tests::utxo::UTXO;
use shinigami_tests::validate;

#[derive(Clone, Drop)]
struct InputData {
Expand Down Expand Up @@ -41,8 +41,8 @@ fn run_with_flags(input: InputDataWithFlags) -> Result<(), felt252> {
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = TransactionImpl::new_signed(script_sig);
let flags = scriptflags::parse_flags(input.Flags);
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, 0, @hash_cache)?;
let _ = engine.execute()?;
Expand All @@ -62,10 +62,13 @@ fn run_with_witness(input: InputDataWithWitness) -> Result<(), felt252> {
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let witness = witness::parse_witness_input(input.Witness);
let tx = TransactionImpl::new_signed_witness(script_sig, witness);
let flags = scriptflags::parse_flags(input.Flags);
let value = 1; // TODO
let tx = EngineInternalTransactionImpl::new_signed_witness(
script_sig, script_pubkey.clone(), witness, value
);
let flags = flags::parse_flags(input.Flags);
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, 0, @hash_cache)?;
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, flags, value, @hash_cache)?;
let _ = engine.execute()?;
Result::Ok(())
}
Expand All @@ -80,7 +83,7 @@ fn run(input: InputData) -> Result<(), felt252> {
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = TransactionImpl::new_signed(script_sig);
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let _ = engine.execute()?;
Expand All @@ -97,7 +100,7 @@ fn run_with_json(input: InputData) -> Result<(), felt252> {
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = TransactionImpl::new_signed(script_sig);
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let _ = engine.execute()?;
Expand All @@ -115,7 +118,7 @@ fn debug(input: InputData) -> Result<bool, felt252> {
let script_pubkey = compiler.compile(input.ScriptPubKey)?;
let compiler = CompilerImpl::new();
let script_sig = compiler.compile(input.ScriptSig)?;
let tx = TransactionImpl::new_signed(script_sig);
let tx = EngineInternalTransactionImpl::new_signed(script_sig, script_pubkey.clone());
let hash_cache = HashCacheImpl::new(@tx);
let mut engine = EngineImpl::new(@script_pubkey, @tx, 0, 0, 0, @hash_cache)?;
let mut res = Result::Ok(true);
Expand Down Expand Up @@ -197,7 +200,7 @@ struct ValidateRawInput {
fn run_raw_transaction(mut input: ValidateRawInput) -> u8 {
println!("Running Bitcoin Script with raw transaction: '{}'", input.raw_transaction);
let raw_transaction = hex_to_bytecode(@input.raw_transaction);
let transaction = TransactionTrait::deserialize(raw_transaction);
let transaction = EngineInternalTransactionTrait::deserialize(raw_transaction);
let mut utxo_hints = array![];
for hint in input
.utxo_hints
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub impl CompilerImpl of CompilerTrait {
let mut compiler = Compiler { opcodes: Default::default() };
// Add the opcodes to the dict
compiler.add_opcode('OP_0', Opcode::OP_0);
compiler.add_opcode('OP_FALSE', Opcode::OP_0);
compiler.add_opcode('OP_FALSE', Opcode::OP_FALSE);
compiler.add_opcode('OP_DATA_1', Opcode::OP_DATA_1);
compiler.add_opcode('OP_DATA_2', Opcode::OP_DATA_2);
compiler.add_opcode('OP_DATA_3', Opcode::OP_DATA_3);
Expand Down
1 change: 0 additions & 1 deletion packages/engine/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2024_07"
[dependencies]
ripemd160.workspace = true
sha1.workspace = true
shinigami_compiler = { path = "../compiler" }
shinigami_utils = { path = "../utils" }

[dev-dependencies]
Expand Down
Loading

0 comments on commit ce878fe

Please sign in to comment.