Skip to content

Commit

Permalink
Add migrations to support using UFVKs instead of Sapling extfvks.
Browse files Browse the repository at this point in the history
Fixes #594
  • Loading branch information
nuttycom committed Aug 18, 2022
1 parent 7c5b320 commit cdfaa57
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 69 deletions.
10 changes: 9 additions & 1 deletion zcash_client_sqlite/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ and this library adheres to Rust's notion of

### Changed
- Various **BREAKING CHANGES** have been made to the database tables. These will
require migrations, which may need to be performed in multiple steps.
require migrations, which may need to be performed in multiple steps. Migrations
will now be automatically performed for any user using
`zcash_client_sqlite::wallet::init_wallet_db`and it is recommended to use this
method to maintain the state of the database going forward.
- The `extfvk` column in the `accounts` table has been replaced by a `ufvk`
column. Values for this column should be derived from the wallet's seed and
the account number; the Sapling component of the resulting Unified Full
Expand Down Expand Up @@ -48,6 +51,11 @@ and this library adheres to Rust's notion of
method to be used in contexts where a transaction has just been
constructed, rather than only in the case that a transaction has
been decrypted after being retrieved from the network.
- `zcash_client_sqlite::wallet::init_wallet_db` has been modified to
take the wallet seed as an argument so that it can correctly perform
migrations that require re-deriving key material. In particular for
this upgrade, the seed is used to derive UFVKs to replace the currently
stored Sapling extfvks as part of the migration process.

### Removed
- `zcash_client_sqlite::wallet`:
Expand Down
14 changes: 7 additions & 7 deletions zcash_client_sqlite/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -177,7 +177,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -247,7 +247,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -317,7 +317,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -386,7 +386,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -441,7 +441,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down Expand Up @@ -493,7 +493,7 @@ mod tests {

let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
let (dfvk, _taddr) = init_test_accounts_table(&db_data);
Expand Down
22 changes: 10 additions & 12 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,15 @@ pub(crate) fn get_unified_full_viewing_keys<P: consensus::Parameters>(
.conn
.prepare("SELECT account, ufvk FROM accounts ORDER BY account ASC")?;

let rows = stmt_fetch_accounts
.query_map(NO_PARAMS, |row| {
let acct: u32 = row.get(0)?;
let account = AccountId::from(acct);
let ufvk_str: String = row.get(1)?;
let ufvk = UnifiedFullViewingKey::decode(&wdb.params, &ufvk_str)
.map_err(SqliteClientError::CorruptedData);

Ok((account, ufvk))
})
.map_err(SqliteClientError::from)?;
let rows = stmt_fetch_accounts.query_map(NO_PARAMS, |row| {
let acct: u32 = row.get(0)?;
let account = AccountId::from(acct);
let ufvk_str: String = row.get(1)?;
let ufvk = UnifiedFullViewingKey::decode(&wdb.params, &ufvk_str)
.map_err(SqliteClientError::CorruptedData);

Ok((account, ufvk))
})?;

let mut res: HashMap<AccountId, UnifiedFullViewingKey> = HashMap::new();
for row in rows {
Expand Down Expand Up @@ -1243,7 +1241,7 @@ mod tests {
fn empty_database_has_no_balance() {
let data_file = NamedTempFile::new().unwrap();
let mut db_data = WalletDb::for_path(data_file.path(), tests::network()).unwrap();
init_wallet_db(&mut db_data).unwrap();
init_wallet_db(&mut db_data, vec![]).unwrap();

// Add an account to the wallet
tests::init_test_accounts_table(&db_data);
Expand Down

0 comments on commit cdfaa57

Please sign in to comment.