Skip to content

Commit

Permalink
remove account_index from the api
Browse files Browse the repository at this point in the history
  • Loading branch information
willemolding committed Sep 30, 2024
1 parent 2152bd9 commit 88e65e4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 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
37 changes: 24 additions & 13 deletions src/bindgen/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
use std::num::NonZeroU32;
use std::ops::Deref;

use serde::{Deserialize, Serialize};
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 +93,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 +179,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>;
36 changes: 16 additions & 20 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use zcash_client_backend::data_api::wallet::{
};
use zcash_client_backend::data_api::{scanning::ScanRange, WalletCommitmentTrees};
use zcash_client_backend::data_api::{
AccountBirthday, AccountPurpose, InputSource, NullifierQuery, WalletRead, WalletSummary,
WalletWrite,
Account, AccountBirthday, AccountPurpose, InputSource, NullifierQuery, WalletRead,
WalletSummary, WalletWrite,
};
use zcash_client_backend::fees::zip317::SingleOutputChangeStrategy;
use zcash_client_backend::proposal::Proposal;
Expand Down Expand Up @@ -143,17 +143,17 @@ where
///
/// # Arguments
/// seed_phrase - mnemonic phrase to initialise the wallet
/// account_index - The HD derivation index to use. Can be any integer
/// account_id - 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<AccountId, Error> {
// decode the mnemonic and derive the first account
let usk = usk_from_seed_str(seed_phrase, account_index, &self.network)?;
let usk = usk_from_seed_str(seed_phrase, account_hd_index, &self.network)?;
let ufvk = usk.to_unified_full_viewing_key();

tracing::info!("Key successfully decoded. Importing into wallet");
Expand All @@ -166,7 +166,7 @@ where
&self,
ufvk: &UnifiedFullViewingKey,
birthday_height: Option<u32>,
) -> Result<String, Error> {
) -> Result<AccountId, Error> {
self.import_account_ufvk(ufvk, birthday_height, AccountPurpose::ViewOnly)
.await
}
Expand All @@ -177,7 +177,7 @@ where
ufvk: &UnifiedFullViewingKey,
birthday_height: Option<u32>,
purpose: AccountPurpose,
) -> Result<String, Error> {
) -> Result<AccountId, Error> {
tracing::info!("Importing account with Ufvk: {:?}", ufvk);
let mut client = self.client.clone();
let birthday = match birthday_height {
Expand Down Expand Up @@ -205,13 +205,12 @@ where
AccountBirthday::from_treestate(treestate, None).map_err(|_| Error::BirthdayError)?
};

let _account = self
Ok(self
.db
.write()
.await
.import_account_ufvk(ufvk, &birthday, purpose)?;

Ok("0".to_string())
.import_account_ufvk(ufvk, &birthday, purpose)?
.id())
}

pub async fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {
Expand Down Expand Up @@ -384,12 +383,10 @@ where
///
async fn propose_transfer(
&self,
account_index: usize,
account_id: AccountId,
to_address: ZcashAddress,
value: u64,
) -> Result<Proposal<FeeRule, NoteRef>, Error> {
let account_id = self.db.read().await.get_account_ids()?[account_index];

let input_selector = GreedyInputSelector::new(
SingleOutputChangeStrategy::new(FeeRule::standard(), None, ShieldedProtocol::Orchard),
Default::default(),
Expand Down Expand Up @@ -467,14 +464,14 @@ where
pub async fn transfer(
&self,
seed_phrase: &str,
from_account_index: usize,
from_account_id: AccountId,
to_address: ZcashAddress,
value: u64,
) -> Result<(), Error> {
let mut client = self.client.clone();
let usk = usk_from_seed_str(seed_phrase, 0, &self.network)?;
let proposal = self
.propose_transfer(from_account_index, to_address, value)
.propose_transfer(from_account_id, to_address, value)
.await?;
// TODO: Add callback for approving the transaction here
let txids = self.create_proposed_transactions(proposal, &usk).await?;
Expand Down Expand Up @@ -512,7 +509,7 @@ where

fn usk_from_seed_str(
seed: &str,
account_index: u32,
account_id: u32,
network: &consensus::Network,
) -> Result<UnifiedSpendingKey, Error> {
let mnemonic = <Mnemonic<English>>::from_phrase(seed).map_err(|_| Error::InvalidSeedPhrase)?;
Expand All @@ -522,7 +519,6 @@ fn usk_from_seed_str(
seed.zeroize();
SecretVec::new(secret)
};
let usk =
UnifiedSpendingKey::from_seed(network, seed.expose_secret(), account_index.try_into()?)?;
let usk = UnifiedSpendingKey::from_seed(network, seed.expose_secret(), account_id.try_into()?)?;
Ok(usk)
}

0 comments on commit 88e65e4

Please sign in to comment.