Skip to content

Commit

Permalink
Migrate storage (#343)
Browse files Browse the repository at this point in the history
* migrate storage

Signed-off-by: Charles Ferrell <[email protected]>

* fix

Signed-off-by: Charles Ferrell <[email protected]>

* use expect and order Cargo.toml

Signed-off-by: Charles Ferrell <[email protected]>

* add changelog and fix lint

Signed-off-by: Charles Ferrell <[email protected]>

---------

Signed-off-by: Charles Ferrell <[email protected]>
  • Loading branch information
ferrell-code authored Mar 9, 2023
1 parent e0c2483 commit f34cf22
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added

### Changed
- [\#343](https://github.com/Manta-Network/manta-signer/pull/343) Adds ability to load mnemonic from old state version.
- [\#338](https://github.com/Manta-Network/manta-signer/pull/338) Change start button to continue in View zkAddress.
- [\#332](https://github.com/Manta-Network/manta-signer/pull/332) Add sink account validation

Expand Down
112 changes: 100 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ disable-restart = []
[dependencies]
argon2 = { version = "0.4.1", default-features = false, features = ["alloc", "password-hash"] }
async-std = { version = "1.11.0", default-features = false, features = ["attributes", "tokio1"] }
bincode = "1.3.3"
chrono = { version = "0.4.19", default-features = false, features = ["clock"] }
derivative = { version = "2.2.0", default-features = false, features = ["use_core"] }
dirs-next = { version = "2.0.0", default-features = false }
Expand All @@ -50,6 +51,7 @@ manta-pay = { git = "https://github.com/manta-network/manta-rs", tag = "v0.5.12"
manta-util = { git = "https://github.com/manta-network/manta-rs", tag = "v0.5.12", default-features = false }
parking_lot = { version = "0.12.1", default-features = false }
password-hash = { version = "0.4.2", default-features = false, features = ["alloc"] }
previous-state-manta-pay = { package = "manta-pay", git = "https://github.com/manta-network/manta-rs", tag = "v0.5.10", default-features = false, features = ["bs58", "groth16", "serde", "wallet", "std", "parameters"] }
reqwest = { version = "0.11.14", default-features = false, features = ["json"] }
secrecy = { version = "0.8.0", default-features = false, features = ["alloc"] }
serde_json = { version = "1.0.91", default-features = false }
Expand Down
48 changes: 43 additions & 5 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ use manta_pay::{
AssetMetadata, TokenType,
},
};

/// previous version of manta-pay state. Should use versioning system for SignerState
use previous_state_manta_pay::signer::base::SignerState as OldSignerState;

use manta_util::{from_variant, serde::Serialize};
use parking_lot::Mutex;
use std::{
Expand Down Expand Up @@ -436,7 +440,7 @@ where
};
let existing_signer = Signer::from_parts(
parameters.clone(),
Self::load_state(existing_state_path, password_hash)
Self::load_state(existing_state_path, password_hash, &parameters)
.await
.expect("Unable to get dolphin state")?,
);
Expand Down Expand Up @@ -474,7 +478,7 @@ where
.expect("Unable to recreate signer instance from existing mnemonic.");
Ok(Some(state))
} else {
Self::load_state(data_path, password_hash).await
Self::load_state(data_path, password_hash, parameters).await
}
}

Expand Down Expand Up @@ -557,17 +561,51 @@ where
async fn load_state(
data_path: &Path,
password_hash: &PasswordHash<Argon2>,
parameters: &SignerParameters,
) -> Result<Option<SignerState>> {
info!("loading signer state from disk")?;
let data_path = data_path.to_owned();
let data_path_buf = data_path.to_owned();
let password_hash_bytes = password_hash.as_bytes();

let state_result = task::spawn_blocking(move || {
File::load::<_, SignerState>(&data_path_buf, &password_hash_bytes)
})
.await?;

if let Ok(correct_state) = state_result {
Ok(Some(correct_state))
} else {
// fallback to try from old state version
Self::new_state_from_old_state(data_path, password_hash, parameters).await
}
}

/// Attempts to create new signer state from the old signer state version
#[inline]
async fn new_state_from_old_state(
data_path: &Path,
password_hash: &PasswordHash<Argon2>,
parameters: &SignerParameters,
) -> Result<Option<SignerState>> {
info!("loading mnemonic from old state")?;
let data_path_buf = data_path.to_owned();
let password_hash_bytes = password_hash.as_bytes();

if let Ok(state) = task::spawn_blocking(move || {
File::load::<_, SignerState>(&data_path, &password_hash_bytes)
File::load::<_, OldSignerState>(&data_path_buf, &password_hash_bytes)
})
.await?
{
Ok(Some(state))
let mnemonic = state.accounts().keys().expose_mnemonic().clone();

let encoded: Vec<u8> = bincode::serialize(&mnemonic).expect("encoding mnenomic failed");
let new_mnemonic: Mnemonic =
bincode::deserialize(&encoded[..]).expect("decoding mnenomic failed");

let new_state =
Self::create_state(data_path, password_hash, new_mnemonic, parameters).await?;

Ok(Some(new_state))
} else {
Ok(None)
}
Expand Down

1 comment on commit f34cf22

@vercel
Copy link

@vercel vercel bot commented on f34cf22 Mar 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.