Skip to content

Commit

Permalink
Merge branch 'main' into ec2/naive-idx-db
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding authored Aug 13, 2024
2 parents a846a1a + 0b95948 commit 1a600a9
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/rust-checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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

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
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 @@ -30,6 +30,8 @@ console_error_panic_hook = { version = "0.1.7", optional = true }
wasm-bindgen-futures = "0.4.42"
indexed_db_futures = "0.5.0"
web-sys = { version = "0.3.69", features = ["console"] }
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 @@ -19,6 +19,8 @@ pub enum Error {
message: String,
code: u16,
},
#[error("Address generation error")]
AddressGenerationError(#[from] zcash_keys::keys::AddressGenerationError),
}

impl From<Error> for JsValue {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion 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 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 1a600a9

Please sign in to comment.