-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add more examples scripts * exclude tempfiles * delete tempfiles * remove external file and generate local pdf * Fix format --------- Co-authored-by: Michiel <michiel.bellengmail.com> Co-authored-by: Jerry <[email protected]>
- Loading branch information
1 parent
1abada7
commit a734b35
Showing
16 changed files
with
1,417 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,3 @@ | ||
network="testnet" | ||
wallet_mnemonic="select calm scorpion mask furnace nerve fade slam bid suggest avoid remove half depend turn little midnight fossil submit cart sick glance inner slide" | ||
blockfrost_api_key="preprod...." |
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,5 @@ | ||
.env | ||
/keys/* | ||
!keys/.gitkeep | ||
tempfile.pdf | ||
tempfile_copy.pdf |
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,78 @@ | ||
import os | ||
|
||
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS | ||
from dotenv import load_dotenv | ||
|
||
from pycardano import * | ||
|
||
load_dotenv() | ||
network = os.getenv("network") | ||
wallet_mnemonic = os.getenv("wallet_mnemonic") | ||
|
||
|
||
if network == "testnet": | ||
base_url = ApiUrls.preprod.value | ||
cardano_network = Network.TESTNET | ||
else: | ||
base_url = ApiUrls.mainnet.value | ||
cardano_network = Network.MAINNET | ||
|
||
|
||
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic) | ||
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0") | ||
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0") | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key) | ||
|
||
|
||
print("Enterprise address (only payment):") | ||
print("Payment Derivation path: m/1852'/1815'/0'/0/0") | ||
|
||
enterprise_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), network=cardano_network | ||
) | ||
print(enterprise_address) | ||
|
||
print(" ") | ||
print("Staking enabled address:") | ||
print("Payment Derivation path: m/1852'/1815'/0'/0/0") | ||
print("Staking Derivation path: m/1852'/1815'/0'/2/0") | ||
|
||
staking_enabled_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), | ||
staking_part=staking_skey.to_verification_key().hash(), | ||
network=cardano_network, | ||
) | ||
print(staking_enabled_address) | ||
|
||
print(" ") | ||
next_step = input("Press Enter to continue...") | ||
print(" ") | ||
|
||
for i in range(5): | ||
derivation_path = f"m/1852'/1815'/0'/0/{i}" | ||
|
||
payment_key = new_wallet.derive_from_path(derivation_path) | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
|
||
enterprise_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), network=cardano_network | ||
) | ||
print(f"Address {derivation_path}: {enterprise_address}") | ||
|
||
print(" ") | ||
next_step = input("Press Enter to continue...") | ||
print(" ") | ||
|
||
for i in range(5): | ||
derivation_path = f"m/1852'/1815'/0'/0/{i}" | ||
|
||
payment_key = new_wallet.derive_from_path(derivation_path) | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
|
||
staking_enabled_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), | ||
staking_part=staking_skey.to_verification_key().hash(), | ||
network=cardano_network, | ||
) | ||
print(f"Address {derivation_path}: {staking_enabled_address}") |
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,67 @@ | ||
import os | ||
import sys | ||
|
||
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS | ||
from dotenv import load_dotenv | ||
|
||
from pycardano import * | ||
|
||
load_dotenv() | ||
network = os.getenv("network") | ||
wallet_mnemonic = os.getenv("wallet_mnemonic") | ||
blockfrost_api_key = os.getenv("blockfrost_api_key") | ||
|
||
|
||
if network == "testnet": | ||
base_url = ApiUrls.preprod.value | ||
cardano_network = Network.TESTNET | ||
else: | ||
base_url = ApiUrls.mainnet.value | ||
cardano_network = Network.MAINNET | ||
|
||
|
||
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic) | ||
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0") | ||
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0") | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key) | ||
|
||
|
||
main_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), | ||
staking_part=staking_skey.to_verification_key().hash(), | ||
network=cardano_network, | ||
) | ||
|
||
print(" ") | ||
print(f"Derived address: {main_address}") | ||
print(" ") | ||
|
||
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url) | ||
|
||
try: | ||
utxos = api.address_utxos(main_address) | ||
except Exception as e: | ||
if e.status_code == 404: | ||
print("Address does not have any UTXOs. ") | ||
if network == "testnet": | ||
print( | ||
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/" | ||
) | ||
else: | ||
print(e.message) | ||
sys.exit(1) | ||
|
||
print(f"hash \t\t\t\t\t\t\t\t\t amount") | ||
print( | ||
"--------------------------------------------------------------------------------------" | ||
) | ||
|
||
for utxo in utxos: | ||
tokens = "" | ||
for token in utxo.amount: | ||
if token.unit != "lovelace": | ||
tokens += f"{token.quantity} {token.unit} + " | ||
print( | ||
f"{utxo.tx_hash}#{utxo.tx_index} \t {int(utxo.amount[0].quantity)/1000000} ADA [{tokens}]" | ||
) |
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 @@ | ||
import os | ||
import sys | ||
|
||
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS | ||
from dotenv import load_dotenv | ||
|
||
from pycardano import * | ||
|
||
load_dotenv() | ||
network = os.getenv("network") | ||
wallet_mnemonic = os.getenv("wallet_mnemonic") | ||
blockfrost_api_key = os.getenv("blockfrost_api_key") | ||
|
||
|
||
if network == "testnet": | ||
base_url = ApiUrls.preprod.value | ||
cardano_network = Network.TESTNET | ||
else: | ||
base_url = ApiUrls.mainnet.value | ||
cardano_network = Network.MAINNET | ||
|
||
|
||
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic) | ||
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0") | ||
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0") | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key) | ||
|
||
|
||
main_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), | ||
staking_part=staking_skey.to_verification_key().hash(), | ||
network=cardano_network, | ||
) | ||
|
||
print(" ") | ||
print(f"Derived address: {main_address}") | ||
print(" ") | ||
|
||
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url) | ||
|
||
try: | ||
utxos = api.address_utxos(main_address) | ||
except Exception as e: | ||
if e.status_code == 404: | ||
print("Address does not have any UTXOs. ") | ||
if network == "testnet": | ||
print( | ||
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/" | ||
) | ||
else: | ||
print(e.message) | ||
sys.exit(1) | ||
|
||
|
||
cardano = BlockFrostChainContext(project_id=blockfrost_api_key, base_url=base_url) | ||
|
||
builder = TransactionBuilder(cardano) | ||
|
||
for i in range(20): | ||
output = TransactionOutput(main_address, Value(4000000)) | ||
builder.add_output(output) | ||
|
||
builder.add_input_address(main_address) | ||
signed_tx = builder.build_and_sign([payment_skey], change_address=main_address) | ||
result = cardano.submit_tx(signed_tx.to_cbor()) | ||
print(f"Number of inputs: \t {len(signed_tx.transaction_body.inputs)}") | ||
print(f"Number of outputs: \t {len(signed_tx.transaction_body.outputs)}") | ||
print(f"Fee: \t\t\t {signed_tx.transaction_body.fee/1000000} ADA") | ||
print(f"Transaction submitted! ID: {result}") |
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,106 @@ | ||
import os | ||
import sys | ||
|
||
from blockfrost import ApiError, ApiUrls, BlockFrostApi, BlockFrostIPFS | ||
from dotenv import load_dotenv | ||
|
||
from pycardano import * | ||
|
||
load_dotenv() | ||
network = os.getenv("network") | ||
wallet_mnemonic = os.getenv("wallet_mnemonic") | ||
blockfrost_api_key = os.getenv("blockfrost_api_key") | ||
|
||
|
||
if network == "testnet": | ||
base_url = ApiUrls.preprod.value | ||
cardano_network = Network.TESTNET | ||
else: | ||
base_url = ApiUrls.mainnet.value | ||
cardano_network = Network.MAINNET | ||
|
||
|
||
new_wallet = crypto.bip32.HDWallet.from_mnemonic(wallet_mnemonic) | ||
payment_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/0/0") | ||
staking_key = new_wallet.derive_from_path(f"m/1852'/1815'/0'/2/0") | ||
payment_skey = ExtendedSigningKey.from_hdwallet(payment_key) | ||
staking_skey = ExtendedSigningKey.from_hdwallet(staking_key) | ||
|
||
|
||
main_address = Address( | ||
payment_part=payment_skey.to_verification_key().hash(), | ||
staking_part=staking_skey.to_verification_key().hash(), | ||
network=cardano_network, | ||
) | ||
|
||
print(" ") | ||
print(f"Derived address: {main_address}") | ||
print(" ") | ||
|
||
api = BlockFrostApi(project_id=blockfrost_api_key, base_url=base_url) | ||
|
||
try: | ||
utxos = api.address_utxos(main_address) | ||
except Exception as e: | ||
if e.status_code == 404: | ||
print("Address does not have any UTXOs. ") | ||
if network == "testnet": | ||
print( | ||
"Request tADA from the faucet: https://docs.cardano.org/cardano-testnets/tools/faucet/" | ||
) | ||
else: | ||
print(e.message) | ||
sys.exit(1) | ||
|
||
|
||
cardano = BlockFrostChainContext(project_id=blockfrost_api_key, base_url=base_url) | ||
|
||
builder = TransactionBuilder(cardano) | ||
|
||
inputs = [] | ||
|
||
total_ada_used = 0 | ||
|
||
for utxo in utxos: | ||
input = TransactionInput.from_primitive([utxo.tx_hash, utxo.tx_index]) | ||
inputs.append(input) | ||
builder.add_input(input) | ||
total_ada_used += int(utxo.amount[0].quantity) | ||
|
||
output = TransactionOutput(main_address, Value(total_ada_used)) | ||
|
||
tx_body = TransactionBody(inputs=inputs, outputs=[output], fee=100000) | ||
|
||
signature = payment_skey.sign(tx_body.hash()) | ||
vk = PaymentVerificationKey.from_signing_key(payment_skey) | ||
vk_witnesses = [VerificationKeyWitness(vk, signature)] | ||
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses)) | ||
|
||
calculated_fee = fee(cardano, len(signed_tx.to_cbor())) | ||
|
||
|
||
total_ada_available = total_ada_used - calculated_fee | ||
output = TransactionOutput(main_address, Value(total_ada_available)) | ||
|
||
tx_body = TransactionBody(inputs=inputs, outputs=[output], fee=calculated_fee) | ||
|
||
signature = payment_skey.sign(tx_body.hash()) | ||
vk = PaymentVerificationKey.from_signing_key(payment_skey) | ||
vk_witnesses = [VerificationKeyWitness(vk, signature)] | ||
signed_tx = Transaction(tx_body, TransactionWitnessSet(vkey_witnesses=vk_witnesses)) | ||
|
||
try: | ||
result = cardano.submit_tx(signed_tx.to_cbor()) | ||
print(f"Number of inputs: \t {len(signed_tx.transaction_body.inputs)}") | ||
print(f"Number of outputs: \t {len(signed_tx.transaction_body.outputs)}") | ||
print(f"Fee: \t\t\t {signed_tx.transaction_body.fee/1000000} ADA") | ||
print(f"Transaction submitted! ID: {result}") | ||
except Exception as e: | ||
if "BadInputsUTxO" in str(e): | ||
print("Trying to spend an input that doesn't exist (or no longer exist).") | ||
elif "ValueNotConservedUTxO" in str(e): | ||
print( | ||
"Transaction not correctly balanced. Inputs and outputs (+fee) don't match." | ||
) | ||
else: | ||
print(e) |
Oops, something went wrong.