@certen.io/sdk 0.10.0 → 0.11.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.
Files changed (60) hide show
  1. package/CHANGELOG.md +80 -0
  2. package/README.md +30 -0
  3. package/dist/address.d.ts +12 -0
  4. package/dist/address.d.ts.map +1 -0
  5. package/dist/address.js +48 -0
  6. package/dist/address.js.map +1 -0
  7. package/dist/browser.d.ts +17 -4
  8. package/dist/browser.d.ts.map +1 -1
  9. package/dist/browser.js +14 -2
  10. package/dist/browser.js.map +1 -1
  11. package/dist/chains.d.ts +20 -0
  12. package/dist/chains.d.ts.map +1 -1
  13. package/dist/chains.js +34 -0
  14. package/dist/chains.js.map +1 -1
  15. package/dist/client.d.ts +15 -1
  16. package/dist/client.d.ts.map +1 -1
  17. package/dist/client.js +16 -2
  18. package/dist/client.js.map +1 -1
  19. package/dist/doctor.d.ts +10 -1
  20. package/dist/doctor.d.ts.map +1 -1
  21. package/dist/doctor.js +6 -4
  22. package/dist/doctor.js.map +1 -1
  23. package/dist/effect.d.ts +99 -0
  24. package/dist/effect.d.ts.map +1 -0
  25. package/dist/effect.js +139 -0
  26. package/dist/effect.js.map +1 -0
  27. package/dist/evm-rpc.d.ts +55 -0
  28. package/dist/evm-rpc.d.ts.map +1 -0
  29. package/dist/evm-rpc.js +126 -0
  30. package/dist/evm-rpc.js.map +1 -0
  31. package/dist/funding.d.ts +171 -0
  32. package/dist/funding.d.ts.map +1 -1
  33. package/dist/funding.js +190 -8
  34. package/dist/funding.js.map +1 -1
  35. package/dist/key-page.d.ts +67 -0
  36. package/dist/key-page.d.ts.map +1 -0
  37. package/dist/key-page.js +154 -0
  38. package/dist/key-page.js.map +1 -0
  39. package/dist/readiness.d.ts +133 -0
  40. package/dist/readiness.d.ts.map +1 -0
  41. package/dist/readiness.js +467 -0
  42. package/dist/readiness.js.map +1 -0
  43. package/dist/receipt.d.ts +233 -0
  44. package/dist/receipt.d.ts.map +1 -0
  45. package/dist/receipt.js +318 -0
  46. package/dist/receipt.js.map +1 -0
  47. package/dist/resources/execute.d.ts +62 -1
  48. package/dist/resources/execute.d.ts.map +1 -1
  49. package/dist/resources/execute.js +102 -9
  50. package/dist/resources/execute.js.map +1 -1
  51. package/dist/shared-proof.d.ts.map +1 -1
  52. package/dist/shared-proof.js +21 -1
  53. package/dist/shared-proof.js.map +1 -1
  54. package/dist/verify.d.ts +126 -2
  55. package/dist/verify.d.ts.map +1 -1
  56. package/dist/verify.js +191 -2
  57. package/dist/verify.js.map +1 -1
  58. package/dist/version.d.ts +1 -1
  59. package/dist/version.js +1 -1
  60. package/package.json +1 -1
