Skip to content

Commit

Permalink
Fix migrate_db_chrysalis_to_stardust() with ledger nano signer type (#…
Browse files Browse the repository at this point in the history
…1342)

* Fix migrate_db_chrysalis_to_stardust() with ledger nano signer type

* Address review comments, format

* Remove obsolete code
  • Loading branch information
Thoralf-M authored Sep 28, 2023
1 parent c3caf02 commit 488aaf4
Show file tree
Hide file tree
Showing 11 changed files with 3,830 additions and 3,884 deletions.
6 changes: 6 additions & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## 1.0.13 - 2023-09-28

### Fixed

- `migrateDbChrysalisToStardust()` when ledger nano was used as signer type;

## 1.0.12 - 2023-09-25

### Changed
Expand Down
2 changes: 1 addition & 1 deletion bindings/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iota/sdk",
"version": "1.0.12",
"version": "1.0.13",
"description": "Node.js binding to the IOTA SDK library",
"main": "out/index.js",
"types": "out/index.d.ts",
Expand Down
7,624 changes: 3,757 additions & 3,867 deletions bindings/nodejs/yarn.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Format of `milestoneIndex` query parameter of `ClientInner::event_status`;
- Don't error if custom remainder address is provided with ledger nano;
- `migrate_db_chrysalis_to_stardust()` when ledger nano was used as signer type;

## 1.0.3 - 2023-09-07

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async fn migrate_snapshot_from_chrysalis_to_stardust(
)
.await?;
stronghold_adapter
.set_bytes(SECRET_MANAGER_KEY, secret_manager_dto.as_bytes())
.set_bytes(SECRET_MANAGER_KEY, secret_manager_dto.to_string().as_bytes())
.await?;
}

Expand Down
20 changes: 9 additions & 11 deletions sdk/src/wallet/migration/chrysalis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ pub async fn migrate_db_chrysalis_to_stardust(
&serde_json::from_str::<Value>(&format!("{{ \"coinType\": {IOTA_COIN_TYPE}}}"))?,
)
.await?;
stardust_storage
.set(SECRET_MANAGER_KEY, &serde_json::from_str::<Value>(&secret_manager_dto)?)
.await?;
stardust_storage.set(SECRET_MANAGER_KEY, &secret_manager_dto).await?;
}

