repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | /** |
| #2 | * Automaton Wallet Management |
| #3 | * |
| #4 | * Creates and manages an EVM wallet for the automaton's identity and payments. |
| #5 | * The private key is the automaton's sovereign identity. |
| #6 | * Adapted from conway-mcp/src/wallet.ts |
| #7 | */ |
| #8 | |
| #9 | import type { PrivateKeyAccount } from "viem"; |
| #10 | import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; |
| #11 | import fs from "fs"; |
| #12 | import path from "path"; |
| #13 | import type { WalletData } from "../types.js"; |
| #14 | |
| #15 | const AUTOMATON_DIR = path.join( |
| #16 | process.env.HOME || "/root", |
| #17 | ".automaton", |
| #18 | ); |
| #19 | const WALLET_FILE = path.join(AUTOMATON_DIR, "wallet.json"); |
| #20 | |
| #21 | export function getAutomatonDir(): string { |
| #22 | return AUTOMATON_DIR; |
| #23 | } |
| #24 | |
| #25 | export function getWalletPath(): string { |
| #26 | return WALLET_FILE; |
| #27 | } |
| #28 | |
| #29 | /** |
| #30 | * Get or create the automaton's wallet. |
| #31 | * The private key IS the automaton's identity -- protect it. |
| #32 | */ |
| #33 | export async function getWallet(): Promise<{ |
| #34 | account: PrivateKeyAccount; |
| #35 | isNew: boolean; |
| #36 | }> { |
| #37 | if (!fs.existsSync(AUTOMATON_DIR)) { |
| #38 | fs.mkdirSync(AUTOMATON_DIR, { recursive: true, mode: 0o700 }); |
| #39 | } |
| #40 | |
| #41 | if (fs.existsSync(WALLET_FILE)) { |
| #42 | const walletData: WalletData = JSON.parse( |
| #43 | fs.readFileSync(WALLET_FILE, "utf-8"), |
| #44 | ); |
| #45 | const account = privateKeyToAccount(walletData.privateKey); |
| #46 | return { account, isNew: false }; |
| #47 | } else { |
| #48 | const privateKey = generatePrivateKey(); |
| #49 | const account = privateKeyToAccount(privateKey); |
| #50 | |
| #51 | const walletData: WalletData = { |
| #52 | privateKey, |
| #53 | createdAt: new Date().toISOString(), |
| #54 | }; |
| #55 | |
| #56 | fs.writeFileSync(WALLET_FILE, JSON.stringify(walletData, null, 2), { |
| #57 | mode: 0o600, |
| #58 | }); |
| #59 | |
| #60 | return { account, isNew: true }; |
| #61 | } |
| #62 | } |
| #63 | |
| #64 | /** |
| #65 | * Get the wallet address without loading the full account. |
| #66 | */ |
| #67 | export function getWalletAddress(): string | null { |
| #68 | if (!fs.existsSync(WALLET_FILE)) { |
| #69 | return null; |
| #70 | } |
| #71 | |
| #72 | const walletData: WalletData = JSON.parse( |
| #73 | fs.readFileSync(WALLET_FILE, "utf-8"), |
| #74 | ); |
| #75 | const account = privateKeyToAccount(walletData.privateKey); |
| #76 | return account.address; |
| #77 | } |
| #78 | |
| #79 | /** |
| #80 | * Load the full wallet account (needed for signing). |
| #81 | */ |
| #82 | export function loadWalletAccount(): PrivateKeyAccount | null { |
| #83 | if (!fs.existsSync(WALLET_FILE)) { |
| #84 | return null; |
| #85 | } |
| #86 | |
| #87 | const walletData: WalletData = JSON.parse( |
| #88 | fs.readFileSync(WALLET_FILE, "utf-8"), |
| #89 | ); |
| #90 | return privateKeyToAccount(walletData.privateKey); |
| #91 | } |
| #92 | |
| #93 | export function walletExists(): boolean { |
| #94 | return fs.existsSync(WALLET_FILE); |
| #95 | } |
| #96 |