@agentbadge/circle-payments 0.1.17 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +77 -0
  2. package/dist/buyer/accepts.d.ts +33 -0
  3. package/dist/buyer/accepts.js +150 -0
  4. package/dist/buyer/caps.d.ts +27 -0
  5. package/dist/buyer/caps.js +67 -0
  6. package/dist/buyer/client.d.ts +66 -0
  7. package/dist/buyer/client.js +282 -0
  8. package/dist/buyer/guard.d.ts +9 -0
  9. package/dist/buyer/guard.js +1 -0
  10. package/dist/buyer/paginate.d.ts +6 -0
  11. package/dist/buyer/paginate.js +21 -0
  12. package/dist/buyer/risk-guard.d.ts +10 -0
  13. package/dist/buyer/risk-guard.js +47 -0
  14. package/dist/buyer/session.d.ts +18 -0
  15. package/dist/buyer/session.js +31 -0
  16. package/dist/chain/chain.d.ts +32 -0
  17. package/dist/chain/chain.js +44 -0
  18. package/dist/chain/index.d.ts +1 -0
  19. package/dist/chain/index.js +1 -0
  20. package/dist/chains.d.ts +8 -0
  21. package/dist/chains.js +28 -1
  22. package/dist/cli/pay.d.ts +7 -0
  23. package/dist/cli/pay.js +275 -0
  24. package/dist/gateway/deposit.d.ts +1 -1
  25. package/dist/gateway-probe.d.ts +1 -1
  26. package/dist/identity/erc8004.d.ts +18 -0
  27. package/dist/identity/erc8004.js +18 -1
  28. package/dist/identity/verify-passport.d.ts +1 -0
  29. package/dist/identity/verify-passport.js +1 -1
  30. package/dist/index.d.ts +12 -0
  31. package/dist/index.js +12 -0
  32. package/dist/middleware.d.ts +11 -0
  33. package/dist/middleware.js +40 -2
  34. package/dist/signer/arc-signer.d.ts +29 -0
  35. package/dist/signer/arc-signer.js +8 -0
  36. package/dist/signer/env-key.d.ts +20 -0
  37. package/dist/signer/env-key.js +58 -0
  38. package/dist/signer/protocol.d.ts +46 -0
  39. package/dist/signer/protocol.js +34 -0
  40. package/dist/signer/socket.d.ts +19 -0
  41. package/dist/signer/socket.js +139 -0
  42. package/package.json +5 -2
