@rohan-protocol/mcp 0.7.0 → 0.8.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 +44 -12
- package/dist/firewall.d.ts.map +1 -1
- package/dist/firewall.js +3 -1
- package/dist/firewall.js.map +1 -1
- package/dist/gateway.d.ts +26 -0
- package/dist/gateway.d.ts.map +1 -1
- package/dist/gateway.js +10 -1
- package/dist/gateway.js.map +1 -1
- package/dist/identity/handshake.d.ts +42 -0
- package/dist/identity/handshake.d.ts.map +1 -0
- package/dist/identity/handshake.js +74 -0
- package/dist/identity/handshake.js.map +1 -0
- package/dist/identity/keystore.d.ts +27 -0
- package/dist/identity/keystore.d.ts.map +1 -0
- package/dist/identity/keystore.js +336 -0
- package/dist/identity/keystore.js.map +1 -0
- package/dist/identity.d.ts +3 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +38 -0
- package/dist/identity.js.map +1 -0
- package/dist/server.d.ts +15 -30
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +69 -45
- package/dist/server.js.map +1 -1
- package/package.json +8 -4
- package/server.json +41 -25
- package/src/firewall.ts +5 -1
- package/src/gateway.ts +11 -1
- package/src/identity/handshake.ts +82 -0
- package/src/identity/keystore.ts +385 -0
- package/src/server.ts +81 -48
- package/tests/firewall.test.js +9 -0
- package/tests/gateway.test.js +27 -0
- package/tests/identity.test.js +170 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import test from 'node:test';
|
|
6
|
+
import {
|
|
7
|
+
publicKeyFromDidKey,
|
|
8
|
+
verifyEd25519Signature,
|
|
9
|
+
} from '@rohan-protocol/sdk';
|
|
10
|
+
import {
|
|
11
|
+
loadKeystoreFile,
|
|
12
|
+
resolveAgentIdentity,
|
|
13
|
+
saveKeystoreFile,
|
|
14
|
+
} from '../dist/identity/keystore.js';
|
|
15
|
+
import {
|
|
16
|
+
resolveHandshakeAgentId,
|
|
17
|
+
ROHAN_HANDSHAKE_TOOL_SCHEMA,
|
|
18
|
+
validateActionSummary,
|
|
19
|
+
validateHandshakeArguments,
|
|
20
|
+
} from '../dist/identity/handshake.js';
|
|
21
|
+
|
|
22
|
+
const TEST_SEED_HEX = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
|
|
23
|
+
|
|
24
|
+
function createTempKeystorePath() {
|
|
25
|
+
const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'rohan-identity-'));
|
|
26
|
+
return {
|
|
27
|
+
directory,
|
|
28
|
+
filePath: path.join(directory, 'agent-identity.json'),
|
|
29
|
+
cleanup: () => fs.rmSync(directory, { recursive: true, force: true }),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
test('auto-provisions a persistent identity and reloads the same signer', async (t) => {
|
|
34
|
+
const temp = createTempKeystorePath();
|
|
35
|
+
t.after(temp.cleanup);
|
|
36
|
+
|
|
37
|
+
const created = await resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath });
|
|
38
|
+
const loaded = await resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath });
|
|
39
|
+
|
|
40
|
+
assert.equal(created.source, 'auto-provisioned');
|
|
41
|
+
assert.equal(loaded.source, 'filesystem');
|
|
42
|
+
assert.equal(loaded.did, created.did);
|
|
43
|
+
assert.equal(fs.statSync(temp.directory).mode & 0o777, 0o700);
|
|
44
|
+
assert.equal(fs.statSync(temp.filePath).mode & 0o777, 0o600);
|
|
45
|
+
assert.equal(fs.statSync(temp.filePath).nlink, 1);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('concurrent first-run provisioning never overwrites the winning identity', async (t) => {
|
|
49
|
+
const temp = createTempKeystorePath();
|
|
50
|
+
t.after(temp.cleanup);
|
|
51
|
+
|
|
52
|
+
const [first, second] = await Promise.all([
|
|
53
|
+
resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath }),
|
|
54
|
+
resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath }),
|
|
55
|
+
]);
|
|
56
|
+
const restored = await resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath });
|
|
57
|
+
|
|
58
|
+
assert.equal(first.did, second.did);
|
|
59
|
+
assert.equal(restored.did, first.did);
|
|
60
|
+
assert.equal(fs.statSync(temp.filePath).mode & 0o777, 0o600);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test('provisioned keystore permissions stay strict under a restrictive umask', async (t) => {
|
|
64
|
+
const temp = createTempKeystorePath();
|
|
65
|
+
t.after(temp.cleanup);
|
|
66
|
+
const previousUmask = process.umask(0o777);
|
|
67
|
+
try {
|
|
68
|
+
await resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath });
|
|
69
|
+
} finally {
|
|
70
|
+
process.umask(previousUmask);
|
|
71
|
+
}
|
|
72
|
+
assert.equal(fs.statSync(temp.directory).mode & 0o777, 0o700);
|
|
73
|
+
assert.equal(fs.statSync(temp.filePath).mode & 0o777, 0o600);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test('environment seed takes precedence and malformed explicit seed fails closed', async (t) => {
|
|
77
|
+
const temp = createTempKeystorePath();
|
|
78
|
+
t.after(temp.cleanup);
|
|
79
|
+
fs.writeFileSync(temp.filePath, '{ broken', { mode: 0o600 });
|
|
80
|
+
|
|
81
|
+
const fromEnv = await resolveAgentIdentity({ privateKeyHex: TEST_SEED_HEX, filePath: temp.filePath });
|
|
82
|
+
const message = new TextEncoder().encode('Rohan Protocol V-08 Verification');
|
|
83
|
+
const signature = await fromEnv.signer.sign(message);
|
|
84
|
+
const publicKey = publicKeyFromDidKey(fromEnv.did);
|
|
85
|
+
assert.equal(fromEnv.source, 'env');
|
|
86
|
+
assert.equal(await verifyEd25519Signature(publicKey, signature, message), true);
|
|
87
|
+
|
|
88
|
+
await assert.rejects(
|
|
89
|
+
resolveAgentIdentity({ privateKeyHex: 'malformed', filePath: temp.filePath }),
|
|
90
|
+
/ROHAN_AGENT_PRIVATE_KEY is set/
|
|
91
|
+
);
|
|
92
|
+
assert.equal(fs.readFileSync(temp.filePath, 'utf8'), '{ broken');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('refuses to replace a corrupted or mismatched keystore', async (t) => {
|
|
96
|
+
const temp = createTempKeystorePath();
|
|
97
|
+
t.after(temp.cleanup);
|
|
98
|
+
fs.writeFileSync(temp.filePath, '{ broken', { mode: 0o600 });
|
|
99
|
+
|
|
100
|
+
await assert.rejects(
|
|
101
|
+
resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath }),
|
|
102
|
+
/CORRUPTED_KEYSTORE/
|
|
103
|
+
);
|
|
104
|
+
assert.equal(fs.readFileSync(temp.filePath, 'utf8'), '{ broken');
|
|
105
|
+
|
|
106
|
+
fs.unlinkSync(temp.filePath);
|
|
107
|
+
const validIdentity = await resolveAgentIdentity({ privateKeyHex: TEST_SEED_HEX, filePath: temp.filePath });
|
|
108
|
+
fs.writeFileSync(temp.filePath, JSON.stringify({
|
|
109
|
+
did: `${validIdentity.did}tampered`,
|
|
110
|
+
privateKeyHex: TEST_SEED_HEX,
|
|
111
|
+
createdAt: Date.now(),
|
|
112
|
+
}), { mode: 0o600 });
|
|
113
|
+
await assert.rejects(loadKeystoreFile(temp.filePath), /Stored DID does not match/);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test('rejects symlinked keystore paths', async (t) => {
|
|
117
|
+
if (process.platform === 'win32') t.skip('Symlink behavior is platform-dependent on Windows.');
|
|
118
|
+
const temp = createTempKeystorePath();
|
|
119
|
+
t.after(temp.cleanup);
|
|
120
|
+
const target = path.join(temp.directory, 'target.json');
|
|
121
|
+
const link = path.join(temp.directory, 'agent-identity.json');
|
|
122
|
+
fs.writeFileSync(target, '{}', { mode: 0o600 });
|
|
123
|
+
fs.symlinkSync(target, link);
|
|
124
|
+
|
|
125
|
+
await assert.rejects(loadKeystoreFile(link), /symbolic link|ELOOP/);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test('requires actionSummary and resolves optional agentId to the signer DID', async () => {
|
|
129
|
+
const temp = createTempKeystorePath();
|
|
130
|
+
try {
|
|
131
|
+
const { signer } = await resolveAgentIdentity({ privateKeyHex: TEST_SEED_HEX, filePath: temp.filePath });
|
|
132
|
+
assert.deepEqual(ROHAN_HANDSHAKE_TOOL_SCHEMA.inputSchema.required, ['actionSummary']);
|
|
133
|
+
assert.equal(ROHAN_HANDSHAKE_TOOL_SCHEMA.inputSchema.required.includes('agentId'), false);
|
|
134
|
+
assert.equal(ROHAN_HANDSHAKE_TOOL_SCHEMA.inputSchema.properties.intent.deprecated, true);
|
|
135
|
+
assert.equal(validateActionSummary(' '), 'INVALID_ARGUMENTS: actionSummary is required and cannot be empty.');
|
|
136
|
+
assert.equal(validateActionSummary(undefined), 'INVALID_ARGUMENTS: actionSummary is required and cannot be empty.');
|
|
137
|
+
assert.equal(validateActionSummary('Review settlement'), undefined);
|
|
138
|
+
assert.match(
|
|
139
|
+
validateHandshakeArguments({ actionSummary: 'review', privateKey: TEST_SEED_HEX }),
|
|
140
|
+
/unsupported properties/
|
|
141
|
+
);
|
|
142
|
+
assert.match(
|
|
143
|
+
validateHandshakeArguments({ intent: 'legacy-only' }),
|
|
144
|
+
/actionSummary is required/
|
|
145
|
+
);
|
|
146
|
+
assert.match(
|
|
147
|
+
validateHandshakeArguments({ actionSummary: 'review', intent: {} }),
|
|
148
|
+
/Deprecated intent must be a string/
|
|
149
|
+
);
|
|
150
|
+
assert.deepEqual(resolveHandshakeAgentId(undefined, signer), { agentId: signer.did });
|
|
151
|
+
assert.deepEqual(resolveHandshakeAgentId(signer.did, signer), { agentId: signer.did });
|
|
152
|
+
assert.match(resolveHandshakeAgentId('did:key:other', signer).error, /^IDENTITY_MISMATCH:/);
|
|
153
|
+
assert.match(resolveHandshakeAgentId(undefined).error, /^MISSING_AGENT_ID:/);
|
|
154
|
+
} finally {
|
|
155
|
+
temp.cleanup();
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('refuses to overwrite an existing keystore when saving', async (t) => {
|
|
160
|
+
const temp = createTempKeystorePath();
|
|
161
|
+
t.after(temp.cleanup);
|
|
162
|
+
|
|
163
|
+
await resolveAgentIdentity({ privateKeyHex: '', filePath: temp.filePath });
|
|
164
|
+
const data = JSON.parse(fs.readFileSync(temp.filePath, 'utf8'));
|
|
165
|
+
|
|
166
|
+
await assert.rejects(
|
|
167
|
+
saveKeystoreFile(temp.filePath, data),
|
|
168
|
+
/already exists/
|
|
169
|
+
);
|
|
170
|
+
});
|