Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

content(docs): adding localwallet guide #790

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions components/landing-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ function LandingPage() {
const startingGuides = [
{
label: "Building your first agent",
path: "/guides/agents/installing-uagent",
path: "/guides/agents/quickstart",
image: buildYourAgent,
},
{
label: "Creating an executable function for AI",
path: "guides/agents/communicating-with-other-agents",
path: "/guides/agents/intermediate/agent-functions",
image: executable,
},
{
label: "The Fetch.ai technology stack",
path: "/guides/agentverse/creating-a-hosted-agent",
path: "/concepts/introducing-fetchai",
image: techStack,
},
];
Expand Down
166 changes: 166 additions & 0 deletions pages/guides/agents/advanced/localwallet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { Callout } from 'nextra/components'


# 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.

<Callout type="info" emoji="ℹ️">
This article is a work-in-progress. It will be expanded rapidly.
</Callout>


## 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:

```python copy
# 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:

```py copy
# 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:

```py copy
# 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:

```py copy
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:

```py copy
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.

```python copy

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"))
```

2 changes: 1 addition & 1 deletion pages/guides/agents/intermediate/mailbox.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ Once the agent is correctly retrieved, you will have to give it a **name**. When

Now your agent doesn't need to be running all the time as messages will be waiting when it comes back online.

**Great! You correctly enrolled your local agent on the Agentverse using the Mailbox feature!**
**Great! You correctly enrolled your local agent on the Agentverse using the Mailbox feature!**
Loading