Skip to content

Commit

Permalink
Merge pull request #30 from ChainSafe/willem/remove-account-id
Browse files Browse the repository at this point in the history
Remove account_index from API and use AccountId instead
  • Loading branch information
willemolding authored Sep 30, 2024
2 parents 2152bd9 + a6ef53d commit 62dc517
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 65 deletions.
24 changes: 12 additions & 12 deletions Cargo.lock

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

14 changes: 7 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ tokio_with_wasm = { version = "0.7.1", features = ["rt", "rt-multi-thread", "syn

## Zcash dependencies

zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", features = ["transparent-inputs", "orchard", "sapling", "unstable"] }
zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen", "orchard"] }
zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", features = ["orchard"] }
zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4" }
zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4" }
zcash_proofs = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["bundled-prover"] }
zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a", features = ["transparent-inputs", "orchard", "sapling", "unstable"] }
zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen", "orchard"] }
zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a", features = ["orchard"] }
zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a" }
zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a" }
zcash_proofs = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a", default-features = false, features = ["bundled-prover"] }

## gRPC Web dependencies
prost = { version = "0.12", default-features = false }
Expand All @@ -77,7 +77,7 @@ tonic = { version = "0.12", default-features = false, features = [

# Used in Native tests
tokio = { version = "1.0" }
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["unstable", "orchard"], optional = true }
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "d1fa3e846c5e61de3f1df23dd9f4d5416915631a", default-features = false, features = ["unstable", "orchard"], optional = true }

getrandom = { version = "0.2", features = ["js"] }
thiserror = "1.0.63"
Expand Down
9 changes: 5 additions & 4 deletions packages/demo-wallet/src/App/Actions.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import initWasm, { initThreadPool, WebWallet } from "@webzjs/webz-core";

import { State, Action } from "./App";
import { MAINNET_LIGHTWALLETD_PROXY } from "./constants";
import { MAINNET_LIGHTWALLETD_PROXY } from "./Constants";

export async function init(dispatch: React.Dispatch<Action>) {
await initWasm();
Expand All @@ -13,8 +13,8 @@ export async function init(dispatch: React.Dispatch<Action>) {
}

export async function addNewAccount(state: State, dispatch: React.Dispatch<Action>, seedPhrase: string, birthdayHeight: number) {
await state.webWallet?.create_account(seedPhrase, 0, birthdayHeight);
dispatch({ type: "append-account-seed", payload: seedPhrase });
let account_id = await state.webWallet?.create_account(seedPhrase, 0, birthdayHeight) || 0;
dispatch({ type: "add-account-seed", payload: [account_id, seedPhrase] });
await syncStateWithWallet(state, dispatch);
}

Expand Down Expand Up @@ -60,6 +60,7 @@ export async function triggerTransfer(
throw new Error("No active account");
}

await state.webWallet?.transfer(state.accountSeeds[state.activeAccount], state.activeAccount, toAddress, amount);
let activeAccountSeedPhrase = state.accountSeeds.get(state.activeAccount) || "";
await state.webWallet?.transfer(activeAccountSeedPhrase, state.activeAccount, toAddress, amount);
await syncStateWithWallet(state, dispatch);
}
10 changes: 5 additions & 5 deletions packages/demo-wallet/src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ export type State = {
activeAccount?: number;
summary?: WalletSummary;
chainHeight?: bigint;
accountSeeds: string[];
accountSeeds: Map<number, string>;
};

const initialState: State = {
activeAccount: undefined,
summary: undefined,
chainHeight: undefined,
accountSeeds: [],
accountSeeds: new Map<number, string>(),
};

export type Action =
| { type: "set-active-account"; payload: number }
| { type: "append-account-seed"; payload: string }
| { type: "add-account-seed"; payload: [number, string] }
| { type: "set-web-wallet"; payload: WebWallet }
| { type: "set-summary"; payload: WalletSummary }
| { type: "set-chain-height"; payload: bigint };
Expand All @@ -45,8 +45,8 @@ const reducer = (state: State, action: Action): State => {
case "set-active-account": {
return { ...state, activeAccount: action.payload };
}
case "append-account-seed": {
return { ...state, accountSeeds: [...state.accountSeeds, action.payload] };
case "add-account-seed": {
return { ...state, accountSeeds: state.accountSeeds.set(action.payload[0], action.payload[1]) };
}
case "set-web-wallet": {
return { ...state, webWallet: action.payload };
Expand Down
36 changes: 23 additions & 13 deletions src/bindgen/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ use wasm_bindgen::prelude::*;
use tonic_web_wasm_client::Client;

use crate::error::Error;
use crate::{BlockRange, MemoryWallet, Wallet, PRUNING_DEPTH};
use crate::{BlockRange, Wallet, PRUNING_DEPTH};
use wasm_thread as thread;
use zcash_address::ZcashAddress;
use zcash_client_backend::data_api::WalletRead;
use zcash_client_backend::proto::service::{
compact_tx_streamer_client::CompactTxStreamerClient, ChainSpec,
};
use zcash_client_memory::MemoryWalletDb;
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_primitives::consensus::{self, BlockHeight};

pub type MemoryWallet<T> = Wallet<MemoryWalletDb<consensus::Network>, T>;
pub type AccountId =
<MemoryWalletDb<zcash_primitives::consensus::Network> as WalletRead>::AccountId;

/// # A Zcash wallet
///
/// A wallet is a set of accounts that can be synchronized together with the blockchain.
Expand Down Expand Up @@ -87,30 +92,30 @@ impl WebWallet {
///
/// # Arguments
/// seed_phrase - mnemonic phrase to initialise the wallet
/// account_index - The HD derivation index to use. Can be any integer
/// account_hd_index - The HD derivation index to use. Can be any integer
/// birthday_height - The block height at which the account was created, optionally None and the current height is used
///
pub async fn create_account(
&self,
seed_phrase: &str,
account_index: u32,
account_hd_index: u32,
birthday_height: Option<u32>,
) -> Result<String, Error> {
) -> Result<u32, Error> {
tracing::info!("Create account called");
self.inner
.create_account(seed_phrase, account_index, birthday_height)
.create_account(seed_phrase, account_hd_index, birthday_height)
.await
.map(|id| *id)
}

pub async fn import_ufvk(
&self,
key: &str,
birthday_height: Option<u32>,
) -> Result<String, Error> {
pub async fn import_ufvk(&self, key: &str, birthday_height: Option<u32>) -> Result<u32, Error> {
let ufvk = UnifiedFullViewingKey::decode(&self.inner.network, key)
.map_err(Error::KeyParseError)?;

self.inner.import_ufvk(&ufvk, birthday_height).await
self.inner
.import_ufvk(&ufvk, birthday_height)
.await
.map(|id| *id)
}

pub async fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {
Expand Down Expand Up @@ -173,13 +178,18 @@ impl WebWallet {
pub async fn transfer(
&self,
seed_phrase: &str,
from_account_index: usize,
from_account_id: u32,
to_address: String,
value: u64,
) -> Result<(), Error> {
let to_address = ZcashAddress::try_from_encoded(&to_address)?;
self.inner
.transfer(seed_phrase, from_account_index, to_address, value)
.transfer(
seed_phrase,
AccountId::from(from_account_id),
to_address,
value,
)
.await
}

Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub mod wallet;
pub use wallet::Wallet;

use wasm_bindgen::prelude::*;
use zcash_client_memory::MemoryWalletDb;
use zcash_primitives::consensus;

/// The maximum number of checkpoints to store in each shard-tree
pub const PRUNING_DEPTH: usize = 100;
Expand All @@ -30,5 +28,3 @@ pub fn init_thread_pool(_threads: usize) {}

#[wasm_bindgen]
pub struct BlockRange(pub u32, pub u32);

pub type MemoryWallet<T> = Wallet<MemoryWalletDb<consensus::Network>, T>;
Loading

0 comments on commit 62dc517

Please sign in to comment.