From a7b05018969015ee23d93d2d41ee25cf6068c82d Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 10 Jan 2023 17:54:48 +0800 Subject: [PATCH] update sth --- .devcontainer/setup.sh | 3 +-- Cargo.toml | 2 +- rustwallet.h | 6 ++++++ scripts/install_targets.sh | 1 + src/lib.rs | 40 +++++++++++++++++++++++++++++++++++++- 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/.devcontainer/setup.sh b/.devcontainer/setup.sh index b9b4626..40eac45 100644 --- a/.devcontainer/setup.sh +++ b/.devcontainer/setup.sh @@ -23,11 +23,10 @@ cargo install cargo-expand cargo install cargo-edit cargo install cargo-ndk cargo install cargo-lipo +cargo install wasm-pack rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android x86_64-linux-android rustup target add aarch64-apple-ios x86_64-apple-ios -curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh - ## setup and install oh-my-zsh sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" diff --git a/Cargo.toml b/Cargo.toml index 91d9798..e2ea61f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,6 @@ shamir_secret_sharing = "0.1.1" tiny-keccak = "1.5" primitive-types = "0.12.1" getrandom = { version = "0.2.7", features = ["js"]} -[target.'cfg(target_arch = "wasm32")'.dependencies] +#[target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen = "0.2.83" diff --git a/rustwallet.h b/rustwallet.h index c518ea9..dfd9460 100644 --- a/rustwallet.h +++ b/rustwallet.h @@ -10,6 +10,12 @@ typedef struct CWallet { char *backup_key; } CWallet; +extern void log(const str *s); + +extern void log_u32(uint32_t a); + +extern void log_many(const str *a, const str *b); + struct CWallet new_wallet(const char *msg); struct CWallet reset_wallet(const char *msg_key, diff --git a/scripts/install_targets.sh b/scripts/install_targets.sh index 0d6b120..95ec5ca 100755 --- a/scripts/install_targets.sh +++ b/scripts/install_targets.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash +cargo install --force cbindgen # install cargo-ndk cargo install cargo-ndk # install cargo-lipo diff --git a/src/lib.rs b/src/lib.rs index a4e9251..86e2acd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -84,6 +84,30 @@ pub struct CWallet { pub backup_key: *mut c_char, } +#[wasm_bindgen] +extern "C" { + // Use `js_namespace` here to bind `console.log(..)` instead of just + // `log(..)` + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); + + // The `console.log` is quite polymorphic, so we can bind it with multiple + // signatures. Note that we need to use `js_name` to ensure we always call + // `log` in JS. + #[wasm_bindgen(js_namespace = console, js_name = log)] + fn log_u32(a: u32); + + // Multiple arguments too! + #[wasm_bindgen(js_namespace = console, js_name = log)] + fn log_many(a: &str, b: &str); +} + +macro_rules! console_log { + // Note that this is using the `log` function imported above during + // `bare_bones` + ($($t:tt)*) => (log(&format_args!($($t)*).to_string())) +} + #[no_mangle] pub unsafe extern "C" fn new_wallet(msg: *const c_char) -> CWallet { let str = cchar_to_str!(msg); @@ -113,7 +137,6 @@ pub unsafe extern "C" fn free_cwallet(cw: CWallet) { } #[no_mangle] -#[wasm_bindgen] pub unsafe extern "C" fn get_address( msg_key: *const c_char, master_key: *const c_char, @@ -127,6 +150,21 @@ pub unsafe extern "C" fn get_address( c_address.into_raw() } +#[wasm_bindgen] +pub fn wget_address(msg_key: String, master_key: String, second_key: Option, backup_key: Option) -> String { + console_log!("wget_address: {}, {}!", msg_key, master_key); + let rwallet = Wallet{ + msg_key, + master_key, + second_key, + backup_key + }; + console_log!("wallet: {:?}", rwallet); + let address = rwallet.get_address(); + let address_str = format!("{:?}", address); + address_str +} + #[no_mangle] pub unsafe extern "C" fn generate_sec_key( msg_key: *const c_char,