Skip to content

Commit

Permalink
Update workflow-rs to 0.10.3 and fix JS data encryption bindings (#396)
Browse files Browse the repository at this point in the history
* Update workflow-rs to 0.10.3

* add encryption example

* fix decrypt_xchacha20poly1305 JS bindings

---------

Co-authored-by: Michael Sutton <mikisiton2@gmail.com>
  • Loading branch information
aspect and michaelsutton authored Jan 17, 2024
1 parent b88b9ce commit cb2a072
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
80 changes: 40 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,16 @@ chrono = "0.4.31"
workflow-perf-monitor = { version = "0.0.2" }

# workflow dependencies
workflow-d3 = { version = "0.10.2" }
workflow-nw = { version = "0.10.2" }
workflow-log = { version = "0.10.2" }
workflow-core = { version = "0.10.2" }
workflow-wasm = { version = "0.10.2" }
workflow-dom = { version = "0.10.2" }
workflow-rpc = { version = "0.10.2" }
workflow-node = { version = "0.10.2" }
workflow-store = { version = "0.10.2" }
workflow-terminal = { version = "0.10.2" }
workflow-d3 = { version = "0.10.3" }
workflow-nw = { version = "0.10.3" }
workflow-log = { version = "0.10.3" }
workflow-core = { version = "0.10.3" }
workflow-wasm = { version = "0.10.3" }
workflow-dom = { version = "0.10.3" }
workflow-rpc = { version = "0.10.3" }
workflow-node = { version = "0.10.3" }
workflow-store = { version = "0.10.3" }
workflow-terminal = { version = "0.10.3" }
nw-sys = "0.1.6"

# if below is enabled, this means that there is an ongoing work
Expand Down
6 changes: 3 additions & 3 deletions wallet/core/src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ pub fn encrypt_xchacha20poly1305(data: &[u8], secret: &Secret) -> Result<Vec<u8>
#[wasm_bindgen(js_name = "decryptXChaCha20Poly1305")]
pub fn js_decrypt_xchacha20poly1305(text: String, password: String) -> Result<String> {
let secret = sha256_hash(password.as_bytes());
let encrypted = decrypt_xchacha20poly1305(text.as_bytes(), &secret)?;
let decoded = general_purpose::STANDARD.decode(encrypted)?;
Ok(String::from_utf8(decoded)?)
let bytes = general_purpose::STANDARD.decode(text)?;
let encrypted = decrypt_xchacha20poly1305(bytes.as_ref(), &secret)?;
Ok(String::from_utf8(encrypted.as_ref().to_vec())?)
}

pub fn decrypt_xchacha20poly1305(data: &[u8], secret: &Secret) -> Result<Secret> {
Expand Down
2 changes: 2 additions & 0 deletions wallet/core/src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ impl From<Vec<u8>> for Secret {
Secret(vec)
}
}

impl From<&[u8]> for Secret {
fn from(slice: &[u8]) -> Self {
Secret(slice.to_vec())
}
}

impl From<&str> for Secret {
fn from(s: &str) -> Self {
Secret(s.trim().as_bytes().to_vec())
Expand Down
12 changes: 12 additions & 0 deletions wasm/nodejs/encryption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const kaspa = require('./kaspa/kaspa_wasm');

kaspa.initConsolePanicHook();

(async () => {

let encrypted = kaspa.encryptXChaCha20Poly1305("my message", "my_password");
console.log("encrypted:", encrypted);
let decrypted = kaspa.decryptXChaCha20Poly1305(encrypted, "my_password");
console.log("decrypted:", decrypted);

})();

0 comments on commit cb2a072

Please sign in to comment.