fetch-logo
ConceptsConceptsGuidesGuidesExamplesExamplesReferencesReferencesAPIsAPIs
GitHub (opens in a new tab)
  • Guides
      • Quickstart
        • What's an Agent?
        • uAgents Framework installation
        • Create your first agent
        • Agents address
        • Communicating with other agents
        • Agent Handlers (on_...)
        • Agents storage functions
        • Public and private Agents
        • Send tokens with Agents
        • Agents Mailboxes
        • Agent Functions
        • Make agents AI Engine compatible
        • Multi-file agent pipeline for AI Engine: Hugging face API to create a multi agent pipeline
        • Options for running local agents
        • Hosted agent
        • Introducing dialogues
        • Almanac Contract
        • Verify messages with Agents
        • Multi-file agent pipeline for AI Engine: Network of Primary and Secondary functions in Agentverse
        • Localwallet
        • Agentverse: Hosted Agents
        • Agentverse: Dice Roll agent
        • Agentverse: Allowed Imports
        • Agentverse: Mailbox
        • Register Agentverse Functions
        • Agentverse Functions: coin toss agent
        • Field descriptions for DeltaV
        • Agentverse Command Line Interface (AVCTL)
        • AVCTL Hosting commands
      • Agents and Functions creation APIs
      • Secret Management APIs
      • How to convert native FET to and from ERC-20 FET
      • How to stake FET tokens
      • Different ways of staking FET
      • Re-delegating staked FET token
      • Reconciliation service
      • How to setup a Multisig Wallet
        • Getting started
        • How to use the Fetch wallet
        • How to stake and claim rewards
        • Address book
        • Connections
        • Fetch Wallet Hardware Connection Guide
        • Installation
        • Connecting to a blockchain
        • Querying balances
        • Wallets and private keys
        • Sending funds
        • Staking
        • Smart contracts
          • Stake auto-compounder
          • Stake optimizer
          • Oracles
          • Wallet top-up
          • Liquidity pool
          • Swap automation
        • Installation️
        • Getting started
        • Keys
          • How to add profiles
          • How to add contract templates
          • How to compile contracts
          • How to deploy contracts
          • Contract interaction
        • Installation
        • How to use chain state snapshots
        • State-synchronization (state-sync)
        • How to set up a validator node
        • How to join a testnet
        • How to run a single node test network
        • Governance
        • How to get testnet tokens via the Token Faucet
          • CLI - Introduction
          • CLI - Managing keys
          • CLI - Managing tokens
          • CLI - Multisig keys
          • CLI - Delegation
          • Governance proposals
      • Agents 101
      • Agents 101 for AI Engine
  • Guides
      • Quickstart
        • What's an Agent?
        • uAgents Framework installation
        • Create your first agent
        • Agents address
        • Communicating with other agents
        • Agent Handlers (on_...)
        • Agents storage functions
        • Public and private Agents
        • Send tokens with Agents
        • Agents Mailboxes
        • Agent Functions
        • Make agents AI Engine compatible
        • Multi-file agent pipeline for AI Engine: Hugging face API to create a multi agent pipeline
        • Options for running local agents
        • Hosted agent
        • Introducing dialogues
        • Almanac Contract
        • Verify messages with Agents
        • Multi-file agent pipeline for AI Engine: Network of Primary and Secondary functions in Agentverse
        • Localwallet
          • Overview
          • Understanding your wallet
          • Generating a new private key
          • Recover an existing private key
          • From mnemonic
          • An Agent that checks for a transaction, and sends funds
        • Agentverse: Hosted Agents
        • Agentverse: Dice Roll agent
        • Agentverse: Allowed Imports
        • Agentverse: Mailbox
        • Register Agentverse Functions
        • Agentverse Functions: coin toss agent
        • Field descriptions for DeltaV
        • Agentverse Command Line Interface (AVCTL)
        • AVCTL Hosting commands
      • Agents and Functions creation APIs
      • Secret Management APIs
      • How to convert native FET to and from ERC-20 FET
      • How to stake FET tokens
      • Different ways of staking FET
      • Re-delegating staked FET token
      • Reconciliation service
      • How to setup a Multisig Wallet
        • Getting started
        • How to use the Fetch wallet
        • How to stake and claim rewards
        • Address book
        • Connections
        • Fetch Wallet Hardware Connection Guide
        • Installation
        • Connecting to a blockchain
        • Querying balances
        • Wallets and private keys
        • Sending funds
        • Staking
        • Smart contracts
          • Stake auto-compounder
          • Stake optimizer
          • Oracles
          • Wallet top-up
          • Liquidity pool
          • Swap automation
        • Installation️
        • Getting started
        • Keys
          • How to add profiles
          • How to add contract templates
          • How to compile contracts
          • How to deploy contracts
          • Contract interaction
        • Installation
        • How to use chain state snapshots
        • State-synchronization (state-sync)
        • How to set up a validator node
        • How to join a testnet
        • How to run a single node test network
        • Governance
        • How to get testnet tokens via the Token Faucet
          • CLI - Introduction
          • CLI - Managing keys
          • CLI - Managing tokens
          • CLI - Multisig keys
          • CLI - Delegation
          • Governance proposals
      • Agents 101
      • Agents 101 for AI Engine
Guides
AI Agents
Advanced Topics
Localwallet

Local Wallet

Overview

To transact with Fet on the Fetch.ai ledger, you will need to manage your wallet with you agent. We use the Cosmpy library for ledger actions, coupled with in library support for Cosmpy in uAgents.

ℹ️

This article is a work-in-progress. It will be expanded rapidly.

Understanding your wallet

