-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathissue-delegation.ts
More file actions
74 lines (65 loc) · 1.91 KB
/
issue-delegation.ts
File metadata and controls
74 lines (65 loc) · 1.91 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
#!/usr/bin/env npx tsx
/**
* Run this to get a delegation VC you can pass to restricted_greet.
*
* Usage:
* npx tsx examples/node-server/issue-delegation.ts
*
* Then copy the printed JSON and pass it as `_kyaos_delegation` when calling
* restricted_greet via MCP Inspector.
*/
import { NodeCryptoProvider } from './node-crypto.js';
import {
generateDidKeyFromBase64,
DelegationCredentialIssuer,
base64urlEncodeFromBytes,
type Proof,
} from '@kya-os/mcp';
async function main() {
const crypto = new NodeCryptoProvider();
const keyPair = await crypto.generateKeyPair();
const did = generateDidKeyFromBase64(keyPair.publicKey);
const kid = `${did}#${did.replace('did:key:', '')}`;
process.stderr.write(`[issue-delegation] Issuer DID: ${did}\n`);
const signingFunction = async (
canonicalVC: string,
_issuerDid: string,
kidArg: string,
): Promise<Proof> => {
const data = new TextEncoder().encode(canonicalVC);
const sigBytes = await crypto.sign(data, keyPair.privateKey);
const proofValue = base64urlEncodeFromBytes(sigBytes);
return {
type: 'Ed25519Signature2020',
created: new Date().toISOString(),
verificationMethod: kidArg,
proofPurpose: 'assertionMethod',
proofValue,
};
};
const issuer = new DelegationCredentialIssuer(
{
getDid: () => did,
getKeyId: () => kid,
getPrivateKey: () => keyPair.privateKey,
},
signingFunction,
);
const delegationId = `delegation-${Date.now()}`;
const vc = await issuer.createAndIssueDelegation(
{
id: delegationId,
issuerDid: did,
subjectDid: did,
constraints: {
scopes: ['greeting:restricted'],
notAfter: Math.floor(Date.now() / 1000) + 3600, // valid for 1 hour
},
},
);
process.stdout.write(JSON.stringify(vc, null, 2) + '\n');
}
main().catch((err) => {
process.stderr.write(`Fatal: ${err}\n`);
process.exit(1);
});