Skip to content

Commit

Permalink
Add zcashlc_is_seed_relevant_to_wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed Mar 15, 2024
1 parent e742f5b commit 70cce12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `zcashlc_free_accounts`
- `FfiAccounts`
- `FfiAccount`
- `zcashlc_is_seed_relevant_to_wallet`

### Changed
- `zcashlc_scan_blocks` now takes a `TreeState` protobuf object that provides
Expand Down
44 changes: 44 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use zcash_client_backend::{
};
use zcash_client_sqlite::{
chain::{init::init_blockmeta_db, BlockMeta},
error::SqliteClientError,
wallet::init::{init_wallet_db, WalletMigrationError},
AccountId, FsBlockDb, WalletDb,
};
Expand Down Expand Up @@ -518,6 +519,49 @@ pub unsafe extern "C" fn zcashlc_create_account(
unwrap_exc_or_null(res)
}

/// Checks whether the given seed is relevant to any of the accounts in the wallet.
///
/// Returns:
/// - `1` for `Ok(true)`.
/// - `0` for `Ok(false)`.
/// - `-1` for `Err(_)`.
#[no_mangle]
pub unsafe extern "C" fn zcashlc_is_seed_relevant_to_wallet(
db_data: *const u8,
db_data_len: usize,
seed: *const u8,
seed_len: usize,
network_id: u32,
) -> i8 {
let res = catch_panic(|| {
let network = parse_network(network_id)?;
let db_data = unsafe { wallet_db(db_data, db_data_len, network)? };
let seed = Secret::new((unsafe { slice::from_raw_parts(seed, seed_len) }).to_vec());

for account_id in db_data.get_account_ids()? {
match db_data.validate_seed(account_id, &seed) {
// The seed is relevant to this account. No need to check any others.
Ok(true) => return Ok(1),
// We know by dead reckoning that this account ID exists in the wallet, so
// this must be the other `false` state, that the account is derived from
// a different seed.
Ok(false) => (),
// The account is imported. The seed _might_ be relevant, but the only way
// we could determine that is by brute-forcing the ZIP 32 account index
// space, which we're not going to do. Assume the seed is not relevant,
// which technically violates the intent of this method.
Err(SqliteClientError::UnknownZip32Derivation) => (),
// Other error cases are assumed to be actual errors, not distinguishers.
Err(e) => return Err(e.into()),
}
}

// The seed was not relevant to any of the accounts in the wallet.
Ok(0)
});
unwrap_exc_or(res, -1)
}

/// A struct that contains an account identifier along with a pointer to the string encoding
/// of an associated key.
///
Expand Down

0 comments on commit 70cce12

Please sign in to comment.