diff --git a/bindings/python/.env.example b/bindings/python/.env.example new file mode 100644 index 0000000000..a6d7394080 --- /dev/null +++ b/bindings/python/.env.example @@ -0,0 +1,5 @@ +NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1="endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river" +NODE_URL="https://api.testnet.shimmer.network" +FAUCET_URL="https://faucet.testnet.shimmer.network/api/enqueue" +EXPLORER_URL="https://explorer.shimmer.network/testnet" +STRONGHOLD_PASSWORD="some_hopefully_secure_password" \ No newline at end of file diff --git a/bindings/python/README.md b/bindings/python/README.md index dbe0930b05..3dda92dad2 100644 --- a/bindings/python/README.md +++ b/bindings/python/README.md @@ -4,7 +4,7 @@ Python binding to the iota-sdk library. ## Requirements -[Python 3.x](https://www.python.org) & [pip](https://pypi.org/project/pip) +[Python 3.x](https://www.python.org) & [pip ^21.x](https://pypi.org/project/pip) `Rust` and `Cargo`, to compile the binding. Install them [here](https://doc.rust-lang.org/cargo/getting-started/installation.html). diff --git a/bindings/python/examples/client/00_get_info.py b/bindings/python/examples/client/00_get_info.py index 94d56433a3..3ef8ebdb0a 100644 --- a/bindings/python/examples/client/00_get_info.py +++ b/bindings/python/examples/client/00_get_info.py @@ -1,7 +1,13 @@ from iota_sdk import Client +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Get the node info node_info = client.get_info() diff --git a/bindings/python/examples/client/02_generate_addresses.py b/bindings/python/examples/client/02_generate_addresses.py index 909e13c546..a2d57442b2 100644 --- a/bindings/python/examples/client/02_generate_addresses.py +++ b/bindings/python/examples/client/02_generate_addresses.py @@ -1,12 +1,21 @@ from iota_sdk import Client, MnemonicSecretManager, CoinType +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') + # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) -# In this example we will create addresses from a mnemonic +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) -secret_manager = MnemonicSecretManager( - "endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river") +# In this example we will create addresses from a mnemonic +secret_manager = MnemonicSecretManager(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) # Generate public address with default account index and range. addresses = client.generate_addresses(secret_manager) diff --git a/bindings/python/examples/client/03_get_address_outputs.py b/bindings/python/examples/client/03_get_address_outputs.py index 119a0e848d..a47b2d111d 100644 --- a/bindings/python/examples/client/03_get_address_outputs.py +++ b/bindings/python/examples/client/03_get_address_outputs.py @@ -1,7 +1,13 @@ from iota_sdk import Client, NodeIndexerAPI +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) query_parameters = NodeIndexerAPI.QueryParameter( address='rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy', diff --git a/bindings/python/examples/client/04_get_output.py b/bindings/python/examples/client/04_get_output.py index fd1d6f4b67..4a0ca32833 100644 --- a/bindings/python/examples/client/04_get_output.py +++ b/bindings/python/examples/client/04_get_output.py @@ -1,8 +1,16 @@ from iota_sdk import Client +from dotenv import load_dotenv + +import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Get an outputs by its id output = client.get_output('0x1e857d380f813d8035e487b6dfd2ff4740b6775273ba1b576f01381ba2a1a44c0000') -print(f'{output}') +print(json.dumps(output, indent=4)) diff --git a/bindings/python/examples/client/05_get_address_balance.py b/bindings/python/examples/client/05_get_address_balance.py index 2aa68ae350..638b7e5d2f 100644 --- a/bindings/python/examples/client/05_get_address_balance.py +++ b/bindings/python/examples/client/05_get_address_balance.py @@ -1,7 +1,13 @@ from iota_sdk import Client, NodeIndexerAPI +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) address='rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy' query_parameters = NodeIndexerAPI.QueryParameter( diff --git a/bindings/python/examples/client/06_simple_block.py b/bindings/python/examples/client/06_simple_block.py index 0df3039218..54753d5fd4 100644 --- a/bindings/python/examples/client/06_simple_block.py +++ b/bindings/python/examples/client/06_simple_block.py @@ -1,8 +1,14 @@ from iota_sdk import Client +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Create and post a block without payload block = client.build_and_post_block() -print(f'{block}') +print(f'Empty block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}') \ No newline at end of file diff --git a/bindings/python/examples/client/07_get_block_data.py b/bindings/python/examples/client/07_get_block_data.py index 4d3e60e3f1..cd4c7ff05b 100644 --- a/bindings/python/examples/client/07_get_block_data.py +++ b/bindings/python/examples/client/07_get_block_data.py @@ -1,16 +1,23 @@ from iota_sdk import Client +from dotenv import load_dotenv +import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Fetch a block ID from the node -block_id = client.get_tips()[0] -print(f'{block_id}') +block_id = client.get_tips() +print(f'Block ids: {block_id}') # Get the metadata for the block -metadata = client.get_block_metadata(block_id) -print(f'{metadata}') +metadata = client.get_block_metadata(block_id[0]) +print(f'Block metadata: {json.dumps(metadata, indent=4)}') # Request the block by its id -block = client.get_block_data(block_id) -print(f'{block}') +block = client.get_block_data(block_id[0]) +print(f'Block: {json.dumps(block, indent=4)}') \ No newline at end of file diff --git a/bindings/python/examples/client/08_data_block.py b/bindings/python/examples/client/08_data_block.py index bb5ece8ab9..1b57b74ac6 100644 --- a/bindings/python/examples/client/08_data_block.py +++ b/bindings/python/examples/client/08_data_block.py @@ -1,9 +1,25 @@ -from iota_sdk import Client, utf8_to_hex +from iota_sdk import Client, utf8_to_hex, hex_to_utf8 +from dotenv import load_dotenv +import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Create and post a block with a tagged data payload block = client.build_and_post_block( tag=utf8_to_hex('hello'), data=utf8_to_hex('hello')) -print(f'{block}') + +print(f'Data block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}') + +block = client.get_block_data(block[0]) +print(f'Block data: {json.dumps(block, indent=4)}') + +payload = block['payload'] + +if payload and 'data' in payload and payload['data']: + print(f'Decoded data: { hex_to_utf8(payload["data"]) }') diff --git a/bindings/python/examples/client/09_transaction.py b/bindings/python/examples/client/09_transaction.py index b6be5f7fff..1fe10f27c4 100644 --- a/bindings/python/examples/client/09_transaction.py +++ b/bindings/python/examples/client/09_transaction.py @@ -1,18 +1,25 @@ -from iota_sdk import Client, MnemonicSecretManager +from iota_sdk import Client, MnemonicSecretManager, Ed25519Address, Utils, AddressWithAmount +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) + +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) -secret_manager = MnemonicSecretManager("flame fever pig forward exact dash body idea link scrub tennis minute " + - "surge unaware prosper over waste kitten ceiling human knife arch situate civil") +secret_manager = MnemonicSecretManager(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) -options = { - "output": { - "address": 'rms1qzpf0tzpf8yqej5zyhjl9k3km7y6j0xjnxxh7m2g3jtj2z5grej67sl6l46', - "amount": '1000000', - } -} +output = AddressWithAmount( + address= 'rms1qzpf0tzpf8yqej5zyhjl9k3km7y6j0xjnxxh7m2g3jtj2z5grej67sl6l46', + amount= 1000000, +) # Create and post a block with a transaction -block = client.build_and_post_block(secret_manager, options) -print(f'{block}') +block = client.build_and_post_block(secret_manager, output=output) +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}') diff --git a/bindings/python/examples/client/10_mint_nft.py b/bindings/python/examples/client/10_mint_nft.py index 556c549935..b6878d5ccd 100644 --- a/bindings/python/examples/client/10_mint_nft.py +++ b/bindings/python/examples/client/10_mint_nft.py @@ -1,10 +1,19 @@ from iota_sdk import * +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) + +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) -secret_manager = MnemonicSecretManager('flame fever pig forward exact dash body idea link scrub tennis minute ' + - 'surge unaware prosper over waste kitten ceiling human knife arch situate civil') +secret_manager = MnemonicSecretManager(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) nft_output = client.build_nft_output( unlock_conditions=[ @@ -25,4 +34,4 @@ # Create and post a block with the nft output block = client.build_and_post_block(secret_manager, outputs=[nft_output]) -print(dumps(block, indent=4)) +print(f'NFT mint block sent: {os.environ["EXPLORER_URL"]}/block/{block[0]}') diff --git a/bindings/python/examples/client/build_alias.py b/bindings/python/examples/client/build_alias.py index 96fb324fa2..78b098c171 100644 --- a/bindings/python/examples/client/build_alias.py +++ b/bindings/python/examples/client/build_alias.py @@ -1,8 +1,14 @@ from iota_sdk import * +from dotenv import load_dotenv import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) hexAddress = Utils.bech32_to_hex( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') diff --git a/bindings/python/examples/client/build_basic.py b/bindings/python/examples/client/build_basic.py index f292bdb7e6..53dea5c6f8 100644 --- a/bindings/python/examples/client/build_basic.py +++ b/bindings/python/examples/client/build_basic.py @@ -1,8 +1,14 @@ from iota_sdk import * +from dotenv import load_dotenv import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) hex_address = Utils.bech32_to_hex( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') diff --git a/bindings/python/examples/client/build_foundry.py b/bindings/python/examples/client/build_foundry.py index de12d66974..007c938ded 100644 --- a/bindings/python/examples/client/build_foundry.py +++ b/bindings/python/examples/client/build_foundry.py @@ -1,8 +1,14 @@ from iota_sdk import * +from dotenv import load_dotenv import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Configure foundry output # TODO: replace with your own values diff --git a/bindings/python/examples/client/build_nft.py b/bindings/python/examples/client/build_nft.py index 996cf28084..3c22a8f2f9 100644 --- a/bindings/python/examples/client/build_nft.py +++ b/bindings/python/examples/client/build_nft.py @@ -1,8 +1,14 @@ from iota_sdk import * +from dotenv import load_dotenv import json +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) hexAddress = Utils.bech32_to_hex( 'rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy') diff --git a/bindings/python/examples/client/get_raw_block.py b/bindings/python/examples/client/get_raw_block.py index 6c8e01c904..2f5b507d81 100644 --- a/bindings/python/examples/client/get_raw_block.py +++ b/bindings/python/examples/client/get_raw_block.py @@ -1,14 +1,20 @@ from iota_sdk import Client +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Fetch a block ID from the node block_id = client.get_tips()[0] -print(f'{block_id}') +print(f'Block id: {block_id}') # Get block raw block_raw = client.get_block_raw(block_id) # Print block raw -print(f'{block_raw}') +print(f'Block bytes: {block_raw}') diff --git a/bindings/python/examples/client/ledger_nano.py b/bindings/python/examples/client/ledger_nano.py index 41e96daaac..00c3f78e07 100644 --- a/bindings/python/examples/client/ledger_nano.py +++ b/bindings/python/examples/client/ledger_nano.py @@ -1,11 +1,17 @@ from iota_sdk import Client, LedgerNanoSecretManager, SecretManager +from dotenv import load_dotenv +import os + +load_dotenv() # In this example we will get the ledger status and generate an address # To use the ledger nano simulator clone https://github.com/iotaledger/ledger-shimmer-app, run `git submodule init && git submodule update --recursive`, # then `./build.sh -m nanos|nanox|nanosplus -s` and use `True` in `LedgerNanoSecretManager(True)`. +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') + # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) is_simulator = True diff --git a/bindings/python/examples/client/logger.py b/bindings/python/examples/client/logger.py index a4e52b183f..dbd1a3f13d 100644 --- a/bindings/python/examples/client/logger.py +++ b/bindings/python/examples/client/logger.py @@ -1,6 +1,10 @@ from iota_sdk import Client, init_logger +from dotenv import load_dotenv +import os import json +load_dotenv() + # Create the log configuration, the log will be outputted in 'iota.rs.log' log_config = { 'name': 'iota.rs.log', @@ -12,8 +16,10 @@ init_logger(log_config_str) +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') + # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Get the node info node_info = client.get_info() diff --git a/bindings/python/examples/client/post_raw_block.py b/bindings/python/examples/client/post_raw_block.py index 878e0bbe2e..102bb33234 100644 --- a/bindings/python/examples/client/post_raw_block.py +++ b/bindings/python/examples/client/post_raw_block.py @@ -1,7 +1,13 @@ from iota_sdk import Client, MnemonicSecretManager +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) # Create and post a block without payload block_id = client.build_and_post_block()[0] @@ -11,4 +17,4 @@ result = client.post_block_raw(blockBytes) # Print block raw -print(f'{result}') +print(f'Posted raw block: {os.environ["EXPLORER_URL"]}/block/{result}') diff --git a/bindings/python/examples/client/stronghold.py b/bindings/python/examples/client/stronghold.py index 2eb480a69e..040598018d 100644 --- a/bindings/python/examples/client/stronghold.py +++ b/bindings/python/examples/client/stronghold.py @@ -1,15 +1,28 @@ from iota_sdk import Client, StrongholdSecretManager, SecretManager +from dotenv import load_dotenv +import os + +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # Create a Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +client = Client(nodes=[node_url]) + +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) + +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) secret_manager = StrongholdSecretManager( - "client.stronghold", "some_hopefully_secure_password") + "client.stronghold", os.environ['STRONGHOLD_PASSWORD']) # 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! -result = SecretManager(secret_manager).store_mnemonic("flame fever pig forward exact dash body idea link scrub tennis minute " + - "surge unaware prosper over waste kitten ceiling human knife arch situate civil") +result = SecretManager(secret_manager).store_mnemonic(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) # Generate public address with custom account index and range. address = client.generate_addresses( diff --git a/bindings/python/examples/client/submit_and_read_block.py b/bindings/python/examples/client/submit_and_read_block.py index b5c33256ec..2d94e01f94 100644 --- a/bindings/python/examples/client/submit_and_read_block.py +++ b/bindings/python/examples/client/submit_and_read_block.py @@ -9,9 +9,15 @@ # Import the python iota client # Make sure you have first installed it with `pip install iota_sdk` from iota_sdk import Client, hex_to_utf8, utf8_to_hex +from dotenv import load_dotenv +import os -# Create an Client instance -client = Client(nodes=['https://api.testnet.shimmer.network']) +load_dotenv() + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') + +# Create a Client instance +client = Client(nodes=[node_url]) ######################################################## @@ -84,6 +90,4 @@ print(f' {message_out}') # Or see the message online, with the testnet explorer -explorer_url = 'https://explorer.iota.org/testnet/block/'+block_id -print(f'\nOr see the message with the testnet explorer:') -print(f' {explorer_url}') +print(f'\nOr see the message with the testnet explorer: {os.environ["EXPLORER_URL"]}/block/{block_id}') diff --git a/bindings/python/examples/secret_manager/generate_addresses.py b/bindings/python/examples/secret_manager/generate_addresses.py index cf0b5f0a3e..05bf7a1a2c 100644 --- a/bindings/python/examples/secret_manager/generate_addresses.py +++ b/bindings/python/examples/secret_manager/generate_addresses.py @@ -1,8 +1,16 @@ from iota_sdk import MnemonicSecretManager, CoinType, SecretManager +from dotenv import load_dotenv +import os + +load_dotenv() + +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) # In this example we will create addresses from a mnemonic -secret_manager = SecretManager(MnemonicSecretManager("endorse answer radar about source reunion marriage tag sausage weekend frost daring base attack because joke dream slender leisure group reason prepare broken river")) +secret_manager = SecretManager(MnemonicSecretManager(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1'])) # Generate public address with default account index and range. addresses = secret_manager.generate_addresses() diff --git a/bindings/python/examples/wallet/0-create-account.py b/bindings/python/examples/wallet/0-create-account.py index 46d7a57b41..d6e47b85c7 100644 --- a/bindings/python/examples/wallet/0-create-account.py +++ b/bindings/python/examples/wallet/0-create-account.py @@ -1,21 +1,35 @@ -from iota_sdk import Wallet, StrongholdSecretManager +from iota_sdk import Wallet, StrongholdSecretManager, CoinType +from dotenv import load_dotenv +import json +import os + +load_dotenv() # This example creates a new database and account +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = { - 'nodes': ['https://api.testnet.shimmer.network'], + 'nodes': [node_url], } # Shimmer coin type -coin_type = 4219 +coin_type = CoinType.SHIMMER + +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) -secret_manager = StrongholdSecretManager("wallet.stronghold", "some_hopefully_secure_password") +secret_manager = StrongholdSecretManager( + "wallet.stronghold", os.environ['STRONGHOLD_PASSWORD']) wallet = Wallet('./alice-database', client_options, coin_type, secret_manager) +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) + # Store the mnemonic in the Stronghold snapshot, this only needs to be done once -account = wallet.store_mnemonic("flame fever pig forward exact dash body idea link scrub tennis minute " + - "surge unaware prosper over waste kitten ceiling human knife arch situate civil") +account = wallet.store_mnemonic(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) account = wallet.create_account('Alice') -print(account) +print(json.dumps(account, indent=4)) diff --git a/bindings/python/examples/wallet/1-generate-address.py b/bindings/python/examples/wallet/1-generate-address.py index a96d41ae31..f706e24088 100644 --- a/bindings/python/examples/wallet/1-generate-address.py +++ b/bindings/python/examples/wallet/1-generate-address.py @@ -1,14 +1,23 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import json +import os + +load_dotenv() # This example generates a new address. wallet = Wallet('./alice-database') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) account = wallet.get_account('Alice') address = account.generate_addresses(1) # address = account.generate_addresses( # 1, {'internal': True, 'metadata': {'syncing': True, 'network': 'Testnet'}}) -print(f'Address: {address}') +print(f'Address: {json.dumps(address, indent=4)}') diff --git a/bindings/python/examples/wallet/10-claim-outputs.py b/bindings/python/examples/wallet/10-claim-outputs.py index bb9216e51b..0c2f84e1f8 100644 --- a/bindings/python/examples/wallet/10-claim-outputs.py +++ b/bindings/python/examples/wallet/10-claim-outputs.py @@ -1,15 +1,20 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will claim outputs that have additional unlock conditions as expiration or storage deposit return +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will claim outputs that have additional unlock conditions as expiration or storage deposit return wallet = Wallet('./alice-database') account = wallet.get_account('Alice') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # Sync account with the node response = account.sync() @@ -20,6 +25,5 @@ print(f'Available outputs to claim: {output_ids}') transaction = account.claim_outputs(output_ids) +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); diff --git a/bindings/python/examples/wallet/11-list-transactions.py b/bindings/python/examples/wallet/11-list-transactions.py index a14c2a21dc..3f3e70531c 100644 --- a/bindings/python/examples/wallet/11-list-transactions.py +++ b/bindings/python/examples/wallet/11-list-transactions.py @@ -1,4 +1,8 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os + +load_dotenv() # In this example we will list the sent transactions @@ -6,7 +10,11 @@ account = wallet.get_account('Alice') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # All transactions sent from the the account transactions = account.transactions() diff --git a/bindings/python/examples/wallet/12-prepare_output.py b/bindings/python/examples/wallet/12-prepare_output.py index d50e7fc557..33d6e2ddc4 100644 --- a/bindings/python/examples/wallet/12-prepare_output.py +++ b/bindings/python/examples/wallet/12-prepare_output.py @@ -1,4 +1,9 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import json +import os + +load_dotenv() # In this example we will prepare an output with an address and expiration unlock condition and send it @@ -6,7 +11,11 @@ account = wallet.get_account("Alice") -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # using prepare_output output = account.prepare_output( @@ -19,34 +28,9 @@ }, } ) -print(f"Output: {output}") - -# using build_basic_output -output = account.build_basic_output( - amount="1000000", - native_tokens=[], - unlock_conditions=[ - { - "type": 0, - "address": { - "type": 0, - "pubKeyHash": "0x47c5f5b6af30518757f3afe86717aaa1f78aaf12c2821103a2d2fc4e92182174", - }, - }, - { - "type": 3, - "returnAddress": { - "type": 0, - "pubKeyHash": "0x8297ac4149c80cca8225e5f2da36df89a93cd2998d7f6d488c97250a881e65af", - }, - "unixTime": 1676570528, - }, - ], - features=[], -) -print(f"Output: {output}") +print(f"Output: {json.dumps(output, indent=4)}") account.sync() transaction = account.send_outputs([output]) -print(f'Sent transaction: {transaction}') +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/13-check-unlock-conditions.py b/bindings/python/examples/wallet/13-check-unlock-conditions.py index 63be260780..9caaeccc64 100644 --- a/bindings/python/examples/wallet/13-check-unlock-conditions.py +++ b/bindings/python/examples/wallet/13-check-unlock-conditions.py @@ -1,4 +1,8 @@ -from iota_sdk import Wallet +from iota_sdk import Wallet, Utils +from dotenv import load_dotenv +import os + +load_dotenv() # In this example we check if an output has only an address unlock condition and that the address is from the account. @@ -6,7 +10,11 @@ account = wallet.get_account("Alice") -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) accountAddresses = account.addresses() @@ -19,7 +27,7 @@ ) def hexAddress(address): - return wallet.bech32_to_hex(address['address']) + return Utils.bech32_to_hex(address['address']) hexEncodedAccountAddresses = map(hexAddress, accountAddresses) diff --git a/bindings/python/examples/wallet/2-check-balance.py b/bindings/python/examples/wallet/2-check-balance.py index b32a5cea06..6b5c4a2e8d 100644 --- a/bindings/python/examples/wallet/2-check-balance.py +++ b/bindings/python/examples/wallet/2-check-balance.py @@ -9,10 +9,12 @@ # Sync account with the node response = account.sync() print(f'Synced: {response}') +print() # Just calculate the balance with the known state balance = account.get_balance() print(f'Balance: {balance}') +print() print('Addresses:') for address in account.addresses(): diff --git a/bindings/python/examples/wallet/3-send-transaction.py b/bindings/python/examples/wallet/3-send-transaction.py index f41d4a144c..0de86efc42 100644 --- a/bindings/python/examples/wallet/3-send-transaction.py +++ b/bindings/python/examples/wallet/3-send-transaction.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# This example sends a transaction. +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# This example sends a transaction. wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) outputs = [{ "address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu", @@ -21,6 +25,4 @@ }] transaction = account.send_amount(outputs) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); \ No newline at end of file +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') \ No newline at end of file diff --git a/bindings/python/examples/wallet/4-send-micro-transaction.py b/bindings/python/examples/wallet/4-send-micro-transaction.py index ddf37a98c4..59f99d965c 100644 --- a/bindings/python/examples/wallet/4-send-micro-transaction.py +++ b/bindings/python/examples/wallet/4-send-micro-transaction.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will send an amount below the minimum storage deposit +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will send an amount below the minimum storage deposit wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) outputs = [{ "address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu", @@ -21,6 +25,4 @@ }] transaction = account.send_amount(outputs, { "allowMicroAmount": True }) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); \ No newline at end of file +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') \ No newline at end of file diff --git a/bindings/python/examples/wallet/5-send-native-tokens.py b/bindings/python/examples/wallet/5-send-native-tokens.py index 5aa3dc6fd8..a2f12a2cbd 100644 --- a/bindings/python/examples/wallet/5-send-native-tokens.py +++ b/bindings/python/examples/wallet/5-send-native-tokens.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will send native tokens +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will send native tokens wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) outputs = [{ "address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu", @@ -25,6 +29,4 @@ }]; transaction = account.send_native_tokens(outputs, None) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/6-send-nft.py b/bindings/python/examples/wallet/6-send-nft.py index 3c2db394ec..4b8983c9ef 100644 --- a/bindings/python/examples/wallet/6-send-nft.py +++ b/bindings/python/examples/wallet/6-send-nft.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will send an nft +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will send an nft wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) outputs = [{ "address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu", @@ -22,5 +26,4 @@ transaction = account.send_nft(outputs) -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/7-mint-native-tokens.py b/bindings/python/examples/wallet/7-mint-native-tokens.py index 6c967867ea..e82a6d9e0a 100644 --- a/bindings/python/examples/wallet/7-mint-native-tokens.py +++ b/bindings/python/examples/wallet/7-mint-native-tokens.py @@ -1,20 +1,24 @@ from iota_sdk import Wallet +from dotenv import load_dotenv import time +import os -# In this example we will mint native tokens +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will mint native tokens wallet = Wallet('./alice-database') account = wallet.get_account('Alice') +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) + # Sync account with the node response = account.sync() -print(f'Synced: {response}') - -wallet.set_stronghold_password("some_hopefully_secure_password") transaction = account.create_alias_output(None, None) @@ -31,6 +35,4 @@ } transaction = account.mint_native_token(native_token_options, None) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["transaction"]["blockId"]}') diff --git a/bindings/python/examples/wallet/8-mint-nft.py b/bindings/python/examples/wallet/8-mint-nft.py index 733e51760d..3554b86ffc 100644 --- a/bindings/python/examples/wallet/8-mint-nft.py +++ b/bindings/python/examples/wallet/8-mint-nft.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet, utf8_to_hex +from dotenv import load_dotenv +import os -# In this example we will mint an nft +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will mint an nft wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) outputs = [{ "immutableMetadata": utf8_to_hex("some immutable nft metadata"), @@ -21,5 +25,4 @@ transaction = account.mint_nfts(outputs) -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/backup.py b/bindings/python/examples/wallet/backup.py index a2ce1945ee..371a578bd5 100644 --- a/bindings/python/examples/wallet/backup.py +++ b/bindings/python/examples/wallet/backup.py @@ -1,25 +1,37 @@ -from iota_sdk import Wallet, StrongholdSecretManager +from iota_sdk import Wallet, StrongholdSecretManager, CoinType +from dotenv import load_dotenv +import os -# This example creates an account and then a stronghold backup. +load_dotenv() +# This example creates a new database and account + +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = { - 'nodes': ['https://api.testnet.shimmer.network'], + 'nodes': [node_url], } # Shimmer coin type -coin_type = 4219 +coin_type = CoinType.SHIMMER + +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) secret_manager = StrongholdSecretManager( - "wallet.stronghold", "some_hopefully_secure_password") + "wallet.stronghold", os.environ['STRONGHOLD_PASSWORD']) wallet = Wallet('./backup-database', client_options, coin_type, secret_manager) +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) + # Store the mnemonic in the Stronghold snapshot, this only needs to be done once -account = wallet.store_mnemonic("float thumb deer talent charge shrug mixed often desert caution mean roof" + - " game shed hint victory opinion verb cloud area pony yellow since motion") +account = wallet.store_mnemonic(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) accounts = wallet.create_account('Alice') -wallet.backup("backup.stronghold", "some_hopefully_secure_password") +wallet.backup("backup.stronghold", os.environ['STRONGHOLD_PASSWORD']) print(f'Created backup') diff --git a/bindings/python/examples/wallet/burn_native_tokens.py b/bindings/python/examples/wallet/burn_native_tokens.py index d091831341..b71f617073 100644 --- a/bindings/python/examples/wallet/burn_native_tokens.py +++ b/bindings/python/examples/wallet/burn_native_tokens.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will burn native tokens +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will burn native tokens wallet = Wallet('./alice-database') @@ -11,9 +12,12 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # TODO: replace with your own values. token_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0300000000" @@ -21,6 +25,4 @@ # Send transaction. transaction = account.burn_native_token(token_id, burn_amount) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/burn_nft.py b/bindings/python/examples/wallet/burn_nft.py index 0e9e7f5955..29b501f8bf 100644 --- a/bindings/python/examples/wallet/burn_nft.py +++ b/bindings/python/examples/wallet/burn_nft.py @@ -1,25 +1,27 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will burn an NFT - -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +load_dotenv() +# In this example we will burn an NFT wallet = Wallet('./alice-database') account = wallet.get_account('Alice') # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # TODO: replace with your own values. nftId = "0xf95f4d5344217a2ba19a6c19a47f97d267edf8c4d76a7b8c08072ad35acbebbe" # Send transaction. transaction = account.burn_nft(nftId) +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); diff --git a/bindings/python/examples/wallet/create_alias.py b/bindings/python/examples/wallet/create_alias.py index ccc2a35634..0885699d78 100644 --- a/bindings/python/examples/wallet/create_alias.py +++ b/bindings/python/examples/wallet/create_alias.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will create an alias ouput +load_dotenv() -# TODO: replace with your own values. -WALLET_PASSWORD = "some_hopefully_secure_password" +# In this example we will create an alias ouput wallet = Wallet('./alice-database') @@ -12,8 +13,12 @@ # Sync account with the node account.sync() -wallet.set_stronghold_password(WALLET_PASSWORD) +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # Send transaction. transaction = account.create_alias_output(None, None) -print(transaction) +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/decrease-native-tokens-supply.py b/bindings/python/examples/wallet/decrease-native-tokens-supply.py index c0d847373e..280225177e 100644 --- a/bindings/python/examples/wallet/decrease-native-tokens-supply.py +++ b/bindings/python/examples/wallet/decrease-native-tokens-supply.py @@ -1,19 +1,23 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will decrease the native token supply +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will decrease the native token supply wallet = Wallet('./alice-database') account = wallet.get_account('Alice') # Sync account with the node -response = account.sync() -print(f'Synced: {response}') +account.sync() -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # TODO: replace with your own values. token_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000" @@ -21,6 +25,4 @@ # Send transaction. transaction = account.decrease_native_token_supply(token_id, melt_amount) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/destroy_alias.py b/bindings/python/examples/wallet/destroy_alias.py index be0acc0b24..47e16f7382 100644 --- a/bindings/python/examples/wallet/destroy_alias.py +++ b/bindings/python/examples/wallet/destroy_alias.py @@ -1,10 +1,13 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os + +load_dotenv() # In this example we will destroy an alias output # TODO: replace with your own values. ALIAS_ID = "0x982667c59ade8ab8a99188f4de38c68b97fc2ca7ba28a1e9d8d683996247e152" -WALLET_PASSWORD = "some_hopefully_secure_password" wallet = Wallet('./alice-database') @@ -13,8 +16,12 @@ # Sync account with the node response = account.sync() -wallet.set_stronghold_password(WALLET_PASSWORD) +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # Send transaction. transaction = account.destroy_alias(ALIAS_ID) -print(transaction) +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/destroy_foundry.py b/bindings/python/examples/wallet/destroy_foundry.py index 5f03c0e81a..f6c0a773b9 100644 --- a/bindings/python/examples/wallet/destroy_foundry.py +++ b/bindings/python/examples/wallet/destroy_foundry.py @@ -1,9 +1,10 @@ from iota_sdk import Wallet +from dotenv import load_dotenv +import os -# In this example we will destroy a foundry +load_dotenv() -# Explorer url -EXPLORER = "https://explorer.shimmer.network/testnet" +# In this example we will destroy a foundry wallet = Wallet('./alice-database') @@ -11,15 +12,16 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') -wallet.set_stronghold_password("some_hopefully_secure_password") +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) + +wallet.set_stronghold_password(os.environ["STRONGHOLD_PASSWORD"]) # TODO: replace with your own values. foundry_id = "0x08429fe5864378ce70699fc2d22bb144cb86a3c4833d136e3b95c5dadfd6ba0cef0500000000" # Send transaction. transaction = account.destroy_foundry(foundry_id) - -print(f'Transaction: {transaction["transactionId"]}') -print(f'Block sent: {EXPLORER}/block/" + {transaction["blockId"]}'); +print(f'Block sent: {os.environ["EXPLORER_URL"]}/block/{transaction["blockId"]}') diff --git a/bindings/python/examples/wallet/getting-started.py b/bindings/python/examples/wallet/getting-started.py index 57010d5233..d35b954a35 100644 --- a/bindings/python/examples/wallet/getting-started.py +++ b/bindings/python/examples/wallet/getting-started.py @@ -2,16 +2,21 @@ # SPDX-License-Identifier: Apache-2.0 from iota_sdk import Wallet, StrongholdSecretManager, CoinType, Utils +from dotenv import load_dotenv +import json +import os + +load_dotenv() # A name to associate with the created account. ACCOUNT_ALIAS = 'Alice' # The node to connect to. -NODE_URL = 'https://api.testnet.shimmer.network' +NODE_URL = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') # A password to encrypt the stored data. # WARNING: Never hardcode passwords in production code. -STRONGHOLD_PASSWORD = 'a-secure-password' +STRONGHOLD_PASSWORD = os.environ.get('STRONGHOLD_PASSWORD', 'a-secure-password') # The path to store the account snapshot. STRONGHOLD_SNAPSHOT_PATH = 'vault.stronghold' diff --git a/bindings/python/examples/wallet/logger.py b/bindings/python/examples/wallet/logger.py index 64b2e1e732..1d8f3381c9 100644 --- a/bindings/python/examples/wallet/logger.py +++ b/bindings/python/examples/wallet/logger.py @@ -1,5 +1,9 @@ -from iota_sdk import Wallet, StrongholdSecretManager, init_logger +from iota_sdk import Wallet, StrongholdSecretManager, init_logger, CoinType +from dotenv import load_dotenv import json +import os + +load_dotenv() # This example creates a new database and account and write debug logs in `wallet.log`. @@ -12,22 +16,29 @@ # Init the logger init_logger(json.dumps(log_config)) +NODE_URL = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = { - 'nodes': ['https://api.testnet.shimmer.network'], + 'nodes': [NODE_URL], } # Shimmer coin type -coin_type = 4219 +coin_type = CoinType.SHIMMER + +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) -secret_manager = StrongholdSecretManager( - "wallet.stronghold", "some_hopefully_secure_password") +secret_manager = StrongholdSecretManager("wallet.stronghold", os.environ["STRONGHOLD_PASSWORD"]) wallet = Wallet('./alice-database', client_options, coin_type, secret_manager) +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) + # Store the mnemonic in the Stronghold snapshot, this only needs to be done once -account = wallet.store_mnemonic("flame fever pig forward exact dash body idea link scrub tennis minute " + - "surge unaware prosper over waste kitten ceiling human knife arch situate civil") +account = wallet.store_mnemonic(os.environ["NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1"]) account = wallet.create_account('Alice') -print(account) +print(json.dumps(account, indent=4)) diff --git a/bindings/python/examples/wallet/recover_accounts.py b/bindings/python/examples/wallet/recover_accounts.py index d67fc90900..79a821537c 100644 --- a/bindings/python/examples/wallet/recover_accounts.py +++ b/bindings/python/examples/wallet/recover_accounts.py @@ -1,25 +1,37 @@ -from iota_sdk import Wallet, StrongholdSecretManager +from iota_sdk import Wallet, StrongholdSecretManager, CoinType +from dotenv import load_dotenv +import json +import os + +load_dotenv() # This example searches for accounts with unspent outputs. +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = { - 'nodes': ['https://api.testnet.shimmer.network'], + 'nodes': [node_url], } # Shimmer coin type -coin_type = 4219 +coin_type = CoinType.SHIMMER + +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) secret_manager = StrongholdSecretManager( - "wallet.stronghold", "some_hopefully_secure_password") + "wallet.stronghold", os.environ['STRONGHOLD_PASSWORD']) + +wallet = Wallet('./alice-database', client_options, coin_type, secret_manager) -wallet = Wallet('./alice-database', client_options, - coin_type, secret_manager) +if 'NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1' not in os.environ: + print(".env mnemonic is undefined, see .env.example") + sys.exit(1) # Store the mnemonic in the Stronghold snapshot, this only needs to be done once -account = wallet.store_mnemonic("flame fever pig forward exact dash body idea link scrub tennis minute " + - "surge unaware prosper over waste kitten ceiling human knife arch situate civil") +account = wallet.store_mnemonic(os.environ['NON_SECURE_USE_OF_DEVELOPMENT_MNEMONIC_1']) # Searches for unspent outputs until no ones are found for 3 accounts in a row # and checks the addresses for each account until 10 addresses in a row have nothing accounts = wallet.recover_accounts(0, 3, 10, None) -print(accounts) +print(json.dumps(accounts, indent=4)) diff --git a/bindings/python/examples/wallet/request_funds.py b/bindings/python/examples/wallet/request_funds.py index 8c59bc5ac4..d33b90f351 100644 --- a/bindings/python/examples/wallet/request_funds.py +++ b/bindings/python/examples/wallet/request_funds.py @@ -1,7 +1,11 @@ from iota_sdk import Wallet, Client - +from dotenv import load_dotenv +import json +import os import time +load_dotenv() + # This example requests funds from the faucet wallet = Wallet('./alice-database') @@ -10,16 +14,15 @@ # Sync account with the node response = account.sync() -print(f'Synced: {response}') # Balance before funding balance = account.get_balance() print( f'balance before faucet request: { balance[ "baseCoin" ][ "available" ] }') -response = Client().request_funds_from_faucet("https://faucet.testnet.shimmer.network/api/enqueue", - "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu") -print(response) +FAUCET_URL = os.environ.get('FAUCET_URL', 'https://faucet.testnet.shimmer.network/api/enqueue') +response = Client().request_funds_from_faucet(FAUCET_URL, "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu") +print(json.dumps(response, indent=4)) time.sleep(20) diff --git a/bindings/python/examples/wallet/restore_backup.py b/bindings/python/examples/wallet/restore_backup.py index 8cdfee602e..4f45803c2d 100644 --- a/bindings/python/examples/wallet/restore_backup.py +++ b/bindings/python/examples/wallet/restore_backup.py @@ -1,19 +1,28 @@ -from iota_sdk import Wallet +from iota_sdk import Wallet, CoinType +from dotenv import load_dotenv +import json +import os + +load_dotenv() # This example restores the wallet from a stronghold. +node_url = os.environ.get('NODE_URL', 'https://api.testnet.shimmer.network') client_options = { - 'nodes': ['https://api.testnet.shimmer.network'], + 'nodes': [node_url], } # Shimmer coin type -coin_type = 4219 +coin_type = CoinType.SHIMMER wallet = Wallet('./restore-backup-database', client_options, coin_type, 'Placeholder') +if 'STRONGHOLD_PASSWORD' not in os.environ: + print(".env STRONGHOLD_PASSWORD is undefined, see .env.example") + sys.exit(1) -wallet.restore_backup("backup.stronghold", "some_hopefully_secure_password") +wallet.restore_backup("backup.stronghold", os.environ['STRONGHOLD_PASSWORD']) accounts = wallet.get_accounts() -print(f'Restored accounts: {accounts}') \ No newline at end of file +print(f'Restored accounts: {json.dumps(accounts, indent=4)}') \ No newline at end of file diff --git a/bindings/python/requirements-dev.txt b/bindings/python/requirements-dev.txt index 974b555e05..a12808f784 100644 --- a/bindings/python/requirements-dev.txt +++ b/bindings/python/requirements-dev.txt @@ -1,4 +1,5 @@ pytest>=7.2.1 setuptools-rust>=1.5.2 wheel>=0.38.4 -pyhumps>=3.8.0 \ No newline at end of file +pyhumps>=3.8.0 +python-dotenv>=1.0.0 \ No newline at end of file