@@ -0,0 +1,67 @@
1
+ export interface KeyPageReading {
2
+ /** The key page that was read. */
3
+ url: string;
4
+ /** The Accumulate node that answered. */
5
+ rpc: string;
6
+ /** SHA-256 of each public key on the page, lower-case hex. */
7
+ keyHashes: string[];
8
+ /**
9
+ * The page's credit balance exactly as the node reports it. The spec does not pin whether that is whole credits or hundredths of one, so
10
+ * it is never converted here: zero is zero in either, and nothing else should be read as a quantity of credits without knowing which.
11
+ */
12
+ creditBalance: number | null;
13
+ version: number | null;
14
+ /** How many of the page's keys must sign for a transaction to execute. */
15
+ acceptThreshold: number | null;
16
+ }
17
+ export type KeyPageHold =
18
+ /** The page was read and lists the key. */
19
+ {
20
+ state: 'holds';
21
+ reading: KeyPageReading;
22
+ }
23
+ /** The page was read and does not list the key. A fact about the page, not a failure to read it. */
24
+ | {
25
+ state: 'absent';
26
+ reading: KeyPageReading;
27
+ }
28
+ /** The page could not be read. `notFound` is the node saying it has no such account (a wrong node, or an identity not yet anchored). */
29
+ | {
30
+ state: 'unreadable';
31
+ rpc: string;
32
+ reason: string;
33
+ notFound: boolean;
34
+ };
35
+ export interface KeyPageReadOptions {
36
+ /** The Accumulate API endpoint, e.g. `https://kermit.accumulatenetwork.io/v3`. */
37
+ rpc: string;
38
+ /** Injectable for tests; defaults to the platform `fetch`. */
39
+ fetch?: typeof fetch;
40
+ /** Default 10 seconds. */
41
+ timeoutMs?: number;
42
+ }
43
+ /**
44
+ * The key hashes on a key page, lower-case hex.
45
+ *
46
+ * Accumulate v3 returns `publicKeyHash` in more than one shape - a hex string, or a serialised Node Buffer (`{type:'Buffer',data:[...]}`)
47
+ * that leaked out of a JSON serializer - and the gateway normalises both for the same reason: a comparison that silently failed to parse
48
+ * one of them would report "this key is not on the page" for a key that is.
49
+ */
50
+ export declare function keyHashesOf(account: {
51
+ keys?: unknown;
52
+ } | null | undefined): string[];
53
+ /**
54
+ * Read one key page from an Accumulate node.
55
+ *
56
+ * @throws CertenError `ACCUMULATE_RPC_INVALID` (a bad endpoint, before any request), `KEY_PAGE_NOT_FOUND` (the node has no such account) or
57
+ * `KEY_PAGE_UNREADABLE` (anything else: no answer, a non-200, a malformed reply, an account that is not a key page).
58
+ */
59
+ export declare function readKeyPage(keyPageUrl: string, options: KeyPageReadOptions): Promise<KeyPageReading>;
60
+ /**
61
+ * Does this key page list this key?
62
+ *
63
+ * `keyHash` is the SHA-256 of the raw public key, hex (what `identity.create` takes as `publicKeyHash`). A malformed hash is a programming
64
+ * error and throws; a page that cannot be read is a result.
65
+ */
66
+ export declare function verifyKeyPageHolds(keyPageUrl: string, keyHash: string, options: KeyPageReadOptions): Promise<KeyPageHold>;
67
+ //# sourceMappingURL=key-page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-page.d.ts","sourceRoot":"","sources":["../src/key-page.ts"],"names":[],"mappings":"AAgCA,MAAM,WAAW,cAAc;IAC7B,kCAAkC;IAClC,GAAG,EAAE,MAAM,CAAC;IACZ,yCAAyC;IACzC,GAAG,EAAE,MAAM,CAAC;IACZ,8DAA8D;IAC9D,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB;;;OAGG;IACH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,0EAA0E;IAC1E,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC;AAED,MAAM,MAAM,WAAW;AACrB,2CAA2C;AACzC;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAAE;AAC7C,oGAAoG;GAClG;IAAE,KAAK,EAAE,QAAQ,CAAC;IAAC,OAAO,EAAE,cAAc,CAAA;CAAE;AAC9C,wIAAwI;GACtI;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAE5E,MAAM,WAAW,kBAAkB;IACjC,kFAAkF;IAClF,GAAG,EAAE,MAAM,CAAC;IACZ,8DAA8D;IAC9D,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,0BAA0B;IAC1B,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAUD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,EAAE,CAapF;AA6BD;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,cAAc,CAAC,CAiE1G;AAED;;;;;GAKG;AACH,wBAAsB,kBAAkB,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,WAAW,CAAC,CAe/H"}
@@ -0,0 +1,154 @@
1
+ import { CertenError } from './errors.js';
2
+ const DEFAULT_TIMEOUT_MS = 10_000;
3
+ /** A key page is a few kilobytes. Anything past this is not one, and is not read to the end. */
4
+ const MAX_RESPONSE_CHARS = 1_000_000;
5
+ const HEX = /^[0-9a-f]+$/;
6
+ const hex = (bytes) => bytes.map((b) => (b & 0xff).toString(16).padStart(2, '0')).join('');
7
+ /**
8
+ * The key hashes on a key page, lower-case hex.
9
+ *
10
+ * Accumulate v3 returns `publicKeyHash` in more than one shape - a hex string, or a serialised Node Buffer (`{type:'Buffer',data:[...]}`)
11
+ * that leaked out of a JSON serializer - and the gateway normalises both for the same reason: a comparison that silently failed to parse
12
+ * one of them would report "this key is not on the page" for a key that is.
13
+ */
14
+ export function keyHashesOf(account) {
15
+ const keys = account?.keys;
16
+ if (!Array.isArray(keys))
17
+ return [];
18
+ const out = [];
19
+ for (const k of keys) {
20
+ const raw = k?.publicKeyHash;
21
+ let h;
22
+ if (typeof raw === 'string')
23
+ h = raw.trim().replace(/^0x/i, '').toLowerCase();
24
+ else if (Array.isArray(raw))
25
+ h = hex(raw);
26
+ else if (raw && typeof raw === 'object' && Array.isArray(raw.data))
27
+ h = hex(raw.data);
28
+ if (h && h.length % 2 === 0 && HEX.test(h))
29
+ out.push(h);
30
+ }
31
+ return out;
32
+ }
33
+ function fail(message, code) {
34
+ throw new CertenError(message, 0, code);
35
+ }
36
+ function checkEndpoint(rpc) {
37
+ let u;
38
+ try {
39
+ u = new URL(rpc);
40
+ }
41
+ catch {
42
+ return fail(`certen: "${rpc}" is not a URL, so it cannot be an Accumulate API endpoint.`, 'ACCUMULATE_RPC_INVALID');
43
+ }
44
+ if (u.protocol !== 'https:' && u.protocol !== 'http:') {
45
+ fail(`certen: the Accumulate API endpoint must be http or https, not ${u.protocol}`, 'ACCUMULATE_RPC_INVALID');
46
+ }
47
+ // A credential in the URL would be sent on to whoever answers; there is no reason for one on a public read.
48
+ if (u.username || u.password) {
49
+ fail('certen: the Accumulate API endpoint must not carry a username or password in the URL.', 'ACCUMULATE_RPC_INVALID');
50
+ }
51
+ return u.toString();
52
+ }
53
+ const asNumber = (v) => {
54
+ if (v === undefined || v === null || v === '')
55
+ return null;
56
+ const n = Number(v);
57
+ return Number.isFinite(n) ? n : null;
58
+ };
59
+ /**
60
+ * Read one key page from an Accumulate node.
61
+ *
62
+ * @throws CertenError `ACCUMULATE_RPC_INVALID` (a bad endpoint, before any request), `KEY_PAGE_NOT_FOUND` (the node has no such account) or
63
+ * `KEY_PAGE_UNREADABLE` (anything else: no answer, a non-200, a malformed reply, an account that is not a key page).
64
+ */
65
+ export async function readKeyPage(keyPageUrl, options) {
66
+ if (!/^acc:\/\/\S+$/i.test(keyPageUrl)) {
67
+ fail(`certen: "${keyPageUrl}" is not an Accumulate URL (acc://...), so it cannot be a key page.`, 'KEY_PAGE_UNREADABLE');
68
+ }
69
+ const endpoint = checkEndpoint(options.rpc);
70
+ const doFetch = options.fetch ?? (typeof fetch === 'function' ? fetch : undefined);
71
+ if (!doFetch)
72
+ fail('certen: this environment has no fetch, so the Accumulate node cannot be asked.', 'KEY_PAGE_UNREADABLE');
73
+ const controller = new AbortController();
74
+ const timer = setTimeout(() => controller.abort(), options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
75
+ let text;
76
+ try {
77
+ const res = await doFetch(endpoint, {
78
+ method: 'POST',
79
+ headers: { 'content-type': 'application/json' },
80
+ // The same request the gateway makes for the same page.
81
+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'query', params: { scope: keyPageUrl } }),
82
+ // A node that redirects is not the node that was named. Not followed.
83
+ redirect: 'error',
84
+ signal: controller.signal,
85
+ });
86
+ if (!res.ok)
87
+ fail(`certen: the Accumulate node at ${endpoint} answered HTTP ${res.status}.`, 'KEY_PAGE_UNREADABLE');
88
+ text = await res.text();
89
+ }
90
+ catch (err) {
91
+ if (err instanceof CertenError)
92
+ throw err;
93
+ const why = controller.signal.aborted
94
+ ? `did not answer within ${(options.timeoutMs ?? DEFAULT_TIMEOUT_MS) / 1000}s`
95
+ : err instanceof Error ? err.message : String(err);
96
+ return fail(`certen: the Accumulate node at ${endpoint} ${why}`, 'KEY_PAGE_UNREADABLE');
97
+ }
98
+ finally {
99
+ clearTimeout(timer);
100
+ }
101
+ if (text.length > MAX_RESPONSE_CHARS) {
102
+ fail(`certen: the Accumulate node at ${endpoint} sent a reply too large to be a key page.`, 'KEY_PAGE_UNREADABLE');
103
+ }
104
+ let body;
105
+ try {
106
+ body = JSON.parse(text);
107
+ }
108
+ catch {
109
+ return fail(`certen: the Accumulate node at ${endpoint} did not answer with JSON.`, 'KEY_PAGE_UNREADABLE');
110
+ }
111
+ if (body?.error) {
112
+ const message = typeof body.error.message === 'string' ? body.error.message : JSON.stringify(body.error);
113
+ fail(`certen: the Accumulate node at ${endpoint} could not read ${keyPageUrl}: ${message}`, /not found|does not exist|no such/i.test(message) ? 'KEY_PAGE_NOT_FOUND' : 'KEY_PAGE_UNREADABLE');
114
+ }
115
+ const account = body?.result?.account;
116
+ if (!account || typeof account !== 'object') {
117
+ return fail(`certen: the Accumulate node at ${endpoint} returned no account record for ${keyPageUrl}.`, 'KEY_PAGE_UNREADABLE');
118
+ }
119
+ if (account.type !== undefined && account.type !== 'keyPage') {
120
+ fail(`certen: ${keyPageUrl} is a ${String(account.type)} on Accumulate, not a key page.`, 'KEY_PAGE_UNREADABLE');
121
+ }
122
+ return {
123
+ url: keyPageUrl,
124
+ rpc: endpoint,
125
+ keyHashes: keyHashesOf(account),
126
+ creditBalance: asNumber(account.creditBalance),
127
+ version: asNumber(account.version),
128
+ acceptThreshold: asNumber(account.acceptThreshold),
129
+ };
130
+ }
131
+ /**
132
+ * Does this key page list this key?
133
+ *
134
+ * `keyHash` is the SHA-256 of the raw public key, hex (what `identity.create` takes as `publicKeyHash`). A malformed hash is a programming
135
+ * error and throws; a page that cannot be read is a result.
136
+ */
137
+ export async function verifyKeyPageHolds(keyPageUrl, keyHash, options) {
138
+ const want = keyHash.trim().replace(/^0x/i, '').toLowerCase();
139
+ if (want.length !== 64 || !HEX.test(want)) {
140
+ throw new CertenError(`certen: "${keyHash}" is not a key hash. Expected 64 hex characters (SHA-256 of the public key).`, 0, 'INVALID_KEY_HASH');
141
+ }
142
+ let reading;
143
+ try {
144
+ reading = await readKeyPage(keyPageUrl, options);
145
+ }
146
+ catch (err) {
147
+ if (err instanceof CertenError && err.code !== 'ACCUMULATE_RPC_INVALID') {
148
+ return { state: 'unreadable', rpc: options.rpc, reason: err.message.replace(/^certen: /, ''), notFound: err.code === 'KEY_PAGE_NOT_FOUND' };
149
+ }
150
+ throw err;
151
+ }
152
+ return { state: reading.keyHashes.includes(want) ? 'holds' : 'absent', reading };
153
+ }
154
+ //# sourceMappingURL=key-page.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"key-page.js","sourceRoot":"","sources":["../src/key-page.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAkE1C,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,gGAAgG;AAChG,MAAM,kBAAkB,GAAG,SAAS,CAAC;AAErC,MAAM,GAAG,GAAG,aAAa,CAAC;AAE1B,MAAM,GAAG,GAAG,CAAC,KAAe,EAAU,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAE7G;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,OAA8C;IACxE,MAAM,IAAI,GAAG,OAAO,EAAE,IAAI,CAAC;IAC3B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC;IACpC,MAAM,GAAG,GAAa,EAAE,CAAC;IACzB,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;QACrB,MAAM,GAAG,GAAI,CAAwC,EAAE,aAAa,CAAC;QACrE,IAAI,CAAqB,CAAC;QAC1B,IAAI,OAAO,GAAG,KAAK,QAAQ;YAAE,CAAC,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,WAAW,E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,CAAC,KAAK,UAAU,kBAAkB,CAAC,UAAkB,EAAE,OAAe,EAAE,OAA2B;IACvG,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IAC9D,IAAI,IAAI,CAAC,MAAM,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,WAAW,CAAC,YAAY,OAAO,8EAA8E,EAAE,CAAC,EAAE,kBAAkB,CAAC,CAAC;IAClJ,CAAC;IACD,IAAI,OAAuB,CAAC;IAC5B,IAAI,CAAC;QACH,OAAO,GAAG,MAAM,WAAW,CAAC,UAAU,EAAE,OAAO,CAAC,CAAC;IACnD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,WAAW,IAAI,GAAG,CAAC,IAAI,KAAK,wBAAwB,EAAE,CAAC;YACxE,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC,EAAE,QAAQ,EAAE,GAAG,CAAC,IAAI,KAAK,oBAAoB,EAAE,CAAC;QAC9I,CAAC;QACD,MAAM,GAAG,CAAC;IACZ,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,QAAQ,EAAE,OAAO,EAAE,CAAC;AACnF,CAAC"}
@@ -0,0 +1,133 @@
1
+ import type { CertenClient } from './client.js';
2
+ import type { ChainsListResponse } from './types.js';
3
+ /**
4
+ * Is this identity, on this chain, able to act right now - and if not, what is the first thing in the way?
5
+ *
6
+ * Readiness used to be scattered. `doctor` answered org-wide questions ("some identity can sign", "every chain account has gas") and
7
+ * nothing answered the one a person has when they are about to send: *the identity and chain I am about to use*. The checks that
8
+ * decide whether an intent will execute were spread across three places that each saw a slice - the gateway's `can_sign` (one party's
9
+ * word on the key page), the funding guard (refuses only on a zero it has observed), and a hand-written `curl` in the docs. This is
10
+ * those, ordered by dependency, in one list a CLI card, an agent and the MCP server can all render.
11
+ *
12
+ * **Unknown is not OK.** A check that could not be answered is `unknown`, and the list is `ready` only when every check is `ok` or `warn`.
13
+ * That is deliberately stricter than the submit-time funding guard, which does not block on a balance it cannot read: a guard that
14
+ * blocked on a lagging view would refuse legitimate work, but a readiness card that said "ready" over a balance nobody read would be
15
+ * a lie. The two are different questions and are allowed to give different answers.
16
+ *
17
+ * **Ordered by dependency, and a failed prerequisite skips what it gates.** Nothing below an unreachable gateway or a rejected
18
+ * credential is meaningful, so it is reported as `skipped` rather than omitted: a list whose length depends on how far the run got is
19
+ * one a caller cannot index into.
20
+ *
21
+ * **Never throws for a failed check.** Like `doctor()`, a diagnosis that cannot report a broken setup is useless. It throws only for a
22
+ * malformed call (no identity, no chain).
23
+ *
24
+ * **Fixes are stated in plain words.** This module does not know it is being driven by a CLI and never names one; the CLI adds the command
25
+ * for a check's `code`, as `doctor` does. A `fix.url` is only ever one the SDK itself knows (a chain's faucet).
26
+ */
27
+ export type ReadinessStatus = 'ok' | 'warn' | 'fail' | 'unknown' | 'skipped';
28
+ export interface ReadinessFix {
29
+ /** What resolves it, in words. */
30
+ text: string;
31
+ /** A command that performs it. Set by a CLI, never by the SDK. */
32
+ command?: string;
33
+ /** Somewhere to do it. */
34
+ url?: string;
35
+ }
36
+ export interface ReadinessCheck {
37
+ name: string;
38
+ status: ReadinessStatus;
39
+ detail: string;
40
+ /** Machine-readable reason, on every check that is not `ok`. Stable: a caller may branch on it. */
41
+ code?: string;
42
+ /** Absent when the check passed. */
43
+ fix?: ReadinessFix;
44
+ /** The facts the verdict was drawn from, for a machine caller that would otherwise parse `detail`. Open: fields are only ever added. */
45
+ data?: Record<string, unknown>;
46
+ }
47
+ export type ReadinessState = 'ready' | 'blocked' | 'unconfirmed';
48
+ export interface ReadinessContext {
49
+ identity_id: string;
50
+ adi_url: string | null;
51
+ key_page_url: string | null;
52
+ chain: string;
53
+ /** The identity's abstract account on `chain`, once known. */
54
+ abstract_account: string | null;
55
+ /** The Accumulate node the independent key page read used, or null when none was made. */
56
+ accumulate_rpc: string | null;
57
+ key_hash: string | null;
58
+ }
59
+ export interface Readiness {
60
+ /** True only when `state` is `ready`: every check is `ok` or `warn`. */
61
+ ready: boolean;
62
+ /**
63
+ * `blocked`: something failed. `unconfirmed`: nothing failed, but something could not be answered (`unknown`) or was not evaluated
64
+ * (`skipped`). Never `ready` over a gap.
65
+ */
66
+ state: ReadinessState;
67
+ /** The first `fail`, else the first `unknown` / `skipped`; null when ready. */
68
+ first_blocker: ReadinessCheck | null;
69
+ /** Ordered by dependency; the same length however far the run got. */
70
+ checks: ReadinessCheck[];
71
+ context: ReadinessContext;
72
+ }
73
+ export interface ReadinessParams {
74
+ /** The identity's UUID. Resolving an ADI URL or a name is the caller's job: the gateway's identity routes take a UUID. */
75
+ identityId: string;
76
+ /** The chain about to be used, as a slug or a numeric id. */
77
+ chain: string;
78
+ /**
79
+ * SHA-256 of the signing key's raw public key, hex. Without it the key page can still be read but not compared with a key, and the check
80
+ * says so (`warn`) instead of claiming the page holds one.
81
+ */
82
+ keyHash?: string;
83
+ /** The native amount about to be moved, in whole units. Judged together with the gas reserve. */
84
+ amount?: string;
85
+ /** The token being moved. When it is not the chain's native token, the amount is not counted against the gas balance. */
86
+ tokenSymbol?: string;
87
+ /** Override the catalogue's gas reserve (whole native units); `null` for none. An estimate either way. */
88
+ gasReserve?: string | null;
89
+ /** The Accumulate API endpoint for the independent key page read. Default: the `accumulate.api` the gateway's chain registry advertises. */
90
+ accumulateRpc?: string;
91
+ /** The chains this client may use. Default: every catalogue chain that is not retired. */
92
+ configuredChains?: readonly string[];
93
+ /**
94
+ * `full` (the default) also checks the gateway, the credential and the organization's balance. `context` runs only what depends on the
95
+ * identity and chain, for a caller (such as `doctor`) that has already made the org-wide checks.
96
+ */
97
+ scope?: 'full' | 'context';
98
+ /**
99
+ * A chain registry the caller has already read (`doctor()` hands one back). The registry is a public document that changes rarely, and a caller
100
+ * that holds it should not pay for it again. Omitted, it is read. Used only with `scope: 'context'`: a `full` run's `gateway` check is the
101
+ * read of it, and a gateway reported reachable on the strength of an earlier read would be a claim nobody just checked.
102
+ */
103
+ registry?: ChainsListResponse;
104
+ /** Injectable for tests; defaults to the platform `fetch`. Used only for the Accumulate read. */
105
+ fetch?: typeof fetch;
106
+ /** How long to wait for the Accumulate node (default 10s). */
107
+ accumulateTimeoutMs?: number;
108
+ }
109
+ /** Every name `runReadiness` can emit, in the order it emits them. */
110
+ export declare const READINESS_CHECKS: readonly ["gateway", "credential", "identity", "chain", "key page", "accumulate credits", "gateway balance", "gas"];
111
+ /** The checks that depend on the identity and chain, as opposed to the organization. What `scope: 'context'` returns. */
112
+ export declare const CONTEXT_CHECKS: readonly ["identity", "chain", "key page", "accumulate credits", "gas"];
113
+ export declare function runReadiness(client: CertenClient, params: ReadinessParams): Promise<Readiness>;
114
+ /**
115
+ * The gas check on its own: the same judgement `runReadiness` makes, for a caller that is waiting for one number to change.
116
+ *
117
+ * `setup --wait-for-funds` and `gas --wait` poll this, not the whole of `readiness()`: the rest of it (the registry, the Accumulate key page,
118
+ * the organization's balance) cannot change while someone is sending gas from a faucet, and asking again every few seconds would be noise
119
+ * on the gateway and on a public Accumulate node. It is the SAME function `runReadiness` calls, so the wait and `status` cannot disagree
120
+ * about when an account counts as funded.
121
+ *
122
+ * Never throws for a read that failed: that is an `unknown` check carrying the HTTP status in `data.status`, so a poller can tell a
123
+ * rejected credential (stop) from a gateway having a bad moment (carry on). It throws only for a malformed call.
124
+ */
125
+ export declare function checkGas(client: CertenClient, params: Pick<ReadinessParams, 'identityId' | 'chain' | 'amount' | 'tokenSymbol' | 'gasReserve'>): Promise<ReadinessCheck>;
126
+ /**
127
+ * The verdict a list of checks adds up to.
128
+ *
129
+ * Exported because a caller that adds a check of its own - the CLI adds "is there a signing key on this machine", which the SDK cannot see -
130
+ * must get the verdict from the same rule rather than restate it.
131
+ */
132
+ export declare function summarizeReadiness(checks: readonly ReadinessCheck[]): Pick<Readiness, 'ready' | 'state' | 'first_blocker'>;
133
+ //# sourceMappingURL=readiness.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"readiness.d.ts","sourceRoot":"","sources":["../src/readiness.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAQhD,OAAO,KAAK,EAAmB,kBAAkB,EAAoB,MAAM,YAAY,CAAC;AAExF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;AAE7E,MAAM,WAAW,YAAY;IAC3B,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,0BAA0B;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,eAAe,CAAC;IACxB,MAAM,EAAE,MAAM,CAAC;IACf,mGAAmG;IACnG,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,oCAAoC;IACpC,GAAG,CAAC,EAAE,YAAY,CAAC;IACnB,wIAAwI;IACxI,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,MAAM,cAAc,GAAG,OAAO,GAAG,SAAS,GAAG,aAAa,CAAC;AAEjE,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,YAAY,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