@kya-os/mcp-i-runtime 1.3.2 → 1.3.3
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/.turbo/turbo-build.log +1 -1
- package/dist/auth/handshake.d.ts +23 -43
- package/dist/auth/handshake.d.ts.map +1 -1
- package/dist/auth/handshake.js +51 -82
- package/dist/auth/handshake.js.map +1 -1
- package/dist/auth/index.d.ts +1 -1
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js.map +1 -1
- package/dist/auth/resume-token.d.ts +52 -0
- package/dist/auth/resume-token.d.ts.map +1 -0
- package/dist/auth/resume-token.js +89 -0
- package/dist/auth/resume-token.js.map +1 -0
- package/dist/auth/types.d.ts +15 -0
- package/dist/auth/types.d.ts.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/dist/proof/companion.d.ts +25 -0
- package/dist/proof/companion.d.ts.map +1 -0
- package/dist/proof/companion.js +14 -0
- package/dist/proof/companion.js.map +1 -0
- package/dist/proof/generator.d.ts +16 -5
- package/dist/proof/generator.d.ts.map +1 -1
- package/dist/proof/generator.js +13 -3
- package/dist/proof/generator.js.map +1 -1
- package/dist/proof/index.d.ts +2 -1
- package/dist/proof/index.d.ts.map +1 -1
- package/dist/proof/index.js +3 -1
- package/dist/proof/index.js.map +1 -1
- package/dist/runtime/audit.d.ts.map +1 -1
- package/dist/runtime/audit.js +12 -0
- package/dist/runtime/audit.js.map +1 -1
- package/dist/runtime/dif-core.d.ts +8 -0
- package/dist/runtime/dif-core.d.ts.map +1 -1
- package/dist/runtime/dif-core.js +10 -0
- package/dist/runtime/dif-core.js.map +1 -1
- package/dist/services/claims-first-guard.d.ts +33 -0
- package/dist/services/claims-first-guard.d.ts.map +1 -0
- package/dist/services/claims-first-guard.js +77 -0
- package/dist/services/claims-first-guard.js.map +1 -0
- package/dist/services/holder-keys.d.ts +95 -0
- package/dist/services/holder-keys.d.ts.map +1 -0
- package/dist/services/holder-keys.js +281 -0
- package/dist/services/holder-keys.js.map +1 -0
- package/dist/services/holder-read-slots.d.ts +42 -0
- package/dist/services/holder-read-slots.d.ts.map +1 -0
- package/dist/services/holder-read-slots.js +82 -0
- package/dist/services/holder-read-slots.js.map +1 -0
- package/dist/services/request-holder.d.ts +78 -0
- package/dist/services/request-holder.d.ts.map +1 -0
- package/dist/services/request-holder.js +81 -0
- package/dist/services/request-holder.js.map +1 -0
- package/eslint.config.js +20 -0
- package/package.json +8 -2
- package/src/__tests__/runtime/audit.test.ts +108 -0
- package/src/auth/__tests__/verify-or-hints-presented.test.ts +155 -0
- package/src/auth/handshake.ts +80 -125
- package/src/auth/index.ts +1 -0
- package/src/auth/resume-token.ts +134 -0
- package/src/auth/types.ts +20 -2
- package/src/index.ts +25 -0
- package/src/proof/__tests__/proof-generator.test.ts +48 -4
- package/src/proof/companion.ts +35 -0
- package/src/proof/generator.ts +35 -8
- package/src/proof/index.ts +3 -0
- package/src/runtime/audit.ts +15 -3
- package/src/runtime/dif-core.ts +16 -0
- package/src/services/__tests__/claims-first-guard.test.ts +93 -0
- package/src/services/__tests__/holder-fixtures.ts +135 -0
- package/src/services/__tests__/holder-keys.test.ts +257 -0
- package/src/services/__tests__/holder-read-slots.test.ts +117 -0
- package/src/services/__tests__/holder-reads.test.ts +284 -0
- package/src/services/__tests__/proof-verifier.test.ts +8 -5
- package/src/services/__tests__/request-holder.test.ts +210 -0
- package/src/services/claims-first-guard.ts +89 -0
- package/src/services/holder-keys.ts +379 -0
- package/src/services/holder-read-slots.ts +85 -0
- package/src/services/request-holder.ts +146 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Holder keys: the keys a request proof's holder DID publishes, as the
|
|
3
|
+
* `resolveKey` and `resolveDidKeys` seams of `@kya-os/mcp/card`'s
|
|
4
|
+
* `requireProof` take them.
|
|
5
|
+
*
|
|
6
|
+
* A did:web holder's keys are the Ed25519 methods its DID document lists
|
|
7
|
+
* under `authentication`, the relationship by which a DID's subject proves it
|
|
8
|
+
* is the party calling. `@kya-os/mcp/card`'s `didKeyedJwks` projects them, as
|
|
9
|
+
* it does for a CIMD client's `private_key_jwt`. The document is read through
|
|
10
|
+
* `@kya-os/mcp`'s did:web resolver over its `RuntimeFetchProvider`: a bounded
|
|
11
|
+
* read under the private-network rule, with the connection pinned to the
|
|
12
|
+
* checked address where the runtime resolves host names, within a deadline,
|
|
13
|
+
* and cached per resolver.
|
|
14
|
+
*
|
|
15
|
+
* A holder whose document does not resolve has no keys, so its proof is
|
|
16
|
+
* refused. Why it did not resolve goes to the log, never to the caller, and
|
|
17
|
+
* the failure is remembered for a short while, so calls that prove nothing
|
|
18
|
+
* cannot each drive a read. A proof naming a key the cached document lacks,
|
|
19
|
+
* as after a key rotation, reads the document again, at most once a while
|
|
20
|
+
* per holder, when the resolver can drop what it cached (the library's
|
|
21
|
+
* did:web resolver can).
|
|
22
|
+
*
|
|
23
|
+
* Nothing has verified a proof when its holder's document is read, so a
|
|
24
|
+
* call can name any host. What that can cost is bounded three ways. A
|
|
25
|
+
* server that names `didWebHosts` reads no other host. Documents are read
|
|
26
|
+
* in slots (./holder-read-slots), a few at once in all and for one host.
|
|
27
|
+
* And the holders whose document has resolved before have slots of their
|
|
28
|
+
* own, so a flood of holders that never resolve cannot hold up the agents a
|
|
29
|
+
* server already serves. A failed read is remembered for the holder, not
|
|
30
|
+
* its host: one caller naming holders a host does not have must not refuse
|
|
31
|
+
* every agent that host does have. With no `didWebHosts`, a caller that
|
|
32
|
+
* hosts documents of its own can still fill the slots, which is why a
|
|
33
|
+
* server that knows its agents' hosts should name them.
|
|
34
|
+
*
|
|
35
|
+
* Every other holder goes to the seams the caller gives: each runtime keeps
|
|
36
|
+
* its own did:key resolution.
|
|
37
|
+
*
|
|
38
|
+
* @module services/holder-keys
|
|
39
|
+
*/
|
|
40
|
+
import type { DIDResolver, RuntimeFetchProviderOptions } from "@kya-os/mcp" with { "resolution-mode": "import" };
|
|
41
|
+
import { type DifCard } from "../runtime/dif-core";
|
|
42
|
+
type VerifyProofDeps = Parameters<DifCard["requireProof"]>[0];
|
|
43
|
+
/** A public key a request proof is verified with. */
|
|
44
|
+
export type ProofPublicJwk = Awaited<ReturnType<VerifyProofDeps["resolveKey"]>>;
|
|
45
|
+
/** The key seams a request proof is verified with. */
|
|
46
|
+
export interface HolderKeySeams {
|
|
47
|
+
/** The key `kid` names; throws when its DID publishes no such key. */
|
|
48
|
+
resolveKey(kid: string): Promise<ProofPublicJwk>;
|
|
49
|
+
/** Every key `did` publishes for the proof to be checked against. */
|
|
50
|
+
resolveDidKeys(did: string): Promise<ProofPublicJwk[]>;
|
|
51
|
+
}
|
|
52
|
+
export interface DidWebHolderOptions {
|
|
53
|
+
/**
|
|
54
|
+
* The did:web resolver. Default: one `@kya-os/mcp` did:web resolver per
|
|
55
|
+
* process over a `RuntimeFetchProvider`, so a holder's document stays
|
|
56
|
+
* cached across calls.
|
|
57
|
+
*/
|
|
58
|
+
didWebResolver?: DIDResolver;
|
|
59
|
+
/**
|
|
60
|
+
* Options for the `RuntimeFetchProvider` a holder's document is read
|
|
61
|
+
* through, when no `didWebResolver` is given: one resolver over a provider
|
|
62
|
+
* with these options serves these seams. `{ development: true }` reads a
|
|
63
|
+
* did:web holder on this machine, such as `did:web:localhost%3A8443`, in
|
|
64
|
+
* local development; the provider throws when it is built while
|
|
65
|
+
* `NODE_ENV` is `production`.
|
|
66
|
+
*/
|
|
67
|
+
didWebFetch?: RuntimeFetchProviderOptions;
|
|
68
|
+
/**
|
|
69
|
+
* The hosts whose did:web holders this server reads, each as a DID names
|
|
70
|
+
* it: `agents.example`, or `agents.example:8443` with its port. A holder
|
|
71
|
+
* on any other host is refused unread. Default: any host the
|
|
72
|
+
* private-network rule admits. Name them when you know where your agents'
|
|
73
|
+
* DIDs live, since a call names its holder before anything verifies it.
|
|
74
|
+
*/
|
|
75
|
+
didWebHosts?: readonly string[];
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The slots of the holders whose document has resolved before, and again of
|
|
79
|
+
* those whose document has not: so many read at once, so many of one host,
|
|
80
|
+
* and so many waiting their turn, one more being refused.
|
|
81
|
+
*/
|
|
82
|
+
export declare const HOLDER_READ_SLOTS: {
|
|
83
|
+
readonly total: 16;
|
|
84
|
+
readonly perHost: 4;
|
|
85
|
+
readonly waiting: 256;
|
|
86
|
+
};
|
|
87
|
+
/** The key seams of a did:web holder, read from its DID document. */
|
|
88
|
+
export declare function didWebHolderKeys(options?: DidWebHolderOptions): HolderKeySeams;
|
|
89
|
+
/**
|
|
90
|
+
* Seams that read a did:web holder's keys from its DID document, and hand
|
|
91
|
+
* every other holder to `otherHolders`.
|
|
92
|
+
*/
|
|
93
|
+
export declare function withDidWebHolders(otherHolders: HolderKeySeams, options?: DidWebHolderOptions): HolderKeySeams;
|
|
94
|
+
export {};
|
|
95
|
+
//# sourceMappingURL=holder-keys.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-keys.d.ts","sourceRoot":"","sources":["../../src/services/holder-keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAEH,OAAO,KAAK,EACV,WAAW,EACX,2BAA2B,EAC5B,MAAM,aAAa,CAAC,OAAO,iBAAiB,EAAE,QAAQ,EAAE,CAAC;AAE1D,OAAO,EAA4B,KAAK,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAG7E,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9D,qDAAqD;AACrD,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,UAAU,CAAC,eAAe,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;AAEhF,sDAAsD;AACtD,MAAM,WAAW,cAAc;IAC7B,sEAAsE;IACtE,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;IACjD,qEAAqE;IACrE,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,EAAE,CAAC,CAAC;CACxD;AAED,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,cAAc,CAAC,EAAE,WAAW,CAAC;IAC7B;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,2BAA2B,CAAC;IAC1C;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACjC;AAUD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB;;;;CAIpB,CAAC;AA4KX,qEAAqE;AACrE,wBAAgB,gBAAgB,CAC9B,OAAO,GAAE,mBAAwB,GAChC,cAAc,CAkFhB;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,YAAY,EAAE,cAAc,EAC5B,OAAO,GAAE,mBAAwB,GAChC,cAAc,CAQhB"}
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Holder keys: the keys a request proof's holder DID publishes, as the
|
|
4
|
+
* `resolveKey` and `resolveDidKeys` seams of `@kya-os/mcp/card`'s
|
|
5
|
+
* `requireProof` take them.
|
|
6
|
+
*
|
|
7
|
+
* A did:web holder's keys are the Ed25519 methods its DID document lists
|
|
8
|
+
* under `authentication`, the relationship by which a DID's subject proves it
|
|
9
|
+
* is the party calling. `@kya-os/mcp/card`'s `didKeyedJwks` projects them, as
|
|
10
|
+
* it does for a CIMD client's `private_key_jwt`. The document is read through
|
|
11
|
+
* `@kya-os/mcp`'s did:web resolver over its `RuntimeFetchProvider`: a bounded
|
|
12
|
+
* read under the private-network rule, with the connection pinned to the
|
|
13
|
+
* checked address where the runtime resolves host names, within a deadline,
|
|
14
|
+
* and cached per resolver.
|
|
15
|
+
*
|
|
16
|
+
* A holder whose document does not resolve has no keys, so its proof is
|
|
17
|
+
* refused. Why it did not resolve goes to the log, never to the caller, and
|
|
18
|
+
* the failure is remembered for a short while, so calls that prove nothing
|
|
19
|
+
* cannot each drive a read. A proof naming a key the cached document lacks,
|
|
20
|
+
* as after a key rotation, reads the document again, at most once a while
|
|
21
|
+
* per holder, when the resolver can drop what it cached (the library's
|
|
22
|
+
* did:web resolver can).
|
|
23
|
+
*
|
|
24
|
+
* Nothing has verified a proof when its holder's document is read, so a
|
|
25
|
+
* call can name any host. What that can cost is bounded three ways. A
|
|
26
|
+
* server that names `didWebHosts` reads no other host. Documents are read
|
|
27
|
+
* in slots (./holder-read-slots), a few at once in all and for one host.
|
|
28
|
+
* And the holders whose document has resolved before have slots of their
|
|
29
|
+
* own, so a flood of holders that never resolve cannot hold up the agents a
|
|
30
|
+
* server already serves. A failed read is remembered for the holder, not
|
|
31
|
+
* its host: one caller naming holders a host does not have must not refuse
|
|
32
|
+
* every agent that host does have. With no `didWebHosts`, a caller that
|
|
33
|
+
* hosts documents of its own can still fill the slots, which is why a
|
|
34
|
+
* server that knows its agents' hosts should name them.
|
|
35
|
+
*
|
|
36
|
+
* Every other holder goes to the seams the caller gives: each runtime keeps
|
|
37
|
+
* its own did:key resolution.
|
|
38
|
+
*
|
|
39
|
+
* @module services/holder-keys
|
|
40
|
+
*/
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.HOLDER_READ_SLOTS = void 0;
|
|
43
|
+
exports.didWebHolderKeys = didWebHolderKeys;
|
|
44
|
+
exports.withDidWebHolders = withDidWebHolders;
|
|
45
|
+
const logging_1 = require("../logging");
|
|
46
|
+
const dif_core_1 = require("../runtime/dif-core");
|
|
47
|
+
const holder_read_slots_1 = require("./holder-read-slots");
|
|
48
|
+
/** How long a holder whose document did not resolve is refused unread. */
|
|
49
|
+
const FAILED_READ_TTL_MS = 15_000;
|
|
50
|
+
/** How often a holder's document may be read again for a key it lacks. */
|
|
51
|
+
const REREAD_INTERVAL_MS = 30_000;
|
|
52
|
+
/** How long a holder whose document resolved keeps its place among the known. */
|
|
53
|
+
const KNOWN_HOLDER_TTL_MS = 60 * 60_000;
|
|
54
|
+
/** The most holders each of those remembers, the oldest forgotten first. */
|
|
55
|
+
const MAX_REMEMBERED_HOLDERS = 1_024;
|
|
56
|
+
/**
|
|
57
|
+
* The slots of the holders whose document has resolved before, and again of
|
|
58
|
+
* those whose document has not: so many read at once, so many of one host,
|
|
59
|
+
* and so many waiting their turn, one more being refused.
|
|
60
|
+
*/
|
|
61
|
+
exports.HOLDER_READ_SLOTS = {
|
|
62
|
+
total: 16,
|
|
63
|
+
perHost: 4,
|
|
64
|
+
waiting: 256,
|
|
65
|
+
};
|
|
66
|
+
/** Holders remembered for a while, bounded in number. */
|
|
67
|
+
class RecentHolders {
|
|
68
|
+
ttlMs;
|
|
69
|
+
until = new Map();
|
|
70
|
+
constructor(ttlMs) {
|
|
71
|
+
this.ttlMs = ttlMs;
|
|
72
|
+
}
|
|
73
|
+
has(did) {
|
|
74
|
+
const until = this.until.get(did);
|
|
75
|
+
if (until === undefined)
|
|
76
|
+
return false;
|
|
77
|
+
if (Date.now() < until)
|
|
78
|
+
return true;
|
|
79
|
+
this.until.delete(did);
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
add(did) {
|
|
83
|
+
this.until.delete(did);
|
|
84
|
+
const oldest = this.until.keys().next();
|
|
85
|
+
if (!oldest.done && this.until.size >= MAX_REMEMBERED_HOLDERS) {
|
|
86
|
+
this.until.delete(oldest.value);
|
|
87
|
+
}
|
|
88
|
+
this.until.set(did, Date.now() + this.ttlMs);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
let sharedResolver;
|
|
92
|
+
/** The process's did:web resolver, built on first use. */
|
|
93
|
+
function sharedDidWebResolver() {
|
|
94
|
+
return (sharedResolver ??= (0, dif_core_1.loadDifCore)().then(({ createDidWebResolver, RuntimeFetchProvider }) => createDidWebResolver(new RuntimeFetchProvider())));
|
|
95
|
+
}
|
|
96
|
+
/** The DID a DID or DID URL names. */
|
|
97
|
+
const didOf = (didOrKid) => didOrKid.split("#")[0] ?? "";
|
|
98
|
+
/** `host` as a URL writes it, lower case and with any port, or undefined. */
|
|
99
|
+
function hostOf(host) {
|
|
100
|
+
try {
|
|
101
|
+
const url = new URL(`https://${host}`);
|
|
102
|
+
return url.host !== "" && `https://${url.host}/` === url.href
|
|
103
|
+
? url.host
|
|
104
|
+
: undefined;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Whether a host is one `hosts` names; every host is when they name none.
|
|
112
|
+
*
|
|
113
|
+
* @throws TypeError for an entry that is not a host
|
|
114
|
+
*/
|
|
115
|
+
function namedHosts(hosts) {
|
|
116
|
+
if (hosts === undefined)
|
|
117
|
+
return () => true;
|
|
118
|
+
const named = new Set(hosts.map((entry) => {
|
|
119
|
+
const host = typeof entry === "string" ? hostOf(entry) : undefined;
|
|
120
|
+
if (host === undefined || host !== entry.toLowerCase()) {
|
|
121
|
+
throw new TypeError(`didWebHosts names "${String(entry)}", which is not a host such as agents.example or agents.example:8443`);
|
|
122
|
+
}
|
|
123
|
+
return host;
|
|
124
|
+
}));
|
|
125
|
+
return (host) => named.has(host);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The keys `did`'s document lists under `authentication`. Throws when the
|
|
129
|
+
* document does not resolve, after logging why.
|
|
130
|
+
*/
|
|
131
|
+
async function authenticationKeys(did, resolver) {
|
|
132
|
+
const document = await resolver.resolve(did);
|
|
133
|
+
if (!document) {
|
|
134
|
+
const failure = resolver.lastFailure?.(did);
|
|
135
|
+
logging_1.logger.warn(`[RequestHolder] No DID document for holder ${did}: ${failure ? `${failure.cause}: ${failure.detail}` : "it did not resolve"}`);
|
|
136
|
+
throw new Error("The holder's DID document did not resolve");
|
|
137
|
+
}
|
|
138
|
+
const { didKeyedJwks } = await (0, dif_core_1.loadDifCard)();
|
|
139
|
+
// didKeyedJwks names a key by its JWK's own `kid` when it has one, which
|
|
140
|
+
// hides its method's id, the name a proof's `kid` uses. Each key is listed
|
|
141
|
+
// under its method's id, and under a `kid` of its own beside it.
|
|
142
|
+
const byMethod = didKeyedJwks(namedByMethod(document)).keys;
|
|
143
|
+
const methodNames = new Set(byMethod.map(({ kid }) => kid));
|
|
144
|
+
return [
|
|
145
|
+
...byMethod,
|
|
146
|
+
...didKeyedJwks(document).keys.filter(({ kid }) => !methodNames.has(kid)),
|
|
147
|
+
];
|
|
148
|
+
}
|
|
149
|
+
/** `method` with a JWK that carries no `kid` of its own. */
|
|
150
|
+
function withoutOwnKid(method) {
|
|
151
|
+
if (typeof method !== "object" || method === null)
|
|
152
|
+
return method;
|
|
153
|
+
const jwk = method.publicKeyJwk;
|
|
154
|
+
if (typeof jwk !== "object" || jwk === null || !("kid" in jwk))
|
|
155
|
+
return method;
|
|
156
|
+
return {
|
|
157
|
+
...method,
|
|
158
|
+
publicKeyJwk: Object.fromEntries(Object.entries(jwk).filter(([name]) => name !== "kid")),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/** `document` with every key named by its verification method's id alone. */
|
|
162
|
+
function namedByMethod(document) {
|
|
163
|
+
const { verificationMethod, authentication } = document;
|
|
164
|
+
return {
|
|
165
|
+
...document,
|
|
166
|
+
...(Array.isArray(verificationMethod)
|
|
167
|
+
? { verificationMethod: verificationMethod.map(withoutOwnKid) }
|
|
168
|
+
: {}),
|
|
169
|
+
...(Array.isArray(authentication)
|
|
170
|
+
? { authentication: authentication.map(withoutOwnKid) }
|
|
171
|
+
: {}),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* The key `kid` names among `did`'s keys. A key is named by its method's
|
|
176
|
+
* id, or by its JWK's `kid`, as written: a DID URL, a relative `#fragment`,
|
|
177
|
+
* or a bare fragment such as `key-1`.
|
|
178
|
+
*/
|
|
179
|
+
function keyNamed(keys, did, kid) {
|
|
180
|
+
const fragment = kid.startsWith(`${did}#`)
|
|
181
|
+
? kid.slice(did.length + 1)
|
|
182
|
+
: undefined;
|
|
183
|
+
return keys.find(({ kid: id }) => id === kid ||
|
|
184
|
+
(fragment !== undefined && (id === `#${fragment}` || id === fragment)));
|
|
185
|
+
}
|
|
186
|
+
/** The resolver `options` name, or the one they configure, or the process's. */
|
|
187
|
+
function resolverOf(options) {
|
|
188
|
+
const { didWebResolver, didWebFetch } = options;
|
|
189
|
+
if (didWebResolver)
|
|
190
|
+
return async () => didWebResolver;
|
|
191
|
+
if (!didWebFetch)
|
|
192
|
+
return sharedDidWebResolver;
|
|
193
|
+
let own;
|
|
194
|
+
return () => (own ??= (0, dif_core_1.loadDifCore)().then(({ createDidWebResolver, RuntimeFetchProvider }) => createDidWebResolver(new RuntimeFetchProvider(didWebFetch))));
|
|
195
|
+
}
|
|
196
|
+
/** The key seams of a did:web holder, read from its DID document. */
|
|
197
|
+
function didWebHolderKeys(options = {}) {
|
|
198
|
+
const resolver = resolverOf(options);
|
|
199
|
+
const isNamedHost = namedHosts(options.didWebHosts);
|
|
200
|
+
const failed = new RecentHolders(FAILED_READ_TTL_MS);
|
|
201
|
+
const reread = new RecentHolders(REREAD_INTERVAL_MS);
|
|
202
|
+
const known = new RecentHolders(KNOWN_HOLDER_TTL_MS);
|
|
203
|
+
const knownSlots = new holder_read_slots_1.ReadSlots(exports.HOLDER_READ_SLOTS);
|
|
204
|
+
const newSlots = new holder_read_slots_1.ReadSlots(exports.HOLDER_READ_SLOTS);
|
|
205
|
+
const reading = new Map();
|
|
206
|
+
let busyLoggedAt = 0;
|
|
207
|
+
/** Read `did`'s keys in a slot: one of the known holders', or of the new. */
|
|
208
|
+
const read = async (did) => {
|
|
209
|
+
const { didWebToUrl } = await (0, dif_core_1.loadDifCore)();
|
|
210
|
+
const url = didWebToUrl(did);
|
|
211
|
+
const host = url ? new URL(url).host : "";
|
|
212
|
+
if (!isNamedHost(host)) {
|
|
213
|
+
logging_1.logger.warn(`[RequestHolder] Holder ${did} is on a host didWebHosts does not name; its document is not read`);
|
|
214
|
+
throw new Error("The holder's host is not one this server reads");
|
|
215
|
+
}
|
|
216
|
+
const slots = known.has(did) ? knownSlots : newSlots;
|
|
217
|
+
const keys = await slots.run(host, async () => authenticationKeys(did, await resolver()));
|
|
218
|
+
known.add(did);
|
|
219
|
+
return keys;
|
|
220
|
+
};
|
|
221
|
+
const keysOf = (did) => {
|
|
222
|
+
if (failed.has(did)) {
|
|
223
|
+
return Promise.reject(new Error("The holder's DID document did not resolve"));
|
|
224
|
+
}
|
|
225
|
+
// Calls that prove one holder at once share its one read.
|
|
226
|
+
const shared = reading.get(did);
|
|
227
|
+
if (shared)
|
|
228
|
+
return shared;
|
|
229
|
+
const pending = read(did)
|
|
230
|
+
.catch((error) => {
|
|
231
|
+
if (error instanceof holder_read_slots_1.HolderReadsBusyError) {
|
|
232
|
+
// No verdict on the holder: nothing was read, so nothing is remembered.
|
|
233
|
+
if (Date.now() - busyLoggedAt >= FAILED_READ_TTL_MS) {
|
|
234
|
+
busyLoggedAt = Date.now();
|
|
235
|
+
logging_1.logger.warn(`[RequestHolder] ${error.message}; new did:web holders are refused until some finish`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
failed.add(did);
|
|
240
|
+
}
|
|
241
|
+
throw error;
|
|
242
|
+
})
|
|
243
|
+
.finally(() => reading.delete(did));
|
|
244
|
+
reading.set(did, pending);
|
|
245
|
+
return pending;
|
|
246
|
+
};
|
|
247
|
+
/** `kid`'s key in a fresh read of `did`'s document, if one is due. */
|
|
248
|
+
const rereadKey = async (did, kid) => {
|
|
249
|
+
const current = await resolver();
|
|
250
|
+
if (typeof current.clearCacheEntry !== "function" || reread.has(did)) {
|
|
251
|
+
return undefined;
|
|
252
|
+
}
|
|
253
|
+
reread.add(did);
|
|
254
|
+
current.clearCacheEntry(did);
|
|
255
|
+
return keyNamed(await keysOf(did), did, kid);
|
|
256
|
+
};
|
|
257
|
+
return {
|
|
258
|
+
async resolveKey(kid) {
|
|
259
|
+
const did = didOf(kid);
|
|
260
|
+
const key = keyNamed(await keysOf(did), did, kid) ?? (await rereadKey(did, kid));
|
|
261
|
+
if (!key) {
|
|
262
|
+
throw new Error("The holder's DID document lists no such key for authentication");
|
|
263
|
+
}
|
|
264
|
+
return key;
|
|
265
|
+
},
|
|
266
|
+
resolveDidKeys: keysOf,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Seams that read a did:web holder's keys from its DID document, and hand
|
|
271
|
+
* every other holder to `otherHolders`.
|
|
272
|
+
*/
|
|
273
|
+
function withDidWebHolders(otherHolders, options = {}) {
|
|
274
|
+
const web = didWebHolderKeys(options);
|
|
275
|
+
const seamsFor = async (didOrKid) => (await (0, dif_core_1.loadDifCore)()).isDidWeb(didOf(didOrKid)) ? web : otherHolders;
|
|
276
|
+
return {
|
|
277
|
+
resolveKey: async (kid) => (await seamsFor(kid)).resolveKey(kid),
|
|
278
|
+
resolveDidKeys: async (did) => (await seamsFor(did)).resolveDidKeys(did),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
//# sourceMappingURL=holder-keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-keys.js","sourceRoot":"","sources":["../../src/services/holder-keys.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;;;AA+OH,4CAoFC;AAMD,8CAWC;AA9UD,wCAAoC;AACpC,kDAA6E;AAC7E,2DAAsE;AAyCtE,0EAA0E;AAC1E,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,0EAA0E;AAC1E,MAAM,kBAAkB,GAAG,MAAM,CAAC;AAClC,iFAAiF;AACjF,MAAM,mBAAmB,GAAG,EAAE,GAAG,MAAM,CAAC;AACxC,4EAA4E;AAC5E,MAAM,sBAAsB,GAAG,KAAK,CAAC;AACrC;;;;GA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AG,CAAC,IAAI,CAAC,MAAM,SAAS,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;YACvE,IAAI,CAAC,GAAG,EAAE,CAAC;gBACT,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;YACJ,CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC;QACD,cAAc,EAAE,MAAM;KACvB,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAgB,iBAAiB,CAC/B,YAA4B,EAC5B,UAA+B,EAAE;IAEjC,MAAM,GAAG,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACtC,MAAM,QAAQ,GAAG,KAAK,EAAE,QAAgB,EAAE,EAAE,CAC1C,CAAC,MAAM,IAAA,sBAAW,GAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,YAAY,CAAC;IACvE,OAAO;QACL,UAAU,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,GAAG,CAAC;QAChE,cAAc,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC;KACzE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The slots holder documents are read in.
|
|
3
|
+
*
|
|
4
|
+
* A request proof names its holder before anything verifies it, so a call
|
|
5
|
+
* can name any did:web holder and have its document read. The reads such
|
|
6
|
+
* calls drive are held to so many at once, in all and for one host; a read
|
|
7
|
+
* beyond that waits its turn, in the order it came, and a read beyond the
|
|
8
|
+
* line is refused. A burst of new holders is then read a few at a time, and
|
|
9
|
+
* no number of calls opens more connections than the slots.
|
|
10
|
+
*
|
|
11
|
+
* @module services/holder-read-slots
|
|
12
|
+
*/
|
|
13
|
+
/** A read refused because the slots and the line behind them are full. */
|
|
14
|
+
export declare class HolderReadsBusyError extends Error {
|
|
15
|
+
constructor();
|
|
16
|
+
}
|
|
17
|
+
export interface ReadSlotLimits {
|
|
18
|
+
/** The most reads at once. */
|
|
19
|
+
total: number;
|
|
20
|
+
/** The most reads at once of one host. */
|
|
21
|
+
perHost: number;
|
|
22
|
+
/** The most reads waiting for a slot. */
|
|
23
|
+
waiting: number;
|
|
24
|
+
}
|
|
25
|
+
export declare class ReadSlots {
|
|
26
|
+
private readonly limits;
|
|
27
|
+
private reading;
|
|
28
|
+
private readonly readingOf;
|
|
29
|
+
private readonly line;
|
|
30
|
+
constructor(limits: ReadSlotLimits);
|
|
31
|
+
/**
|
|
32
|
+
* Run `read` of a document on `host` in a slot, waiting for one in line.
|
|
33
|
+
*
|
|
34
|
+
* @throws HolderReadsBusyError when the line is full; `read` did not run
|
|
35
|
+
*/
|
|
36
|
+
run<T>(host: string, read: () => Promise<T>): Promise<T>;
|
|
37
|
+
private hasSlot;
|
|
38
|
+
private take;
|
|
39
|
+
/** Free `host`'s slot, and give one to the first read in line it suits. */
|
|
40
|
+
private release;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=holder-read-slots.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-read-slots.d.ts","sourceRoot":"","sources":["../../src/services/holder-read-slots.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,0EAA0E;AAC1E,qBAAa,oBAAqB,SAAQ,KAAK;;CAK9C;AAED,MAAM,WAAW,cAAc;IAC7B,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,OAAO,EAAE,MAAM,CAAC;IAChB,yCAAyC;IACzC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,SAAS;IAKR,OAAO,CAAC,QAAQ,CAAC,MAAM;IAJnC,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA6B;IACvD,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAkD;gBAE1C,MAAM,EAAE,cAAc;IAEnD;;;;OAIG;IACG,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAgB9D,OAAO,CAAC,OAAO;IAOf,OAAO,CAAC,IAAI;IAKZ,2EAA2E;IAC3E,OAAO,CAAC,OAAO;CAahB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The slots holder documents are read in.
|
|
4
|
+
*
|
|
5
|
+
* A request proof names its holder before anything verifies it, so a call
|
|
6
|
+
* can name any did:web holder and have its document read. The reads such
|
|
7
|
+
* calls drive are held to so many at once, in all and for one host; a read
|
|
8
|
+
* beyond that waits its turn, in the order it came, and a read beyond the
|
|
9
|
+
* line is refused. A burst of new holders is then read a few at a time, and
|
|
10
|
+
* no number of calls opens more connections than the slots.
|
|
11
|
+
*
|
|
12
|
+
* @module services/holder-read-slots
|
|
13
|
+
*/
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.ReadSlots = exports.HolderReadsBusyError = void 0;
|
|
16
|
+
/** A read refused because the slots and the line behind them are full. */
|
|
17
|
+
class HolderReadsBusyError extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Too many holder documents are being read");
|
|
20
|
+
this.name = "HolderReadsBusyError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.HolderReadsBusyError = HolderReadsBusyError;
|
|
24
|
+
class ReadSlots {
|
|
25
|
+
limits;
|
|
26
|
+
reading = 0;
|
|
27
|
+
readingOf = new Map();
|
|
28
|
+
line = [];
|
|
29
|
+
constructor(limits) {
|
|
30
|
+
this.limits = limits;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Run `read` of a document on `host` in a slot, waiting for one in line.
|
|
34
|
+
*
|
|
35
|
+
* @throws HolderReadsBusyError when the line is full; `read` did not run
|
|
36
|
+
*/
|
|
37
|
+
async run(host, read) {
|
|
38
|
+
if (this.hasSlot(host)) {
|
|
39
|
+
this.take(host);
|
|
40
|
+
}
|
|
41
|
+
else if (this.line.length >= this.limits.waiting) {
|
|
42
|
+
throw new HolderReadsBusyError();
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// Its slot is taken for it when its turn comes (./release).
|
|
46
|
+
await new Promise((start) => this.line.push({ host, start }));
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return await read();
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
this.release(host);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
hasSlot(host) {
|
|
56
|
+
return (this.reading < this.limits.total &&
|
|
57
|
+
(this.readingOf.get(host) ?? 0) < this.limits.perHost);
|
|
58
|
+
}
|
|
59
|
+
take(host) {
|
|
60
|
+
this.reading += 1;
|
|
61
|
+
this.readingOf.set(host, (this.readingOf.get(host) ?? 0) + 1);
|
|
62
|
+
}
|
|
63
|
+
/** Free `host`'s slot, and give one to the first read in line it suits. */
|
|
64
|
+
release(host) {
|
|
65
|
+
this.reading -= 1;
|
|
66
|
+
const left = (this.readingOf.get(host) ?? 1) - 1;
|
|
67
|
+
if (left > 0)
|
|
68
|
+
this.readingOf.set(host, left);
|
|
69
|
+
else
|
|
70
|
+
this.readingOf.delete(host);
|
|
71
|
+
const next = this.line.findIndex((waiting) => this.hasSlot(waiting.host));
|
|
72
|
+
if (next < 0)
|
|
73
|
+
return;
|
|
74
|
+
const [waiting] = this.line.splice(next, 1);
|
|
75
|
+
if (!waiting)
|
|
76
|
+
return;
|
|
77
|
+
this.take(waiting.host);
|
|
78
|
+
waiting.start();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.ReadSlots = ReadSlots;
|
|
82
|
+
//# sourceMappingURL=holder-read-slots.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"holder-read-slots.js","sourceRoot":"","sources":["../../src/services/holder-read-slots.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;GAWG;;;AAEH,0EAA0E;AAC1E,MAAa,oBAAqB,SAAQ,KAAK;IAC7C;QACE,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AALD,oDAKC;AAWD,MAAa,SAAS;IAKS;IAJrB,OAAO,GAAG,CAAC,CAAC;IACH,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IACtC,IAAI,GAA+C,EAAE,CAAC;IAEvE,YAA6B,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IAEvD;;;;OAIG;IACH,KAAK,CAAC,GAAG,CAAI,IAAY,EAAE,IAAsB;QAC/C,IAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACvB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,IAAI,CAAC,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACnD,MAAM,IAAI,oBAAoB,EAAE,CAAC;QACnC,CAAC;aAAM,CAAC;YACN,4DAA4D;YAC5D,MAAM,IAAI,OAAO,CAAO,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;QACtE,CAAC;QACD,IAAI,CAAC;YACH,OAAO,MAAM,IAAI,EAAE,CAAC;QACtB,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;IAEO,OAAO,CAAC,IAAY;QA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CAAC;CACF;AAtDD,8BAsDC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request holder: the DID whose key a tools/call proves its caller holds.
|
|
3
|
+
*
|
|
4
|
+
* The proof is the call's request proof, `_meta["org.kya-os/request-proof"]`
|
|
5
|
+
* (SPEC-ENTITY-CARD §8, SPEC-MCP-EXTENSION §6), verified fail closed by
|
|
6
|
+
* `@kya-os/mcp/card`'s `requireProof`: its request hash covers the call's
|
|
7
|
+
* method and params without `_meta`, its audience is this server's DID, its
|
|
8
|
+
* nonce is spent once, and it is signed by the key the holder DID names. This
|
|
9
|
+
* is the check the Cloudflare adapter's request admission makes
|
|
10
|
+
* (mcp-i-cloudflare `security/mcp-holder-admission`). A did:key holder's key
|
|
11
|
+
* is resolved locally with no network; a did:web holder's keys are the ones
|
|
12
|
+
* its DID document lists for authentication (./holder-keys).
|
|
13
|
+
*
|
|
14
|
+
* A proof names its holder before anything verifies it, so the holder's
|
|
15
|
+
* document is read only for a proof its own claims do not already refuse
|
|
16
|
+
* (./claims-first-guard).
|
|
17
|
+
*
|
|
18
|
+
* A verified holder is identity only: it grants nothing until a delegation
|
|
19
|
+
* the call presents is verified for it.
|
|
20
|
+
*
|
|
21
|
+
* @module services/request-holder
|
|
22
|
+
*/
|
|
23
|
+
import { type DifCard } from "../runtime/dif-core";
|
|
24
|
+
import { type DidWebHolderOptions, type ProofPublicJwk } from "./holder-keys";
|
|
25
|
+
type VerifyProofDeps = Parameters<DifCard["requireProof"]>[0];
|
|
26
|
+
type ProofGateCode = Extract<Awaited<ReturnType<ReturnType<DifCard["requireProof"]>>>, {
|
|
27
|
+
ok: false;
|
|
28
|
+
}>["error"]["code"];
|
|
29
|
+
/** Seconds of clock skew a request proof's validity window is widened by. */
|
|
30
|
+
export declare const REQUEST_PROOF_SKEW_SEC = 30;
|
|
31
|
+
/** Why a call proved no holder: it carried no request proof, or a bad one. */
|
|
32
|
+
export type RequestHolderRefusal = "holder_proof_required" | ProofGateCode;
|
|
33
|
+
/** The holder a call proved, or why it proved none. */
|
|
34
|
+
export type RequestHolderResult = {
|
|
35
|
+
ok: true;
|
|
36
|
+
did: string;
|
|
37
|
+
} | {
|
|
38
|
+
ok: false;
|
|
39
|
+
code: RequestHolderRefusal;
|
|
40
|
+
};
|
|
41
|
+
/** How a server checks its calls' request proofs, beside their audience. */
|
|
42
|
+
export interface RequestProofOptions extends DidWebHolderOptions {
|
|
43
|
+
/**
|
|
44
|
+
* The atomic test-and-set that spends a proof's nonce:
|
|
45
|
+
* `(nonce, did, minTtlSec) => Promise<boolean>`, true when the nonce was
|
|
46
|
+
* fresh. Default: one in-memory cache per verifier, so one per process. A
|
|
47
|
+
* proof lives at most 60 seconds and is accepted within 30 seconds of
|
|
48
|
+
* clock skew, so behind several instances that each keep their own cache,
|
|
49
|
+
* a captured proof is accepted once by each, for up to 90 seconds after
|
|
50
|
+
* it was signed. Share one store across the instances that serve one
|
|
51
|
+
* server DID, as the Cloudflare adapter shares a Durable Object.
|
|
52
|
+
*/
|
|
53
|
+
consumeNonceIfFresh?: VerifyProofDeps["consumeNonceIfFresh"];
|
|
54
|
+
}
|
|
55
|
+
export interface RequestHolderOptions extends RequestProofOptions {
|
|
56
|
+
/** This server's DID: a proof addressed to any other is refused. */
|
|
57
|
+
audience: string;
|
|
58
|
+
/** Clock in epoch milliseconds, for tests. */
|
|
59
|
+
now?: () => number;
|
|
60
|
+
}
|
|
61
|
+
/** A tools/call as received: its method and params, `_meta` included. */
|
|
62
|
+
export interface HolderRequest {
|
|
63
|
+
method: string;
|
|
64
|
+
params?: Record<string, unknown>;
|
|
65
|
+
}
|
|
66
|
+
export type RequestHolderVerifier = (request: HolderRequest) => Promise<RequestHolderResult>;
|
|
67
|
+
/**
|
|
68
|
+
* The Ed25519 key a did:key holder names by its own key id, resolved with no
|
|
69
|
+
* network. Any other kid, or any other DID method, has no key here.
|
|
70
|
+
*/
|
|
71
|
+
export declare function resolveDidKeyHolder(kid: string): Promise<ProofPublicJwk>;
|
|
72
|
+
/**
|
|
73
|
+
* Build the verifier of the holder a tools/call proves. One verifier keeps
|
|
74
|
+
* one nonce cache, so build it once per server.
|
|
75
|
+
*/
|
|
76
|
+
export declare function createRequestHolderVerifier(options: RequestHolderOptions): RequestHolderVerifier;
|
|
77
|
+
export {};
|
|
78
|
+
//# sourceMappingURL=request-holder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"request-holder.d.ts","sourceRoot":"","sources":["../../src/services/request-holder.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAA4B,KAAK,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE7E,OAAO,EAEL,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACpB,MAAM,eAAe,CAAC;AAEvB,KAAK,eAAe,GAAG,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC9D,KAAK,aAAa,GAAG,OAAO,CAC1B,OAAO,CAAC,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,EACxD;IAAE,EAAE,EAAE,KAAK,CAAA;CAAE,CACd,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnB,6EAA6E;AAC7E,eAAO,MAAM,sBAAsB,KAAK,CAAC;AAEzC,8EAA8E;AAC9E,MAAM,MAAM,oBAAoB,GAAG,uBAAuB,GAAG,aAAa,CAAC;AAE3E,uDAAuD;AACvD,MAAM,MAAM,mBAAmB,GAC3B;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,IAAI,EAAE,oBAAoB,CAAA;CAAE,CAAC;AAE9C,4EAA4E;AAC5E,MAAM,WAAW,mBAAoB,SAAQ,mBAAmB;IAC9D;;;;;;;;;OASG;IACH,mBAAm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