diff --git a/components/equihash/src/verify.rs b/components/equihash/src/verify.rs index f356acdf9..1fdbb91a9 100644 --- a/components/equihash/src/verify.rs +++ b/components/equihash/src/verify.rs @@ -314,7 +314,7 @@ fn tree_validator(p: &Params, state: &Blake2bState, indices: &[u32]) -> Result State<'a> { let hash = Blake2bParams::new() .hash_length(self.left.len()) .personal(&H_PERS!(i)) - .hash(&self.right); + .hash(self.right); xor(self.left, hash.as_bytes()) } @@ -103,7 +103,7 @@ impl<'a> State<'a> { let hash = Blake2bParams::new() .hash_length(OUTBYTES) .personal(&G_PERS!(i, j as u16)) - .hash(&self.left); + .hash(self.left); xor(&mut self.right[j * OUTBYTES..], hash.as_bytes()); } } @@ -155,25 +155,15 @@ pub fn f4jumble_inv_mut(message: &mut [u8]) -> Result<(), Error> { } #[cfg(feature = "std")] -pub fn f4jumble(message: &[u8]) -> Option> { +pub fn f4jumble(message: &[u8]) -> Result, Error> { let mut result = message.to_vec(); - let res = f4jumble_mut(&mut result); - if res.is_ok() { - Some(result) - } else { - None - } + f4jumble_mut(&mut result).map(|()| result) } #[cfg(feature = "std")] -pub fn f4jumble_inv(message: &[u8]) -> Option> { +pub fn f4jumble_inv(message: &[u8]) -> Result, Error> { let mut result = message.to_vec(); - let res = f4jumble_inv_mut(&mut result); - if res.is_ok() { - Some(result) - } else { - None - } + f4jumble_inv_mut(&mut result).map(|()| result) } #[cfg(test)] @@ -198,11 +188,11 @@ mod common_tests { #[cfg(feature = "std")] let mut cache = vec![0u8; test_vectors::MAX_VECTOR_LENGTH]; for v in test_vectors::TEST_VECTORS { - let mut data = &mut cache[..v.normal.len()]; - data.clone_from_slice(&v.normal); - f4jumble_mut(&mut data).unwrap(); + let data = &mut cache[..v.normal.len()]; + data.clone_from_slice(v.normal); + f4jumble_mut(data).unwrap(); assert_eq!(data, v.jumbled); - f4jumble_inv_mut(&mut data).unwrap(); + f4jumble_inv_mut(data).unwrap(); assert_eq!(data, v.normal); } } @@ -246,9 +236,9 @@ mod std_tests { #[test] fn f4jumble_check_vectors() { for v in test_vectors::TEST_VECTORS { - let jumbled = f4jumble(&v.normal).unwrap(); + let jumbled = f4jumble(v.normal).unwrap(); assert_eq!(jumbled, v.jumbled); - let unjumbled = f4jumble_inv(&v.jumbled).unwrap(); + let unjumbled = f4jumble_inv(v.jumbled).unwrap(); assert_eq!(unjumbled, v.normal); } } diff --git a/components/zcash_address/src/kind/unified.rs b/components/zcash_address/src/kind/unified.rs index 7a501c2d3..533516354 100644 --- a/components/zcash_address/src/kind/unified.rs +++ b/components/zcash_address/src/kind/unified.rs @@ -222,11 +222,12 @@ pub(crate) mod private { writer.write_all(&padding).unwrap(); let padded = writer.into_inner(); - f4jumble::f4jumble(&padded).unwrap_or_else(|| panic!("f4jumble failed on {:?}", padded)) + f4jumble::f4jumble(&padded) + .unwrap_or_else(|e| panic!("f4jumble failed on {:?}: {}", padded, e)) } /// Parse the items of the unified container. - fn parse_items(hrp: &str, buf: &[u8]) -> Result, ParseError> { + fn parse_items>>(hrp: &str, buf: T) -> Result, ParseError> { fn read_receiver( mut cursor: &mut std::io::Cursor<&[u8]>, ) -> Result { @@ -265,8 +266,10 @@ pub(crate) mod private { result } - let encoded = f4jumble::f4jumble_inv(buf).ok_or_else(|| { - ParseError::InvalidEncoding("F4Jumble decoding failed".to_owned()) + // Here we allocate if necessary to get a mutable Vec to unjumble. + let mut encoded = buf.into(); + f4jumble::f4jumble_inv_mut(&mut encoded[..]).map_err(|e| { + ParseError::InvalidEncoding(format!("F4Jumble decoding failed: {}", e)) })?; // Validate and strip trailing padding bytes. @@ -326,7 +329,7 @@ pub(crate) mod private { } } - fn parse_internal(hrp: &str, buf: &[u8]) -> Result { + fn parse_internal>>(hrp: &str, buf: T) -> Result { Self::parse_items(hrp, buf).and_then(Self::try_from_items_internal) } } @@ -364,7 +367,7 @@ pub trait Encoding: private::SealedContainer { let data = Vec::::from_base32(&data) .map_err(|e| ParseError::InvalidEncoding(e.to_string()))?; - Self::parse_internal(hrp, &data[..]).map(|value| (net, value)) + Self::parse_internal(hrp, data).map(|value| (net, value)) } else { Err(ParseError::NotUnified) } diff --git a/components/zcash_note_encryption/src/lib.rs b/components/zcash_note_encryption/src/lib.rs index ed3edd5c3..8a02098e6 100644 --- a/components/zcash_note_encryption/src/lib.rs +++ b/components/zcash_note_encryption/src/lib.rs @@ -475,7 +475,7 @@ impl NoteEncryption { rng: &mut R, ) -> [u8; OUT_CIPHERTEXT_SIZE] { let (ock, input) = if let Some(ovk) = &self.ovk { - let ock = D::derive_ock(ovk, &cv, &cmstar.into(), &D::epk_bytes(&self.epk)); + let ock = D::derive_ock(ovk, cv, &cmstar.into(), &D::epk_bytes(&self.epk)); let input = D::outgoing_plaintext_bytes(&self.note, &self.esk); (ock, input) @@ -563,7 +563,7 @@ fn parse_note_plaintext_without_memo_ivk( cmstar_bytes: &D::ExtractedCommitmentBytes, plaintext: &[u8], ) -> Option<(D::Note, D::Recipient)> { - let (note, to) = domain.parse_note_plaintext_without_memo_ivk(ivk, &plaintext)?; + let (note, to) = domain.parse_note_plaintext_without_memo_ivk(ivk, plaintext)?; if let NoteValidity::Valid = check_note_validity::(¬e, ephemeral_key, cmstar_bytes) { Some((note, to)) @@ -577,10 +577,10 @@ fn check_note_validity( ephemeral_key: &EphemeralKeyBytes, cmstar_bytes: &D::ExtractedCommitmentBytes, ) -> NoteValidity { - if &D::ExtractedCommitmentBytes::from(&D::cmstar(¬e)) == cmstar_bytes { + if &D::ExtractedCommitmentBytes::from(&D::cmstar(note)) == cmstar_bytes { if let Some(derived_esk) = D::derive_esk(note) { - if D::epk_bytes(&D::ka_derive_public(¬e, &derived_esk)) - .ct_eq(&ephemeral_key) + if D::epk_bytes(&D::ka_derive_public(note, &derived_esk)) + .ct_eq(ephemeral_key) .into() { NoteValidity::Valid @@ -614,7 +614,7 @@ pub fn try_compact_note_decryption Option<(D::Note, D::Recipient, D::Memo)> { - let ock = D::derive_ock(ovk, &cv, &output.cmstar_bytes(), &output.ephemeral_key()); + let ock = D::derive_ock(ovk, cv, &output.cmstar_bytes(), &output.ephemeral_key()); try_output_recovery_with_ock(domain, &ock, output, out_ciphertext) } diff --git a/zcash_client_backend/src/data_api/error.rs b/zcash_client_backend/src/data_api/error.rs index bd1dc9559..132e3bb02 100644 --- a/zcash_client_backend/src/data_api/error.rs +++ b/zcash_client_backend/src/data_api/error.rs @@ -92,7 +92,7 @@ impl fmt::Display for Error { 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, diff --git a/zcash_client_backend/src/data_api/wallet.rs b/zcash_client_backend/src/data_api/wallet.rs index c81c43085..0ba8fe107 100644 --- a/zcash_client_backend/src/data_api/wallet.rs +++ b/zcash_client_backend/src/data_api/wallet.rs @@ -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(); /// @@ -347,7 +347,7 @@ where Err(Error::MemoForbidden) } else { builder - .add_transparent_output(&to, payment.amount) + .add_transparent_output(to, payment.amount) .map_err(Error::Builder) } } @@ -439,7 +439,7 @@ where { // Check that the ExtendedSpendingKey we have been given corresponds to the // ExtendedFullViewingKey for the account we are spending from. - if !wallet_db.is_valid_account_extfvk(account, &extfvk)? { + if !wallet_db.is_valid_account_extfvk(account, extfvk)? { return Err(E::from(Error::InvalidExtSk(account))); } diff --git a/zcash_client_backend/src/encoding.rs b/zcash_client_backend/src/encoding.rs index 14e552efc..1bebbf5cf 100644 --- a/zcash_client_backend/src/encoding.rs +++ b/zcash_client_backend/src/encoding.rs @@ -32,7 +32,7 @@ where { match bech32::decode(s)? { (decoded_hrp, data, Variant::Bech32) if decoded_hrp == hrp => { - Vec::::from_base32(&data).map(|data| read(data)) + Vec::::from_base32(&data).map(read) } _ => Ok(None), } @@ -107,7 +107,7 @@ impl AddressCodec

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); /// ``` diff --git a/zcash_client_backend/src/keys.rs b/zcash_client_backend/src/keys.rs index ba9789351..7a43e0d51 100644 --- a/zcash_client_backend/src/keys.rs +++ b/zcash_client_backend/src/keys.rs @@ -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 { @@ -35,11 +35,11 @@ pub mod sapling { } ExtendedSpendingKey::from_path( - &ExtendedSpendingKey::master(&seed), + &ExtendedSpendingKey::master(seed), &[ 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()); } } diff --git a/zcash_client_backend/src/welding_rig.rs b/zcash_client_backend/src/welding_rig.rs index fc943ac32..0614e4a39 100644 --- a/zcash_client_backend/src/welding_rig.rs +++ b/zcash_client_backend/src/welding_rig.rs @@ -68,7 +68,7 @@ fn scan_output( // - Change created by spending fractions of notes. // - Notes created by consolidation transactions. // - Notes sent from one account to itself. - let is_change = spent_from_accounts.contains(&account); + let is_change = spent_from_accounts.contains(account); let witness = IncrementalWitness::from_tree(tree); let nf = vk.nf(¬e, &witness); @@ -229,7 +229,7 @@ pub fn scan_block( .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); diff --git a/zcash_client_backend/src/zip321.rs b/zcash_client_backend/src/zip321.rs index c6e3cdda2..2d575844d 100644 --- a/zcash_client_backend/src/zip321.rs +++ b/zcash_client_backend/src/zip321.rs @@ -173,32 +173,32 @@ impl TransactionRequest { payment .memo .as_ref() - .map(|m| render::memo_param(&m, payment_index)), + .map(|m| render::memo_param(m, payment_index)), ) .chain( payment .label .as_ref() - .map(|m| render::str_param("label", &m, payment_index)), + .map(|m| render::str_param("label", m, payment_index)), ) .chain( payment .message .as_ref() - .map(|m| render::str_param("message", &m, payment_index)), + .map(|m| render::str_param("message", m, payment_index)), ) .chain( payment .other_params .iter() - .map(move |(name, value)| render::str_param(&name, &value, payment_index)), + .map(move |(name, value)| render::str_param(name, value, payment_index)), ) } match &self.payments[..] { [] => None, [payment] => { - let query_params = payment_params(&payment, None) + let query_params = payment_params(payment, None) .into_iter() .collect::>(); @@ -217,7 +217,7 @@ impl TransactionRequest { let primary_address = payment.recipient_address.clone(); std::iter::empty() .chain(Some(render::addr_param(params, &primary_address, Some(i)))) - .chain(payment_params(&payment, Some(i))) + .chain(payment_params(payment, Some(i))) }) .collect::>(); @@ -255,7 +255,7 @@ impl TransactionRequest { } Some(current) => { - if parse::has_duplicate_param(¤t, &p.param) { + if parse::has_duplicate_param(current, &p.param) { return Err(Zip321Error::DuplicateParameter(p.param, p.payment_index)); } else { current.push(p.param); @@ -775,12 +775,12 @@ mod tests { #[test] fn test_zip321_parse_simple() { let uri = "zcash:ztestsapling1n65uaftvs2g7075q2x2a04shfk066u3lldzxsrprfrqtzxnhc9ps73v4lhx4l9yfxj46sl0q90k?amount=3768769.02796286&message="; - let parse_result = TransactionRequest::from_uri(&TEST_NETWORK, &uri).unwrap(); + let parse_result = TransactionRequest::from_uri(&TEST_NETWORK, uri).unwrap(); let expected = TransactionRequest { payments: vec![ Payment { - recipient_address: RecipientAddress::Shielded(decode_payment_address(&TEST_NETWORK.hrp_sapling_payment_address(), "ztestsapling1n65uaftvs2g7075q2x2a04shfk066u3lldzxsrprfrqtzxnhc9ps73v4lhx4l9yfxj46sl0q90k").unwrap().unwrap()), + recipient_address: RecipientAddress::Shielded(decode_payment_address(TEST_NETWORK.hrp_sapling_payment_address(), "ztestsapling1n65uaftvs2g7075q2x2a04shfk066u3lldzxsrprfrqtzxnhc9ps73v4lhx4l9yfxj46sl0q90k").unwrap().unwrap()), amount: Amount::from_u64(376876902796286).unwrap(), memo: None, label: None, @@ -833,14 +833,14 @@ mod tests { #[test] fn test_zip321_spec_valid_examples() { let valid_1 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=1&memo=VGhpcyBpcyBhIHNpbXBsZSBtZW1vLg&message=Thank%20you%20for%20your%20purchase"; - let v1r = TransactionRequest::from_uri(&TEST_NETWORK, &valid_1).unwrap(); + let v1r = TransactionRequest::from_uri(&TEST_NETWORK, valid_1).unwrap(); assert_eq!( v1r.payments.get(0).map(|p| p.amount), Some(Amount::from_u64(100000000).unwrap()) ); let valid_2 = "zcash:?address=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU&amount=123.456&address.1=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez&amount.1=0.789&memo.1=VGhpcyBpcyBhIHVuaWNvZGUgbWVtbyDinKjwn6aE8J-PhvCfjok"; - let mut v2r = TransactionRequest::from_uri(&TEST_NETWORK, &valid_2).unwrap(); + let mut v2r = TransactionRequest::from_uri(&TEST_NETWORK, valid_2).unwrap(); v2r.normalize(&TEST_NETWORK); assert_eq!( v2r.payments.get(0).map(|p| p.amount), @@ -854,7 +854,7 @@ mod tests { // valid; amount just less than MAX_MONEY // 20999999.99999999 let valid_3 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=20999999.99999999"; - let v3r = TransactionRequest::from_uri(&TEST_NETWORK, &valid_3).unwrap(); + let v3r = TransactionRequest::from_uri(&TEST_NETWORK, valid_3).unwrap(); assert_eq!( v3r.payments.get(0).map(|p| p.amount), Some(Amount::from_u64(2099999999999999u64).unwrap()) @@ -863,7 +863,7 @@ mod tests { // valid; MAX_MONEY // 21000000 let valid_4 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=21000000"; - let v4r = TransactionRequest::from_uri(&TEST_NETWORK, &valid_4).unwrap(); + let v4r = TransactionRequest::from_uri(&TEST_NETWORK, valid_4).unwrap(); assert_eq!( v4r.payments.get(0).map(|p| p.amount), Some(Amount::from_u64(2100000000000000u64).unwrap()) @@ -874,63 +874,63 @@ mod tests { fn test_zip321_spec_invalid_examples() { // invalid; missing `address=` let invalid_1 = "zcash:?amount=3491405.05201255&address.1=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez&amount.1=5740296.87793245"; - let i1r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_1); + let i1r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_1); assert!(i1r.is_err()); // invalid; missing `address.1=` let invalid_2 = "zcash:?address=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU&amount=1&amount.1=2&address.2=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez"; - let i2r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_2); + let i2r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_2); assert!(i2r.is_err()); // invalid; `address.0=` and `amount.0=` are not permitted (leading 0s). let invalid_3 = "zcash:?address.0=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez&amount.0=2"; - let i3r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_3); + let i3r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_3); assert!(i3r.is_err()); // invalid; duplicate `amount=` field let invalid_4 = "zcash:?amount=1.234&amount=2.345&address=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU"; - let i4r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_4); + let i4r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_4); assert!(i4r.is_err()); // invalid; duplicate `amount.1=` field let invalid_5 = "zcash:?amount.1=1.234&amount.1=2.345&address.1=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU"; - let i5r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_5); + let i5r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_5); assert!(i5r.is_err()); //invalid; memo associated with t-addr let invalid_6 = "zcash:?address=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU&amount=123.456&memo=eyAia2V5IjogIlRoaXMgaXMgYSBKU09OLXN0cnVjdHVyZWQgbWVtby4iIH0&address.1=ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez&amount.1=0.789&memo.1=VGhpcyBpcyBhIHVuaWNvZGUgbWVtbyDinKjwn6aE8J-PhvCfjok"; - let i6r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_6); + let i6r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_6); assert!(i6r.is_err()); // invalid; amount component exceeds an i64 // 9223372036854775808 = i64::MAX + 1 let invalid_7 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=9223372036854775808"; - let i7r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_7); + let i7r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_7); assert!(i7r.is_err()); // invalid; amount component wraps into a valid small positive i64 // 18446744073709551624 let invalid_7a = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=18446744073709551624"; - let i7ar = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_7a); + let i7ar = TransactionRequest::from_uri(&TEST_NETWORK, invalid_7a); assert!(i7ar.is_err()); // invalid; amount component is MAX_MONEY // 21000000.00000001 let invalid_8 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=21000000.00000001"; - let i8r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_8); + let i8r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_8); assert!(i8r.is_err()); // invalid; negative amount let invalid_9 = "zcash:ztestsapling10yy2ex5dcqkclhc7z7yrnjq2z6feyjad56ptwlfgmy77dmaqqrl9gyhprdx59qgmsnyfska2kez?amount=-1"; - let i9r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_9); + let i9r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_9); assert!(i9r.is_err()); // invalid; parameter index too large let invalid_10 = "zcash:?amount.10000=1.23&address.10000=tmEZhbWHTpdKMw5it8YDspUXSMGQyFwovpU"; - let i10r = TransactionRequest::from_uri(&TEST_NETWORK, &invalid_10); + let i10r = TransactionRequest::from_uri(&TEST_NETWORK, invalid_10); assert!(i10r.is_err()); } diff --git a/zcash_client_sqlite/src/chain.rs b/zcash_client_sqlite/src/chain.rs index 2bc61ead7..66866f655 100644 --- a/zcash_client_sqlite/src/chain.rs +++ b/zcash_client_sqlite/src/chain.rs @@ -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() ); } diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 50acbac7a..199c10377 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -212,29 +212,29 @@ impl WalletRead for WalletDb

