From cb349f897a6b0c6c6d71e0301c7c789df3535a56 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Wed, 2 Oct 2024 15:48:40 -0600 Subject: [PATCH] zcash_client_sqlite: Add unstable conversions between `AccountId` and `u32` This is necessary for third parties using the `zcash_client_sqlite` crate directly to be able to refer to individual account IDs without having to always use FVKs to look them up. --- zcash_client_sqlite/CHANGELOG.md | 4 ++++ zcash_client_sqlite/src/lib.rs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/zcash_client_sqlite/CHANGELOG.md b/zcash_client_sqlite/CHANGELOG.md index 6a646adfc3..88350e2c0f 100644 --- a/zcash_client_sqlite/CHANGELOG.md +++ b/zcash_client_sqlite/CHANGELOG.md @@ -7,6 +7,10 @@ and this library adheres to Rust's notion of ## [Unreleased] +### Added +- Exposed `AccountId::from_u32` and `AccountId::as_u32` conversions under the + `unstable` feature flag. + ## [0.12.0] - 2024-10-04 ### Added diff --git a/zcash_client_sqlite/src/lib.rs b/zcash_client_sqlite/src/lib.rs index 4e297d3342..cdc7dc57d5 100644 --- a/zcash_client_sqlite/src/lib.rs +++ b/zcash_client_sqlite/src/lib.rs @@ -164,6 +164,26 @@ pub(crate) const DEFAULT_UA_REQUEST: UnifiedAddressRequest = #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)] pub struct AccountId(u32); +impl AccountId { + /// Constructs an `AccountId` from a bare `u32` value. The resulting identifier is not + /// guaranteed to correspond to any account stored in the database. + #[cfg(feature = "unstable")] + pub fn from_u32(value: u32) -> Self { + AccountId(value) + } + + /// Unwraps a raw `accounts` table primary key value from its typesafe wrapper. + /// + /// Note that account identifiers are not guaranteed to be stable; if a wallet is restored from + /// seed, the account identifiers of the restored wallet are not likely to correspond to the + /// identifiers for the same accounts in another wallet created or restored from the same seed. + /// These unwrapped identifier values should therefore be treated as ephemeral. + #[cfg(feature = "unstable")] + pub fn as_u32(&self) -> u32 { + self.0 + } +} + impl ConditionallySelectable for AccountId { fn conditional_select(a: &Self, b: &Self, choice: subtle::Choice) -> Self { AccountId(ConditionallySelectable::conditional_select(