@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,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base58 Utilities (Bitcoin alphabet)
|
|
3
|
+
*
|
|
4
|
+
* Encoding and decoding utilities for Base58 (Bitcoin alphabet).
|
|
5
|
+
* Used for did:key multibase encoding (with 'z' prefix for base58btc).
|
|
6
|
+
*
|
|
7
|
+
* The Bitcoin alphabet excludes ambiguous characters (0, O, I, l).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
|
|
11
|
+
const ALPHABET_MAP = new Map<string, number>();
|
|
12
|
+
|
|
13
|
+
// Build reverse lookup map
|
|
14
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
15
|
+
const char = ALPHABET[i];
|
|
16
|
+
if (char !== undefined) {
|
|
17
|
+
ALPHABET_MAP.set(char, i);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Encode bytes to Base58 (Bitcoin alphabet)
|
|
23
|
+
*
|
|
24
|
+
* @param bytes - Bytes to encode
|
|
25
|
+
* @returns Base58-encoded string
|
|
26
|
+
*/
|
|
27
|
+
export function base58Encode(bytes: Uint8Array): string {
|
|
28
|
+
if (bytes.length === 0) return '';
|
|
29
|
+
|
|
30
|
+
// Convert bytes to big integer
|
|
31
|
+
let num = BigInt(0);
|
|
32
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
33
|
+
const byte = bytes[i];
|
|
34
|
+
if (byte !== undefined) {
|
|
35
|
+
num = num * BigInt(256) + BigInt(byte);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Convert to base58
|
|
40
|
+
let result = '';
|
|
41
|
+
while (num > 0) {
|
|
42
|
+
result = ALPHABET[Number(num % BigInt(58))] + result;
|
|
43
|
+
num = num / BigInt(58);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Add leading zeros (encoded as '1' in base58)
|
|
47
|
+
for (let i = 0; i < bytes.length && bytes[i] === 0; i++) {
|
|
48
|
+
result = '1' + result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Decode Base58 (Bitcoin alphabet) to bytes
|
|
56
|
+
*
|
|
57
|
+
* @param encoded - Base58-encoded string
|
|
58
|
+
* @returns Decoded bytes
|
|
59
|
+
* @throws Error if input contains invalid characters
|
|
60
|
+
*/
|
|
61
|
+
export function base58Decode(encoded: string): Uint8Array {
|
|
62
|
+
if (encoded.length === 0) return new Uint8Array(0);
|
|
63
|
+
|
|
64
|
+
// Convert base58 to big integer
|
|
65
|
+
let num = BigInt(0);
|
|
66
|
+
for (const char of encoded) {
|
|
67
|
+
const value = ALPHABET_MAP.get(char);
|
|
68
|
+
if (value === undefined) {
|
|
69
|
+
throw new Error(`Invalid base58 character: ${char}`);
|
|
70
|
+
}
|
|
71
|
+
num = num * BigInt(58) + BigInt(value);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Convert big integer to bytes
|
|
75
|
+
const bytes: number[] = [];
|
|
76
|
+
while (num > 0) {
|
|
77
|
+
bytes.unshift(Number(num % BigInt(256)));
|
|
78
|
+
num = num / BigInt(256);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Count leading zeros in input (encoded as '1')
|
|
82
|
+
let leadingZeros = 0;
|
|
83
|
+
for (const char of encoded) {
|
|
84
|
+
if (char === '1') {
|
|
85
|
+
leadingZeros++;
|
|
86
|
+
} else {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Prepend leading zero bytes
|
|
92
|
+
const result = new Uint8Array(leadingZeros + bytes.length);
|
|
93
|
+
// Leading zeros are already 0 in Uint8Array
|
|
94
|
+
result.set(bytes, leadingZeros);
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Validate a Base58 string
|
|
101
|
+
*
|
|
102
|
+
* @param encoded - String to validate
|
|
103
|
+
* @returns true if valid Base58, false otherwise
|
|
104
|
+
*/
|
|
105
|
+
export function isValidBase58(encoded: string): boolean {
|
|
106
|
+
if (encoded.length === 0) return true;
|
|
107
|
+
|
|
108
|
+
for (const char of encoded) {
|
|
109
|
+
if (!ALPHABET_MAP.has(char)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base64URL Encoding/Decoding Utilities
|
|
3
|
+
*
|
|
4
|
+
* Environment-aware base64url helpers that work in both Node.js and Cloudflare Workers.
|
|
5
|
+
* Uses Buffer in Node.js, TextEncoder/Decoder fallback for Workers.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function base64urlDecodeToString(input: string): string {
|
|
9
|
+
const padded = addPadding(input);
|
|
10
|
+
const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
|
|
11
|
+
|
|
12
|
+
if (typeof Buffer !== 'undefined') {
|
|
13
|
+
const base64Regex = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
14
|
+
if (!base64Regex.test(base64)) {
|
|
15
|
+
throw new Error('Invalid base64url string: contains invalid characters');
|
|
16
|
+
}
|
|
17
|
+
return Buffer.from(base64, 'base64').toString('utf-8');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (typeof atob !== 'undefined') {
|
|
21
|
+
const binaryString = atob(base64);
|
|
22
|
+
if (typeof TextDecoder !== 'undefined') {
|
|
23
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
24
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
25
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
26
|
+
}
|
|
27
|
+
return new TextDecoder().decode(bytes);
|
|
28
|
+
}
|
|
29
|
+
return binaryString;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
throw new Error('Neither Buffer nor atob is available');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function base64urlDecodeToBytes(input: string): Uint8Array {
|
|
36
|
+
const padded = addPadding(input);
|
|
37
|
+
const base64 = padded.replace(/-/g, '+').replace(/_/g, '/');
|
|
38
|
+
|
|
39
|
+
if (typeof atob !== 'undefined') {
|
|
40
|
+
const binaryString = atob(base64);
|
|
41
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
42
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
43
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
44
|
+
}
|
|
45
|
+
return bytes;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return new Uint8Array(Buffer.from(base64, 'base64'));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function base64urlEncodeFromString(input: string): string {
|
|
52
|
+
if (typeof Buffer !== 'undefined') {
|
|
53
|
+
const base64 = Buffer.from(input, 'utf-8').toString('base64');
|
|
54
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (typeof btoa !== 'undefined') {
|
|
58
|
+
const utf8Bytes = new TextEncoder().encode(input);
|
|
59
|
+
const binaryString = Array.from(utf8Bytes)
|
|
60
|
+
.map((byte) => String.fromCharCode(byte))
|
|
61
|
+
.join('');
|
|
62
|
+
const base64 = btoa(binaryString);
|
|
63
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new Error('Neither Buffer nor btoa is available');
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function base64urlEncodeFromBytes(bytes: Uint8Array): string {
|
|
70
|
+
if (typeof btoa !== 'undefined') {
|
|
71
|
+
const binaryString = Array.from(bytes)
|
|
72
|
+
.map((byte) => String.fromCharCode(byte))
|
|
73
|
+
.join('');
|
|
74
|
+
const base64 = btoa(binaryString);
|
|
75
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const base64 = Buffer.from(bytes).toString('base64');
|
|
79
|
+
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function bytesToBase64(bytes: Uint8Array): string {
|
|
83
|
+
if (typeof btoa !== 'undefined') {
|
|
84
|
+
const binaryString = Array.from(bytes)
|
|
85
|
+
.map((byte) => String.fromCharCode(byte))
|
|
86
|
+
.join('');
|
|
87
|
+
return btoa(binaryString);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return Buffer.from(bytes).toString('base64');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function base64ToBytes(base64: string): Uint8Array {
|
|
94
|
+
let standardBase64 = base64.replace(/-/g, '+').replace(/_/g, '/');
|
|
95
|
+
const paddingNeeded = (4 - (standardBase64.length % 4)) % 4;
|
|
96
|
+
standardBase64 += '='.repeat(paddingNeeded);
|
|
97
|
+
|
|
98
|
+
if (typeof atob !== 'undefined') {
|
|
99
|
+
const binaryString = atob(standardBase64);
|
|
100
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
101
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
102
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
103
|
+
}
|
|
104
|
+
return bytes;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return new Uint8Array(Buffer.from(standardBase64, 'base64'));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function addPadding(input: string): string {
|
|
111
|
+
const remainder = input.length % 4;
|
|
112
|
+
if (remainder === 0) {
|
|
113
|
+
return input;
|
|
114
|
+
}
|
|
115
|
+
return input + '='.repeat((4 - remainder) % 4);
|
|
116
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CryptoService
|
|
3
|
+
*
|
|
4
|
+
* Centralized cryptographic operations service providing consistent
|
|
5
|
+
* signature verification across all platforms (Cloudflare, Node.js, etc.).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { CryptoProvider } from '../providers/base.js';
|
|
9
|
+
import {
|
|
10
|
+
base64urlDecodeToString,
|
|
11
|
+
base64urlDecodeToBytes,
|
|
12
|
+
base64urlEncodeFromBytes,
|
|
13
|
+
bytesToBase64,
|
|
14
|
+
} from './base64.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Minimal Ed25519 JWK interface
|
|
18
|
+
*/
|
|
19
|
+
export interface Ed25519JWK {
|
|
20
|
+
kty: 'OKP';
|
|
21
|
+
crv: 'Ed25519';
|
|
22
|
+
x: string;
|
|
23
|
+
kid?: string;
|
|
24
|
+
use?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ParsedJWS {
|
|
28
|
+
header: Record<string, unknown>;
|
|
29
|
+
payload?: Record<string, unknown>;
|
|
30
|
+
signatureBytes: Uint8Array;
|
|
31
|
+
signingInput: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class CryptoService {
|
|
35
|
+
constructor(private cryptoProvider: CryptoProvider) {}
|
|
36
|
+
|
|
37
|
+
async verifyEd25519(
|
|
38
|
+
data: Uint8Array,
|
|
39
|
+
signature: Uint8Array,
|
|
40
|
+
publicKey: string
|
|
41
|
+
): Promise<boolean> {
|
|
42
|
+
try {
|
|
43
|
+
const result = await this.cryptoProvider.verify(data, signature, publicKey);
|
|
44
|
+
return result === true;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error('[CryptoService] Ed25519 verification error:', error);
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
parseJWS(jws: string): ParsedJWS {
|
|
52
|
+
const parts = jws.split('.');
|
|
53
|
+
if (parts.length !== 3) {
|
|
54
|
+
throw new Error('Invalid JWS format: expected header.payload.signature');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const [headerB64, payloadB64, signatureB64] = parts;
|
|
58
|
+
|
|
59
|
+
let header: Record<string, unknown>;
|
|
60
|
+
try {
|
|
61
|
+
header = JSON.parse(base64urlDecodeToString(headerB64!)) as Record<string, unknown>;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Invalid header base64: ${error instanceof Error ? error.message : String(error)}`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
let payload: Record<string, unknown> | undefined;
|
|
69
|
+
if (payloadB64) {
|
|
70
|
+
try {
|
|
71
|
+
payload = JSON.parse(base64urlDecodeToString(payloadB64)) as Record<string, unknown>;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`Invalid payload base64: ${error instanceof Error ? error.message : String(error)}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let signatureBytes: Uint8Array;
|
|
80
|
+
try {
|
|
81
|
+
signatureBytes = base64urlDecodeToBytes(signatureB64!);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Invalid signature base64: ${error instanceof Error ? error.message : String(error)}`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const signingInput = `${headerB64}.${payloadB64}`;
|
|
89
|
+
|
|
90
|
+
return { header, payload, signatureBytes, signingInput };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async verifyJWS(
|
|
94
|
+
jws: string,
|
|
95
|
+
publicKeyJwk: Ed25519JWK,
|
|
96
|
+
options?: {
|
|
97
|
+
detachedPayload?: Uint8Array | string;
|
|
98
|
+
expectedKid?: string;
|
|
99
|
+
alg?: 'EdDSA';
|
|
100
|
+
}
|
|
101
|
+
): Promise<boolean> {
|
|
102
|
+
try {
|
|
103
|
+
if (!this.isValidEd25519JWK(publicKeyJwk)) {
|
|
104
|
+
console.error('[CryptoService] Invalid Ed25519 JWK format');
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (options?.expectedKid && publicKeyJwk.kid !== options.expectedKid) {
|
|
109
|
+
console.error('[CryptoService] Key ID mismatch');
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let parsed: ParsedJWS;
|
|
114
|
+
try {
|
|
115
|
+
parsed = this.parseJWS(jws);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (options?.detachedPayload !== undefined) {
|
|
118
|
+
const parts = jws.split('.');
|
|
119
|
+
if (parts.length === 3 && parts[1] === '') {
|
|
120
|
+
try {
|
|
121
|
+
const headerB64 = parts[0]!;
|
|
122
|
+
const signatureB64 = parts[2]!;
|
|
123
|
+
const header = JSON.parse(
|
|
124
|
+
base64urlDecodeToString(headerB64)
|
|
125
|
+
) as Record<string, unknown>;
|
|
126
|
+
const signatureBytes = base64urlDecodeToBytes(signatureB64);
|
|
127
|
+
parsed = { header, payload: undefined, signatureBytes, signingInput: '' };
|
|
128
|
+
} catch {
|
|
129
|
+
console.error('[CryptoService] Invalid detached JWS format');
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
console.error('[CryptoService] Invalid JWS format:', error);
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
console.error('[CryptoService] Invalid JWS format:', error);
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const expectedAlg = options?.alg || 'EdDSA';
|
|
143
|
+
if (parsed.header['alg'] !== expectedAlg) {
|
|
144
|
+
console.error(
|
|
145
|
+
`[CryptoService] Unsupported algorithm: ${parsed.header['alg']}, expected ${expectedAlg}`
|
|
146
|
+
);
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
let signingInputBytes: Uint8Array;
|
|
151
|
+
|
|
152
|
+
if (options?.detachedPayload !== undefined) {
|
|
153
|
+
const headerB64 = jws.split('.')[0]!;
|
|
154
|
+
let payloadB64: string;
|
|
155
|
+
|
|
156
|
+
if (options.detachedPayload instanceof Uint8Array) {
|
|
157
|
+
payloadB64 = base64urlEncodeFromBytes(options.detachedPayload);
|
|
158
|
+
} else {
|
|
159
|
+
payloadB64 = base64urlEncodeFromBytes(
|
|
160
|
+
new TextEncoder().encode(options.detachedPayload)
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
signingInputBytes = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
|
|
165
|
+
} else {
|
|
166
|
+
if (!parsed.signingInput) {
|
|
167
|
+
console.error('[CryptoService] Missing signing input for compact JWS');
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
signingInputBytes = new TextEncoder().encode(parsed.signingInput);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let publicKeyBase64: string;
|
|
174
|
+
try {
|
|
175
|
+
publicKeyBase64 = this.jwkToBase64PublicKey(publicKeyJwk);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
console.error('[CryptoService] Failed to extract public key:', error);
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return await this.verifyEd25519(signingInputBytes, parsed.signatureBytes, publicKeyBase64);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error('[CryptoService] JWS verification error:', error);
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private isValidEd25519JWK(jwk: unknown): jwk is Ed25519JWK {
|
|
189
|
+
return (
|
|
190
|
+
typeof jwk === 'object' &&
|
|
191
|
+
jwk !== null &&
|
|
192
|
+
'kty' in jwk &&
|
|
193
|
+
(jwk as Record<string, unknown>)['kty'] === 'OKP' &&
|
|
194
|
+
'crv' in jwk &&
|
|
195
|
+
(jwk as Record<string, unknown>)['crv'] === 'Ed25519' &&
|
|
196
|
+
'x' in jwk &&
|
|
197
|
+
typeof (jwk as Record<string, unknown>)['x'] === 'string' &&
|
|
198
|
+
((jwk as Record<string, unknown>)['x'] as string).length > 0
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
private jwkToBase64PublicKey(jwk: Ed25519JWK): string {
|
|
203
|
+
const publicKeyBytes = base64urlDecodeToBytes(jwk.x);
|
|
204
|
+
if (publicKeyBytes.length !== 32) {
|
|
205
|
+
throw new Error(`Invalid Ed25519 public key length: ${publicKeyBytes.length}`);
|
|
206
|
+
}
|
|
207
|
+
return bytesToBase64(publicKeyBytes);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
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
|
+
import { base58Encode } from "./base58.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Check if a string is a valid DID format
|
|
14
|
+
*
|
|
15
|
+
* @param did - String to validate
|
|
16
|
+
* @returns true if string starts with "did:"
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* isValidDid("did:key:z6Mk...") // true
|
|
21
|
+
* isValidDid("not-a-did") // false
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export function isValidDid(did: string): boolean {
|
|
25
|
+
return typeof did === "string" && did.startsWith("did:");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get the DID method from a DID string
|
|
30
|
+
*
|
|
31
|
+
* @param did - DID string
|
|
32
|
+
* @returns DID method (e.g., "key", "web") or null if invalid
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* getDidMethod("did:key:z6Mk...") // "key"
|
|
37
|
+
* getDidMethod("did:web:example.com") // "web"
|
|
38
|
+
* getDidMethod("invalid") // null
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export function getDidMethod(did: string): string | null {
|
|
42
|
+
if (!isValidDid(did)) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
const match = did.match(/^did:([^:]+):/);
|
|
46
|
+
return match?.[1] ?? null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Normalize a DID string (trim whitespace)
|
|
51
|
+
*
|
|
52
|
+
* @param did - DID string to normalize
|
|
53
|
+
* @returns Normalized DID string
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```typescript
|
|
57
|
+
* normalizeDid(" did:key:z6Mk... ") // "did:key:z6Mk..."
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function normalizeDid(did: string): string {
|
|
61
|
+
return did.trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Compare two DIDs for equality (case-sensitive)
|
|
66
|
+
*
|
|
67
|
+
* @param did1 - First DID
|
|
68
|
+
* @param did2 - Second DID
|
|
69
|
+
* @returns true if DIDs are equal (after normalization)
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* compareDids("did:key:z6Mk...", "did:key:z6Mk...") // true
|
|
74
|
+
* compareDids("did:key:z6Mk...", "did:web:example.com") // false
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function compareDids(did1: string, did2: string): boolean {
|
|
78
|
+
return normalizeDid(did1) === normalizeDid(did2);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Extract server DID from config (supports both old and new field names)
|
|
83
|
+
*
|
|
84
|
+
* Supports backward compatibility by reading both `serverDid` and deprecated `agentDid`.
|
|
85
|
+
* Prefers `serverDid` if both are present.
|
|
86
|
+
*
|
|
87
|
+
* @param config - Config object with identity field
|
|
88
|
+
* @returns Server DID string
|
|
89
|
+
* @throws Error if neither serverDid nor agentDid is configured
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* // New config
|
|
94
|
+
* getServerDid({ identity: { serverDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
95
|
+
*
|
|
96
|
+
* // Old config (backward compatibility)
|
|
97
|
+
* getServerDid({ identity: { agentDid: "did:web:example.com" } }) // "did:web:example.com"
|
|
98
|
+
*
|
|
99
|
+
* // Prefers serverDid over agentDid
|
|
100
|
+
* getServerDid({ identity: { serverDid: "new", agentDid: "old" } }) // "new"
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export function getServerDid(config: {
|
|
104
|
+
identity: { serverDid?: string; agentDid?: string };
|
|
105
|
+
}): string {
|
|
106
|
+
const serverDid = config.identity.serverDid || config.identity.agentDid;
|
|
107
|
+
if (!serverDid) {
|
|
108
|
+
throw new Error("Server DID not configured");
|
|
109
|
+
}
|
|
110
|
+
return serverDid;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Extract agent ID from DID
|
|
115
|
+
*
|
|
116
|
+
* The agent ID is the last component of the DID.
|
|
117
|
+
*
|
|
118
|
+
* @param did - DID string
|
|
119
|
+
* @returns Agent ID (last component of DID)
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```typescript
|
|
123
|
+
* extractAgentId("did:web:example.com:agents:my-agent") // "my-agent"
|
|
124
|
+
* extractAgentId("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
125
|
+
* extractAgentId("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK") // "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK"
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
export function extractAgentId(did: string): string {
|
|
129
|
+
const parts = did.split(':');
|
|
130
|
+
// split() always returns at least one element
|
|
131
|
+
return parts[parts.length - 1] ?? did;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Extract agent slug from DID
|
|
136
|
+
*
|
|
137
|
+
* Agent slug is the same as agent ID - the last component of the DID.
|
|
138
|
+
* For DID format: did:web:example.com:agents:my-agent
|
|
139
|
+
* Returns: my-agent
|
|
140
|
+
*
|
|
141
|
+
* @param did - DID string
|
|
142
|
+
* @returns Agent slug (last component of DID)
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* extractAgentSlug("did:web:example.com:agents:my-agent") // "my-agent"
|
|
147
|
+
* extractAgentSlug("did:web:localhost:3000:agents:12912feb") // "12912feb"
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export function extractAgentSlug(did: string): string {
|
|
151
|
+
return extractAgentId(did);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Ed25519 multicodec prefix for did:key encoding
|
|
156
|
+
* As per https://w3c-ccg.github.io/did-method-key/
|
|
157
|
+
*/
|
|
158
|
+
const ED25519_MULTICODEC_PREFIX = new Uint8Array([0xed, 0x01]);
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Generate a did:key from Ed25519 public key bytes
|
|
162
|
+
*
|
|
163
|
+
* Following spec: https://w3c-ccg.github.io/did-method-key/
|
|
164
|
+
* Format: did:key:z<multibase-base58btc(<multicodec-ed25519-pub><publicKey>)>
|
|
165
|
+
*
|
|
166
|
+
* @param publicKeyBytes - Ed25519 public key as Uint8Array (32 bytes)
|
|
167
|
+
* @returns did:key string
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const publicKey = new Uint8Array(32); // 32-byte Ed25519 public key
|
|
172
|
+
* const did = generateDidKeyFromBytes(publicKey);
|
|
173
|
+
* // did = "did:key:z6Mk..."
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export function generateDidKeyFromBytes(publicKeyBytes: Uint8Array): string {
|
|
177
|
+
// Combine multicodec prefix + public key
|
|
178
|
+
const multicodecKey = new Uint8Array(
|
|
179
|
+
ED25519_MULTICODEC_PREFIX.length + publicKeyBytes.length
|
|
180
|
+
);
|
|
181
|
+
multicodecKey.set(ED25519_MULTICODEC_PREFIX);
|
|
182
|
+
multicodecKey.set(publicKeyBytes, ED25519_MULTICODEC_PREFIX.length);
|
|
183
|
+
|
|
184
|
+
// Base58-btc encode and add multibase prefix 'z'
|
|
185
|
+
const base58Encoded = base58Encode(multicodecKey);
|
|
186
|
+
return `did:key:z${base58Encoded}`;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Generate a did:key from base64-encoded Ed25519 public key
|
|
191
|
+
*
|
|
192
|
+
* Convenience wrapper around generateDidKeyFromBytes for base64-encoded keys.
|
|
193
|
+
*
|
|
194
|
+
* @param publicKeyBase64 - Ed25519 public key as base64 string
|
|
195
|
+
* @returns did:key string
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```typescript
|
|
199
|
+
* const publicKeyBase64 = "...base64 encoded key...";
|
|
200
|
+
* const did = generateDidKeyFromBase64(publicKeyBase64);
|
|
201
|
+
* // did = "did:key:z6Mk..."
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export function generateDidKeyFromBase64(publicKeyBase64: string): string {
|
|
205
|
+
// Decode base64 to bytes
|
|
206
|
+
const publicKeyBytes = Uint8Array.from(atob(publicKeyBase64), (c) =>
|
|
207
|
+
c.charCodeAt(0)
|
|
208
|
+
);
|
|
209
|
+
return generateDidKeyFromBytes(publicKeyBytes);
|
|
210
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ed25519 Key Format Constants
|
|
3
|
+
*
|
|
4
|
+
* DER-encoded headers for Ed25519 key formats per RFC 8410 and RFC 5958.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* DER-encoded PKCS#8 header for Ed25519 private keys (RFC 8410 §7, RFC 5958).
|
|
9
|
+
* Prepended to the 32-byte Ed25519 seed to form a valid PKCS#8 private key.
|
|
10
|
+
*/
|
|
11
|
+
export const ED25519_PKCS8_DER_HEADER = new Uint8Array([
|
|
12
|
+
0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05,
|
|
13
|
+
0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20,
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* DER-encoded SPKI header for Ed25519 public keys (RFC 8410 §4).
|
|
18
|
+
* The 32-byte public key follows after this header.
|
|
19
|
+
*/
|
|
20
|
+
export const ED25519_SPKI_DER_HEADER_LENGTH = 12;
|
|
21
|
+
|
|
22
|
+
/** Ed25519 raw key size in bytes */
|
|
23
|
+
export const ED25519_KEY_SIZE = 32;
|