@me2em/core 0.4.3-alpha.1 → 0.4.4-alpha.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/.github/workflows/ci.yml +0 -0
- package/CODE_OF_CONDUCT.md +73 -0
- package/CONTRIBUTING.md +145 -0
- package/GOVERNANCE.md +132 -0
- package/README.md +373 -159
- package/SECURITY.md +70 -0
- package/package.json +12 -36
- package/packages/core/README.md +656 -0
- package/packages/core/package.json +44 -0
- package/{src → packages/core/src}/crypto/ed25519.ts +4 -0
- package/{src → packages/core/src}/handle.ts +42 -6
- package/{src → packages/core/src}/identity.ts +33 -0
- package/packages/core/src/index.ts +35 -0
- package/packages/core/src/seed.ts +138 -0
- package/packages/core/src/session.ts +47 -0
- package/packages/core/test/derivation.spec.ts +117 -0
- package/packages/core/tsconfig.json +9 -0
- package/pnpm-workspace.yaml +2 -0
- package/specs/core.md +44 -0
- package/specs/openapi/server-ref.yaml +10 -0
- package/specs/test-vectors.json +13 -0
- package/tsconfig.base.json +20 -0
- package/typedoc.json +18 -0
- package/dist/crypto/ed25519.d.ts +0 -8
- package/dist/crypto/ed25519.d.ts.map +0 -1
- package/dist/crypto/ed25519.js +0 -8
- package/dist/crypto/ed25519.js.map +0 -1
- package/dist/crypto/hkdf.d.ts +0 -13
- package/dist/crypto/hkdf.d.ts.map +0 -1
- package/dist/crypto/hkdf.js +0 -16
- package/dist/crypto/hkdf.js.map +0 -1
- package/dist/crypto/index.d.ts +0 -3
- package/dist/crypto/index.d.ts.map +0 -1
- package/dist/crypto/index.js +0 -3
- package/dist/crypto/index.js.map +0 -1
- package/dist/crypto/init.d.ts +0 -9
- package/dist/crypto/init.d.ts.map +0 -1
- package/dist/crypto/init.js +0 -16
- package/dist/crypto/init.js.map +0 -1
- package/dist/handle.d.ts +0 -53
- package/dist/handle.d.ts.map +0 -1
- package/dist/handle.js +0 -79
- package/dist/handle.js.map +0 -1
- package/dist/identity.d.ts +0 -10
- package/dist/identity.d.ts.map +0 -1
- package/dist/identity.js +0 -38
- package/dist/identity.js.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
- package/dist/seed.d.ts +0 -24
- package/dist/seed.d.ts.map +0 -1
- package/dist/seed.js +0 -55
- package/dist/seed.js.map +0 -1
- package/dist/session.d.ts +0 -20
- package/dist/session.d.ts.map +0 -1
- package/dist/session.js +0 -16
- package/dist/session.js.map +0 -1
- package/src/index.ts +0 -15
- package/src/seed.ts +0 -65
- package/src/session.ts +0 -25
- /package/{src → packages/core/src}/crypto/hkdf.ts +0 -0
- /package/{src → packages/core/src}/crypto/index.ts +0 -0
- /package/{src → packages/core/src}/crypto/init.ts +0 -0
package/README.md
CHANGED
|
@@ -1,25 +1,14 @@
|
|
|
1
|
-
# 📦 `@me2em/core` —
|
|
1
|
+
# 📦 `@me2em/core` — Developer Documentation
|
|
2
2
|
|
|
3
|
-
> Core primitives for the Me2em authorization protocol: `Identity`, `Handle`, and
|
|
3
|
+
> Core primitives for the Me2em authorization protocol: `Identity`, `Handle`, and `Session` management with Ed25519 cryptography.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@me2em/core)
|
|
6
|
-
[](https://github.com/me2em-org/me2em-protocol/blob/main/LICENSE)
|
|
7
7
|
[](https://docs.me2em.com)
|
|
8
8
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
## 🎯
|
|
12
|
-
|
|
13
|
-
`@me2em/core` provides the cryptographic foundation for the Me2em protocol — a decentralized multi-context identity system. It enables:
|
|
14
|
-
|
|
15
|
-
- **Hierarchical Deterministic Identities**: One seed → multiple isolated Handles
|
|
16
|
-
- **Zero-Knowledge Password Management**: Deterministic password derivation without storage
|
|
17
|
-
- **Secure Channel Communication**: Encrypted channels between Identity and Handles without key exchange
|
|
18
|
-
- **Stateless Authentication**: Cryptographic proof without server-side session storage
|
|
19
|
-
|
|
20
|
-
---
|
|
21
|
-
|
|
22
|
-
## 📦 Installation
|
|
11
|
+
## 🎯 Quick Start
|
|
23
12
|
|
|
24
13
|
```bash
|
|
25
14
|
npm install @me2em/core
|
|
@@ -29,30 +18,20 @@ pnpm add @me2em/core
|
|
|
29
18
|
yarn add @me2em/core
|
|
30
19
|
```
|
|
31
20
|
|
|
32
|
-
**Dependencies:**
|
|
33
|
-
- `@noble/ed25519` — Ed25519 signatures
|
|
34
|
-
- `@noble/hashes` — HKDF, SHA-256
|
|
35
|
-
|
|
36
|
-
---
|
|
37
|
-
|
|
38
|
-
## 🚀 Quick Start
|
|
39
|
-
|
|
40
|
-
### Basic Usage
|
|
41
|
-
|
|
42
21
|
```typescript
|
|
43
22
|
import { Identity, Handle } from '@me2em/core';
|
|
44
23
|
|
|
45
|
-
// 1. Create
|
|
46
|
-
const seed =
|
|
24
|
+
// 1. Create or restore identity from seed
|
|
25
|
+
const seed = 'your-32-byte-hex-seed-or-24-words';
|
|
47
26
|
const identity = await Identity.fromSeed(seed);
|
|
48
27
|
|
|
49
|
-
// 2. Derive a contextual
|
|
28
|
+
// 2. Derive a contextual handle
|
|
50
29
|
const workHandle = await identity.deriveHandle('work', {
|
|
51
30
|
displayName: 'Alice @ Work',
|
|
52
31
|
avatar: 'https://example.com/avatar.png'
|
|
53
32
|
});
|
|
54
33
|
|
|
55
|
-
// 3. Use
|
|
34
|
+
// 3. Use handle for cryptographic operations
|
|
56
35
|
const message = new TextEncoder().encode('Hello, world!');
|
|
57
36
|
const signature = await workHandle.sign(message);
|
|
58
37
|
const isValid = await Handle.verify(signature, message, workHandle.getPublicKey());
|
|
@@ -61,34 +40,77 @@ console.log('Handle ID:', workHandle.getId()); // base64url(publicKey)
|
|
|
61
40
|
console.log('Signature valid:', isValid); // true
|
|
62
41
|
```
|
|
63
42
|
|
|
64
|
-
|
|
43
|
+
---
|
|
65
44
|
|
|
66
|
-
|
|
45
|
+
## 📚 Table of Contents
|
|
67
46
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
47
|
+
1. [Core Concepts](#-core-concepts)
|
|
48
|
+
2. [API Reference](#-api-reference)
|
|
49
|
+
3. [Use Cases](#-use-cases)
|
|
50
|
+
4. [Cryptographic Details](#-cryptographic-details)
|
|
51
|
+
5. [Integration Guide](#-integration-guide)
|
|
52
|
+
6. [Security Best Practices](#-security-best-practices)
|
|
53
|
+
7. [Testing](#-testing)
|
|
54
|
+
8. [Contributing](#-contributing)
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## 🔑 Core Concepts
|
|
59
|
+
|
|
60
|
+
### Identity — Cryptographic Root
|
|
61
|
+
|
|
62
|
+
`Identity` represents the root cryptographic identity derived from a seed phrase.
|
|
63
|
+
|
|
64
|
+
```
|
|
65
|
+
Seed (32 bytes / 24 words)
|
|
66
|
+
│
|
|
67
|
+
▼
|
|
68
|
+
┌─────────────────┐
|
|
69
|
+
│ Identity │
|
|
70
|
+
│ • Ed25519 key │
|
|
71
|
+
│ • Deterministic │
|
|
72
|
+
│ • Client-only │
|
|
73
|
+
└─────────────────┘
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Key properties:**
|
|
77
|
+
- 🔐 **Never transmitted**: The root private key never leaves the client device
|
|
78
|
+
- 🔄 **Deterministic**: Same seed → same Identity → same derived Handles
|
|
79
|
+
- 🧩 **Stateless**: No server storage required for cryptographic operations
|
|
80
|
+
|
|
81
|
+
### Handle — Contextual Profile
|
|
82
|
+
|
|
83
|
+
`Handle` is a derived Ed25519 keypair representing a specific context (work, personal, IoT device).
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
Identity
|
|
87
|
+
│
|
|
88
|
+
├─ deriveHandle('work') → Handle(@alice_work)
|
|
89
|
+
├─ deriveHandle('private') → Handle(@alice_private)
|
|
90
|
+
└─ deriveHandle('iot-device-001') → Handle(@device_001)
|
|
87
91
|
```
|
|
88
92
|
|
|
89
|
-
**
|
|
90
|
-
|
|
93
|
+
**Key properties:**
|
|
94
|
+
- 🔗 **Cryptographically isolated**: Each Handle has its own keypair; compromise of one does not affect others
|
|
95
|
+
- 🏷️ **Public identifier**: `Handle.getId()` returns `base64url(publicKey)` — safe to share
|
|
96
|
+
- 📦 **Metadata support**: Attach public metadata (displayName, avatar) without revealing Identity
|
|
97
|
+
|
|
98
|
+
### Session — Scoped Access Token
|
|
99
|
+
|
|
100
|
+
`Session` represents a time-limited, scoped authorization token for accessing a specific application.
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
interface SessionOptions {
|
|
104
|
+
audience: string; // e.g., 'https://api.myapp.com'
|
|
105
|
+
scopes: string[]; // e.g., ['read:profile', 'write:messages']
|
|
106
|
+
ttl?: number; // seconds until expiration (default: 3600)
|
|
107
|
+
}
|
|
108
|
+
```
|
|
91
109
|
|
|
110
|
+
**Key properties:**
|
|
111
|
+
- ⏱️ **Short-lived**: Tokens expire automatically; refresh via Handle re-authentication
|
|
112
|
+
- 🔐 **Scoped**: Each token grants only specified permissions
|
|
113
|
+
- 🌐 **Stateless verification**: Servers verify signatures using Handle public key — no database lookup required
|
|
92
114
|
|
|
93
115
|
---
|
|
94
116
|
|
|
@@ -96,12 +118,10 @@ BIP39 produces a 64-byte seed, but Ed25519 requires exactly 32 bytes. The `get32
|
|
|
96
118
|
|
|
97
119
|
### `Identity` Class
|
|
98
120
|
|
|
99
|
-
The root cryptographic identity, derived from a seed phrase.
|
|
100
|
-
|
|
101
121
|
```typescript
|
|
102
122
|
class Identity {
|
|
103
|
-
// Create Identity from seed (32-byte Uint8Array)
|
|
104
|
-
static fromSeed(seed: Uint8Array): Promise<Identity>;
|
|
123
|
+
// Create Identity from seed (32-byte Uint8Array or hex string)
|
|
124
|
+
static fromSeed(seed: Uint8Array | string): Promise<Identity>;
|
|
105
125
|
|
|
106
126
|
// Derive a new Handle with optional metadata
|
|
107
127
|
deriveHandle(name: string, metadata?: HandleMetadata): Promise<Handle>;
|
|
@@ -113,66 +133,36 @@ class Identity {
|
|
|
113
133
|
|
|
114
134
|
#### `Identity.fromSeed(seed)`
|
|
115
135
|
|
|
116
|
-
Creates an Identity from a 32-byte seed.
|
|
117
|
-
|
|
118
136
|
```typescript
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
137
|
+
// From hex string (64 chars = 32 bytes)
|
|
138
|
+
const identity = await Identity.fromSeed('a1b2c3...');
|
|
139
|
+
|
|
140
|
+
// From Uint8Array
|
|
141
|
+
const seedBytes = new Uint8Array(32).fill(42);
|
|
142
|
+
const identity = await Identity.fromSeed(seedBytes);
|
|
122
143
|
```
|
|
123
144
|
|
|
124
|
-
**Security
|
|
145
|
+
⚠️ **Security**: The seed must be kept secret. Never log, transmit, or store it in plaintext.
|
|
125
146
|
|
|
126
147
|
#### `Identity.deriveHandle(name, metadata?)`
|
|
127
148
|
|
|
128
|
-
Derives a new Handle deterministically from the Identity.
|
|
129
|
-
|
|
130
149
|
```typescript
|
|
131
|
-
const handle = await identity.deriveHandle('
|
|
132
|
-
displayName: 'Alice
|
|
133
|
-
avatar: 'https://example.com/alice.png'
|
|
150
|
+
const handle = await identity.deriveHandle('work', {
|
|
151
|
+
displayName: 'Alice Developer',
|
|
152
|
+
avatar: 'https://example.com/alice.png',
|
|
153
|
+
org: 'Acme Corp'
|
|
134
154
|
});
|
|
135
155
|
```
|
|
136
156
|
|
|
137
|
-
|
|
138
|
-
- `
|
|
139
|
-
- `metadata`: Optional public data attached to the Handle
|
|
140
|
-
|
|
141
|
-
**Returns:** A `Handle` instance with its own keypair, derived deterministically from the Identity.
|
|
157
|
+
- `name`: Unique identifier for this Handle within the Identity (case-insensitive, trimmed)
|
|
158
|
+
- `metadata`: Arbitrary public data attached to the Handle (visible to servers/other users)
|
|
142
159
|
|
|
143
|
-
|
|
144
|
-
- 🔁 **Deterministic**: Same seed + same name → same Handle (always)
|
|
145
|
-
- 🔗 **Isolated**: Each Handle has its own keypair; compromise of one does not affect others
|
|
146
|
-
- 🔒 **One-way**: Cannot derive Identity key from Handle key
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
|
-
### Seed Utilities
|
|
151
|
-
|
|
152
|
-
Built-in helpers for human-friendly key generation and validation.
|
|
153
|
-
|
|
154
|
-
```typescript
|
|
155
|
-
// Generate a new phrase (128 bits = 12 words, 256 bits = 24 words)
|
|
156
|
-
function generateSeedPhrase(strength: 128 | 256 = 128): string[];
|
|
157
|
-
|
|
158
|
-
// Normalize input (handles extra spaces, lowercase)
|
|
159
|
-
function normalizeSeedPhrase(input: string | string[]): string[];
|
|
160
|
-
|
|
161
|
-
// Validate word count, wordlist, and BIP39 checksum
|
|
162
|
-
function validateSeedPhrase(words: string[]): { isValid: boolean; error?: string };
|
|
163
|
-
|
|
164
|
-
// Convert validated mnemonic to a secure 32-byte Uint8Array for Ed25519
|
|
165
|
-
async function get32ByteSeedFromMnemonic(phrase: string | string[]): Promise<Uint8Array>;
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
✅ **Benefit**: Developers don't need to manually manage `@scure/bip39` imports or remember to hash the 64-byte output. Everything is handled securely within the protocol.
|
|
160
|
+
Returns a `Handle` instance with its own keypair, derived deterministically from the Identity.
|
|
169
161
|
|
|
170
162
|
---
|
|
171
163
|
|
|
172
164
|
### `Handle` Class
|
|
173
165
|
|
|
174
|
-
A derived Ed25519 keypair representing a specific context (work, personal, IoT device).
|
|
175
|
-
|
|
176
166
|
```typescript
|
|
177
167
|
class Handle {
|
|
178
168
|
// Get public identifier (safe to share)
|
|
@@ -187,7 +177,7 @@ class Handle {
|
|
|
187
177
|
// Deterministically derive a password/secret for a specific context
|
|
188
178
|
derivePassword(context: string, length?: number): string;
|
|
189
179
|
|
|
190
|
-
// Derive a symmetric channel key for encrypted communication
|
|
180
|
+
// Derive a symmetric 256-bit channel key for encrypted communication
|
|
191
181
|
deriveChannelKey(context: string): Uint8Array;
|
|
192
182
|
|
|
193
183
|
// Accessors
|
|
@@ -246,7 +236,7 @@ if (!isValid) throw new Error('Invalid signature');
|
|
|
246
236
|
|
|
247
237
|
#### `Handle.derivePassword(context, length?)`
|
|
248
238
|
|
|
249
|
-
|
|
239
|
+
Generates a deterministic, cryptographically strong secret (e.g., a password) for a specific service, without ever exposing the private key.
|
|
250
240
|
|
|
251
241
|
```typescript
|
|
252
242
|
// Derive a password for a specific service
|
|
@@ -257,15 +247,7 @@ const googlePassword = workHandle.derivePassword('google');
|
|
|
257
247
|
const apiKey = workHandle.derivePassword('aws-api', 32);
|
|
258
248
|
```
|
|
259
249
|
|
|
260
|
-
**
|
|
261
|
-
- `context`: A unique identifier for the service (e.g., `'google'`, `'github'`, `'wifi-router'`)
|
|
262
|
-
- `length`: Length of the derived raw bytes (default: 16 bytes = ~22 chars base64url)
|
|
263
|
-
|
|
264
|
-
**Returns:** A URL-safe base64 string suitable for use as a strong password.
|
|
265
|
-
|
|
266
|
-
**Security Benefit:** The `privateKey` remains strictly encapsulated within the `Handle` instance. It is used internally by HKDF-SHA256 and is never returned, logged, or serialized.
|
|
267
|
-
|
|
268
|
-
**Use Case:** Zero-knowledge password management. No database of passwords is required on the server. If a service forces a password change, the user simply derives a new handle (e.g., `'google-v2'`) or adds a version suffix to the context.
|
|
250
|
+
✅ **Security Benefit**: The `privateKey` remains strictly encapsulated within the `Handle` instance. It is used internally by HKDF-SHA256 and is never returned, logged, or serialized.
|
|
269
251
|
|
|
270
252
|
#### `Handle.deriveChannelKey(context)`
|
|
271
253
|
|
|
@@ -282,20 +264,138 @@ const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
|
282
264
|
// Identical key, derived independently
|
|
283
265
|
```
|
|
284
266
|
|
|
285
|
-
**
|
|
286
|
-
|
|
267
|
+
✅ **Security Benefit**: The `privateKey` remains strictly encapsulated within the `Handle` instance. No key exchange protocol (ECDH, etc.) is needed — both parties derive the same key independently from the shared Handle private key.
|
|
268
|
+
|
|
269
|
+
⚠️ **Note**: This method returns raw bytes (Uint8Array), unlike `derivePassword` which returns a base64url string. Use the returned bytes directly with AES-256-GCM via Web Crypto API or similar.
|
|
270
|
+
|
|
271
|
+
---
|
|
272
|
+
|
|
273
|
+
### `Session` Class (Planned)
|
|
287
274
|
|
|
288
|
-
|
|
275
|
+
> ⚠️ Session management is planned for v0.2. Current implementations should use custom token logic with Handle signatures.
|
|
289
276
|
|
|
290
|
-
|
|
277
|
+
```typescript
|
|
278
|
+
// Future API (not yet implemented)
|
|
279
|
+
const session = await handle.requestSession({
|
|
280
|
+
audience: 'https://api.myapp.com',
|
|
281
|
+
scopes: ['read:profile', 'write:messages'],
|
|
282
|
+
ttl: 3600
|
|
283
|
+
});
|
|
291
284
|
|
|
292
|
-
|
|
285
|
+
// Use token
|
|
286
|
+
fetch('https://api.myapp.com/me', {
|
|
287
|
+
headers: { Authorization: `Bearer ${session.token}` }
|
|
288
|
+
});
|
|
289
|
+
```
|
|
293
290
|
|
|
294
291
|
---
|
|
295
292
|
|
|
296
293
|
## 🎯 Use Cases
|
|
297
294
|
|
|
298
|
-
### 1.
|
|
295
|
+
### 1. Multi-Context User Authentication
|
|
296
|
+
|
|
297
|
+
```typescript
|
|
298
|
+
// User logs in with seed
|
|
299
|
+
const identity = await Identity.fromSeed(userSeed);
|
|
300
|
+
|
|
301
|
+
// Derive work handle
|
|
302
|
+
const workHandle = await identity.deriveHandle('work', {
|
|
303
|
+
displayName: 'Alice @ Acme',
|
|
304
|
+
role: 'engineer'
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Authenticate to work app
|
|
308
|
+
const workSig = await workHandle.sign(challenge);
|
|
309
|
+
// Send workHandle.getId() + workSig to https://work.acme.com/auth
|
|
310
|
+
|
|
311
|
+
// Later, derive personal handle for social app
|
|
312
|
+
const personalHandle = await identity.deriveHandle('personal', {
|
|
313
|
+
displayName: 'Alice',
|
|
314
|
+
interests: ['hiking', 'photography']
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// Authenticate to social app with different handle
|
|
318
|
+
const socialSig = await personalHandle.sign(challenge);
|
|
319
|
+
// Send personalHandle.getId() + socialSig to https://social.app/auth
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
✅ **Benefit**: One seed, multiple isolated identities. Apps see only the Handle they interact with.
|
|
323
|
+
|
|
324
|
+
---
|
|
325
|
+
|
|
326
|
+
### 2. IoT Device Hierarchy
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
// Root identity for device fleet
|
|
330
|
+
const fleetIdentity = await Identity.fromSeed(fleetSeed);
|
|
331
|
+
|
|
332
|
+
// Derive handle for gateway
|
|
333
|
+
const gateway = await fleetIdentity.deriveHandle('gateway-001', {
|
|
334
|
+
type: 'gateway',
|
|
335
|
+
location: 'warehouse-a'
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
// Derive child handles for sensors (using name prefix for hierarchy)
|
|
339
|
+
const sensor1 = await fleetIdentity.deriveHandle('gateway-001/sensor-temp', {
|
|
340
|
+
type: 'temperature',
|
|
341
|
+
unit: 'celsius'
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const sensor2 = await fleetIdentity.deriveHandle('gateway-001/sensor-humidity', {
|
|
345
|
+
type: 'humidity',
|
|
346
|
+
unit: 'percent'
|
|
347
|
+
});
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
✅ **Benefit**: Compromise of `sensor-temp` does not expose `gateway-001` or other sensors. Each device has isolated credentials.
|
|
351
|
+
|
|
352
|
+
---
|
|
353
|
+
|
|
354
|
+
### 3. Anonymous Guest Access
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
// Generate ephemeral seed for guest session
|
|
358
|
+
const guestSeed = crypto.getRandomValues(new Uint8Array(32));
|
|
359
|
+
const guestIdentity = await Identity.fromSeed(guestSeed);
|
|
360
|
+
|
|
361
|
+
// Create anonymous handle
|
|
362
|
+
const guestHandle = await guestIdentity.deriveHandle('guest', {
|
|
363
|
+
displayName: 'Anonymous User',
|
|
364
|
+
ephemeral: true
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
// Use for limited-scope access
|
|
368
|
+
const guestSig = await guestHandle.sign(challenge);
|
|
369
|
+
// Send to server with scope: ['read:public-content']
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
✅ **Benefit**: No email, no phone, no tracking. Guest access with cryptographic accountability.
|
|
373
|
+
|
|
374
|
+
---
|
|
375
|
+
|
|
376
|
+
### 4. Cross-App Single Sign-On (SSO)
|
|
377
|
+
|
|
378
|
+
```typescript
|
|
379
|
+
// User authenticates once with Identity
|
|
380
|
+
const identity = await Identity.fromSeed(seed);
|
|
381
|
+
|
|
382
|
+
// App A requests session for 'app-a.example.com'
|
|
383
|
+
const handleA = await identity.deriveHandle('app-a', { app: 'example-a' });
|
|
384
|
+
const sessionA = { /* custom token logic */ };
|
|
385
|
+
|
|
386
|
+
// App B requests session for 'app-b.example.com'
|
|
387
|
+
const handleB = await identity.deriveHandle('app-b', { app: 'example-b' });
|
|
388
|
+
const sessionB = { /* custom token logic */ };
|
|
389
|
+
|
|
390
|
+
// User switches between apps without re-entering seed
|
|
391
|
+
// Each app sees only its Handle, not the others
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
✅ **Benefit**: Seamless SSO with privacy isolation — apps cannot correlate user activity across services.
|
|
395
|
+
|
|
396
|
+
---
|
|
397
|
+
|
|
398
|
+
### 5. Deterministic Password Manager (Access Key Keeper)
|
|
299
399
|
|
|
300
400
|
Instead of storing passwords in a database, derive them deterministically from the Handle. The user only needs to remember their root Seed and the service name.
|
|
301
401
|
|
|
@@ -316,11 +416,9 @@ console.log('Login:', 'alice@example.com');
|
|
|
316
416
|
console.log('Password:', password); // Always the same for this seed + handle + context
|
|
317
417
|
```
|
|
318
418
|
|
|
319
|
-
✅ **Benefit**: Zero-knowledge password management. No database of passwords is required on the server. If a service forces a password change, the user simply derives a new handle (e.g., `'google-v2'`) or adds a version suffix to the context (e.g., `derivePassword('google
|
|
320
|
-
|
|
321
|
-
---
|
|
419
|
+
✅ **Benefit**: Zero-knowledge password management. No database of passwords is required on the server. If a service forces a password change, the user simply derives a new handle (e.g., `'google-v2'`) or adds a version suffix to the context (e.g., `derivePassword('google', 16, 2)`).
|
|
322
420
|
|
|
323
|
-
###
|
|
421
|
+
### 6. IoT Fleet with Encrypted Channels
|
|
324
422
|
|
|
325
423
|
Control a fleet of devices (drones, sensors, robots) with zero-knowledge encrypted communication. The control center derives a channel key for each device, and the device independently derives the same key — no key exchange protocol required.
|
|
326
424
|
|
|
@@ -387,7 +485,7 @@ async function sendTelemetry(telemetry: object) {
|
|
|
387
485
|
|
|
388
486
|
---
|
|
389
487
|
|
|
390
|
-
###
|
|
488
|
+
### 7. Stateless Multi-Device Synchronization
|
|
391
489
|
|
|
392
490
|
A user with the same Identity on multiple devices (phone, laptop, tablet) can derive identical Handles and secrets on each device without any synchronization protocol.
|
|
393
491
|
|
|
@@ -409,7 +507,7 @@ const handleId = messengerHandle.getId(); // Identical handleId!
|
|
|
409
507
|
|
|
410
508
|
---
|
|
411
509
|
|
|
412
|
-
###
|
|
510
|
+
### 8. Break-Glass Recovery and Inheritance
|
|
413
511
|
|
|
414
512
|
A user's entire digital identity can be recovered from a single seed phrase, even years later, on any device, without contacting any service provider.
|
|
415
513
|
|
|
@@ -436,7 +534,7 @@ const messengerChannelKey = messengerHandle.deriveChannelKey('session-2026');
|
|
|
436
534
|
|
|
437
535
|
---
|
|
438
536
|
|
|
439
|
-
###
|
|
537
|
+
### 9. Ephemeral Delegated Access
|
|
440
538
|
|
|
441
539
|
Grant time-limited access to a contractor or temporary service by deriving a Handle with a time-bound name.
|
|
442
540
|
|
|
@@ -459,11 +557,9 @@ const contractorHandle = await identity.deriveHandle('contractor-acme-2026-12-31
|
|
|
459
557
|
|
|
460
558
|
✅ **Benefit**: Clean delegation without polluting the permanent identity. Expired Handles become inert cryptographic artifacts.
|
|
461
559
|
|
|
462
|
-
---
|
|
463
|
-
|
|
464
560
|
## 🔐 Cryptographic Details
|
|
465
561
|
|
|
466
|
-
###
|
|
562
|
+
### Key Derivation Algorithm
|
|
467
563
|
|
|
468
564
|
Handles are derived using **HKDF-SHA256** with domain separation:
|
|
469
565
|
|
|
@@ -483,25 +579,6 @@ HandleId = Base64Url(HandlePublicKey)
|
|
|
483
579
|
- 🔒 **One-way**: Cannot derive Identity key from Handle key
|
|
484
580
|
- 🧩 **Isolated**: Each Handle uses independent Ed25519 keypair
|
|
485
581
|
|
|
486
|
-
### Password Derivation Algorithm
|
|
487
|
-
|
|
488
|
-
Passwords are derived using **HKDF-SHA256** with the Handle's private key as input key material:
|
|
489
|
-
|
|
490
|
-
```
|
|
491
|
-
PasswordBytes = HKDF-SHA256(
|
|
492
|
-
inputKeyMaterial = HandlePrivateKey,
|
|
493
|
-
salt = "me2em/secret/" + lowercase(context),
|
|
494
|
-
info = "me2em/secret/v1",
|
|
495
|
-
length = 16 (default)
|
|
496
|
-
)
|
|
497
|
-
Password = Base64Url(PasswordBytes)
|
|
498
|
-
```
|
|
499
|
-
|
|
500
|
-
**Properties:**
|
|
501
|
-
- 🔁 **Deterministic**: Same Handle + same context → same password
|
|
502
|
-
- 🔐 **Encapsulated**: Private key never leaves the Handle instance
|
|
503
|
-
- 🧩 **Domain-separated**: Different contexts produce different passwords
|
|
504
|
-
|
|
505
582
|
### Channel Key Derivation
|
|
506
583
|
|
|
507
584
|
Channel keys are derived using **HKDF-SHA256** with the Handle's private key as input key material:
|
|
@@ -515,7 +592,7 @@ ChannelKey = HKDF-SHA256(
|
|
|
515
592
|
)
|
|
516
593
|
```
|
|
517
594
|
|
|
518
|
-
**Properties
|
|
595
|
+
**Properties**:
|
|
519
596
|
- 🔁 **Deterministic**: Same Handle + same context → same 32-byte key
|
|
520
597
|
- 🔐 **Encapsulated**: Private key never leaves the Handle instance
|
|
521
598
|
- 🧩 **Domain-separated**: Different contexts produce different keys (e.g., `'telemetry-v1'` vs `'command-v1'`)
|
|
@@ -527,6 +604,91 @@ ChannelKey = HKDF-SHA256(
|
|
|
527
604
|
- **Hash**: SHA-512 (via `@noble/ed25519`)
|
|
528
605
|
- **Encoding**: Raw bytes → base64url for transport
|
|
529
606
|
|
|
607
|
+
### Test Vectors
|
|
608
|
+
|
|
609
|
+
See [`specs/test-vectors.json`](../../specs/test-vectors.json) for canonical derivation examples to ensure cross-implementation compatibility.
|
|
610
|
+
|
|
611
|
+
Example vector:
|
|
612
|
+
```json
|
|
613
|
+
{
|
|
614
|
+
"seed_hex": "2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a2a",
|
|
615
|
+
"handle_name": "work",
|
|
616
|
+
"expected_handle_id": "pK7xJ2mN8vQ3rL5wY9zB1cD4eF6gH8iJ0kL2mN4oP6qR8sT0uV2wX4yZ6aB8cD0",
|
|
617
|
+
"expected_public_key_hex": "a4b5c6d7e8f90123456789abcdef0123456789abcdef0123456789abcdef0123"
|
|
618
|
+
}
|
|
619
|
+
```
|
|
620
|
+
|
|
621
|
+
---
|
|
622
|
+
|
|
623
|
+
## 🔌 Integration Guide
|
|
624
|
+
|
|
625
|
+
### Backend Verification (Node.js Example)
|
|
626
|
+
|
|
627
|
+
```typescript
|
|
628
|
+
import { Handle } from '@me2em/core';
|
|
629
|
+
|
|
630
|
+
// Middleware to verify Handle-signed requests
|
|
631
|
+
export async function me2emAuthMiddleware(req: Request, res: Response, next: NextFunction) {
|
|
632
|
+
const handleId = req.headers['x-handle-id'] as string;
|
|
633
|
+
const signature = req.headers['x-signature'] as string;
|
|
634
|
+
const payload = JSON.stringify(req.body);
|
|
635
|
+
|
|
636
|
+
if (!handleId || !signature) {
|
|
637
|
+
return res.status(401).json({ error: 'Missing authentication headers' });
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
try {
|
|
641
|
+
// Decode base64url to Uint8Array
|
|
642
|
+
const publicKey = base64urlToBytes(handleId);
|
|
643
|
+
const sig = base64urlToBytes(signature);
|
|
644
|
+
const data = new TextEncoder().encode(payload);
|
|
645
|
+
|
|
646
|
+
// Verify signature
|
|
647
|
+
const isValid = await Handle.verify(sig, data, publicKey);
|
|
648
|
+
if (!isValid) throw new Error('Invalid signature');
|
|
649
|
+
|
|
650
|
+
// Attach handle info to request for downstream use
|
|
651
|
+
req.me2em = { handleId, publicKey };
|
|
652
|
+
next();
|
|
653
|
+
} catch (err) {
|
|
654
|
+
res.status(401).json({ error: 'Authentication failed' });
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function base64urlToBytes(str: string): Uint8Array {
|
|
659
|
+
const padding = '='.repeat((4 - (str.length % 4)) % 4);
|
|
660
|
+
const base64 = str.replace(/-/g, '+').replace(/_/g, '/') + padding;
|
|
661
|
+
return Uint8Array.from(atob(base64), c => c.charCodeAt(0));
|
|
662
|
+
}
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
### Frontend Secure Key Handling
|
|
666
|
+
|
|
667
|
+
```typescript
|
|
668
|
+
// Store encrypted seed in IndexedDB (never plaintext)
|
|
669
|
+
import { encryptWithPin } from './crypto/local';
|
|
670
|
+
|
|
671
|
+
async function saveIdentity(seed: string, pin: string) {
|
|
672
|
+
const encrypted = await encryptWithPin(seed, pin);
|
|
673
|
+
await db.identities.add({
|
|
674
|
+
id: 'current',
|
|
675
|
+
encryptedSeed: encrypted,
|
|
676
|
+
// seed is NEVER stored in plaintext
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// Load and decrypt on login
|
|
681
|
+
async function loadIdentity(pin: string) {
|
|
682
|
+
const record = await db.identities.get('current');
|
|
683
|
+
if (!record) throw new Error('No identity found');
|
|
684
|
+
|
|
685
|
+
const seed = await decryptWithPin(record.encryptedSeed, pin);
|
|
686
|
+
return Identity.fromSeed(seed);
|
|
687
|
+
}
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
✅ **Best practice**: Use Web Crypto API or secure enclave for PIN/biometric decryption.
|
|
691
|
+
|
|
530
692
|
---
|
|
531
693
|
|
|
532
694
|
## 🛡️ Security Best Practices
|
|
@@ -558,7 +720,7 @@ ChannelKey = HKDF-SHA256(
|
|
|
558
720
|
[Derive Handle(s) as needed]
|
|
559
721
|
│
|
|
560
722
|
▼
|
|
561
|
-
[Sign challenge / data
|
|
723
|
+
[Sign challenge / data]
|
|
562
724
|
│
|
|
563
725
|
▼
|
|
564
726
|
[Zero out private key buffers] ← Critical!
|
|
@@ -574,15 +736,13 @@ ChannelKey = HKDF-SHA256(
|
|
|
574
736
|
### Unit Tests
|
|
575
737
|
|
|
576
738
|
```bash
|
|
577
|
-
cd packages/core
|
|
739
|
+
cd packages/protocol-core
|
|
578
740
|
pnpm test
|
|
579
741
|
```
|
|
580
742
|
|
|
581
743
|
Tests cover:
|
|
582
744
|
- ✅ Deterministic derivation (same seed + name → same HandleId)
|
|
583
745
|
- ✅ Signature generation and verification
|
|
584
|
-
- ✅ Password derivation (same Handle + context → same password)
|
|
585
|
-
- ✅ Channel key derivation (same Handle + context → same key)
|
|
586
746
|
- ✅ Edge cases (empty metadata, long names, unicode)
|
|
587
747
|
|
|
588
748
|
### Integration Test Example
|
|
@@ -608,6 +768,57 @@ test('full auth flow', async () => {
|
|
|
608
768
|
});
|
|
609
769
|
```
|
|
610
770
|
|
|
771
|
+
### Cross-Implementation Testing
|
|
772
|
+
|
|
773
|
+
Use [`specs/test-vectors.json`](../../specs/test-vectors.json) to verify your implementation matches the reference:
|
|
774
|
+
|
|
775
|
+
```typescript
|
|
776
|
+
import vectors from '@me2em/core/specs/test-vectors.json';
|
|
777
|
+
|
|
778
|
+
for (const vector of vectors) {
|
|
779
|
+
const identity = await Identity.fromSeed(vector.seed_hex);
|
|
780
|
+
const handle = await identity.deriveHandle(vector.handle_name);
|
|
781
|
+
|
|
782
|
+
expect(handle.getId()).toBe(vector.expected_handle_id);
|
|
783
|
+
expect(bytesToHex(handle.getPublicKey())).toBe(vector.expected_public_key_hex);
|
|
784
|
+
}
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
---
|
|
788
|
+
|
|
789
|
+
## 🤝 Contributing
|
|
790
|
+
|
|
791
|
+
We welcome contributions! See:
|
|
792
|
+
|
|
793
|
+
- 📄 [CONTRIBUTING.md](../../CONTRIBUTING.md) — How to contribute code/docs
|
|
794
|
+
- 🗳️ [GOVERNANCE.md](../../GOVERNANCE.md) — Project decision-making process
|
|
795
|
+
- 🔐 [SECURITY.md](../../SECURITY.md) — Responsible disclosure policy
|
|
796
|
+
|
|
797
|
+
### Quick Start for Contributors
|
|
798
|
+
|
|
799
|
+
```bash
|
|
800
|
+
git clone https://github.com/me2em-org/me2em-protocol.git
|
|
801
|
+
cd me2em-protocol
|
|
802
|
+
pnpm install
|
|
803
|
+
|
|
804
|
+
# Run tests
|
|
805
|
+
pnpm -r test
|
|
806
|
+
|
|
807
|
+
# Build all packages
|
|
808
|
+
pnpm -r build
|
|
809
|
+
|
|
810
|
+
# Lint
|
|
811
|
+
pnpm -r lint
|
|
812
|
+
```
|
|
813
|
+
|
|
814
|
+
### Adding a New Feature
|
|
815
|
+
|
|
816
|
+
1. Open a [Discussion](https://github.com/me2em-org/me2em-protocol/discussions) to propose the change
|
|
817
|
+
2. Implement in a feature branch
|
|
818
|
+
3. Add tests covering new functionality
|
|
819
|
+
4. Update `specs/core.md` if protocol behavior changes
|
|
820
|
+
5. Submit PR with clear description and test results
|
|
821
|
+
|
|
611
822
|
---
|
|
612
823
|
|
|
613
824
|
## 📦 Package Info
|
|
@@ -615,7 +826,7 @@ test('full auth flow', async () => {
|
|
|
615
826
|
```json
|
|
616
827
|
{
|
|
617
828
|
"name": "@me2em/core",
|
|
618
|
-
"version": "0.
|
|
829
|
+
"version": "0.3.0-alpha.1",
|
|
619
830
|
"type": "module",
|
|
620
831
|
"main": "./dist/index.js",
|
|
621
832
|
"types": "./dist/index.d.ts",
|
|
@@ -628,8 +839,7 @@ test('full auth flow', async () => {
|
|
|
628
839
|
},
|
|
629
840
|
"dependencies": {
|
|
630
841
|
"@noble/ed25519": "^3.1.0",
|
|
631
|
-
"@noble/hashes": "^2.2.0"
|
|
632
|
-
"@scure/bip39": "^1.3.0"
|
|
842
|
+
"@noble/hashes": "^2.2.0"
|
|
633
843
|
},
|
|
634
844
|
"engines": {
|
|
635
845
|
"node": ">=18.0.0"
|
|
@@ -639,13 +849,17 @@ test('full auth flow', async () => {
|
|
|
639
849
|
|
|
640
850
|
---
|
|
641
851
|
|
|
642
|
-
##
|
|
852
|
+
## 🌐 Ecosystem
|
|
643
853
|
|
|
644
|
-
|
|
854
|
+
This package is part of the Me2em ecosystem:
|
|
645
855
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
856
|
+
@me2em/core ← криптография (Identity, Handle, derivePassword, deriveChannelKey)
|
|
857
|
+
@me2em/sdk ← browser wrapper (IndexedDB, PIN, biometric, session management)
|
|
858
|
+
@me2em/react ← UI компоненты (SeedDisplay, SeedVerification, HandleManager)
|
|
859
|
+
@me2em/auth-middleware ← NestJS/Express middleware для Handle.verify()
|
|
860
|
+
@me2em/server ← (опционально) готовый NestJS backend из Phase 1
|
|
861
|
+
|
|
862
|
+
🔗 Learn more: [github.com/me2em-org](https://github.com/me2em-org) | [docs.me2em.com](https://docs.me2em.com)
|
|
649
863
|
|
|
650
864
|
---
|
|
651
865
|
|