UNPKG

@bitte-ai/agent-sdk

Version:

Agent SDK for Bitte Protocol

50 lines (49 loc) 1.51 kB
import { getAddress, isHex, toHex, toBytes, zeroAddress } from "viem"; export * from "./types"; export * from "./erc20"; export * from "./weth"; export * from "./tokens"; export * from "./safe"; export * from "./chain"; export * from "./client"; export function hexifyValue(value) { if (isHex(value)) { return value; } return toHex(toBytes(BigInt(value))); } export function signRequestFor({ from, chainId, metaTransactions, }) { return { method: "eth_sendTransaction", chainId, params: metaTransactions.map((mt) => { return { from: from ?? zeroAddress, to: getAddress(mt.to), value: hexifyValue(mt.value), data: mt.data, }; }), }; } export function fallbackResponder(responseData, init) { return { json: (_data, responseInit) => ({ data: responseData, ...init, ...responseInit, }), }; } export async function validateRequest(req, responder) { const createResponse = responder ? responder : fallbackResponder; const metadataHeader = req.headers.get("mb-metadata"); const metadata = JSON.parse(metadataHeader ?? "{}"); const { accountId, evmAddress } = metadata; if (!accountId && !evmAddress) { const error = "Missing accountId and evmAddress in metadata"; console.error(error); return createResponse({ error }, { status: 400 }); } return null; }