Skip to content

Commit

Permalink
🔨 Feature/sha256 (#240)
Browse files Browse the repository at this point in the history
* 0.3.26

* 🔍 Experiment Sub Opcodes with Base 16

* 🔧 Fix CodeCopy Opcode

* 🔨 Add SHL Opcode

* 🔨 Add peek_step Function to Machine

* 🔧 Remove CodeCopy Opcode Pop

* 🔧 Fix NOT Opcode

* 🔧 Fix ADD and MUL Opcodes

* 🔧 Fix DIV Opcode

* 🔍 Inspect CALLER opcode

* docs: add opcodes

* 🔨 Add get_window_data function

* 🔨 Add Padding to Memory

* 🔨 Add filter_left_zeros Function

* 🔧 Fix MLOAD Opcode

* 🔨 Add Test

* 🔧 Fix CODECOPY Opcode

* 🔧 Fix COPYCODE Opcode Memory Allocation

* 🔨 Add EXM SHA256 Function

* 🔧 Employ Cargo Fmt

* 🔧 Retest

* 🔨 Create SmartWeave convertString Function

* 🔨 Build convertTo Function for Smartweave

* 🔍 Inspect CI Test

* 🔨 Run Cargo FMT

---------

Co-authored-by: Darwin <[email protected]>
  • Loading branch information
Roaring30s and charmful0x committed Dec 12, 2023
1 parent 66977f2 commit 3c0ffd8
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 42 deletions.
191 changes: 155 additions & 36 deletions crates/evm/lib.rs

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions crates/exm/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@
testDelKv() {
return this.kv;
}

async sha256(buffer) {
return subtle.digest('SHA-256', buffer).then((hashBuffer) => {
const hashArray = Array.from(new Uint8Array(hashBuffer));
Expand All @@ -248,7 +247,6 @@
return hashHex;
});
}

}

const ExmSymbol = Symbol('exm');
Expand Down
15 changes: 15 additions & 0 deletions crates/smartweave/smartweave.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,21 @@
};
}

get convertString() {
return {
async toSHA256(input) {
const encoder = new TextEncoder();
const data = encoder.encode(input);
const hashBuffer = await globalThis.SmartWeave.arweave.crypto.hash(data);
const hashArray = Array.from(hashBuffer);
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');

return hashHex;

}
}
}

get unsafeClient() {
const txGetData = async (txId, opts) => {
try {
Expand Down
50 changes: 50 additions & 0 deletions docs/opcodes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

## List of the supported and tested 3EM EVM opcodes:

| opcodes | status |
|:---------:|:--------:|
| `CallValue` ||
| `Dup1` ||
| `IsZero` ||
| `Push3` ||
| `JumpI` ||
| `JumpDest` ||
| `Pop` ||
| `Push1` ||
| `MLoad` ||
| `CodeSize` ||
| `Sub` ||
| `CodeCopy` ||
| `Dup2` ||
| `Add` ||
| `MStore` ||
| `Swap1` ||
| `Swap2` ||
| `Jump` ||
| `Push0` ||
| `Dup3` ||
| `Dup5` ||
| `SLt` ||
| `Dup6` ||
| `Eq` ||
| `Swap3` ||
| `Push32` ||
| `Push8` ||
| `Gt` ||
| `SLoad` ||
| `Div` ||
| `And` ||
| `Lt` ||
| `Dup4` ||
| `Dup8` ||
| `Not` ||
| `Mul` ||
| `Shr` ||
| `Or` ||
| `Dup7` ||
| `SStore` ||
| `Caller` ||
| `Shl` ||
| `Push20` ||
| `Revert` ||
| `Mload` ||
45 changes: 41 additions & 4 deletions js/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ mod tests {

let contract_result = contract.state;
let str_state = contract_result.to_string();
//println!("STATUS::::: {:#?}", str_state.contains("wearemintingyes"));
assert!(str_state.contains("wearemintingyes"));

}
Expand Down Expand Up @@ -753,16 +754,52 @@ mod tests {
};

let contract = simulate_contract(execution_context).await.unwrap();
//println!("{}", contract.exm_context);
//println!("{}", contract.result);
assert_eq!(contract.result, "[\"Yangtze\",\"Amazon\"]");
}


#[tokio::test]
pub async fn simulate_sha256() {
let contract_source_bytes =
include_bytes!("../../../testdata/contracts/sha256.js");
let contract_source_vec = contract_source_bytes.to_vec();
let execution_context: SimulateExecutionContext =
SimulateExecutionContext {
contract_id: String::new(),
interactions: vec![SimulateInput {
id: String::from("abcd"),
owner: String::from("210392sdaspd-asdm-asd_sa0d1293-lc"),
quantity: String::from("12301"),
reward: String::from("12931293"),
target: None,
tags: vec![],
block: None,
input: serde_json::json!({
"act": "1",
})
.to_string(),
}],
contract_init_state: Some(r#"{}"#.into()),
maybe_config: None,
maybe_cache: Some(false),
maybe_bundled_contract: None,
maybe_settings: None,
maybe_exm_context: Some(r#"{"requests": {}, "kv": {}, "initiated":[]}"#.into()),
maybe_contract_source: Some(ContractSource {
contract_src: contract_source_vec.into(),
contract_type: SimulateContractType::JAVASCRIPT,
}),
};

let contract = simulate_contract(execution_context).await.unwrap();
const hello_hash: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
assert_eq!(contract.state[0], hello_hash);
}

#[tokio::test]
pub async fn simulate_kv_get() {
let contract_source_bytes =
include_bytes!("../../../testdata/contracts/getKv.js");

let contract_source_vec = contract_source_bytes.to_vec();
let execution_context: SimulateExecutionContext =
SimulateExecutionContext {
Expand Down
8 changes: 8 additions & 0 deletions testdata/contracts/sha256.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export async function handle(state, action) {
try {
const someSha = await SmartWeave.convertString.toSHA256("hello");
return { state: [someSha] };
} catch(e) {
return { state: e.toString() }
}
}

0 comments on commit 3c0ffd8

Please sign in to comment.