forked from d1aboilik/Python-IOTA-Chrysalis-Workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8e0904
commit 1e7014a
Showing
9 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
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,29 @@ | ||
# This example generates a new mnemonic using iota_wallet library. | ||
# This step is not necessary since in the following examples is an "example" seed provided. | ||
# However, if you want to try this workshop with the Firefly wallet, you may want to use this example file. | ||
# In that case, import the generated Mnemonic in the Firefly wallet and replace Seed values in the following exercises | ||
# with the generated Seed. | ||
|
||
# YOU SHOULD NEVER TRUST 3RD PARTY TOOLS/PROGRAMS WITH YOUR SEED/MNEMONIC! | ||
# USE THE PRIVATE KEYS GENERATED BY THIS EXAMPLE ONLY IN THIS WORKSHOP. | ||
# IN PRACTISE, NEVER STORE YOUR PRIVATE KEYS IN VARIABLES. USE ENVIROMENT VARIABLES! | ||
|
||
import binascii | ||
import hashlib | ||
import unicodedata | ||
import iota_wallet | ||
|
||
# Creates an AccountManager class from iota_wallet library to generate a mnemonic | ||
mnemonic = iota_wallet.AccountManager(storage_path='./storage').generate_mnemonic() | ||
# iota_wallet's AccountManager creates its own ./storage folder. We won't need it for this workshop | ||
# since we will be using iota_client, that uses Seed instead of Accounts stored in Stronghold. | ||
# Feel free to remove that folder if it was created BY THIS WORKSHOP (not by you or your applications) | ||
|
||
seed = hashlib.pbkdf2_hmac( | ||
"sha512", | ||
unicodedata.normalize("NFKD", mnemonic).encode("utf-8"), | ||
"mnemonic".encode("utf-8"), | ||
2048 | ||
) | ||
print(f'Generated mnemonic: {mnemonic}') | ||
print(f'Generated seed: {binascii.hexlify(seed).decode("utf-8")}') |
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,7 @@ | ||
# This example creates a new instance of the IOTA Client object and prints out | ||
# the node information of the node you are connected to. | ||
import iota_client | ||
|
||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
print(f'{client.get_info()}') |
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,13 @@ | ||
# This example creates a simple message with a custom index/data. | ||
import iota_client | ||
|
||
INDEX = "Chrysalis Python Workshop" | ||
DATA = "Hello World!".encode() | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
message_id_indexation = client.message(index=INDEX, data=DATA) | ||
|
||
print(f"Message sent!\nhttps://explorer.iota.org/chrysalis/message/{message_id_indexation['message_id']}") | ||
print("Copy the ID of your message. You'll need it in the next example.") | ||
print(f"Message ID: {message_id_indexation['message_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# This example fetches and prints a message with the given ID. | ||
import iota_client | ||
|
||
# *** Replace with the ID you got from the previous example *** | ||
MESSAGE_ID = 'fc464e422f5dcb612c4e303172d7077281aada6cfe61e3d786d34ff97dc99a64' | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
message = client.get_message_data(MESSAGE_ID) | ||
message_index = message['payload']['indexation'][0]['index'] | ||
message_content = message['payload']['indexation'][0]['data'] | ||
print(f'Message data: {message}') | ||
print(f'Message index: {bytes.fromhex(message_index).decode("utf-8")}') | ||
print(f'Message content: {bytes(message_content).decode()}') |
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,14 @@ | ||
# This example generates an unused address for the provided seed. | ||
import iota_client | ||
|
||
# *** Replace with your testing seed (In case you decided to generate one, otherwise keep this one) *** | ||
TEST_SEED = 'a201599ad3bc079378e1e3cba43ee976828c146ec95f296c7d3a4ddc7dee24f37edba7b2bf8055503babd992964e0cb3649af6f184626636741cd9d6813b8c57' | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
address = client.get_unspent_address(TEST_SEED) | ||
print(f"Generated address: {address[0]}") | ||
print("Copy your address. You'll need it in the next example.") | ||
print("Go to https://faucet.tanglekit.de/ and paste your address to receive devnet tokens. " | ||
"You'll need them in the following examples.") | ||
|
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,14 @@ | ||
# This example retreives the balance of the given address and total balance of the given seed. | ||
import iota_client | ||
|
||
# *** Replace with your testing seed (In case you decided to generate one, otherwise keep this one) *** | ||
TEST_SEED = 'a201599ad3bc079378e1e3cba43ee976828c146ec95f296c7d3a4ddc7dee24f37edba7b2bf8055503babd992964e0cb3649af6f184626636741cd9d6813b8c57' | ||
# *** Replace with your address from the previous example *** | ||
TEST_ADDRESS = 'atoi1qzr2qca680txhplug4dkyhyvgu3w7g7jeuw2jale2ht60el3u9el2v375fe' | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
address_balance = client.get_address_balances([TEST_ADDRESS]) | ||
seed_balance = client.get_balance(TEST_SEED) | ||
print(f"Address balance: {str(address_balance[0]['balance'])} IOTA") | ||
print(f"Total seed balance: {str(seed_balance)} IOTA") |
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,19 @@ | ||
# This example creates a transaction with a custom index/data. | ||
# The amount of iota specified in the "amount" variable will be transferred to the given address. | ||
import iota_client | ||
|
||
# *** Replace with your testing seed (In case you decided to generate one, otherwise keep this one) *** | ||
SENDER_SEED = 'a201599ad3bc079378e1e3cba43ee976828c146ec95f296c7d3a4ddc7dee24f37edba7b2bf8055503babd992964e0cb3649af6f184626636741cd9d6813b8c57' | ||
RECIPIENT_ADDRESS = 'atoi1qz8wn7fj23g3qz8rpk9dk38zffs6u7wmdalvu7eak8a5r7me72pw2vspcl0' | ||
INDEX = "Chrysalis Python Workshop" | ||
DATA = "Here are your IOTAs!".encode() | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
output = { | ||
'address': RECIPIENT_ADDRESS, | ||
'amount': 2000000 # 2 MIOTA | ||
} | ||
|
||
message_id = client.message(seed=SENDER_SEED, index=INDEX, data=DATA, outputs=[output]) | ||
print(f"IOTAs sent!\nhttps://explorer.iota.org/chrysalis/message/{message_id['message_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This example creates a data transaction split over multiple messages. | ||
# May come in handy with big messages exceeding 32768 byte limit | ||
import iota_client | ||
|
||
INDEX = "Chrysalis Python Workshop" | ||
DATA = ("Chrysalis Python Workshop|" * 10).encode() # 260 characters | ||
CHUNK_SIZE = 128 # Split into multiple 128 character messages | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
|
||
counter = 1 | ||
message_ids = [] | ||
while len(DATA): | ||
message_id_indexation = client.message(index=INDEX, data=DATA[:128]) | ||
print('Message ' + str(counter) + " sent!") | ||
print("https://explorer.iota.org/chrysalis/message/" + message_id_indexation['message_id']) | ||
message_ids.append(message_id_indexation['message_id']) | ||
DATA = DATA[128:] | ||
counter += 1 | ||
|
||
print("\nCopy IDs of your messages. You'll need them in the next example.") | ||
print(f'Message IDs: {str(message_ids)}') |
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,23 @@ | ||
# This example fetches and prints a multi-transaction message. | ||
import iota_client | ||
|
||
# *** Replace with IDs from the previous example *** | ||
MESSAGE_IDS = ['3f0ad429395d2990a32f1bd31e5787931dea2ab4ad2035c1d554bb4dbf3dd34d', | ||
'26894efc6fe60cd1660e52780c5f3e167cd23d3a1f64175a1c226181e8e803e7', | ||
'b48ca2a18303010b8f9736b5b8bb85eb959e2ac87561e15c88fc57a76a9de48f'] | ||
# Chrysalis testnet node | ||
client = iota_client.Client(node='https://api.lb-0.testnet.chrysalis2.com') | ||
full_message = "" | ||
for x in range(len(MESSAGE_IDS)): | ||
message = client.get_message_data(MESSAGE_IDS[x]) | ||
message_index = message['payload']['indexation'][0]['index'] | ||
message_content = message['payload']['indexation'][0]['data'] | ||
full_message += bytes(message_content).decode() | ||
# Additional message information, feel free to uncomment following lines | ||
# print(f'Message {x + 1} data: {message}') | ||
# print(f'Message {x + 1} index: {bytes.fromhex(message_index).decode("utf-8")}') | ||
print(f'Message {x + 1} content: {bytes(message_content).decode()}') | ||
print(f'Message {x + 1} length: {len(bytes(message_content).decode())} bytes') | ||
|
||
print(f'\nResulting message: {full_message}') | ||
print(f'Resulting message length: {len(full_message)} bytes') |