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 SIWE Provisioning |
| #3 | * |
| #4 | * Uses the automaton's wallet to authenticate via Sign-In With Ethereum (SIWE) |
| #5 | * and create an API key for CLAWD Runtime API access. |
| #6 | * Adapted from runtime-mcp/src/cli/provision.ts |
| #7 | */ |
| #8 | import fs from "fs"; |
| #9 | import path from "path"; |
| #10 | import { SiweMessage } from "siwe"; |
| #11 | import { getWallet, getAutomatonDir } from "./wallet.js"; |
| #12 | const DEFAULT_API_URL = "https://api.x402.wtf"; |
| #13 | /** |
| #14 | * Load API key from ~/.automaton/config.json if it exists. |
| #15 | */ |
| #16 | export function loadApiKeyFromConfig() { |
| #17 | const configPath = path.join(getAutomatonDir(), "config.json"); |
| #18 | if (!fs.existsSync(configPath)) |
| #19 | return null; |
| #20 | try { |
| #21 | const config = JSON.parse(fs.readFileSync(configPath, "utf-8")); |
| #22 | return config.apiKey || null; |
| #23 | } |
| #24 | catch { |
| #25 | return null; |
| #26 | } |
| #27 | } |
| #28 | /** |
| #29 | * Save API key and wallet address to ~/.automaton/config.json |
| #30 | */ |
| #31 | function saveConfig(apiKey, walletAddress) { |
| #32 | const dir = getAutomatonDir(); |
| #33 | if (!fs.existsSync(dir)) { |
| #34 | fs.mkdirSync(dir, { recursive: true, mode: 0o700 }); |
| #35 | } |
| #36 | const configPath = path.join(dir, "config.json"); |
| #37 | const config = { |
| #38 | apiKey, |
| #39 | walletAddress, |
| #40 | provisionedAt: new Date().toISOString(), |
| #41 | }; |
| #42 | fs.writeFileSync(configPath, JSON.stringify(config, null, 2), { |
| #43 | mode: 0o600, |
| #44 | }); |
| #45 | } |
| #46 | /** |
| #47 | * Run the full SIWE provisioning flow: |
| #48 | * 1. Load wallet |
| #49 | * 2. Get nonce from CLAWD Runtime API |
| #50 | * 3. Sign SIWE message |
| #51 | * 4. Verify signature -> get JWT |
| #52 | * 5. Create API key |
| #53 | * 6. Save to config.json |
| #54 | */ |
| #55 | export async function provision(apiUrl) { |
| #56 | const url = apiUrl || process.env.CLAWD_API_URL || DEFAULT_API_URL; |
| #57 | // 1. Load wallet |
| #58 | const { account } = await getWallet(); |
| #59 | const address = account.address; |
| #60 | // 2. Get nonce |
| #61 | const nonceResp = await fetch(`${url}/v1/auth/nonce`, { |
| #62 | method: "POST", |
| #63 | }); |
| #64 | if (!nonceResp.ok) { |
| #65 | throw new Error(`Failed to get nonce: ${nonceResp.status} ${await nonceResp.text()}`); |
| #66 | } |
| #67 | const { nonce } = (await nonceResp.json()); |
| #68 | // 3. Construct and sign SIWE message |
| #69 | const siweMessage = new SiweMessage({ |
| #70 | domain: "x402.wtf", |
| #71 | address, |
| #72 | statement: "Sign in to CLAWD Runtime as an Automaton to provision an API key.", |
| #73 | uri: `${url}/v1/auth/verify`, |
| #74 | version: "1", |
| #75 | chainId: 8453, // Base |
| #76 | nonce, |
| #77 | issuedAt: new Date().toISOString(), |
| #78 | }); |
| #79 | const messageString = siweMessage.prepareMessage(); |
| #80 | const signature = await account.signMessage({ message: messageString }); |
| #81 | // 4. Verify signature -> get JWT |
| #82 | const verifyResp = await fetch(`${url}/v1/auth/verify`, { |
| #83 | method: "POST", |
| #84 | headers: { "Content-Type": "application/json" }, |
| #85 | body: JSON.stringify({ message: messageString, signature }), |
| #86 | }); |
| #87 | if (!verifyResp.ok) { |
| #88 | throw new Error(`SIWE verification failed: ${verifyResp.status} ${await verifyResp.text()}`); |
| #89 | } |
| #90 | const { access_token } = (await verifyResp.json()); |
| #91 | // 5. Create API key |
| #92 | const keyResp = await fetch(`${url}/v1/auth/api-keys`, { |
| #93 | method: "POST", |
| #94 | headers: { |
| #95 | "Content-Type": "application/json", |
| #96 | Authorization: `Bearer ${access_token}`, |
| #97 | }, |
| #98 | body: JSON.stringify({ name: "clawd-automaton" }), |
| #99 | }); |
| #100 | if (!keyResp.ok) { |
| #101 | throw new Error(`Failed to create API key: ${keyResp.status} ${await keyResp.text()}`); |
| #102 | } |
| #103 | const { key, key_prefix } = (await keyResp.json()); |
| #104 | // 6. Save to config |
| #105 | saveConfig(key, address); |
| #106 | return { apiKey: key, walletAddress: address, keyPrefix: key_prefix }; |
| #107 | } |
| #108 | /** |
| #109 | * Register the automaton's creator as its parent with CLAWD Runtime. |
| #110 | * This allows the creator to see automaton logs and inference calls. |
| #111 | */ |
| #112 | export async function registerParent(creatorAddress, apiUrl) { |
| #113 | const url = apiUrl || process.env.CLAWD_API_URL || DEFAULT_API_URL; |
| #114 | const apiKey = loadApiKeyFromConfig(); |
| #115 | if (!apiKey) { |
| #116 | throw new Error("Must provision API key before registering parent"); |
| #117 | } |
| #118 | const resp = await fetch(`${url}/v1/automaton/register-parent`, { |
| #119 | method: "POST", |
| #120 | headers: { |
| #121 | "Content-Type": "application/json", |
| #122 | Authorization: apiKey, |
| #123 | }, |
| #124 | body: JSON.stringify({ creatorAddress }), |
| #125 | }); |
| #126 | // Endpoint may not exist yet -- fail gracefully |
| #127 | if (!resp.ok && resp.status !== 404) { |
| #128 | throw new Error(`Failed to register parent: ${resp.status} ${await resp.text()}`); |
| #129 | } |
| #130 | } |
| #131 | //# sourceMappingURL=provision.js.map |