@mcp-i/core 0.1.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/LICENSE +21 -0
- package/README.md +390 -0
- package/dist/auth/handshake.d.ts +104 -0
- package/dist/auth/handshake.d.ts.map +1 -0
- package/dist/auth/handshake.js +230 -0
- package/dist/auth/handshake.js.map +1 -0
- package/dist/auth/index.d.ts +3 -0
- package/dist/auth/index.d.ts.map +1 -0
- package/dist/auth/index.js +2 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/types.d.ts +31 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +7 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/delegation/audience-validator.d.ts +9 -0
- package/dist/delegation/audience-validator.d.ts.map +1 -0
- package/dist/delegation/audience-validator.js +17 -0
- package/dist/delegation/audience-validator.js.map +1 -0
- package/dist/delegation/bitstring.d.ts +37 -0
- package/dist/delegation/bitstring.d.ts.map +1 -0
- package/dist/delegation/bitstring.js +117 -0
- package/dist/delegation/bitstring.js.map +1 -0
- package/dist/delegation/cascading-revocation.d.ts +45 -0
- package/dist/delegation/cascading-revocation.d.ts.map +1 -0
- package/dist/delegation/cascading-revocation.js +148 -0
- package/dist/delegation/cascading-revocation.js.map +1 -0
- package/dist/delegation/delegation-graph.d.ts +49 -0
- package/dist/delegation/delegation-graph.d.ts.map +1 -0
- package/dist/delegation/delegation-graph.js +99 -0
- package/dist/delegation/delegation-graph.js.map +1 -0
- package/dist/delegation/did-key-resolver.d.ts +64 -0
- package/dist/delegation/did-key-resolver.d.ts.map +1 -0
- package/dist/delegation/did-key-resolver.js +154 -0
- package/dist/delegation/did-key-resolver.js.map +1 -0
- package/dist/delegation/did-web-resolver.d.ts +83 -0
- package/dist/delegation/did-web-resolver.d.ts.map +1 -0
- package/dist/delegation/did-web-resolver.js +218 -0
- package/dist/delegation/did-web-resolver.js.map +1 -0
- package/dist/delegation/index.d.ts +21 -0
- package/dist/delegation/index.d.ts.map +1 -0
- package/dist/delegation/index.js +21 -0
- package/dist/delegation/index.js.map +1 -0
- package/dist/delegation/outbound-headers.d.ts +81 -0
- package/dist/delegation/outbound-headers.d.ts.map +1 -0
- package/dist/delegation/outbound-headers.js +139 -0
- package/dist/delegation/outbound-headers.js.map +1 -0
- package/dist/delegation/outbound-proof.d.ts +43 -0
- package/dist/delegation/outbound-proof.d.ts.map +1 -0
- package/dist/delegation/outbound-proof.js +52 -0
- package/dist/delegation/outbound-proof.js.map +1 -0
- package/dist/delegation/statuslist-manager.d.ts +44 -0
- package/dist/delegation/statuslist-manager.d.ts.map +1 -0
- package/dist/delegation/statuslist-manager.js +126 -0
- package/dist/delegation/statuslist-manager.js.map +1 -0
- package/dist/delegation/storage/memory-graph-storage.d.ts +70 -0
- package/dist/delegation/storage/memory-graph-storage.d.ts.map +1 -0
- package/dist/delegation/storage/memory-graph-storage.js +145 -0
- package/dist/delegation/storage/memory-graph-storage.js.map +1 -0
- package/dist/delegation/storage/memory-statuslist-storage.d.ts +19 -0
- package/dist/delegation/storage/memory-statuslist-storage.d.ts.map +1 -0
- package/dist/delegation/storage/memory-statuslist-storage.js +33 -0
- package/dist/delegation/storage/memory-statuslist-storage.js.map +1 -0
- package/dist/delegation/utils.d.ts +49 -0
- package/dist/delegation/utils.d.ts.map +1 -0
- package/dist/delegation/utils.js +131 -0
- package/dist/delegation/utils.js.map +1 -0
- package/dist/delegation/vc-issuer.d.ts +56 -0
- package/dist/delegation/vc-issuer.d.ts.map +1 -0
- package/dist/delegation/vc-issuer.js +80 -0
- package/dist/delegation/vc-issuer.js.map +1 -0
- package/dist/delegation/vc-verifier.d.ts +112 -0
- package/dist/delegation/vc-verifier.d.ts.map +1 -0
- package/dist/delegation/vc-verifier.js +280 -0
- package/dist/delegation/vc-verifier.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +53 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/index.d.ts +2 -0
- package/dist/logging/index.d.ts.map +1 -0
- package/dist/logging/index.js +2 -0
- package/dist/logging/index.js.map +1 -0
- package/dist/logging/logger.d.ts +23 -0
- package/dist/logging/logger.d.ts.map +1 -0
- package/dist/logging/logger.js +82 -0
- package/dist/logging/logger.js.map +1 -0
- package/dist/middleware/index.d.ts +7 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +7 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/middleware/with-mcpi.d.ts +152 -0
- package/dist/middleware/with-mcpi.d.ts.map +1 -0
- package/dist/middleware/with-mcpi.js +472 -0
- package/dist/middleware/with-mcpi.js.map +1 -0
- package/dist/proof/errors.d.ts +49 -0
- package/dist/proof/errors.d.ts.map +1 -0
- package/dist/proof/errors.js +61 -0
- package/dist/proof/errors.js.map +1 -0
- package/dist/proof/generator.d.ts +65 -0
- package/dist/proof/generator.d.ts.map +1 -0
- package/dist/proof/generator.js +163 -0
- package/dist/proof/generator.js.map +1 -0
- package/dist/proof/index.d.ts +4 -0
- package/dist/proof/index.d.ts.map +1 -0
- package/dist/proof/index.js +4 -0
- package/dist/proof/index.js.map +1 -0
- package/dist/proof/verifier.d.ts +108 -0
- package/dist/proof/verifier.d.ts.map +1 -0
- package/dist/proof/verifier.js +299 -0
- package/dist/proof/verifier.js.map +1 -0
- package/dist/providers/base.d.ts +64 -0
- package/dist/providers/base.d.ts.map +1 -0
- package/dist/providers/base.js +19 -0
- package/dist/providers/base.js.map +1 -0
- package/dist/providers/index.d.ts +3 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +3 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/memory.d.ts +33 -0
- package/dist/providers/memory.d.ts.map +1 -0
- package/dist/providers/memory.js +102 -0
- package/dist/providers/memory.js.map +1 -0
- package/dist/session/index.d.ts +2 -0
- package/dist/session/index.d.ts.map +1 -0
- package/dist/session/index.js +2 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/manager.d.ts +77 -0
- package/dist/session/manager.d.ts.map +1 -0
- package/dist/session/manager.js +251 -0
- package/dist/session/manager.js.map +1 -0
- package/dist/types/protocol.d.ts +320 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +229 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/utils/base58.d.ts +31 -0
- package/dist/utils/base58.d.ts.map +1 -0
- package/dist/utils/base58.js +104 -0
- package/dist/utils/base58.js.map +1 -0
- package/dist/utils/base64.d.ts +13 -0
- package/dist/utils/base64.d.ts.map +1 -0
- package/dist/utils/base64.js +99 -0
- package/dist/utils/base64.js.map +1 -0
- package/dist/utils/crypto-service.d.ts +37 -0
- package/dist/utils/crypto-service.d.ts.map +1 -0
- package/dist/utils/crypto-service.js +153 -0
- package/dist/utils/crypto-service.js.map +1 -0
- package/dist/utils/did-helpers.d.ts +156 -0
- package/dist/utils/did-helpers.d.ts.map +1 -0
- package/dist/utils/did-helpers.js +193 -0
- package/dist/utils/did-helpers.js.map +1 -0
- package/dist/utils/ed25519-constants.d.ts +18 -0
- package/dist/utils/ed25519-constants.d.ts.map +1 -0
- package/dist/utils/ed25519-constants.js +21 -0
- package/dist/utils/ed25519-constants.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +105 -0
- package/src/__tests__/integration/full-flow.test.ts +362 -0
- package/src/__tests__/providers/base.test.ts +173 -0
- package/src/__tests__/providers/memory.test.ts +332 -0
- package/src/__tests__/utils/mock-providers.ts +319 -0
- package/src/__tests__/utils/node-crypto-provider.ts +93 -0
- package/src/auth/handshake.ts +411 -0
- package/src/auth/index.ts +11 -0
- package/src/auth/types.ts +40 -0
- package/src/delegation/__tests__/audience-validator.test.ts +110 -0
- package/src/delegation/__tests__/bitstring.test.ts +346 -0
- package/src/delegation/__tests__/cascading-revocation.test.ts +624 -0
- package/src/delegation/__tests__/delegation-graph.test.ts +623 -0
- package/src/delegation/__tests__/did-key-resolver.test.ts +265 -0
- package/src/delegation/__tests__/did-web-resolver.test.ts +467 -0
- package/src/delegation/__tests__/outbound-headers.test.ts +230 -0
- package/src/delegation/__tests__/outbound-proof.test.ts +179 -0
- package/src/delegation/__tests__/statuslist-manager.test.ts +515 -0
- package/src/delegation/__tests__/utils.test.ts +185 -0
- package/src/delegation/__tests__/vc-issuer.test.ts +487 -0
- package/src/delegation/__tests__/vc-verifier.test.ts +1029 -0
- package/src/delegation/audience-validator.ts +24 -0
- package/src/delegation/bitstring.ts +160 -0
- package/src/delegation/cascading-revocation.ts +224 -0
- package/src/delegation/delegation-graph.ts +143 -0
- package/src/delegation/did-key-resolver.ts +181 -0
- package/src/delegation/did-web-resolver.ts +270 -0
- package/src/delegation/index.ts +33 -0
- package/src/delegation/outbound-headers.ts +193 -0
- package/src/delegation/outbound-proof.ts +90 -0
- package/src/delegation/statuslist-manager.ts +219 -0
- package/src/delegation/storage/__tests__/memory-graph-storage.test.ts +366 -0
- package/src/delegation/storage/__tests__/memory-statuslist-storage.test.ts +228 -0
- package/src/delegation/storage/memory-graph-storage.ts +178 -0
- package/src/delegation/storage/memory-statuslist-storage.ts +42 -0
- package/src/delegation/utils.ts +189 -0
- package/src/delegation/vc-issuer.ts +137 -0
- package/src/delegation/vc-verifier.ts +440 -0
- package/src/index.ts +264 -0
- package/src/logging/__tests__/logger.test.ts +366 -0
- package/src/logging/index.ts +6 -0
- package/src/logging/logger.ts +91 -0
- package/src/middleware/__tests__/with-mcpi.test.ts +504 -0
- package/src/middleware/index.ts +16 -0
- package/src/middleware/with-mcpi.ts +766 -0
- package/src/proof/__tests__/proof-generator.test.ts +483 -0
- package/src/proof/__tests__/verifier.test.ts +488 -0
- package/src/proof/errors.ts +75 -0
- package/src/proof/generator.ts +255 -0
- package/src/proof/index.ts +22 -0
- package/src/proof/verifier.ts +449 -0
- package/src/providers/base.ts +68 -0
- package/src/providers/index.ts +15 -0
- package/src/providers/memory.ts +130 -0
- package/src/session/__tests__/session-manager.test.ts +342 -0
- package/src/session/index.ts +7 -0
- package/src/session/manager.ts +332 -0
- package/src/types/protocol.ts +596 -0
- package/src/utils/__tests__/base58.test.ts +281 -0
- package/src/utils/__tests__/base64.test.ts +239 -0
- package/src/utils/__tests__/crypto-service.test.ts +530 -0
- package/src/utils/__tests__/did-helpers.test.ts +156 -0
- package/src/utils/base58.ts +115 -0
- package/src/utils/base64.ts +116 -0
- package/src/utils/crypto-service.ts +209 -0
- package/src/utils/did-helpers.ts +210 -0
- package/src/utils/ed25519-constants.ts +23 -0
- package/src/utils/index.ts +9 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CryptoService
|
|
3
|
+
*
|
|
4
|
+
* Centralized cryptographic operations service providing consistent
|
|
5
|
+
* signature verification across all platforms (Cloudflare, Node.js, etc.).
|
|
6
|
+
*/
|
|
7
|
+
import { base64urlDecodeToString, base64urlDecodeToBytes, base64urlEncodeFromBytes, bytesToBase64, } from './base64.js';
|
|
8
|
+
export class CryptoService {
|
|
9
|
+
cryptoProvider;
|
|
10
|
+
constructor(cryptoProvider) {
|
|
11
|
+
this.cryptoProvider = cryptoProvider;
|
|
12
|
+
}
|
|
13
|
+
async verifyEd25519(data, signature, publicKey) {
|
|
14
|
+
try {
|
|
15
|
+
const result = await this.cryptoProvider.verify(data, signature, publicKey);
|
|
16
|
+
return result === true;
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
console.error('[CryptoService] Ed25519 verification error:', error);
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
parseJWS(jws) {
|
|
24
|
+
const parts = jws.split('.');
|
|
25
|
+
if (parts.length !== 3) {
|
|
26
|
+
throw new Error('Invalid JWS format: expected header.payload.signature');
|
|
27
|
+
}
|
|
28
|
+
const [headerB64, payloadB64, signatureB64] = parts;
|
|
29
|
+
let header;
|
|
30
|
+
try {
|
|
31
|
+
header = JSON.parse(base64urlDecodeToString(headerB64));
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
throw new Error(`Invalid header base64: ${error instanceof Error ? error.message : String(error)}`);
|
|
35
|
+
}
|
|
36
|
+
let payload;
|
|
37
|
+
if (payloadB64) {
|
|
38
|
+
try {
|
|
39
|
+
payload = JSON.parse(base64urlDecodeToString(payloadB64));
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
throw new Error(`Invalid payload base64: ${error instanceof Error ? error.message : String(error)}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
let signatureBytes;
|
|
46
|
+
try {
|
|
47
|
+
signatureBytes = base64urlDecodeToBytes(signatureB64);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new Error(`Invalid signature base64: ${error instanceof Error ? error.message : String(error)}`);
|
|
51
|
+
}
|
|
52
|
+
const signingInput = `${headerB64}.${payloadB64}`;
|
|
53
|
+
return { header, payload, signatureBytes, signingInput };
|
|
54
|
+
}
|
|
55
|
+
async verifyJWS(jws, publicKeyJwk, options) {
|
|
56
|
+
try {
|
|
57
|
+
if (!this.isValidEd25519JWK(publicKeyJwk)) {
|
|
58
|
+
console.error('[CryptoService] Invalid Ed25519 JWK format');
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
if (options?.expectedKid && publicKeyJwk.kid !== options.expectedKid) {
|
|
62
|
+
console.error('[CryptoService] Key ID mismatch');
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
let parsed;
|
|
66
|
+
try {
|
|
67
|
+
parsed = this.parseJWS(jws);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (options?.detachedPayload !== undefined) {
|
|
71
|
+
const parts = jws.split('.');
|
|
72
|
+
if (parts.length === 3 && parts[1] === '') {
|
|
73
|
+
try {
|
|
74
|
+
const headerB64 = parts[0];
|
|
75
|
+
const signatureB64 = parts[2];
|
|
76
|
+
const header = JSON.parse(base64urlDecodeToString(headerB64));
|
|
77
|
+
const signatureBytes = base64urlDecodeToBytes(signatureB64);
|
|
78
|
+
parsed = { header, payload: undefined, signatureBytes, signingInput: '' };
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
console.error('[CryptoService] Invalid detached JWS format');
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
console.error('[CryptoService] Invalid JWS format:', error);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
console.error('[CryptoService] Invalid JWS format:', error);
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const expectedAlg = options?.alg || 'EdDSA';
|
|
96
|
+
if (parsed.header['alg'] !== expectedAlg) {
|
|
97
|
+
console.error(`[CryptoService] Unsupported algorithm: ${parsed.header['alg']}, expected ${expectedAlg}`);
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
let signingInputBytes;
|
|
101
|
+
if (options?.detachedPayload !== undefined) {
|
|
102
|
+
const headerB64 = jws.split('.')[0];
|
|
103
|
+
let payloadB64;
|
|
104
|
+
if (options.detachedPayload instanceof Uint8Array) {
|
|
105
|
+
payloadB64 = base64urlEncodeFromBytes(options.detachedPayload);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
payloadB64 = base64urlEncodeFromBytes(new TextEncoder().encode(options.detachedPayload));
|
|
109
|
+
}
|
|
110
|
+
signingInputBytes = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
if (!parsed.signingInput) {
|
|
114
|
+
console.error('[CryptoService] Missing signing input for compact JWS');
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
signingInputBytes = new TextEncoder().encode(parsed.signingInput);
|
|
118
|
+
}
|
|
119
|
+
let publicKeyBase64;
|
|
120
|
+
try {
|
|
121
|
+
publicKeyBase64 = this.jwkToBase64PublicKey(publicKeyJwk);
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
console.error('[CryptoService] Failed to extract public key:', error);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return await this.verifyEd25519(signingInputBytes, parsed.signatureBytes, publicKeyBase64);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.error('[CryptoService] JWS verification error:', error);
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
isValidEd25519JWK(jwk) {
|
|
135
|
+
return (typeof jwk === 'object' &&
|
|
136
|
+
jwk !== null &&
|
|
137
|
+
'kty' in jwk &&
|
|
138
|
+
jwk['kty'] === 'OKP' &&
|
|
139
|
+
'crv' in jwk &&
|
|
140
|
+
jwk['crv'] === 'Ed25519' &&
|
|
141
|
+
'x' in jwk &&
|
|
142
|
+
typeof jwk['x'] === 'string' &&
|
|
143
|
+
jwk['x'].length > 0);
|
|
144
|
+
}
|
|
145
|
+
jwkToBase64PublicKey(jwk) {
|
|
146
|
+
const publicKeyBytes = base64urlDecodeToBytes(jwk.x);
|
|
147
|
+
if (publicKeyBytes.length !== 32) {
|
|
148
|
+
throw new Error(`Invalid Ed25519 public key length: ${publicKeyBytes.length}`);
|
|
149
|
+
}
|
|
150
|
+
return bytesToBase64(publicKeyBytes);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=crypto-service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto-service.js","sourceRoot":"","sources":["../../src/utils/crypto-service.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,wBAAwB,EACxB,aAAa,GACd,MAAM,aAAa,CAAC;AAoBrB,MAAM,OAAO,aAAa;IACJ;IAApB,YAAoB,cAA8B;QAA9B,mBAA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AO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAEO,iBAAiB,CAAC,GAAY;QACpC,OAAO,CACL,OAAO,GAAG,KAAK,QAAQ;YACvB,GAAG,KAAK,IAAI;YACZ,KAAK,IAAI,GAAG;YACX,GAA+B,CAAC,KAAK,CAAC,KAAK,KAAK;YACjD,KAAK,IAAI,GAAG;YACX,GAA+B,CAAC,KAAK,CAAC,KAAK,SAAS;YACrD,GAAG,IAAI,GAAG;YACV,OAAQ,GAA+B,CAAC,GAAG,CAAC,KAAK,QAAQ;YACvD,GAA+B,CAAC,GAAG,CAAY,CAAC,MAAM,GAAG,CAAC,CAC7D,CAAC;IACJ,CAAC;IAEO,oBAAoB,CAAC,GAAe;QAC1C,MAAM,cAAc,GAAG,sBAAsB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;QACrD,IAAI,cAAc,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,sCAAsC,cAAc,CAAC,MAAM,EAAE,CAAC,CAAC;QACjF,CAAC;QACD,OAAO,aAAa,CAAC,cAAc,CAAC,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DID Validation and Helper Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized utilities for DID validation, normalization, and handling.
|
|
5
|
+
* Promotes DRY principle and consistency across the codebase.
|
|
6
|
+
*
|
|
7
|
+
* @package @mcp-i/core/utils
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Check if a string is a valid DID format
|
|
11
|
+
*
|
|
12
|
+
* @param did - String to validate
|
|
13
|
+
* @returns true if string starts with "did:"
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* isValidDid("did:key:z6Mk...") // true
|
|
18
|
+
* isValidDid("not-a-did") // false
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare function isValidDid(did: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Get the DID method from a DID string
|
|
24
|
+
*
|
|
25
|
+
* @param did - DID string
|
|
26
|
+
* @returns DID method (e.g., "key", "web") or null if invalid
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* getDidMethod("did:key:z6Mk...") // "key"
|
|
31
|
+
* getDidMethod("did:web:example.com") // "web"
|
|
32
|
+
* getDidMethod("invalid") // null
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function getDidMethod(did: string): string | null;
|
|
36
|
+
/**
|
|
37
|
+
* Normalize a DID string (trim whitespace)
|
|
38
|
+
*
|
|
39
|
+
* @param did - DID string to normalize
|
|
40
|
+
* @returns Normalized DID string
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* normalizeDid(" did:key:z6Mk... ") // "did:key:z6Mk..."
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function normalizeDid(did: string): string;
|
|
48
|
+
/**
|
|
49
|
+
* Compare two DIDs for equality (case-sensitive)
|
|
50
|
+
*
|
|
51
|
+
* @param did1 - First DID
|
|
52
|
+
* @param did2 - Second DID
|
|
53
|
+
* @returns true if DIDs are equal (after normalization)
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* compareDids("did:key:z6Mk...", "did:key:z6Mk...") // true
|
|
58
|
+
* compareDids("did:key:z6Mk...", "did:web:example.com") // false
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function compareDids(did1: string, did2: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Extract server DID from config (supports both old and new field names)
|
|
64
|
+
*
|
|
65
|
+
* Supports backward compatibility by reading both `serverDid` and deprecated `agentDid`.
|
|
66
|
+
* Prefers `serverDid` if both are present.
|
|
67
|
+
*
|
|
68
|
+
* @param config - Config object with identity field
|
|
69
|
+
* @returns Server DID string
|
|
70
|
+
* @throws Error if neither serverDid nor agentDid is configured
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* // New config
|
|
75
|
+
* getServerDid({ identity: { serverDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
76
|
+
*
|
|
77
|
+
* // Old config (backward compatibility)
|
|
78
|
+
* getServerDid({ identity: { agentDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
79
|
+
*
|
|
80
|
+
* // Prefers serverDid over agentDid
|
|
81
|
+
* getServerDid({ identity: { serverDid: "new", agentDid: "old" } }) // "new"
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function getServerDid(config: {
|
|
85
|
+
identity: {
|
|
86
|
+
serverDid?: string;
|
|
87
|
+
agentDid?: string;
|
|
88
|
+
};
|
|
89
|
+
}): string;
|
|
90
|
+
/**
|
|
91
|
+
* Extract agent ID from DID
|
|
92
|
+
*
|
|
93
|
+
* The agent ID is the last component of the DID.
|
|
94
|
+
*
|
|
95
|
+
* @param did - DID string
|
|
96
|
+
* @returns Agent ID (last component of DID)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* extractAgentId("did:web:example.com:agents:my-agent") // "my-agent"
|
|
101
|
+
* extractAgentId("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
102
|
+
* extractAgentId("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK") // "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function extractAgentId(did: string): string;
|
|
106
|
+
/**
|
|
107
|
+
* Extract agent slug from DID
|
|
108
|
+
*
|
|
109
|
+
* Agent slug is the same as agent ID - the last component of the DID.
|
|
110
|
+
* For DID format: did:web:example.com:agents:my-agent
|
|
111
|
+
* Returns: my-agent
|
|
112
|
+
*
|
|
113
|
+
* @param did - DID string
|
|
114
|
+
* @returns Agent slug (last component of DID)
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* extractAgentSlug("did:web:example.com:agents:my-agent") // "my-agent"
|
|
119
|
+
* extractAgentSlug("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare function extractAgentSlug(did: string): string;
|
|
123
|
+
/**
|
|
124
|
+
* Generate a did:key from Ed25519 public key bytes
|
|
125
|
+
*
|
|
126
|
+
* Following spec: https://w3c-ccg.github.io/did-method-key/
|
|
127
|
+
* Format: did:key:z<multibase-base58btc(<multicodec-ed25519-pub><publicKey>)>
|
|
128
|
+
*
|
|
129
|
+
* @param publicKeyBytes - Ed25519 public key as Uint8Array (32 bytes)
|
|
130
|
+
* @returns did:key string
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* const publicKey = new Uint8Array(32); // 32-byte Ed25519 public key
|
|
135
|
+
* const did = generateDidKeyFromBytes(publicKey);
|
|
136
|
+
* // did = "did:key:z6Mk..."
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export declare function generateDidKeyFromBytes(publicKeyBytes: Uint8Array): string;
|
|
140
|
+
/**
|
|
141
|
+
* Generate a did:key from base64-encoded Ed25519 public key
|
|
142
|
+
*
|
|
143
|
+
* Convenience wrapper around generateDidKeyFromBytes for base64-encoded keys.
|
|
144
|
+
*
|
|
145
|
+
* @param publicKeyBase64 - Ed25519 public key as base64 string
|
|
146
|
+
* @returns did:key string
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const publicKeyBase64 = "...base64 encoded key...";
|
|
151
|
+
* const did = generateDidKeyFromBase64(publicKeyBase64);
|
|
152
|
+
* // did = "did:key:z6Mk..."
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
export declare function generateDidKeyFromBase64(publicKeyBase64: string): string;
|
|
156
|
+
//# sourceMappingURL=did-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-helpers.d.ts","sourceRoot":"","sources":["../../src/utils/did-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE/C;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAMvD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE/D;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACnC,QAAQ,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CACrD,GAAG,MAAM,CAMT;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAIlD;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEpD;AAQD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,uBAAuB,CAAC,cAAc,EAAE,UAAU,GAAG,MAAM,CAW1E;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAMxE"}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DID Validation and Helper Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized utilities for DID validation, normalization, and handling.
|
|
5
|
+
* Promotes DRY principle and consistency across the codebase.
|
|
6
|
+
*
|
|
7
|
+
* @package @mcp-i/core/utils
|
|
8
|
+
*/
|
|
9
|
+
import { base58Encode } from "./base58.js";
|
|
10
|
+
/**
|
|
11
|
+
* Check if a string is a valid DID format
|
|
12
|
+
*
|
|
13
|
+
* @param did - String to validate
|
|
14
|
+
* @returns true if string starts with "did:"
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* isValidDid("did:key:z6Mk...") // true
|
|
19
|
+
* isValidDid("not-a-did") // false
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function isValidDid(did) {
|
|
23
|
+
return typeof did === "string" && did.startsWith("did:");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the DID method from a DID string
|
|
27
|
+
*
|
|
28
|
+
* @param did - DID string
|
|
29
|
+
* @returns DID method (e.g., "key", "web") or null if invalid
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* getDidMethod("did:key:z6Mk...") // "key"
|
|
34
|
+
* getDidMethod("did:web:example.com") // "web"
|
|
35
|
+
* getDidMethod("invalid") // null
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function getDidMethod(did) {
|
|
39
|
+
if (!isValidDid(did)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const match = did.match(/^did:([^:]+):/);
|
|
43
|
+
return match?.[1] ?? null;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Normalize a DID string (trim whitespace)
|
|
47
|
+
*
|
|
48
|
+
* @param did - DID string to normalize
|
|
49
|
+
* @returns Normalized DID string
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* normalizeDid(" did:key:z6Mk... ") // "did:key:z6Mk..."
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function normalizeDid(did) {
|
|
57
|
+
return did.trim();
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Compare two DIDs for equality (case-sensitive)
|
|
61
|
+
*
|
|
62
|
+
* @param did1 - First DID
|
|
63
|
+
* @param did2 - Second DID
|
|
64
|
+
* @returns true if DIDs are equal (after normalization)
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* compareDids("did:key:z6Mk...", "did:key:z6Mk...") // true
|
|
69
|
+
* compareDids("did:key:z6Mk...", "did:web:example.com") // false
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export function compareDids(did1, did2) {
|
|
73
|
+
return normalizeDid(did1) === normalizeDid(did2);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Extract server DID from config (supports both old and new field names)
|
|
77
|
+
*
|
|
78
|
+
* Supports backward compatibility by reading both `serverDid` and deprecated `agentDid`.
|
|
79
|
+
* Prefers `serverDid` if both are present.
|
|
80
|
+
*
|
|
81
|
+
* @param config - Config object with identity field
|
|
82
|
+
* @returns Server DID string
|
|
83
|
+
* @throws Error if neither serverDid nor agentDid is configured
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // New config
|
|
88
|
+
* getServerDid({ identity: { serverDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
89
|
+
*
|
|
90
|
+
* // Old config (backward compatibility)
|
|
91
|
+
* getServerDid({ identity: { agentDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
92
|
+
*
|
|
93
|
+
* // Prefers serverDid over agentDid
|
|
94
|
+
* getServerDid({ identity: { serverDid: "new", agentDid: "old" } }) // "new"
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
export function getServerDid(config) {
|
|
98
|
+
const serverDid = config.identity.serverDid || config.identity.agentDid;
|
|
99
|
+
if (!serverDid) {
|
|
100
|
+
throw new Error("Server DID not configured");
|
|
101
|
+
}
|
|
102
|
+
return serverDid;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Extract agent ID from DID
|
|
106
|
+
*
|
|
107
|
+
* The agent ID is the last component of the DID.
|
|
108
|
+
*
|
|
109
|
+
* @param did - DID string
|
|
110
|
+
* @returns Agent ID (last component of DID)
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* extractAgentId("did:web:example.com:agents:my-agent") // "my-agent"
|
|
115
|
+
* extractAgentId("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
116
|
+
* extractAgentId("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK") // "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export function extractAgentId(did) {
|
|
120
|
+
const parts = did.split(':');
|
|
121
|
+
// split() always returns at least one element
|
|
122
|
+
return parts[parts.length - 1] ?? did;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Extract agent slug from DID
|
|
126
|
+
*
|
|
127
|
+
* Agent slug is the same as agent ID - the last component of the DID.
|
|
128
|
+
* For DID format: did:web:example.com:agents:my-agent
|
|
129
|
+
* Returns: my-agent
|
|
130
|
+
*
|
|
131
|
+
* @param did - DID string
|
|
132
|
+
* @returns Agent slug (last component of DID)
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* extractAgentSlug("did:web:example.com:agents:my-agent") // "my-agent"
|
|
137
|
+
* extractAgentSlug("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export function extractAgentSlug(did) {
|
|
141
|
+
return extractAgentId(did);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Ed25519 multicodec prefix for did:key encoding
|
|
145
|
+
* As per https://w3c-ccg.github.io/did-method-key/
|
|
146
|
+
*/
|
|
147
|
+
const ED25519_MULTICODEC_PREFIX = new Uint8Array([0xed, 0x01]);
|
|
148
|
+
/**
|
|
149
|
+
* Generate a did:key from Ed25519 public key bytes
|
|
150
|
+
*
|
|
151
|
+
* Following spec: https://w3c-ccg.github.io/did-method-key/
|
|
152
|
+
* Format: did:key:z<multibase-base58btc(<multicodec-ed25519-pub><publicKey>)>
|
|
153
|
+
*
|
|
154
|
+
* @param publicKeyBytes - Ed25519 public key as Uint8Array (32 bytes)
|
|
155
|
+
* @returns did:key string
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const publicKey = new Uint8Array(32); // 32-byte Ed25519 public key
|
|
160
|
+
* const did = generateDidKeyFromBytes(publicKey);
|
|
161
|
+
* // did = "did:key:z6Mk..."
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
export function generateDidKeyFromBytes(publicKeyBytes) {
|
|
165
|
+
// Combine multicodec prefix + public key
|
|
166
|
+
const multicodecKey = new Uint8Array(ED25519_MULTICODEC_PREFIX.length + publicKeyBytes.length);
|
|
167
|
+
multicodecKey.set(ED25519_MULTICODEC_PREFIX);
|
|
168
|
+
multicodecKey.set(publicKeyBytes, ED25519_MULTICODEC_PREFIX.length);
|
|
169
|
+
// Base58-btc encode and add multibase prefix 'z'
|
|
170
|
+
const base58Encoded = base58Encode(multicodecKey);
|
|
171
|
+
return `did:key:z${base58Encoded}`;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Generate a did:key from base64-encoded Ed25519 public key
|
|
175
|
+
*
|
|
176
|
+
* Convenience wrapper around generateDidKeyFromBytes for base64-encoded keys.
|
|
177
|
+
*
|
|
178
|
+
* @param publicKeyBase64 - Ed25519 public key as base64 string
|
|
179
|
+
* @returns did:key string
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* const publicKeyBase64 = "...base64 encoded key...";
|
|
184
|
+
* const did = generateDidKeyFromBase64(publicKeyBase64);
|
|
185
|
+
* // did = "did:key:z6Mk..."
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export function generateDidKeyFromBase64(publicKeyBase64) {
|
|
189
|
+
// Decode base64 to bytes
|
|
190
|
+
const publicKeyBytes = Uint8Array.from(atob(publicKeyBase64), (c) => c.charCodeAt(0));
|
|
191
|
+
return generateDidKeyFromBytes(publicKeyBytes);
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=did-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"did-helpers.js","sourceRoot":"","sources":["../../src/utils/did-helpers.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE3C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;AAC3D,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;IACzC,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW;IACtC,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC;AACpB,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,IAAY;IACpD,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,YAAY,CAAC,IAAI,CAA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ACH,MAAM,UAAU,wBAAwB,CAAC,eAAuB;IAC9D,yBAAyB;IACzB,MAAM,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAClE,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAChB,CAAC;IACF,OAAO,uBAAuB,CAAC,cAAc,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 Key Format Constants
|
|
3
|
+
*
|
|
4
|
+
* DER-encoded headers for Ed25519 key formats per RFC 8410 and RFC 5958.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* DER-encoded PKCS#8 header for Ed25519 private keys (RFC 8410 §7, RFC 5958).
|
|
8
|
+
* Prepended to the 32-byte Ed25519 seed to form a valid PKCS#8 private key.
|
|
9
|
+
*/
|
|
10
|
+
export declare const ED25519_PKCS8_DER_HEADER: Uint8Array<ArrayBuffer>;
|
|
11
|
+
/**
|
|
12
|
+
* DER-encoded SPKI header for Ed25519 public keys (RFC 8410 §4).
|
|
13
|
+
* The 32-byte public key follows after this header.
|
|
14
|
+
*/
|
|
15
|
+
export declare const ED25519_SPKI_DER_HEADER_LENGTH = 12;
|
|
16
|
+
/** Ed25519 raw key size in bytes */
|
|
17
|
+
export declare const ED25519_KEY_SIZE = 32;
|
|
18
|
+
//# sourceMappingURL=ed25519-constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519-constants.d.ts","sourceRoot":"","sources":["../../src/utils/ed25519-constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,eAAO,MAAM,wBAAwB,yBAGnC,CAAC;AAEH;;;GAGG;AACH,eAAO,MAAM,8BAA8B,KAAK,CAAC;AAEjD,oCAAoC;AACpC,eAAO,MAAM,gBAAgB,KAAK,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 Key Format Constants
|
|
3
|
+
*
|
|
4
|
+
* DER-encoded headers for Ed25519 key formats per RFC 8410 and RFC 5958.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* DER-encoded PKCS#8 header for Ed25519 private keys (RFC 8410 §7, RFC 5958).
|
|
8
|
+
* Prepended to the 32-byte Ed25519 seed to form a valid PKCS#8 private key.
|
|
9
|
+
*/
|
|
10
|
+
export const ED25519_PKCS8_DER_HEADER = new Uint8Array([
|
|
11
|
+
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05,
|
|
12
|
+
0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
|
|
13
|
+
]);
|
|
14
|
+
/**
|
|
15
|
+
* DER-encoded SPKI header for Ed25519 public keys (RFC 8410 §4).
|
|
16
|
+
* The 32-byte public key follows after this header.
|
|
17
|
+
*/
|
|
18
|
+
export const ED25519_SPKI_DER_HEADER_LENGTH = 12;
|
|
19
|
+
/** Ed25519 raw key size in bytes */
|
|
20
|
+
export const ED25519_KEY_SIZE = 32;
|
|
21
|
+
//# sourceMappingURL=ed25519-constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519-constants.js","sourceRoot":"","sources":["../../src/utils/ed25519-constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,UAAU,CAAC;IACrD,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;IACxC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI;CACrD,CAAC,CAAC;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,EAAE,CAAC;AAEjD,oCAAoC;AACpC,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC9B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC9B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC"}
|