Skip to content

Commit

Permalink
expose sha256 for testing (#43)
Browse files Browse the repository at this point in the history
* expose sha256 for testing

* Expose sha256 for testing

* Remove unused code

* Add common test functions

* Use npm assert

* move test utils to TS

* Remove test code

* Remove test code

* Remove test code
  • Loading branch information
aqk authored Nov 8, 2024
1 parent 0f7752c commit 37175e8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions wasm/src/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,3 +663,9 @@ pub fn chia_identity(seed: &str) -> Result<JsValue, JsValue> {
let js_identity: JsChiaIdentity = identity.into();
serde_wasm_bindgen::to_value(&js_identity).into_js()
}

#[wasm_bindgen]
pub fn sha256bytes(bytes_str: &str) -> Result<JsValue, JsValue> {
let hashed = Sha256Input::Bytes(bytes_str.as_bytes()).hash();
serde_wasm_bindgen::to_value(&hashed).into_js()
}
6 changes: 6 additions & 0 deletions wasm/tests/src/lib/tests/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export function to_hex_string(byteArray: Array<number>) {
return Array.from(byteArray, function(byte: number) {
return ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join('')
}
14 changes: 14 additions & 0 deletions wasm/tests/src/lib/tests/sha256bytes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { init, sha256bytes } from '../../../../pkg/chia_gaming_wasm.js';
import { to_hex_string } from './common';
import * as assert from 'assert';

let utf8Encode = new TextEncoder();
let b = utf8Encode.encode("abc");

it('hashes', async () => {
init();
let msg = 'hello.there.my.dear.friend';
let hash = sha256bytes(msg);
console.log(msg, hash);
assert.equal( to_hex_string(hash), "5272821c151fdd49f19cc58cf8833da5781c7478a36d500e8dc2364be39f8216" );
});

0 comments on commit 37175e8

Please sign in to comment.