Skip to content

Commit

Permalink
fix(#8): onchain storage of character
Browse files Browse the repository at this point in the history
  • Loading branch information
0xDevVoyager committed Dec 28, 2024
1 parent 0209c7c commit 436b413
Show file tree
Hide file tree
Showing 10 changed files with 534 additions and 296 deletions.
19 changes: 17 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
# BlockStore Adapter Configuration
BLOCKSTORE_STORE=false
BLOCKSTORE_STORE_CHARACTER=false
BLOCKSTORE_STORE_MEMORY=false
BLOCKSTORE_CHAIN=base # ethereum/base/celestia
BLOCKSTORE_RECOVERY=false
BLOCKSTORE_RECOVERY_ALL=false
BLOCKSTORE_REGISTRY_URL= # e.g. "http://localhost:8545"
BLOCKSTORE_REGISTRY_ADDR= # e.g. "0x{contract-address}"
BLOCKSTORE_REGISTRY_PRIVATEKEY= # e.g. "0x{preivate-key-hex}"
BLOCKSTORE_DATA_URL= # e.g. "http://localhost:8545"
BLOCKSTORE_DATA_PRIVATEKEY= # e.g. "0x{preivate-key-hex}"

# BlockStore Adapter Configuration
BLOCKSTORE_STORE_CHARACTER=false
BLOCKSTORE_STORE_MEMORY=false
BLOCKSTORE_CHAIN=evm # ethereum/evm/celestia
BLOCKSTORE_RECOVERY_ALL=false
BLOCKSTORE_RECOVERY_BLOB_COUNT= # {number}, default to all

BLOCKSTORE_REGISTRY_URL= # e.g. "http://localhost:8545"
BLOCKSTORE_REGISTRY_ADDR= # e.g. "0x{contract-address}"
BLOCKSTORE_REGISTRY_PRIVATEKEY= # e.g. "0x{preivate-key-hex}"

BLOCKSTORE_DATA_URL= # e.g. "http://localhost:8545"
BLOCKSTORE_DATA_PRIVATEKEY= # e.g. "0x{preivate-key-hex}"

# Discord Configuration
DISCORD_APPLICATION_ID=
DISCORD_API_TOKEN= # Bot token
Expand Down
8 changes: 4 additions & 4 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,10 +430,10 @@ async function startAgent(character: Character, directClient) {
character.username ??= character.name;

const blockStoreAdapter = new BlockStoreQueue(character.id);
if (process.env.BLOCKSTORE_RECOVERY == "true") {
if (process.env.BLOCKSTORE_RECOVERY_ALL.toLowerCase() == "true") {
const bsUtil = new BlockStoreUtil(character.id);
character = await bsUtil.restoreCharacter(character);
} else if (process.env.BLOCKSTORE_STORE == "true") {
character = await bsUtil.restoreCharacter();
} else if (process.env.BLOCKSTORE_STORE_CHARACTER.toLowerCase() == "true") {
blockStoreAdapter.enqueue(BlockStoreMsgType.character, character);
}

Expand All @@ -449,7 +449,7 @@ async function startAgent(character: Character, directClient) {

await db.init();

if (process.env.BLOCKSTORE_RECOVERY == "true") {
if (process.env.BLOCKSTORE_RECOVERY_ALL.toLowerCase() == "true") {
const bsUtil = new BlockStoreUtil(character.id, db);
await bsUtil.restoreMemory();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/adapter-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
import { IBlockchain, Message } from "./types";
import Web3 from 'web3';

export class BaseStoreAdapter implements IBlockchain {
export class EvmCompatibleStoreAdapter implements IBlockchain {
private account;
private web3;

Expand Down Expand Up @@ -127,8 +127,8 @@ export function createBlockchain(
return new EthereumAdapter();
case "celestia":
return new CelestiaStoreAdapter();
case "base":
return new BaseStoreAdapter();
case "evm":
return new EvmCompatibleStoreAdapter();
default:
throw new Error(`Unknown blockchain adapter: ${chain}`);
}
Expand Down
171 changes: 0 additions & 171 deletions packages/adapter-blockchain/src/contract.ts

This file was deleted.

109 changes: 70 additions & 39 deletions packages/adapter-blockchain/src/contract/AgentRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,96 @@
pragma solidity ^0.8.0;

contract AgentRegistry {
// Mapping to store the UUID (key) and corresponding custom value (value)
mapping(string => string) private registry;
// Mappings for blobIdx, keyStore, and character.
mapping(string => string) private blobIdxMapping;
mapping(string => string) private keyStoreMapping;
mapping(string => string) private characterMapping;

// Mapping to store the owner of each key
// Mapping to store the owner of each agentID
mapping(string => address) private keyOwners;

// Event to log registration, update, or deletion
event Registered(string indexed key, string value, address indexed owner);
event Deleted(string indexed key);
event OwnershipTransferred(string indexed key, address indexed previousOwner, address indexed newOwner);
// Events for logging updates and ownership changes
event BlobIdxUpdated(string indexed agentID, string blobIdx);
event KeyStoreUpdated(string indexed agentID, string keyStore);
event CharacterUpdated(string indexed agentID, string character);
event AgentDeleted(string indexed agentID);
event OwnershipTransferred(string indexed agentID, address indexed previousOwner, address indexed newOwner);

// Modifier to ensure the user can only modify their own key
modifier onlyKeyOwner(string memory key) {
require(keyOwners[key] == msg.sender, "Not the owner of this key");
// Modifier to ensure the caller is the owner of the agentID
modifier onlyAgentOwner(string memory agentID) {
require(keyOwners[agentID] == msg.sender, "Not the owner of this agentID");
_;
}

// Register or update a key with its associated custom value
function registerOrUpdate(string memory key, string memory value) public {
// If the key is new, assign ownership to the sender
if (keyOwners[key] == address(0)) {
keyOwners[key] = msg.sender;
// Update or register ownership of the agentID (if not already assigned)
function updateOrRegisterOwnership(string memory agentID) internal {
if (keyOwners[agentID] == address(0)) {
keyOwners[agentID] = msg.sender;
} else {
require(keyOwners[agentID] == msg.sender, "Not the owner of this agentID");
}
}

// Update or register blobIdx for a given agent
function updateOrRegisterBlobIdx(string memory agentID, string memory newBlobIdx) external {
updateOrRegisterOwnership(agentID); // Ensure ownership
blobIdxMapping[agentID] = newBlobIdx;
emit BlobIdxUpdated(agentID, newBlobIdx);
}

// Ensure the sender is the owner of the key
require(keyOwners[key] == msg.sender, "Not the owner of this key");
// Update or register keyStore for a given agent
function updateOrRegisterKeyStore(string memory agentID, string memory newKeyStore) external {
updateOrRegisterOwnership(agentID); // Ensure ownership
keyStoreMapping[agentID] = newKeyStore;
emit KeyStoreUpdated(agentID, newKeyStore);
}

registry[key] = value;
emit Registered(key, value, msg.sender);
// Update or register character for a given agent
function updateOrRegisterCharacter(string memory agentID, string memory newCharacter) external {
updateOrRegisterOwnership(agentID); // Ensure ownership
characterMapping[agentID] = newCharacter;
emit CharacterUpdated(agentID, newCharacter);
}

// Delete a key from the registry
function deleteKey(string memory key) public onlyKeyOwner(key) {
require(bytes(registry[key]).length != 0, "Key does not exist");
delete registry[key];
delete keyOwners[key];
emit Deleted(key);
// Delete an agent with a given agent
function deleteAgent(string memory agentID) external onlyAgentOwner(agentID) {
delete blobIdxMapping[agentID];
delete keyStoreMapping[agentID];
delete characterMapping[agentID];
delete keyOwners[agentID];
emit AgentDeleted(agentID);
}

// Transfer ownership of a key to another address
function transferOwnership(string memory key, address newOwner) public onlyKeyOwner(key) {
// Transfer ownership of an agentID to a new owner
function transferOwnership(string memory agentID, address newOwner) external onlyAgentOwner(agentID) {
require(newOwner != address(0), "New owner cannot be the zero address");
address previousOwner = keyOwners[key];
keyOwners[key] = newOwner;
emit OwnershipTransferred(key, previousOwner, newOwner);
address previousOwner = keyOwners[agentID];
keyOwners[agentID] = newOwner;
emit OwnershipTransferred(agentID, previousOwner, newOwner);
}

// Retrieve blobIdx for a given agentID
function getBlobIdx(string memory agentID) external view returns (string memory) {
return blobIdxMapping[agentID];
}

// Retrieve keyStore for a given agentID
function getKeyStore(string memory agentID) external view returns (string memory) {
return keyStoreMapping[agentID];
}

// Retrieve the value associated with a given key
function getValue(string memory key) public view returns (string memory) {
return registry[key];
// Retrieve character for a given agentID
function getCharacter(string memory agentID) external view returns (string memory) {
return characterMapping[agentID];
}

// Check if a key exists in the registry
function keyExists(string memory key) public view returns (bool) {
return bytes(registry[key]).length != 0;
// Check if an agentID exists
function agentExists(string memory agentID) external view returns (bool) {
return keyOwners[agentID] != address(0);
}

// Retrieve the owner of a given key
function getKeyOwner(string memory key) public view returns (address) {
return keyOwners[key];
// Retrieve the owner of a given agentID
function getAgentOwner(string memory agentID) external view returns (address) {
return keyOwners[agentID];
}
}
Loading

0 comments on commit 436b413

Please sign in to comment.