From 89bf5410647922f729b095560c87a24baa1fa880 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 12:05:36 -0400 Subject: [PATCH 1/9] use newer ubuntu runner to get compatible llvm --- .github/workflows/rust-checks.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/rust-checks.yml diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml new file mode 100644 index 0000000..58b07c5 --- /dev/null +++ b/.github/workflows/rust-checks.yml @@ -0,0 +1,23 @@ +name: Rust-Checks + +on: + pull_request: + push: + branches: main + +jobs: + fmt: + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install Rust toolchain + uses: ./.github/actions/install-rust-toolchain + with: + components: rustfmt + + - uses: actions-rs/cargo@v1 + with: + command: fmt + args: --all -- --check From dee4ec11dfd52f6b36aeb8d2234809afe12a32d0 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 12:08:18 -0400 Subject: [PATCH 2/9] fmt to make runner pass --- src/store/injected_store.rs | 2 +- src/store/memory_store.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/store/injected_store.rs b/src/store/injected_store.rs index fdfb642..3e0d469 100644 --- a/src/store/injected_store.rs +++ b/src/store/injected_store.rs @@ -1,8 +1,8 @@ // Copyright 2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -use crate::store::WalletStore; use crate::error::Error; +use crate::store::WalletStore; use wasm_bindgen::prelude::*; #[wasm_bindgen(typescript_custom_section)] diff --git a/src/store/memory_store.rs b/src/store/memory_store.rs index 42ce153..89c87b5 100644 --- a/src/store/memory_store.rs +++ b/src/store/memory_store.rs @@ -1,8 +1,8 @@ // Copyright 2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT -use crate::store::WalletStore; use crate::error::Error; +use crate::store::WalletStore; /// A simple in-memory store for wallet data. Useful for testing pub struct MemoryStore { From 65b6e745c5e3168db124bbde74baa5faf4bdf768 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 12:12:06 -0400 Subject: [PATCH 3/9] add clippy job --- .github/workflows/rust-checks.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/rust-checks.yml b/.github/workflows/rust-checks.yml index 58b07c5..1b5fb2d 100644 --- a/.github/workflows/rust-checks.yml +++ b/.github/workflows/rust-checks.yml @@ -21,3 +21,19 @@ jobs: with: command: fmt args: --all -- --check + + clippy: + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install Rust toolchain + uses: ./.github/actions/install-rust-toolchain + with: + components: clippy + + - uses: actions-rs/cargo@v1 + with: + command: clippy + args: --all --all-targets -- -D warnings From 340bb2f6ae45d20573984d9461ecbc76c77e1951 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 12:20:21 -0400 Subject: [PATCH 4/9] allow async fn in trait --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 70b4139..76ea0d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0, MIT //! This is the top level documentation! +#![allow(async_fn_in_trait)] pub mod account; pub mod error; From 0294577ab3028e7ceb4073b42dcefbd1aece9c1b Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 12:28:18 -0400 Subject: [PATCH 5/9] fix clippy for test --- tests/web_accounts.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/web_accounts.rs b/tests/web_accounts.rs index 5f21e54..ef854ea 100644 --- a/tests/web_accounts.rs +++ b/tests/web_accounts.rs @@ -6,5 +6,5 @@ use webz_core::account::Account; #[wasm_bindgen_test] fn test_account_from_seed() { let seed = [0; 32]; - let a = Account::from_seed(&seed, 0).unwrap(); + let _a = Account::from_seed(&seed, 0).unwrap(); } From 12c65e9ee9b3e4877166772705e87285c306846f Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 13:39:15 -0400 Subject: [PATCH 6/9] add methods to derive string encoded addresses for an account --- src/account.rs | 37 +++++++++++++++++++++++++++++++++---- src/error.rs | 2 ++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/account.rs b/src/account.rs index d540281..b9298dd 100644 --- a/src/account.rs +++ b/src/account.rs @@ -2,9 +2,9 @@ // SPDX-License-Identifier: Apache-2.0, MIT use wasm_bindgen::prelude::*; -use zcash_keys::keys::{Era, UnifiedSpendingKey}; +use zcash_keys::keys::{Era, UnifiedAddressRequest, UnifiedSpendingKey}; use zcash_primitives::consensus::MAIN_NETWORK; -use zcash_primitives::zip32::AccountId; +use zcash_primitives::zip32::{AccountId, DiversifierIndex}; use crate::error::Error; @@ -12,7 +12,8 @@ pub type AccountIndex = u32; #[wasm_bindgen] pub struct Account { - usk: UnifiedSpendingKey, + #[wasm_bindgen(skip)] + pub usk: UnifiedSpendingKey, } #[wasm_bindgen] @@ -42,4 +43,32 @@ impl Account { usk: UnifiedSpendingKey::from_bytes(Era::Orchard, encoded).unwrap(), }) } -} + + #[wasm_bindgen] + /// Return the string encoded address for this account. This returns a unified address with all address subtypes (orchard, sapling, p2pkh) + /// The diversifier index can be used to derive different valid addresses for the same account + pub fn unified_address(&self, diversifier_index: u64) -> Result { + Ok(self + .usk + .to_unified_full_viewing_key() + .address( + DiversifierIndex::from(diversifier_index), + UnifiedAddressRequest::all().unwrap(), + )? + .encode(&MAIN_NETWORK)) + } + + #[wasm_bindgen] + /// Return the string encoded address for this accounts transparent address + /// The diversifier index can be used to derive different valid addresses for the same account + pub fn transparent_address(&self, diversifier_index: u64) -> Result { + Ok(self + .usk + .to_unified_full_viewing_key() + .address( + DiversifierIndex::from(diversifier_index), + UnifiedAddressRequest::new(false, false, true).unwrap(), + )? + .encode(&MAIN_NETWORK)) + } + } diff --git a/src/error.rs b/src/error.rs index 9853fe3..34d9be3 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,6 +13,8 @@ pub enum Error { // DecodingError(#[from] zcash_keys::keys::DecodingError), #[error("Javascript error")] JsError(JsValue), + #[error("Address generation error")] + AddressGenerationError(#[from] zcash_keys::keys::AddressGenerationError), } impl From for JsValue { From 6d745cce31eecd12170af5d626fd1872275e516a Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 14:25:00 -0400 Subject: [PATCH 7/9] add unified and transparent encoding for account addresses --- Cargo.lock | 2 ++ Cargo.toml | 2 ++ src/account.rs | 25 ++++++++++++++----------- tests/web_accounts.rs | 14 ++++++++++++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bd2aee7..48529f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1411,6 +1411,8 @@ dependencies = [ "console_error_panic_hook", "getrandom", "js-sys", + "ripemd", + "sha2", "thiserror", "tonic-build", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 355efb6..0c3d0e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,8 @@ getrandom = { version = "0.2", features = ["js"] } thiserror = "1.0.63" console_error_panic_hook = { version = "0.1.7", optional = true } wasm-bindgen-futures = "0.4.42" +sha2 = "0.10" +ripemd = "0.1" [dev-dependencies] wasm-bindgen-test = "0.3.42" diff --git a/src/account.rs b/src/account.rs index b9298dd..d2a490c 100644 --- a/src/account.rs +++ b/src/account.rs @@ -2,9 +2,14 @@ // SPDX-License-Identifier: Apache-2.0, MIT use wasm_bindgen::prelude::*; +use zcash_keys::encoding::AddressCodec; use zcash_keys::keys::{Era, UnifiedAddressRequest, UnifiedSpendingKey}; use zcash_primitives::consensus::MAIN_NETWORK; +use zcash_primitives::legacy::keys::pubkey_to_address; +use zcash_primitives::legacy::TransparentAddress; use zcash_primitives::zip32::{AccountId, DiversifierIndex}; +use sha2::{Digest, Sha256}; + use crate::error::Error; @@ -46,7 +51,7 @@ impl Account { #[wasm_bindgen] /// Return the string encoded address for this account. This returns a unified address with all address subtypes (orchard, sapling, p2pkh) - /// The diversifier index can be used to derive different valid addresses for the same account + /// The diversifier index can be used to derive different valid addresses for the same account. Diversifier index must be > 0 pub fn unified_address(&self, diversifier_index: u64) -> Result { Ok(self .usk @@ -60,15 +65,13 @@ impl Account { #[wasm_bindgen] /// Return the string encoded address for this accounts transparent address - /// The diversifier index can be used to derive different valid addresses for the same account - pub fn transparent_address(&self, diversifier_index: u64) -> Result { - Ok(self - .usk - .to_unified_full_viewing_key() - .address( - DiversifierIndex::from(diversifier_index), - UnifiedAddressRequest::new(false, false, true).unwrap(), - )? - .encode(&MAIN_NETWORK)) + /// Should this also support a diversifier? + pub fn transparent_address(&self) -> Result { + let pubkey = self.usk.transparent().to_account_pubkey(); + let t_address = TransparentAddress::PublicKeyHash( + *ripemd::Ripemd160::digest(Sha256::digest(pubkey.serialize())).as_ref(), + ); + Ok(t_address.encode(&MAIN_NETWORK)) } } + diff --git a/tests/web_accounts.rs b/tests/web_accounts.rs index ef854ea..2ab462e 100644 --- a/tests/web_accounts.rs +++ b/tests/web_accounts.rs @@ -4,7 +4,17 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use webz_core::account::Account; #[wasm_bindgen_test] -fn test_account_from_seed() { +fn test_unified_address_encoding() { let seed = [0; 32]; - let _a = Account::from_seed(&seed, 0).unwrap(); + let a = Account::from_seed(&seed, 0).unwrap(); + let address = a.unified_address(1).unwrap(); + assert_eq!(address.len(), 213); +} + +#[wasm_bindgen_test] +fn test_transparent_address_encoding() { + let seed = [0; 32]; + let a = Account::from_seed(&seed, 0).unwrap(); + let address = a.transparent_address().unwrap(); + assert_eq!(address.len(), 35); } From 497c61820351ddc3fc70ea7d954cacf2ed173b71 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 14:25:12 -0400 Subject: [PATCH 8/9] fmt --- src/account.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/account.rs b/src/account.rs index d2a490c..11e8480 100644 --- a/src/account.rs +++ b/src/account.rs @@ -1,6 +1,7 @@ // Copyright 2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +use sha2::{Digest, Sha256}; use wasm_bindgen::prelude::*; use zcash_keys::encoding::AddressCodec; use zcash_keys::keys::{Era, UnifiedAddressRequest, UnifiedSpendingKey}; @@ -8,8 +9,6 @@ use zcash_primitives::consensus::MAIN_NETWORK; use zcash_primitives::legacy::keys::pubkey_to_address; use zcash_primitives::legacy::TransparentAddress; use zcash_primitives::zip32::{AccountId, DiversifierIndex}; -use sha2::{Digest, Sha256}; - use crate::error::Error; @@ -73,5 +72,4 @@ impl Account { ); Ok(t_address.encode(&MAIN_NETWORK)) } - } - +} From 69ba1029c3e35f8413b0a707f71614457a143e13 Mon Sep 17 00:00:00 2001 From: Willem Olding Date: Mon, 12 Aug 2024 14:32:28 -0400 Subject: [PATCH 9/9] drop unused import --- src/account.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/account.rs b/src/account.rs index 11e8480..c3b6e0d 100644 --- a/src/account.rs +++ b/src/account.rs @@ -6,7 +6,6 @@ use wasm_bindgen::prelude::*; use zcash_keys::encoding::AddressCodec; use zcash_keys::keys::{Era, UnifiedAddressRequest, UnifiedSpendingKey}; use zcash_primitives::consensus::MAIN_NETWORK; -use zcash_primitives::legacy::keys::pubkey_to_address; use zcash_primitives::legacy::TransparentAddress; use zcash_primitives::zip32::{AccountId, DiversifierIndex};