Skip to content

Commit

Permalink
Merge pull request #2 from ChainSafe/willem/account-js-interface
Browse files Browse the repository at this point in the history
Add formatted address generation for accounts
  • Loading branch information
willemolding authored Aug 13, 2024
2 parents a37765e + 69ba102 commit 0b95948
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 32 additions & 3 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// Copyright 2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use sha2::{Digest, Sha256};
use wasm_bindgen::prelude::*;
use zcash_keys::keys::{Era, UnifiedSpendingKey};
use zcash_keys::encoding::AddressCodec;
use zcash_keys::keys::{Era, UnifiedAddressRequest, UnifiedSpendingKey};
use zcash_primitives::consensus::MAIN_NETWORK;
use zcash_primitives::zip32::AccountId;
use zcash_primitives::legacy::TransparentAddress;
use zcash_primitives::zip32::{AccountId, DiversifierIndex};

use crate::error::Error;

pub type AccountIndex = u32;

#[wasm_bindgen]
pub struct Account {
usk: UnifiedSpendingKey,
#[wasm_bindgen(skip)]
pub usk: UnifiedSpendingKey,
}

#[wasm_bindgen]
Expand Down Expand Up @@ -42,4 +46,29 @@ 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. Diversifier index must be > 0
pub fn unified_address(&self, diversifier_index: u64) -> Result<String, Error> {
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
/// Should this also support a diversifier?
pub fn transparent_address(&self) -> Result<String, Error> {
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))
}
}
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Error> for JsValue {
Expand Down
14 changes: 12 additions & 2 deletions tests/web_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 0b95948

Please sign in to comment.