-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoperator-flow.ts
More file actions
218 lines (202 loc) · 5.39 KB
/
Copy pathoperator-flow.ts
File metadata and controls
218 lines (202 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env npx tsx
import {
RuntimeFetchProvider,
NodeCryptoProvider,
} from '@kya-os/mcp';
import {
CheqdDidRegistrarClient,
cheqdResolver,
createLocalEd25519CheqdRegistrarSigner,
prepareCheqdDlrResource,
updateCheqdAlsoKnownAs,
type CheqdDlrArtifact,
} from '@kya-os/mcp/cheqd';
const did = requiredEnv('CHEQD_DID');
const didWeb = requiredEnv('CHEQD_DID_WEB');
const kid = requiredEnv('CHEQD_KID');
const privateKey = requiredEnv('CHEQD_PRIVATE_KEY_BASE64');
const registrarUrl =
process.env['CHEQD_REGISTRAR_URL'] ?? 'https://did-registrar-staging.cheqd.net/1.0';
const resolverUrl = process.env['CHEQD_RESOLVER_URL'] ?? 'https://resolver.cheqd.net';
const cryptoProvider = new NodeCryptoProvider();
const fetchProvider = new RuntimeFetchProvider({
didResolvers: {
cheqd: cheqdResolver({ resolverUrl }),
},
});
const resolver = { resolve: (value: string) => fetchProvider.resolveDID(value) };
const registrar = new CheqdDidRegistrarClient({
registrarUrl,
fetchProvider,
});
const signer = createLocalEd25519CheqdRegistrarSigner({
cryptoProvider,
privateKey,
verificationMethodId: kid,
signatureEncoding: 'base64url',
});
const runId = new Date().toISOString().replace(/[:.]/g, '-');
console.log(`Resolving ${did}`);
const existingDocument = await resolver.resolve(did);
if (!existingDocument) {
throw new Error(`Could not resolve ${did}`);
}
const linkageResult = await updateCheqdAlsoKnownAs({
didWeb,
didCheqd: did,
resolver,
registrar,
signer,
verificationMethodId: kid,
});
if (linkageResult.registrarResult && !linkageResult.registrarResult.success) {
throw new Error(linkageResult.reason ?? 'alsoKnownAs update failed');
}
console.log(
linkageResult.changed
? `Updated ${did} alsoKnownAs with ${didWeb}`
: `${did} already references ${didWeb}`,
);
for (const artifact of await buildDlrArtifacts()) {
const prepared = await prepareCheqdDlrResource(artifact, cryptoProvider);
const result = await registrar.createResource({
did,
resource: prepared.resource,
signer,
verificationMethodId: kid,
});
if (!result.success) {
throw new Error(`${artifact.type} publish failed: ${result.reason}`);
}
console.log(JSON.stringify({
artifactType: artifact.type,
resourceId: findStringByKey(result.response, 'resourceId'),
contentHash: prepared.contentHash,
}));
}
async function buildDlrArtifacts(): Promise<CheqdDlrArtifact[]> {
const createdAt = new Date().toISOString();
const policyHash = await cryptoProvider.hash(
new TextEncoder().encode(
JSON.stringify({
did,
didWeb,
policy: 'allow:operator-example',
}),
),
);
const metadata = {
runId,
purpose: 'operator-example',
};
return [
{
type: 'CapabilityManifest',
subjectDid: did,
createdAt,
name: `kya-os-capabilities-${runId}`,
resourceType: 'CapabilityManifest',
version: runId,
metadata,
content: {
subjectDid: did,
linkedDidWeb: didWeb,
capabilities: [
{
id: 'kya.proof.issue',
inputs: ['mcpToolCall', 'delegationChain'],
outputs: ['kyaProofReceipt'],
},
],
},
},
{
type: 'ConformanceManifest',
subjectDid: did,
createdAt,
name: `kya-os-conformance-${runId}`,
resourceType: 'ConformanceManifest',
version: runId,
metadata,
content: {
subjectDid: did,
profiles: ['kya-os-mcp', 'cheqd-dlr'],
checks: [
{ id: 'did-linkage-alsoknownas', result: 'pass', evidence: didWeb },
{ id: 'registrar-client-managed-secret', result: 'pass' },
],
},
},
{
type: 'AccessHashManifest',
subjectDid: did,
createdAt,
name: `kya-os-access-hashes-${runId}`,
resourceType: 'AccessHashManifest',
version: runId,
metadata,
content: {
subjectDid: did,
protectedArtifacts: [
{
id: 'mcp-tool-policy',
algorithm: 'sha256',
hash: policyHash,
canonicalization: 'json-canonicalize',
},
],
},
},
{
type: 'TrustConfigManifest',
subjectDid: did,
createdAt,
name: `kya-os-trust-config-${runId}`,
resourceType: 'TrustConfigManifest',
version: runId,
metadata,
content: {
subjectDid: did,
linkedDidWeb: didWeb,
acceptedDidMethods: ['did:web', 'did:key', 'did:cheqd'],
requiredLinkage: {
type: 'alsoKnownAs',
bidirectional: true,
},
trustedIssuers: [did],
},
},
];
}
function requiredEnv(name: string): string {
const value = process.env[name];
if (!value) {
throw new Error(`${name} is required`);
}
return value;
}
function findStringByKey(value: unknown, key: string): string | undefined {
if (Array.isArray(value)) {
for (const entry of value) {
const found = findStringByKey(entry, key);
if (found) {
return found;
}
}
return undefined;
}
if (typeof value !== 'object' || value === null) {
return undefined;
}
const record = value as Record<string, unknown>;
if (typeof record[key] === 'string') {
return record[key];
}
for (const nested of Object.values(record)) {
const found = findStringByKey(nested, key);
if (found) {
return found;
}
}
return undefined;
}