Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs single account wallet #1580

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use iota_sdk;
use iota_sdk::{
client::secret::{SecretManager, SecretManagerDto},
types::block::address::Bech32Address,
utils::serde::bip44::option_bip44,
wallet::{ClientOptions, Wallet},
};
use serde::Deserialize;
Expand Down Expand Up @@ -45,6 +46,7 @@ pub fn init_logger(config: String) -> std::result::Result<(), fern_logger::Error
pub struct WalletOptions {
pub address: Option<Bech32Address>,
pub alias: Option<String>,
#[serde(with = "option_bip44", default)]
pub bip_path: Option<Bip44>,
pub client_options: Option<ClientOptions>,
#[derivative(Debug(format_with = "OmittedDebug::omitted_fmt"))]
Expand Down
9 changes: 8 additions & 1 deletion bindings/core/src/method_handler/secret_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use iota_sdk::client::secret::stronghold::StrongholdSecretManager;
use iota_sdk::{
client::{
api::{GetAddressesOptions, PreparedTransactionData},
secret::{DowncastSecretManager, SecretManage, SignBlock},
secret::{DowncastSecretManager, SecretManage, SecretManager, SignBlock},
},
types::{
block::{address::ToBech32Ext, core::UnsignedBlock, unlock::Unlock, SignedBlockDto},
Expand Down Expand Up @@ -124,6 +124,13 @@ where
if let Some(secret_manager) = secret_manager.downcast::<StrongholdSecretManager>() {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else if let Some(secret_manager) = secret_manager.downcast::<SecretManager>() {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
if let SecretManager::Stronghold(secret_manager) = secret_manager {
secret_manager.store_mnemonic(mnemonic).await?;
Response::Ok
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
} else {
return Err(iota_sdk::client::Error::SecretManagerMismatch.into());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { Wallet, CoinType, initLogger, WalletOptions } from '@iota/sdk';
import {
Wallet,
CoinType,
initLogger,
WalletOptions,
SecretManager,
} from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });

// Run with command:
// yarn run-example ./how_tos/accounts_and_addresses/create-account.ts
// yarn run-example ./how_tos/accounts_and_addresses/create-wallet.ts

// This example creates a new database and account.
// This example creates a new database and wallet.
async function run() {
initLogger();
for (const envVar of [
Expand All @@ -24,32 +30,47 @@ async function run() {
}

try {
const strongholdSecretManager = {
stronghold: {
snapshotPath: process.env.STRONGHOLD_SNAPSHOT_PATH,
password: process.env.STRONGHOLD_PASSWORD,
},
};

const secretManager = new SecretManager(strongholdSecretManager);

// A mnemonic can be generated with `Utils.generateMnemonic()`.
// Store the mnemonic in the Stronghold snapshot, this needs to be done only the first time.
// The mnemonic can't be retrieved from the Stronghold file, so make a backup in a secure place!
await secretManager.storeMnemonic(process.env.MNEMONIC as string);

const wallet_address = await secretManager.generateEd25519Addresses({
coinType: CoinType.IOTA,
accountIndex: 0,
range: {
start: 0,
end: 1,
},
bech32Hrp: 'tst',
});

const walletOptions: WalletOptions = {
address: wallet_address[0],
storagePath: process.env.WALLET_DB_PATH,
clientOptions: {
nodes: [process.env.NODE_URL as string],
},
coinType: CoinType.Shimmer,
secretManager: {
stronghold: {
snapshotPath: process.env.STRONGHOLD_SNAPSHOT_PATH,
password: process.env.STRONGHOLD_PASSWORD,
},
bipPath: {
coinType: CoinType.IOTA,
},
secretManager: strongholdSecretManager,
};

const wallet = new Wallet(walletOptions);

// A mnemonic can be generated with `Utils.generateMnemonic()`.
// Store the mnemonic in the Stronghold snapshot, this needs to be done only the first time.
// The mnemonic can't be retrieved from the Stronghold file, so make a backup in a secure place!
await wallet.storeMnemonic(process.env.MNEMONIC as string);

// Create a new account
const account = await wallet.createAccount({
alias: 'Alice',
});
console.log('Generated new account:', account.getMetadata().alias);
console.log(
'Generated wallet with address: ' + (await wallet.address()),
);
} catch (error) {
console.error('Error: ', error);
}
Expand Down
194 changes: 0 additions & 194 deletions bindings/nodejs/lib/types/wallet/account.ts

This file was deleted.

14 changes: 1 addition & 13 deletions bindings/nodejs/lib/types/wallet/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { SlotIndex } from '../block/slot';
import { Bech32Address, NftId, OutputId, TokenId } from '../block';
import { Bech32Address, NftId, TokenId } from '../block';
import { NumericString, u256, u64 } from '../utils';

/** A Bip44 address */
Expand Down Expand Up @@ -35,18 +35,6 @@ export interface SendParams {
expiration?: SlotIndex;
}

/** Address with unspent outputs */
export interface AddressWithUnspentOutputs {
/** The Bech32 address. */
address: Bech32Address;
/** The address key index. */
keyIndex: number;
/** Whether the address is a public or an internal (change) address. */
internal: boolean;
/** The IDs of associated unspent outputs. */
outputIds: OutputId[];
}

/** Address with native tokens */
export interface SendNativeTokensParams {
/** The Bech32 address. */
Expand Down
3 changes: 2 additions & 1 deletion bindings/nodejs/lib/types/wallet/bridge/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import type { SyncOptions, FilterOptions } from '../account';
import type {
SendParams,
SendNativeTokensParams,
Expand All @@ -25,10 +24,12 @@ import type {
} from '../participation';
import type { ConsolidationParams } from '../consolidation-params';
import {
FilterOptions,
HexEncodedAmount,
NumericString,
Output,
OutputId,
SyncOptions,
TokenId,
TransactionId,
} from '../../';
Expand Down
Loading
Loading