Offline transactions

Throughout this tutorial, we’ll walk you through the creation of a basic script to generate a list of spend transactions and to sign them.

It’ll consist of three parts:

  • Build a list of spend transactions (offline)

  • Sign each transactions (offline)

  • Broadcast the transactions obtained in the above steps

We’ll assume you have the SDK installed.

First import the required libraries

from aeternity.node import NodeClient, Config
from aeternity.signing import Account
from aeternity.transactions import TxBuilder, TxSigner
from aeternity import utils, defaults, identifiers
import os

For the next steps we need to have an account available.

# Accounts addresses
account = Account.generate()

Hint

To successfully complete the tutorial, you will have to have a positive balance on the account just creataed. How to get some funds is explained in the first tutorial using the Faucet app.

Once we have an account available we will use the TxBuilder to prepare the transactions.

# instantiate the transactions builder
build = TxBuilder()

# we will be creating 5 transactions for later broadcast TODO: warn about the nonce limit
txs = []

# each transaction is going to be a spend
amount = utils.amount_to_aettos("0.05AE")
payload = b''

for i in range(5):
    # increase the account nonce
    account.nonce = account.nonce + 1
    # build the transaction
    tx = build.tx_spend(
        account.get_address(),  # sender
        Account.generate().get_address(),  # random generated recipient
        amount,
        payload,
        defaults.FEE,
        defaults.TX_TTL,
        account.nonce
    )
    # save the transaction
    txs.append(tx)

Once the transactions are ready they need to be signed and encapsulated in signed transaction:

Hint

the Network ID is required to compute a valid signature for transactions; when using the online node client the Network ID is automatically retrieved from the node.

# define the network_id
network_id = identifiers.NETWORK_ID_TESTNET

The TxSigner object provides the functionality to compute the signature for transactions.

# instantiate a transaction signer
signer = TxSigner(account, network_id)

# collect the signed tx for broadcast
signed_txs = []
# sign all transactions 
for tx in txs:
    signature = signer.sign_transaction(tx)
    signed_tx = build.tx_signed([signature], tx)
    signed_txs.append(signed_tx)

Finally we can instantiate a node client and broadcast the transactions:

# Broadcast the transactions
NODE_URL = os.environ.get('TEST_URL', 'https://testnet.aeternity.io')

node_cli = NodeClient(Config(
    external_url=NODE_URL,
    blocking_mode=False,
))

# broadcast all transactions
for stx in signed_txs:
    node_cli.broadcast_transaction(stx)

# verify that all transactions have been posted
for stx in signed_txs:
    height = node_cli.wait_for_transaction(stx)
    assert(height > 0)

Thats it! You have successfully executed your transaction in the Aeternity Blockchain testnet network. For the mainnet network the procedure is the same except you will have to get some tokens via an exchange or via other means.