diff --git a/bindings/nodejs/README.md b/bindings/nodejs/README.md index 1c560ab0d4..abac727e55 100644 --- a/bindings/nodejs/README.md +++ b/bindings/nodejs/README.md @@ -108,7 +108,7 @@ run().then(() => process.exit()); ## Wallet Usage The following example will create a -new [`Wallet`](https://wiki.iota.org/shimmer/iota-sdk/references/nodejs/classes/Wallet/) [`Account`](https://wiki.iota.org/shimmer/iota-sdk/references/nodejs/classes/Account/) +new [`Wallet`](https://wiki.iota.org/shimmer/iota-sdk/references/nodejs/classes/Wallet/) that connects to the [Shimmer Testnet](https://api.testnet.shimmer.network) using the [`StrongholdSecretManager`](https://wiki.iota.org/shimmer/iota-sdk/references/python/iota_sdk/secret_manager/#strongholdsecretmanager-objects). @@ -116,7 +116,7 @@ that connects to the [Shimmer Testnet](https://api.testnet.shimmer.network) usin import { Wallet, CoinType, WalletOptions } from '@iota/sdk'; const walletOptions: WalletOptions = { - storagePath: `Alice`, // A name to associate with the created account. + storagePath: `Alice`, // A name to associate with the created wallet. clientOptions: { nodes: ['https://api.testnet.shimmer.network'], // The node to connect to. }, @@ -124,7 +124,7 @@ const walletOptions: WalletOptions = { secretManager: { // Setup Stronghold secret manager stronghold: { - snapshotPath: 'vault.stronghold', // The path to store the account snapshot. + snapshotPath: 'vault.stronghold', // The path to store the wallet snapshot. password: 'a-secure-password', // A password to encrypt the stored data. WARNING: Never hardcode passwords in production code. }, }, diff --git a/bindings/nodejs/examples/exchange/1-create-account.ts b/bindings/nodejs/examples/exchange/1-create-account.ts deleted file mode 100644 index c48bf4f490..0000000000 --- a/bindings/nodejs/examples/exchange/1-create-account.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -// This example creates a new database and account. -// Run with command: -// yarn run-example ./exchange/1-create-account.ts - -import { Wallet, WalletOptions, CoinType } from '@iota/sdk'; - -// This example uses secrets in environment variables for simplicity which should not be done in production. -require('dotenv').config({ path: '.env' }); - -async function run() { - try { - for (const envVar of [ - 'WALLET_DB_PATH', - 'NODE_URL', - 'STRONGHOLD_SNAPSHOT_PATH', - 'STRONGHOLD_PASSWORD', - 'MNEMONIC', - ]) - if (!(envVar in process.env)) { - throw new Error( - `.env ${envVar} is undefined, see .env.example`, - ); - } - - const walletOptions: WalletOptions = { - storagePath: process.env.WALLET_DB_PATH, - clientOptions: { - nodes: [process.env.NODE_URL as string], - }, - coinType: CoinType.IOTA, - secretManager: { - stronghold: { - snapshotPath: process.env.STRONGHOLD_SNAPSHOT_PATH, - password: process.env.STRONGHOLD_PASSWORD, - }, - }, - }; - - const wallet = new Wallet(walletOptions); - - // Mnemonic only needs to be set the first time. - await wallet.storeMnemonic(process.env.MNEMONIC as string); - - const account = await wallet.createAccount({ - alias: 'Alice', - }); - - // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, - // have a storage deposit return, expiration or are nft/account/foundry outputs. - account.setDefaultSyncOptions({ syncOnlyMostBasicOutputs: true }); - - console.log(account); - } catch (error) { - console.error(error); - } -} - -run().then(() => process.exit()); diff --git a/bindings/nodejs/examples/exchange/1-create-wallet.ts b/bindings/nodejs/examples/exchange/1-create-wallet.ts new file mode 100644 index 0000000000..a7266456f9 --- /dev/null +++ b/bindings/nodejs/examples/exchange/1-create-wallet.ts @@ -0,0 +1,74 @@ +// Copyright 2023 IOTA Stiftung +// SPDX-License-Identifier: Apache-2.0 + +// This example creates a new database and wallet. +// Run with command: +// yarn run-example ./exchange/1-create-wallet.ts + +import { Wallet, WalletOptions, CoinType, 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' }); + +async function run() { + try { + for (const envVar of [ + 'WALLET_DB_PATH', + 'NODE_URL', + 'STRONGHOLD_SNAPSHOT_PATH', + 'STRONGHOLD_PASSWORD', + 'MNEMONIC', + ]) + if (!(envVar in process.env)) { + throw new Error( + `.env ${envVar} is undefined, see .env.example`, + ); + } + + 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 walletAddress = await secretManager.generateEd25519Addresses({ + coinType: CoinType.IOTA, + accountIndex: 0, + range: { + start: 0, + end: 1, + }, + bech32Hrp: 'tst', + }); + + const walletOptions: WalletOptions = { + address: walletAddress[0], + storagePath: process.env.WALLET_DB_PATH, + clientOptions: { + nodes: [process.env.NODE_URL as string], + }, + bipPath: { + coinType: CoinType.IOTA, + }, + secretManager: strongholdSecretManager, + }; + + const wallet = new Wallet(walletOptions); + + // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, + // have a storage deposit return, expiration or are nft/account/foundry outputs. + wallet.setDefaultSyncOptions({ syncOnlyMostBasicOutputs: true }); + } catch (error) { + console.error(error); + } +} + +run().then(() => process.exit()); diff --git a/bindings/nodejs/examples/exchange/2-generate-address.ts b/bindings/nodejs/examples/exchange/2-generate-address.ts deleted file mode 100644 index 6ad4ac8b32..0000000000 --- a/bindings/nodejs/examples/exchange/2-generate-address.ts +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -// This example generates an address for an account. -// Run with command: -// yarn run-example ./exchange/2-generate-address.ts - -import { Wallet } from '@iota/sdk'; - -// This example uses secrets in environment variables for simplicity which should not be done in production. -require('dotenv').config({ path: '.env' }); - -async function run() { - try { - for (const envVar of ['WALLET_DB_PATH', 'STRONGHOLD_PASSWORD']) - if (!(envVar in process.env)) { - throw new Error( - `.env ${envVar} is undefined, see .env.example`, - ); - } - - const wallet = new Wallet({ - storagePath: process.env.WALLET_DB_PATH, - }); - - await wallet.setStrongholdPassword( - process.env.STRONGHOLD_PASSWORD as string, - ); - - const account = await wallet.getAccount('Alice'); - - const address = (await account.generateEd25519Addresses(1))[0]; - - console.log('Address:', address); - } catch (error) { - console.error(error); - } -} - -run().then(() => process.exit()); diff --git a/bindings/nodejs/examples/exchange/3-check-balance.ts b/bindings/nodejs/examples/exchange/3-check-balance.ts index 53d43b6093..ed9346243a 100644 --- a/bindings/nodejs/examples/exchange/3-check-balance.ts +++ b/bindings/nodejs/examples/exchange/3-check-balance.ts @@ -1,7 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -// This example gets the balance of an account. +// This example gets the balance of a wallet. // Run with command: // yarn run-example ./exchange/3-check-balance.ts @@ -21,15 +21,13 @@ async function run() { const wallet = new Wallet({ storagePath: process.env.WALLET_DB_PATH, }); + const address = await wallet.address(); - const account = await wallet.getAccount('Alice'); - const addresses = await account.addresses(); - - console.log('Addresses:', addresses); + console.log('Address:', address); // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, // have a storage deposit return, expiration or are nft/account/foundry outputs. - const balance = await account.sync({ syncOnlyMostBasicOutputs: true }); + const balance = await wallet.sync({ syncOnlyMostBasicOutputs: true }); console.log('Balance', balance); diff --git a/bindings/nodejs/examples/exchange/4-listen-events.ts b/bindings/nodejs/examples/exchange/4-listen-events.ts index ac1cd83838..88a6a58d7a 100644 --- a/bindings/nodejs/examples/exchange/4-listen-events.ts +++ b/bindings/nodejs/examples/exchange/4-listen-events.ts @@ -33,15 +33,13 @@ async function run() { // Only interested in new outputs here. await wallet.listen([WalletEventType.NewOutput], callback); - const account = await wallet.getAccount('Alice'); - // Use the faucet to send testnet tokens to your address. console.log( 'Fill your address with the faucet: https://faucet.testnet.shimmer.network/', ); - const addresses = await account.addresses(); - console.log('Send funds to:', addresses[0].address); + const address = await wallet.address(); + console.log('Send funds to:', address); // Sync every 5 seconds until the faucet transaction gets confirmed. for (let i = 0; i < 100; i++) { @@ -50,7 +48,7 @@ async function run() { // Sync to detect new outputs // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, // have a storage deposit return, expiration or are nft/account/foundry outputs. - await account.sync({ syncOnlyMostBasicOutputs: true }); + await wallet.sync({ syncOnlyMostBasicOutputs: true }); } } catch (error) { console.error(error); diff --git a/bindings/nodejs/examples/exchange/5-send-amount.ts b/bindings/nodejs/examples/exchange/5-send-amount.ts index 1c09592ee8..e387f1587a 100644 --- a/bindings/nodejs/examples/exchange/5-send-amount.ts +++ b/bindings/nodejs/examples/exchange/5-send-amount.ts @@ -31,14 +31,11 @@ async function run() { process.env.STRONGHOLD_PASSWORD as string, ); - const account = await wallet.getAccount('Alice'); - console.log('Account:', account); - // Set syncOnlyMostBasicOutputs to true if not interested in outputs that are timelocked, // have a storage deposit return, expiration or are nft/account/foundry outputs. - await account.sync({ syncOnlyMostBasicOutputs: true }); + await wallet.sync({ syncOnlyMostBasicOutputs: true }); - const response = await account.send( + const response = await wallet.send( BigInt(1000000), // Replace with the address of your choice! 'rms1qrrv7flg6lz5cssvzv2lsdt8c673khad060l4quev6q09tkm9mgtupgf0h0', diff --git a/bindings/nodejs/examples/how_tos/account_output/create.ts b/bindings/nodejs/examples/how_tos/account_output/create.ts index d5872abc0b..335b3884ba 100644 --- a/bindings/nodejs/examples/how_tos/account_output/create.ts +++ b/bindings/nodejs/examples/how_tos/account_output/create.ts @@ -6,7 +6,7 @@ import { Wallet, initLogger } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. // // Make sure that `example.stronghold` and `example.walletdb` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // require('dotenv').config({ path: '.env' }); @@ -31,11 +31,8 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); console.log(`Accounts BEFORE:\n`, balance.accounts); @@ -47,19 +44,19 @@ async function run() { console.log('Sending the create-account transaction...'); // Create an account output - const transaction = await account.createAccountOutput(); + const transaction = await wallet.createAccountOutput(); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); console.log(`Accounts AFTER:\n`, balance.accounts); } catch (error) { console.log('Error: ', error); diff --git a/bindings/nodejs/examples/how_tos/account_output/destroy.ts b/bindings/nodejs/examples/how_tos/account_output/destroy.ts index 78d369b0f2..b5f6eb23d9 100644 --- a/bindings/nodejs/examples/how_tos/account_output/destroy.ts +++ b/bindings/nodejs/examples/how_tos/account_output/destroy.ts @@ -27,17 +27,14 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); if (balance.accounts.length == 0) { throw new Error(`No Account output available in account 'Alice'`); } - // We try to destroy the first account output in the account + // We try to destroy the first account output in the wallet const accountId = balance.accounts[0]; console.log( @@ -53,14 +50,14 @@ async function run() { console.log('Sending the destroy-account transaction...'); // Destroy an account output - const transaction = await account + const transaction = await wallet .prepareDestroyAccount(accountId) .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( @@ -68,7 +65,7 @@ async function run() { ); console.log(`Destroyed account output ${accountId}`); - balance = await account.sync(); + balance = await wallet.sync(); console.log( `Accounts AFTER destroying (${balance.accounts.length}):\n`, balance.accounts, diff --git a/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts b/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts index f754bd1e3e..3e1ec12583 100644 --- a/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts +++ b/bindings/nodejs/examples/how_tos/account_wallet/request-funds.ts @@ -6,7 +6,7 @@ import { Utils, Wallet, initLogger } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. // // Make sure that `example.stronghold` and `example.walletdb` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // require('dotenv').config({ path: '.env' }); @@ -27,10 +27,7 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `create_wallet` - const account = await wallet.getAccount('Alice'); - - const balance = await account.sync(); + const balance = await wallet.sync(); const totalBaseTokenBalance = balance.baseCoin.total; console.log( @@ -57,7 +54,7 @@ async function run() { basicOutputs: true, }, }; - const totalBaseTokenBalanceAfter = (await account.sync(syncOptions)) + const totalBaseTokenBalanceAfter = (await wallet.sync(syncOptions)) .baseCoin.total; console.log( `Balance after requesting funds on account address: ${totalBaseTokenBalanceAfter}`, diff --git a/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts b/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts index aa7176e992..8fe6b32a4b 100644 --- a/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts +++ b/bindings/nodejs/examples/how_tos/account_wallet/transaction.ts @@ -6,7 +6,7 @@ import { Wallet, initLogger, Utils } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. // // Make sure that `example.stronghold` and `example.walletdb` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // require('dotenv').config({ path: '.env' }); @@ -33,11 +33,9 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); - const balance = await account.sync(syncOptions); + const balance = await wallet.sync(syncOptions); const totalBaseTokenBalance = balance.baseCoin.total; console.log( @@ -72,15 +70,13 @@ async function run() { mandatoryInputs: [input], allowMicroAmount: false, }; - const transaction = await account.sendWithParams(params, options); - await account.reissueTransactionUntilIncluded( - transaction.transactionId, - ); + const transaction = await wallet.sendWithParams(params, options); + await wallet.reissueTransactionUntilIncluded(transaction.transactionId); console.log( `Transaction with custom input: https://explorer.iota.org/testnet/transaction/${transaction.transactionId}`, ); - const totalBaseTokenBalanceAfter = (await account.sync(syncOptions)) + const totalBaseTokenBalanceAfter = (await wallet.sync(syncOptions)) .baseCoin.total; console.log( `Balance after sending funds from account: ${totalBaseTokenBalanceAfter}`, diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts index 042db1296a..af11654fb0 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/check-balance.ts @@ -20,14 +20,12 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - // Sync new outputs from the node. // eslint-disable-next-line @typescript-eslint/no-unused-vars - const _syncBalance = await account.sync(); + const _syncBalance = await wallet.sync(); // After syncing the balance can also be computed with the local data - const balance = await account.getBalance(); + const balance = await wallet.getBalance(); console.log('Balance', balance); } catch (error) { console.error('Error: ', error); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts index 50c1e3c330..e7619c3288 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/consolidate-outputs.ts @@ -9,7 +9,7 @@ require('dotenv').config({ path: '.env' }); // Run with command: // yarn run-example ./how_tos/accounts_and_addresses/consolidate-outputs.ts -// In this example we will consolidate basic outputs from an account with only an AddressUnlockCondition by sending +// In this example we will consolidate basic outputs from an wallet with only an AddressUnlockCondition by sending // them to the same address again. async function run() { initLogger(); @@ -29,23 +29,21 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - // To create an address we need to unlock stronghold. await wallet.setStrongholdPassword( process.env.STRONGHOLD_PASSWORD as string, ); - // Sync account to make sure account is updated with outputs from previous examples - account.sync(); - console.log('Account synced'); + // Sync wallet to make sure wallet is updated with outputs from previous examples + await wallet.sync(); + console.log('Wallet synced'); // List unspent outputs before consolidation. // The output we created with example `request_funds` and the basic output from `mint` have only one // unlock condition and it is an `AddressUnlockCondition`, and so they are valid for consolidation. They have the - // same `AddressUnlockCondition`(the first address of the account), so they will be consolidated into one + // same `AddressUnlockCondition`(the address of the wallet), so they will be consolidated into one // output. - const outputs = await account.unspentOutputs(); + const outputs = await wallet.unspentOutputs(); console.log('Outputs BEFORE consolidation:'); outputs.forEach(({ output, address }, i) => { @@ -64,13 +62,13 @@ async function run() { // Consolidate unspent outputs and print the consolidation transaction ID // Set `force` to true to force the consolidation even though the `output_threshold` isn't reached - const transaction = await account.consolidateOutputs({ + const transaction = await wallet.consolidateOutputs({ force: true, }); console.log('Transaction sent: %s', transaction.transactionId); // Wait for the consolidation transaction to get confirmed - const blockId = account.reissueTransactionUntilIncluded( + const blockId = wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -80,9 +78,9 @@ async function run() { blockId, ); - // Sync account - account.sync(); - console.log('Account synced'); + // Sync wallet + await wallet.sync(); + console.log('Wallet synced'); // Outputs after consolidation console.log('Outputs AFTER consolidation:'); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-address.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-address.ts deleted file mode 100644 index c525a4e16f..0000000000 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-address.ts +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -import { Wallet, initLogger } from '@iota/sdk'; -require('dotenv').config({ path: '.env' }); - -// Run with command: -// yarn run-example ./how_tos/accounts_and_addresses/create-address.ts - -// This example creates an address -async function run() { - initLogger(); - for (const envVar of ['WALLET_DB_PATH', 'STRONGHOLD_PASSWORD']) - if (!(envVar in process.env)) { - throw new Error(`.env ${envVar} is undefined, see .env.example`); - } - - try { - const wallet = new Wallet({ - storagePath: process.env.WALLET_DB_PATH, - }); - - const account = await wallet.getAccount('Alice'); - - // To create an address we need to unlock stronghold. - await wallet.setStrongholdPassword( - process.env.STRONGHOLD_PASSWORD as string, - ); - - const address = (await account.generateEd25519Addresses(1))[0]; - - console.log(`Generated address:`, address.address); - } catch (error) { - console.error('Error: ', error); - } -} - -run().then(() => process.exit()); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts index d6fdfa4553..e713c26fb3 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/create-wallet.ts @@ -44,7 +44,7 @@ async function run() { // 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({ + const walletAddress = await secretManager.generateEd25519Addresses({ coinType: CoinType.IOTA, accountIndex: 0, range: { @@ -55,7 +55,7 @@ async function run() { }); const walletOptions: WalletOptions = { - address: wallet_address[0], + address: walletAddress[0], storagePath: process.env.WALLET_DB_PATH, clientOptions: { nodes: [process.env.NODE_URL as string], diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts index f3e5662372..2ba9ac7942 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-accounts.ts @@ -9,7 +9,7 @@ require('dotenv').config({ path: '.env' }); // Run with command: // yarn run-example ./how_tos/accounts_and_addresses/list-accounts.ts -// This example lists all accounts in the wallet. +// This example lists all account outputs in the wallet. async function run() { initLogger(); if (!process.env.WALLET_DB_PATH) { @@ -20,10 +20,9 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const accounts = await wallet.getAccounts(); + const accounts = await wallet.accounts(); - for (const account of accounts) - console.log(account.getMetadata().alias); + for (const account of accounts) console.log(account); } catch (error) { console.error('Error: ', error); } diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-addresses.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-addresses.ts deleted file mode 100644 index 02e56eb9b5..0000000000 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-addresses.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2023 IOTA Stiftung -// SPDX-License-Identifier: Apache-2.0 - -import { Wallet, initLogger } 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/list-addresses.ts - -// This example lists all addresses in the account. -async function run() { - initLogger(); - if (!process.env.WALLET_DB_PATH) { - throw new Error('.env WALLET_DB_PATH is undefined, see .env.example'); - } - try { - const wallet = new Wallet({ - storagePath: process.env.WALLET_DB_PATH, - }); - - const account = await wallet.getAccount('Alice'); - - const addresses = await account.addresses(); - - for (const address of addresses) console.log(address.address); - } catch (error) { - console.error('Error: ', error); - } -} - -run().then(() => process.exit()); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts index a3fe14be53..a25192d276 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-outputs.ts @@ -9,7 +9,7 @@ require('dotenv').config({ path: '.env' }); // Run with command: // yarn run-example ./how_tos/accounts_and_addresses/list-outputs.ts -// This example lists all outputs in the account. +// This example lists all outputs in the wallet. async function run() { initLogger(); if (!process.env.WALLET_DB_PATH) { @@ -20,16 +20,14 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); + await wallet.sync(); - await account.sync(); - - const outputs = await account.outputs(); + const outputs = await wallet.outputs(); console.log('Output ids:'); for (const output of outputs) console.log(output.outputId); - const unspentOutputs = await account.unspentOutputs(); + const unspentOutputs = await wallet.unspentOutputs(); console.log('Unspent output ids:'); for (const output of unspentOutputs) console.log(output.outputId); diff --git a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts index 0cfd55dd81..feb53b0898 100644 --- a/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts +++ b/bindings/nodejs/examples/how_tos/accounts_and_addresses/list-transactions.ts @@ -9,7 +9,7 @@ require('dotenv').config({ path: '.env' }); // Run with command: // yarn run-example ./how_tos/accounts_and_addresses/list-transactions.ts -// This example lists all transactions in the account. +// This example lists all transactions in the wallet. async function run() { initLogger(); if (!process.env.WALLET_DB_PATH) { @@ -19,16 +19,14 @@ async function run() { const wallet = new Wallet({ storagePath: process.env.WALLET_DB_PATH, }); + await wallet.sync({ syncIncomingTransactions: true }); - const account = await wallet.getAccount('Alice'); - await account.sync({ syncIncomingTransactions: true }); - - const transactions = await account.transactions(); + const transactions = await wallet.transactions(); console.log('Sent transactions:'); for (const transaction of transactions) console.log(transaction.transactionId); - const incomingTransactions = await account.incomingTransactions(); + const incomingTransactions = await wallet.incomingTransactions(); console.log('Incoming transactions:'); for (const transaction of incomingTransactions) { console.log(transaction.transactionId); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts index 13d22cd700..199fab2471 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/advanced_transaction.ts @@ -28,9 +28,7 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - - await account.sync(); + await wallet.sync(); // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); @@ -53,11 +51,11 @@ async function run() { ], }); - const transaction = await account.sendOutputs([basicOutput]); + const transaction = await wallet.sendOutputs([basicOutput]); console.log(`Transaction sent: ${transaction.transactionId}`); console.log('Waiting until included in block...'); - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts index 5d586875ed..67a1fb7b53 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/claim_transaction.ts @@ -7,7 +7,7 @@ require('dotenv').config({ path: '.env' }); // Run with command: // yarn run-example ./how_tos/advanced_transactions/claim_transaction.ts -// This example claims all claimable outputs in the account. +// This example claims all claimable outputs in the wallet. async function run() { initLogger(); try { @@ -21,24 +21,22 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - - await account.sync(); + await wallet.sync(); // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); // Get all claimable outputs - const output_ids = await account.claimableOutputs(OutputsToClaim.All); + const output_ids = await wallet.claimableOutputs(OutputsToClaim.All); console.log(`Available outputs to claim:`); for (const output_id of output_ids) { console.log(output_id); } - const transaction = await account.claimOutputs(output_ids); + const transaction = await wallet.claimOutputs(output_ids); console.log(`Transaction sent: ${transaction.transactionId}`); - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log(`Block sent: ${process.env.EXPLORER_URL}/block/${blockId}`); diff --git a/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts b/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts index 22da16ac31..6cb5e31db1 100644 --- a/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts +++ b/bindings/nodejs/examples/how_tos/advanced_transactions/send_micro_transaction.ts @@ -21,9 +21,7 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - - await account.sync(); + await wallet.sync(); // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); @@ -33,13 +31,13 @@ async function run() { 'rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu'; const amount = BigInt(1); - const transaction = await account.send(amount, address, { + const transaction = await wallet.send(amount, address, { allowMicroAmount: true, }); console.log(`Transaction sent: ${transaction.transactionId}`); - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); diff --git a/bindings/nodejs/examples/how_tos/native_tokens/burn.ts b/bindings/nodejs/examples/how_tos/native_tokens/burn.ts index 5f68f0fa76..432895eae3 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/burn.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/burn.ts @@ -3,7 +3,7 @@ import { getUnlockedWallet } from '../../wallet/common'; -// The minimum available native token amount to search for in the account. +// The minimum available native token amount to search for in the wallet. const MIN_AVAILABLE_AMOUNT = BigInt(11); // The amount of the native token to burn. const BURN_AMOUNT = BigInt(1); @@ -13,7 +13,7 @@ const BURN_AMOUNT = BigInt(1); // output that minted it. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/burn.ts @@ -22,11 +22,8 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); // Get a token with sufficient balance const tokenId = balance.nativeTokens.find( @@ -42,21 +39,21 @@ async function run() { throw new Error( `Native token '${tokenId}' doesn't exist or there's not at least '${Number( MIN_AVAILABLE_AMOUNT, - )}' tokens of it in account 'Alice'`, + )}' tokens of it in the wallet`, ); } console.log(`Balance before burning: ${token.available}`); // Burn a native token - const transaction = await account + const transaction = await wallet .prepareBurnNativeToken(token.tokenId, BURN_AMOUNT) .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -64,7 +61,7 @@ async function run() { `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); token = balance.nativeTokens.find( (nativeToken) => nativeToken.tokenId == tokenId, diff --git a/bindings/nodejs/examples/how_tos/native_tokens/create.ts b/bindings/nodejs/examples/how_tos/native_tokens/create.ts index 6011237381..81035d82e1 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/create.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/create.ts @@ -13,7 +13,7 @@ const MAXIMUM_SUPPLY = BigInt(100); // In this example we will create a native token. // // Make sure that `example.stronghold` and `example.walletdb` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/create.ts @@ -22,22 +22,19 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - const balance = await account.sync(); + const balance = await wallet.sync(); // We can first check if we already have an account output in our account, because an account output can have // many foundry outputs and therefore we can reuse an existing one if (balance.accounts.length == 0) { // If we don't have an account output, we need to create one - const transaction = await account + const transaction = await wallet .prepareCreateAccountOutput() .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -45,8 +42,8 @@ async function run() { `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - await account.sync(); - console.log('Account synced'); + await wallet.sync(); + console.log('Wallet synced'); } console.log('Preparing transaction to create native token...'); @@ -57,20 +54,20 @@ async function run() { 10, ).withDescription('A native token to test the iota-sdk.'); - // If we omit the AccountAddress field the first address of the account is used by default + // If we omit the AccountAddress field the wallet address is used by default const params: CreateNativeTokenParams = { circulatingSupply: CIRCULATING_SUPPLY, maximumSupply: MAXIMUM_SUPPLY, foundryMetadata: metadata.asHex(), }; - const prepared = await account.prepareCreateNativeToken(params); + const prepared = await wallet.prepareCreateNativeToken(params); const transaction = await prepared.send(); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -81,8 +78,8 @@ async function run() { console.log(`Created token: ${prepared.tokenId()}`); // Ensure the account is synced after creating the native token. - await account.sync(); - console.log('Account synced'); + await wallet.sync(); + console.log('Wallet synced'); } catch (error) { console.log('Error: ', error); } diff --git a/bindings/nodejs/examples/how_tos/native_tokens/destroy-foundry.ts b/bindings/nodejs/examples/how_tos/native_tokens/destroy-foundry.ts index 50ac1e83fe..90c557b856 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/destroy-foundry.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/destroy-foundry.ts @@ -3,11 +3,11 @@ import { getUnlockedWallet } from '../../wallet/common'; -// In this example we will try to destroy the first foundry there is in the account. This is only possible if its +// In this example we will try to destroy the first foundry there is in the wallet. This is only possible if its // circulating supply is 0 and no native tokens were burned. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/destroy-foundry.ts @@ -16,37 +16,34 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); if (balance.foundries.length == 0) { - throw new Error(`No Foundry available in account 'Alice'`); + throw new Error(`No Foundry available in the wallet`); } - // We try to destroy the first foundry in the account + // We try to destroy the first foundry in the wallet const foundry = balance.foundries[0]; console.log(`Foundries before destroying: ${balance.foundries.length}`); // Burn a foundry - const transaction = await account + const transaction = await wallet .prepareDestroyFoundry(foundry) .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); console.log(`Foundries after destroying: ${balance.foundries.length}`); } catch (error) { console.log('Error: ', error); diff --git a/bindings/nodejs/examples/how_tos/native_tokens/melt.ts b/bindings/nodejs/examples/how_tos/native_tokens/melt.ts index d2381f0888..5826a1c851 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/melt.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/melt.ts @@ -9,7 +9,7 @@ const MELT_AMOUNT = BigInt(10); // In this example we will melt an existing native token with its foundry. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/melt.ts @@ -18,11 +18,8 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); if (balance.foundries.length == 0) { throw new Error(`No Foundry available in account 'Alice'`); @@ -36,14 +33,14 @@ async function run() { ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance before melting: ${token.available}`); // Melt some of the circulating supply - const transaction = await account.meltNativeToken( + const transaction = await wallet.meltNativeToken( token.tokenId, MELT_AMOUNT, ); @@ -51,7 +48,7 @@ async function run() { console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -59,13 +56,13 @@ async function run() { `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); token = balance.nativeTokens.find( (nativeToken) => nativeToken.tokenId == tokenId, ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance after melting: ${token.available}`); diff --git a/bindings/nodejs/examples/how_tos/native_tokens/mint.ts b/bindings/nodejs/examples/how_tos/native_tokens/mint.ts index 6fa0aead4b..56a7362824 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/mint.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/mint.ts @@ -9,7 +9,7 @@ const MINT_AMOUNT = BigInt(10); // In this example we will mint an existing native token with its foundry. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/mint.ts @@ -18,11 +18,8 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); if (balance.foundries.length == 0) { throw new Error(`No Foundry available in account 'Alice'`); @@ -36,20 +33,20 @@ async function run() { ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance before minting: ${token.available}`); // Mint some more native tokens - const transaction = await account + const transaction = await wallet .prepareMintNativeToken(token.tokenId, MINT_AMOUNT) .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -57,13 +54,13 @@ async function run() { `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); token = balance.nativeTokens.find( (nativeToken) => nativeToken.tokenId == tokenId, ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance after minting: ${token.available}`); diff --git a/bindings/nodejs/examples/how_tos/native_tokens/send.ts b/bindings/nodejs/examples/how_tos/native_tokens/send.ts index 4b3824eec8..a42a1459b4 100644 --- a/bindings/nodejs/examples/how_tos/native_tokens/send.ts +++ b/bindings/nodejs/examples/how_tos/native_tokens/send.ts @@ -14,7 +14,7 @@ const RECV_ADDRESS = // In this example we will send native tokens. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/native_tokens/send.ts @@ -23,11 +23,8 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); // Get a token with sufficient balance const tokenId = balance.nativeTokens.find( @@ -47,17 +44,17 @@ async function run() { ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance before sending: ${token.available}`); - const transaction = await account.sendNativeTokens(outputs); + const transaction = await wallet.sendNativeTokens(outputs); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -65,13 +62,13 @@ async function run() { `Block included: ${process.env.EXPLORER_URL}/block/${blockId}`, ); - balance = await account.sync(); + balance = await wallet.sync(); token = balance.nativeTokens.find( (nativeToken) => nativeToken.tokenId == tokenId, ); if (token == null) { throw new Error( - `Couldn't find native token '${tokenId}' in the account`, + `Couldn't find native token '${tokenId}' in the wallet`, ); } console.log(`Balance after sending: ${token.available}`); diff --git a/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts b/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts index 9850ec2e81..1106c6ecbd 100644 --- a/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts +++ b/bindings/nodejs/examples/how_tos/nft_collection/00_mint_issuer_nft.ts @@ -14,7 +14,7 @@ require('dotenv').config({ path: '.env' }); // In this example we will mint the issuer NFT for the NFT collection. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/nft_collection/00_mint_issuer_nft.ts @@ -34,11 +34,8 @@ async function run() { // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - await account.sync(); - console.log(`Account synced!`); + await wallet.sync(); + console.log(`Wallet synced!`); // Issue the minting transaction and wait for its inclusion console.log(`Sending NFT minting transaction...`); @@ -47,10 +44,10 @@ async function run() { 'This NFT will be the issuer from the awesome NFT collection', ), }; - const transaction = await account.mintNfts([params]); + const transaction = await wallet.mintNfts([params]); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( diff --git a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts index f5f607ce3b..6f7a6034ea 100644 --- a/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts +++ b/bindings/nodejs/examples/how_tos/nft_collection/01_mint_collection_nft.ts @@ -12,7 +12,7 @@ const NUM_NFTS_MINTED_PER_TRANSACTION = 50; // In this example we will mint the issuer NFT for the NFT collection. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/nfts/01_mint_collection_nft.ts @@ -32,11 +32,9 @@ async function run() { // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); - // Get the account we generated with `how_tos/accounts_and_addresses/create-account` - const account = await wallet.getAccount('Alice'); - - await account.sync(); - console.log(`Account synced!`); + // Get the wallet we generated with `how_tos/accounts_and_addresses/create-wallet` + await wallet.sync(); + console.log(`Wallet synced!`); // Get the id we generated with `00_mint_issuer_nft` const issuerNftId: NftId = process.argv[2]; @@ -70,10 +68,10 @@ async function run() { i + chunk.length }/${NFT_COLLECTION_SIZE})`, ); - const transaction = await account.mintNfts(chunk); + const transaction = await wallet.mintNfts(chunk); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( @@ -81,7 +79,7 @@ async function run() { ); // Sync so the new outputs are available again for new transactions - await account.sync(); + await wallet.sync(); } // After the NFTs are minted, the issuer nft can be sent to the so called "null address" diff --git a/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts b/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts index c9ac0ef987..d72d9dba05 100644 --- a/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/burn_nft.ts @@ -7,7 +7,7 @@ require('dotenv').config({ path: '.env' }); // In this example we will burn an existing nft output. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/nfts/burn_nft.ts @@ -24,19 +24,14 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - // We need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); - // May want to ensure the account is synced before sending a transaction. - let balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + let balance = await wallet.sync(); if (balance.nfts.length == 0) { - throw new Error( - `No NFT available in account '${process.env.ACCOUNT_ALIAS_1}'`, - ); + throw new Error(`No NFT available in the wallet`); } // Get the first nft const nftId = balance.nfts[0]; @@ -44,14 +39,14 @@ async function run() { console.log(`Balance BEFORE burning:\n`, balance); // Burn an NFT - const transaction = await account + const transaction = await wallet .prepareBurnNft(nftId) .then((prepared) => prepared.send()); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); console.log( @@ -59,7 +54,7 @@ async function run() { ); console.log(`Burned NFT ${nftId}`); - balance = await account.sync(); + balance = await wallet.sync(); console.log(`Balance AFTER burning:\n`, balance); } catch (error) { console.log('Error: ', error); diff --git a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts index de78ab4d04..98378daacf 100644 --- a/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/mint_nft.ts @@ -27,7 +27,7 @@ const NFT2_AMOUNT = '1000000'; // In this example we will mint a new nft. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/nfts/mint_nft.ts @@ -43,10 +43,8 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - - // We send from the first address in the account. - const senderAddress = (await account.addresses())[0].address; + // We send from the address in the wallet. + const senderAddress = await wallet.address(); // We need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); @@ -65,11 +63,11 @@ async function run() { issuer: senderAddress, immutableMetadata: metadata.asHex(), }; - let transaction = await account.mintNfts([params]); + let transaction = await wallet.mintNfts([params]); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - let blockId = await account.reissueTransactionUntilIncluded( + let blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -96,11 +94,11 @@ async function run() { features: [new SenderFeature(new Ed25519Address(hexAddress))], }); - transaction = await account.sendOutputs([output]); + transaction = await wallet.sendOutputs([output]); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - blockId = await account.reissueTransactionUntilIncluded( + blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -110,8 +108,8 @@ async function run() { console.log('Minted NFT 2'); - // Ensure the account is synced after minting. - await account.sync(); + // Ensure the wallet is synced after minting. + await wallet.sync(); } catch (error) { console.error('Error: ', error); } diff --git a/bindings/nodejs/examples/how_tos/nfts/send_nft.ts b/bindings/nodejs/examples/how_tos/nfts/send_nft.ts index 77097345c2..907efaa23c 100644 --- a/bindings/nodejs/examples/how_tos/nfts/send_nft.ts +++ b/bindings/nodejs/examples/how_tos/nfts/send_nft.ts @@ -11,7 +11,7 @@ const RECV_ADDRESS = // In this example we will send an NFT (Non-fungible token). // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./how_tos/nfts/send_nft.ts @@ -27,14 +27,11 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - // We need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); - // May want to ensure the account is synced before sending a transaction. - const balance = await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + const balance = await wallet.sync(); if (balance.nfts.length == 0) { throw new Error('No available NFTs'); @@ -50,12 +47,12 @@ async function run() { ]; // Send the full NFT output to the specified address - const transaction = await account.sendNft(outputs); + const transaction = await wallet.sendNft(outputs); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); @@ -64,7 +61,7 @@ async function run() { ); // To send an NFT with expiration unlock condition prepareOutput() can be used like this: - // const output = await account.prepareOutput({ + // const output = await wallet.prepareOutput({ // recipientAddress: 'rms1qz6aj69rumk3qu0ra5ag6p6kk8ga3j8rfjlaym3wefugs3mmxgzfwa6kw3l', // amount: "47000", // unlocks: { @@ -76,7 +73,7 @@ async function run() { // storageDeposit: { returnStrategy: 'Gift' } // }); - // const transaction = await account.sendOutputs([output]); + // const transaction = await wallet.sendOutputs([output]); } catch (error) { console.log('Error: ', error); } diff --git a/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts b/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts index 2a86b934ef..8236e5d3dc 100644 --- a/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts +++ b/bindings/nodejs/examples/how_tos/simple_transaction/request-funds.ts @@ -23,10 +23,7 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - // Get the account we generated with `create-account` - const account = await wallet.getAccount('Alice'); - - const address = (await account.addresses())[0].address; + const address = await wallet.address(); console.log(address); const faucetResponse = await ( diff --git a/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts b/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts index 78c4079e5b..ec1249840b 100644 --- a/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts +++ b/bindings/nodejs/examples/how_tos/simple_transaction/simple-transaction.ts @@ -23,9 +23,7 @@ async function run() { storagePath: process.env.WALLET_DB_PATH, }); - const account = await wallet.getAccount('Alice'); - - await account.sync(); + await wallet.sync(); // To sign a transaction we need to unlock stronghold. await wallet.setStrongholdPassword(process.env.STRONGHOLD_PASSWORD); @@ -35,7 +33,7 @@ async function run() { 'rms1qrrv7flg6lz5cssvzv2lsdt8c673khad060l4quev6q09tkm9mgtupgf0h0'; const amount = BigInt(1000000); - const response = await account.send(amount, address); + const response = await wallet.send(amount, address); console.log( `Block sent: ${process.env.EXPLORER_URL}/block/${response.blockId}`, diff --git a/bindings/nodejs/examples/wallet/06-send-micro-transaction.ts b/bindings/nodejs/examples/wallet/06-send-micro-transaction.ts index d466cd282e..ff839d0746 100644 --- a/bindings/nodejs/examples/wallet/06-send-micro-transaction.ts +++ b/bindings/nodejs/examples/wallet/06-send-micro-transaction.ts @@ -14,7 +14,7 @@ const RECV_ADDRESS = // In this example we will send an amount below the minimum storage deposit. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./wallet/06-send-micro-transaction.ts @@ -23,11 +23,8 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); - - // May want to ensure the account is synced before sending a transaction. - await account.sync(); + // May want to ensure the wallet is synced before sending a transaction. + await wallet.sync(); console.log( `Sending '${SEND_MICRO_AMOUNT}' coin(s) to '${RECV_ADDRESS}'...`, @@ -36,13 +33,13 @@ async function run() { { address: RECV_ADDRESS, amount: SEND_MICRO_AMOUNT }, ]; - const transaction = await account.sendWithParams(params, { + const transaction = await wallet.sendWithParams(params, { allowMicroAmount: true, }); console.log(`Transaction sent: ${transaction.transactionId}`); // Wait for transaction to get included - const blockId = await account.reissueTransactionUntilIncluded( + const blockId = await wallet.reissueTransactionUntilIncluded( transaction.transactionId, ); diff --git a/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts b/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts index 0b48964e74..2e1faa23e5 100644 --- a/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts +++ b/bindings/nodejs/examples/wallet/17-check-unlock-conditions.ts @@ -8,10 +8,10 @@ import { getUnlockedWallet } from './common'; // The amount to build the basic output with const AMOUNT = BigInt(1000000); -// In this example we check if an output has only an address unlock condition and that the address is from the account. +// In this example we check if an output has only an address unlock condition and that the address is from the wallet. // // Make sure that `STRONGHOLD_SNAPSHOT_PATH` and `WALLET_DB_PATH` already exist by -// running the `how_tos/accounts_and_addresses/create-account` example! +// running the `how_tos/accounts_and_addresses/create-wallet` example! // // Rename `.env.example` to `.env` first, then run // yarn run-example ./wallet/17-check-unlock-conditions.ts @@ -20,40 +20,35 @@ async function run() { // Create the wallet const wallet = await getUnlockedWallet(); - // Get the account we generated with `01-create-wallet` - const account = await wallet.getAccount('Alice'); + const walletAddress = await wallet.address(); - const accountAddresses = await account.addresses(); - - const output: Output = await account.prepareOutput({ - recipientAddress: accountAddresses[0].address, + const output: Output = await wallet.prepareOutput({ + recipientAddress: walletAddress, amount: AMOUNT, }); - const hexEncodedAccountAddresses = accountAddresses.map((a) => - Utils.bech32ToHex(a.address), - ); + const hexEncodedwalletAddress = Utils.bech32ToHex(walletAddress); if (output instanceof BasicOutput) { const basicOutput = output as BasicOutput; - let controlledByAccount = false; + let controlledByWallet = false; if ( basicOutput.unlockConditions.length === 1 && basicOutput.unlockConditions[0] instanceof AddressUnlockCondition && - hexEncodedAccountAddresses.includes( + hexEncodedwalletAddress.includes( ( basicOutput .unlockConditions[0] as AddressUnlockCondition ).address.toString(), ) ) { - controlledByAccount = true; + controlledByWallet = true; } console.log( - 'The output has only an address unlock condition and the address is from the account: ' + - controlledByAccount, + 'The output has only an address unlock condition and the address is from the wallet: ' + + controlledByWallet, ); } } catch (error) { diff --git a/bindings/nodejs/examples/wallet/common.ts b/bindings/nodejs/examples/wallet/common.ts index 642d28ff88..4479a2a00f 100644 --- a/bindings/nodejs/examples/wallet/common.ts +++ b/bindings/nodejs/examples/wallet/common.ts @@ -1,7 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { initLogger, Wallet, CoinType, WalletOptions } from '@iota/sdk'; +import { initLogger, Wallet, WalletOptions } from '@iota/sdk'; // This example uses secrets in environment variables for simplicity which should not be done in production. require('dotenv').config({ path: '.env' }); @@ -21,16 +21,6 @@ async function getUnlockedWallet() { const walletOptions: WalletOptions = { 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, - }, - }, }; const wallet = new Wallet(walletOptions); diff --git a/bindings/nodejs/examples/wallet/events.ts b/bindings/nodejs/examples/wallet/events.ts index 0657f4c831..13e7663064 100644 --- a/bindings/nodejs/examples/wallet/events.ts +++ b/bindings/nodejs/examples/wallet/events.ts @@ -25,12 +25,7 @@ async function run() { const wallet = await getUnlockedWallet(); const callback = function (err: any, event: Event) { - console.log( - 'AccountIndex:', - event.accountIndex, - ', Event:', - event.event, - ); + console.log('Event:', event); }; await wallet.listen([], callback); diff --git a/bindings/nodejs/examples/wallet/getting-started.ts b/bindings/nodejs/examples/wallet/getting-started.ts index 1913122e68..541020e59b 100644 --- a/bindings/nodejs/examples/wallet/getting-started.ts +++ b/bindings/nodejs/examples/wallet/getting-started.ts @@ -1,7 +1,13 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { Wallet, CoinType, WalletOptions, Utils } from '@iota/sdk'; +import { + Wallet, + CoinType, + WalletOptions, + Utils, + SecretManager, +} from '@iota/sdk'; // Run with command: // yarn run-example ./wallet/getting-started.ts @@ -9,9 +15,6 @@ import { Wallet, CoinType, WalletOptions, Utils } from '@iota/sdk'; // The database path. const WALLET_DB_PATH = 'getting-started-db'; -// A name to associate with the created account. -const ACCOUNT_ALIAS = 'Alice'; - // The node to connect to. const NODE_URL = 'https://api.testnet.shimmer.network'; @@ -19,40 +22,53 @@ const NODE_URL = 'https://api.testnet.shimmer.network'; // WARNING: Never hardcode passwords in production code. const STRONGHOLD_PASSWORD = 'a-secure-password'; -// The path to store the account snapshot. +// The path to store the wallet snapshot. const STRONGHOLD_SNAPSHOT_PATH = 'vault.stronghold'; async function main() { - const walletOptions: WalletOptions = { - storagePath: WALLET_DB_PATH, - clientOptions: { - nodes: [NODE_URL], - }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: STRONGHOLD_SNAPSHOT_PATH, - password: STRONGHOLD_PASSWORD, - }, + const strongholdSecretManager = { + stronghold: { + snapshotPath: STRONGHOLD_SNAPSHOT_PATH, + password: STRONGHOLD_PASSWORD, }, }; - const wallet = new Wallet(walletOptions); + const secretManager = new SecretManager(strongholdSecretManager); // Generate a mnemonic and store its seed in the Stronghold vault. // INFO: It is best practice to back up the mnemonic somewhere secure. const mnemonic = Utils.generateMnemonic(); console.log('Mnemonic:' + mnemonic); - await wallet.storeMnemonic(mnemonic); - // Create an account. - const account = await wallet.createAccount({ - alias: ACCOUNT_ALIAS, + // 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(mnemonic); + + const wallet_address = await secretManager.generateEd25519Addresses({ + coinType: CoinType.IOTA, + accountIndex: 0, + range: { + start: 0, + end: 1, + }, + bech32Hrp: 'tst', }); - // Get the first address and print it. - const address = (await account.addresses())[0]; - console.log(`Address: ${address.address}\n`); + const walletOptions: WalletOptions = { + address: wallet_address[0], + storagePath: WALLET_DB_PATH, + clientOptions: { + nodes: [NODE_URL as string], + }, + bipPath: { + coinType: CoinType.IOTA, + }, + secretManager: strongholdSecretManager, + }; + + const wallet = new Wallet(walletOptions); + + console.log('Generated wallet with address: ' + (await wallet.address())); process.exit(0); } diff --git a/bindings/nodejs/examples/wallet/migrate-stronghold-snapshot-v2-to-v3.ts b/bindings/nodejs/examples/wallet/migrate-stronghold-snapshot-v2-to-v3.ts index 0a3663a269..04d67eb36c 100644 --- a/bindings/nodejs/examples/wallet/migrate-stronghold-snapshot-v2-to-v3.ts +++ b/bindings/nodejs/examples/wallet/migrate-stronghold-snapshot-v2-to-v3.ts @@ -1,12 +1,7 @@ // Copyright 2023 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -import { - CoinType, - WalletOptions, - Wallet, - migrateStrongholdSnapshotV2ToV3, -} from '@iota/sdk'; +import { migrateStrongholdSnapshotV2ToV3, SecretManager } from '@iota/sdk'; require('dotenv').config({ path: '.env' }); const v2Path = '../../../sdk/tests/wallet/fixtures/v2.stronghold'; @@ -21,23 +16,16 @@ async function run() { throw new Error(`.env ${envVar} is undefined, see .env.example`); } - let walletOptions: WalletOptions = { - storagePath: process.env.WALLET_DB_PATH, - clientOptions: { - nodes: [process.env.NODE_URL as string], - }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: v2Path, - password: 'current_password', - }, + const strongholdSecretManager = { + stronghold: { + snapshotPath: process.env.STRONGHOLD_SNAPSHOT_PATH, + password: process.env.STRONGHOLD_PASSWORD, }, }; try { // This should fail with error, migration required. - new Wallet(walletOptions); + new SecretManager(strongholdSecretManager); } catch (error) { console.error(error); } @@ -51,22 +39,8 @@ async function run() { 'new_password', ); - walletOptions = { - storagePath: process.env.WALLET_DB_PATH, - clientOptions: { - nodes: [process.env.NODE_URL as string], - }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: v3Path, - password: 'new_password', - }, - }, - }; - // This shouldn't fail anymore as snapshot has been migrated. - new Wallet(walletOptions); + new SecretManager(strongholdSecretManager); } run().then(() => process.exit()); diff --git a/bindings/nodejs/lib/types/wallet/address.ts b/bindings/nodejs/lib/types/wallet/address.ts index 04bc9b84b0..c88fbf5fde 100644 --- a/bindings/nodejs/lib/types/wallet/address.ts +++ b/bindings/nodejs/lib/types/wallet/address.ts @@ -24,7 +24,7 @@ export interface SendParams { /** * Bech32 encoded address, to which the storage deposit will be returned if one is necessary * given the provided amount. If a storage deposit is needed and a return address is not provided, it will - * default to the first address of the account. + * default to the address of the wallet. */ returnAddress?: string; /** @@ -43,7 +43,7 @@ export interface SendNativeTokensParams { nativeTokens: [TokenId, u256][]; /** * Bech32 encoded address, to which the storage deposit will be returned. - * Default will use the first address of the account. + * Default will use the address of the wallet. */ returnAddress?: Bech32Address; /** diff --git a/bindings/nodejs/lib/types/wallet/transaction-options.ts b/bindings/nodejs/lib/types/wallet/transaction-options.ts index eebf1c2e2c..5184335c61 100644 --- a/bindings/nodejs/lib/types/wallet/transaction-options.ts +++ b/bindings/nodejs/lib/types/wallet/transaction-options.ts @@ -77,7 +77,7 @@ export interface CreateNativeTokenParams { /** Options for minting NFTs. */ export interface MintNftParams { /** Bech32 encoded address to which the Nft will be minted. Default will use the - * first address of the account + * address of the wallet. */ address?: Bech32Address; /** Bech32 encoded sender address **/ @@ -95,7 +95,7 @@ export interface MintNftParams { /** Options for the account output creation */ export interface AccountOutputParams { /** Bech32 encoded address to which the Nft will be minted. Default will use the - * first address of the account + * address of the wallet. */ address?: Bech32Address; /** Hex encoded bytes */ diff --git a/bindings/nodejs/lib/types/wallet/wallet.ts b/bindings/nodejs/lib/types/wallet/wallet.ts index e40cc53203..85e2d77d92 100644 --- a/bindings/nodejs/lib/types/wallet/wallet.ts +++ b/bindings/nodejs/lib/types/wallet/wallet.ts @@ -78,7 +78,7 @@ export interface NativeTokenBalance { available: u256; } -/** Sync options for an account */ +/** Sync options for a wallet */ export interface SyncOptions { /** * Usually syncing is skipped if it's called in between 200ms, because there can only be new changes every diff --git a/bindings/nodejs/lib/wallet/wallet.ts b/bindings/nodejs/lib/wallet/wallet.ts index c4f70b9683..545d7e7fa3 100644 --- a/bindings/nodejs/lib/wallet/wallet.ts +++ b/bindings/nodejs/lib/wallet/wallet.ts @@ -399,7 +399,7 @@ export class Wallet { /** * Claim basic or nft outputs that have additional unlock conditions - * to their `AddressUnlockCondition` from the account. + * to their `AddressUnlockCondition` from the wallet. * @param outputIds The outputs to claim. * @returns The resulting transaction. */ @@ -419,7 +419,7 @@ export class Wallet { } /** - * Consolidate basic outputs with only an `AddressUnlockCondition` from an account + * Consolidate basic outputs with only an `AddressUnlockCondition` from a wallet * by sending them to an own address again if the output amount is greater or * equal to the output consolidation threshold. * @param params Consolidation options. @@ -432,7 +432,7 @@ export class Wallet { } /** - * Consolidate basic outputs with only an `AddressUnlockCondition` from an account + * Consolidate basic outputs with only an `AddressUnlockCondition` from a wallet * by sending them to an own address again if the output amount is greater or * equal to the output consolidation threshold. * @param params Consolidation options. @@ -757,7 +757,7 @@ export class Wallet { /** * Get a `FoundryOutput` by native token ID. It will try to get the foundry from - * the account, if it isn't in the account it will try to get it from the node. + * the account, if it isn't in the wallet it will try to get it from the node. * * @param tokenId The native token ID to get the foundry for. * @returns The `FoundryOutput` that minted the token. @@ -789,7 +789,7 @@ export class Wallet { } /** - * Get a transaction stored in the account. + * Get a transaction stored in the wallet. * * @param transactionId The ID of the transaction to get. * @returns The transaction. @@ -810,7 +810,7 @@ export class Wallet { } /** - * Get the transaction with inputs of an incoming transaction stored in the account + * Get the transaction with inputs of an incoming transaction stored in the wallet * List might not be complete, if the node pruned the data already * * @param transactionId The ID of the transaction to get. @@ -1229,7 +1229,7 @@ export class Wallet { } /** - * Reissues a transaction sent from the account for a provided transaction id until it's + * Reissues a transaction sent from the wallet for a provided transaction id until it's * included (referenced by a milestone). Returns the included block id. */ async reissueTransactionUntilIncluded( @@ -1493,7 +1493,7 @@ export class Wallet { } /** - * Validate the transaction, submit it to a node and store it in the account. + * Validate the transaction, submit it to a node and store it in the wallet. * * @param signedTransactionData A signed transaction to submit and store. * @returns The sent transaction. @@ -1584,7 +1584,7 @@ export class Wallet { } /** - * Calculates the voting overview of an account. + * Calculates the voting overview of a wallet. * * @param eventIds Optional, filters participations only for provided events. * @returns An instance of `ParticipationOverview` diff --git a/bindings/nodejs/tests/wallet/wallet.spec.ts b/bindings/nodejs/tests/wallet/wallet.spec.ts index 09e1d48784..427d374559 100644 --- a/bindings/nodejs/tests/wallet/wallet.spec.ts +++ b/bindings/nodejs/tests/wallet/wallet.spec.ts @@ -4,119 +4,93 @@ import 'reflect-metadata'; import { describe, it, expect } from '@jest/globals'; -import { Wallet, CoinType, WalletOptions } from '../../lib/'; +import { Wallet, CoinType, WalletOptions, SecretManager } from '../../lib/'; describe('Wallet', () => { - it('create account', async () => { - let storagePath = 'test-create-account'; + it('create wallet', async () => { + let storagePath = 'test-create-wallet'; removeDir(storagePath); - const walletOptions = { - storagePath: './test-create-account', - clientOptions: { - nodes: ['https://api.testnet.shimmer.network'], - }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: `./${storagePath}/wallet.stronghold`, - password: `A12345678*`, - }, + const strongholdSecretManager = { + stronghold: { + snapshotPath: `./${storagePath}/wallet.stronghold`, + password: `A12345678*`, }, }; - const wallet = new Wallet(walletOptions); - await wallet.storeMnemonic( - 'vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim', - ); - - const account = await wallet.createAccount({ - alias: 'Alice', - }); - - expect(account.getMetadata().index).toStrictEqual(0); + const secretManager = new SecretManager(strongholdSecretManager); - await wallet.destroy() - removeDir(storagePath) - }, 8000); + await secretManager.storeMnemonic('vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim',); - it('generate address', async () => { - let storagePath = 'test-generate-address'; - removeDir(storagePath); + const wallet_address = await secretManager.generateEd25519Addresses({ + coinType: CoinType.IOTA, + accountIndex: 0, + range: { + start: 0, + end: 1, + }, + bech32Hrp: 'tst', + }); const walletOptions: WalletOptions = { - storagePath, + address: wallet_address[0], + storagePath: './test-create-wallet', clientOptions: { nodes: ['https://api.testnet.shimmer.network'], }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: `./${storagePath}/wallet.stronghold`, - password: `A12345678*`, - }, + bipPath: { + coinType: CoinType.IOTA, }, + secretManager: strongholdSecretManager, }; + const wallet = new Wallet(walletOptions); - await wallet.storeMnemonic( - 'vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim', - ); - - const address = await wallet.generateEd25519Address( - 0, - 0, - { internal: false, ledgerNanoPrompt: false }, - 'rms', - ); - - expect(address).toStrictEqual( - 'rms1qpqzgvcehafmlxh87zrf9w8ck8q2kw5070ztf68ylhzk89en9a4fy5jqrg8', - ); - - const anotherAddress = await wallet.generateEd25519Address( - 10, - 10, - { internal: true, ledgerNanoPrompt: false }, - 'tst', - ); - - expect(anotherAddress).toStrictEqual( - 'tst1qzp37j45rkfmqn05fapq66vyw0vkmz5zqhmeuey5fked0wt4ry43jeqp2wv', - ); await wallet.destroy() removeDir(storagePath) }, 8000); + it('recreate wallet', async () => { let storagePath = 'test-recreate-wallet'; removeDir(storagePath); - const walletOptions = { - storagePath, + const strongholdSecretManager = { + stronghold: { + snapshotPath: `./${storagePath}/wallet.stronghold`, + password: `A12345678*`, + }, + }; + + const secretManager = new SecretManager(strongholdSecretManager); + + await secretManager.storeMnemonic('vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim',); + + 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: './test-recreate-wallet', clientOptions: { nodes: ['https://api.testnet.shimmer.network'], }, - coinType: CoinType.Shimmer, - secretManager: { - stronghold: { - snapshotPath: `./${storagePath}/wallet.stronghold`, - password: `A12345678*`, - }, + bipPath: { + coinType: CoinType.IOTA, }, + secretManager: strongholdSecretManager, }; - const wallet = new Wallet(walletOptions); - await wallet.storeMnemonic( - 'vital give early extra blind skin eight discover scissors there globe deal goat fat load robot return rate fragile recycle select live ordinary claim', - ); - const account = await wallet.createAccount({ - alias: 'Alice', - }); - - expect(account.getMetadata().index).toStrictEqual(0); + const wallet = new Wallet(walletOptions); const client = await wallet.getClient(); const hrp = await client.getBech32Hrp(); @@ -124,9 +98,7 @@ describe('Wallet', () => { await wallet.destroy(); - const recreatedWallet = new Wallet(walletOptions); - const accounts = await recreatedWallet.getAccounts(); - expect(accounts.length).toStrictEqual(1); + const recreatedWallet = new Wallet({ storagePath: './test-recreate-wallet' }); await recreatedWallet.destroy() removeDir(storagePath) diff --git a/bindings/python/examples/exchange/1_create_account.py b/bindings/python/examples/exchange/1_create_account.py index 6146522fe0..d870accd6d 100644 --- a/bindings/python/examples/exchange/1_create_account.py +++ b/bindings/python/examples/exchange/1_create_account.py @@ -1,7 +1,7 @@ # Copyright 2023 IOTA Stiftung # SPDX-License-Identifier: Apache-2.0 -# This example creates a new database and account. +# This example creates a new database and wallet. import os diff --git a/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py b/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py index 6bf89a6d0c..261446ff26 100644 --- a/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py +++ b/bindings/python/examples/how_tos/accounts_and_addresses/consolidate_outputs.py @@ -27,7 +27,7 @@ # List unspent outputs before consolidation. # The output we created with example `request_funds` and the basic output from `mint` have only one # unlock condition and it is an `AddressUnlockCondition`, and so they are valid for consolidation. They have the -# same `AddressUnlockCondition`(the first address of the account), so they will be consolidated into one +# same `AddressUnlockCondition`(the address of the wallet), so they will be consolidated into one # output. outputs = account.unspent_outputs() print('Outputs BEFORE consolidation:') diff --git a/bindings/python/examples/how_tos/accounts_and_addresses/create_account.py b/bindings/python/examples/how_tos/accounts_and_addresses/create_account.py index 55dbc1abb0..09758a6c23 100644 --- a/bindings/python/examples/how_tos/accounts_and_addresses/create_account.py +++ b/bindings/python/examples/how_tos/accounts_and_addresses/create_account.py @@ -6,7 +6,7 @@ load_dotenv() -# This example creates a new database and account +# This example creates a new database and wallet node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = ClientOptions(nodes=[node_url]) diff --git a/bindings/python/examples/wallet/backup.py b/bindings/python/examples/wallet/backup.py index b34ad45433..b5302bb3df 100644 --- a/bindings/python/examples/wallet/backup.py +++ b/bindings/python/examples/wallet/backup.py @@ -6,7 +6,7 @@ load_dotenv() -# This example creates a new database and account +# This example creates a new database and wallet node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = ClientOptions(nodes=[node_url]) diff --git a/bindings/python/examples/wallet/logger.py b/bindings/python/examples/wallet/logger.py index c10b2aa806..30c0019a14 100644 --- a/bindings/python/examples/wallet/logger.py +++ b/bindings/python/examples/wallet/logger.py @@ -9,7 +9,7 @@ load_dotenv() -# This example creates a new database and account and write debug logs in +# This example creates a new database and wallet and write debug logs in # `wallet.log`. log_config = { diff --git a/bindings/python/iota_sdk/types/send_params.py b/bindings/python/iota_sdk/types/send_params.py index cd3e404472..c8f47f5e0d 100644 --- a/bindings/python/iota_sdk/types/send_params.py +++ b/bindings/python/iota_sdk/types/send_params.py @@ -89,7 +89,7 @@ class MintNftParams(): """Parameters for minting NFTs. Attributes: - address: A Bech32 encoded address to which the NFT will be minted. Default will use the first address of the account. + address: A Bech32 encoded address to which the NFT will be minted. Default will use the address of the wallet. sender: An NFT sender feature. metadata: An NFT metadata feature. tag: An NFT tag feature. @@ -110,7 +110,7 @@ class CreateAccountOutputParams(): """Parameters for creating accounts. Attributes: - address: A Bech32 encoded address which will control the account. Default will use the first address of the account. + address: A Bech32 encoded address which will control the account. Default will use the address of the wallet. immutable_metadata: Immutable account metadata. metadata: Account metadata. """ diff --git a/bindings/wasm/examples/node.js b/bindings/wasm/examples/node.js index 6a779abebb..57489f2303 100644 --- a/bindings/wasm/examples/node.js +++ b/bindings/wasm/examples/node.js @@ -3,7 +3,7 @@ const console = require('console'); const fs = require('fs'); -const { Wallet, CoinType, initLogger } = require('../node/lib'); +const { Wallet, CoinType, initLogger, SecretManager } = require('../node/lib'); async function run() { try { @@ -19,27 +19,36 @@ async function run() { colorEnabled: true, }); + const mnemonicSecretManager = { + mnemonic: + 'inhale gorilla deny three celery song category owner lottery rent author wealth penalty crawl hobby obtain glad warm early rain clutch slab august bleak', + }; + + const secretManager = new SecretManager(mnemonicSecretManager); + + const walletAddress = await secretManager.generateEd25519Addresses({ + coinType: CoinType.IOTA, + accountIndex: 0, + range: { + start: 0, + end: 1, + }, + bech32Hrp: 'tst', + }); + const wallet = new Wallet({ + address: walletAddress[0], storagePath: './alice-database', - coinType: CoinType.Shimmer, + bipPath: { + coinType: CoinType.IOTA, + }, clientOptions: { nodes: ['https://api.testnet.shimmer.network'], }, - secretManager: { - mnemonic: - 'inhale gorilla deny three celery song category owner lottery rent author wealth penalty crawl hobby obtain glad warm early rain clutch slab august bleak', - }, + secretManager: mnemonicSecretManager, }); - const account = await wallet.createAccount({ - alias: 'Alice', - bech32Hrp: 'rms', - }); - - console.log('Account created:', account); - account.setAlias('new alias'); - - const balance = await account.sync(); + const balance = await wallet.sync(); console.log(balance); } diff --git a/bindings/wasm/test/account.spec.ts b/bindings/wasm/test/account.spec.ts index 37ac3ef02b..56a206d467 100644 --- a/bindings/wasm/test/account.spec.ts +++ b/bindings/wasm/test/account.spec.ts @@ -29,7 +29,7 @@ async function run() { expect(account.getMetadata().alias).toBe('Alice'); - const balance: Balance = await account.sync(); + const balance: Balance = await wallet.sync(); expect(balance.baseCoin.available).not.toBeNaN(); await account.setAlias('new alias'); diff --git a/sdk/src/wallet/operations/transaction/high_level/minting/mint_nfts.rs b/sdk/src/wallet/operations/transaction/high_level/minting/mint_nfts.rs index b5eb4a945e..3bef7fc38d 100644 --- a/sdk/src/wallet/operations/transaction/high_level/minting/mint_nfts.rs +++ b/sdk/src/wallet/operations/transaction/high_level/minting/mint_nfts.rs @@ -26,7 +26,7 @@ use crate::{ #[serde(rename_all = "camelCase")] pub struct MintNftParams { /// Bech32 encoded address to which the NFT will be minted. Default will use the - /// first address of the account. + /// address of the wallet. #[getset(get = "pub")] address: Option, /// NFT sender feature. diff --git a/sdk/src/wallet/operations/transaction/high_level/send.rs b/sdk/src/wallet/operations/transaction/high_level/send.rs index dadc527e4b..e1e36f24b7 100644 --- a/sdk/src/wallet/operations/transaction/high_level/send.rs +++ b/sdk/src/wallet/operations/transaction/high_level/send.rs @@ -36,7 +36,7 @@ pub struct SendParams { address: Bech32Address, /// Bech32 encoded return address, to which the storage deposit will be returned if one is necessary /// given the provided amount. If a storage deposit is needed and a return address is not provided, it will - /// default to the first address of the account. + /// default to the address of the wallet. #[getset(get = "pub")] return_address: Option, /// Expiration in slot indices, after which the output will be available for the sender again, if not spent by the diff --git a/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs b/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs index 3f77ad9e19..c487b043a9 100644 --- a/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs +++ b/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs @@ -36,7 +36,7 @@ pub struct SendNativeTokensParams { #[getset(get = "pub")] native_tokens: Vec<(TokenId, U256)>, /// Bech32 encoded address return address, to which the storage deposit will be returned. Default will use the - /// first address of the account + /// address of the wallet. #[getset(get = "pub")] return_address: Option, /// Expiration in slot indices, after which the output will be available for the sender again, if not spent by the