@agentbadge/circle-payments 0.1.0
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.
- package/README.md +203 -0
- package/dist/balance.d.ts +33 -0
- package/dist/balance.js +72 -0
- package/dist/chains.d.ts +63 -0
- package/dist/chains.js +41 -0
- package/dist/escrow/commission.d.ts +37 -0
- package/dist/escrow/commission.js +52 -0
- package/dist/escrow/erc8183.d.ts +288 -0
- package/dist/escrow/erc8183.js +318 -0
- package/dist/escrow/evaluator.d.ts +40 -0
- package/dist/escrow/evaluator.js +87 -0
- package/dist/history.d.ts +42 -0
- package/dist/history.js +52 -0
- package/dist/identity/erc8004.d.ts +227 -0
- package/dist/identity/erc8004.js +284 -0
- package/dist/identity/verify-passport.d.ts +17 -0
- package/dist/identity/verify-passport.js +64 -0
- package/dist/identity.d.ts +14 -0
- package/dist/identity.js +38 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/ledger.d.ts +18 -0
- package/dist/ledger.js +39 -0
- package/dist/memo/memo.d.ts +100 -0
- package/dist/memo/memo.js +106 -0
- package/dist/middleware.d.ts +26 -0
- package/dist/middleware.js +104 -0
- package/dist/pricing.d.ts +9 -0
- package/dist/pricing.js +25 -0
- package/dist/router.d.ts +30 -0
- package/dist/router.js +72 -0
- package/dist/schemes/arc-self-settle.d.ts +29 -0
- package/dist/schemes/arc-self-settle.js +114 -0
- package/dist/schemes/exact.d.ts +22 -0
- package/dist/schemes/exact.js +71 -0
- package/dist/schemes/gateway.d.ts +22 -0
- package/dist/schemes/gateway.js +76 -0
- package/dist/status.d.ts +32 -0
- package/dist/status.js +86 -0
- package/package.json +51 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { keccak256, toBytes } from "viem";
|
|
2
|
+
export function createEvaluationStore() {
|
|
3
|
+
const map = new Map();
|
|
4
|
+
return {
|
|
5
|
+
put: (v) => map.set(v.jobId, v),
|
|
6
|
+
get: (jobId) => map.get(jobId.toString()),
|
|
7
|
+
list: () => [...map.values()],
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export class EvaluatorError extends Error {
|
|
11
|
+
jobId;
|
|
12
|
+
cause;
|
|
13
|
+
constructor(message, jobId, cause) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.jobId = jobId;
|
|
16
|
+
this.cause = cause;
|
|
17
|
+
this.name = "EvaluatorError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const defaultVerify = async (_job, result) => result !== undefined && result !== null
|
|
21
|
+
? { pass: true, reason: "deliverable present" }
|
|
22
|
+
: { pass: false, reason: "empty deliverable" };
|
|
23
|
+
function reasonHashOf(reason) {
|
|
24
|
+
return keccak256(toBytes(reason));
|
|
25
|
+
}
|
|
26
|
+
export function createEvaluator(deps) {
|
|
27
|
+
const store = deps.store ?? createEvaluationStore();
|
|
28
|
+
const verify = deps.verify ?? defaultVerify;
|
|
29
|
+
const now = deps.now ?? (() => Math.floor(Date.now() / 1000));
|
|
30
|
+
async function record(v) {
|
|
31
|
+
store.put(v);
|
|
32
|
+
return v;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
store,
|
|
36
|
+
async evaluate(jobId, result) {
|
|
37
|
+
const job = await deps.escrow.getJob(jobId);
|
|
38
|
+
const evaluatedAt = new Date().toISOString();
|
|
39
|
+
if ((job.status === "Funded" || job.status === "Submitted") &&
|
|
40
|
+
BigInt(now()) >= job.expiredAt) {
|
|
41
|
+
const reason = `expired at ${job.expiredAt}`;
|
|
42
|
+
const txHash = await deps.escrow.claimRefund(deps.wallet, jobId);
|
|
43
|
+
return record({
|
|
44
|
+
jobId: jobId.toString(),
|
|
45
|
+
verdict: "expired",
|
|
46
|
+
reason,
|
|
47
|
+
reasonHash: reasonHashOf(reason),
|
|
48
|
+
txHash,
|
|
49
|
+
evaluatedAt,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
if (job.status !== "Submitted") {
|
|
53
|
+
throw new EvaluatorError(`job ${jobId} not evaluable (status=${job.status})`, jobId);
|
|
54
|
+
}
|
|
55
|
+
const check = await verify(job, result);
|
|
56
|
+
const reasonHash = reasonHashOf(check.reason);
|
|
57
|
+
if (check.pass) {
|
|
58
|
+
const txHash = await deps.escrow.completeJob(deps.wallet, {
|
|
59
|
+
jobId,
|
|
60
|
+
reason: reasonHash,
|
|
61
|
+
});
|
|
62
|
+
return record({
|
|
63
|
+
jobId: jobId.toString(),
|
|
64
|
+
verdict: "approve",
|
|
65
|
+
reason: check.reason,
|
|
66
|
+
reasonHash,
|
|
67
|
+
evidence: check.evidence,
|
|
68
|
+
txHash,
|
|
69
|
+
evaluatedAt,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const txHash = await deps.escrow.rejectJob(deps.wallet, {
|
|
73
|
+
jobId,
|
|
74
|
+
reason: reasonHash,
|
|
75
|
+
});
|
|
76
|
+
return record({
|
|
77
|
+
jobId: jobId.toString(),
|
|
78
|
+
verdict: "reject",
|
|
79
|
+
reason: check.reason,
|
|
80
|
+
reasonHash,
|
|
81
|
+
evidence: check.evidence,
|
|
82
|
+
txHash,
|
|
83
|
+
evaluatedAt,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FailureStore } from "./ledger";
|
|
2
|
+
export interface PaymentHistoryEntry {
|
|
3
|
+
type: "settled" | "failed";
|
|
4
|
+
scheme: string;
|
|
5
|
+
network?: string;
|
|
6
|
+
amount?: string;
|
|
7
|
+
payer?: string;
|
|
8
|
+
ref?: string;
|
|
9
|
+
reason?: string;
|
|
10
|
+
at?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface PaymentHistoryQuery {
|
|
13
|
+
limit?: number;
|
|
14
|
+
status?: "settled" | "failed";
|
|
15
|
+
}
|
|
16
|
+
export interface GatewayTransferSearch {
|
|
17
|
+
searchTransfers(params: {
|
|
18
|
+
to?: string;
|
|
19
|
+
network?: string;
|
|
20
|
+
status?: string;
|
|
21
|
+
pageSize?: number;
|
|
22
|
+
}): Promise<{
|
|
23
|
+
transfers?: TransferRow[];
|
|
24
|
+
} | TransferRow[]>;
|
|
25
|
+
}
|
|
26
|
+
interface TransferRow {
|
|
27
|
+
id?: string;
|
|
28
|
+
status?: string;
|
|
29
|
+
sendingNetwork?: string;
|
|
30
|
+
fromAddress?: string;
|
|
31
|
+
amount?: string;
|
|
32
|
+
updatedAt?: string;
|
|
33
|
+
createdAt?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface PaymentHistoryDeps {
|
|
36
|
+
failureStore?: FailureStore;
|
|
37
|
+
gatewayTransfers?: GatewayTransferSearch;
|
|
38
|
+
sellerAddress: string;
|
|
39
|
+
}
|
|
40
|
+
export type PaymentHistory = (q: PaymentHistoryQuery) => Promise<PaymentHistoryEntry[]>;
|
|
41
|
+
export declare function createPaymentHistory(deps: PaymentHistoryDeps): PaymentHistory;
|
|
42
|
+
export {};
|
package/dist/history.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const DEFAULT_LIMIT = 20;
|
|
2
|
+
export function createPaymentHistory(deps) {
|
|
3
|
+
return async (q) => {
|
|
4
|
+
const limit = q.limit ?? DEFAULT_LIMIT;
|
|
5
|
+
const wantSettled = !q.status || q.status === "settled";
|
|
6
|
+
const wantFailed = !q.status || q.status === "failed";
|
|
7
|
+
const entries = [];
|
|
8
|
+
if (wantSettled && deps.gatewayTransfers) {
|
|
9
|
+
try {
|
|
10
|
+
const resp = await deps.gatewayTransfers.searchTransfers({
|
|
11
|
+
to: deps.sellerAddress,
|
|
12
|
+
pageSize: limit,
|
|
13
|
+
});
|
|
14
|
+
const rows = Array.isArray(resp) ? resp : (resp.transfers ?? []);
|
|
15
|
+
for (const t of rows) {
|
|
16
|
+
entries.push({
|
|
17
|
+
type: "settled",
|
|
18
|
+
scheme: "gateway",
|
|
19
|
+
network: t.sendingNetwork,
|
|
20
|
+
amount: t.amount,
|
|
21
|
+
payer: t.fromAddress,
|
|
22
|
+
ref: t.id,
|
|
23
|
+
at: t.updatedAt ?? t.createdAt,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (wantFailed && deps.failureStore) {
|
|
31
|
+
try {
|
|
32
|
+
const failures = await deps.failureStore.list();
|
|
33
|
+
for (const f of failures) {
|
|
34
|
+
entries.push({
|
|
35
|
+
type: "failed",
|
|
36
|
+
scheme: f.scheme,
|
|
37
|
+
network: f.network,
|
|
38
|
+
amount: f.amount,
|
|
39
|
+
payer: f.payer,
|
|
40
|
+
ref: f.txRef ?? f.paymentId,
|
|
41
|
+
reason: f.reason,
|
|
42
|
+
at: f.at,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
entries.sort((a, b) => (b.at ?? "").localeCompare(a.at ?? ""));
|
|
50
|
+
return entries.slice(0, limit);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type { Hex } from "viem";
|
|
2
|
+
import type { ReadClient, WriteClient } from "../escrow/erc8183.js";
|
|
3
|
+
export declare const ERC8004_IDENTITY_REGISTRY: "0x8004A818BFB912233c491871b3d84c89A494BD9e";
|
|
4
|
+
export declare const ERC8004_REPUTATION_REGISTRY: "0x8004B663056A597Dffe9eCcC1965A193B7388713";
|
|
5
|
+
export declare const ERC8004_VALIDATION_REGISTRY: "0x8004Cb1BF31DAf7788923b405b754f57acEB4272";
|
|
6
|
+
export declare const ERC8004_ABI: readonly [{
|
|
7
|
+
readonly type: "function";
|
|
8
|
+
readonly name: "register";
|
|
9
|
+
readonly stateMutability: "nonpayable";
|
|
10
|
+
readonly inputs: readonly [{
|
|
11
|
+
readonly name: "metadataURI";
|
|
12
|
+
readonly type: "string";
|
|
13
|
+
}];
|
|
14
|
+
readonly outputs: readonly [{
|
|
15
|
+
readonly name: "agentId";
|
|
16
|
+
readonly type: "uint256";
|
|
17
|
+
}];
|
|
18
|
+
}, {
|
|
19
|
+
readonly type: "function";
|
|
20
|
+
readonly name: "ownerOf";
|
|
21
|
+
readonly stateMutability: "view";
|
|
22
|
+
readonly inputs: readonly [{
|
|
23
|
+
readonly name: "tokenId";
|
|
24
|
+
readonly type: "uint256";
|
|
25
|
+
}];
|
|
26
|
+
readonly outputs: readonly [{
|
|
27
|
+
readonly name: "";
|
|
28
|
+
readonly type: "address";
|
|
29
|
+
}];
|
|
30
|
+
}, {
|
|
31
|
+
readonly type: "function";
|
|
32
|
+
readonly name: "tokenURI";
|
|
33
|
+
readonly stateMutability: "view";
|
|
34
|
+
readonly inputs: readonly [{
|
|
35
|
+
readonly name: "tokenId";
|
|
36
|
+
readonly type: "uint256";
|
|
37
|
+
}];
|
|
38
|
+
readonly outputs: readonly [{
|
|
39
|
+
readonly name: "";
|
|
40
|
+
readonly type: "string";
|
|
41
|
+
}];
|
|
42
|
+
}, {
|
|
43
|
+
readonly type: "function";
|
|
44
|
+
readonly name: "giveFeedback";
|
|
45
|
+
readonly stateMutability: "nonpayable";
|
|
46
|
+
readonly inputs: readonly [{
|
|
47
|
+
readonly name: "agentId";
|
|
48
|
+
readonly type: "uint256";
|
|
49
|
+
}, {
|
|
50
|
+
readonly name: "value";
|
|
51
|
+
readonly type: "int128";
|
|
52
|
+
}, {
|
|
53
|
+
readonly name: "decimals";
|
|
54
|
+
readonly type: "uint8";
|
|
55
|
+
}, {
|
|
56
|
+
readonly name: "tag1";
|
|
57
|
+
readonly type: "string";
|
|
58
|
+
}, {
|
|
59
|
+
readonly name: "tag2";
|
|
60
|
+
readonly type: "string";
|
|
61
|
+
}, {
|
|
62
|
+
readonly name: "endpoint";
|
|
63
|
+
readonly type: "string";
|
|
64
|
+
}, {
|
|
65
|
+
readonly name: "feedbackURI";
|
|
66
|
+
readonly type: "string";
|
|
67
|
+
}, {
|
|
68
|
+
readonly name: "hash";
|
|
69
|
+
readonly type: "bytes32";
|
|
70
|
+
}];
|
|
71
|
+
readonly outputs: readonly [];
|
|
72
|
+
}, {
|
|
73
|
+
readonly type: "function";
|
|
74
|
+
readonly name: "validationRequest";
|
|
75
|
+
readonly stateMutability: "nonpayable";
|
|
76
|
+
readonly inputs: readonly [{
|
|
77
|
+
readonly name: "validator";
|
|
78
|
+
readonly type: "address";
|
|
79
|
+
}, {
|
|
80
|
+
readonly name: "agentId";
|
|
81
|
+
readonly type: "uint256";
|
|
82
|
+
}, {
|
|
83
|
+
readonly name: "requestURI";
|
|
84
|
+
readonly type: "string";
|
|
85
|
+
}, {
|
|
86
|
+
readonly name: "requestHash";
|
|
87
|
+
readonly type: "bytes32";
|
|
88
|
+
}];
|
|
89
|
+
readonly outputs: readonly [];
|
|
90
|
+
}, {
|
|
91
|
+
readonly type: "function";
|
|
92
|
+
readonly name: "validationResponse";
|
|
93
|
+
readonly stateMutability: "nonpayable";
|
|
94
|
+
readonly inputs: readonly [{
|
|
95
|
+
readonly name: "requestHash";
|
|
96
|
+
readonly type: "bytes32";
|
|
97
|
+
}, {
|
|
98
|
+
readonly name: "response";
|
|
99
|
+
readonly type: "uint8";
|
|
100
|
+
}, {
|
|
101
|
+
readonly name: "responseURI";
|
|
102
|
+
readonly type: "string";
|
|
103
|
+
}, {
|
|
104
|
+
readonly name: "responseHash";
|
|
105
|
+
readonly type: "bytes32";
|
|
106
|
+
}, {
|
|
107
|
+
readonly name: "tag";
|
|
108
|
+
readonly type: "string";
|
|
109
|
+
}];
|
|
110
|
+
readonly outputs: readonly [];
|
|
111
|
+
}, {
|
|
112
|
+
readonly type: "function";
|
|
113
|
+
readonly name: "getValidationStatus";
|
|
114
|
+
readonly stateMutability: "view";
|
|
115
|
+
readonly inputs: readonly [{
|
|
116
|
+
readonly name: "requestHash";
|
|
117
|
+
readonly type: "bytes32";
|
|
118
|
+
}];
|
|
119
|
+
readonly outputs: readonly [{
|
|
120
|
+
readonly name: "validatorAddress";
|
|
121
|
+
readonly type: "address";
|
|
122
|
+
}, {
|
|
123
|
+
readonly name: "agentId";
|
|
124
|
+
readonly type: "uint256";
|
|
125
|
+
}, {
|
|
126
|
+
readonly name: "response";
|
|
127
|
+
readonly type: "uint8";
|
|
128
|
+
}, {
|
|
129
|
+
readonly name: "responseHash";
|
|
130
|
+
readonly type: "bytes32";
|
|
131
|
+
}, {
|
|
132
|
+
readonly name: "tag";
|
|
133
|
+
readonly type: "string";
|
|
134
|
+
}, {
|
|
135
|
+
readonly name: "lastUpdate";
|
|
136
|
+
readonly type: "uint256";
|
|
137
|
+
}];
|
|
138
|
+
}, {
|
|
139
|
+
readonly type: "event";
|
|
140
|
+
readonly name: "Transfer";
|
|
141
|
+
readonly inputs: readonly [{
|
|
142
|
+
readonly indexed: true;
|
|
143
|
+
readonly name: "from";
|
|
144
|
+
readonly type: "address";
|
|
145
|
+
}, {
|
|
146
|
+
readonly indexed: true;
|
|
147
|
+
readonly name: "to";
|
|
148
|
+
readonly type: "address";
|
|
149
|
+
}, {
|
|
150
|
+
readonly indexed: true;
|
|
151
|
+
readonly name: "tokenId";
|
|
152
|
+
readonly type: "uint256";
|
|
153
|
+
}];
|
|
154
|
+
readonly anonymous: false;
|
|
155
|
+
}];
|
|
156
|
+
export declare class Erc8004Error extends Error {
|
|
157
|
+
readonly phase: "register" | "lookup" | "giveFeedback" | "validationRequest" | "validationResponse" | "getValidationStatus";
|
|
158
|
+
readonly cause?: unknown | undefined;
|
|
159
|
+
constructor(message: string, phase: "register" | "lookup" | "giveFeedback" | "validationRequest" | "validationResponse" | "getValidationStatus", cause?: unknown | undefined);
|
|
160
|
+
}
|
|
161
|
+
export interface Erc8004Config {
|
|
162
|
+
read: ReadClient;
|
|
163
|
+
identity?: `0x${string}`;
|
|
164
|
+
reputation?: `0x${string}`;
|
|
165
|
+
validation?: `0x${string}`;
|
|
166
|
+
}
|
|
167
|
+
export interface AgentLookup {
|
|
168
|
+
agentId: bigint;
|
|
169
|
+
owner: `0x${string}`;
|
|
170
|
+
metadataURI: string;
|
|
171
|
+
}
|
|
172
|
+
export interface ValidationStatus {
|
|
173
|
+
validatorAddress: `0x${string}`;
|
|
174
|
+
agentId: bigint;
|
|
175
|
+
response: number;
|
|
176
|
+
responseHash: Hex;
|
|
177
|
+
tag: string;
|
|
178
|
+
lastUpdate: bigint;
|
|
179
|
+
}
|
|
180
|
+
export declare function createErc8004(cfg: Erc8004Config): {
|
|
181
|
+
identity: `0x${string}`;
|
|
182
|
+
reputation: `0x${string}`;
|
|
183
|
+
validation: `0x${string}`;
|
|
184
|
+
registerMirror(wallet: WriteClient, metadataURI: string): Promise<{
|
|
185
|
+
agentId: bigint;
|
|
186
|
+
txHash: Hex;
|
|
187
|
+
}>;
|
|
188
|
+
lookup(agentId: bigint): Promise<AgentLookup>;
|
|
189
|
+
giveFeedback(wallet: WriteClient, args: {
|
|
190
|
+
agentId: bigint;
|
|
191
|
+
value: bigint;
|
|
192
|
+
decimals?: number;
|
|
193
|
+
tag1?: string;
|
|
194
|
+
tag2?: string;
|
|
195
|
+
endpoint?: string;
|
|
196
|
+
feedbackURI?: string;
|
|
197
|
+
hash?: Hex;
|
|
198
|
+
}): Promise<Hex>;
|
|
199
|
+
requestValidation(wallet: WriteClient, args: {
|
|
200
|
+
validator: `0x${string}`;
|
|
201
|
+
agentId: bigint;
|
|
202
|
+
requestURI: string;
|
|
203
|
+
requestHash: Hex;
|
|
204
|
+
}): Promise<Hex>;
|
|
205
|
+
respondValidation(wallet: WriteClient, args: {
|
|
206
|
+
requestHash: Hex;
|
|
207
|
+
response: number;
|
|
208
|
+
responseURI?: string;
|
|
209
|
+
responseHash?: Hex;
|
|
210
|
+
tag?: string;
|
|
211
|
+
}): Promise<Hex>;
|
|
212
|
+
getValidationStatus(requestHash: Hex): Promise<ValidationStatus>;
|
|
213
|
+
};
|
|
214
|
+
export type Erc8004 = ReturnType<typeof createErc8004>;
|
|
215
|
+
export declare function createMirrorRegistry(): {
|
|
216
|
+
link(passportId: string, agentId: bigint, txHash: Hex): void;
|
|
217
|
+
agentFor(passportId: string): bigint | undefined;
|
|
218
|
+
passportFor(agentId: bigint): string | undefined;
|
|
219
|
+
list(): {
|
|
220
|
+
agentId: bigint;
|
|
221
|
+
txHash: Hex;
|
|
222
|
+
mirroredAt: string;
|
|
223
|
+
passportId: string;
|
|
224
|
+
}[];
|
|
225
|
+
size(): number;
|
|
226
|
+
};
|
|
227
|
+
export type MirrorRegistry = ReturnType<typeof createMirrorRegistry>;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { decodeEventLog } from "viem";
|
|
2
|
+
export const ERC8004_IDENTITY_REGISTRY = "0x8004A818BFB912233c491871b3d84c89A494BD9e";
|
|
3
|
+
export const ERC8004_REPUTATION_REGISTRY = "0x8004B663056A597Dffe9eCcC1965A193B7388713";
|
|
4
|
+
export const ERC8004_VALIDATION_REGISTRY = "0x8004Cb1BF31DAf7788923b405b754f57acEB4272";
|
|
5
|
+
export const ERC8004_ABI = [
|
|
6
|
+
{
|
|
7
|
+
type: "function",
|
|
8
|
+
name: "register",
|
|
9
|
+
stateMutability: "nonpayable",
|
|
10
|
+
inputs: [{ name: "metadataURI", type: "string" }],
|
|
11
|
+
outputs: [{ name: "agentId", type: "uint256" }],
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
type: "function",
|
|
15
|
+
name: "ownerOf",
|
|
16
|
+
stateMutability: "view",
|
|
17
|
+
inputs: [{ name: "tokenId", type: "uint256" }],
|
|
18
|
+
outputs: [{ name: "", type: "address" }],
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
type: "function",
|
|
22
|
+
name: "tokenURI",
|
|
23
|
+
stateMutability: "view",
|
|
24
|
+
inputs: [{ name: "tokenId", type: "uint256" }],
|
|
25
|
+
outputs: [{ name: "", type: "string" }],
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
type: "function",
|
|
29
|
+
name: "giveFeedback",
|
|
30
|
+
stateMutability: "nonpayable",
|
|
31
|
+
inputs: [
|
|
32
|
+
{ name: "agentId", type: "uint256" },
|
|
33
|
+
{ name: "value", type: "int128" },
|
|
34
|
+
{ name: "decimals", type: "uint8" },
|
|
35
|
+
{ name: "tag1", type: "string" },
|
|
36
|
+
{ name: "tag2", type: "string" },
|
|
37
|
+
{ name: "endpoint", type: "string" },
|
|
38
|
+
{ name: "feedbackURI", type: "string" },
|
|
39
|
+
{ name: "hash", type: "bytes32" },
|
|
40
|
+
],
|
|
41
|
+
outputs: [],
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: "function",
|
|
45
|
+
name: "validationRequest",
|
|
46
|
+
stateMutability: "nonpayable",
|
|
47
|
+
inputs: [
|
|
48
|
+
{ name: "validator", type: "address" },
|
|
49
|
+
{ name: "agentId", type: "uint256" },
|
|
50
|
+
{ name: "requestURI", type: "string" },
|
|
51
|
+
{ name: "requestHash", type: "bytes32" },
|
|
52
|
+
],
|
|
53
|
+
outputs: [],
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
type: "function",
|
|
57
|
+
name: "validationResponse",
|
|
58
|
+
stateMutability: "nonpayable",
|
|
59
|
+
inputs: [
|
|
60
|
+
{ name: "requestHash", type: "bytes32" },
|
|
61
|
+
{ name: "response", type: "uint8" },
|
|
62
|
+
{ name: "responseURI", type: "string" },
|
|
63
|
+
{ name: "responseHash", type: "bytes32" },
|
|
64
|
+
{ name: "tag", type: "string" },
|
|
65
|
+
],
|
|
66
|
+
outputs: [],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
type: "function",
|
|
70
|
+
name: "getValidationStatus",
|
|
71
|
+
stateMutability: "view",
|
|
72
|
+
inputs: [{ name: "requestHash", type: "bytes32" }],
|
|
73
|
+
outputs: [
|
|
74
|
+
{ name: "validatorAddress", type: "address" },
|
|
75
|
+
{ name: "agentId", type: "uint256" },
|
|
76
|
+
{ name: "response", type: "uint8" },
|
|
77
|
+
{ name: "responseHash", type: "bytes32" },
|
|
78
|
+
{ name: "tag", type: "string" },
|
|
79
|
+
{ name: "lastUpdate", type: "uint256" },
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
type: "event",
|
|
84
|
+
name: "Transfer",
|
|
85
|
+
inputs: [
|
|
86
|
+
{ indexed: true, name: "from", type: "address" },
|
|
87
|
+
{ indexed: true, name: "to", type: "address" },
|
|
88
|
+
{ indexed: true, name: "tokenId", type: "uint256" },
|
|
89
|
+
],
|
|
90
|
+
anonymous: false,
|
|
91
|
+
},
|
|
92
|
+
];
|
|
93
|
+
export class Erc8004Error extends Error {
|
|
94
|
+
phase;
|
|
95
|
+
cause;
|
|
96
|
+
constructor(message, phase, cause) {
|
|
97
|
+
super(message);
|
|
98
|
+
this.phase = phase;
|
|
99
|
+
this.cause = cause;
|
|
100
|
+
this.name = "Erc8004Error";
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function extractAgentId(receipt) {
|
|
104
|
+
for (const log of receipt.logs) {
|
|
105
|
+
try {
|
|
106
|
+
const decoded = decodeEventLog({
|
|
107
|
+
abi: ERC8004_ABI,
|
|
108
|
+
data: log.data,
|
|
109
|
+
topics: log.topics,
|
|
110
|
+
});
|
|
111
|
+
if (decoded.eventName === "Transfer") {
|
|
112
|
+
return decoded.args.tokenId;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
throw new Erc8004Error("Transfer event not found in receipt", "register");
|
|
120
|
+
}
|
|
121
|
+
export function createErc8004(cfg) {
|
|
122
|
+
const identity = cfg.identity ?? ERC8004_IDENTITY_REGISTRY;
|
|
123
|
+
const reputation = cfg.reputation ?? ERC8004_REPUTATION_REGISTRY;
|
|
124
|
+
const validation = cfg.validation ?? ERC8004_VALIDATION_REGISTRY;
|
|
125
|
+
return {
|
|
126
|
+
identity,
|
|
127
|
+
reputation,
|
|
128
|
+
validation,
|
|
129
|
+
async registerMirror(wallet, metadataURI) {
|
|
130
|
+
try {
|
|
131
|
+
const txHash = await wallet.writeContract({
|
|
132
|
+
address: identity,
|
|
133
|
+
abi: ERC8004_ABI,
|
|
134
|
+
functionName: "register",
|
|
135
|
+
args: [metadataURI],
|
|
136
|
+
});
|
|
137
|
+
const receipt = cfg.read.waitForTransactionReceipt
|
|
138
|
+
? await cfg.read.waitForTransactionReceipt({ hash: txHash })
|
|
139
|
+
: await cfg.read.getTransactionReceipt({ hash: txHash });
|
|
140
|
+
return { agentId: extractAgentId(receipt), txHash };
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
if (e instanceof Erc8004Error)
|
|
144
|
+
throw e;
|
|
145
|
+
throw new Erc8004Error("register failed", "register", e);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
async lookup(agentId) {
|
|
149
|
+
try {
|
|
150
|
+
const [owner, metadataURI] = await Promise.all([
|
|
151
|
+
cfg.read.readContract({
|
|
152
|
+
address: identity,
|
|
153
|
+
abi: ERC8004_ABI,
|
|
154
|
+
functionName: "ownerOf",
|
|
155
|
+
args: [agentId],
|
|
156
|
+
}),
|
|
157
|
+
cfg.read.readContract({
|
|
158
|
+
address: identity,
|
|
159
|
+
abi: ERC8004_ABI,
|
|
160
|
+
functionName: "tokenURI",
|
|
161
|
+
args: [agentId],
|
|
162
|
+
}),
|
|
163
|
+
]);
|
|
164
|
+
return { agentId, owner, metadataURI };
|
|
165
|
+
}
|
|
166
|
+
catch (e) {
|
|
167
|
+
throw new Erc8004Error("lookup failed", "lookup", e);
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
async giveFeedback(wallet, args) {
|
|
171
|
+
try {
|
|
172
|
+
return await wallet.writeContract({
|
|
173
|
+
address: reputation,
|
|
174
|
+
abi: ERC8004_ABI,
|
|
175
|
+
functionName: "giveFeedback",
|
|
176
|
+
args: [
|
|
177
|
+
args.agentId,
|
|
178
|
+
args.value,
|
|
179
|
+
args.decimals ?? 0,
|
|
180
|
+
args.tag1 ?? "",
|
|
181
|
+
args.tag2 ?? "",
|
|
182
|
+
args.endpoint ?? "",
|
|
183
|
+
args.feedbackURI ?? "",
|
|
184
|
+
args.hash ??
|
|
185
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
186
|
+
],
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
throw new Erc8004Error("giveFeedback failed", "giveFeedback", e);
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
async requestValidation(wallet, args) {
|
|
194
|
+
try {
|
|
195
|
+
return await wallet.writeContract({
|
|
196
|
+
address: validation,
|
|
197
|
+
abi: ERC8004_ABI,
|
|
198
|
+
functionName: "validationRequest",
|
|
199
|
+
args: [
|
|
200
|
+
args.validator,
|
|
201
|
+
args.agentId,
|
|
202
|
+
args.requestURI,
|
|
203
|
+
args.requestHash,
|
|
204
|
+
],
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
catch (e) {
|
|
208
|
+
throw new Erc8004Error("validationRequest failed", "validationRequest", e);
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
async respondValidation(wallet, args) {
|
|
212
|
+
try {
|
|
213
|
+
return await wallet.writeContract({
|
|
214
|
+
address: validation,
|
|
215
|
+
abi: ERC8004_ABI,
|
|
216
|
+
functionName: "validationResponse",
|
|
217
|
+
args: [
|
|
218
|
+
args.requestHash,
|
|
219
|
+
args.response,
|
|
220
|
+
args.responseURI ?? "",
|
|
221
|
+
args.responseHash ??
|
|
222
|
+
"0x0000000000000000000000000000000000000000000000000000000000000000",
|
|
223
|
+
args.tag ?? "",
|
|
224
|
+
],
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
throw new Erc8004Error("validationResponse failed", "validationResponse", e);
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
async getValidationStatus(requestHash) {
|
|
232
|
+
try {
|
|
233
|
+
const raw = (await cfg.read.readContract({
|
|
234
|
+
address: validation,
|
|
235
|
+
abi: ERC8004_ABI,
|
|
236
|
+
functionName: "getValidationStatus",
|
|
237
|
+
args: [requestHash],
|
|
238
|
+
}));
|
|
239
|
+
return {
|
|
240
|
+
validatorAddress: raw[0],
|
|
241
|
+
agentId: raw[1],
|
|
242
|
+
response: raw[2],
|
|
243
|
+
responseHash: raw[3],
|
|
244
|
+
tag: raw[4],
|
|
245
|
+
lastUpdate: raw[5],
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (e) {
|
|
249
|
+
throw new Erc8004Error("getValidationStatus failed", "getValidationStatus", e);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
export function createMirrorRegistry() {
|
|
255
|
+
const byPassport = new Map();
|
|
256
|
+
return {
|
|
257
|
+
link(passportId, agentId, txHash) {
|
|
258
|
+
byPassport.set(passportId, {
|
|
259
|
+
agentId,
|
|
260
|
+
txHash,
|
|
261
|
+
mirroredAt: new Date().toISOString(),
|
|
262
|
+
});
|
|
263
|
+
},
|
|
264
|
+
agentFor(passportId) {
|
|
265
|
+
return byPassport.get(passportId)?.agentId;
|
|
266
|
+
},
|
|
267
|
+
passportFor(agentId) {
|
|
268
|
+
for (const [pid, v] of byPassport) {
|
|
269
|
+
if (v.agentId === agentId)
|
|
270
|
+
return pid;
|
|
271
|
+
}
|
|
272
|
+
return undefined;
|
|
273
|
+
},
|
|
274
|
+
list() {
|
|
275
|
+
return [...byPassport.entries()].map(([passportId, v]) => ({
|
|
276
|
+
passportId,
|
|
277
|
+
...v,
|
|
278
|
+
}));
|
|
279
|
+
},
|
|
280
|
+
size() {
|
|
281
|
+
return byPassport.size;
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
}
|