// set db migration version
Expand All @@ -146,9 +144,9 @@ pub(crate) fn migrate_from_chrysalis_data(
storage_path: &Path,
// in stronghold the keys are hashed first
stronghold: bool,
) -> Result<(Vec<AccountDetailsDto>, Option<String>)> {
) -> Result<(Vec<AccountDetailsDto>, Option<Value>)> {
let mut new_accounts: Vec<AccountDetailsDto> = Vec::new();
let mut secret_manager_dto: Option<String> = None;
let mut secret_manager_dto: Option<Value> = None;

let account_indexation_key = to_chrysalis_key(b"iota-wallet-account-indexation", stronghold);
if let Some(account_indexation) = chrysalis_data.get(&account_indexation_key) {
Expand All @@ -163,12 +161,12 @@ pub(crate) fn migrate_from_chrysalis_data(
let account_data = serde_json::from_str::<serde_json::Value>(account_data)?;
if secret_manager_dto.is_none() {
let dto = match &account_data["signerType"]["type"].as_str() {
Some("Stronghold") => format!(
r#"{{"Stronghold": {{"password": null, "timeout": null, "snapshotPath": "{}/wallet.stronghold"}} }}"#,
storage_path.to_string_lossy()
),
Some("LedgerNano") => r#"{{"LedgerNano": false }}"#.into(),
Some("LedgerNanoSimulator") => r#"{{"LedgerNano": true }}"#.into(),
Some("Stronghold") => serde_json::json!({"Stronghold": {"password": null, "timeout": null,
"snapshotPath": format!("{}/wallet.stronghold", storage_path.to_string_lossy())
}
}),
Some("LedgerNano") => serde_json::json!({"LedgerNano": false }),
Some("LedgerNanoSimulator") => serde_json::json!({"LedgerNano": true }),
_ => return Err(Error::Migration("Missing signerType".into())),
};
secret_manager_dto = Some(dto);
Expand Down
57 changes: 53 additions & 4 deletions sdk/tests/wallet/chrysalis_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use zeroize::Zeroizing;

use crate::wallet::common::{setup, tear_down};

const CHRYSALIS_ACCOUNT_INDEXATION_KEY: &str = "iota-wallet-account-indexation";

#[tokio::test]
async fn migrate_chrysalis_db() -> Result<()> {
iota_stronghold::engine::snapshot::try_set_encrypt_work_factor(0).unwrap();
Expand Down Expand Up @@ -71,7 +73,7 @@ async fn migrate_chrysalis_db() -> Result<()> {
);

let chrysalis_data = wallet.get_chrysalis_data().await?.unwrap();
let accounts_indexation = chrysalis_data.get("iota-wallet-account-indexation").unwrap();
let accounts_indexation = chrysalis_data.get(CHRYSALIS_ACCOUNT_INDEXATION_KEY).unwrap();
assert_eq!(
accounts_indexation,
"[{\"key\":\"wallet-account://b5e020ec9a67eb7ce07be742116bd27ae722e1159098c89dd7e50d972a7b13fc\"},{\"key\":\"wallet-account://e59975e320b8433916b4946bb1e21107e8d3f36d1e587782cbd35acf59c90d1a\"}]"
Expand Down Expand Up @@ -161,7 +163,7 @@ async fn migrate_chrysalis_db_encrypted() -> Result<()> {
);

let chrysalis_data = wallet.get_chrysalis_data().await?.unwrap();
let accounts_indexation = chrysalis_data.get("iota-wallet-account-indexation").unwrap();
let accounts_indexation = chrysalis_data.get(CHRYSALIS_ACCOUNT_INDEXATION_KEY).unwrap();
assert_eq!(
accounts_indexation,
"[{\"key\":\"wallet-account://b5e020ec9a67eb7ce07be742116bd27ae722e1159098c89dd7e50d972a7b13fc\"},{\"key\":\"wallet-account://e59975e320b8433916b4946bb1e21107e8d3f36d1e587782cbd35acf59c90d1a\"}]"
Expand Down Expand Up @@ -247,7 +249,7 @@ async fn migrate_chrysalis_db_encrypted_encrypt_new() -> Result<()> {
);

let chrysalis_data = wallet.get_chrysalis_data().await?.unwrap();
let accounts_indexation = chrysalis_data.get("iota-wallet-account-indexation").unwrap();
let accounts_indexation = chrysalis_data.get(CHRYSALIS_ACCOUNT_INDEXATION_KEY).unwrap();
assert_eq!(
accounts_indexation,
"[{\"key\":\"wallet-account://b5e020ec9a67eb7ce07be742116bd27ae722e1159098c89dd7e50d972a7b13fc\"},{\"key\":\"wallet-account://e59975e320b8433916b4946bb1e21107e8d3f36d1e587782cbd35acf59c90d1a\"}]"
Expand Down Expand Up @@ -324,7 +326,7 @@ async fn migrate_chrysalis_stronghold() -> Result<()> {
);

let chrysalis_data = wallet.get_chrysalis_data().await?.unwrap();
// "iota-wallet-account-indexation"
// CHRYSALIS_ACCOUNT_INDEXATION_KEY
let accounts_indexation = chrysalis_data
.get("0xddc058ad3b93b5a575b0051aafbc8ff17ad0415d7aa1c54d")
.unwrap();
Expand Down Expand Up @@ -384,3 +386,50 @@ fn copy_folder(src: impl AsRef<Path>, dest: impl AsRef<Path>) -> io::Result<()>
}
Ok(())
}

#[cfg(feature = "ledger_nano")]
#[tokio::test]
async fn migrate_chrysalis_db_ledger() -> Result<()> {
let storage_path = "migrate_chrysalis_db_ledger/db";
setup(storage_path)?;
// Copy db so the original doesn't get modified
copy_folder("./tests/wallet/fixtures/chrysalis-db-ledger/db", storage_path).unwrap();

migrate_db_chrysalis_to_stardust("migrate_chrysalis_db_ledger", None, None).await?;

let client_options = ClientOptions::new();
let wallet = Wallet::builder()
.with_storage_path("migrate_chrysalis_db_ledger")
.with_client_options(client_options)
.finish()
.await?;

let accounts = wallet.get_accounts().await?;
assert_eq!(accounts.len(), 1);
assert_eq!(accounts[0].alias().await, "ledger");

let alice_acc_details = accounts[0].details().await;
assert_eq!(alice_acc_details.public_addresses().len(), 4);
// mnemonic: glory promote mansion idle axis finger extra february uncover one trip resource lawn turtle enact
// monster seven myth punch hobby comfort wild raise skin
assert_eq!(
alice_acc_details.public_addresses()[0].address().try_to_bech32("rms")?,
"rms1qqdnv60ryxynaeyu8paq3lp9rkll7d7d92vpumz88fdj4l0pn5mruskth6z"
);
assert_eq!(alice_acc_details.internal_addresses().len(), 1);
assert_eq!(
alice_acc_details.internal_addresses()[0]
.address()
.try_to_bech32("rms")?,
"rms1qzev23h8qtdfjzzx4jqrdfaw2nnnwu2m4hhu2tkdmp2wrt6y8qwq22963tv"
);

let chrysalis_data = wallet.get_chrysalis_data().await?.unwrap();
let accounts_indexation = chrysalis_data.get(CHRYSALIS_ACCOUNT_INDEXATION_KEY).unwrap();
assert_eq!(
accounts_indexation,
"[{\"key\":\"wallet-account://2b9bd865368556d58f9d5a9fd44c30205f1fc80b09cde1dcb9b3a37748210854\"}]"
);

tear_down("migrate_chrysalis_db_ledger")
}
Binary file not shown.
1 change: 1 addition & 0 deletions sdk/tests/wallet/fixtures/chrysalis-db-ledger/db/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000010
1 change: 1 addition & 0 deletions sdk/tests/wallet/fixtures/chrysalis-db-ledger/db/IDENTITY
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c68fb305-90e8-4a23-ab09-793fd35b2dad
Binary file not shown.

0 comments on commit 488aaf4

Please sign in to comment.