UNPKG

industry-tools

Version:

Industry Tools is a TypeScript library providing essential tools for the Industry AI Agent Platform.

40 lines (39 loc) 1.52 kB
import { Wallet } from "ethers"; export async function createEVMWallet(input) { const { userId, characterId, network, storage } = input; try { const existingWalletData = await storage.getItem(`USER#${userId}`, `WALLETS#${characterId}`); let wallets = existingWalletData?.wallets || []; const existingNetworkWallet = wallets.find(w => w.network.toUpperCase() === network.toUpperCase()); if (existingNetworkWallet) { return { wallet: { address: existingNetworkWallet.address }, message: "Existing wallet retrieved successfully" }; } const wallet = Wallet.createRandom(); const newWallet = { privateKey: wallet.privateKey, address: wallet.address, network: network.toUpperCase(), createdAt: new Date().toISOString(), typename: "CharacterWallet" }; wallets.push(newWallet); await storage.createItem(`USER#${userId}`, `WALLETS#${characterId}`, { ...existingWalletData, wallets: wallets }); return { wallet: { address: wallet.address }, message: "Wallet created successfully" }; } catch (error) { console.log(error); return { error: error instanceof Error ? error.name : 'WalletCreationError', message: `Failed to create wallet: ${error instanceof Error ? error.message : String(error)}` }; } }