@certen.io/sdk 0.9.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.
- package/CHANGELOG.md +153 -0
- package/README.md +111 -0
- package/dist/address.d.ts +12 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +48 -0
- package/dist/address.js.map +1 -0
- package/dist/agent.d.ts +8 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +12 -0
- package/dist/agent.js.map +1 -1
- package/dist/browser.d.ts +47 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +55 -0
- package/dist/browser.js.map +1 -0
- package/dist/bytes.d.ts +14 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +47 -0
- package/dist/bytes.js.map +1 -0
- package/dist/chains.d.ts +218 -0
- package/dist/chains.d.ts.map +1 -0
- package/dist/chains.js +295 -0
- package/dist/chains.js.map +1 -0
- package/dist/client.d.ts +17 -2
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +54 -8
- package/dist/client.js.map +1 -1
- package/dist/defaults.d.ts +3 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +3 -0
- package/dist/defaults.js.map +1 -0
- package/dist/doctor.d.ts +10 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +42 -27
- package/dist/doctor.js.map +1 -1
- package/dist/effect.d.ts +99 -0
- package/dist/effect.d.ts.map +1 -0
- package/dist/effect.js +139 -0
- package/dist/effect.js.map +1 -0
- package/dist/error-codes.d.ts.map +1 -1
- package/dist/error-codes.js +17 -0
- package/dist/error-codes.js.map +1 -1
- package/dist/errors.d.ts +76 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +108 -0
- package/dist/errors.js.map +1 -1
- package/dist/evm-rpc.d.ts +55 -0
- package/dist/evm-rpc.d.ts.map +1 -0
- package/dist/evm-rpc.js +126 -0
- package/dist/evm-rpc.js.map +1 -0
- package/dist/execution-proof.d.ts.map +1 -1
- package/dist/execution-proof.js +2 -1
- package/dist/execution-proof.js.map +1 -1
- package/dist/funding.d.ts +214 -6
- package/dist/funding.d.ts.map +1 -1
- package/dist/funding.js +251 -29
- package/dist/funding.js.map +1 -1
- package/dist/header-fields.d.ts +68 -0
- package/dist/header-fields.d.ts.map +1 -0
- package/dist/header-fields.js +147 -0
- package/dist/header-fields.js.map +1 -0
- package/dist/index.d.ts +9 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -26
- package/dist/index.js.map +1 -1
- package/dist/intent-states.d.ts +75 -0
- package/dist/intent-states.d.ts.map +1 -0
- package/dist/intent-states.js +66 -0
- package/dist/intent-states.js.map +1 -0
- package/dist/internal.d.ts +18 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +44 -0
- package/dist/internal.js.map +1 -1
- package/dist/key-page.d.ts +67 -0
- package/dist/key-page.d.ts.map +1 -0
- package/dist/key-page.js +154 -0
- package/dist/key-page.js.map +1 -0
- package/dist/origin.d.ts +19 -0
- package/dist/origin.d.ts.map +1 -0
- package/dist/origin.js +80 -0
- package/dist/origin.js.map +1 -0
- package/dist/random.d.ts +27 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +30 -0
- package/dist/random.js.map +1 -0
- package/dist/readiness.d.ts +133 -0
- package/dist/readiness.d.ts.map +1 -0
- package/dist/readiness.js +467 -0
- package/dist/readiness.js.map +1 -0
- package/dist/reason-codes.d.ts +15 -0
- package/dist/reason-codes.d.ts.map +1 -0
- package/dist/reason-codes.js +30 -0
- package/dist/reason-codes.js.map +1 -0
- package/dist/receipt.d.ts +233 -0
- package/dist/receipt.d.ts.map +1 -0
- package/dist/receipt.js +318 -0
- package/dist/receipt.js.map +1 -0
- package/dist/resources/admin.d.ts +5 -2
- package/dist/resources/admin.d.ts.map +1 -1
- package/dist/resources/admin.js +8 -3
- package/dist/resources/admin.js.map +1 -1
- package/dist/resources/billing.d.ts.map +1 -1
- package/dist/resources/billing.js +5 -4
- package/dist/resources/billing.js.map +1 -1
- package/dist/resources/chains.d.ts +10 -0
- package/dist/resources/chains.d.ts.map +1 -1
- package/dist/resources/chains.js +15 -1
- package/dist/resources/chains.js.map +1 -1
- package/dist/resources/device.d.ts.map +1 -1
- package/dist/resources/device.js +2 -1
- package/dist/resources/device.js.map +1 -1
- package/dist/resources/execute.d.ts +110 -4
- package/dist/resources/execute.d.ts.map +1 -1
- package/dist/resources/execute.js +207 -35
- package/dist/resources/execute.js.map +1 -1
- package/dist/resources/governance.js +3 -3
- package/dist/resources/governance.js.map +1 -1
- package/dist/resources/identity.js +5 -5
- package/dist/resources/identity.js.map +1 -1
- package/dist/resources/oauth-clients.d.ts.map +1 -1
- package/dist/resources/oauth-clients.js +3 -2
- package/dist/resources/oauth-clients.js.map +1 -1
- package/dist/resources/proof.d.ts +10 -2
- package/dist/resources/proof.d.ts.map +1 -1
- package/dist/resources/proof.js +33 -11
- package/dist/resources/proof.js.map +1 -1
- package/dist/resources/registration-tokens.d.ts.map +1 -1
- package/dist/resources/registration-tokens.js +2 -1
- package/dist/resources/registration-tokens.js.map +1 -1
- package/dist/resources/sign.js +2 -2
- package/dist/resources/sign.js.map +1 -1
- package/dist/resources/transaction.d.ts.map +1 -1
- package/dist/resources/transaction.js +7 -3
- package/dist/resources/transaction.js.map +1 -1
- package/dist/resources/transparency.d.ts.map +1 -1
- package/dist/resources/transparency.js +3 -2
- package/dist/resources/transparency.js.map +1 -1
- package/dist/resources/webhooks.d.ts +5 -3
- package/dist/resources/webhooks.d.ts.map +1 -1
- package/dist/resources/webhooks.js +12 -8
- package/dist/resources/webhooks.js.map +1 -1
- package/dist/sha256.d.ts +10 -0
- package/dist/sha256.d.ts.map +1 -0
- package/dist/sha256.js +71 -0
- package/dist/sha256.js.map +1 -0
- package/dist/share-target.d.ts +10 -0
- package/dist/share-target.d.ts.map +1 -0
- package/dist/share-target.js +25 -0
- package/dist/share-target.js.map +1 -0
- package/dist/shared-proof.d.ts +1 -5
- package/dist/shared-proof.d.ts.map +1 -1
- package/dist/shared-proof.js +27 -21
- package/dist/shared-proof.js.map +1 -1
- package/dist/signing-check.d.ts +66 -0
- package/dist/signing-check.d.ts.map +1 -0
- package/dist/signing-check.js +171 -0
- package/dist/signing-check.js.map +1 -0
- package/dist/types.d.ts +139 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/verify-receipt.d.ts.map +1 -1
- package/dist/verify-receipt.js +68 -28
- package/dist/verify-receipt.js.map +1 -1
- package/dist/verify.d.ts +206 -0
- package/dist/verify.d.ts.map +1 -0
- package/dist/verify.js +369 -0
- package/dist/verify.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +4 -0
- package/dist/version.js.map +1 -0
- package/package.json +25 -7
package/dist/origin.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { CertenForeignOriginError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* The client only ever talks to its own gateway.
|
|
4
|
+
*
|
|
5
|
+
* The API key travels on every request the client sends, and axios sends an instance's default headers to an ABSOLUTE url on any
|
|
6
|
+
* host (axios 1.19.0 and 1.20.0 alike; reproduced against a local listener in RUNLOG_RB7b F16). Some responses name a url the client
|
|
7
|
+
* is expected to follow (`submit_url` on an intent and on a sign request). A compromised or buggy gateway, or anything able to
|
|
8
|
+
* alter a response, could therefore name `https://evil.example/x` and receive the key with the signature.
|
|
9
|
+
*
|
|
10
|
+
* So every url the client is about to request must resolve to the client's own base origin, and is refused by name before any
|
|
11
|
+
* request is sent otherwise (`FOREIGN_ORIGIN_URL`, with the url in `details`). Refusing is the only safe answer: a url that
|
|
12
|
+
* "looks right" but points elsewhere is the attack.
|
|
13
|
+
*/
|
|
14
|
+
/** How axios decides a url is absolute: a scheme, or a protocol-relative `//host`. */
|
|
15
|
+
const AXIOS_ABSOLUTE = /^([a-z][a-z\d+\-.]*:)?\/\//i;
|
|
16
|
+
/** Resolve `url` the way axios would build the request, so the check is on the url that is actually sent. */
|
|
17
|
+
function effectiveUrl(url, baseURL) {
|
|
18
|
+
const base = new URL(baseURL);
|
|
19
|
+
if (AXIOS_ABSOLUTE.test(url))
|
|
20
|
+
return new URL(url, base); // `//host/x` takes the base's scheme
|
|
21
|
+
// axios `combineURLs`: base without trailing slashes + '/' + url without leading slashes.
|
|
22
|
+
return new URL(`${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`);
|
|
23
|
+
}
|
|
24
|
+
/** Would a request for `url` stay on `baseURL`'s origin, with no credentials smuggled in the url? */
|
|
25
|
+
export function checkOwnOrigin(url, baseURL) {
|
|
26
|
+
if (!baseURL)
|
|
27
|
+
return { ok: true }; // a client with no base url sends absolute urls by construction; nothing to compare against
|
|
28
|
+
if (url === undefined || url === '')
|
|
29
|
+
return { ok: true };
|
|
30
|
+
let target;
|
|
31
|
+
let base;
|
|
32
|
+
try {
|
|
33
|
+
base = new URL(baseURL);
|
|
34
|
+
target = effectiveUrl(url, baseURL);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return { ok: false, reason: 'is not a url this client can resolve' };
|
|
38
|
+
}
|
|
39
|
+
if (target.origin !== base.origin) {
|
|
40
|
+
return { ok: false, reason: `leaves ${base.origin} for ${target.origin}`, resolved: target.href };
|
|
41
|
+
}
|
|
42
|
+
if (target.username || target.password) {
|
|
43
|
+
return { ok: false, reason: 'carries credentials in the url', resolved: target.href };
|
|
44
|
+
}
|
|
45
|
+
return { ok: true, resolved: target.href };
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Throw `CertenForeignOriginError` unless `url` stays on the client's own origin.
|
|
49
|
+
* `what` names where the url came from (`submit_url`, `request`, `redirect`), so the message says what to distrust.
|
|
50
|
+
*/
|
|
51
|
+
export function assertOwnOrigin(url, baseURL, what) {
|
|
52
|
+
const r = checkOwnOrigin(url, baseURL);
|
|
53
|
+
if (!r.ok) {
|
|
54
|
+
throw new CertenForeignOriginError(`certen: refusing to send a request to ${JSON.stringify(url)} (${what}): it ${r.reason}. `
|
|
55
|
+
+ 'The client only talks to its own gateway, so nothing was sent and no credential left this process.', String(url), baseURL ?? '', what);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Is a redirect from `from` to `to` safe for a request carrying credentials?
|
|
60
|
+
* Same origin, or an http -> https upgrade of the same host on the default ports (a gateway configured as `http://` that
|
|
61
|
+
* redirects to its own TLS port); never another host, and never https -> http.
|
|
62
|
+
*/
|
|
63
|
+
export function redirectStaysOnOrigin(from, to) {
|
|
64
|
+
let a;
|
|
65
|
+
let b;
|
|
66
|
+
try {
|
|
67
|
+
a = new URL(from);
|
|
68
|
+
b = new URL(to);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
if (b.username || b.password)
|
|
74
|
+
return false;
|
|
75
|
+
if (a.origin === b.origin)
|
|
76
|
+
return true;
|
|
77
|
+
return a.protocol === 'http:' && b.protocol === 'https:' && a.hostname === b.hostname
|
|
78
|
+
&& (a.port === '' || a.port === '80') && (b.port === '' || b.port === '443');
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=origin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"origin.js","sourceRoot":"","sources":["../src/origin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAEvD;;;;;;;;;;;GAWG;AAEH,sFAAsF;AACtF,MAAM,cAAc,GAAG,6BAA6B,CAAC;AAErD,6GAA6G;AAC7G,SAAS,YAAY,CAAC,GAAW,EAAE,OAAe;IAChD,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,CAAC;IAC9B,IAAI,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,qCAAqC;IAC9F,0FAA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d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographically random values from Web Crypto, which Node (>= 19) and every current browser expose as `globalThis.crypto`.
|
|
3
|
+
*
|
|
4
|
+
* The client used `randomBytes` from `node:crypto` and `randomUUID` from `crypto`, which a browser bundle cannot resolve. There is
|
|
5
|
+
* no weaker fallback here: where `crypto.getRandomValues` is absent the call throws, because an idempotency key drawn from
|
|
6
|
+
* `Math.random` would let two clients collide on the key that stops a payment being made twice.
|
|
7
|
+
*/
|
|
8
|
+
/** The few Web Crypto members this SDK uses, declared structurally because the SDK's type environment has no DOM lib. */
|
|
9
|
+
export interface WebCryptoLike {
|
|
10
|
+
getRandomValues(array: Uint8Array): Uint8Array;
|
|
11
|
+
randomUUID?: () => string;
|
|
12
|
+
subtle?: {
|
|
13
|
+
importKey(format: 'raw', key: Uint8Array, algorithm: {
|
|
14
|
+
name: string;
|
|
15
|
+
}, extractable: boolean, usages: string[]): Promise<unknown>;
|
|
16
|
+
verify(algorithm: {
|
|
17
|
+
name: string;
|
|
18
|
+
}, key: unknown, signature: Uint8Array, data: Uint8Array): Promise<boolean>;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** `globalThis.crypto` when the runtime has it, otherwise undefined. */
|
|
22
|
+
export declare function maybeWebCrypto(): WebCryptoLike | undefined;
|
|
23
|
+
export declare function randomBytes(n: number): Uint8Array;
|
|
24
|
+
export declare function randomHex(n: number): string;
|
|
25
|
+
/** An RFC 4122 version 4 UUID. `crypto.randomUUID` needs a secure context in browsers; building it from random bytes does not. */
|
|
26
|
+
export declare function uuid(): string;
|
|
27
|
+
//# sourceMappingURL=random.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,yHAAyH;AACzH,MAAM,WAAW,aAAa;IAC5B,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAC;IAC/C,UAAU,CAAC,EAAE,MAAM,MAAM,CAAC;IAC1B,MAAM,CAAC,EAAE;QACP,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,SAAS,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;QACjI,MAAM,CAAC,SAAS,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;KAC9G,CAAC;CACH;AAED,wEAAwE;AACxE,wBAAgB,cAAc,IAAI,aAAa,GAAG,SAAS,CAE1D;AAUD,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,GAAG,UAAU,CAEjD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAE3C;AAED,kIAAkI;AAClI,wBAAgB,IAAI,IAAI,MAAM,CAQ7B"}
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { bytesToHex } from './bytes.js';
|
|
2
|
+
/** `globalThis.crypto` when the runtime has it, otherwise undefined. */
|
|
3
|
+
export function maybeWebCrypto() {
|
|
4
|
+
return globalThis.crypto;
|
|
5
|
+
}
|
|
6
|
+
function webCrypto() {
|
|
7
|
+
const c = maybeWebCrypto();
|
|
8
|
+
if (!c || typeof c.getRandomValues !== 'function') {
|
|
9
|
+
throw new Error('certen: this runtime has no Web Crypto (globalThis.crypto.getRandomValues), so no secure random value can be made');
|
|
10
|
+
}
|
|
11
|
+
return c;
|
|
12
|
+
}
|
|
13
|
+
export function randomBytes(n) {
|
|
14
|
+
return webCrypto().getRandomValues(new Uint8Array(n));
|
|
15
|
+
}
|
|
16
|
+
export function randomHex(n) {
|
|
17
|
+
return bytesToHex(randomBytes(n));
|
|
18
|
+
}
|
|
19
|
+
/** An RFC 4122 version 4 UUID. `crypto.randomUUID` needs a secure context in browsers; building it from random bytes does not. */
|
|
20
|
+
export function uuid() {
|
|
21
|
+
const c = webCrypto();
|
|
22
|
+
if (typeof c.randomUUID === 'function')
|
|
23
|
+
return c.randomUUID();
|
|
24
|
+
const b = randomBytes(16);
|
|
25
|
+
b[6] = (b[6] & 0x0f) | 0x40;
|
|
26
|
+
b[8] = (b[8] & 0x3f) | 0x80;
|
|
27
|
+
const h = bytesToHex(b);
|
|
28
|
+
return `${h.slice(0, 8)}-${h.slice(8, 12)}-${h.slice(12, 16)}-${h.slice(16, 20)}-${h.slice(20)}`;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=random.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAmBxC,wEAAwE;AACxE,MAAM,UAAU,cAAc;IAC5B,OAAQ,UAAyC,CAAC,MAAM,CAAC;AAC3D,CAAC;AAED,SAAS,SAAS;IAChB,MAAM,CAAC,GAAG,cAAc,EAAE,CAAC;IAC3B,IAAI,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,eAAe,KAAK,UAAU,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CAAC,mHAAmH,CAAC,CAAC;IACvI,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,CAAS;IACnC,OAAO,SAAS,EAAE,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC;AACxD,CAAC;AAED,MAAM,UAAU,SAAS,CAAC,CAAS;IACjC,OAAO,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;AACpC,CAAC;AAED,kIAAkI;AAClI,MAAM,UAAU,IAAI;IAClB,MAAM,CAAC,GAAG,SAAS,EAAE,CAAC;IACtB,IAAI,OAAO,CAAC,CAAC,UAAU,KAAK,UAAU;QAAE,OAAO,CAAC,CAAC,UAAU,EAAE,CAAC;IAC9D,MAAM,CAAC,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC5B,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAC5B,MAAM,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC;IACxB,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,CAAC;AACnG,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,G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