diff --git a/.changes/stronghold.md b/.changes/stronghold.md new file mode 100644 index 0000000000..31deb432df --- /dev/null +++ b/.changes/stronghold.md @@ -0,0 +1,5 @@ +--- +"nodejs-binding": patch +--- + +Added migrateStrongholdSnapshotV2ToV3(); \ No newline at end of file diff --git a/bindings/nodejs/examples/binding_examples/migrate-stronghold-snapshot-v2-to-v3.js b/bindings/nodejs/examples/binding_examples/migrate-stronghold-snapshot-v2-to-v3.js new file mode 100644 index 0000000000..477e01799c --- /dev/null +++ b/bindings/nodejs/examples/binding_examples/migrate-stronghold-snapshot-v2-to-v3.js @@ -0,0 +1,33 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +const { AccountManager } = require('@iota/wallet'); +require('dotenv').config({ path: '.env' }); + +const v2Path = '../../../../tests/fixtures/v2_with_backup.stronghold'; +const v3Path = './migration-database/wallet.stronghold'; + +// Run with command: +// node run-example wallet/migrate-stronghold-snapshot-v2-to-v3.js + +async function run() { + const manager = new AccountManager({ + storagePath: './migration-database', + }); + + manager.migrateStrongholdSnapshotV2ToV3( + v2Path, + 'current_password', + 'wallet.rs', + 100, + v3Path, + // Optional, can also stay the same password + 'new_password', + ); + console.log('Migrated stronghold'); + + // This shouldn't fail anymore as snapshot has been migrated. + manager.setStrongholdPassword('new_password'); +} + +run().then(() => process.exit()); diff --git a/bindings/nodejs/src/classes/account_manager/mod.rs b/bindings/nodejs/src/classes/account_manager/mod.rs index 8984505f1c..247237eacf 100644 --- a/bindings/nodejs/src/classes/account_manager/mod.rs +++ b/bindings/nodejs/src/classes/account_manager/mod.rs @@ -725,13 +725,9 @@ pub fn migrate_stronghold_snapshot_v2_to_v3(mut cx: FunctionContext) -> JsResult new_path.as_ref(), new_password.as_deref(), ) - .unwrap(); - // .or_else(|e| { - // cx.throw_error( - // serde_json::to_string(&Response::Error(e.into())) - // .expect("the response is generated manually, so unwrap is safe."), - // ) - // })?; + .or_else(|e| { + cx.throw_error(serde_json::to_string(&e).expect("the response is generated manually, so unwrap is safe.")) + })?; Ok(cx.undefined()) } diff --git a/examples/migrate_stronghold_snapshot_v2_to_v3.rs b/examples/migrate_stronghold_snapshot_v2_to_v3.rs index c7f94c8bbc..1414f51094 100644 --- a/examples/migrate_stronghold_snapshot_v2_to_v3.rs +++ b/examples/migrate_stronghold_snapshot_v2_to_v3.rs @@ -39,16 +39,5 @@ async fn main() -> Result<(), Error> { // This shouldn't fail anymore as snapshot has been migrated. manager.set_stronghold_password("new_password").await?; - // Generate addresses with custom account index and range - // let addresses = GetAddressesBuilder::new(&SecretManager::Stronghold(stronghold_secret_manager)) - // .with_bech32_hrp(SHIMMER_TESTNET_BECH32_HRP) - // .with_coin_type(SHIMMER_COIN_TYPE) - // .with_account_index(0) - // .with_range(0..1) - // .finish() - // .await?; - - // println!("First public address: {}", addresses[0]); - Ok(()) } diff --git a/src/stronghold/migration.rs b/src/stronghold/migration.rs index 2c1752a870..4860c34960 100644 --- a/src/stronghold/migration.rs +++ b/src/stronghold/migration.rs @@ -7,17 +7,9 @@ use std::{ path::{Path, PathBuf}, }; -// use crypto::ciphers::chacha::{self}; -// use iota_stronghold::{Client, SnapshotPath, Stronghold}; use zeroize::Zeroize; -// use super::common::PRIVATE_DATA_CLIENT_PATH; -use crate::{ - actor::AccountManager, - // stronghold::{check_or_create_snapshot, storage::insert as v3_insert}, - Result, - StrongholdError, -}; +use crate::{actor::AccountManager, Result, StrongholdError}; impl AccountManager { /// Migrates a stronghold snapshot from version 2 to version 3.