@kokuin/vault 0.1.0 → 0.7.1
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 +108 -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 +269 -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 +8 -7
package/lib/index.js
CHANGED
|
@@ -7,6 +7,6 @@ export * from './id.js';
|
|
|
7
7
|
export * from './key-cache.js';
|
|
8
8
|
export * from './pin-throttle.js';
|
|
9
9
|
export * from './record-types.js';
|
|
10
|
-
export
|
|
10
|
+
export { decodeVaultRecord, encodeVaultRecordPayload, VAULT_OP_KIND, VAULT_RECORD_TYPES, VaultRecordPayloadError, vaultRecordMeta, vaultRevocationRecordID } from './records.js';
|
|
11
11
|
export * from './suite.js';
|
|
12
12
|
export * from './wrappable.js';
|
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, YieldOptions } 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 & YieldOptions & {
|
|
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' | 'yield'>;
|
|
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,25 @@ 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
|
+
yield: params.yield
|
|
166
|
+
});
|
|
167
|
+
}));
|
|
168
|
+
for (const key of keys){
|
|
169
|
+
if (key !== null && key.claim.owner === op.claim.owner && await vaultRecordMeta({
|
|
170
|
+
...params,
|
|
171
|
+
type: KEY,
|
|
172
|
+
claim: key.claim
|
|
173
|
+
}) !== null) return true;
|
|
174
|
+
}
|
|
175
|
+
return false;
|
|
143
176
|
}
|
|
144
|
-
async function revoked(params, op, store, descriptors) {
|
|
177
|
+
async function revoked({ params, op, store, descriptors }) {
|
|
145
178
|
const denied = new Set((await params.portsFor(store).listRevocations({
|
|
146
179
|
ownerDID: op.claim.owner,
|
|
147
180
|
keyID: op.claim.meta.keyID
|
|
@@ -162,18 +195,43 @@ async function revoked(params, op, store, descriptors) {
|
|
|
162
195
|
return false;
|
|
163
196
|
}
|
|
164
197
|
async function authorizeBranch(params, op, ctx) {
|
|
165
|
-
const meta = vaultRecordMeta(
|
|
166
|
-
|
|
198
|
+
const meta = await vaultRecordMeta({
|
|
199
|
+
...params,
|
|
200
|
+
type: BRANCH,
|
|
201
|
+
claim: op.claim
|
|
202
|
+
});
|
|
203
|
+
const decoded = await decodedAs({
|
|
204
|
+
...params,
|
|
205
|
+
record: ctx.record,
|
|
206
|
+
op,
|
|
207
|
+
type: BRANCH
|
|
208
|
+
});
|
|
167
209
|
if (meta === null || decoded === null) return false;
|
|
168
210
|
const payload = decoded.payload;
|
|
169
|
-
const introducing = await verifyRecordOp(
|
|
170
|
-
|
|
211
|
+
const introducing = await verifyRecordOp({
|
|
212
|
+
opJWT: payload.opJWT,
|
|
213
|
+
opKind: params.opKind,
|
|
214
|
+
methods: params.methods,
|
|
215
|
+
crypto: params?.crypto,
|
|
216
|
+
yield: params.yield
|
|
217
|
+
});
|
|
218
|
+
const key = introducing === null ? null : await vaultRecordMeta({
|
|
219
|
+
...params,
|
|
220
|
+
type: KEY,
|
|
221
|
+
claim: introducing.claim
|
|
222
|
+
});
|
|
171
223
|
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
|
-
|
|
224
|
+
const keyPayload = encodeVaultRecordPayload({
|
|
225
|
+
type: KEY,
|
|
226
|
+
payload: {
|
|
227
|
+
suite: payload.suite,
|
|
228
|
+
state: key.state
|
|
229
|
+
}
|
|
175
230
|
});
|
|
176
|
-
if (introducing.claim.payloadDigest !== recordPayloadDigest(
|
|
231
|
+
if (introducing.claim.payloadDigest !== await recordPayloadDigest({
|
|
232
|
+
...params,
|
|
233
|
+
payload: keyPayload
|
|
234
|
+
})) return false;
|
|
177
235
|
// The embedded op is independent authenticated evidence and gets its own consumer decision.
|
|
178
236
|
const keyRecord = {
|
|
179
237
|
type: KEY,
|
|
@@ -184,9 +242,22 @@ async function authorizeBranch(params, op, ctx) {
|
|
|
184
242
|
opJWT: payload.opJWT,
|
|
185
243
|
opHash: introducing.opHash
|
|
186
244
|
};
|
|
187
|
-
return await authorized(params, requestFor(
|
|
245
|
+
return await authorized(params, requestFor({
|
|
246
|
+
op: introducing,
|
|
247
|
+
record: keyRecord,
|
|
248
|
+
store: ctx.store,
|
|
249
|
+
transportContext: ctx.transportContext,
|
|
250
|
+
verb: 'administer'
|
|
251
|
+
})) && await authorized(params, requestFor({
|
|
252
|
+
op,
|
|
253
|
+
record: ctx.record,
|
|
254
|
+
store: ctx.store,
|
|
255
|
+
transportContext: ctx.transportContext,
|
|
256
|
+
verb: 'administer'
|
|
257
|
+
}));
|
|
188
258
|
}
|
|
189
259
|
function createType(type, params, bindReceiver = true) {
|
|
260
|
+
const capturedParams = params;
|
|
190
261
|
const definition = {
|
|
191
262
|
type,
|
|
192
263
|
policy: type === KEY ? {
|
|
@@ -206,42 +277,116 @@ function createType(type, params, bindReceiver = true) {
|
|
|
206
277
|
] : [
|
|
207
278
|
KEY
|
|
208
279
|
],
|
|
209
|
-
precheck: async (op)=>validStructure(type, op),
|
|
210
|
-
dependencies: (_record, op)=>dependencies(type, op),
|
|
211
|
-
async authorize (op, ctx) {
|
|
280
|
+
precheck: async ({ op })=>await validStructure(type, op, params),
|
|
281
|
+
dependencies: ({ record: _record, op })=>dependencies(type, op),
|
|
282
|
+
async authorize ({ op, ctx }) {
|
|
283
|
+
const params = {
|
|
284
|
+
...capturedParams,
|
|
285
|
+
yield: ctx.yield ?? capturedParams.yield
|
|
286
|
+
};
|
|
212
287
|
// Enforce the factory's replay domain even when the engine is misconfigured.
|
|
213
|
-
const checked = await verifySignedRecord(
|
|
288
|
+
const checked = await verifySignedRecord({
|
|
289
|
+
record: ctx.record,
|
|
290
|
+
opKind: params.opKind,
|
|
291
|
+
methods: params.methods,
|
|
292
|
+
crypto: params?.crypto,
|
|
293
|
+
yield: params.yield
|
|
294
|
+
});
|
|
214
295
|
if (checked === null || checked.opHash !== op.opHash) return false;
|
|
215
|
-
if (!validStructure(type, op) || !recordPayloadMatches(
|
|
296
|
+
if (!await validStructure(type, op, params) || !await recordPayloadMatches({
|
|
297
|
+
...params,
|
|
298
|
+
record: ctx.record,
|
|
299
|
+
claim: op.claim
|
|
300
|
+
})) return false;
|
|
216
301
|
if (type !== KEY && !await keyOwnerMatches(params, op, ctx)) return false;
|
|
217
302
|
if (type === BRANCH) return await authorizeBranch(params, op, ctx);
|
|
218
303
|
if (!op.claim.deleted) {
|
|
219
|
-
const decoded = decodedAs(
|
|
304
|
+
const decoded = await decodedAs({
|
|
305
|
+
...params,
|
|
306
|
+
record: ctx.record,
|
|
307
|
+
op,
|
|
308
|
+
type
|
|
309
|
+
});
|
|
220
310
|
if (decoded === null) return false;
|
|
221
311
|
if (decoded.type === KEY && decoded.payload.state !== op.claim.meta.state) return false;
|
|
222
|
-
if (decoded.type === WRAPPING && await revoked(
|
|
312
|
+
if (decoded.type === WRAPPING && await revoked({
|
|
313
|
+
params,
|
|
314
|
+
op,
|
|
315
|
+
store: ctx.store,
|
|
316
|
+
descriptors: decoded.payload.descriptors
|
|
317
|
+
})) return false;
|
|
223
318
|
}
|
|
224
|
-
if (!await authorized(params, requestFor(
|
|
225
|
-
|
|
319
|
+
if (!await authorized(params, requestFor({
|
|
320
|
+
op,
|
|
321
|
+
record: ctx.record,
|
|
322
|
+
store: ctx.store,
|
|
323
|
+
transportContext: ctx.transportContext,
|
|
324
|
+
verb: 'administer'
|
|
325
|
+
}))) return false;
|
|
326
|
+
return type !== WRAPPING || !bindReceiver || await authorized(params, requestFor({
|
|
327
|
+
op,
|
|
328
|
+
record: ctx.record,
|
|
329
|
+
store: ctx.store,
|
|
330
|
+
transportContext: ctx.transportContext,
|
|
331
|
+
verb: 'read'
|
|
332
|
+
}));
|
|
226
333
|
},
|
|
227
|
-
async visibleTo (record, ctx) {
|
|
334
|
+
async visibleTo ({ record, ctx }) {
|
|
335
|
+
const params = {
|
|
336
|
+
...capturedParams,
|
|
337
|
+
yield: ctx.yield ?? capturedParams.yield
|
|
338
|
+
};
|
|
228
339
|
if (ctx.opKind !== params.opKind) return false;
|
|
229
|
-
const op = await verifySignedRecord(
|
|
230
|
-
|
|
340
|
+
const op = await verifySignedRecord({
|
|
341
|
+
record,
|
|
342
|
+
opKind: params.opKind,
|
|
343
|
+
methods: params.methods,
|
|
344
|
+
crypto: params?.crypto,
|
|
345
|
+
yield: params.yield
|
|
346
|
+
});
|
|
347
|
+
if (op === null || !await validStructure(type, op, params) || !await recordPayloadMatches({
|
|
348
|
+
...params,
|
|
349
|
+
record,
|
|
350
|
+
claim: op.claim
|
|
351
|
+
})) return false;
|
|
231
352
|
if (!op.claim.deleted) {
|
|
232
|
-
const decoded = decodedAs(
|
|
353
|
+
const decoded = await decodedAs({
|
|
354
|
+
...params,
|
|
355
|
+
record,
|
|
356
|
+
op,
|
|
357
|
+
type
|
|
358
|
+
});
|
|
233
359
|
if (decoded === null) return false;
|
|
234
|
-
if (decoded.type === WRAPPING && await revoked(
|
|
360
|
+
if (decoded.type === WRAPPING && await revoked({
|
|
361
|
+
params,
|
|
362
|
+
op,
|
|
363
|
+
store: ctx.store,
|
|
364
|
+
descriptors: decoded.payload.descriptors
|
|
365
|
+
})) return false;
|
|
235
366
|
}
|
|
236
|
-
return await authorized(params, requestFor(
|
|
367
|
+
return await authorized(params, requestFor({
|
|
368
|
+
op,
|
|
369
|
+
record,
|
|
370
|
+
store: ctx.store,
|
|
371
|
+
transportContext: ctx.transportContext,
|
|
372
|
+
verb: 'read'
|
|
373
|
+
}));
|
|
237
374
|
},
|
|
238
|
-
async lockKeys (incoming, stored) {
|
|
375
|
+
async lockKeys ({ incoming, stored }) {
|
|
239
376
|
const keys = new Map();
|
|
240
377
|
const records = [
|
|
241
378
|
incoming,
|
|
242
379
|
...stored?.copies ?? []
|
|
243
380
|
];
|
|
244
|
-
const ops = await Promise.all(records.map((record)=>
|
|
381
|
+
const ops = await Promise.all(records.map((record)=>{
|
|
382
|
+
return verifySignedRecord({
|
|
383
|
+
record,
|
|
384
|
+
opKind: params.opKind,
|
|
385
|
+
methods: params.methods,
|
|
386
|
+
crypto: params?.crypto,
|
|
387
|
+
yield: params.yield
|
|
388
|
+
});
|
|
389
|
+
}));
|
|
245
390
|
for (const op of ops){
|
|
246
391
|
if (op === null) continue;
|
|
247
392
|
const keyID = op.claim.type === KEY ? op.claim.id : op.claim.meta.keyID;
|
|
@@ -265,13 +410,19 @@ function createType(type, params, bindReceiver = true) {
|
|
|
265
410
|
definition.onEqual = ()=>'fork';
|
|
266
411
|
}
|
|
267
412
|
if (type === ENTRY) {
|
|
268
|
-
definition.afterAuthorize = async (record, store)=>{
|
|
413
|
+
definition.afterAuthorize = async ({ record, store })=>{
|
|
269
414
|
await params.portsFor(store).retainEntryCandidate(record);
|
|
270
415
|
};
|
|
271
416
|
}
|
|
272
417
|
if (type === WRAPPING || type === REVOCATION) {
|
|
273
|
-
definition.afterApply = async (record, store)=>{
|
|
274
|
-
const op = await verifySignedRecord(
|
|
418
|
+
definition.afterApply = async ({ record, store })=>{
|
|
419
|
+
const op = await verifySignedRecord({
|
|
420
|
+
record,
|
|
421
|
+
opKind: params.opKind,
|
|
422
|
+
methods: params.methods,
|
|
423
|
+
crypto: params?.crypto,
|
|
424
|
+
yield: params.yield
|
|
425
|
+
});
|
|
275
426
|
if (op === null) throw new Error('Applied record signature is invalid');
|
|
276
427
|
await params.portsFor(store).dropRevokedWrappings({
|
|
277
428
|
ownerDID: op.claim.owner,
|
|
@@ -281,7 +432,33 @@ function createType(type, params, bindReceiver = true) {
|
|
|
281
432
|
}
|
|
282
433
|
return definition;
|
|
283
434
|
}
|
|
284
|
-
|
|
435
|
+
function captureParams(input) {
|
|
436
|
+
const getter = Object.getOwnPropertyDescriptor(input, 'methods')?.get;
|
|
437
|
+
const yieldGetter = Object.getOwnPropertyDescriptor(input, 'yield')?.get;
|
|
438
|
+
const yieldPolicy = input.yield;
|
|
439
|
+
const methods = input.methods?.map((method)=>{
|
|
440
|
+
return {
|
|
441
|
+
...method,
|
|
442
|
+
resolve: method.resolve.bind(method),
|
|
443
|
+
resolveHistoric: method.resolveHistoric?.bind(method),
|
|
444
|
+
resolveDenySet: method.resolveDenySet?.bind(method),
|
|
445
|
+
resolveAgreementKey: method.resolveAgreementKey?.bind(method)
|
|
446
|
+
};
|
|
447
|
+
});
|
|
448
|
+
return {
|
|
449
|
+
...input,
|
|
450
|
+
crypto: input.crypto ?? nobleCrypto,
|
|
451
|
+
get yield () {
|
|
452
|
+
return yieldGetter === undefined ? yieldPolicy : yieldGetter.call(input);
|
|
453
|
+
},
|
|
454
|
+
// Store adapters supply an accessor bound to their current transaction.
|
|
455
|
+
get methods () {
|
|
456
|
+
return getter === undefined ? methods : getter.call(input);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
/** One explicit replay domain and consumer policy for the five generic record types. */ export function vaultRecordTypes(input) {
|
|
461
|
+
const params = captureParams(input);
|
|
285
462
|
if (typeof params.opKind !== 'string' || params.opKind.length === 0) throw new TypeError('An explicit operation kind is required');
|
|
286
463
|
return new Map(VAULT_RECORD_TYPES.map((type)=>[
|
|
287
464
|
type,
|
|
@@ -297,43 +474,56 @@ function createType(type, params, bindReceiver = true) {
|
|
|
297
474
|
]
|
|
298
475
|
]);
|
|
299
476
|
}
|
|
300
|
-
|
|
301
|
-
|
|
477
|
+
export function vaultKeyVersionReader(input) {
|
|
478
|
+
const { store } = input;
|
|
479
|
+
const params = captureParams(input);
|
|
480
|
+
return async ({ owner, keyID })=>{
|
|
302
481
|
const stored = await store.get({
|
|
303
482
|
type: KEY,
|
|
304
483
|
owner,
|
|
305
484
|
id: keyID
|
|
306
485
|
});
|
|
307
486
|
const copy = stored?.copies[0];
|
|
308
|
-
const op = copy === undefined ? null : await verifySignedRecord(
|
|
309
|
-
|
|
487
|
+
const op = copy === undefined ? null : await verifySignedRecord({
|
|
488
|
+
record: copy,
|
|
489
|
+
opKind: params.opKind,
|
|
490
|
+
methods: params.methods,
|
|
491
|
+
crypto: params?.crypto,
|
|
492
|
+
yield: params.yield
|
|
493
|
+
});
|
|
494
|
+
const meta = op === null ? null : await vaultRecordMeta({
|
|
495
|
+
...params,
|
|
496
|
+
type: KEY,
|
|
497
|
+
claim: op.claim
|
|
498
|
+
});
|
|
310
499
|
return op === null || meta === null || op.claim.deleted || op.claim.owner !== owner ? null : meta.version;
|
|
311
500
|
};
|
|
312
501
|
}
|
|
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)=>{
|
|
502
|
+
export function vaultProofAuthorizer(input) {
|
|
503
|
+
const { transportContext, store } = input;
|
|
504
|
+
const params = captureParams(input);
|
|
505
|
+
const wrapping = createType(WRAPPING, params, false);
|
|
506
|
+
return async ({ record, stored })=>{
|
|
326
507
|
if (record.type !== WRAPPING) return false;
|
|
327
|
-
const op = await verifySignedRecord(
|
|
328
|
-
if (op === null) return false;
|
|
329
|
-
return await wrapping.authorize(op, {
|
|
508
|
+
const op = await verifySignedRecord({
|
|
330
509
|
record,
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
510
|
+
opKind: params.opKind,
|
|
511
|
+
methods: params.methods,
|
|
512
|
+
crypto: params?.crypto,
|
|
513
|
+
yield: params.yield
|
|
514
|
+
});
|
|
515
|
+
if (op === null) return false;
|
|
516
|
+
return await wrapping.authorize({
|
|
517
|
+
op,
|
|
518
|
+
ctx: {
|
|
519
|
+
record,
|
|
520
|
+
stored,
|
|
521
|
+
store,
|
|
522
|
+
transportContext,
|
|
523
|
+
// Generic vault policy delegates all authority to the consumer callback.
|
|
524
|
+
resolveController: async ()=>null,
|
|
525
|
+
verifyCapability: async ()=>false
|
|
526
|
+
}
|
|
337
527
|
});
|
|
338
528
|
};
|
|
339
529
|
}
|