meshsig 0.5.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 +276 -0
- package/dist/crypto.d.ts +43 -0
- package/dist/crypto.js +108 -0
- package/dist/crypto.js.map +1 -0
- package/dist/dashboard.html +635 -0
- package/dist/demo.d.ts +2 -0
- package/dist/demo.js +107 -0
- package/dist/demo.js.map +1 -0
- package/dist/discovery.d.ts +24 -0
- package/dist/discovery.js +119 -0
- package/dist/discovery.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +459 -0
- package/dist/main.js.map +1 -0
- package/dist/peers.d.ts +35 -0
- package/dist/peers.js +227 -0
- package/dist/peers.js.map +1 -0
- package/dist/registry.d.ts +85 -0
- package/dist/registry.js +311 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +27 -0
- package/dist/server.js +433 -0
- package/dist/server.js.map +1 -0
- package/dist/terminal.d.ts +17 -0
- package/dist/terminal.js +175 -0
- package/dist/terminal.js.map +1 -0
- package/package.json +51 -0
- package/scripts/install.sh +271 -0
- package/scripts/invoke-mesh.sh +190 -0
- package/scripts/register-agent.sh +89 -0
- package/scripts/uninstall.sh +25 -0
- package/scripts/unregister-agent.sh +23 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AgentMesh Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="assets/logo.svg" width="280" alt="MeshSig Logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<img src="https://img.shields.io/badge/Ed25519-Cryptographic_Identity-00d4ff?style=for-the-badge" />
|
|
7
|
+
<img src="https://img.shields.io/badge/W3C-DID_Standard-8b5cf6?style=for-the-badge" />
|
|
8
|
+
<img src="https://img.shields.io/badge/License-MIT-green?style=for-the-badge" />
|
|
9
|
+
<img src="https://img.shields.io/badge/npm-meshsig-f0b429?style=for-the-badge" />
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<h1 align="center">MeshSig</h1>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<strong>Cryptographic security layer for AI agents.</strong><br>
|
|
16
|
+
<em>Identity · Signed Messages · Verified Handshakes · Trust Scoring</em>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
<p align="center">
|
|
20
|
+
<a href="https://meshsig.ai">meshsig.ai</a> ·
|
|
21
|
+
<a href="#cli">CLI</a> ·
|
|
22
|
+
<a href="#dashboard">Dashboard</a> ·
|
|
23
|
+
<a href="#openclaw-integration">OpenClaw</a> ·
|
|
24
|
+
<a href="#api-reference">API</a> ·
|
|
25
|
+
<a href="#audit--compliance">Audit</a>
|
|
26
|
+
</p>
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## What is MeshSig?
|
|
31
|
+
|
|
32
|
+
MeshSig gives every AI agent a **cryptographic identity** and secures every agent-to-agent communication with **Ed25519 digital signatures**.
|
|
33
|
+
|
|
34
|
+
When Agent A sends a task to Agent B, MeshSig:
|
|
35
|
+
- Signs the message with Agent A's private key
|
|
36
|
+
- Verifies the signature mathematically before delivery
|
|
37
|
+
- Logs the interaction with tamper-proof audit trail
|
|
38
|
+
- Updates trust scores based on verified history
|
|
39
|
+
|
|
40
|
+
No one can impersonate an agent. No one can tamper with a message. Every interaction has cryptographic proof.
|
|
41
|
+
|
|
42
|
+
## Quick Start
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
git clone https://github.com/carlostroy/meshsig.git
|
|
46
|
+
cd meshsig
|
|
47
|
+
npm install && npm run build
|
|
48
|
+
node dist/main.js start
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Open `http://localhost:4888` — live security operations dashboard.
|
|
52
|
+
|
|
53
|
+
## CLI
|
|
54
|
+
|
|
55
|
+
MeshSig works as a standalone command-line tool. No server required for signing and verifying.
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Generate your Ed25519 identity
|
|
59
|
+
meshsig init
|
|
60
|
+
# ✓ Identity generated
|
|
61
|
+
# DID: did:msig:3icqQkmJWby4S5rpaSRoCcKvjKWdTvqViyPrCEC7Tek2
|
|
62
|
+
|
|
63
|
+
# Sign a message
|
|
64
|
+
meshsig sign "Deploy the new model to production"
|
|
65
|
+
# ✓ Message signed
|
|
66
|
+
# SIGNATURE: HkyrXOPOXF7v422A4iOcg/qkg...
|
|
67
|
+
|
|
68
|
+
# Verify a signature (with DID or public key)
|
|
69
|
+
meshsig verify "Deploy the new model" "HkyrXO..." "did:msig:3icq..."
|
|
70
|
+
# ✓ SIGNATURE VALID
|
|
71
|
+
|
|
72
|
+
# Show your identity
|
|
73
|
+
meshsig identity
|
|
74
|
+
|
|
75
|
+
# List agents on the mesh
|
|
76
|
+
meshsig agents
|
|
77
|
+
|
|
78
|
+
# Server statistics
|
|
79
|
+
meshsig stats
|
|
80
|
+
|
|
81
|
+
# Export audit log
|
|
82
|
+
meshsig audit --json > report.json
|
|
83
|
+
|
|
84
|
+
# Start the server
|
|
85
|
+
meshsig start --port 4888
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
All commands support `--json` for piping and automation.
|
|
89
|
+
|
|
90
|
+
## Dashboard
|
|
91
|
+
|
|
92
|
+
Real-time security operations center showing agents, connections, trust scores, and signed messages flowing through the network.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
meshsig start --port 4888
|
|
96
|
+
# Open http://localhost:4888
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Features:
|
|
100
|
+
- Live network graph with D3.js force simulation
|
|
101
|
+
- Flowing particles on connections between agents
|
|
102
|
+
- Sound notifications on message signing
|
|
103
|
+
- Agent stats: trust scores, interactions, capabilities
|
|
104
|
+
- Local and remote agent distinction
|
|
105
|
+
- Event feed with signature verification status
|
|
106
|
+
|
|
107
|
+
## Audit & Compliance
|
|
108
|
+
|
|
109
|
+
Every signed message is logged with cryptographic proof. Export the complete audit trail for compliance.
|
|
110
|
+
|
|
111
|
+
**API endpoint:**
|
|
112
|
+
```bash
|
|
113
|
+
curl http://localhost:4888/audit/export
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
Returns JSON with:
|
|
117
|
+
- Summary (total agents, messages, verified/failed counts, average trust)
|
|
118
|
+
- All agents with DIDs, public keys, trust scores
|
|
119
|
+
- All connections with handshake proof
|
|
120
|
+
- All messages with signatures and verification status
|
|
121
|
+
|
|
122
|
+
**CLI:**
|
|
123
|
+
```bash
|
|
124
|
+
meshsig audit --json > audit-2026-03.json
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Public Signature Verifier
|
|
128
|
+
|
|
129
|
+
Anyone can verify a signature in the browser — no account, no install needed.
|
|
130
|
+
|
|
131
|
+
Open `http://localhost:4888/verify`, paste a message, signature, and public key or DID. One click verification.
|
|
132
|
+
|
|
133
|
+
**API:**
|
|
134
|
+
```bash
|
|
135
|
+
curl -X POST http://localhost:4888/verify \
|
|
136
|
+
-H 'Content-Type: application/json' \
|
|
137
|
+
-d '{"message":"hello","signature":"base64...","did":"did:msig:..."}'
|
|
138
|
+
# {"valid": true, "verifiedAt": "2026-03-13T..."}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## OpenClaw Integration
|
|
142
|
+
|
|
143
|
+
MeshSig integrates natively with [OpenClaw](https://openclaw.com). One install secures all agent-to-agent delegations with cryptographic signatures.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
# With MeshSig running on the same server as OpenClaw:
|
|
147
|
+
bash scripts/install.sh
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The install script automatically:
|
|
151
|
+
1. Discovers all OpenClaw agents on the machine
|
|
152
|
+
2. Generates Ed25519 identity (`did:msig:...`) for each agent
|
|
153
|
+
3. Creates verified connections via cryptographic handshake
|
|
154
|
+
4. Replaces `invoke.sh` with a signed version (original backed up)
|
|
155
|
+
|
|
156
|
+
```
|
|
157
|
+
Before: Agent A → invoke.sh → Agent B (no proof)
|
|
158
|
+
After: Agent A → invoke.sh → [SIGN] → MeshSig → [VERIFY] → Agent B
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Auto-register new agents
|
|
162
|
+
|
|
163
|
+
```bash
|
|
164
|
+
# When a new agent is provisioned:
|
|
165
|
+
bash scripts/register-agent.sh agent-name-here
|
|
166
|
+
|
|
167
|
+
# When an agent is removed:
|
|
168
|
+
bash scripts/unregister-agent.sh agent-name-here
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## How It Works
|
|
172
|
+
|
|
173
|
+
### Identity
|
|
174
|
+
|
|
175
|
+
Every agent receives an Ed25519 keypair and a W3C Decentralized Identifier:
|
|
176
|
+
|
|
177
|
+
```
|
|
178
|
+
did:msig:6QoiRtfC29pfDoDA4um3TMrBpaCq6kr...
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
The DID is derived from the public key. Impossible to forge. Universally verifiable.
|
|
182
|
+
|
|
183
|
+
### Signed Messages
|
|
184
|
+
|
|
185
|
+
Every message carries a digital signature:
|
|
186
|
+
|
|
187
|
+
```json
|
|
188
|
+
{
|
|
189
|
+
"from": "did:msig:6Qoi...",
|
|
190
|
+
"to": "did:msig:8GkC...",
|
|
191
|
+
"message": "Analyze the Q1 sales report",
|
|
192
|
+
"signature": "LsBbF/FRgaacn1jIMBwK6hxr22jCT...",
|
|
193
|
+
"verified": true
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Trust Scoring
|
|
198
|
+
|
|
199
|
+
Trust is earned, not declared:
|
|
200
|
+
- Every verified message: trust increases
|
|
201
|
+
- Every failed verification: trust decreases
|
|
202
|
+
- Based on real interactions, not self-assessment
|
|
203
|
+
|
|
204
|
+
### Multi-Server Networking
|
|
205
|
+
|
|
206
|
+
Connect MeshSig instances across servers:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# Server 1
|
|
210
|
+
meshsig start --port 4888
|
|
211
|
+
|
|
212
|
+
# Server 2 — connects to Server 1, agents sync automatically
|
|
213
|
+
meshsig start --port 4888 --peer ws://server1:4888
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Remote agents appear on the dashboard with origin labels.
|
|
217
|
+
|
|
218
|
+
## API Reference
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
GET / Live dashboard (Security Operations Center)
|
|
222
|
+
GET /health Server status
|
|
223
|
+
GET /stats Network statistics
|
|
224
|
+
GET /snapshot Full network state
|
|
225
|
+
GET /verify Public signature verifier (browser)
|
|
226
|
+
POST /verify Verify a signature (API)
|
|
227
|
+
GET /audit/export Compliance audit report (JSON)
|
|
228
|
+
|
|
229
|
+
POST /agents/register Register agent → returns Ed25519 keypair + DID
|
|
230
|
+
GET /agents List all agents with trust scores
|
|
231
|
+
GET /agents/:did Get specific agent
|
|
232
|
+
|
|
233
|
+
POST /discover Find agents by capability
|
|
234
|
+
POST /discover/network Find across connected peers
|
|
235
|
+
|
|
236
|
+
POST /messages/send Sign + verify + log a message
|
|
237
|
+
POST /messages/verify Verify a message signature
|
|
238
|
+
|
|
239
|
+
POST /handshake Cryptographic handshake between agents
|
|
240
|
+
GET /connections List verified connections
|
|
241
|
+
GET /messages Recent signed messages
|
|
242
|
+
|
|
243
|
+
GET /peers Connected MeshSig instances
|
|
244
|
+
POST /peers/connect Connect to another instance
|
|
245
|
+
|
|
246
|
+
WS ws://host:port Live event stream
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
## Security
|
|
250
|
+
|
|
251
|
+
| Layer | Implementation |
|
|
252
|
+
|-------|---------------|
|
|
253
|
+
| Signatures | Ed25519 — same as SSH, Signal, WireGuard, TLS 1.3 |
|
|
254
|
+
| Identity | W3C DID standard (`did:msig:`) |
|
|
255
|
+
| Handshake | Mutual challenge-response with nonce and timestamp |
|
|
256
|
+
| Storage | Local SQLite — no cloud dependency |
|
|
257
|
+
| Audit | Tamper-evident log with cryptographic hashes |
|
|
258
|
+
|
|
259
|
+
See [docs/SECURITY.md](docs/SECURITY.md) for the full security whitepaper.
|
|
260
|
+
|
|
261
|
+
## Requirements
|
|
262
|
+
|
|
263
|
+
- Node.js ≥ 18
|
|
264
|
+
|
|
265
|
+
No database to configure. No cloud services. No API keys. Install, start, secure.
|
|
266
|
+
|
|
267
|
+
## License
|
|
268
|
+
|
|
269
|
+
MIT
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
<p align="center">
|
|
274
|
+
<strong>MeshSig</strong> — Cryptographic security layer for AI agents.<br>
|
|
275
|
+
<a href="https://meshsig.ai">meshsig.ai</a> · <a href="https://github.com/carlostroy/meshsig">GitHub</a>
|
|
276
|
+
</p>
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface AgentIdentity {
|
|
2
|
+
did: string;
|
|
3
|
+
publicKey: string;
|
|
4
|
+
privateKey: string;
|
|
5
|
+
createdAt: string;
|
|
6
|
+
}
|
|
7
|
+
export interface Capability {
|
|
8
|
+
type: string;
|
|
9
|
+
confidence?: number;
|
|
10
|
+
}
|
|
11
|
+
export type PermissionScope = 'read:capabilities' | 'send:request' | 'send:broadcast' | 'execute:task' | 'read:status' | 'write:shared_state';
|
|
12
|
+
export interface HandshakeRequest {
|
|
13
|
+
fromDid: string;
|
|
14
|
+
toDid: string;
|
|
15
|
+
nonce: string;
|
|
16
|
+
timestamp: string;
|
|
17
|
+
signature: string;
|
|
18
|
+
requestedPermissions: PermissionScope[];
|
|
19
|
+
}
|
|
20
|
+
export interface HandshakeResponse {
|
|
21
|
+
accepted: boolean;
|
|
22
|
+
nonce: string;
|
|
23
|
+
signature: string;
|
|
24
|
+
grantedPermissions: PermissionScope[];
|
|
25
|
+
channelId: string;
|
|
26
|
+
}
|
|
27
|
+
export declare class MeshError extends Error {
|
|
28
|
+
code: string;
|
|
29
|
+
statusCode: number;
|
|
30
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
31
|
+
}
|
|
32
|
+
export declare function generateIdentity(): Promise<AgentIdentity>;
|
|
33
|
+
export declare function didToPublicKey(did: string): Uint8Array;
|
|
34
|
+
export declare function isValidDid(did: string): boolean;
|
|
35
|
+
export declare function sign(message: string, privateKeyBase64: string): Promise<string>;
|
|
36
|
+
export declare function verify(message: string, signature: string, publicKeyBase64: string): Promise<boolean>;
|
|
37
|
+
export declare function verifyWithDid(message: string, signature: string, did: string): Promise<boolean>;
|
|
38
|
+
export declare function hashPayload(payload: unknown): string;
|
|
39
|
+
export declare function generateNonce(): string;
|
|
40
|
+
export declare function createHandshakeRequest(fromDid: string, toDid: string, privateKey: string, permissions: PermissionScope[]): Promise<HandshakeRequest>;
|
|
41
|
+
export declare function verifyHandshakeRequest(req: HandshakeRequest, publicKey: string): Promise<boolean>;
|
|
42
|
+
export declare function createHandshakeResponse(req: HandshakeRequest, responderDid: string, privateKey: string, accepted: boolean, permissions: PermissionScope[], channelId: string): Promise<HandshakeResponse>;
|
|
43
|
+
export declare function verifyHandshakeResponse(res: HandshakeResponse, req: HandshakeRequest, publicKey: string): Promise<boolean>;
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// MeshSig — Crypto Layer
|
|
3
|
+
// Ed25519 identity, signing, verification, hashing.
|
|
4
|
+
// ============================================================================
|
|
5
|
+
import * as ed from '@noble/ed25519';
|
|
6
|
+
import { sha512 } from '@noble/hashes/sha512';
|
|
7
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
8
|
+
import bs58 from 'bs58';
|
|
9
|
+
ed.etc.sha512Sync = (...m) => sha512(ed.etc.concatBytes(...m));
|
|
10
|
+
const DID_PREFIX = 'did:msig:';
|
|
11
|
+
export class MeshError extends Error {
|
|
12
|
+
code;
|
|
13
|
+
statusCode;
|
|
14
|
+
constructor(message, code, statusCode = 500) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.statusCode = statusCode;
|
|
18
|
+
this.name = 'MeshError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
// -- Identity ----------------------------------------------------------------
|
|
22
|
+
export async function generateIdentity() {
|
|
23
|
+
const priv = ed.utils.randomPrivateKey();
|
|
24
|
+
const pub = await ed.getPublicKeyAsync(priv);
|
|
25
|
+
return {
|
|
26
|
+
did: `${DID_PREFIX}${bs58.encode(pub)}`,
|
|
27
|
+
publicKey: Buffer.from(pub).toString('base64'),
|
|
28
|
+
privateKey: Buffer.from(priv).toString('base64'),
|
|
29
|
+
createdAt: new Date().toISOString(),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export function didToPublicKey(did) {
|
|
33
|
+
if (!did.startsWith(DID_PREFIX))
|
|
34
|
+
throw new MeshError(`Invalid DID: ${did}`, 'INVALID_DID', 400);
|
|
35
|
+
const bytes = bs58.decode(did.slice(DID_PREFIX.length));
|
|
36
|
+
if (bytes.length !== 32)
|
|
37
|
+
throw new MeshError('Invalid DID key length', 'INVALID_DID', 400);
|
|
38
|
+
return bytes;
|
|
39
|
+
}
|
|
40
|
+
export function isValidDid(did) {
|
|
41
|
+
try {
|
|
42
|
+
didToPublicKey(did);
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// -- Signing -----------------------------------------------------------------
|
|
50
|
+
export async function sign(message, privateKeyBase64) {
|
|
51
|
+
const sig = await ed.signAsync(new TextEncoder().encode(message), Buffer.from(privateKeyBase64, 'base64'));
|
|
52
|
+
return Buffer.from(sig).toString('base64');
|
|
53
|
+
}
|
|
54
|
+
export async function verify(message, signature, publicKeyBase64) {
|
|
55
|
+
try {
|
|
56
|
+
return await ed.verifyAsync(Buffer.from(signature, 'base64'), new TextEncoder().encode(message), Buffer.from(publicKeyBase64, 'base64'));
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export async function verifyWithDid(message, signature, did) {
|
|
63
|
+
return verify(message, signature, Buffer.from(didToPublicKey(did)).toString('base64'));
|
|
64
|
+
}
|
|
65
|
+
export function hashPayload(payload) {
|
|
66
|
+
return Buffer.from(sha256(new TextEncoder().encode(JSON.stringify(payload)))).toString('hex');
|
|
67
|
+
}
|
|
68
|
+
export function generateNonce() {
|
|
69
|
+
return Buffer.from(ed.utils.randomPrivateKey()).toString('base64');
|
|
70
|
+
}
|
|
71
|
+
// -- Handshake ---------------------------------------------------------------
|
|
72
|
+
export async function createHandshakeRequest(fromDid, toDid, privateKey, permissions) {
|
|
73
|
+
const nonce = generateNonce();
|
|
74
|
+
const timestamp = new Date().toISOString();
|
|
75
|
+
return {
|
|
76
|
+
fromDid, toDid, nonce, timestamp,
|
|
77
|
+
signature: await sign(`${nonce}${toDid}${timestamp}`, privateKey),
|
|
78
|
+
requestedPermissions: permissions,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export async function verifyHandshakeRequest(req, publicKey) {
|
|
82
|
+
const age = Date.now() - new Date(req.timestamp).getTime();
|
|
83
|
+
if (age > 60_000)
|
|
84
|
+
throw new MeshError('Handshake expired', 'HANDSHAKE_EXPIRED', 400);
|
|
85
|
+
if (age < -5_000)
|
|
86
|
+
throw new MeshError('Handshake from future', 'HANDSHAKE_EXPIRED', 400);
|
|
87
|
+
const valid = await verify(`${req.nonce}${req.toDid}${req.timestamp}`, req.signature, publicKey);
|
|
88
|
+
if (!valid)
|
|
89
|
+
throw new MeshError('Invalid signature', 'INVALID_SIGNATURE', 401);
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
export async function createHandshakeResponse(req, responderDid, privateKey, accepted, permissions, channelId) {
|
|
93
|
+
const nonce = generateNonce();
|
|
94
|
+
return {
|
|
95
|
+
accepted, nonce, channelId: accepted ? channelId : '',
|
|
96
|
+
signature: await sign(`${req.nonce}${req.fromDid}${nonce}`, privateKey),
|
|
97
|
+
grantedPermissions: accepted ? permissions : [],
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
export async function verifyHandshakeResponse(res, req, publicKey) {
|
|
101
|
+
if (!res.accepted)
|
|
102
|
+
return true;
|
|
103
|
+
const valid = await verify(`${req.nonce}${req.fromDid}${res.nonce}`, res.signature, publicKey);
|
|
104
|
+
if (!valid)
|
|
105
|
+
throw new MeshError('Invalid response signature', 'INVALID_SIGNATURE', 401);
|
|
106
|
+
return true;
|
|
107
|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