{ fn block_height_extrema(&self) -> Result, Self::Error> { #[allow(deprecated)] - wallet::block_height_extrema(&self).map_err(SqliteClientError::from) + wallet::block_height_extrema(self).map_err(SqliteClientError::from) } fn get_block_hash(&self, block_height: BlockHeight) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_block_hash(&self, block_height).map_err(SqliteClientError::from) + wallet::get_block_hash(self, block_height).map_err(SqliteClientError::from) } fn get_tx_height(&self, txid: TxId) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_tx_height(&self, txid).map_err(SqliteClientError::from) + wallet::get_tx_height(self, txid).map_err(SqliteClientError::from) } fn get_extended_full_viewing_keys( &self, ) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_extended_full_viewing_keys(&self) + wallet::get_extended_full_viewing_keys(self) } fn get_address(&self, account: AccountId) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_address(&self, account) + wallet::get_address(self, account) } fn is_valid_account_extfvk( @@ -243,7 +243,7 @@ impl WalletRead for WalletDb

{ extfvk: &ExtendedFullViewingKey, ) -> Result { #[allow(deprecated)] - wallet::is_valid_account_extfvk(&self, account, extfvk) + wallet::is_valid_account_extfvk(self, account, extfvk) } fn get_balance_at( @@ -252,12 +252,12 @@ impl WalletRead for WalletDb

{ anchor_height: BlockHeight, ) -> Result { #[allow(deprecated)] - wallet::get_balance_at(&self, account, anchor_height) + wallet::get_balance_at(self, account, anchor_height) } fn get_transaction(&self, id_tx: i64) -> Result { #[allow(deprecated)] - wallet::get_transaction(&self, id_tx) + wallet::get_transaction(self, id_tx) } fn get_memo(&self, id_note: Self::NoteRef) -> Result { @@ -273,7 +273,7 @@ impl WalletRead for WalletDb

{ block_height: BlockHeight, ) -> Result>, Self::Error> { #[allow(deprecated)] - wallet::get_commitment_tree(&self, block_height) + wallet::get_commitment_tree(self, block_height) } #[allow(clippy::type_complexity)] @@ -282,17 +282,17 @@ impl WalletRead for WalletDb

{ block_height: BlockHeight, ) -> Result)>, Self::Error> { #[allow(deprecated)] - wallet::get_witnesses(&self, block_height) + wallet::get_witnesses(self, block_height) } fn get_nullifiers(&self) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_nullifiers(&self) + wallet::get_nullifiers(self) } fn get_all_nullifiers(&self) -> Result, Self::Error> { #[allow(deprecated)] - wallet::get_all_nullifiers(&self) + wallet::get_all_nullifiers(self) } fn get_spendable_sapling_notes( @@ -301,7 +301,7 @@ impl WalletRead for WalletDb

{ anchor_height: BlockHeight, ) -> Result, Self::Error> { #[allow(deprecated)] - wallet::transact::get_spendable_sapling_notes(&self, account, anchor_height) + wallet::transact::get_spendable_sapling_notes(self, account, anchor_height) } fn select_spendable_sapling_notes( @@ -311,12 +311,7 @@ impl WalletRead for WalletDb

{ anchor_height: BlockHeight, ) -> Result, Self::Error> { #[allow(deprecated)] - wallet::transact::select_spendable_sapling_notes( - &self, - account, - target_value, - anchor_height, - ) + wallet::transact::select_spendable_sapling_notes(self, account, target_value, anchor_height) } } @@ -327,7 +322,7 @@ impl WalletReadTransparent for WalletDb

{ address: &TransparentAddress, max_height: BlockHeight, ) -> Result, Self::Error> { - wallet::get_unspent_transparent_outputs(&self, address, max_height) + wallet::get_unspent_transparent_outputs(self, address, max_height) } } @@ -519,12 +514,12 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { block.block_height, block.block_hash, block.block_time, - &block.commitment_tree, + block.commitment_tree, )?; let mut new_witnesses = vec![]; for tx in block.transactions { - let tx_row = wallet::put_tx_meta(up, &tx, block.block_height)?; + let tx_row = wallet::put_tx_meta(up, tx, block.block_height)?; // Mark notes as spent and remove them from the scanning cache for spend in &tx.shielded_spends { @@ -624,7 +619,7 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { fn store_sent_tx(&mut self, sent_tx: &SentTransaction) -> Result { // Update the database atomically, to ensure the result is internally consistent. self.transactionally(|up| { - let tx_ref = wallet::put_tx_data(up, &sent_tx.tx, Some(sent_tx.created))?; + let tx_ref = wallet::put_tx_data(up, sent_tx.tx, Some(sent_tx.created))?; // Mark notes as spent. // @@ -642,7 +637,7 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { #[cfg(feature = "transparent-inputs")] for utxo_outpoint in &sent_tx.utxos_spent { - wallet::mark_transparent_utxo_spent(up, tx_ref, &utxo_outpoint)?; + wallet::mark_transparent_utxo_spent(up, tx_ref, utxo_outpoint)?; } for output in &sent_tx.outputs { @@ -652,7 +647,7 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { tx_ref, output.output_index, sent_tx.account, - &addr, + addr, output.value, output.memo.as_ref(), )?, @@ -661,7 +656,7 @@ impl<'a, P: consensus::Parameters> WalletWrite for DataConnStmtCache<'a, P> { tx_ref, output.output_index, sent_tx.account, - &addr, + addr, output.value, )?, } @@ -785,7 +780,7 @@ mod tests { db_data: &WalletDb, ) -> (ExtendedFullViewingKey, Option) { 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); diff --git a/zcash_client_sqlite/src/wallet.rs b/zcash_client_sqlite/src/wallet.rs index 720873d7a..8b17042ef 100644 --- a/zcash_client_sqlite/src/wallet.rs +++ b/zcash_client_sqlite/src/wallet.rs @@ -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( 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( 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( .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( 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( /// /// 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

(wdb: &WalletDb

, account: AccountId) -> Result(wdb: &WalletDb

, account: AccountId) -> Result( "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)), )?; @@ -564,12 +564,12 @@ pub(crate) fn rewind_to_height( .query_row("SELECT MAX(height) FROM blocks", NO_PARAMS, |row| { row.get(0) .map(|h: u32| h.into()) - .or(Ok(sapling_activation_height - 1)) + .or_else(|_| Ok(sapling_activation_height - 1)) })?; if block_height < last_scanned_height - PRUNING_HEIGHT { #[allow(deprecated)] - if let Some(h) = get_rewind_height(&wdb)? { + if let Some(h) = get_rewind_height(wdb)? { if block_height > h { return Err(SqliteClientError::RequestedRewindInvalid(h, block_height)); } @@ -724,9 +724,12 @@ pub fn get_nullifiers

( 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 = 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::>()?; @@ -743,9 +746,12 @@ pub(crate) fn get_all_nullifiers

( 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 = 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::>()?; @@ -778,13 +784,13 @@ pub(crate) fn get_unspent_transparent_outputs( 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, @@ -920,14 +926,14 @@ pub(crate) fn mark_transparent_utxo_spent<'a, P>( outpoint: &OutPoint, ) -> Result<(), SqliteClientError> { let sql_args: &[(&str, &dyn ToSql)] = &[ - (&":spent_in_tx", &tx_ref), - (&":prevout_txid", &outpoint.hash().to_vec()), - (&":prevout_idx", &outpoint.n()), + (":spent_in_tx", &tx_ref), + (":prevout_txid", &outpoint.hash().to_vec()), + (":prevout_idx", &outpoint.n()), ]; stmts .stmt_mark_transparent_utxo_spent - .execute_named(&sql_args)?; + .execute_named(sql_args)?; Ok(()) } @@ -940,19 +946,19 @@ pub(crate) fn put_received_transparent_utxo<'a, P: consensus::Parameters>( ) -> Result { let sql_args: &[(&str, &dyn ToSql)] = &[ ( - &":address", + ":address", &output.address().encode(&stmts.wallet_db.params), ), - (&":prevout_txid", &output.outpoint.hash().to_vec()), - (&":prevout_idx", &output.outpoint.n()), - (&":script", &output.txout.script_pubkey.0), - (&":value_zat", &i64::from(output.txout.value)), - (&":height", &u32::from(output.height)), + (":prevout_txid", &output.outpoint.hash().to_vec()), + (":prevout_idx", &output.outpoint.n()), + (":script", &output.txout.script_pubkey.0), + (":value_zat", &i64::from(output.txout.value)), + (":height", &u32::from(output.height)), ]; stmts .stmt_insert_received_transparent_utxo - .execute_named(&sql_args)?; + .execute_named(sql_args)?; Ok(UtxoId(stmts.wallet_db.conn.last_insert_rowid())) } @@ -969,11 +975,11 @@ pub fn delete_utxos_above<'a, P: consensus::Parameters>( height: BlockHeight, ) -> Result { let sql_args: &[(&str, &dyn ToSql)] = &[ - (&":address", &taddr.encode(&stmts.wallet_db.params)), - (&":above_height", &u32::from(height)), + (":address", &taddr.encode(&stmts.wallet_db.params)), + (":above_height", &u32::from(height)), ]; - let rows = stmts.stmt_delete_utxos.execute_named(&sql_args)?; + let rows = stmts.stmt_delete_utxos.execute_named(sql_args)?; Ok(rows) } @@ -993,7 +999,7 @@ pub fn put_received_note<'a, P, T: ShieldedOutput>( tx_ref: i64, ) -> Result { 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(); @@ -1004,21 +1010,21 @@ pub fn put_received_note<'a, P, T: ShieldedOutput>( let nf_bytes = output.nullifier().map(|nf| nf.0.to_vec()); let sql_args: &[(&str, &dyn ToSql)] = &[ - (&":account", &account), - (&":diversifier", &diversifier), - (&":value", &value), - (&":rcm", &rcm), - (&":nf", &nf_bytes), - (&":memo", &memo), - (&":is_change", &is_change), - (&":tx", &tx), - (&":output_index", &output_index), + (":account", &account), + (":diversifier", &diversifier), + (":value", &value), + (":rcm", &rcm), + (":nf", &nf_bytes), + (":memo", &memo), + (":is_change", &is_change), + (":tx", &tx), + (":output_index", &output_index), ]; // First try updating an existing received note into the database. - if stmts.stmt_update_received_note.execute_named(&sql_args)? == 0 { + if stmts.stmt_update_received_note.execute_named(sql_args)? == 0 { // It isn't there, so insert our note into the database. - stmts.stmt_insert_received_note.execute_named(&sql_args)?; + stmts.stmt_insert_received_note.execute_named(sql_args)?; Ok(NoteId::ReceivedNoteId( stmts.wallet_db.conn.last_insert_rowid(), @@ -1099,8 +1105,8 @@ 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, - encode_payment_address_p(&stmts.wallet_db.params, &to), + u32::from(account), + encode_payment_address_p(&stmts.wallet_db.params, to), ivalue, &memo.map(|m| m.as_slice()), tx_ref, @@ -1109,7 +1115,7 @@ pub fn put_sent_note<'a, P: consensus::Parameters>( ])? == 0 { // It isn't there, so insert. - insert_sent_note(stmts, tx_ref, output_index, account, &to, value, memo)? + insert_sent_note(stmts, tx_ref, output_index, account, to, value, memo)? } Ok(()) @@ -1134,8 +1140,8 @@ 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, - encode_transparent_address_p(&stmts.wallet_db.params, &to), + u32::from(account), + encode_transparent_address_p(&stmts.wallet_db.params, to), ivalue, (None::<&[u8]>), tx_ref, @@ -1144,7 +1150,7 @@ pub fn put_sent_utxo<'a, P: consensus::Parameters>( ])? == 0 { // It isn't there, so insert. - insert_sent_utxo(stmts, tx_ref, output_index, account, &to, value)? + insert_sent_utxo(stmts, tx_ref, output_index, account, to, value)? } Ok(()) @@ -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() + ); } } diff --git a/zcash_client_sqlite/src/wallet/init.rs b/zcash_client_sqlite/src/wallet/init.rs index 28e0e3443..7488bb16c 100644 --- a/zcash_client_sqlite/src/wallet/init.rs +++ b/zcash_client_sqlite/src/wallet/init.rs @@ -177,7 +177,7 @@ pub fn init_wallet_db

(wdb: &WalletDb

) -> 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( 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); } } diff --git a/zcash_client_sqlite/src/wallet/transact.rs b/zcash_client_sqlite/src/wallet/transact.rs index 34f77cd49..c6ea61916 100644 --- a/zcash_client_sqlite/src/wallet/transact.rs +++ b/zcash_client_sqlite/src/wallet/transact.rs @@ -82,7 +82,7 @@ pub fn get_spendable_sapling_notes

( // 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

( // 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(), diff --git a/zcash_primitives/CHANGELOG.md b/zcash_primitives/CHANGELOG.md index 09d24988b..d421bf51f 100644 --- a/zcash_primitives/CHANGELOG.md +++ b/zcash_primitives/CHANGELOG.md @@ -97,6 +97,8 @@ and this library adheres to Rust's notion of account values. - The `zcash_primitives::zip32::AccountId`, a type-safe wrapper for ZIP 32 account indices. +- In `zcash_primitives::transaction::components::amount`: + - `impl Sum<&Amount> for Option` ### Changed - MSRV is now 1.51.0. diff --git a/zcash_primitives/src/block.rs b/zcash_primitives/src/block.rs index 5c8cc9463..1304192d6 100644 --- a/zcash_primitives/src/block.rs +++ b/zcash_primitives/src/block.rs @@ -29,7 +29,7 @@ impl BlockHash { pub fn from_slice(bytes: &[u8]) -> Self { assert_eq!(bytes.len(), 32); let mut hash = [0; 32]; - hash.copy_from_slice(&bytes); + hash.copy_from_slice(bytes); BlockHash(hash) } } diff --git a/zcash_primitives/src/extensions/transparent.rs b/zcash_primitives/src/extensions/transparent.rs index c846959ab..a105b73f9 100644 --- a/zcash_primitives/src/extensions/transparent.rs +++ b/zcash_primitives/src/extensions/transparent.rs @@ -76,6 +76,16 @@ pub struct Witness { pub payload: T, } +impl Witness { + pub fn map_payload U>(self, f: F) -> Witness { + Witness { + extension_id: self.extension_id, + mode: self.mode, + payload: f(self.payload), + } + } +} + impl Witness { /// Produce the intermediate format for an extension-specific witness /// type. @@ -155,7 +165,7 @@ pub trait Extension { self.verify_inner( &Self::Precondition::from_payload(precondition.mode, &precondition.payload)?, &Self::Witness::from_payload(witness.mode, &witness.payload.0)?, - &context, + context, ) } } diff --git a/zcash_primitives/src/legacy/keys.rs b/zcash_primitives/src/legacy/keys.rs index 2915f10c6..6080c2e5d 100644 --- a/zcash_primitives/src/legacy/keys.rs +++ b/zcash_primitives/src/legacy/keys.rs @@ -26,10 +26,10 @@ impl AccountPrivKey { seed: &[u8], account: AccountId, ) -> Result { - ExtendedPrivKey::with_seed(&seed)? + 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) } diff --git a/zcash_primitives/src/merkle_tree/incremental.rs b/zcash_primitives/src/merkle_tree/incremental.rs index ae3535fef..3e3e3dc52 100644 --- a/zcash_primitives/src/merkle_tree/incremental.rs +++ b/zcash_primitives/src/merkle_tree/incremental.rs @@ -61,7 +61,7 @@ pub fn write_nonempty_frontier_v1( Optional::write(&mut writer, Some(b), |w, n| n.write(w))?; } } - Vector::write(&mut writer, &frontier.ommers(), |w, e| e.write(w))?; + Vector::write(&mut writer, frontier.ommers(), |w, e| e.write(w))?; Ok(()) } diff --git a/zcash_primitives/src/sapling/note_encryption.rs b/zcash_primitives/src/sapling/note_encryption.rs index fdfa070fb..da7f71882 100644 --- a/zcash_primitives/src/sapling/note_encryption.rs +++ b/zcash_primitives/src/sapling/note_encryption.rs @@ -253,7 +253,7 @@ impl Domain for SaplingDomain

{ ivk: &Self::IncomingViewingKey, plaintext: &[u8], ) -> Option<(Self::Note, Self::Recipient)> { - sapling_parse_note_plaintext_without_memo(&self, plaintext, |diversifier| { + sapling_parse_note_plaintext_without_memo(self, plaintext, |diversifier| { Some(diversifier.g_d()? * ivk.0) }) } @@ -265,7 +265,7 @@ impl Domain for SaplingDomain

{ ephemeral_key: &EphemeralKeyBytes, plaintext: &NotePlaintextBytes, ) -> Option<(Self::Note, Self::Recipient)> { - sapling_parse_note_plaintext_without_memo(&self, &plaintext.0, |diversifier| { + sapling_parse_note_plaintext_without_memo(self, &plaintext.0, |diversifier| { if (diversifier.g_d()? * esk).to_bytes() == ephemeral_key.0 { Some(*pk_d) } else { @@ -595,7 +595,7 @@ mod tests { out_ciphertext: &[u8; OUT_CIPHERTEXT_SIZE], modify_plaintext: impl Fn(&mut [u8; NOTE_PLAINTEXT_SIZE]), ) { - let ock = prf_ock(&ovk, &cv, &cmu.to_repr(), ephemeral_key); + let ock = prf_ock(ovk, cv, &cmu.to_repr(), ephemeral_key); let mut op = [0; OUT_PLAINTEXT_SIZE]; op.copy_from_slice(&out_ciphertext[..OUT_PLAINTEXT_SIZE]); diff --git a/zcash_primitives/src/sapling/pedersen_hash.rs b/zcash_primitives/src/sapling/pedersen_hash.rs index 6d92b8e3b..0e5ed26c5 100644 --- a/zcash_primitives/src/sapling/pedersen_hash.rs +++ b/zcash_primitives/src/sapling/pedersen_hash.rs @@ -87,7 +87,7 @@ where } let mut table: &[Vec] = - &generators.next().expect("we don't have enough generators"); + generators.next().expect("we don't have enough generators"); let window = PEDERSEN_HASH_EXP_WINDOW_SIZE as usize; let window_mask = (1u64 << window) - 1; diff --git a/zcash_primitives/src/sapling/prover.rs b/zcash_primitives/src/sapling/prover.rs index 1c4ae9ee1..6f6e4d889 100644 --- a/zcash_primitives/src/sapling/prover.rs +++ b/zcash_primitives/src/sapling/prover.rs @@ -106,8 +106,8 @@ pub mod mock { .commitment() .into(); - let rk = PublicKey(proof_generation_key.ak.clone().into()) - .randomize(ar, SPENDING_KEY_GENERATOR); + let rk = + PublicKey(proof_generation_key.ak.into()).randomize(ar, SPENDING_KEY_GENERATOR); Ok(([0u8; GROTH_PROOF_SIZE], cv, rk)) } diff --git a/zcash_primitives/src/sapling/redjubjub.rs b/zcash_primitives/src/sapling/redjubjub.rs index 37efd5d91..74b7b6e0d 100644 --- a/zcash_primitives/src/sapling/redjubjub.rs +++ b/zcash_primitives/src/sapling/redjubjub.rs @@ -55,6 +55,7 @@ impl Signature { } impl PrivateKey { + #[must_use] pub fn randomize(&self, alpha: jubjub::Fr) -> Self { let mut tmp = self.0; tmp.add_assign(&alpha); @@ -100,6 +101,7 @@ impl PublicKey { PublicKey((p_g * privkey.0).into()) } + #[must_use] pub fn randomize(&self, alpha: jubjub::Fr, p_g: SubgroupPoint) -> Self { PublicKey(ExtendedPoint::from(p_g * alpha) + self.0) } diff --git a/zcash_primitives/src/test_vectors/note_encryption.rs b/zcash_primitives/src/test_vectors/note_encryption.rs index 03a1b4c82..09209f29a 100644 --- a/zcash_primitives/src/test_vectors/note_encryption.rs +++ b/zcash_primitives/src/test_vectors/note_encryption.rs @@ -12,10 +12,10 @@ pub(crate) struct TestVector { pub epk: [u8; 32], pub shared_secret: [u8; 32], pub k_enc: [u8; 32], - pub p_enc: [u8; 564], + pub _p_enc: [u8; 564], pub c_enc: [u8; 580], pub ock: [u8; 32], - pub op: [u8; 64], + pub _op: [u8; 64], pub c_out: [u8; 80], } @@ -116,7 +116,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xb3, 0x3c, 0x3c, 0x32, 0x29, 0xfa, 0x0b, 0x91, 0x26, 0xf9, 0xdd, 0xdb, 0x43, 0x29, 0x66, 0x10, 0x00, 0x69, ], - p_enc: [ + _p_enc: [ 0x01, 0xf1, 0x9d, 0x9b, 0x79, 0x7e, 0x39, 0xf3, 0x37, 0x44, 0x58, 0x39, 0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x39, 0x17, 0x6d, 0xac, 0x39, 0xac, 0xe4, 0x98, 0x0e, 0xcc, 0x8d, 0x77, 0x8e, 0x89, 0x86, 0x02, 0x55, 0xec, 0x36, 0x15, 0x06, 0x00, @@ -208,7 +208,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xca, 0x3a, 0xc6, 0x42, 0x2e, 0xc4, 0xfe, 0x21, 0xe5, 0xd1, 0x53, 0x86, 0x55, 0x8e, 0x4d, 0x37, 0x79, 0x6d, ], - op: [ + _op: [ 0xdb, 0x4c, 0xd2, 0xb0, 0xaa, 0xc4, 0xf7, 0xeb, 0x8c, 0xa1, 0x31, 0xf1, 0x65, 0x67, 0xc4, 0x45, 0xa9, 0x55, 0x51, 0x26, 0xd3, 0xc2, 0x9f, 0x14, 0xe3, 0xd7, 0x76, 0xe8, 0x41, 0xae, 0x74, 0x15, 0x81, 0xc7, 0xb2, 0x17, 0x1f, 0xf4, 0x41, 0x52, 0x50, 0xca, @@ -318,7 +318,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xa7, 0x02, 0x7a, 0x49, 0x36, 0x2a, 0xa2, 0xdd, 0x3b, 0x54, 0x36, 0xd8, 0x89, 0x75, 0xe0, 0x2a, 0xd0, 0xca, ], - p_enc: [ + _p_enc: [ 0x01, 0xae, 0xf1, 0x80, 0xf6, 0xe3, 0x4e, 0x35, 0x4b, 0x88, 0x8f, 0x81, 0x00, 0xc2, 0xeb, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x47, 0x8b, 0xa0, 0xee, 0x6e, 0x1a, 0x75, 0xb6, 0x00, 0x03, 0x6f, 0x26, 0xf1, 0x8b, 0x70, 0x15, 0xab, 0x55, 0x6b, 0xed, 0xdf, 0x8b, @@ -410,7 +410,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x4c, 0xa7, 0xe4, 0xa5, 0x82, 0x1d, 0x45, 0x5f, 0x0f, 0xa8, 0x2c, 0xd5, 0x44, 0xec, 0xb4, 0x20, 0x91, 0xfa, ], - op: [ + _op: [ 0xa6, 0xb1, 0x3e, 0xa3, 0x36, 0xdd, 0xb7, 0xa6, 0x7b, 0xb0, 0x9a, 0x0e, 0x68, 0xe9, 0xd3, 0xcf, 0xb3, 0x92, 0x10, 0x83, 0x1e, 0xa3, 0xa2, 0x96, 0xba, 0x09, 0xa9, 0x22, 0x06, 0x0f, 0xd3, 0x8b, 0xad, 0x4a, 0xd6, 0x24, 0x77, 0xc2, 0xc8, 0x83, 0xc8, 0xba, @@ -520,7 +520,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xa2, 0xd8, 0x06, 0x40, 0x53, 0x0a, 0x4c, 0xa9, 0x7b, 0x82, 0x92, 0xa5, 0xa5, 0x25, 0x0f, 0x1b, 0xf2, 0x40, ], - p_enc: [ + _p_enc: [ 0x01, 0x75, 0x99, 0xf0, 0xbf, 0x9b, 0x57, 0xcd, 0x2d, 0xc2, 0x99, 0xb6, 0x00, 0xa3, 0xe1, 0x11, 0x00, 0x00, 0x00, 0x00, 0x14, 0x7c, 0xf2, 0xb5, 0x1b, 0x4c, 0x7c, 0x63, 0xcb, 0x77, 0xb9, 0x9e, 0x8b, 0x78, 0x3e, 0x5b, 0x51, 0x11, 0xdb, 0x0a, 0x7c, 0xa0, @@ -612,7 +612,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xcf, 0x0e, 0x49, 0xc3, 0x66, 0xae, 0x72, 0xc9, 0x79, 0xc4, 0x90, 0x49, 0xc9, 0x4b, 0xd3, 0xc7, 0x5c, 0xf4, ], - op: [ + _op: [ 0x66, 0x14, 0x17, 0x39, 0x51, 0x4b, 0x28, 0xf0, 0x5d, 0xef, 0x8a, 0x18, 0xee, 0xee, 0x5e, 0xed, 0x4d, 0x44, 0xc6, 0x22, 0x5c, 0x3c, 0x65, 0xd8, 0x8d, 0xd9, 0x90, 0x77, 0x08, 0x01, 0x2f, 0x5a, 0x99, 0xaa, 0x10, 0xc0, 0x57, 0x88, 0x08, 0x1c, 0x0d, 0xa7, @@ -722,7 +722,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x33, 0x0c, 0xf9, 0xc8, 0x2b, 0x1e, 0xb1, 0xfe, 0xe6, 0xa1, 0xa5, 0x54, 0x4a, 0x82, 0xc7, 0xb3, 0x16, 0x82, ], - p_enc: [ + _p_enc: [ 0x01, 0x1b, 0x81, 0x61, 0x4f, 0x1d, 0xad, 0xea, 0x0f, 0x8d, 0x0a, 0x58, 0x00, 0x84, 0xd7, 0x17, 0x00, 0x00, 0x00, 0x00, 0x34, 0xa4, 0xb2, 0xa9, 0x14, 0x4f, 0xf5, 0xea, 0x54, 0xef, 0xee, 0x87, 0xcf, 0x90, 0x1b, 0x5b, 0xed, 0x5e, 0x35, 0xd2, 0x1f, 0xbb, @@ -814,7 +814,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xf1, 0x98, 0x4c, 0x9a, 0x8b, 0x98, 0xe0, 0x6e, 0xe5, 0xd8, 0x36, 0xce, 0x0e, 0x6c, 0x89, 0xab, 0x56, 0xfd, ], - op: [ + _op: [ 0x25, 0xeb, 0x55, 0xfc, 0xcf, 0x76, 0x1f, 0xc6, 0x4e, 0x85, 0xa5, 0x88, 0xef, 0xe6, 0xea, 0xd7, 0x83, 0x2f, 0xb1, 0xf0, 0xf7, 0xa8, 0x31, 0x65, 0x89, 0x5b, 0xdf, 0xf9, 0x42, 0x92, 0x5f, 0x5c, 0xbd, 0xde, 0x13, 0x81, 0xec, 0x9f, 0xf4, 0x21, 0xca, 0xfd, @@ -924,7 +924,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x7f, 0x26, 0x1f, 0xbb, 0x2d, 0xfa, 0xd8, 0x88, 0x66, 0xb4, 0x32, 0xff, 0x0d, 0xfa, 0xee, 0xc5, 0xb2, 0xcf, ], - p_enc: [ + _p_enc: [ 0x01, 0xfc, 0xfb, 0x68, 0xa4, 0x0d, 0x4b, 0xc6, 0xa0, 0x4b, 0x09, 0xc4, 0x00, 0x65, 0xcd, 0x1d, 0x00, 0x00, 0x00, 0x00, 0xe5, 0x57, 0x85, 0x13, 0x55, 0x74, 0x7c, 0x09, 0xac, 0x59, 0x01, 0x3c, 0xbd, 0xe8, 0x59, 0x80, 0x96, 0x4e, 0xc1, 0x84, 0x4d, 0x9c, @@ -1016,7 +1016,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x3a, 0x4d, 0xb2, 0x81, 0x86, 0x37, 0x86, 0x88, 0xbe, 0xc6, 0x19, 0x56, 0x23, 0x2e, 0x42, 0xb7, 0x0a, 0xba, ], - op: [ + _op: [ 0x8b, 0x2a, 0x33, 0x7f, 0x03, 0x62, 0x2c, 0x24, 0xff, 0x38, 0x1d, 0x4c, 0x54, 0x6f, 0x69, 0x77, 0xf9, 0x05, 0x22, 0xe9, 0x2f, 0xde, 0x44, 0xc9, 0xd1, 0xbb, 0x09, 0x97, 0x14, 0xb9, 0xdb, 0x2b, 0x3d, 0xc1, 0x66, 0xd5, 0x6a, 0x1d, 0x62, 0xf5, 0xa8, 0xd7, @@ -1126,7 +1126,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x7c, 0xd0, 0x0c, 0xe0, 0xdc, 0x4b, 0x4d, 0x00, 0xcc, 0x8d, 0x8d, 0x3b, 0xa2, 0xce, 0x6e, 0xa9, 0xc2, 0x97, ], - p_enc: [ + _p_enc: [ 0x01, 0xeb, 0x51, 0x98, 0x82, 0xad, 0x1e, 0x5c, 0xc6, 0x54, 0xcd, 0x59, 0x00, 0x46, 0xc3, 0x23, 0x00, 0x00, 0x00, 0x00, 0x68, 0xf0, 0x61, 0x04, 0x60, 0x6b, 0x0c, 0x54, 0x49, 0x84, 0x5f, 0xf4, 0xc6, 0x5f, 0x73, 0xe9, 0x0f, 0x45, 0xef, 0x5a, 0x43, 0xc9, @@ -1218,7 +1218,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x5a, 0x11, 0x75, 0x86, 0xae, 0x8a, 0xdd, 0x64, 0x99, 0x7d, 0x02, 0xec, 0xb8, 0xb5, 0xcb, 0xac, 0x14, 0x87, ], - op: [ + _op: [ 0x6b, 0x27, 0xda, 0xcc, 0xb5, 0xa8, 0x20, 0x7f, 0x53, 0x2d, 0x10, 0xca, 0x23, 0x8f, 0x97, 0x86, 0x64, 0x8a, 0x11, 0xb5, 0x96, 0x6e, 0x51, 0xa2, 0xf7, 0xd8, 0x9e, 0x15, 0xd2, 0x9b, 0x8f, 0xdf, 0x4e, 0x41, 0x8c, 0x3c, 0x54, 0x3d, 0x6b, 0xf0, 0x15, 0x31, @@ -1328,7 +1328,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xba, 0x54, 0x76, 0x19, 0x8e, 0x29, 0x1d, 0xf7, 0x57, 0x8c, 0x2b, 0xef, 0x87, 0xe6, 0x4a, 0x71, 0x6a, 0xe7, ], - p_enc: [ + _p_enc: [ 0x01, 0xbe, 0xbb, 0x0f, 0xb4, 0x6b, 0x8a, 0xaf, 0xf8, 0x90, 0x40, 0xf6, 0x00, 0x27, 0xb9, 0x29, 0x00, 0x00, 0x00, 0x00, 0x49, 0xf9, 0x0b, 0x47, 0xfd, 0x52, 0xfe, 0xe7, 0xc1, 0xc8, 0x1f, 0x0d, 0xcb, 0x5b, 0x74, 0xc3, 0xfb, 0x9b, 0x3e, 0x03, 0x97, 0x6f, @@ -1420,7 +1420,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x14, 0x92, 0xd1, 0x8d, 0x5c, 0x85, 0x3c, 0x8f, 0x2f, 0x0c, 0xd5, 0xd1, 0x9d, 0x6d, 0x34, 0xcf, 0x7c, 0x2d, ], - op: [ + _op: [ 0xd1, 0x1d, 0xa0, 0x1f, 0x0b, 0x43, 0xbd, 0xd5, 0x28, 0x8d, 0x32, 0x38, 0x5b, 0x87, 0x71, 0xd2, 0x23, 0x49, 0x3c, 0x69, 0x80, 0x25, 0x44, 0x04, 0x3f, 0x77, 0xcf, 0x1d, 0x71, 0xc1, 0xcb, 0x8c, 0x6d, 0xa9, 0x45, 0xd3, 0x03, 0x81, 0xc2, 0xee, 0xd2, 0xb8, @@ -1530,7 +1530,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x39, 0x97, 0x42, 0xa9, 0x29, 0xbb, 0x23, 0x10, 0x0a, 0x7c, 0x51, 0xed, 0x32, 0xf9, 0xcb, 0x45, 0x96, 0xc6, ], - p_enc: [ + _p_enc: [ 0x01, 0xad, 0x6e, 0x2e, 0x18, 0x5a, 0x31, 0x00, 0xe3, 0xa6, 0xa8, 0xb3, 0x00, 0x08, 0xaf, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x51, 0x65, 0xaf, 0xf2, 0x2d, 0xd4, 0xed, 0x56, 0xb4, 0xd8, 0x1d, 0x1f, 0x17, 0x1c, 0xc3, 0xd6, 0x43, 0x2f, 0xed, 0x1b, 0xeb, 0xf2, @@ -1622,7 +1622,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x83, 0xa9, 0x27, 0x7c, 0x61, 0x82, 0xa8, 0x08, 0xcc, 0x53, 0xa1, 0xbe, 0xdd, 0xd2, 0x03, 0x68, 0xb1, 0x0a, ], - op: [ + _op: [ 0x32, 0xcb, 0x28, 0x06, 0xb8, 0x82, 0xf1, 0x36, 0x8b, 0x0d, 0x4a, 0x89, 0x8f, 0x72, 0xc4, 0xc8, 0xf7, 0x28, 0x13, 0x2c, 0xc1, 0x24, 0x56, 0x94, 0x6e, 0x7f, 0x4c, 0xb0, 0xfb, 0x05, 0x8d, 0xa9, 0xab, 0x2a, 0xff, 0x03, 0x32, 0xd5, 0x43, 0xfd, 0x1d, 0x80, @@ -1732,7 +1732,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xa6, 0xb9, 0xca, 0xbe, 0xfd, 0xba, 0x9e, 0x7a, 0x74, 0xf5, 0xba, 0x2f, 0x81, 0xb8, 0x71, 0x40, 0x1f, 0x08, ], - p_enc: [ + _p_enc: [ 0x01, 0x21, 0xc9, 0x0e, 0x1c, 0x65, 0x8b, 0x3e, 0xfe, 0x86, 0xaf, 0x58, 0x00, 0xe9, 0xa4, 0x35, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x3e, 0x56, 0x44, 0x9d, 0xc8, 0x63, 0x54, 0xd3, 0x3b, 0x02, 0x5e, 0xf2, 0x79, 0x34, 0x60, 0xbc, 0xb1, 0x69, 0xf3, 0x32, 0x4e, @@ -1824,7 +1824,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xa5, 0xf3, 0x58, 0x05, 0xba, 0x78, 0x5a, 0x2c, 0x92, 0xa9, 0xaa, 0x62, 0x32, 0xb0, 0x55, 0x1c, 0xf3, 0xf4, ], - op: [ + _op: [ 0x9e, 0x64, 0x17, 0x4b, 0x4a, 0xb9, 0x81, 0x40, 0x5c, 0x32, 0x3b, 0x5e, 0x12, 0x47, 0x59, 0x45, 0xa4, 0x6d, 0x4f, 0xed, 0xf8, 0x06, 0x08, 0x28, 0x04, 0x1c, 0xd2, 0x0e, 0x62, 0xfd, 0x2c, 0xef, 0xa5, 0x3d, 0x19, 0xf5, 0x69, 0x45, 0x95, 0xd5, 0xae, 0x63, @@ -1934,7 +1934,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0xc9, 0xc9, 0x60, 0xad, 0x7c, 0xcc, 0x59, 0x77, 0x43, 0x74, 0x4c, 0x18, 0xc9, 0xc2, 0xa5, 0x62, 0xf6, 0x3a, ], - p_enc: [ + _p_enc: [ 0x01, 0x23, 0x3c, 0x4a, 0xb8, 0x86, 0xa5, 0x5e, 0x3b, 0xa3, 0x74, 0xc0, 0x00, 0xca, 0x9a, 0x3b, 0x00, 0x00, 0x00, 0x00, 0x6e, 0xbb, 0xed, 0x74, 0x36, 0x19, 0xa2, 0x56, 0xf9, 0xad, 0x2e, 0x85, 0x88, 0x0c, 0xfa, 0xa9, 0x09, 0x8a, 0x5f, 0xdb, 0x16, 0x29, @@ -2026,7 +2026,7 @@ pub(crate) fn make_test_vectors() -> Vec { 0x8c, 0xca, 0x55, 0x71, 0xee, 0xcc, 0x69, 0xb7, 0x22, 0xa0, 0xa3, 0xb8, 0x67, 0x50, 0x72, 0x92, 0x99, 0xa0, ], - op: [ + _op: [ 0xb6, 0x8e, 0x9e, 0xe0, 0xc0, 0x67, 0x8d, 0x7b, 0x30, 0x36, 0x93, 0x1c, 0x83, 0x1a, 0x25, 0x25, 0x5f, 0x7e, 0xe4, 0x87, 0x38, 0x5a, 0x30, 0x31, 0x6e, 0x15, 0xf6, 0x48, 0x2b, 0x87, 0x4f, 0xda, 0x29, 0x95, 0x89, 0x80, 0x69, 0x4f, 0x7f, 0x67, 0x08, 0x09, diff --git a/zcash_primitives/src/transaction/builder.rs b/zcash_primitives/src/transaction/builder.rs index 4d6fb4ae0..81abaffb7 100644 --- a/zcash_primitives/src/transaction/builder.rs +++ b/zcash_primitives/src/transaction/builder.rs @@ -1,6 +1,5 @@ //! Structs for building transactions. -use std::array; use std::error; use std::fmt; use std::sync::mpsc::Sender; @@ -267,7 +266,7 @@ impl<'a, P: consensus::Parameters, R: RngCore> Builder<'a, P, R> { .ok_or(Error::InvalidAmount)?, ]; - array::IntoIter::new(value_balances) + IntoIterator::into_iter(&value_balances) .sum::>() .ok_or(Error::InvalidAmount) } diff --git a/zcash_primitives/src/transaction/components/amount.rs b/zcash_primitives/src/transaction/components/amount.rs index 7d86f9121..5d4edd8a1 100644 --- a/zcash_primitives/src/transaction/components/amount.rs +++ b/zcash_primitives/src/transaction/components/amount.rs @@ -200,6 +200,12 @@ impl Sum for Option { } } +impl<'a> Sum<&'a Amount> for Option { + fn sum>(iter: I) -> Self { + iter.fold(Some(Amount::zero()), |acc, a| acc? + *a) + } +} + impl Neg for Amount { type Output = Self; diff --git a/zcash_primitives/src/transaction/components/orchard.rs b/zcash_primitives/src/transaction/components/orchard.rs index 0ee878854..4d30b64dd 100644 --- a/zcash_primitives/src/transaction/components/orchard.rs +++ b/zcash_primitives/src/transaction/components/orchard.rs @@ -28,6 +28,11 @@ impl Authorization for Unauthorized { type SpendAuth = (); } +pub trait MapAuth { + fn map_spend_auth(&self, s: A::SpendAuth) -> B::SpendAuth; + fn map_authorization(&self, a: A) -> B; +} + /// Reads an [`orchard::Bundle`] from a v5 transaction format. pub fn read_v5_bundle( mut reader: R, @@ -236,11 +241,11 @@ pub fn write_action_without_auth( mut writer: W, act: &Action<::SpendAuth>, ) -> io::Result<()> { - write_value_commitment(&mut writer, &act.cv_net())?; - write_nullifier(&mut writer, &act.nullifier())?; - write_verification_key(&mut writer, &act.rk())?; - write_cmx(&mut writer, &act.cmx())?; - write_note_ciphertext(&mut writer, &act.encrypted_note())?; + write_value_commitment(&mut writer, act.cv_net())?; + write_nullifier(&mut writer, act.nullifier())?; + write_verification_key(&mut writer, act.rk())?; + write_cmx(&mut writer, act.cmx())?; + write_note_ciphertext(&mut writer, act.encrypted_note())?; Ok(()) } diff --git a/zcash_primitives/src/transaction/components/sapling.rs b/zcash_primitives/src/transaction/components/sapling.rs index 2352f6723..8413b897b 100644 --- a/zcash_primitives/src/transaction/components/sapling.rs +++ b/zcash_primitives/src/transaction/components/sapling.rs @@ -47,6 +47,12 @@ impl Authorization for Authorized { type AuthSig = redjubjub::Signature; } +pub trait MapAuth { + fn map_proof(&self, p: A::Proof) -> B::Proof; + fn map_auth_sig(&self, s: A::AuthSig) -> B::AuthSig; + fn map_authorization(&self, a: A) -> B; +} + #[derive(Debug, Clone)] pub struct Bundle { pub shielded_spends: Vec>, @@ -55,6 +61,39 @@ pub struct Bundle { pub authorization: A, } +impl Bundle { + pub fn map_authorization>(self, f: F) -> Bundle { + Bundle { + shielded_spends: self + .shielded_spends + .into_iter() + .map(|d| SpendDescription { + cv: d.cv, + anchor: d.anchor, + nullifier: d.nullifier, + rk: d.rk, + zkproof: f.map_proof(d.zkproof), + spend_auth_sig: f.map_auth_sig(d.spend_auth_sig), + }) + .collect(), + shielded_outputs: self + .shielded_outputs + .into_iter() + .map(|o| OutputDescription { + cv: o.cv, + cmu: o.cmu, + ephemeral_key: o.ephemeral_key, + enc_ciphertext: o.enc_ciphertext, + out_ciphertext: o.out_ciphertext, + zkproof: f.map_proof(o.zkproof), + }) + .collect(), + value_balance: self.value_balance, + authorization: f.map_authorization(self.authorization), + } + } +} + #[derive(Clone)] pub struct SpendDescription { pub cv: jubjub::ExtendedPoint, diff --git a/zcash_primitives/src/transaction/components/sprout.rs b/zcash_primitives/src/transaction/components/sprout.rs index 79f563330..0c11b0e04 100644 --- a/zcash_primitives/src/transaction/components/sprout.rs +++ b/zcash_primitives/src/transaction/components/sprout.rs @@ -18,9 +18,9 @@ pub struct Bundle { } #[derive(Clone)] +#[allow(clippy::upper_case_acronyms)] pub(crate) enum SproutProof { Groth([u8; GROTH_PROOF_SIZE]), - #[allow(clippy::upper_case_acronyms)] PHGR([u8; PHGR_PROOF_SIZE]), } diff --git a/zcash_primitives/src/transaction/components/transparent.rs b/zcash_primitives/src/transaction/components/transparent.rs index ad290e70e..f5a132f64 100644 --- a/zcash_primitives/src/transaction/components/transparent.rs +++ b/zcash_primitives/src/transaction/components/transparent.rs @@ -22,6 +22,11 @@ impl Authorization for Authorized { type ScriptSig = Script; } +pub trait MapAuth { + fn map_script_sig(&self, s: A::ScriptSig) -> B::ScriptSig; + fn map_authorization(&self, s: A) -> B; +} + #[derive(Debug, Clone, PartialEq)] pub struct Bundle { pub vin: Vec>, @@ -41,6 +46,22 @@ impl Bundle { // return (vin.size() == 1 && vin[0].prevout.IsNull()); matches!(&self.vin[..], [input] if input.prevout.is_null()) } + + pub fn map_authorization>(self, f: F) -> Bundle { + Bundle { + vin: self + .vin + .into_iter() + .map(|txin| TxIn { + prevout: txin.prevout, + script_sig: f.map_script_sig(txin.script_sig), + sequence: txin.sequence, + }) + .collect(), + vout: self.vout, + authorization: f.map_authorization(self.authorization), + } + } } #[derive(Clone, Debug, PartialEq)] diff --git a/zcash_primitives/src/transaction/components/transparent/builder.rs b/zcash_primitives/src/transaction/components/transparent/builder.rs index e383f8445..ec73f0fee 100644 --- a/zcash_primitives/src/transaction/components/transparent/builder.rs +++ b/zcash_primitives/src/transaction/components/transparent/builder.rs @@ -222,7 +222,7 @@ impl Bundle { #[cfg(feature = "transparent-inputs")] txid_parts_cache: &TxDigests, ) -> Bundle { #[cfg(feature = "transparent-inputs")] - let script_sigs: Vec