@kokuin/vault 0.1.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +92 -1
- package/lib/branch.js +2 -3
- package/lib/digest.d.ts +27 -8
- package/lib/digest.js +63 -41
- package/lib/errors.d.ts +38 -13
- package/lib/errors.js +43 -25
- package/lib/factors.d.ts +18 -6
- package/lib/factors.js +12 -6
- package/lib/id.d.ts +12 -3
- package/lib/id.js +14 -6
- package/lib/index.d.ts +7 -1
- package/lib/index.js +1 -1
- package/lib/key-cache.d.ts +22 -4
- package/lib/key-cache.js +4 -4
- package/lib/record-types.d.ts +17 -6
- package/lib/record-types.js +247 -79
- package/lib/records.d.ts +29 -10
- package/lib/records.js +57 -14
- package/lib/suite.d.ts +3 -3
- package/lib/suite.js +127 -31
- package/package.json +7 -6
package/lib/key-cache.d.ts
CHANGED
|
@@ -1,17 +1,35 @@
|
|
|
1
|
+
export type KeyCacheSetParams = {
|
|
2
|
+
keyID: string;
|
|
3
|
+
contentKey: Uint8Array;
|
|
4
|
+
keyVersion: number;
|
|
5
|
+
branchID: string;
|
|
6
|
+
};
|
|
1
7
|
export declare const DEFAULT_UNLOCK_TTL: number;
|
|
2
8
|
export type KeyCacheParams = {
|
|
3
9
|
ttl?: number;
|
|
4
10
|
now?: () => number;
|
|
5
11
|
};
|
|
6
12
|
export type KeyCache = {
|
|
7
|
-
get(
|
|
13
|
+
get(params: KeyCacheGetParams): {
|
|
8
14
|
contentKey: Uint8Array;
|
|
9
15
|
branchID: string;
|
|
10
16
|
} | null;
|
|
11
|
-
has(
|
|
12
|
-
set(
|
|
13
|
-
evictVersion(
|
|
17
|
+
has(params: KeyCacheHasParams): boolean;
|
|
18
|
+
set(params: KeyCacheSetParams): void;
|
|
19
|
+
evictVersion(params: KeyCacheEvictVersionParams): void;
|
|
14
20
|
evict(keyID: string): void;
|
|
15
21
|
evictAll(): void;
|
|
16
22
|
};
|
|
17
23
|
export declare function createKeyCache(params?: KeyCacheParams): KeyCache;
|
|
24
|
+
export type KeyCacheGetParams = {
|
|
25
|
+
keyID: string;
|
|
26
|
+
keyVersion: number;
|
|
27
|
+
};
|
|
28
|
+
export type KeyCacheHasParams = {
|
|
29
|
+
keyID: string;
|
|
30
|
+
keyVersion: number;
|
|
31
|
+
};
|
|
32
|
+
export type KeyCacheEvictVersionParams = {
|
|
33
|
+
keyID: string;
|
|
34
|
+
keyVersion: number;
|
|
35
|
+
};
|
package/lib/key-cache.js
CHANGED
|
@@ -29,7 +29,7 @@ export function createKeyCache(params = {}) {
|
|
|
29
29
|
return cached;
|
|
30
30
|
}
|
|
31
31
|
return {
|
|
32
|
-
get (keyID, keyVersion) {
|
|
32
|
+
get ({ keyID, keyVersion }) {
|
|
33
33
|
const cached = live(keyID, keyVersion);
|
|
34
34
|
if (cached == null) {
|
|
35
35
|
return null;
|
|
@@ -39,10 +39,10 @@ export function createKeyCache(params = {}) {
|
|
|
39
39
|
branchID: cached.branchID
|
|
40
40
|
};
|
|
41
41
|
},
|
|
42
|
-
has (keyID, keyVersion) {
|
|
42
|
+
has ({ keyID, keyVersion }) {
|
|
43
43
|
return live(keyID, keyVersion) != null;
|
|
44
44
|
},
|
|
45
|
-
set (keyID, contentKey, keyVersion, branchID) {
|
|
45
|
+
set ({ keyID, contentKey, keyVersion, branchID }) {
|
|
46
46
|
evictVersion(keyID, keyVersion);
|
|
47
47
|
const versions = entries.get(keyID) ?? new Map();
|
|
48
48
|
versions.set(keyVersion, {
|
|
@@ -53,7 +53,7 @@ export function createKeyCache(params = {}) {
|
|
|
53
53
|
entries.set(keyID, versions);
|
|
54
54
|
},
|
|
55
55
|
evict,
|
|
56
|
-
evictVersion,
|
|
56
|
+
evictVersion: ({ keyID, keyVersion })=>evictVersion(keyID, keyVersion),
|
|
57
57
|
evictAll () {
|
|
58
58
|
for (const keyID of [
|
|
59
59
|
...entries.keys()
|
package/lib/record-types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RecordKey, RecordStoreAdapter, RecordTypeDefinition, SignedRecord, VerifiedRecordOp, VisibilityContext } from '@kokuin/records';
|
|
2
|
-
import type { MethodRegistry } from '@kokuin/token';
|
|
2
|
+
import type { CryptoOptions, MethodRegistry } from '@kokuin/token';
|
|
3
3
|
export type CredentialRecordReadPort = Pick<RecordStoreAdapter, 'get' | 'getMany' | 'list'>;
|
|
4
4
|
export type CredentialAuthorityRequest = {
|
|
5
5
|
source: 'record';
|
|
@@ -26,19 +26,30 @@ export type VaultRecordPorts = {
|
|
|
26
26
|
};
|
|
27
27
|
export type VaultRecordHooks = {
|
|
28
28
|
authority: CredentialAuthority;
|
|
29
|
-
validateConsumerMetadata(
|
|
29
|
+
validateConsumerMetadata(params: VaultRecordHooksValidateConsumerMetadataParams): Promise<boolean>;
|
|
30
30
|
/** Must resolve ports bound to this adapter's transaction, including routed adapters. */
|
|
31
31
|
portsFor(store: RecordStoreAdapter): VaultRecordPorts;
|
|
32
32
|
};
|
|
33
|
-
export type VaultRecordTypeParams = VaultRecordHooks & {
|
|
33
|
+
export type VaultRecordTypeParams = VaultRecordHooks & CryptoOptions & {
|
|
34
34
|
opKind: string;
|
|
35
35
|
methods?: MethodRegistry;
|
|
36
36
|
};
|
|
37
37
|
/** One explicit replay domain and consumer policy for the five generic record types. */
|
|
38
|
-
export declare function vaultRecordTypes(
|
|
38
|
+
export declare function vaultRecordTypes(input: VaultRecordTypeParams): Map<string, RecordTypeDefinition>;
|
|
39
39
|
/** Only wrappings can prove key holding, regardless of other records' metadata. */
|
|
40
40
|
export declare function vaultHolderTypes(types: ReadonlyMap<string, RecordTypeDefinition>): ReadonlyMap<string, RecordTypeDefinition>;
|
|
41
41
|
/** Read a live, owner-qualified key version in the explicit replay domain. */
|
|
42
|
-
export
|
|
42
|
+
export type VaultKeyVersionReaderParams = {
|
|
43
|
+
store: RecordStoreAdapter;
|
|
44
|
+
} & Pick<VaultRecordTypeParams, 'opKind' | 'methods' | 'crypto'>;
|
|
45
|
+
export declare function vaultKeyVersionReader(input: VaultKeyVersionReaderParams): VisibilityContext['currentKeyVersion'];
|
|
43
46
|
/** Proofs retain consumer authority checks but are never addressed to the local receiver. */
|
|
44
|
-
export
|
|
47
|
+
export type VaultProofAuthorizerParams = {
|
|
48
|
+
store: RecordStoreAdapter;
|
|
49
|
+
transportContext?: unknown;
|
|
50
|
+
} & VaultRecordTypeParams;
|
|
51
|
+
export declare function vaultProofAuthorizer(input: VaultProofAuthorizerParams): VisibilityContext['authorizeProof'];
|
|
52
|
+
export type VaultRecordHooksValidateConsumerMetadataParams = {
|
|
53
|
+
value: unknown;
|
|
54
|
+
request: CredentialAuthorityRequest;
|
|
55
|
+
};
|
package/lib/record-types.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
+
import { nobleCrypto } from '@kokuin/crypto';
|
|
1
2
|
import { compareRecordDecisionHLC, parseCanonicalHLC, RecordContentRefusalError, recordPayloadDigest, recordPayloadMatches, verifyRecordOp, verifySignedRecord } from '@kokuin/records';
|
|
2
3
|
import { normalizeDID } from '@kokuin/token';
|
|
3
|
-
import { decodeVaultRecord, encodeVaultRecordPayload, VAULT_RECORD_TYPES, vaultRecordMeta } from './records.js';
|
|
4
|
+
import { decodeVaultRecord, encodeVaultRecordPayload, parseVaultRecordMeta, VAULT_RECORD_TYPES, VaultRecordPayloadError, vaultRecordMeta } from './records.js';
|
|
4
5
|
const KEY = 'vault.key';
|
|
5
6
|
const BRANCH = 'vault.key-branch';
|
|
6
7
|
const WRAPPING = 'vault.wrapping';
|
|
7
8
|
const ENTRY = 'vault.entry';
|
|
8
9
|
const REVOCATION = 'vault.revocation';
|
|
9
|
-
function requestFor(op, record, store, transportContext, verb) {
|
|
10
|
+
function requestFor({ op, record, store, transportContext, verb }) {
|
|
10
11
|
const getMany = store.getMany;
|
|
11
12
|
const read = {
|
|
12
13
|
get: (key)=>store.get(key),
|
|
@@ -32,12 +33,17 @@ function requestFor(op, record, store, transportContext, verb) {
|
|
|
32
33
|
};
|
|
33
34
|
}
|
|
34
35
|
async function authorized(params, request) {
|
|
35
|
-
return await params.validateConsumerMetadata(
|
|
36
|
+
return await params.validateConsumerMetadata({
|
|
37
|
+
value: request.op.claim.meta.consumer,
|
|
38
|
+
request
|
|
39
|
+
}) && await params.authority.authorize(request);
|
|
36
40
|
}
|
|
37
41
|
function entryOrder(claim) {
|
|
38
|
-
const meta =
|
|
42
|
+
const meta = parseVaultRecordMeta(ENTRY, claim);
|
|
39
43
|
const stamp = meta === null ? null : parseCanonicalHLC(meta.contentHLC);
|
|
40
|
-
if (meta === null || stamp === null) throw new RecordContentRefusalError(
|
|
44
|
+
if (meta === null || stamp === null) throw new RecordContentRefusalError({
|
|
45
|
+
message: 'Entry content stamp is malformed'
|
|
46
|
+
});
|
|
41
47
|
const node = Array.from({
|
|
42
48
|
length: stamp.nodeID.length
|
|
43
49
|
}, (_, i)=>stamp.nodeID.charCodeAt(i));
|
|
@@ -50,28 +56,43 @@ function entryOrder(claim) {
|
|
|
50
56
|
];
|
|
51
57
|
}
|
|
52
58
|
function keyOrder(claim) {
|
|
53
|
-
const meta =
|
|
54
|
-
if (meta === null) throw new RecordContentRefusalError(
|
|
59
|
+
const meta = parseVaultRecordMeta(KEY, claim);
|
|
60
|
+
if (meta === null) throw new RecordContentRefusalError({
|
|
61
|
+
message: 'Key metadata is malformed'
|
|
62
|
+
});
|
|
55
63
|
return [
|
|
56
64
|
meta.version,
|
|
57
65
|
claim.deleted ? 2 : meta.state === 'retired' ? 1 : 0
|
|
58
66
|
];
|
|
59
67
|
}
|
|
60
|
-
function validStructure(type, op) {
|
|
61
|
-
const meta = vaultRecordMeta(
|
|
68
|
+
async function validStructure(type, op, options) {
|
|
69
|
+
const meta = await vaultRecordMeta({
|
|
70
|
+
...options,
|
|
71
|
+
type,
|
|
72
|
+
claim: op.claim
|
|
73
|
+
});
|
|
62
74
|
if (meta === null) return false;
|
|
63
75
|
if (type === ENTRY) {
|
|
64
|
-
const entry =
|
|
76
|
+
const entry = meta;
|
|
65
77
|
return entry !== null && parseCanonicalHLC(op.claim.hlc) !== null && compareRecordDecisionHLC(entry.contentHLC, op.claim.hlc) <= 0;
|
|
66
78
|
}
|
|
67
79
|
return true;
|
|
68
80
|
}
|
|
69
|
-
function decodedAs(record, op, type) {
|
|
70
|
-
if (op.claim.deleted || !recordPayloadMatches(
|
|
81
|
+
async function decodedAs({ record, op, type, ...options }) {
|
|
82
|
+
if (op.claim.deleted || !await recordPayloadMatches({
|
|
83
|
+
...options,
|
|
84
|
+
record,
|
|
85
|
+
claim: op.claim
|
|
86
|
+
})) return null;
|
|
71
87
|
try {
|
|
72
|
-
const decoded = decodeVaultRecord(
|
|
88
|
+
const decoded = await decodeVaultRecord({
|
|
89
|
+
...options,
|
|
90
|
+
record,
|
|
91
|
+
recordMeta: op.claim.meta
|
|
92
|
+
});
|
|
73
93
|
return decoded.type === type ? decoded : null;
|
|
74
|
-
} catch
|
|
94
|
+
} catch (error) {
|
|
95
|
+
if (!(error instanceof VaultRecordPayloadError)) throw error;
|
|
75
96
|
return null;
|
|
76
97
|
}
|
|
77
98
|
}
|
|
@@ -81,7 +102,7 @@ function keyAtLeast(owner, keyID, version) {
|
|
|
81
102
|
owner,
|
|
82
103
|
id: keyID,
|
|
83
104
|
satisfiedBy: (op)=>{
|
|
84
|
-
const meta =
|
|
105
|
+
const meta = parseVaultRecordMeta(KEY, op.claim);
|
|
85
106
|
return op.claim.owner === owner && meta !== null && meta.version >= version;
|
|
86
107
|
}
|
|
87
108
|
};
|
|
@@ -92,14 +113,14 @@ function exactBranch(op, meta) {
|
|
|
92
113
|
owner: op.claim.owner,
|
|
93
114
|
id: meta.branchRecordID,
|
|
94
115
|
satisfiedBy: (branch)=>{
|
|
95
|
-
const info =
|
|
116
|
+
const info = parseVaultRecordMeta(BRANCH, branch.claim);
|
|
96
117
|
return info !== null && branch.claim.owner === op.claim.owner && !branch.claim.deleted && info.keyID === meta.keyID && info.keyVersion === meta.keyVersion && info.branchID === meta.branchID;
|
|
97
118
|
}
|
|
98
119
|
};
|
|
99
120
|
}
|
|
100
121
|
function dependencies(type, op) {
|
|
101
122
|
if (type === KEY) return [];
|
|
102
|
-
const meta =
|
|
123
|
+
const meta = parseVaultRecordMeta(type, op.claim);
|
|
103
124
|
if (meta === null) return [];
|
|
104
125
|
const key = {
|
|
105
126
|
type: KEY,
|
|
@@ -135,13 +156,24 @@ async function keyOwnerMatches(params, op, ctx) {
|
|
|
135
156
|
id: op.claim.meta.keyID
|
|
136
157
|
});
|
|
137
158
|
if (decision === null) return false;
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
159
|
+
const keys = await Promise.all(decision.copies.map((record)=>{
|
|
160
|
+
return verifySignedRecord({
|
|
161
|
+
record,
|
|
162
|
+
opKind: params.opKind,
|
|
163
|
+
methods: params.methods,
|
|
164
|
+
crypto: params?.crypto
|
|
165
|
+
});
|
|
166
|
+
}));
|
|
167
|
+
for (const key of keys){
|
|
168
|
+
if (key !== null && key.claim.owner === op.claim.owner && await vaultRecordMeta({
|
|
169
|
+
...params,
|
|
170
|
+
type: KEY,
|
|
171
|
+
claim: key.claim
|
|
172
|
+
}) !== null) return true;
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
143
175
|
}
|
|
144
|
-
async function revoked(params, op, store, descriptors) {
|
|
176
|
+
async function revoked({ params, op, store, descriptors }) {
|
|
145
177
|
const denied = new Set((await params.portsFor(store).listRevocations({
|
|
146
178
|
ownerDID: op.claim.owner,
|
|
147
179
|
keyID: op.claim.meta.keyID
|
|
@@ -162,18 +194,42 @@ async function revoked(params, op, store, descriptors) {
|
|
|
162
194
|
return false;
|
|
163
195
|
}
|
|
164
196
|
async function authorizeBranch(params, op, ctx) {
|
|
165
|
-
const meta = vaultRecordMeta(
|
|
166
|
-
|
|
197
|
+
const meta = await vaultRecordMeta({
|
|
198
|
+
...params,
|
|
199
|
+
type: BRANCH,
|
|
200
|
+
claim: op.claim
|
|
201
|
+
});
|
|
202
|
+
const decoded = await decodedAs({
|
|
203
|
+
...params,
|
|
204
|
+
record: ctx.record,
|
|
205
|
+
op,
|
|
206
|
+
type: BRANCH
|
|
207
|
+
});
|
|
167
208
|
if (meta === null || decoded === null) return false;
|
|
168
209
|
const payload = decoded.payload;
|
|
169
|
-
const introducing = await verifyRecordOp(
|
|
170
|
-
|
|
210
|
+
const introducing = await verifyRecordOp({
|
|
211
|
+
opJWT: payload.opJWT,
|
|
212
|
+
opKind: params.opKind,
|
|
213
|
+
methods: params.methods,
|
|
214
|
+
crypto: params?.crypto
|
|
215
|
+
});
|
|
216
|
+
const key = introducing === null ? null : await vaultRecordMeta({
|
|
217
|
+
...params,
|
|
218
|
+
type: KEY,
|
|
219
|
+
claim: introducing.claim
|
|
220
|
+
});
|
|
171
221
|
if (introducing === null || key === null || introducing.claim.deleted || introducing.claim.id !== meta.keyID || introducing.claim.owner !== op.claim.owner || key.version !== meta.keyVersion || introducing.opHash !== payload.opHash || introducing.claim.hlc !== payload.hlc || introducing.issuer !== normalizeDID(payload.authorDID) || meta.branchID !== `${payload.hlc}:${payload.opHash}`) return false;
|
|
172
|
-
const keyPayload = encodeVaultRecordPayload(
|
|
173
|
-
|
|
174
|
-
|
|
222
|
+
const keyPayload = encodeVaultRecordPayload({
|
|
223
|
+
type: KEY,
|
|
224
|
+
payload: {
|
|
225
|
+
suite: payload.suite,
|
|
226
|
+
state: key.state
|
|
227
|
+
}
|
|
175
228
|
});
|
|
176
|
-
if (introducing.claim.payloadDigest !== recordPayloadDigest(
|
|
229
|
+
if (introducing.claim.payloadDigest !== await recordPayloadDigest({
|
|
230
|
+
...params,
|
|
231
|
+
payload: keyPayload
|
|
232
|
+
})) return false;
|
|
177
233
|
// The embedded op is independent authenticated evidence and gets its own consumer decision.
|
|
178
234
|
const keyRecord = {
|
|
179
235
|
type: KEY,
|
|
@@ -184,7 +240,19 @@ async function authorizeBranch(params, op, ctx) {
|
|
|
184
240
|
opJWT: payload.opJWT,
|
|
185
241
|
opHash: introducing.opHash
|
|
186
242
|
};
|
|
187
|
-
return await authorized(params, requestFor(
|
|
243
|
+
return await authorized(params, requestFor({
|
|
244
|
+
op: introducing,
|
|
245
|
+
record: keyRecord,
|
|
246
|
+
store: ctx.store,
|
|
247
|
+
transportContext: ctx.transportContext,
|
|
248
|
+
verb: 'administer'
|
|
249
|
+
})) && await authorized(params, requestFor({
|
|
250
|
+
op,
|
|
251
|
+
record: ctx.record,
|
|
252
|
+
store: ctx.store,
|
|
253
|
+
transportContext: ctx.transportContext,
|
|
254
|
+
verb: 'administer'
|
|
255
|
+
}));
|
|
188
256
|
}
|
|
189
257
|
function createType(type, params, bindReceiver = true) {
|
|
190
258
|
const definition = {
|
|
@@ -206,42 +274,105 @@ function createType(type, params, bindReceiver = true) {
|
|
|
206
274
|
] : [
|
|
207
275
|
KEY
|
|
208
276
|
],
|
|
209
|
-
precheck: async (op)=>validStructure(type, op),
|
|
210
|
-
dependencies: (_record, op)=>dependencies(type, op),
|
|
211
|
-
async authorize (op, ctx) {
|
|
277
|
+
precheck: async ({ op })=>await validStructure(type, op, params),
|
|
278
|
+
dependencies: ({ record: _record, op })=>dependencies(type, op),
|
|
279
|
+
async authorize ({ op, ctx }) {
|
|
212
280
|
// Enforce the factory's replay domain even when the engine is misconfigured.
|
|
213
|
-
const checked = await verifySignedRecord(
|
|
281
|
+
const checked = await verifySignedRecord({
|
|
282
|
+
record: ctx.record,
|
|
283
|
+
opKind: params.opKind,
|
|
284
|
+
methods: params.methods,
|
|
285
|
+
crypto: params?.crypto
|
|
286
|
+
});
|
|
214
287
|
if (checked === null || checked.opHash !== op.opHash) return false;
|
|
215
|
-
if (!validStructure(type, op) || !recordPayloadMatches(
|
|
288
|
+
if (!await validStructure(type, op, params) || !await recordPayloadMatches({
|
|
289
|
+
...params,
|
|
290
|
+
record: ctx.record,
|
|
291
|
+
claim: op.claim
|
|
292
|
+
})) return false;
|
|
216
293
|
if (type !== KEY && !await keyOwnerMatches(params, op, ctx)) return false;
|
|
217
294
|
if (type === BRANCH) return await authorizeBranch(params, op, ctx);
|
|
218
295
|
if (!op.claim.deleted) {
|
|
219
|
-
const decoded = decodedAs(
|
|
296
|
+
const decoded = await decodedAs({
|
|
297
|
+
...params,
|
|
298
|
+
record: ctx.record,
|
|
299
|
+
op,
|
|
300
|
+
type
|
|
301
|
+
});
|
|
220
302
|
if (decoded === null) return false;
|
|
221
303
|
if (decoded.type === KEY && decoded.payload.state !== op.claim.meta.state) return false;
|
|
222
|
-
if (decoded.type === WRAPPING && await revoked(
|
|
304
|
+
if (decoded.type === WRAPPING && await revoked({
|
|
305
|
+
params,
|
|
306
|
+
op,
|
|
307
|
+
store: ctx.store,
|
|
308
|
+
descriptors: decoded.payload.descriptors
|
|
309
|
+
})) return false;
|
|
223
310
|
}
|
|
224
|
-
if (!await authorized(params, requestFor(
|
|
225
|
-
|
|
311
|
+
if (!await authorized(params, requestFor({
|
|
312
|
+
op,
|
|
313
|
+
record: ctx.record,
|
|
314
|
+
store: ctx.store,
|
|
315
|
+
transportContext: ctx.transportContext,
|
|
316
|
+
verb: 'administer'
|
|
317
|
+
}))) return false;
|
|
318
|
+
return type !== WRAPPING || !bindReceiver || await authorized(params, requestFor({
|
|
319
|
+
op,
|
|
320
|
+
record: ctx.record,
|
|
321
|
+
store: ctx.store,
|
|
322
|
+
transportContext: ctx.transportContext,
|
|
323
|
+
verb: 'read'
|
|
324
|
+
}));
|
|
226
325
|
},
|
|
227
|
-
async visibleTo (record, ctx) {
|
|
326
|
+
async visibleTo ({ record, ctx }) {
|
|
228
327
|
if (ctx.opKind !== params.opKind) return false;
|
|
229
|
-
const op = await verifySignedRecord(
|
|
230
|
-
|
|
328
|
+
const op = await verifySignedRecord({
|
|
329
|
+
record,
|
|
330
|
+
opKind: params.opKind,
|
|
331
|
+
methods: params.methods,
|
|
332
|
+
crypto: params?.crypto
|
|
333
|
+
});
|
|
334
|
+
if (op === null || !await validStructure(type, op, params) || !await recordPayloadMatches({
|
|
335
|
+
...params,
|
|
336
|
+
record,
|
|
337
|
+
claim: op.claim
|
|
338
|
+
})) return false;
|
|
231
339
|
if (!op.claim.deleted) {
|
|
232
|
-
const decoded = decodedAs(
|
|
340
|
+
const decoded = await decodedAs({
|
|
341
|
+
...params,
|
|
342
|
+
record,
|
|
343
|
+
op,
|
|
344
|
+
type
|
|
345
|
+
});
|
|
233
346
|
if (decoded === null) return false;
|
|
234
|
-
if (decoded.type === WRAPPING && await revoked(
|
|
347
|
+
if (decoded.type === WRAPPING && await revoked({
|
|
348
|
+
params,
|
|
349
|
+
op,
|
|
350
|
+
store: ctx.store,
|
|
351
|
+
descriptors: decoded.payload.descriptors
|
|
352
|
+
})) return false;
|
|
235
353
|
}
|
|
236
|
-
return await authorized(params, requestFor(
|
|
354
|
+
return await authorized(params, requestFor({
|
|
355
|
+
op,
|
|
356
|
+
record,
|
|
357
|
+
store: ctx.store,
|
|
358
|
+
transportContext: ctx.transportContext,
|
|
359
|
+
verb: 'read'
|
|
360
|
+
}));
|
|
237
361
|
},
|
|
238
|
-
async lockKeys (incoming, stored) {
|
|
362
|
+
async lockKeys ({ incoming, stored }) {
|
|
239
363
|
const keys = new Map();
|
|
240
364
|
const records = [
|
|
241
365
|
incoming,
|
|
242
366
|
...stored?.copies ?? []
|
|
243
367
|
];
|
|
244
|
-
const ops = await Promise.all(records.map((record)=>
|
|
368
|
+
const ops = await Promise.all(records.map((record)=>{
|
|
369
|
+
return verifySignedRecord({
|
|
370
|
+
record,
|
|
371
|
+
opKind: params.opKind,
|
|
372
|
+
methods: params.methods,
|
|
373
|
+
crypto: params?.crypto
|
|
374
|
+
});
|
|
375
|
+
}));
|
|
245
376
|
for (const op of ops){
|
|
246
377
|
if (op === null) continue;
|
|
247
378
|
const keyID = op.claim.type === KEY ? op.claim.id : op.claim.meta.keyID;
|
|
@@ -265,13 +396,18 @@ function createType(type, params, bindReceiver = true) {
|
|
|
265
396
|
definition.onEqual = ()=>'fork';
|
|
266
397
|
}
|
|
267
398
|
if (type === ENTRY) {
|
|
268
|
-
definition.afterAuthorize = async (record, store)=>{
|
|
399
|
+
definition.afterAuthorize = async ({ record, store })=>{
|
|
269
400
|
await params.portsFor(store).retainEntryCandidate(record);
|
|
270
401
|
};
|
|
271
402
|
}
|
|
272
403
|
if (type === WRAPPING || type === REVOCATION) {
|
|
273
|
-
definition.afterApply = async (record, store)=>{
|
|
274
|
-
const op = await verifySignedRecord(
|
|
404
|
+
definition.afterApply = async ({ record, store })=>{
|
|
405
|
+
const op = await verifySignedRecord({
|
|
406
|
+
record,
|
|
407
|
+
opKind: params.opKind,
|
|
408
|
+
methods: params.methods,
|
|
409
|
+
crypto: params?.crypto
|
|
410
|
+
});
|
|
275
411
|
if (op === null) throw new Error('Applied record signature is invalid');
|
|
276
412
|
await params.portsFor(store).dropRevokedWrappings({
|
|
277
413
|
ownerDID: op.claim.owner,
|
|
@@ -281,7 +417,28 @@ function createType(type, params, bindReceiver = true) {
|
|
|
281
417
|
}
|
|
282
418
|
return definition;
|
|
283
419
|
}
|
|
284
|
-
|
|
420
|
+
function captureParams(input) {
|
|
421
|
+
const getter = Object.getOwnPropertyDescriptor(input, 'methods')?.get;
|
|
422
|
+
const methods = input.methods?.map((method)=>{
|
|
423
|
+
return {
|
|
424
|
+
...method,
|
|
425
|
+
resolve: method.resolve.bind(method),
|
|
426
|
+
resolveHistoric: method.resolveHistoric?.bind(method),
|
|
427
|
+
resolveDenySet: method.resolveDenySet?.bind(method),
|
|
428
|
+
resolveAgreementKey: method.resolveAgreementKey?.bind(method)
|
|
429
|
+
};
|
|
430
|
+
});
|
|
431
|
+
return {
|
|
432
|
+
...input,
|
|
433
|
+
crypto: input.crypto ?? nobleCrypto,
|
|
434
|
+
// Store adapters supply an accessor bound to their current transaction.
|
|
435
|
+
get methods () {
|
|
436
|
+
return getter === undefined ? methods : getter.call(input);
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
/** One explicit replay domain and consumer policy for the five generic record types. */ export function vaultRecordTypes(input) {
|
|
441
|
+
const params = captureParams(input);
|
|
285
442
|
if (typeof params.opKind !== 'string' || params.opKind.length === 0) throw new TypeError('An explicit operation kind is required');
|
|
286
443
|
return new Map(VAULT_RECORD_TYPES.map((type)=>[
|
|
287
444
|
type,
|
|
@@ -297,43 +454,54 @@ function createType(type, params, bindReceiver = true) {
|
|
|
297
454
|
]
|
|
298
455
|
]);
|
|
299
456
|
}
|
|
300
|
-
|
|
301
|
-
|
|
457
|
+
export function vaultKeyVersionReader(input) {
|
|
458
|
+
const { store } = input;
|
|
459
|
+
const params = captureParams(input);
|
|
460
|
+
return async ({ owner, keyID })=>{
|
|
302
461
|
const stored = await store.get({
|
|
303
462
|
type: KEY,
|
|
304
463
|
owner,
|
|
305
464
|
id: keyID
|
|
306
465
|
});
|
|
307
466
|
const copy = stored?.copies[0];
|
|
308
|
-
const op = copy === undefined ? null : await verifySignedRecord(
|
|
309
|
-
|
|
467
|
+
const op = copy === undefined ? null : await verifySignedRecord({
|
|
468
|
+
record: copy,
|
|
469
|
+
opKind: params.opKind,
|
|
470
|
+
methods: params.methods,
|
|
471
|
+
crypto: params?.crypto
|
|
472
|
+
});
|
|
473
|
+
const meta = op === null ? null : await vaultRecordMeta({
|
|
474
|
+
...params,
|
|
475
|
+
type: KEY,
|
|
476
|
+
claim: op.claim
|
|
477
|
+
});
|
|
310
478
|
return op === null || meta === null || op.claim.deleted || op.claim.owner !== owner ? null : meta.version;
|
|
311
479
|
};
|
|
312
480
|
}
|
|
313
|
-
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
definition = createType(WRAPPING, params, false);
|
|
319
|
-
proofTypes.set(params, definition);
|
|
320
|
-
}
|
|
321
|
-
return definition;
|
|
322
|
-
}
|
|
323
|
-
/** Proofs retain consumer authority checks but are never addressed to the local receiver. */ export function vaultProofAuthorizer(params, store, transportContext) {
|
|
324
|
-
const wrapping = proofTypeFor(params);
|
|
325
|
-
return async (record, stored)=>{
|
|
481
|
+
export function vaultProofAuthorizer(input) {
|
|
482
|
+
const { transportContext, store } = input;
|
|
483
|
+
const params = captureParams(input);
|
|
484
|
+
const wrapping = createType(WRAPPING, params, false);
|
|
485
|
+
return async ({ record, stored })=>{
|
|
326
486
|
if (record.type !== WRAPPING) return false;
|
|
327
|
-
const op = await verifySignedRecord(
|
|
328
|
-
if (op === null) return false;
|
|
329
|
-
return await wrapping.authorize(op, {
|
|
487
|
+
const op = await verifySignedRecord({
|
|
330
488
|
record,
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
489
|
+
opKind: params.opKind,
|
|
490
|
+
methods: params.methods,
|
|
491
|
+
crypto: params?.crypto
|
|
492
|
+
});
|
|
493
|
+
if (op === null) return false;
|
|
494
|
+
return await wrapping.authorize({
|
|
495
|
+
op,
|
|
496
|
+
ctx: {
|
|
497
|
+
record,
|
|
498
|
+
stored,
|
|
499
|
+
store,
|
|
500
|
+
transportContext,
|
|
501
|
+
// Generic vault policy delegates all authority to the consumer callback.
|
|
502
|
+
resolveController: async ()=>null,
|
|
503
|
+
verifyCapability: async ()=>false
|
|
504
|
+
}
|
|
337
505
|
});
|
|
338
506
|
};
|
|
339
507
|
}
|
package/lib/records.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { RecordClaim, SignedRecord } from '@kokuin/records';
|
|
2
|
+
import type { CryptoOptions } from '@kokuin/token';
|
|
2
3
|
import type { EncryptEntryResult, WrapResult } from './suite.js';
|
|
4
|
+
export type VaultRecordPayloadErrorParams = {
|
|
5
|
+
type: string;
|
|
6
|
+
id: string;
|
|
7
|
+
reason: string;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
};
|
|
3
10
|
export declare const VAULT_OP_KIND = "kokuin/vault-op/v1";
|
|
4
11
|
export declare const VAULT_RECORD_TYPES: readonly ["vault.key", "vault.key-branch", "vault.wrapping", "vault.entry", "vault.revocation"];
|
|
5
12
|
export type VaultRecordType = (typeof VAULT_RECORD_TYPES)[number];
|
|
@@ -72,20 +79,32 @@ export type DecodedVaultRecord = {
|
|
|
72
79
|
}[VaultRecordType];
|
|
73
80
|
export declare class VaultRecordPayloadError extends Error {
|
|
74
81
|
#private;
|
|
75
|
-
constructor(params:
|
|
76
|
-
type: string;
|
|
77
|
-
id: string;
|
|
78
|
-
reason: string;
|
|
79
|
-
cause?: unknown;
|
|
80
|
-
});
|
|
82
|
+
constructor(params: VaultRecordPayloadErrorParams);
|
|
81
83
|
get type(): string;
|
|
82
84
|
get id(): string;
|
|
83
85
|
}
|
|
84
|
-
export
|
|
86
|
+
export type VaultRevocationRecordIDParams = {
|
|
87
|
+
keyID: string;
|
|
88
|
+
recipientDID: string;
|
|
89
|
+
};
|
|
90
|
+
export declare function vaultRevocationRecordID({ recipientDID, keyID, }: VaultRevocationRecordIDParams): string;
|
|
85
91
|
/** Structural metadata only. Consumer evidence is checked by the injected validator. */
|
|
86
|
-
export declare function
|
|
92
|
+
export declare function parseVaultRecordMeta<Type extends VaultRecordType>(type: Type, claim: RecordClaim): VaultRecordMetas[Type] | null;
|
|
93
|
+
export type VaultRecordMetaParams<Type extends VaultRecordType> = {
|
|
94
|
+
type: Type;
|
|
95
|
+
claim: RecordClaim;
|
|
96
|
+
} & CryptoOptions;
|
|
97
|
+
export declare function vaultRecordMeta<Type extends VaultRecordType>({ claim, type, ...options }: VaultRecordMetaParams<Type>): Promise<VaultRecordMetas[Type] | null>;
|
|
87
98
|
/** Canonical JSON with binary fields encoded as unpadded base64url. */
|
|
88
|
-
export
|
|
99
|
+
export type EncodeVaultRecordPayloadParams<Type extends VaultRecordType> = {
|
|
100
|
+
type: Type;
|
|
101
|
+
payload: VaultRecordPayloads[Type];
|
|
102
|
+
};
|
|
103
|
+
export declare function encodeVaultRecordPayload<Type extends VaultRecordType>({ payload, type, }: EncodeVaultRecordPayloadParams<Type>): Uint8Array;
|
|
89
104
|
/** Pure codec. Signature and payload-digest verification belong to the caller. */
|
|
90
|
-
export
|
|
105
|
+
export type DecodeVaultRecordParams = {
|
|
106
|
+
record: Pick<SignedRecord, 'type' | 'id' | 'owner' | 'hlc' | 'payload'>;
|
|
107
|
+
recordMeta: Record<string, unknown>;
|
|
108
|
+
} & CryptoOptions;
|
|
109
|
+
export declare function decodeVaultRecord({ recordMeta, record, ...options }: DecodeVaultRecordParams): Promise<DecodedVaultRecord>;
|
|
91
110
|
export {};
|