Skip to content

Commit

Permalink
added env vars to python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Brord van Wierst committed May 3, 2023
1 parent b47cf99 commit b28f4a5
Show file tree
Hide file tree
Showing 49 changed files with 506 additions and 232 deletions.
5 changes: 5 additions & 0 deletions bindings/python/.env.example
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion bindings/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/00_get_info.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
17 changes: 13 additions & 4 deletions bindings/python/examples/client/02_generate_addresses.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/03_get_address_outputs.py
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
12 changes: 10 additions & 2 deletions bindings/python/examples/client/04_get_output.py
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 7 additions & 1 deletion bindings/python/examples/client/05_get_address_balance.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
10 changes: 8 additions & 2 deletions bindings/python/examples/client/06_simple_block.py
Original file line number Diff line number Diff line change
@@ -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]}')
21 changes: 14 additions & 7 deletions bindings/python/examples/client/07_get_block_data.py
Original file line number Diff line number Diff line change
@@ -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)}')
22 changes: 19 additions & 3 deletions bindings/python/examples/client/08_data_block.py
Original file line number Diff line number Diff line change
@@ -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"]) }')
31 changes: 19 additions & 12 deletions bindings/python/examples/client/09_transaction.py
Original file line number Diff line number Diff line change
@@ -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]}')
17 changes: 13 additions & 4 deletions bindings/python/examples/client/10_mint_nft.py
Original file line number Diff line number Diff line change
@@ -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=[
Expand All @@ -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]}')
8 changes: 7 additions & 1 deletion bindings/python/examples/client/build_alias.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/build_basic.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/build_foundry.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/build_nft.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
12 changes: 9 additions & 3 deletions bindings/python/examples/client/get_raw_block.py
Original file line number Diff line number Diff line change
@@ -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}')
8 changes: 7 additions & 1 deletion bindings/python/examples/client/ledger_nano.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 7 additions & 1 deletion bindings/python/examples/client/logger.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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()
Expand Down
Loading

0 comments on commit b28f4a5

Please sign in to comment.