Make AccountId internal representation private.

This commit is contained in:
Kris Nuttycombe 2022-02-10 09:47:42 -07:00
parent 970b6d6750
commit e20de50b84
12 changed files with 157 additions and 121 deletions

View File

@ -92,7 +92,7 @@ impl<N: fmt::Display> fmt::Display for Error<N> {
write!(f, "Invalid chain (upper bound: {}): {:?}", u32::from(*upper_bound), cause)
}
Error::InvalidExtSk(account) => {
write!(f, "Incorrect ExtendedSpendingKey for account {}", account.0)
write!(f, "Incorrect ExtendedSpendingKey for account {}", u32::from(*account))
}
Error::InvalidNewWitnessAnchor(output, txid, last_height, anchor) => write!(
f,

View File

@ -142,7 +142,7 @@ where
/// }
/// };
///
/// let account = AccountId(0);
/// let account = AccountId::from(0);
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, account);
/// let to = extsk.default_address().1.into();
///

View File

@ -107,7 +107,7 @@ impl<P: consensus::Parameters> AddressCodec<P> for TransparentAddress {
/// keys::sapling,
/// };
///
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId::from(0));
/// let encoded = encode_extended_spending_key(HRP_SAPLING_EXTENDED_SPENDING_KEY, &extsk);
/// ```
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
@ -140,7 +140,7 @@ pub fn decode_extended_spending_key(
/// };
/// use zcash_primitives::zip32::ExtendedFullViewingKey;
///
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId::from(0));
/// let extfvk = ExtendedFullViewingKey::from(&extsk);
/// let encoded = encode_extended_full_viewing_key(HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, &extfvk);
/// ```

View File

@ -26,7 +26,7 @@ pub mod sapling {
/// keys::sapling,
/// };
///
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId(0));
/// let extsk = sapling::spending_key(&[0; 32][..], COIN_TYPE, AccountId::from(0));
/// ```
/// [`ExtendedSpendingKey`]: zcash_primitives::zip32::ExtendedSpendingKey
pub fn spending_key(seed: &[u8], coin_type: u32, account: AccountId) -> ExtendedSpendingKey {
@ -39,7 +39,7 @@ pub mod sapling {
&[
ChildIndex::Hardened(32),
ChildIndex::Hardened(coin_type),
ChildIndex::Hardened(account.0),
ChildIndex::Hardened(account.into()),
],
)
}
@ -186,20 +186,21 @@ mod tests {
#[test]
#[should_panic]
fn spending_key_panics_on_short_seed() {
let _ = sapling::spending_key(&[0; 31][..], 0, AccountId(0));
let _ = sapling::spending_key(&[0; 31][..], 0, AccountId::from(0));
}
#[cfg(feature = "transparent-inputs")]
#[test]
fn pk_to_taddr() {
let taddr = legacy::keys::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId(0))
.unwrap()
.to_account_pubkey()
.derive_external_ivk()
.unwrap()
.derive_address(0)
.unwrap()
.encode(&MAIN_NETWORK);
let taddr =
legacy::keys::AccountPrivKey::from_seed(&MAIN_NETWORK, &seed(), AccountId::from(0))
.unwrap()
.to_account_pubkey()
.derive_external_ivk()
.unwrap()
.derive_address(0)
.unwrap()
.encode(&MAIN_NETWORK);
assert_eq!(taddr, "t1PKtYdJJHhc3Pxowmznkg7vdTwnhEsCvR4".to_string());
}
}

View File