@@ -0,0 +1,34 @@
1
+ export const SIGNER_METHODS = [
2
+ "getAddress",
3
+ "health",
4
+ "signMessage",
5
+ "signTypedData",
6
+ "sendTransaction",
7
+ ];
8
+ const MAX_LINE_BYTES = 256 * 1024;
9
+ export function encodeRpcLine(msg) {
10
+ return `${JSON.stringify(msg)}\n`;
11
+ }
12
+ export function createLineDecoder() {
13
+ let buf = "";
14
+ return {
15
+ push(chunk) {
16
+ buf += chunk;
17
+ const out = [];
18
+ for (;;) {
19
+ const nl = buf.indexOf("\n");
20
+ if (nl < 0) {
21
+ if (buf.length > MAX_LINE_BYTES) {
22
+ throw new Error("signer rpc frame exceeds 256KB");
23
+ }
24
+ break;
25
+ }
26
+ const line = buf.slice(0, nl).trim();
27
+ buf = buf.slice(nl + 1);
28
+ if (line.length > 0)
29
+ out.push(JSON.parse(line));
30
+ }
31
+ return out;
32
+ },
33
+ };
34
+ }
@@ -0,0 +1,19 @@
1
+ import net from "node:net";
2
+ import { type Hex } from "viem";
3
+ import { type ArcSigner, type SignerWriteCall } from "./arc-signer.js";
4
+ import { type HealthResult } from "./protocol.js";
5
+ export declare const DEFAULT_SOCKET_PATH = "/tmp/agentbadge-signer.sock";
6
+ export interface SocketSignerOptions {
7
+ socketPath?: string;
8
+ requestTimeoutMs?: number;
9
+ reconnectMinMs?: number;
10
+ reconnectMaxMs?: number;
11
+ connect?: (path: string) => net.Socket;
12
+ }
13
+ export interface SocketSigner extends ArcSigner {
14
+ ready(): boolean;
15
+ ping(): Promise<HealthResult>;
16
+ close(): void;
17
+ writeContract(call: SignerWriteCall): Promise<Hex>;
18
+ }
19
+ export declare function createSocketSigner(opts?: SocketSignerOptions): SocketSigner;
@@ -0,0 +1,139 @@
1
+ import net from "node:net";
2
+ import { encodeFunctionData } from "viem";
3
+ import { SignerUnavailableError } from "./arc-signer.js";
4
+ import { createLineDecoder, encodeRpcLine, } from "./protocol.js";
5
+ export const DEFAULT_SOCKET_PATH = "/tmp/agentbadge-signer.sock";
6
+ export function createSocketSigner(opts = {}) {
7
+ const path = opts.socketPath ?? DEFAULT_SOCKET_PATH;
8
+ const timeoutMs = opts.requestTimeoutMs ?? 5_000;
9
+ const minMs = opts.reconnectMinMs ?? 250;
10
+ const maxMs = opts.reconnectMaxMs ?? 5_000;
11
+ const openSocket = opts.connect ?? ((p) => net.createConnection(p));
12
+ let sock = null;
13
+ let closed = false;
14
+ let addr = null;
15
+ let backoff = minMs;
16
+ let seq = 0;
17
+ const pending = new Map();
18
+ const decoder = createLineDecoder();
19
+ const failAll = (e) => {
20
+ for (const [, p] of pending) {
21
+ clearTimeout(p.timer);
22
+ p.reject(e);
23
+ }
24
+ pending.clear();
25
+ addr = null;
26
+ };
27
+ const scheduleReconnect = () => {
28
+ if (closed)
29
+ return;
30
+ const wait = backoff;
31
+ backoff = Math.min(Math.floor(backoff * 1.5), maxMs);
32
+ setTimeout(connect, wait).unref?.();
33
+ };
34
+ function connect() {
35
+ if (closed)
36
+ return;
37
+ sock = openSocket(path);
38
+ sock.setNoDelay?.(true);
39
+ sock.on("data", (chunk) => {
40
+ let msgs;
41
+ try {
42
+ msgs = decoder.push(chunk.toString("utf8"));
43
+ }
44
+ catch (e) {
45
+ failAll(e instanceof Error ? e : new Error(String(e)));
46
+ sock?.destroy();
47
+ scheduleReconnect();
48
+ return;
49
+ }
50
+ for (const m of msgs) {
51
+ const p = pending.get(m.id);
52
+ if (!p)
53
+ continue;
54
+ pending.delete(m.id);
55
+ clearTimeout(p.timer);
56
+ if (m.ok)
57
+ p.resolve(m.result);
58
+ else
59
+ p.reject(new SignerUnavailableError("rpc", `${m.error.code}: ${m.error.message}`));
60
+ }
61
+ });
62
+ let dead = false;
63
+ const onDead = (e) => {
64
+ if (dead)
65
+ return;
66
+ dead = true;
67
+ sock = null;
68
+ failAll(e instanceof SignerUnavailableError
69
+ ? e
70
+ : new SignerUnavailableError("connect", e?.message ?? "socket closed"));
71
+ scheduleReconnect();
72
+ };
73
+ sock.on("error", onDead);
74
+ sock.on("close", () => onDead());
75
+ sock.once("connect", () => {
76
+ backoff = minMs;
77
+ call("getAddress", {})
78
+ .then((a) => {
79
+ addr = a;
80
+ })
81
+ .catch(() => { });
82
+ });
83
+ }
84
+ function call(method, params) {
85
+ if (!sock || sock.destroyed) {
86
+ return Promise.reject(new SignerUnavailableError(method, `signer service unreachable on ${path}`));
87
+ }
88
+ const id = `${process.pid}:${++seq}`;
89
+ const req = { id, method, params };
90
+ return new Promise((resolve, reject) => {
91
+ const timer = setTimeout(() => {
92
+ pending.delete(id);
93
+ reject(new SignerUnavailableError(method, `timeout after ${timeoutMs}ms`));
94
+ }, timeoutMs);
95
+ timer.unref?.();
96
+ pending.set(id, { resolve, reject, timer });
97
+ try {
98
+ sock.write(encodeRpcLine(req));
99
+ }
100
+ catch (e) {
101
+ pending.delete(id);
102
+ clearTimeout(timer);
103
+ reject(e instanceof Error ? e : new Error(String(e)));
104
+ }
105
+ });
106
+ }
107
+ connect();
108
+ return {
109
+ get address() {
110
+ if (!addr) {
111
+ throw new SignerUnavailableError("address", "signer service not connected yet — call sites must health-gate");
112
+ }
113
+ return addr;
114
+ },
115
+ ready: () => !!sock && !sock.destroyed && addr !== null,
116
+ ping: async () => (await call("health", {})),
117
+ signMessage: (message) => call("signMessage", { message }),
118
+ signTypedData: (args) => call("signTypedData", args),
119
+ sendTransaction: (tx) => call("sendTransaction", {
120
+ to: tx.to,
121
+ data: tx.data,
122
+ ...(tx.value !== undefined ? { value: tx.value.toString() } : {}),
123
+ }),
124
+ writeContract: (call_) => call("sendTransaction", {
125
+ to: call_.address,
126
+ data: encodeFunctionData({
127
+ abi: call_.abi,
128
+ functionName: call_.functionName,
129
+ args: call_.args,
130
+ }),
131
+ }),
132
+ close: () => {
133
+ closed = true;
134
+ failAll(new SignerUnavailableError("close", "signer client closed"));
135
+ sock?.destroy();
136
+ sock = null;
137
+ },
138
+ };
139
+ }
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@agentbadge/circle-payments",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Circle Gateway nanopayments + x402 payment rails (Base Sepolia, Arc) for AgentBadge \u2014 trusted agent payments",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "agentbadge-pay": "dist/cli/pay.js"
10
+ },
8
11
  "files": [
9
12
  "dist",
10
13
  "diagrams"
@@ -49,4 +52,4 @@
49
52
  "peerDependencies": {
50
53
  "hono": "^4.7.0"
51
54
  }
52
- }
55
+ }