-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add implicit account examples and fix some Python types (#2193)
* Add implicit account examples and fix some Python types * Use const * Clean docs * Rename to implicit_account_creation_address --------- Co-authored-by: Thibault Martinez <[email protected]> Co-authored-by: DaughterOfMars <[email protected]>
- Loading branch information
1 parent
fe407ff
commit 754eaee
Showing
10 changed files
with
158 additions
and
36 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
bindings/nodejs/examples/how_tos/account_output/implicit-account-creation-address.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2024 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. | ||
// | ||
// Make sure that `example.stronghold` and `example.walletdb` already exist by | ||
// running the `how_tos/wallet/create-wallet` example! | ||
// | ||
require('dotenv').config({ path: '.env' }); | ||
|
||
// Run with command: | ||
// yarn run-example ./how_tos/account_output/implicit-account-creation-address.ts | ||
|
||
// In this example we create an implicit account creation address. | ||
async function run() { | ||
initLogger(); | ||
for (const envVar of ['WALLET_DB_PATH']) { | ||
if (!(envVar in process.env)) { | ||
throw new Error(`.env ${envVar} is undefined, see .env.example`); | ||
} | ||
} | ||
|
||
try { | ||
// Create the wallet | ||
const wallet = await Wallet.create({ | ||
storagePath: process.env.WALLET_DB_PATH, | ||
}); | ||
|
||
// Get the implicit account address. | ||
const implicitAccountCreationAddress = | ||
await wallet.implicitAccountCreationAddress(); | ||
console.log( | ||
`Implicit account creation address: ${implicitAccountCreationAddress}`, | ||
); | ||
} catch (error) { | ||
console.log('Error: ', error); | ||
} | ||
process.exit(0); | ||
} | ||
|
||
void run().then(() => process.exit()); |
70 changes: 70 additions & 0 deletions
70
bindings/nodejs/examples/how_tos/account_output/implicit-account-transition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2024 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. | ||
// | ||
// Make sure that `example.stronghold` and `example.walletdb` already exist by | ||
// running the `how_tos/wallet/create-wallet` example! | ||
// | ||
require('dotenv').config({ path: '.env' }); | ||
|
||
// Run with command: | ||
// yarn run-example ./how_tos/account_output/implicit-account-transition.ts | ||
|
||
// In this example we transition an implicit account to an account. | ||
async function run() { | ||
initLogger(); | ||
for (const envVar of [ | ||
'WALLET_DB_PATH', | ||
'STRONGHOLD_PASSWORD', | ||
'EXPLORER_URL', | ||
]) { | ||
if (!(envVar in process.env)) { | ||
throw new Error(`.env ${envVar} is undefined, see .env.example`); | ||
} | ||
} | ||
|
||
try { | ||
// Create the wallet | ||
const wallet = await Wallet.create({ | ||
storagePath: process.env.WALLET_DB_PATH, | ||
}); | ||
|
||
// Need to sync the wallet with implicit accounts option enabled. | ||
let balance = await wallet.sync({ syncImplicitAccounts: true }); | ||
|
||
const implicitAccounts = await wallet.implicitAccounts(); | ||
if (implicitAccounts.length == 0) { | ||
throw new Error(`No implicit account available`); | ||
} | ||
|
||
// To sign a transaction we need to unlock stronghold. | ||
await wallet.setStrongholdPassword( | ||
process.env.STRONGHOLD_PASSWORD as string, | ||
); | ||
|
||
console.log('Sending the transition transaction...'); | ||
|
||
// Transition to the account output. | ||
const transaction = await wallet.implicitAccountTransition( | ||
implicitAccounts[0].outputId, | ||
); | ||
|
||
console.log(`Transaction sent: ${transaction.transactionId}`); | ||
|
||
await wallet.waitForTransactionAcceptance(transaction.transactionId); | ||
console.log( | ||
`Tx accepted: ${process.env.EXPLORER_URL}/transactions/${transaction.transactionId}`, | ||
); | ||
|
||
balance = await wallet.sync(); | ||
console.log(`Accounts:\n`, balance.accounts); | ||
} catch (error) { | ||
console.log('Error: ', error); | ||
} | ||
process.exit(0); | ||
} | ||
|
||
void run().then(() => process.exit()); |
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
bindings/python/examples/how_tos/account_output/implicit_account_transition.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
|
||
from dotenv import load_dotenv | ||
from iota_sdk import Wallet, WalletOptions, SyncOptions | ||
|
||
# In this example we transition an implicit account to an account. | ||
|
||
load_dotenv() | ||
|
||
for env_var in ['WALLET_DB_PATH', 'STRONGHOLD_PASSWORD', 'EXPLORER_URL']: | ||
if env_var not in os.environ: | ||
raise Exception(f".env {env_var} is undefined, see .env.example") | ||
|
||
# Need to sync the wallet with implicit accounts option enabled. | ||
wallet = Wallet(WalletOptions(storage_path=os.environ.get('WALLET_DB_PATH'))) | ||
|
||
wallet.sync(SyncOptions(sync_implicit_accounts=True)) | ||
|
||
implicit_accounts = wallet.implicit_accounts() | ||
|
||
wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) | ||
|
||
# Transition to the account output. | ||
transaction = wallet.implicit_account_transition( | ||
implicit_accounts[0].output_id) | ||
print(f'Transaction sent: {transaction.transaction_id}') | ||
|
||
wallet.wait_for_transaction_acceptance(transaction.transaction_id) | ||
print( | ||
f'Tx accepted: {os.environ["EXPLORER_URL"]}/transactions/{transaction.transaction_id}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters