@moonbeam-foundation/sdk 0.5.2
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/LICENSE +17 -0
- package/README.md +378 -0
- package/bin/moonbeam.mjs +56 -0
- package/dist/acp/coverage.d.ts +36 -0
- package/dist/acp/coverage.d.ts.map +1 -0
- package/dist/acp/coverage.js +51 -0
- package/dist/acp/coverage.js.map +1 -0
- package/dist/acp/events.d.ts +201 -0
- package/dist/acp/events.d.ts.map +1 -0
- package/dist/acp/events.js +201 -0
- package/dist/acp/events.js.map +1 -0
- package/dist/acp/hex.d.ts +29 -0
- package/dist/acp/hex.d.ts.map +1 -0
- package/dist/acp/hex.js +51 -0
- package/dist/acp/hex.js.map +1 -0
- package/dist/acp/lifecycle-types.d.ts +11 -0
- package/dist/acp/lifecycle-types.d.ts.map +1 -0
- package/dist/acp/lifecycle-types.js +8 -0
- package/dist/acp/lifecycle-types.js.map +1 -0
- package/dist/acp/lifecycle.d.ts +78 -0
- package/dist/acp/lifecycle.d.ts.map +1 -0
- package/dist/acp/lifecycle.js +224 -0
- package/dist/acp/lifecycle.js.map +1 -0
- package/dist/acp/outcome.d.ts +45 -0
- package/dist/acp/outcome.d.ts.map +1 -0
- package/dist/acp/outcome.js +104 -0
- package/dist/acp/outcome.js.map +1 -0
- package/dist/acp/pipeline.d.ts +30 -0
- package/dist/acp/pipeline.d.ts.map +1 -0
- package/dist/acp/pipeline.js +25 -0
- package/dist/acp/pipeline.js.map +1 -0
- package/dist/acp/recourse.d.ts +74 -0
- package/dist/acp/recourse.d.ts.map +1 -0
- package/dist/acp/recourse.js +234 -0
- package/dist/acp/recourse.js.map +1 -0
- package/dist/acp/registry.d.ts +62 -0
- package/dist/acp/registry.d.ts.map +1 -0
- package/dist/acp/registry.js +86 -0
- package/dist/acp/registry.js.map +1 -0
- package/dist/acp/transitions.d.ts +82 -0
- package/dist/acp/transitions.d.ts.map +1 -0
- package/dist/acp/transitions.js +106 -0
- package/dist/acp/transitions.js.map +1 -0
- package/dist/actors/index.d.ts +84 -0
- package/dist/actors/index.d.ts.map +1 -0
- package/dist/actors/index.js +133 -0
- package/dist/actors/index.js.map +1 -0
- package/dist/actors/track-record.d.ts +79 -0
- package/dist/actors/track-record.d.ts.map +1 -0
- package/dist/actors/track-record.js +105 -0
- package/dist/actors/track-record.js.map +1 -0
- package/dist/adapters/acp.d.ts +48 -0
- package/dist/adapters/acp.d.ts.map +1 -0
- package/dist/adapters/acp.js +74 -0
- package/dist/adapters/acp.js.map +1 -0
- package/dist/adapters/port.d.ts +53 -0
- package/dist/adapters/port.d.ts.map +1 -0
- package/dist/adapters/port.js +2 -0
- package/dist/adapters/port.js.map +1 -0
- package/dist/adapters/stamp.d.ts +57 -0
- package/dist/adapters/stamp.d.ts.map +1 -0
- package/dist/adapters/stamp.js +114 -0
- package/dist/adapters/stamp.js.map +1 -0
- package/dist/client/index.d.ts +123 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +78 -0
- package/dist/client/index.js.map +1 -0
- package/dist/connect/attestation.d.ts +64 -0
- package/dist/connect/attestation.d.ts.map +1 -0
- package/dist/connect/attestation.js +62 -0
- package/dist/connect/attestation.js.map +1 -0
- package/dist/connect/http.d.ts +36 -0
- package/dist/connect/http.d.ts.map +1 -0
- package/dist/connect/http.js +51 -0
- package/dist/connect/http.js.map +1 -0
- package/dist/connect/logs.d.ts +53 -0
- package/dist/connect/logs.d.ts.map +1 -0
- package/dist/connect/logs.js +42 -0
- package/dist/connect/logs.js.map +1 -0
- package/dist/connect/result.d.ts +49 -0
- package/dist/connect/result.d.ts.map +1 -0
- package/dist/connect/result.js +41 -0
- package/dist/connect/result.js.map +1 -0
- package/dist/core/actors.d.ts +13 -0
- package/dist/core/actors.d.ts.map +1 -0
- package/dist/core/actors.js +57 -0
- package/dist/core/actors.js.map +1 -0
- package/dist/core/clock.d.ts +55 -0
- package/dist/core/clock.d.ts.map +1 -0
- package/dist/core/clock.js +36 -0
- package/dist/core/clock.js.map +1 -0
- package/dist/core/money.d.ts +32 -0
- package/dist/core/money.d.ts.map +1 -0
- package/dist/core/money.js +57 -0
- package/dist/core/money.js.map +1 -0
- package/dist/core/pool.d.ts +71 -0
- package/dist/core/pool.d.ts.map +1 -0
- package/dist/core/pool.js +98 -0
- package/dist/core/pool.js.map +1 -0
- package/dist/core/settle.d.ts +50 -0
- package/dist/core/settle.d.ts.map +1 -0
- package/dist/core/settle.js +175 -0
- package/dist/core/settle.js.map +1 -0
- package/dist/core/types.d.ts +109 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +6 -0
- package/dist/core/types.js.map +1 -0
- package/dist/erc8004/index.d.ts +94 -0
- package/dist/erc8004/index.d.ts.map +1 -0
- package/dist/erc8004/index.js +118 -0
- package/dist/erc8004/index.js.map +1 -0
- package/dist/erc8004/registry.generated.d.ts +17 -0
- package/dist/erc8004/registry.generated.d.ts.map +1 -0
- package/dist/erc8004/registry.generated.js +20 -0
- package/dist/erc8004/registry.generated.js.map +1 -0
- package/dist/erc8183/index.d.ts +114 -0
- package/dist/erc8183/index.d.ts.map +1 -0
- package/dist/erc8183/index.js +109 -0
- package/dist/erc8183/index.js.map +1 -0
- package/dist/index.d.ts +81 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/dist/judge/index.d.ts +64 -0
- package/dist/judge/index.d.ts.map +1 -0
- package/dist/judge/index.js +64 -0
- package/dist/judge/index.js.map +1 -0
- package/dist/launcher/index.d.ts +109 -0
- package/dist/launcher/index.d.ts.map +1 -0
- package/dist/launcher/index.js +144 -0
- package/dist/launcher/index.js.map +1 -0
- package/dist/negotiate/index.d.ts +166 -0
- package/dist/negotiate/index.d.ts.map +1 -0
- package/dist/negotiate/index.js +150 -0
- package/dist/negotiate/index.js.map +1 -0
- package/dist/onboard/index.d.ts +533 -0
- package/dist/onboard/index.d.ts.map +1 -0
- package/dist/onboard/index.js +509 -0
- package/dist/onboard/index.js.map +1 -0
- package/dist/onboarding/index.d.ts +177 -0
- package/dist/onboarding/index.d.ts.map +1 -0
- package/dist/onboarding/index.js +390 -0
- package/dist/onboarding/index.js.map +1 -0
- package/dist/pools/index.d.ts +191 -0
- package/dist/pools/index.d.ts.map +1 -0
- package/dist/pools/index.js +244 -0
- package/dist/pools/index.js.map +1 -0
- package/docs/demo.gif +0 -0
- package/docs/demo.tape +14 -0
- package/examples/01-settle-a-covered-job.mjs +18 -0
- package/examples/02-negotiate-a-hire.mjs +21 -0
- package/examples/03-end-to-end-offline.mjs +27 -0
- package/examples/10-grade-evidence.mjs +12 -0
- package/examples/11-grade-facts.mjs +10 -0
- package/examples/12-grade-answers.mjs +8 -0
- package/examples/13-grade-verdict.mjs +10 -0
- package/examples/14-grade-record.mjs +11 -0
- package/examples/15-grade-end-the-job.mjs +11 -0
- package/examples/16-grade-verify.mjs +9 -0
- package/examples/17-grade-live.mjs +14 -0
- package/examples/20-onboard-client.mjs +8 -0
- package/examples/21-onboard-provider.mjs +9 -0
- package/examples/22-onboard-evaluator.mjs +7 -0
- package/examples/23-onboard-backer.mjs +8 -0
- package/examples/30-read-a-real-job.mjs +19 -0
- package/examples/31-read-the-ending.mjs +15 -0
- package/examples/32-read-live-from-base.mjs +33 -0
- package/examples/33-read-many-jobs.mjs +12 -0
- package/examples/40-the-article.mjs +78 -0
- package/examples/README.md +93 -0
- package/examples/data/answers.mjs +8 -0
- package/examples/data/base-grade.recorded.json +72 -0
- package/examples/data/pilot-grade.recorded.json +78 -0
- package/examples/data/pilot-hire.json +35 -0
- package/examples/data/terms.mjs +11 -0
- package/examples/data/virtuals-job-74926.json +74 -0
- package/examples/data/virtuals-job-rejected.json +74 -0
- package/package.json +124 -0
- package/skills/moonbeam-acp-decoding/SKILL.md +80 -0
- package/skills/moonbeam-actor-backer/SKILL.md +81 -0
- package/skills/moonbeam-actor-challenger/SKILL.md +62 -0
- package/skills/moonbeam-actor-client/SKILL.md +67 -0
- package/skills/moonbeam-actor-doubter/SKILL.md +55 -0
- package/skills/moonbeam-actor-evaluator/SKILL.md +67 -0
- package/skills/moonbeam-actor-provider/SKILL.md +63 -0
- package/skills/moonbeam-assurance/SKILL.md +58 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export declare const STAMP_VERDICTS: readonly ["delivered_as_specified", "delivered_with_defects", "not_delivered", "cannot_determine"];
|
|
2
|
+
export type StampVerdict = (typeof STAMP_VERDICTS)[number];
|
|
3
|
+
export declare const STAMP_VERDICT_CODE: Readonly<Record<StampVerdict, 1 | 2 | 3 | 4>>;
|
|
4
|
+
export declare const STAMP_MODE: Readonly<{
|
|
5
|
+
readonly deterministic: 1;
|
|
6
|
+
readonly calibrated: 2;
|
|
7
|
+
}>;
|
|
8
|
+
export type StampMode = keyof typeof STAMP_MODE;
|
|
9
|
+
/** deployed registries; the devnet is the only one today (Base and Arc go through the deployer ceremony) */
|
|
10
|
+
export declare const GRADE_STAMP_REGISTRY: Readonly<Record<number, string>>;
|
|
11
|
+
/** the ERC-8004 Reputation Registry sits at one address on every chain it is deployed to */
|
|
12
|
+
export declare const ERC8004_REPUTATION_REGISTRY: string;
|
|
13
|
+
/** The grade as the judge answered it: closed question, answer list, model version, and the three fields. */
|
|
14
|
+
export interface Grade {
|
|
15
|
+
id: string;
|
|
16
|
+
question: string;
|
|
17
|
+
options: readonly string[];
|
|
18
|
+
model: string | null;
|
|
19
|
+
value: StampVerdict;
|
|
20
|
+
probabilities: Readonly<Record<string, number>>;
|
|
21
|
+
confidence: number;
|
|
22
|
+
}
|
|
23
|
+
/** Canonical form: this exact key order, no whitespace. The digest is sha256 over it. */
|
|
24
|
+
export declare function canonicalGrade(g: Grade): string;
|
|
25
|
+
export declare function gradeDigest(g: Grade): string;
|
|
26
|
+
export declare function keccak256Hex(hexData: string): string;
|
|
27
|
+
/** subjectOf(chainId, contract, jobId) exactly as the registry computes it: keccak256(abi.encode("taifoon.stamp.subject.v1", chainId, contract, jobId)). */
|
|
28
|
+
export declare function subjectOf(chainId: number | bigint, jobContract: string, jobId: string | number | bigint): string;
|
|
29
|
+
export interface UnsignedCall {
|
|
30
|
+
chainId: number;
|
|
31
|
+
to: string;
|
|
32
|
+
data: string;
|
|
33
|
+
value: '0x0';
|
|
34
|
+
what: string;
|
|
35
|
+
signer: string;
|
|
36
|
+
}
|
|
37
|
+
/** The unsigned `stamp(subject, digest, verdict, mode, model, uri)` call for the GradeStampRegistry. */
|
|
38
|
+
export declare function encodeStamp(a: {
|
|
39
|
+
chainId: number;
|
|
40
|
+
subject: string;
|
|
41
|
+
grade: Grade;
|
|
42
|
+
mode: StampMode;
|
|
43
|
+
uri: string;
|
|
44
|
+
registry?: string;
|
|
45
|
+
}): UnsignedCall;
|
|
46
|
+
/** The unsigned ERC-8004 `giveFeedback(...)` call carrying the grade as feedback about `agentId`: tag1 "jev-grade", tag2 the verdict, value the confidence in hundredths, feedbackHash the digest. */
|
|
47
|
+
export declare function encodeErc8004Feedback(a: {
|
|
48
|
+
chainId: number;
|
|
49
|
+
agentId: number | bigint;
|
|
50
|
+
grade: Grade;
|
|
51
|
+
endpoint: string;
|
|
52
|
+
feedbackURI: string;
|
|
53
|
+
registry?: string;
|
|
54
|
+
}): UnsignedCall;
|
|
55
|
+
/** Does a stamp's digest match a grade you hold? The whole point of the registry. */
|
|
56
|
+
export declare function verifyStamp(stampDigest: string, grade: Grade): boolean;
|
|
57
|
+
//# sourceMappingURL=stamp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamp.d.ts","sourceRoot":"","sources":["../../src/adapters/stamp.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,cAAc,oGAAqG,CAAC;AACjI,MAAM,MAAM,YAAY,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AAC3D,eAAO,MAAM,kBAAkB,EAAE,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,CAAC,GAAG,CAAC,GAAG,CAA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AAgB,WAAW,CAAC,CAAC,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,SAAS,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,YAAY,CAShJ;AAED,sMAAsM;AACtM,wBAAgB,qBAAqB,CAAC,CAAC,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IAAC,KAAK,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,YAAY,CAQ5K;AAED,qFAAqF;AACrF,wBAAgB,WAAW,CAAC,WAAW,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,GAAG,OAAO,CAA2E"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stamp adapter — a grade, stamped on chain, that anyone recomputes.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `@taifoon/stamp` so an integrator holding a Verdict from this SDK can put its digest on
|
|
5
|
+
* chain in one of two places without a second dependency:
|
|
6
|
+
* - the GradeStampRegistry (immutable, append-only; deployed on the Taifoon devnet today), or
|
|
7
|
+
* - the ERC-8004 Reputation Registry, as feedback about the agent that did the work.
|
|
8
|
+
*
|
|
9
|
+
* Pure: everything here returns calldata or a hash. Nothing signs, nothing sends, nothing reads a
|
|
10
|
+
* clock. A stamp is an attestation, never a proof; `mode` says which kind of grade it carries.
|
|
11
|
+
*/
|
|
12
|
+
import { createHash } from 'node:crypto';
|
|
13
|
+
import { ERC8004_CANONICAL } from '../erc8004/registry.generated.js';
|
|
14
|
+
export const STAMP_VERDICTS = ['delivered_as_specified', 'delivered_with_defects', 'not_delivered', 'cannot_determine'];
|
|
15
|
+
export const STAMP_VERDICT_CODE = Object.freeze({ delivered_as_specified: 1, delivered_with_defects: 2, not_delivered: 3, cannot_determine: 4 });
|
|
16
|
+
export const STAMP_MODE = Object.freeze({ deterministic: 1, calibrated: 2 });
|
|
17
|
+
/** deployed registries; the devnet is the only one today (Base and Arc go through the deployer ceremony) */
|
|
18
|
+
export const GRADE_STAMP_REGISTRY = Object.freeze({ 36927: '0xff5B4852AC7066D04FEd8de97bdA52401d03ad6E' });
|
|
19
|
+
/** the ERC-8004 Reputation Registry sits at one address on every chain it is deployed to */
|
|
20
|
+
export const ERC8004_REPUTATION_REGISTRY = ERC8004_CANONICAL.reputation.toLowerCase();
|
|
21
|
+
/** Canonical form: this exact key order, no whitespace. The digest is sha256 over it. */
|
|
22
|
+
export function canonicalGrade(g) {
|
|
23
|
+
return JSON.stringify({ id: g.id, question: g.question, options: g.options, model: g.model, value: g.value, probabilities: g.probabilities, confidence: g.confidence });
|
|
24
|
+
}
|
|
25
|
+
export function gradeDigest(g) { return '0x' + createHash('sha256').update(canonicalGrade(g)).digest('hex'); }
|
|
26
|
+
// ── minimal ABI encoding, so this adapter carries no chain library ─────────────────────────────────
|
|
27
|
+
const hex = (n, bytes = 32) => n.toString(16).padStart(bytes * 2, '0');
|
|
28
|
+
const word = (n) => hex(BigInt(n));
|
|
29
|
+
const addr = (a) => a.toLowerCase().replace(/^0x/, '').padStart(64, '0');
|
|
30
|
+
const b32 = (h) => { const s = h.toLowerCase().replace(/^0x/, ''); if (s.length !== 64)
|
|
31
|
+
throw new Error('expected 32 bytes'); return s; };
|
|
32
|
+
const utf8 = (s) => Buffer.from(s, 'utf8');
|
|
33
|
+
const dyn = (s) => { const b = utf8(s); const padded = Math.ceil(b.length / 32) * 32; return word(b.length) + b.toString('hex').padEnd(padded * 2, '0'); };
|
|
34
|
+
/** abi.encode over a head/tail layout: static words inline, dynamic strings offset-pointed */
|
|
35
|
+
function abiEncode(parts) {
|
|
36
|
+
const headLen = parts.length * 32;
|
|
37
|
+
let tail = '';
|
|
38
|
+
let head = '';
|
|
39
|
+
for (const p of parts) {
|
|
40
|
+
if (p.kind === 'static')
|
|
41
|
+
head += p.word;
|
|
42
|
+
else {
|
|
43
|
+
head += word(headLen + tail.length / 2);
|
|
44
|
+
tail += dyn(p.value);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return head + tail;
|
|
48
|
+
}
|
|
49
|
+
export function keccak256Hex(hexData) {
|
|
50
|
+
// Keccak-256 without a dependency: a compact implementation of the permutation.
|
|
51
|
+
const RC = [1n, 0x8082n, 0x800000000000808an, 0x8000000080008000n, 0x808bn, 0x80000001n, 0x8000000080008081n, 0x8000000000008009n, 0x8an, 0x88n, 0x80008009n, 0x8000000an, 0x8000808bn, 0x800000000000008bn, 0x8000000000008089n, 0x8000000000008003n, 0x8000000000008002n, 0x8000000000000080n, 0x800an, 0x800000008000000an, 0x8000000080008081n, 0x8000000000008080n, 0x80000001n, 0x8000000080008008n];
|
|
52
|
+
const ROT = [[0, 36, 3, 41, 18], [1, 44, 10, 45, 2], [62, 6, 43, 15, 61], [28, 55, 25, 21, 56], [27, 20, 39, 8, 14]];
|
|
53
|
+
const M = (1n << 64n) - 1n;
|
|
54
|
+
const rol = (x, n) => n === 0 ? x : (((x << BigInt(n)) | (x >> BigInt(64 - n))) & M);
|
|
55
|
+
const bytes = Buffer.from(hexData.replace(/^0x/, ''), 'hex');
|
|
56
|
+
const rate = 136;
|
|
57
|
+
const padded = Buffer.concat([bytes, Buffer.alloc(rate - (bytes.length % rate))]);
|
|
58
|
+
padded[bytes.length] = (padded[bytes.length] ?? 0) ^ 0x01;
|
|
59
|
+
padded[padded.length - 1] = (padded[padded.length - 1] ?? 0) ^ 0x80;
|
|
60
|
+
const st = new Array(25).fill(0n);
|
|
61
|
+
for (let off = 0; off < padded.length; off += rate) {
|
|
62
|
+
for (let i = 0; i < rate / 8; i++)
|
|
63
|
+
st[i] = st[i] ^ padded.readBigUInt64LE(off + i * 8);
|
|
64
|
+
for (let r = 0; r < 24; r++) {
|
|
65
|
+
const c = [0, 1, 2, 3, 4].map((x) => st[x] ^ st[x + 5] ^ st[x + 10] ^ st[x + 15] ^ st[x + 20]);
|
|
66
|
+
const d = [0, 1, 2, 3, 4].map((x) => c[(x + 4) % 5] ^ rol(c[(x + 1) % 5], 1));
|
|
67
|
+
for (let i = 0; i < 25; i++)
|
|
68
|
+
st[i] = st[i] ^ d[i % 5];
|
|
69
|
+
const b = new Array(25).fill(0n);
|
|
70
|
+
for (let x = 0; x < 5; x++)
|
|
71
|
+
for (let y = 0; y < 5; y++)
|
|
72
|
+
b[y + 5 * ((2 * x + 3 * y) % 5)] = rol(st[x + 5 * y], ROT[x][y]);
|
|
73
|
+
for (let x = 0; x < 5; x++)
|
|
74
|
+
for (let y = 0; y < 5; y++)
|
|
75
|
+
st[x + 5 * y] = b[x + 5 * y] ^ (~b[(x + 1) % 5 + 5 * y] & b[(x + 2) % 5 + 5 * y]);
|
|
76
|
+
st[0] = st[0] ^ RC[r];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const out = Buffer.alloc(32);
|
|
80
|
+
for (let i = 0; i < 4; i++)
|
|
81
|
+
out.writeBigUInt64LE(st[i], i * 8);
|
|
82
|
+
return '0x' + out.toString('hex');
|
|
83
|
+
}
|
|
84
|
+
const selector = (sig) => keccak256Hex('0x' + utf8(sig).toString('hex')).slice(0, 10);
|
|
85
|
+
/** subjectOf(chainId, contract, jobId) exactly as the registry computes it: keccak256(abi.encode("taifoon.stamp.subject.v1", chainId, contract, jobId)). */
|
|
86
|
+
export function subjectOf(chainId, jobContract, jobId) {
|
|
87
|
+
const id32 = typeof jobId === 'string' && jobId.startsWith('0x') ? b32(jobId) : hex(BigInt(jobId));
|
|
88
|
+
return keccak256Hex('0x' + abiEncode([{ kind: 'string', value: 'taifoon.stamp.subject.v1' }, { kind: 'static', word: word(BigInt(chainId)) }, { kind: 'static', word: addr(jobContract) }, { kind: 'static', word: id32 }]));
|
|
89
|
+
}
|
|
90
|
+
/** The unsigned `stamp(subject, digest, verdict, mode, model, uri)` call for the GradeStampRegistry. */
|
|
91
|
+
export function encodeStamp(a) {
|
|
92
|
+
const registry = a.registry ?? GRADE_STAMP_REGISTRY[a.chainId];
|
|
93
|
+
if (!registry)
|
|
94
|
+
throw new Error(`no GradeStampRegistry known on chain ${a.chainId}`);
|
|
95
|
+
const data = selector('stamp(bytes32,bytes32,uint8,uint8,string,string)') + abiEncode([
|
|
96
|
+
{ kind: 'static', word: b32(a.subject) }, { kind: 'static', word: b32(gradeDigest(a.grade)) },
|
|
97
|
+
{ kind: 'static', word: word(STAMP_VERDICT_CODE[a.grade.value]) }, { kind: 'static', word: word(STAMP_MODE[a.mode]) },
|
|
98
|
+
{ kind: 'string', value: a.grade.model ?? 'unknown' }, { kind: 'string', value: a.uri },
|
|
99
|
+
]);
|
|
100
|
+
return { chainId: a.chainId, to: registry, data, value: '0x0', what: `stamp ${a.grade.value} (${a.mode}) for subject ${a.subject.slice(0, 10)}… — append-only, never edited`, signer: 'anyone; the stamper is whoever signs' };
|
|
101
|
+
}
|
|
102
|
+
/** The unsigned ERC-8004 `giveFeedback(...)` call carrying the grade as feedback about `agentId`: tag1 "jev-grade", tag2 the verdict, value the confidence in hundredths, feedbackHash the digest. */
|
|
103
|
+
export function encodeErc8004Feedback(a) {
|
|
104
|
+
const value = BigInt(Math.round(Math.max(0, Math.min(1, a.grade.confidence)) * 100));
|
|
105
|
+
const data = selector('giveFeedback(uint256,int128,uint8,string,string,string,string,bytes32)') + abiEncode([
|
|
106
|
+
{ kind: 'static', word: word(BigInt(a.agentId)) }, { kind: 'static', word: word(value) }, { kind: 'static', word: word(2) },
|
|
107
|
+
{ kind: 'string', value: 'jev-grade' }, { kind: 'string', value: a.grade.value }, { kind: 'string', value: a.endpoint }, { kind: 'string', value: a.feedbackURI },
|
|
108
|
+
{ kind: 'static', word: b32(gradeDigest(a.grade)) },
|
|
109
|
+
]);
|
|
110
|
+
return { chainId: a.chainId, to: a.registry ?? ERC8004_REPUTATION_REGISTRY, data, value: '0x0', what: `ERC-8004 feedback on agent ${a.agentId}: jev-grade / ${a.grade.value}, value ${value} (confidence, 2 decimals), feedbackHash = grade digest`, signer: 'any address that is not the agent’s owner or operator' };
|
|
111
|
+
}
|
|
112
|
+
/** Does a stamp's digest match a grade you hold? The whole point of the registry. */
|
|
113
|
+
export function verifyStamp(stampDigest, grade) { return stampDigest.toLowerCase() === gradeDigest(grade).toLowerCase(); }
|
|
114
|
+
//# sourceMappingURL=stamp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stamp.js","sourceRoot":"","sources":["../../src/adapters/stamp.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAErE,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,wBAAwB,EAAE,wBAAwB,EAAE,eAAe,EAAE,kBAAkB,CAAU,CAAC;AAEjI,MAAM,CAAC,MAAM,kBAAkB,GAAkD,MAAM,CAAC,MAAM,CAAC,EAAE,sBAAsB,EAAE,CAAC,EAAE,sBAAsB,EAAE,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,CAAC,CAAC;AAChM,MAAM,CAAC,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,aAAa,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC,EAAW,CAAC,CAAC;AAGtF,4GAA4G;AAC5G,MAAM,CAAC,MAAM,oBAAoB,GAAqC,MAAM,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,4CAA4C,EAAE,CAAC,CAAC;AAC7I,4FAA4F;AAC5F,MAAM,CAAC,MAAM,2BAA2B,GAAG,iBAAiB,CAAC,UAAW,CAAC,WAAW,EAAE,CAAC;AAavF,yFAAyF;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IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE;KACpD,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,EAAE,CAAC,CAAC,QAAQ,IAAI,2BAA2B,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,8BAA8B,CAAC,CAAC,OAAO,iBAAiB,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW,KAAK,wDAAwD,EAAE,MAAM,EAAE,uDAAuD,EAAE,CAAC;AACzT,CAAC;AAED,qFAAqF;AACrF,MAAM,UAAU,WAAW,CAAC,WAAmB,EAAE,KAAY,IAAa,OAAO,WAAW,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { Address, Hex, Hex32 } from '../acp/hex.js';
|
|
2
|
+
/**
|
|
3
|
+
* Building the transactions that drive an insured job.
|
|
4
|
+
*
|
|
5
|
+
* This module builds calls; it does not sign or send them. Two reasons, and
|
|
6
|
+
* both are deliberate:
|
|
7
|
+
*
|
|
8
|
+
* - The SDK never holds a key. A package that models economics has no
|
|
9
|
+
* business owning a signing surface, and callers already have a wallet.
|
|
10
|
+
* - Every builder is testable offline. A returned call is a value you can
|
|
11
|
+
* assert on, with no chain, no mocking of a provider, and no keys in CI.
|
|
12
|
+
*
|
|
13
|
+
* Pass the result to whatever your application already uses to send
|
|
14
|
+
* transactions.
|
|
15
|
+
*/
|
|
16
|
+
export interface CallRequest {
|
|
17
|
+
readonly to: Address;
|
|
18
|
+
readonly data: Hex;
|
|
19
|
+
readonly value: bigint;
|
|
20
|
+
readonly chainId: number;
|
|
21
|
+
/** What this call does, for logging and for confirmation prompts. */
|
|
22
|
+
readonly summary: string;
|
|
23
|
+
}
|
|
24
|
+
/** Function selectors, each the first four bytes of its signature's hash. */
|
|
25
|
+
export declare const SELECTOR: {
|
|
26
|
+
readonly lock: "0x9f1f9b45";
|
|
27
|
+
readonly release: "0x8e6c1cfd";
|
|
28
|
+
readonly refundOnTimeout: "0x3f0ed84b";
|
|
29
|
+
readonly dispute: "0x0e6ecc42";
|
|
30
|
+
readonly complete: "0xd75bbdf3";
|
|
31
|
+
readonly reject: "0x41dd26f5";
|
|
32
|
+
};
|
|
33
|
+
export interface ChainConfig {
|
|
34
|
+
readonly chainId: number;
|
|
35
|
+
readonly escrow: Address;
|
|
36
|
+
readonly taskSpace?: Address;
|
|
37
|
+
readonly evaluator?: Address;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Chain ids the SDK will build calls for.
|
|
41
|
+
*
|
|
42
|
+
* Mainnets are deliberately absent: the contracts these calls target are not
|
|
43
|
+
* deployed there, so a built transaction would be addressed to nothing. Adding
|
|
44
|
+
* a mainnet id here without a verified deployment would turn a compile-time
|
|
45
|
+
* refusal into a lost transaction.
|
|
46
|
+
*/
|
|
47
|
+
export declare const SUPPORTED_CHAIN_IDS: readonly number[];
|
|
48
|
+
export declare class UnsupportedChainError extends Error {
|
|
49
|
+
constructor(chainId: number);
|
|
50
|
+
}
|
|
51
|
+
export declare function assertSupportedChain(chainId: number): void;
|
|
52
|
+
/** A proof blob. Non-empty by construction — see `approve` below. */
|
|
53
|
+
export type ProofBlob = Hex & {
|
|
54
|
+
readonly __brand: 'ProofBlob';
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Wrap a blob as a proof, rejecting anything empty.
|
|
58
|
+
*
|
|
59
|
+
* The brand is what lets `approve` demand a proof at the type level: an empty
|
|
60
|
+
* or absent blob cannot be turned into one, so it cannot reach a call that
|
|
61
|
+
* releases funds.
|
|
62
|
+
*/
|
|
63
|
+
export declare function asProofBlob(value: Hex): ProofBlob;
|
|
64
|
+
export interface EscrowCalls {
|
|
65
|
+
/** Client locks payment for a task, with the deadline that makes refunds claimable. */
|
|
66
|
+
lock(args: {
|
|
67
|
+
taskId: Hex32;
|
|
68
|
+
agent: Address;
|
|
69
|
+
deadline: number;
|
|
70
|
+
value: bigint;
|
|
71
|
+
}): CallRequest;
|
|
72
|
+
/** Client accepts delivery and releases payment. */
|
|
73
|
+
release(args: {
|
|
74
|
+
taskId: Hex32;
|
|
75
|
+
deliverableHash: Hex32;
|
|
76
|
+
proof?: Hex;
|
|
77
|
+
}): CallRequest;
|
|
78
|
+
/**
|
|
79
|
+
* Claim a refund after the deadline.
|
|
80
|
+
*
|
|
81
|
+
* Callable by anyone, which is the property that stops a silent counterparty
|
|
82
|
+
* holding funds hostage. It is not automatic: someone must call it.
|
|
83
|
+
*/
|
|
84
|
+
claimRefund(args: {
|
|
85
|
+
taskId: Hex32;
|
|
86
|
+
}): CallRequest;
|
|
87
|
+
/** Either party freezes the escrow for arbitration. */
|
|
88
|
+
dispute(args: {
|
|
89
|
+
taskId: Hex32;
|
|
90
|
+
}): CallRequest;
|
|
91
|
+
}
|
|
92
|
+
export interface EvaluatorCalls {
|
|
93
|
+
/**
|
|
94
|
+
* Approve a deliverable and release payment. Requires a proof.
|
|
95
|
+
*
|
|
96
|
+
* The `ProofBlob` type is not decorative: paying out someone else's money
|
|
97
|
+
* must be backed by something that verified, and this signature makes it
|
|
98
|
+
* impossible to call otherwise.
|
|
99
|
+
*/
|
|
100
|
+
approve(args: {
|
|
101
|
+
jobId: bigint;
|
|
102
|
+
proof: ProofBlob;
|
|
103
|
+
}): CallRequest;
|
|
104
|
+
/**
|
|
105
|
+
* Reject a deliverable. Requires no proof.
|
|
106
|
+
*
|
|
107
|
+
* Refusing to pay is always safe — it withholds rather than moves — so it
|
|
108
|
+
* carries no proof obligation. Keeping this asymmetry visible in the two
|
|
109
|
+
* signatures is the point.
|
|
110
|
+
*/
|
|
111
|
+
reject(args: {
|
|
112
|
+
jobId: bigint;
|
|
113
|
+
reason: Hex32;
|
|
114
|
+
}): CallRequest;
|
|
115
|
+
}
|
|
116
|
+
export interface AssuranceClient {
|
|
117
|
+
readonly chainId: number;
|
|
118
|
+
readonly escrow: EscrowCalls;
|
|
119
|
+
readonly evaluator: EvaluatorCalls;
|
|
120
|
+
}
|
|
121
|
+
/** Build a client for a chain the contracts actually exist on. */
|
|
122
|
+
export declare function createAssuranceClient(config: ChainConfig): AssuranceClient;
|
|
123
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AAEH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC;IACnB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,qEAAqE;IACrE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED,6EAA6E;AAC7E,eAAO,MAAM,QAAQ;;;;;;;CAOX,CAAC;AAOX,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,EAAE,SAAS,MAAM,EAA0B,CAAC;AAE5E,qBAAa,qBAAsB,SAAQ,KAAK;gBAClC,OAAO,EAAE,MAAM;CAO5B;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAE1D;AAED,qEAAq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kE;AAClE,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,WAAW,GAAG,eAAe,CAoE1E"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** Function selectors, each the first four bytes of its signature's hash. */
|
|
2
|
+
export const SELECTOR = {
|
|
3
|
+
lock: '0x9f1f9b45',
|
|
4
|
+
release: '0x8e6c1cfd',
|
|
5
|
+
refundOnTimeout: '0x3f0ed84b',
|
|
6
|
+
dispute: '0x0e6ecc42',
|
|
7
|
+
complete: '0xd75bbdf3',
|
|
8
|
+
reject: '0x41dd26f5',
|
|
9
|
+
};
|
|
10
|
+
const pad = (hex) => hex.replace(/^0x/, '').toLowerCase().padStart(64, '0');
|
|
11
|
+
const encodeAddress = (value) => pad(value);
|
|
12
|
+
const encodeUint = (value) => pad(BigInt(value).toString(16));
|
|
13
|
+
const encodeBytes32 = (value) => pad(value);
|
|
14
|
+
/**
|
|
15
|
+
* Chain ids the SDK will build calls for.
|
|
16
|
+
*
|
|
17
|
+
* Mainnets are deliberately absent: the contracts these calls target are not
|
|
18
|
+
* deployed there, so a built transaction would be addressed to nothing. Adding
|
|
19
|
+
* a mainnet id here without a verified deployment would turn a compile-time
|
|
20
|
+
* refusal into a lost transaction.
|
|
21
|
+
*/
|
|
22
|
+
export const SUPPORTED_CHAIN_IDS = [84532, 36927, 31337];
|
|
23
|
+
export class UnsupportedChainError extends Error {
|
|
24
|
+
constructor(chainId) {
|
|
25
|
+
super(`chain ${chainId} is not supported: the assurance contracts are not deployed there. ` +
|
|
26
|
+
`Supported: ${SUPPORTED_CHAIN_IDS.join(', ')}.`);
|
|
27
|
+
this.name = 'UnsupportedChainError';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export function assertSupportedChain(chainId) {
|
|
31
|
+
if (!SUPPORTED_CHAIN_IDS.includes(chainId))
|
|
32
|
+
throw new UnsupportedChainError(chainId);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Wrap a blob as a proof, rejecting anything empty.
|
|
36
|
+
*
|
|
37
|
+
* The brand is what lets `approve` demand a proof at the type level: an empty
|
|
38
|
+
* or absent blob cannot be turned into one, so it cannot reach a call that
|
|
39
|
+
* releases funds.
|
|
40
|
+
*/
|
|
41
|
+
export function asProofBlob(value) {
|
|
42
|
+
if (!value || value === '0x') {
|
|
43
|
+
throw new RangeError('a proof blob may not be empty: approving a payment requires proof');
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
/** Build a client for a chain the contracts actually exist on. */
|
|
48
|
+
export function createAssuranceClient(config) {
|
|
49
|
+
assertSupportedChain(config.chainId);
|
|
50
|
+
const { chainId, escrow, evaluator } = config;
|
|
51
|
+
const call = (to, data, value, summary) => ({
|
|
52
|
+
to,
|
|
53
|
+
data: `0x${data}`,
|
|
54
|
+
value,
|
|
55
|
+
chainId,
|
|
56
|
+
summary,
|
|
57
|
+
});
|
|
58
|
+
const requireEvaluator = () => {
|
|
59
|
+
if (!evaluator) {
|
|
60
|
+
throw new Error('no evaluator contract configured for this chain');
|
|
61
|
+
}
|
|
62
|
+
return evaluator;
|
|
63
|
+
};
|
|
64
|
+
return {
|
|
65
|
+
chainId,
|
|
66
|
+
escrow: {
|
|
67
|
+
lock: ({ taskId, agent, deadline, value }) => call(escrow, SELECTOR.lock.slice(2) + encodeBytes32(taskId) + encodeAddress(agent) + encodeUint(deadline), value, `lock ${value} for task ${taskId} until ${deadline}`),
|
|
68
|
+
release: ({ taskId, deliverableHash }) => call(escrow, SELECTOR.release.slice(2) + encodeBytes32(taskId) + encodeBytes32(deliverableHash), 0n, `release payment for task ${taskId}`),
|
|
69
|
+
claimRefund: ({ taskId }) => call(escrow, SELECTOR.refundOnTimeout.slice(2) + encodeBytes32(taskId), 0n, `claim the refund for task ${taskId} (callable by anyone after the deadline)`),
|
|
70
|
+
dispute: ({ taskId }) => call(escrow, SELECTOR.dispute.slice(2) + encodeBytes32(taskId), 0n, `raise a dispute on task ${taskId} and freeze the escrow`),
|
|
71
|
+
},
|
|
72
|
+
evaluator: {
|
|
73
|
+
approve: ({ jobId }) => call(requireEvaluator(), SELECTOR.complete.slice(2) + encodeUint(jobId), 0n, `approve job ${jobId} against a verified proof`),
|
|
74
|
+
reject: ({ jobId, reason }) => call(requireEvaluator(), SELECTOR.reject.slice(2) + encodeUint(jobId) + encodeBytes32(reason), 0n, `reject job ${jobId} (no proof required)`),
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AA0BA,6EAA6E;AAC7E,MAAM,CAAC,MAAM,QAAQ,GAAG;IACtB,IAAI,EAAE,YAAY;IAClB,OAAO,EAAE,YAAY;IACrB,eAAe,EAAE,YAAY;IAC7B,OAAO,EAAE,YAAY;IACrB,QAAQ,EAAE,YAAY;IACtB,MAAM,EAAE,YAAY;CACZ,CAAC;AAEX,MAAM,GAAG,GAAG,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC;AAC5F,MAAM,aAAa,GAAG,CAAC,KAAc,EAAU,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AAC7D,MAAM,UAAU,GAAG,CAAC,KAAsB,EAAU,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;AACvF,MAAM,aAAa,GAAG,CAAC,KAAY,EAAU,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;AAS3D;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAsB,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;AAE5E,MAAM,O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E,CAC5B,IAAI,CACF,gBAAgB,EAAE,EAClB,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,aAAa,CAAC,MAAM,CAAC,EACpE,EAAE,EACF,cAAc,KAAK,sBAAsB,CAC1C;SACJ;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Clock, FinalityAttestation } from '../core/clock.js';
|
|
2
|
+
import type { LookupError, Result } from './result.js';
|
|
3
|
+
/**
|
|
4
|
+
* Reading a signed finality attestation, and — crucially — checking it.
|
|
5
|
+
*
|
|
6
|
+
* An attestation arrives carrying a `signer` field. That field is *self-
|
|
7
|
+
* reported*: a source claiming "this was signed by X" proves nothing until the
|
|
8
|
+
* signature is recovered and compared. So verification is a separate, explicit
|
|
9
|
+
* step, and the SDK never presents an unverified attestation as evidence.
|
|
10
|
+
*
|
|
11
|
+
* The recovery primitive is injected rather than bundled, so the package keeps
|
|
12
|
+
* no runtime dependency and callers use whatever crypto they already have.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Recovers the signing address from a digest and signature.
|
|
16
|
+
*
|
|
17
|
+
* **How the digest is signed matters, and getting it wrong fails silently.**
|
|
18
|
+
* Recovery always returns *some* address; a mismatched scheme returns a
|
|
19
|
+
* plausible-looking wrong one rather than an error. Attestations are commonly
|
|
20
|
+
* signed as EIP-191 personal messages over the digest bytes rather than as a
|
|
21
|
+
* raw hash, so a recovery that treats the digest as a bare hash will produce
|
|
22
|
+
* an address that never matches and no explanation of why.
|
|
23
|
+
*
|
|
24
|
+
* With viem, that means:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* const ecrecover: Ecrecover = (digest, signature) =>
|
|
28
|
+
* recoverMessageAddress({ message: { raw: digest as Hex }, signature: signature as Hex });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* Verify against a known-good attestation once before relying on it.
|
|
32
|
+
*/
|
|
33
|
+
export type Ecrecover = (digest: string, signature: string) => string | Promise<string>;
|
|
34
|
+
export interface AttestationSource {
|
|
35
|
+
/**
|
|
36
|
+
* Fetch the latest finality attestation for a chain.
|
|
37
|
+
*
|
|
38
|
+
* Returns `noData` when the source has nothing for this chain — an honest
|
|
39
|
+
* answer, not a failure. A chain that is not covered simply is not covered.
|
|
40
|
+
*/
|
|
41
|
+
latest(chainId: number): Promise<Result<FinalityAttestation, LookupError>>;
|
|
42
|
+
}
|
|
43
|
+
export type Verification = {
|
|
44
|
+
readonly verified: true;
|
|
45
|
+
readonly signer: string;
|
|
46
|
+
} | {
|
|
47
|
+
readonly verified: false;
|
|
48
|
+
readonly reason: string;
|
|
49
|
+
};
|
|
50
|
+
/** Does this attestation's signature actually recover to the address it claims? */
|
|
51
|
+
export declare function verifyAttestation(attestation: FinalityAttestation, ecrecover: Ecrecover): Promise<Verification>;
|
|
52
|
+
/** Is this attestation signed by a signer we are willing to rely on? */
|
|
53
|
+
export declare function isSignedByTrusted(attestation: FinalityAttestation, trusted: readonly string[], ecrecover: Ecrecover): Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* Turn an attestation into a clock, but only once its signature checks out.
|
|
56
|
+
*
|
|
57
|
+
* An unverified attestation is just a number someone sent you, so it is
|
|
58
|
+
* refused here rather than quietly downgraded to a local clock — a caller that
|
|
59
|
+
* asked for attested time should hear that it could not be provided.
|
|
60
|
+
*/
|
|
61
|
+
export declare function clockFromAttestation(attestation: FinalityAttestation, ecrecover: Ecrecover): Promise<Result<Clock, LookupError>>;
|
|
62
|
+
/** Shape-check a parsed attestation payload before trusting its fields. */
|
|
63
|
+
export declare function parseAttestation(payload: unknown): Result<FinalityAttestation, LookupError>;
|
|
64
|
+
//# sourceMappingURL=attestation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attestation.d.ts","sourceRoot":"","sources":["../../src/connect/attestation.ts"],"names":[],"mappings":"AACA,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E,WAAW,CAAC,CAwB3F"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { attestedClock } from '../core/clock.js';
|
|
2
|
+
import { err, ok } from './result.js';
|
|
3
|
+
/** Does this attestation's signature actually recover to the address it claims? */
|
|
4
|
+
export async function verifyAttestation(attestation, ecrecover) {
|
|
5
|
+
try {
|
|
6
|
+
const recovered = await ecrecover(attestation.digest, attestation.signature);
|
|
7
|
+
if (recovered.toLowerCase() !== attestation.signer.toLowerCase()) {
|
|
8
|
+
return {
|
|
9
|
+
verified: false,
|
|
10
|
+
reason: `signature recovers to ${recovered}, not the claimed ${attestation.signer}`,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
return { verified: true, signer: recovered };
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
return { verified: false, reason: `could not recover a signer: ${String(cause)}` };
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** Is this attestation signed by a signer we are willing to rely on? */
|
|
20
|
+
export async function isSignedByTrusted(attestation, trusted, ecrecover) {
|
|
21
|
+
const result = await verifyAttestation(attestation, ecrecover);
|
|
22
|
+
if (!result.verified)
|
|
23
|
+
return false;
|
|
24
|
+
return trusted.some((address) => address.toLowerCase() === result.signer.toLowerCase());
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Turn an attestation into a clock, but only once its signature checks out.
|
|
28
|
+
*
|
|
29
|
+
* An unverified attestation is just a number someone sent you, so it is
|
|
30
|
+
* refused here rather than quietly downgraded to a local clock — a caller that
|
|
31
|
+
* asked for attested time should hear that it could not be provided.
|
|
32
|
+
*/
|
|
33
|
+
export async function clockFromAttestation(attestation, ecrecover) {
|
|
34
|
+
const verification = await verifyAttestation(attestation, ecrecover);
|
|
35
|
+
if (!verification.verified) {
|
|
36
|
+
return err({ kind: 'malformed', message: verification.reason });
|
|
37
|
+
}
|
|
38
|
+
return ok(attestedClock(attestation));
|
|
39
|
+
}
|
|
40
|
+
/** Shape-check a parsed attestation payload before trusting its fields. */
|
|
41
|
+
export function parseAttestation(payload) {
|
|
42
|
+
if (typeof payload !== 'object' || payload === null) {
|
|
43
|
+
return err({ kind: 'malformed', message: 'attestation payload is not an object' });
|
|
44
|
+
}
|
|
45
|
+
const p = payload;
|
|
46
|
+
const chainId = p['chain_id'] ?? p['chainId'];
|
|
47
|
+
const finalizedBlock = p['finalized_block'] ?? p['finalizedBlock'];
|
|
48
|
+
const timestamp = p['timestamp'];
|
|
49
|
+
const digest = p['digest'];
|
|
50
|
+
const signature = p['signature'];
|
|
51
|
+
const signer = p['signer'];
|
|
52
|
+
if (typeof chainId !== 'number' ||
|
|
53
|
+
typeof finalizedBlock !== 'number' ||
|
|
54
|
+
typeof timestamp !== 'number' ||
|
|
55
|
+
typeof digest !== 'string' ||
|
|
56
|
+
typeof signature !== 'string' ||
|
|
57
|
+
typeof signer !== 'string') {
|
|
58
|
+
return err({ kind: 'malformed', message: 'attestation is missing required fields' });
|
|
59
|
+
}
|
|
60
|
+
return ok({ chainId, finalizedBlock, timestamp, digest, signature, signer });
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=attestation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attestation.js","sourceRoot":"","sources":["../../src/connect/attestation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAkDtC,mFAAmF;AACnF,MAAM,CAAC,KAAK,UAAU,iBAAiB,CACrC,WAAgC,EAChC,SAAoB;IAEpB,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,MAAM,SAAS,CAAC,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;QAC7E,IAAI,SAAS,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC,MAAM,CAAC,WAAW,EAAE,EAAE,CAAC;YACjE,OAAO;gBACL,Q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AO,KAAK,IAAI,EAAE,CAAC;QACpD,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,sCAAsC,EAAE,CAAC,CAAC;IACrF,CAAC;IACD,MAAM,CAAC,GAAG,OAAkC,CAAC;IAC7C,MAAM,OAAO,GAAG,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,CAAC;IAC9C,MAAM,cAAc,GAAG,CAAC,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,gBAAgB,CAAC,CAAC;IACnE,MAAM,SAAS,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;IAC3B,MAAM,SAAS,GAAG,CAAC,CAAC,WAAW,CAAC,CAAC;IACjC,MAAM,MAAM,GAAG,CAAC,CAAC,QAAQ,CAAC,CAAC;IAE3B,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,cAAc,KAAK,QAAQ;QAClC,OAAO,SAAS,KAAK,QAAQ;QAC7B,OAAO,MAAM,KAAK,QAAQ;QAC1B,OAAO,SAAS,KAAK,QAAQ;QAC7B,OAAO,MAAM,KAAK,QAAQ,EAC1B,CAAC;QACD,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,wCAAwC,EAAE,CAAC,CAAC;IACvF,CAAC;IAED,OAAO,EAAE,CAAC,EAAE,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC,CAAC;AAC/E,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { AttestationSource } from './attestation.js';
|
|
2
|
+
import type { LookupError, Result } from './result.js';
|
|
3
|
+
/**
|
|
4
|
+
* HTTP-backed sources.
|
|
5
|
+
*
|
|
6
|
+
* `fetch` is injected so every test runs offline against recorded responses,
|
|
7
|
+
* and so a caller can supply its own retry, caching or logging without the SDK
|
|
8
|
+
* inventing a policy for any of them.
|
|
9
|
+
*/
|
|
10
|
+
export type Fetch = (url: string, init?: {
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
}) => Promise<{
|
|
13
|
+
readonly ok: boolean;
|
|
14
|
+
readonly status: number;
|
|
15
|
+
json(): Promise<unknown>;
|
|
16
|
+
}>;
|
|
17
|
+
export interface HttpSourceOptions {
|
|
18
|
+
readonly baseUrl: string;
|
|
19
|
+
readonly fetch?: Fetch;
|
|
20
|
+
readonly timeoutMs?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Reads signed finality attestations over HTTP. */
|
|
23
|
+
export declare function createAttestationSource(options: HttpSourceOptions): AttestationSource;
|
|
24
|
+
/** Identifies one event: chain, transaction, and position within it. */
|
|
25
|
+
export interface EventRef {
|
|
26
|
+
readonly chainId: number;
|
|
27
|
+
readonly txHash: string;
|
|
28
|
+
readonly logIndex: number;
|
|
29
|
+
}
|
|
30
|
+
export declare function formatEventRef(ref: EventRef): string;
|
|
31
|
+
export interface ProofSource {
|
|
32
|
+
/** Fetch a proof blob for one event, for onward on-chain verification. */
|
|
33
|
+
forEvent(ref: EventRef): Promise<Result<unknown, LookupError>>;
|
|
34
|
+
}
|
|
35
|
+
export declare function createProofSource(options: HttpSourceOptions): ProofSource;
|
|
36
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/connect/http.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAG1D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEvD;;;;;;GAMG;AAEH,MAAM,MAAM,KAAK,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,KAAK,OAAO,CAAC;I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