Wallets allow individuals to manage, store and transact across blockchain networks. Wallets are composed of a public and private key pair. The private key is a unique and secret cryptographic code that provides ownership and control over the wallet and allows individuals to authorize transactions signing. The public address is a cryptographic identifier that allows individuals to receive and view transactions.

Changes to the state of a network are achieved by broadcasting transactions. To accomplish this, we can use CosmPy in a straightforward way to create and manage private keys and addresses. In this guide, you will learn how to both generate a new private key as well as recover a previously generated one.

The examples below outlines how to achieve both of these operations.

Generating a new private key

First of all, import the needed modules and then use the PrivateKey claass to create your private_key:

# Import necessary classes
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey
 
# Creating a random private key
private_key = PrivateKey()

Recover an existing private key

Let's start by extracting the private key and convert it into a base64 encoded string. You can do this on macOS or Linux for the Fetch.ai network using the FetchD CL.

An example is provided in the code snippet below:

# Extract the private key and convert it into a base64 encoded string
fetchd keys export mykeyname --unsafe --unarmored-hex | xxd -r -p | base64

Once you have extracted and converted the key into a base64 encoded string you can recover to an existing private key by following along the provided example below:

# Import necessary classes
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.crypto.keypairs import PrivateKey
 
# Recovering an existing private key
private_key = PrivateKey('<base64 encoded private key>') # Here is where you provide the base64 encoded private key string

The PrivateKey object is one of CosmPy's low level primitives. This is why it is generally paired with a Wallet object. You can now create a local wallet using the LocalWallet class from the CosmPy library and initialize it with the private_key you generated or recovered earlier. The LocalWallet class is used to manage a local wallet associated with a private key for interacting with the Cosmos blockchain.

With this wallet object, you can perform various operations, such as generating the corresponding public address associated with the private key and signing transactions. You can also query useful information such as the address from the wallet directly. The example below showcases how to achieve both of these outlined operations:

wallet = LocalWallet(private_key)
print(wallet.address()) # will print the address for the wallet

From mnemonic

You can also use an account's mnemonic phrase to get the associated private key. The example provided below showcases how to achieve that:

from cosmpy.aerial.wallet import LocalWallet
 
mnemonic = "person knife december tail tortoise jewel warm when worry limit reward memory piece cool sphere kitchen knee embody soft own victory sauce silly page"
 
wallet = LocalWallet.from_mnemonic(mnemonic)

An Agent that checks for a transaction, and sends funds

A simple agent that creates, or loads in a wallet, then validates a transaction has been received.

 
from cosmpy.aerial.wallet import LocalWallet
from cosmpy.aerial.client import LedgerClient, NetworkConfig
from cosmpy.crypto.keypairs import PrivateKey
from uagents import Agent, Context, Model
from uagents.network import get_faucet, wait_for_tx_to_complete
 
mainnet_ledger = LedgerClient(NetworkConfig.fetchai_mainnet())
 
 
class RequestWithTX(Model):
    message: str
    tx_hash: str
 
 
class DataResponse(Model):
    message: str
 
 
class PaymentError(Model):
    message: str
    tx_hash: str
 
 
DataSellingAgent = Agent(
    name="DataSellingAgent",
    seed="dwefwegferdwdwedgko4o430490349jf340-jffjweiopfnw",
    port=8001,
    endpoint=["http://localhost:8001/submit"],
)
 
print (DataSellingAgent.address)
 
AMOUNT = 1
DENOM = "afet"
DATA_TO_SELL = "..."
 
## at first you may want to generate a wallet
my_wallet = LocalWallet.generate()
## or open one from a seed you've set
# my_wallet = LocalWallet.from_unsafe_seed("registration test wallet")
# pk = my_wallet._private_key
## or from a pk you already have
# wallet = LocalWallet(PrivateKey("T7w1yHq1QIcQiSqV27YSwk+i1i+Y4JMKhkpawCQIh6s="))
 
...
 
 
@DataSellingAgent.on_message(model=RequestWithTX)
async def message_handler(ctx: Context, sender: str, msg: RequestWithTX):
    ctx.logger.info(f"Received message from {sender}: {msg.message}")
 
    mainnet_ledger.query_tx(msg.tx_hash)
    tx_resp = await wait_for_tx_to_complete(msg.tx_hash, mainnet_ledger)
 
    coin_received = tx_resp.events["coin_received"]
    if (
            coin_received["receiver"] == str(my_wallet.address)
            and coin_received["amount"] == f"{AMOUNT}{DENOM}"
    ):
        ctx.logger.info(f"Transaction was successful: {coin_received}")
        await ctx.send(sender, DataResponse(message=DATA_TO_SELL))
 
    else:
        await ctx.send(sender, PaymentError(message="Incorrect tx", tx_hash=msg.tx_hash))
 
if __name__ == "__main__":
    DataSellingAgent.run()
 
 

Validating a transaction with Cosmpy and uAgents is really easy, and to test the above agent you just need to replicate the Models and send a RequestWithTX in any function in another agent:

@DataBuyingAgent.on_event("startup")
async def introduce_agent(ctx: Context):
    ctx.logger.info("Starting DataBuyingAgent...")
    await ctx.send(agent_address, RequestWithTX(message="buy",  tx_hash="D165966A6B9269EHHHHH7C8F659F1CDA871C8CD83F9102DD35A437211F3DE0CF"))

Was this page helpful?

Multi-file agent pipeline for AI Engine: Network of Primary and Secondary functions in AgentverseAgentverse: Hosted Agents
footer-logo

Main website

Integrations

Events

We’re hiring!

Twitter (opens in a new tab)Telegram (opens in a new tab)Discord (opens in a new tab)GitHub (opens in a new tab)Youtube (opens in a new tab)LinkedIn (opens in a new tab)Reddit (opens in a new tab)
Sign up for developer updates