@ -229,7 +229,7 @@ pub fn scan_block<P: consensus::Parameters, K: ScanningKey>(
.iter()
.map(|&(account, nf)| CtOption::new(account, nf.ct_eq(&spend_nf)))
.fold(
CtOption::new(AccountId::default(), 0.into()),
CtOption::new(AccountId::from(0), 0.into()),
|first, next| CtOption::conditional_select(&next, &first, first.is_some()),
)
.map(|account| WalletShieldedSpend {
@ -441,7 +441,7 @@ mod tests {
let txs = scan_block(
&Network::TestNetwork,
cb,
&[(&AccountId(0), &extfvk)],
&[(&AccountId::from(0), &extfvk)],
&[],
&mut tree,
&mut [],
@ -455,7 +455,7 @@ mod tests {
assert_eq!(tx.shielded_spends.len(), 0);
assert_eq!(tx.shielded_outputs.len(), 1);
assert_eq!(tx.shielded_outputs[0].index, 0);
assert_eq!(tx.shielded_outputs[0].account, AccountId(0));
assert_eq!(tx.shielded_outputs[0].account, AccountId::from(0));
assert_eq!(tx.shielded_outputs[0].note.value, 5);
// Check that the witness root matches
@ -480,7 +480,7 @@ mod tests {
let txs = scan_block(
&Network::TestNetwork,
cb,
&[(&AccountId(0), &extfvk)],
&[(&AccountId::from(0), &extfvk)],
&[],
&mut tree,
&mut [],
@ -494,7 +494,7 @@ mod tests {
assert_eq!(tx.shielded_spends.len(), 0);
assert_eq!(tx.shielded_outputs.len(), 1);
assert_eq!(tx.shielded_outputs[0].index, 0);
assert_eq!(tx.shielded_outputs[0].account, AccountId(0));
assert_eq!(tx.shielded_outputs[0].account, AccountId::from(0));
assert_eq!(tx.shielded_outputs[0].note.value, 5);
// Check that the witness root matches
@ -506,7 +506,7 @@ mod tests {
let extsk = ExtendedSpendingKey::master(&[]);
let extfvk = ExtendedFullViewingKey::from(&extsk);
let nf = Nullifier([7; 32]);
let account = AccountId(12);
let account = AccountId::from(12);
let cb = fake_compact_block(1u32.into(), nf, extfvk, Amount::from_u64(5).unwrap(), false);
assert_eq!(cb.vtx.len(), 2);

View File

@ -323,7 +323,10 @@ mod tests {
let (extfvk, _taddr) = init_test_accounts_table(&db_data);
// Account balance should be zero
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
// Create fake CompactBlocks sending value to the address
let value = Amount::from_u64(5).unwrap();
@ -346,7 +349,7 @@ mod tests {
// Account balance should reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
@ -355,7 +358,7 @@ mod tests {
// Account balance should be unaltered
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
@ -363,14 +366,14 @@ mod tests {
rewind_to_height(&db_data, sapling_activation_height()).unwrap();
// Account balance should only contain the first received note
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
// Scan the cache again
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
// Account balance should again reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
}
@ -399,7 +402,7 @@ mod tests {
insert_into_cache(&db_cache, &cb1);
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
// We cannot scan a block of height SAPLING_ACTIVATION_HEIGHT + 2 next
let (cb2, _) = fake_compact_block(
@ -429,7 +432,7 @@ mod tests {
insert_into_cache(&db_cache, &cb2);
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::from_u64(150_000).unwrap()
);
}
@ -448,7 +451,10 @@ mod tests {
let (extfvk, _taddr) = init_test_accounts_table(&db_data);
// Account balance should be zero
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
// Create a fake CompactBlock sending value to the address
let value = Amount::from_u64(5).unwrap();
@ -465,7 +471,7 @@ mod tests {
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
// Account balance should reflect the received note
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
// Create a second fake CompactBlock sending more value to the address
let value2 = Amount::from_u64(7).unwrap();
@ -478,7 +484,7 @@ mod tests {
// Account balance should reflect both received notes
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value + value2).unwrap()
);
}
@ -497,7 +503,10 @@ mod tests {
let (extfvk, _taddr) = init_test_accounts_table(&db_data);
// Account balance should be zero
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
// Create a fake CompactBlock sending value to the address
let value = Amount::from_u64(5).unwrap();
@ -514,7 +523,7 @@ mod tests {
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
// Account balance should reflect the received note
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
// Create a second fake CompactBlock spending value from the address
let extsk2 = ExtendedSpendingKey::master(&[0]);
@ -537,7 +546,7 @@ mod tests {
// Account balance should equal the change
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value - value2).unwrap()
);
}

View File

@ -780,7 +780,7 @@ mod tests {
db_data: &WalletDb<Network>,
) -> (ExtendedFullViewingKey, Option<TransparentAddress>) {
let seed = [0u8; 32];
let account = AccountId(0);
let account = AccountId::from(0);
let extsk = sapling::spending_key(&seed, network().coin_type(), account);
let extfvk = ExtendedFullViewingKey::from(&extsk);

View File

@ -146,7 +146,7 @@ impl ShieldedOutput for DecryptedOutput {
///
/// let data_file = NamedTempFile::new().unwrap();
/// let db = WalletDb::for_path(data_file, Network::TestNetwork).unwrap();
/// let addr = get_address(&db, AccountId(0));
/// let addr = get_address(&db, AccountId::from(0));
/// ```
#[deprecated(
note = "This function will be removed in a future release. Use zcash_client_backend::data_api::WalletRead::get_address instead."
@ -158,7 +158,7 @@ pub fn get_address<P: consensus::Parameters>(
let addr: String = wdb.conn.query_row(
"SELECT address FROM accounts
WHERE account = ?",
&[account.0],
&[u32::from(account)],
|row| row.get(0),
)?;
@ -182,7 +182,7 @@ pub fn get_extended_full_viewing_keys<P: consensus::Parameters>(
let rows = stmt_fetch_accounts
.query_map(NO_PARAMS, |row| {
let acct = row.get(0).map(AccountId)?;
let acct: u32 = row.get(0)?;
let extfvk = row.get(1).map(|extfvk: String| {
decode_extended_full_viewing_key(
wdb.params.hrp_sapling_extended_full_viewing_key(),
@ -192,7 +192,7 @@ pub fn get_extended_full_viewing_keys<P: consensus::Parameters>(
.and_then(|k| k.ok_or(SqliteClientError::IncorrectHrpExtFvk))
})?;
Ok((acct, extfvk))
Ok((AccountId::from(acct), extfvk))
})
.map_err(SqliteClientError::from)?;
@ -220,7 +220,7 @@ pub fn is_valid_account_extfvk<P: consensus::Parameters>(
wdb.conn
.prepare("SELECT * FROM accounts WHERE account = ? AND extfvk = ?")?
.exists(&[
account.0.to_sql()?,
u32::from(account).to_sql()?,
encode_extended_full_viewing_key(
wdb.params.hrp_sapling_extended_full_viewing_key(),
extfvk,
@ -253,7 +253,7 @@ pub fn is_valid_account_extfvk<P: consensus::Parameters>(
///
/// let data_file = NamedTempFile::new().unwrap();
/// let db = WalletDb::for_path(data_file, Network::TestNetwork).unwrap();
/// let addr = get_balance(&db, AccountId(0));
/// let addr = get_balance(&db, AccountId::from(0));
/// ```
#[deprecated(
note = "This function will be removed in a future release. Use zcash_client_backend::data_api::WalletRead::get_balance_at instead."
@ -263,7 +263,7 @@ pub fn get_balance<P>(wdb: &WalletDb<P>, account: AccountId) -> Result<Amount, S
"SELECT SUM(value) FROM received_notes
INNER JOIN transactions ON transactions.id_tx = received_notes.tx
WHERE account = ? AND spent IS NULL AND transactions.block IS NOT NULL",
&[account.0],
&[u32::from(account)],
|row| row.get(0).or(Ok(0)),
)?;
@ -294,7 +294,7 @@ pub fn get_balance<P>(wdb: &WalletDb<P>, account: AccountId) -> Result<Amount, S
///
/// let data_file = NamedTempFile::new().unwrap();
/// let db = WalletDb::for_path(data_file, Network::TestNetwork).unwrap();
/// let addr = get_balance_at(&db, AccountId(0), BlockHeight::from_u32(0));
/// let addr = get_balance_at(&db, AccountId::from(0), BlockHeight::from_u32(0));
/// ```
#[deprecated(
note = "This function will be removed in a future release. Use zcash_client_backend::data_api::WalletRead::get_balance_at instead."
@ -308,7 +308,7 @@ pub fn get_balance_at<P>(
"SELECT SUM(value) FROM received_notes
INNER JOIN transactions ON transactions.id_tx = received_notes.tx
WHERE account = ? AND spent IS NULL AND transactions.block <= ?",
&[account.0, u32::from(anchor_height)],
&[u32::from(account), u32::from(anchor_height)],
|row| row.get(0).or(Ok(0)),
)?;
@ -724,9 +724,12 @@ pub fn get_nullifiers<P>(
WHERE block IS NULL",
)?;
let nullifiers = stmt_fetch_nullifiers.query_map(NO_PARAMS, |row| {
let account = AccountId(row.get(1)?);
let account: u32 = row.get(1)?;
let nf_bytes: Vec<u8> = row.get(2)?;
Ok((account, Nullifier::from_slice(&nf_bytes).unwrap()))
Ok((
AccountId::from(account),
Nullifier::from_slice(&nf_bytes).unwrap(),
))
})?;
let res: Vec<_> = nullifiers.collect::<Result<_, _>>()?;
@ -743,9 +746,12 @@ pub(crate) fn get_all_nullifiers<P>(
FROM received_notes rn",
)?;
let nullifiers = stmt_fetch_nullifiers.query_map(NO_PARAMS, |row| {
let account = AccountId(row.get(1)?);
let account: u32 = row.get(1)?;
let nf_bytes: Vec<u8> = row.get(2)?;
Ok((account, Nullifier::from_slice(&nf_bytes).unwrap()))
Ok((
AccountId::from(account),
Nullifier::from_slice(&nf_bytes).unwrap(),
))
})?;
let res: Vec<_> = nullifiers.collect::<Result<_, _>>()?;
@ -778,13 +784,13 @@ pub(crate) fn get_unspent_transparent_outputs<P: consensus::Parameters>(
let mut txid_bytes = [0u8; 32];
txid_bytes.copy_from_slice(&id);
let index: i32 = row.get(1)?;
let index: u32 = row.get(1)?;
let script_pubkey = Script(row.get(2)?);
let value = Amount::from_i64(row.get(3)?).unwrap();
let height: u32 = row.get(4)?;
Ok(WalletTransparentOutput {
outpoint: OutPoint::new(txid_bytes, index as u32),
outpoint: OutPoint::new(txid_bytes, index),
txout: TxOut {
value,
script_pubkey,
@ -993,7 +999,7 @@ pub fn put_received_note<'a, P, T: ShieldedOutput>(
tx_ref: i64,
) -> Result<NoteId, SqliteClientError> {
let rcm = output.note().rcm().to_repr();
let account = output.account().0 as i64;
let account = u32::from(output.account());
let diversifier = output.to().diversifier().0.to_vec();
let value = output.note().value as i64;
let rcm = rcm.as_ref();
@ -1099,7 +1105,7 @@ pub fn put_sent_note<'a, P: consensus::Parameters>(
let ivalue: i64 = value.into();
// Try updating an existing sent note.
if stmts.stmt_update_sent_note.execute(params![
account.0 as i64,
u32::from(account),
encode_payment_address_p(&stmts.wallet_db.params, to),
ivalue,
&memo.map(|m| m.as_slice()),
@ -1134,7 +1140,7 @@ pub fn put_sent_utxo<'a, P: consensus::Parameters>(
let ivalue: i64 = value.into();
// Try updating an existing sent UTXO.
if stmts.stmt_update_sent_note.execute(params![
account.0 as i64,
u32::from(account),
encode_transparent_address_p(&stmts.wallet_db.params, to),
ivalue,
(None::<&[u8]>),
@ -1176,7 +1182,7 @@ pub fn insert_sent_note<'a, P: consensus::Parameters>(
tx_ref,
PoolType::Sapling.typecode(),
(output_index as i64),
account.0,
u32::from(account),
to_str,
ivalue,
memo.map(|m| m.as_slice().to_vec()),
@ -1204,8 +1210,8 @@ pub fn insert_sent_utxo<'a, P: consensus::Parameters>(
stmts.stmt_insert_sent_note.execute(params![
tx_ref,
PoolType::Transparent.typecode(),
(output_index as i64),
account.0,
output_index as i64,
u32::from(account),
to_str,
ivalue,
(None::<&[u8]>),
@ -1237,13 +1243,19 @@ mod tests {
tests::init_test_accounts_table(&db_data);
// The account should be empty
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
// We can't get an anchor height, as we have not scanned any blocks.
assert_eq!((&db_data).get_target_and_anchor_heights(10).unwrap(), None);
// An invalid account has zero balance
assert!(get_address(&db_data, AccountId(1)).is_err());
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert!(get_address(&db_data, AccountId::from(1)).is_err());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
}
}

View File

@ -177,7 +177,7 @@ pub fn init_wallet_db<P>(wdb: &WalletDb<P>) -> Result<(), rusqlite::Error> {
/// init_wallet_db(&db_data).unwrap();
///
/// let seed = [0u8; 32]; // insecure; replace with a strong random seed
/// let account = AccountId(0);
/// let account = AccountId::from(0);
/// let extsk = sapling::spending_key(&seed, Network::TestNetwork.coin_type(), account);
/// let extfvk = ExtendedFullViewingKey::from(&extsk);
/// let ufvk = UnifiedFullViewingKey::new(account, None, Some(extfvk)).unwrap();
@ -222,7 +222,12 @@ pub fn init_accounts_table<P: consensus::Parameters>(
wdb.conn.execute(
"INSERT INTO accounts (account, extfvk, address, transparent_address)
VALUES (?, ?, ?, ?)",
params![key.account().0, extfvk_str, address_str, taddress_str,],
params![
u32::from(key.account()),
extfvk_str,
address_str,
taddress_str,
],
)?;
}
wdb.conn.execute("COMMIT", NO_PARAMS)?;
@ -323,7 +328,7 @@ mod tests {
init_accounts_table(&db_data, &[]).unwrap();
let seed = [0u8; 32];
let account = AccountId(0);
let account = AccountId::from(0);
// First call with data should initialise the accounts table
let extsk = sapling::spending_key(&seed, network().coin_type(), account);
@ -387,14 +392,14 @@ mod tests {
let seed = [0u8; 32];
// Add an account to the wallet
let account_id = AccountId(0);
let account_id = AccountId::from(0);
let usk = UnifiedSpendingKey::from_seed(&tests::network(), &seed, account_id).unwrap();
let ufvk = usk.to_unified_full_viewing_key();
let expected_address = ufvk.sapling().unwrap().default_address().1;
init_accounts_table(&db_data, &[ufvk]).unwrap();
// The account's address should be in the data DB
let pa = get_address(&db_data, AccountId(0)).unwrap();
let pa = get_address(&db_data, AccountId::from(0)).unwrap();
assert_eq!(pa.unwrap(), expected_address);
}
}

View File

@ -82,7 +82,7 @@ pub fn get_spendable_sapling_notes<P>(
// Select notes
let notes = stmt_select_notes.query_and_then_named::<_, SqliteClientError, _>(
named_params![
":account": &i64::from(account.0),
":account": &u32::from(account),
":anchor_height": &u32::from(anchor_height),
],
to_spendable_note,
@ -143,7 +143,7 @@ pub fn select_spendable_sapling_notes<P>(
// Select notes
let notes = stmt_select_notes.query_and_then_named::<_, SqliteClientError, _>(
named_params![
":account": &i64::from(account.0),
":account": &u32::from(account),
":anchor_height": &u32::from(anchor_height),
":target_value": &i64::from(target_value),
],
@ -205,26 +205,28 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add two accounts to the wallet
let extsk0 = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk1 = sapling::spending_key(&[1u8; 32], network().coin_type(), AccountId(1));
let extsk0 = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extsk1 = sapling::spending_key(&[1u8; 32], network().coin_type(), AccountId::from(1));
let extfvk0 = ExtendedFullViewingKey::from(&extsk0);
let extfvk1 = ExtendedFullViewingKey::from(&extsk1);
#[cfg(feature = "transparent-inputs")]
let ufvks = {
let tsk0 = transparent::AccountPrivKey::from_seed(&network(), &[0u8; 32], AccountId(0))
.unwrap();
let tsk1 = transparent::AccountPrivKey::from_seed(&network(), &[1u8; 32], AccountId(1))
.unwrap();
let tsk0 =
transparent::AccountPrivKey::from_seed(&network(), &[0u8; 32], AccountId::from(0))
.unwrap();
let tsk1 =
transparent::AccountPrivKey::from_seed(&network(), &[1u8; 32], AccountId::from(1))
.unwrap();
[
UnifiedFullViewingKey::new(
AccountId(0),
AccountId::from(0),
Some(tsk0.to_account_pubkey()),
Some(extfvk0),
)
.unwrap(),
UnifiedFullViewingKey::new(
AccountId(1),
AccountId::from(1),
Some(tsk1.to_account_pubkey()),
Some(extfvk1),
)
@ -233,8 +235,8 @@ mod tests {
};
#[cfg(not(feature = "transparent-inputs"))]
let ufvks = [
UnifiedFullViewingKey::new(AccountId(0), Some(extfvk0)).unwrap(),
UnifiedFullViewingKey::new(AccountId(1), Some(extfvk1)).unwrap(),
UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk0)).unwrap(),
UnifiedFullViewingKey::new(AccountId::from(1), Some(extfvk1)).unwrap(),
];
init_accounts_table(&db_data, &ufvks).unwrap();
@ -246,7 +248,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk1,
&to,
Amount::from_u64(1).unwrap(),
@ -262,7 +264,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(1),
AccountId::from(1),
&extsk0,
&to,
Amount::from_u64(1).unwrap(),
@ -282,13 +284,13 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk)).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk)).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk)).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
let to = extsk.default_address().1.into();
@ -298,7 +300,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(1).unwrap(),
@ -326,17 +328,20 @@ mod tests {
.unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk)).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk)).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk)).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk)).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
let to = extsk.default_address().1.into();
// Account balance should be zero
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), Amount::zero());
assert_eq!(
get_balance(&db_data, AccountId::from(0)).unwrap(),
Amount::zero()
);
// We cannot spend anything
let mut db_write = db_data.get_update_ops().unwrap();
@ -344,7 +349,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(1).unwrap(),
@ -371,12 +376,13 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
let ufvk =
UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk.clone())).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk.clone())).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
// Add funds to the wallet in a single note
@ -396,9 +402,9 @@ mod tests {
.get_target_and_anchor_heights(10)
.unwrap()
.unwrap();
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance_at(&db_data, AccountId(0), anchor_height).unwrap(),
get_balance_at(&db_data, AccountId::from(0), anchor_height).unwrap(),
value
);
@ -418,11 +424,11 @@ mod tests {
.unwrap()
.unwrap();
assert_eq!(
get_balance(&db_data, AccountId(0)).unwrap(),
get_balance(&db_data, AccountId::from(0)).unwrap(),
(value + value).unwrap()
);
assert_eq!(
get_balance_at(&db_data, AccountId(0), anchor_height2).unwrap(),
get_balance_at(&db_data, AccountId::from(0), anchor_height2).unwrap(),
value
);
@ -433,7 +439,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(70000).unwrap(),
@ -466,7 +472,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(70000).unwrap(),
@ -492,7 +498,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(70000).unwrap(),
@ -514,12 +520,13 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
let ufvk =
UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk.clone())).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk.clone())).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
// Add funds to the wallet in a single note
@ -533,7 +540,7 @@ mod tests {
insert_into_cache(&db_cache, &cb);
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
// Send some of the funds to another address
let extsk2 = ExtendedSpendingKey::master(&[]);
@ -542,7 +549,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(15000).unwrap(),
@ -557,7 +564,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
@ -590,7 +597,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
@ -620,7 +627,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
@ -643,12 +650,13 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network.coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network.coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
let ufvk =
UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk.clone())).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk.clone())).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
// Add funds to the wallet in a single note
@ -662,7 +670,7 @@ mod tests {
insert_into_cache(&db_cache, &cb);
let mut db_write = db_data.get_update_ops().unwrap();
scan_cached_blocks(&tests::network(), &db_cache, &mut db_write, None).unwrap();
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
let extsk2 = ExtendedSpendingKey::master(&[]);
let addr2 = extsk2.default_address().1;
@ -673,7 +681,7 @@ mod tests {
db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(15000).unwrap(),
@ -753,12 +761,13 @@ mod tests {
init_wallet_db(&db_data).unwrap();
// Add an account to the wallet
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId(0));
let extsk = sapling::spending_key(&[0u8; 32], network().coin_type(), AccountId::from(0));
let extfvk = ExtendedFullViewingKey::from(&extsk);
#[cfg(feature = "transparent-inputs")]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), None, Some(extfvk.clone())).unwrap();
let ufvk =
UnifiedFullViewingKey::new(AccountId::from(0), None, Some(extfvk.clone())).unwrap();
#[cfg(not(feature = "transparent-inputs"))]
let ufvk = UnifiedFullViewingKey::new(AccountId(0), Some(extfvk.clone())).unwrap();
let ufvk = UnifiedFullViewingKey::new(AccountId::from(0), Some(extfvk.clone())).unwrap();
init_accounts_table(&db_data, &[ufvk]).unwrap();
// Add funds to the wallet in a single note
@ -778,9 +787,9 @@ mod tests {
.get_target_and_anchor_heights(10)
.unwrap()
.unwrap();
assert_eq!(get_balance(&db_data, AccountId(0)).unwrap(), value);
assert_eq!(get_balance(&db_data, AccountId::from(0)).unwrap(), value);
assert_eq!(
get_balance_at(&db_data, AccountId(0), anchor_height).unwrap(),
get_balance_at(&db_data, AccountId::from(0), anchor_height).unwrap(),
value
);
@ -789,7 +798,7 @@ mod tests {
&mut db_write,
&tests::network(),
test_prover(),
AccountId(0),
AccountId::from(0),
&extsk,
&to,
Amount::from_u64(50000).unwrap(),

View File

@ -29,7 +29,7 @@ impl AccountPrivKey {
ExtendedPrivKey::with_seed(seed)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(44)?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(params.coin_type())?)?
.derive_private_key(KeyIndex::hardened_from_normalize_index(account.0)?)
.derive_private_key(KeyIndex::hardened_from_normalize_index(account.into())?)
.map(AccountPrivKey)
}

View File

@ -27,7 +27,7 @@ pub const ZIP32_SAPLING_INT_PERSONALIZATION: &[u8; 16] = b"Zcash_SaplingInt";
/// A type-safe wrapper for account identifiers.
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AccountId(pub u32);
pub struct AccountId(u32);
impl From<u32> for AccountId {
fn from(id: u32) -> Self {
@ -35,9 +35,9 @@ impl From<u32> for AccountId {
}
}
impl Into<u32> for AccountId {
fn into(self) -> u32 {
self.0
impl From<AccountId> for u32 {
fn from(id: AccountId) -> Self {
id.0
}
}