@me2em/core 0.4.1-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/LICENSE +201 -0
- package/README.md +871 -0
- package/dist/crypto/ed25519.d.ts +8 -0
- package/dist/crypto/ed25519.d.ts.map +1 -0
- package/dist/crypto/ed25519.js +8 -0
- package/dist/crypto/ed25519.js.map +1 -0
- package/dist/crypto/hkdf.d.ts +13 -0
- package/dist/crypto/hkdf.d.ts.map +1 -0
- package/dist/crypto/hkdf.js +16 -0
- package/dist/crypto/hkdf.js.map +1 -0
- package/dist/crypto/index.d.ts +3 -0
- package/dist/crypto/index.d.ts.map +1 -0
- package/dist/crypto/index.js +3 -0
- package/dist/crypto/index.js.map +1 -0
- package/dist/crypto/init.d.ts +9 -0
- package/dist/crypto/init.d.ts.map +1 -0
- package/dist/crypto/init.js +16 -0
- package/dist/crypto/init.js.map +1 -0
- package/dist/handle.d.ts +53 -0
- package/dist/handle.d.ts.map +1 -0
- package/dist/handle.js +79 -0
- package/dist/handle.js.map +1 -0
- package/dist/identity.d.ts +10 -0
- package/dist/identity.d.ts.map +1 -0
- package/dist/identity.js +38 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/session.d.ts +20 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +16 -0
- package/dist/session.js.map +1 -0
- package/package.json +41 -0
- package/src/crypto/ed25519.ts +8 -0
- package/src/crypto/hkdf.ts +22 -0
- package/src/crypto/index.ts +2 -0
- package/src/crypto/init.ts +18 -0
- package/src/handle.ts +94 -0
- package/src/identity.ts +56 -0
- package/src/index.ts +6 -0
- package/src/session.ts +25 -0
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ed } from './init.js';
|
|
2
|
+
export declare const getPublicKey: (secretKey: ed.TArg<Uint8Array>) => ed.TRet<Uint8Array>;
|
|
3
|
+
export declare const sign: (message: ed.TArg<Uint8Array>, secretKey: ed.TArg<Uint8Array>) => ed.TRet<Uint8Array>;
|
|
4
|
+
export declare const verify: (signature: ed.TArg<Uint8Array>, message: ed.TArg<Uint8Array>, publicKey: ed.TArg<Uint8Array>, opts?: ed.TArg<ed.EdDSAVerifyOpts>) => boolean;
|
|
5
|
+
export declare const getPublicKeyAsync: (secretKey: ed.TArg<Uint8Array>) => Promise<ed.TRet<Uint8Array>>;
|
|
6
|
+
export declare const signAsync: (message: ed.TArg<Uint8Array>, secretKey: ed.TArg<Uint8Array>) => Promise<ed.TRet<Uint8Array>>;
|
|
7
|
+
export declare const verifyAsync: (signature: ed.TArg<Uint8Array>, message: ed.TArg<Uint8Array>, publicKey: ed.TArg<Uint8Array>, opts?: ed.TArg<ed.EdDSAVerifyOpts>) => Promise<boolean>;
|
|
8
|
+
//# sourceMappingURL=ed25519.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../../src/crypto/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAE/B,eAAO,MAAM,YAAY,yDAAkB,CAAC;AAC5C,eAAO,MAAM,IAAI,uFAAU,CAAC;AAC5B,eAAO,MAAM,MAAM,+IAAY,CAAC;AAChC,eAAO,MAAM,iBAAiB,kEAAuB,CAAC;AACtD,eAAO,MAAM,SAAS,gGAAe,CAAC;AACtC,eAAO,MAAM,WAAW,wJAAiB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ed } from './init.js';
|
|
2
|
+
export const getPublicKey = ed.getPublicKey;
|
|
3
|
+
export const sign = ed.sign;
|
|
4
|
+
export const verify = ed.verify;
|
|
5
|
+
export const getPublicKeyAsync = ed.getPublicKeyAsync;
|
|
6
|
+
export const signAsync = ed.signAsync;
|
|
7
|
+
export const verifyAsync = ed.verifyAsync;
|
|
8
|
+
//# sourceMappingURL=ed25519.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519.js","sourceRoot":"","sources":["../../src/crypto/ed25519.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAE/B,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC,YAAY,CAAC;AAC5C,MAAM,CAAC,MAAM,IAAI,GAAG,EAAE,CAAC,IAAI,CAAC;AAC5B,MAAM,CAAC,MAAM,MAAM,GAAG,EAAE,CAAC,MAAM,CAAC;AAChC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC,iBAAiB,CAAC;AACtD,MAAM,CAAC,MAAM,SAAS,GAAG,EAAE,CAAC,SAAS,CAAC;AACtC,MAAM,CAAC,MAAM,WAAW,GAAG,EAAE,CAAC,WAAW,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';
|
|
2
|
+
/**
|
|
3
|
+
* HKDF-SHA256 wrapper for me2em protocol
|
|
4
|
+
*
|
|
5
|
+
* @param ikm - Input key material
|
|
6
|
+
* @param salt - Salt (can be empty Uint8Array)
|
|
7
|
+
* @param info - Context info
|
|
8
|
+
* @param length - Output length in bytes
|
|
9
|
+
* @returns Derived key as Uint8Array
|
|
10
|
+
*/
|
|
11
|
+
export declare function hkdf(ikm: Uint8Array, salt: Uint8Array, info: Uint8Array, length: number): Uint8Array;
|
|
12
|
+
export { nobleSha256 as sha256 };
|
|
13
|
+
//# sourceMappingURL=hkdf.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hkdf.d.ts","sourceRoot":"","sources":["../../src/crypto/hkdf.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;;;;;;;GAQG;AACH,wBAAgB,IAAI,CAClB,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,MAAM,GACb,UAAU,CAEZ;AAED,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { hkdf as nobleHkdf } from '@noble/hashes/hkdf.js';
|
|
2
|
+
import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';
|
|
3
|
+
/**
|
|
4
|
+
* HKDF-SHA256 wrapper for me2em protocol
|
|
5
|
+
*
|
|
6
|
+
* @param ikm - Input key material
|
|
7
|
+
* @param salt - Salt (can be empty Uint8Array)
|
|
8
|
+
* @param info - Context info
|
|
9
|
+
* @param length - Output length in bytes
|
|
10
|
+
* @returns Derived key as Uint8Array
|
|
11
|
+
*/
|
|
12
|
+
export function hkdf(ikm, salt, info, length) {
|
|
13
|
+
return nobleHkdf(nobleSha256, ikm, salt, info, length);
|
|
14
|
+
}
|
|
15
|
+
export { nobleSha256 as sha256 };
|
|
16
|
+
//# sourceMappingURL=hkdf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hkdf.js","sourceRoot":"","sources":["../../src/crypto/hkdf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,MAAM,IAAI,WAAW,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,UAAU,IAAI,CAClB,GAAe,EACf,IAAgB,EAChB,IAAgB,EAChB,MAAc;IAEd,OAAO,SAAS,CAAC,WAAW,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;AACzD,CAAC;AAED,OAAO,EAAE,WAAW,IAAI,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/crypto/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as ed from '@noble/ed25519';
|
|
2
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
3
|
+
/**
|
|
4
|
+
* Configure @noble/ed25519 for synchronous operations.
|
|
5
|
+
* Must be called before any ed25519 operation (sign, verify, getPublicKey).
|
|
6
|
+
*/
|
|
7
|
+
export declare function initEd25519(): void;
|
|
8
|
+
export { ed, sha512 };
|
|
9
|
+
//# sourceMappingURL=init.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../../src/crypto/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE/C;;;GAGG;AACH,wBAAgB,WAAW,IAAI,IAAI,CAKlC;AAKD,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as ed from '@noble/ed25519';
|
|
2
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
3
|
+
/**
|
|
4
|
+
* Configure @noble/ed25519 for synchronous operations.
|
|
5
|
+
* Must be called before any ed25519 operation (sign, verify, getPublicKey).
|
|
6
|
+
*/
|
|
7
|
+
export function initEd25519() {
|
|
8
|
+
// Configure sha512 for sync operations (required for @noble/ed25519 v3)
|
|
9
|
+
if (!ed.hashes.sha512) {
|
|
10
|
+
ed.hashes.sha512 = sha512;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
// Auto-init on module load
|
|
14
|
+
initEd25519();
|
|
15
|
+
export { ed, sha512 };
|
|
16
|
+
//# sourceMappingURL=init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../../src/crypto/init.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAE/C;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,wEAAwE;IACxE,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC;QACtB,EAAE,CAAC,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;IAC5B,CAAC;AACH,CAAC;AAED,2BAA2B;AAC3B,WAAW,EAAE,CAAC;AAEd,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/handle.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface HandleMetadata {
|
|
2
|
+
displayName?: string;
|
|
3
|
+
avatar?: string;
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
}
|
|
6
|
+
export declare class Handle {
|
|
7
|
+
private readonly privateKey;
|
|
8
|
+
private readonly publicKey;
|
|
9
|
+
private readonly _name;
|
|
10
|
+
private readonly _metadata?;
|
|
11
|
+
constructor(privateKey: Uint8Array, name: string, metadata?: HandleMetadata);
|
|
12
|
+
getId(): string;
|
|
13
|
+
sign(data: Uint8Array): Promise<Uint8Array>;
|
|
14
|
+
static verify(signature: Uint8Array, data: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
|
|
15
|
+
getName(): string;
|
|
16
|
+
getMetadata(): HandleMetadata | undefined;
|
|
17
|
+
getPublicKey(): Uint8Array;
|
|
18
|
+
/**
|
|
19
|
+
* Deterministically derives a secret (e.g., a password or API key) for a specific service context.
|
|
20
|
+
* The private key NEVER leaves this class, ensuring maximum security.
|
|
21
|
+
*
|
|
22
|
+
* @param context - A unique identifier for the service (e.g., 'google', 'github', 'wifi-router').
|
|
23
|
+
* @param length - Length of the derived raw bytes (default: 16 bytes = ~22 chars base64url).
|
|
24
|
+
* @returns A URL-safe base64 string suitable for use as a strong password.
|
|
25
|
+
*/
|
|
26
|
+
derivePassword(context: string, length?: number): string;
|
|
27
|
+
/**
|
|
28
|
+
* Derives a symmetric 256-bit channel key for secure communication between
|
|
29
|
+
* the Identity (controller) and this Handle (device/context).
|
|
30
|
+
*
|
|
31
|
+
* Both parties can independently compute this key because:
|
|
32
|
+
* - The Handle possesses its own private key directly
|
|
33
|
+
* - The Identity can derive the same private key via `identity.deriveHandle(name)`
|
|
34
|
+
*
|
|
35
|
+
* This enables zero-knowledge encrypted channels without key exchange protocols.
|
|
36
|
+
*
|
|
37
|
+
* @param context - Channel identifier for domain separation (e.g., 'drone-001', 'session-abc').
|
|
38
|
+
* Both parties MUST use the same context to derive the same key.
|
|
39
|
+
* @returns 32-byte Uint8Array suitable for AES-256-GCM encryption.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // On the drone (Handle side):
|
|
43
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
44
|
+
* const encrypted = await encryptAESGCM(telemetry, channelKey);
|
|
45
|
+
*
|
|
46
|
+
* // On the control center (Identity side):
|
|
47
|
+
* const droneHandle = await centerIdentity.deriveHandle('drone-001');
|
|
48
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
49
|
+
* const decrypted = await decryptAESGCM(encrypted, channelKey);
|
|
50
|
+
*/
|
|
51
|
+
deriveChannelKey(context: string): Uint8Array;
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=handle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,cAAc;IAC7B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAED,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAiB;gBAEhC,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,cAAc;IAO3E,KAAK,IAAI,MAAM;IAOT,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;WAIpC,MAAM,CAAC,SAAS,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC;IAIrG,OAAO,IAAI,MAAM;IACjB,WAAW,IAAI,cAAc,GAAG,SAAS;IACzC,YAAY,IAAI,UAAU;IAE1B;;;;;;;OAOG;IACH,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,MAAW,GAAG,MAAM;IAc5D;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU;CAK9C"}
|
package/dist/handle.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { ed } from './crypto/init.js';
|
|
2
|
+
import { hkdf } from '@noble/hashes/hkdf.js';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
export class Handle {
|
|
5
|
+
privateKey;
|
|
6
|
+
publicKey;
|
|
7
|
+
_name;
|
|
8
|
+
_metadata;
|
|
9
|
+
constructor(privateKey, name, metadata) {
|
|
10
|
+
this.privateKey = privateKey;
|
|
11
|
+
this.publicKey = ed.getPublicKey(privateKey);
|
|
12
|
+
this._name = name;
|
|
13
|
+
this._metadata = metadata;
|
|
14
|
+
}
|
|
15
|
+
getId() {
|
|
16
|
+
return btoa(String.fromCharCode(...this.publicKey))
|
|
17
|
+
.replace(/\+/g, '-')
|
|
18
|
+
.replace(/\//g, '_')
|
|
19
|
+
.replace(/=+$/, '');
|
|
20
|
+
}
|
|
21
|
+
async sign(data) {
|
|
22
|
+
return ed.sign(data, this.privateKey);
|
|
23
|
+
}
|
|
24
|
+
static async verify(signature, data, publicKey) {
|
|
25
|
+
return ed.verify(signature, data, publicKey);
|
|
26
|
+
}
|
|
27
|
+
getName() { return this._name; }
|
|
28
|
+
getMetadata() { return this._metadata; }
|
|
29
|
+
getPublicKey() { return this.publicKey; }
|
|
30
|
+
/**
|
|
31
|
+
* Deterministically derives a secret (e.g., a password or API key) for a specific service context.
|
|
32
|
+
* The private key NEVER leaves this class, ensuring maximum security.
|
|
33
|
+
*
|
|
34
|
+
* @param context - A unique identifier for the service (e.g., 'google', 'github', 'wifi-router').
|
|
35
|
+
* @param length - Length of the derived raw bytes (default: 16 bytes = ~22 chars base64url).
|
|
36
|
+
* @returns A URL-safe base64 string suitable for use as a strong password.
|
|
37
|
+
*/
|
|
38
|
+
derivePassword(context, length = 16) {
|
|
39
|
+
const salt = new TextEncoder().encode(`me2em/secret/${context.toLowerCase().trim()}`);
|
|
40
|
+
const info = new TextEncoder().encode('me2em/secret/v1');
|
|
41
|
+
// HKDF использует this.privateKey напрямую, не экспортируя его
|
|
42
|
+
const secretBytes = hkdf(sha256, this.privateKey, salt, info, length);
|
|
43
|
+
// Конвертация в URL-safe base64 (аналогично getId)
|
|
44
|
+
return btoa(String.fromCharCode(...secretBytes))
|
|
45
|
+
.replace(/\+/g, '-')
|
|
46
|
+
.replace(/\//g, '_')
|
|
47
|
+
.replace(/=+$/, '');
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Derives a symmetric 256-bit channel key for secure communication between
|
|
51
|
+
* the Identity (controller) and this Handle (device/context).
|
|
52
|
+
*
|
|
53
|
+
* Both parties can independently compute this key because:
|
|
54
|
+
* - The Handle possesses its own private key directly
|
|
55
|
+
* - The Identity can derive the same private key via `identity.deriveHandle(name)`
|
|
56
|
+
*
|
|
57
|
+
* This enables zero-knowledge encrypted channels without key exchange protocols.
|
|
58
|
+
*
|
|
59
|
+
* @param context - Channel identifier for domain separation (e.g., 'drone-001', 'session-abc').
|
|
60
|
+
* Both parties MUST use the same context to derive the same key.
|
|
61
|
+
* @returns 32-byte Uint8Array suitable for AES-256-GCM encryption.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // On the drone (Handle side):
|
|
65
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
66
|
+
* const encrypted = await encryptAESGCM(telemetry, channelKey);
|
|
67
|
+
*
|
|
68
|
+
* // On the control center (Identity side):
|
|
69
|
+
* const droneHandle = await centerIdentity.deriveHandle('drone-001');
|
|
70
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
71
|
+
* const decrypted = await decryptAESGCM(encrypted, channelKey);
|
|
72
|
+
*/
|
|
73
|
+
deriveChannelKey(context) {
|
|
74
|
+
const salt = new TextEncoder().encode(`me2em/channel/${context.toLowerCase().trim()}`);
|
|
75
|
+
const info = new TextEncoder().encode('me2em/channel/v1');
|
|
76
|
+
return hkdf(sha256, this.privateKey, salt, info, 32);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=handle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handle.js","sourceRoot":"","sources":["../src/handle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,kBAAkB,CAAC;AACtC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAQ/C,MAAM,OAAO,MAAM;IACA,UAAU,CAAa;IACvB,SAAS,CAAa;IACtB,KAAK,CAAS;IACd,SAAS,CAAkB;IAE5C,YAAY,UAAsB,EAAE,IAAY,EAAE,QAAyB;QACzE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC;QAC7C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;QAClB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC;IAC5B,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC;aAChD,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACxB,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,IAAgB;QACzB,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,SAAqB,EAAE,IAAgB,EAAE,SAAqB;QAChF,OAAO,EAAE,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,SAAS,CAAC,CAAC;IAC/C,CAAC;IAED,OAAO,KAAa,OAAO,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;IACxC,WAAW,KAAiC,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IACpE,YAAY,KAAiB,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IAErD;;;;;;;OAOG;IACH,cAAc,CAAC,OAAe,EAAE,SAAiB,EAAE;QACjD,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,gBAAgB,OAAO,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACtF,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;QAEzD,+DAA+D;QAC/D,MAAM,WAAW,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QAEtE,mDAAmD;QACnD,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,WAAW,CAAC,CAAC;aAC7C,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;aACnB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACxB,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,gBAAgB,CAAC,OAAe;QAC9B,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,iBAAiB,OAAO,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACvF,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC;QAC1D,OAAO,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC;IACvD,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Handle, type HandleMetadata } from './handle.js';
|
|
2
|
+
export declare class Identity {
|
|
3
|
+
private readonly privateKey;
|
|
4
|
+
private readonly publicKey;
|
|
5
|
+
private constructor();
|
|
6
|
+
static fromSeed(seed: Uint8Array | string): Promise<Identity>;
|
|
7
|
+
deriveHandle(name: string, metadata?: HandleMetadata): Promise<Handle>;
|
|
8
|
+
getPublicKey(): Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=identity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.d.ts","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAI1D,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IAEvC,OAAO;WAKM,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAoB7D,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;IAgB5E,YAAY,IAAI,UAAU;CAG3B"}
|
package/dist/identity.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ed } from './crypto/init.js'; // ← sha512 теперь импортируется из init
|
|
2
|
+
import { hkdf } from '@noble/hashes/hkdf.js';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import { Handle } from './handle.js';
|
|
5
|
+
const ME2EM_HKDF_INFO_PREFIX = 'me2em/handle/v1/';
|
|
6
|
+
export class Identity {
|
|
7
|
+
privateKey;
|
|
8
|
+
publicKey;
|
|
9
|
+
constructor(privateKey) {
|
|
10
|
+
this.privateKey = privateKey;
|
|
11
|
+
this.publicKey = ed.getPublicKey(privateKey); // Теперь работает синхронно
|
|
12
|
+
}
|
|
13
|
+
static async fromSeed(seed) {
|
|
14
|
+
const seedBytes = typeof seed === 'string'
|
|
15
|
+
? Uint8Array.from(Buffer.from(seed, 'hex'))
|
|
16
|
+
: seed;
|
|
17
|
+
if (seedBytes.length !== 32) {
|
|
18
|
+
throw new Error('Invalid seed: must be 32 bytes');
|
|
19
|
+
}
|
|
20
|
+
const key = hkdf(sha256, seedBytes, new Uint8Array(0), // salt
|
|
21
|
+
new TextEncoder().encode('me2em/identity/v1/root'), // info
|
|
22
|
+
32 // length
|
|
23
|
+
);
|
|
24
|
+
return new Identity(key);
|
|
25
|
+
}
|
|
26
|
+
async deriveHandle(name, metadata) {
|
|
27
|
+
const info = new TextEncoder().encode(ME2EM_HKDF_INFO_PREFIX + name.toLowerCase().trim());
|
|
28
|
+
// Используем sha256 для единообразия (или sha512, если нужно)
|
|
29
|
+
// Важно: используем тот же хэш, что и в спецификации
|
|
30
|
+
const handleKey = hkdf(sha256, // ← Используем sha256 для совместимости с Identity деривацией
|
|
31
|
+
this.privateKey, new Uint8Array(0), info, 32);
|
|
32
|
+
return new Handle(handleKey, name, metadata);
|
|
33
|
+
}
|
|
34
|
+
getPublicKey() {
|
|
35
|
+
return this.publicKey;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identity.js","sourceRoot":"","sources":["../src/identity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAU,MAAM,kBAAkB,CAAC,CAAE,wCAAwC;AACxF,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAuB,MAAM,aAAa,CAAC;AAE1D,MAAM,sBAAsB,GAAG,kBAAkB,CAAC;AAElD,MAAM,OAAO,QAAQ;IACF,UAAU,CAAa;IACvB,SAAS,CAAa;IAEvC,YAAoB,UAAsB;QACxC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,CAAC,CAAE,4BAA4B;IAC7E,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAyB;QAC7C,MAAM,SAAS,GAAG,OAAO,IAAI,KAAK,QAAQ;YACxC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;YAC3C,CAAC,CAAC,IAAI,CAAC;QAET,IAAI,SAAS,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;QAED,MAAM,GAAG,GAAG,IAAI,CACd,MAAM,EACN,SAAS,EACT,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO;QAC1B,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,wBAAwB,CAAC,EAAE,OAAO;QAC3D,EAAE,CAAC,SAAS;SACb,CAAC;QAEF,OAAO,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC3B,CAAC;IAED,KAAK,CAAC,YAAY,CAAC,IAAY,EAAE,QAAyB;QACxD,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,sBAAsB,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC;QAE1F,8DAA8D;QAC9D,qDAAqD;QACrD,MAAM,SAAS,GAAG,IAAI,CACpB,MAAM,EAAG,8DAA8D;QACvE,IAAI,CAAC,UAAU,EACf,IAAI,UAAU,CAAC,CAAC,CAAC,EACjB,IAAI,EACJ,EAAE,CACH,CAAC;QAEF,OAAO,IAAI,MAAM,CAAC,SAAS,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAED,YAAY;QACV,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Identity } from './identity.js';
|
|
2
|
+
export { Handle, type HandleMetadata } from './handle.js';
|
|
3
|
+
export { Session, type SessionOptions, type SessionToken } from './session.js';
|
|
4
|
+
export * as crypto from './crypto/index.js';
|
|
5
|
+
export declare const PROTOCOL_VERSION = "0.1.0-alpha.1";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,KAAK,YAAY,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAE5C,eAAO,MAAM,gBAAgB,kBAAkB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAuB,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,OAAO,EAA0C,MAAM,cAAc,CAAC;AAC/E,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAE5C,MAAM,CAAC,MAAM,gBAAgB,GAAG,eAAe,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface SessionOptions {
|
|
2
|
+
audience: string;
|
|
3
|
+
scopes: string[];
|
|
4
|
+
ttl?: number;
|
|
5
|
+
}
|
|
6
|
+
export interface SessionToken {
|
|
7
|
+
handleId: string;
|
|
8
|
+
scopes: string[];
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
token: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class Session {
|
|
13
|
+
readonly handleId: string;
|
|
14
|
+
readonly scopes: string[];
|
|
15
|
+
readonly expiresAt: number;
|
|
16
|
+
readonly token: string;
|
|
17
|
+
constructor(handleId: string, scopes: string[], expiresAt: number, token: string);
|
|
18
|
+
isExpired(): boolean;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACf;AAED,qBAAa,OAAO;aAEA,QAAQ,EAAE,MAAM;aAChB,MAAM,EAAE,MAAM,EAAE;aAChB,SAAS,EAAE,MAAM;aACjB,KAAK,EAAE,MAAM;gBAHb,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EAAE,EAChB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,MAAM;IAG/B,SAAS,IAAI,OAAO;CAGrB"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class Session {
|
|
2
|
+
handleId;
|
|
3
|
+
scopes;
|
|
4
|
+
expiresAt;
|
|
5
|
+
token;
|
|
6
|
+
constructor(handleId, scopes, expiresAt, token) {
|
|
7
|
+
this.handleId = handleId;
|
|
8
|
+
this.scopes = scopes;
|
|
9
|
+
this.expiresAt = expiresAt;
|
|
10
|
+
this.token = token;
|
|
11
|
+
}
|
|
12
|
+
isExpired() {
|
|
13
|
+
return Date.now() >= this.expiresAt * 1000;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,OAAO;IAEA;IACA;IACA;IACA;IAJlB,YACkB,QAAgB,EAChB,MAAgB,EAChB,SAAiB,EACjB,KAAa;QAHb,aAAQ,GAAR,QAAQ,CAAQ;QAChB,WAAM,GAAN,MAAM,CAAU;QAChB,cAAS,GAAT,SAAS,CAAQ;QACjB,UAAK,GAAL,KAAK,CAAQ;IAC5B,CAAC;IAEJ,SAAS;QACP,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;IAC7C,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@me2em/core",
|
|
3
|
+
"version": "0.4.1-alpha.1",
|
|
4
|
+
"description": "Core primitives for the Me2em authorization protocol: Identity, Handle, and Session",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@noble/ed25519": "^3.1.0",
|
|
27
|
+
"@noble/hashes": "^2.2.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^25.9.1",
|
|
31
|
+
"eslint": "^10.4.1",
|
|
32
|
+
"typescript": "^6.0.3",
|
|
33
|
+
"vitest": "^4.1.7"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -p tsconfig.json",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"lint": "eslint src",
|
|
39
|
+
"typecheck": "tsc --noEmit"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ed } from './init.js';
|
|
2
|
+
|
|
3
|
+
export const getPublicKey = ed.getPublicKey;
|
|
4
|
+
export const sign = ed.sign;
|
|
5
|
+
export const verify = ed.verify;
|
|
6
|
+
export const getPublicKeyAsync = ed.getPublicKeyAsync;
|
|
7
|
+
export const signAsync = ed.signAsync;
|
|
8
|
+
export const verifyAsync = ed.verifyAsync;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { hkdf as nobleHkdf } from '@noble/hashes/hkdf.js';
|
|
2
|
+
import { sha256 as nobleSha256 } from '@noble/hashes/sha2.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* HKDF-SHA256 wrapper for me2em protocol
|
|
6
|
+
*
|
|
7
|
+
* @param ikm - Input key material
|
|
8
|
+
* @param salt - Salt (can be empty Uint8Array)
|
|
9
|
+
* @param info - Context info
|
|
10
|
+
* @param length - Output length in bytes
|
|
11
|
+
* @returns Derived key as Uint8Array
|
|
12
|
+
*/
|
|
13
|
+
export function hkdf(
|
|
14
|
+
ikm: Uint8Array,
|
|
15
|
+
salt: Uint8Array,
|
|
16
|
+
info: Uint8Array,
|
|
17
|
+
length: number
|
|
18
|
+
): Uint8Array {
|
|
19
|
+
return nobleHkdf(nobleSha256, ikm, salt, info, length);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { nobleSha256 as sha256 };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as ed from '@noble/ed25519';
|
|
2
|
+
import { sha512 } from '@noble/hashes/sha2.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configure @noble/ed25519 for synchronous operations.
|
|
6
|
+
* Must be called before any ed25519 operation (sign, verify, getPublicKey).
|
|
7
|
+
*/
|
|
8
|
+
export function initEd25519(): void {
|
|
9
|
+
// Configure sha512 for sync operations (required for @noble/ed25519 v3)
|
|
10
|
+
if (!ed.hashes.sha512) {
|
|
11
|
+
ed.hashes.sha512 = sha512;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Auto-init on module load
|
|
16
|
+
initEd25519();
|
|
17
|
+
|
|
18
|
+
export { ed, sha512 };
|
package/src/handle.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { ed } from './crypto/init.js';
|
|
2
|
+
import { hkdf } from '@noble/hashes/hkdf.js';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
|
|
5
|
+
export interface HandleMetadata {
|
|
6
|
+
displayName?: string;
|
|
7
|
+
avatar?: string;
|
|
8
|
+
[key: string]: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class Handle {
|
|
12
|
+
private readonly privateKey: Uint8Array;
|
|
13
|
+
private readonly publicKey: Uint8Array;
|
|
14
|
+
private readonly _name: string;
|
|
15
|
+
private readonly _metadata?: HandleMetadata;
|
|
16
|
+
|
|
17
|
+
constructor(privateKey: Uint8Array, name: string, metadata?: HandleMetadata) {
|
|
18
|
+
this.privateKey = privateKey;
|
|
19
|
+
this.publicKey = ed.getPublicKey(privateKey);
|
|
20
|
+
this._name = name;
|
|
21
|
+
this._metadata = metadata;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
getId(): string {
|
|
25
|
+
return btoa(String.fromCharCode(...this.publicKey))
|
|
26
|
+
.replace(/\+/g, '-')
|
|
27
|
+
.replace(/\//g, '_')
|
|
28
|
+
.replace(/=+$/, '');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async sign(data: Uint8Array): Promise<Uint8Array> {
|
|
32
|
+
return ed.sign(data, this.privateKey);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static async verify(signature: Uint8Array, data: Uint8Array, publicKey: Uint8Array): Promise<boolean> {
|
|
36
|
+
return ed.verify(signature, data, publicKey);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getName(): string { return this._name; }
|
|
40
|
+
getMetadata(): HandleMetadata | undefined { return this._metadata; }
|
|
41
|
+
getPublicKey(): Uint8Array { return this.publicKey; }
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Deterministically derives a secret (e.g., a password or API key) for a specific service context.
|
|
45
|
+
* The private key NEVER leaves this class, ensuring maximum security.
|
|
46
|
+
*
|
|
47
|
+
* @param context - A unique identifier for the service (e.g., 'google', 'github', 'wifi-router').
|
|
48
|
+
* @param length - Length of the derived raw bytes (default: 16 bytes = ~22 chars base64url).
|
|
49
|
+
* @returns A URL-safe base64 string suitable for use as a strong password.
|
|
50
|
+
*/
|
|
51
|
+
derivePassword(context: string, length: number = 16): string {
|
|
52
|
+
const salt = new TextEncoder().encode(`me2em/secret/${context.toLowerCase().trim()}`);
|
|
53
|
+
const info = new TextEncoder().encode('me2em/secret/v1');
|
|
54
|
+
|
|
55
|
+
// HKDF использует this.privateKey напрямую, не экспортируя его
|
|
56
|
+
const secretBytes = hkdf(sha256, this.privateKey, salt, info, length);
|
|
57
|
+
|
|
58
|
+
// Конвертация в URL-safe base64 (аналогично getId)
|
|
59
|
+
return btoa(String.fromCharCode(...secretBytes))
|
|
60
|
+
.replace(/\+/g, '-')
|
|
61
|
+
.replace(/\//g, '_')
|
|
62
|
+
.replace(/=+$/, '');
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Derives a symmetric 256-bit channel key for secure communication between
|
|
67
|
+
* the Identity (controller) and this Handle (device/context).
|
|
68
|
+
*
|
|
69
|
+
* Both parties can independently compute this key because:
|
|
70
|
+
* - The Handle possesses its own private key directly
|
|
71
|
+
* - The Identity can derive the same private key via `identity.deriveHandle(name)`
|
|
72
|
+
*
|
|
73
|
+
* This enables zero-knowledge encrypted channels without key exchange protocols.
|
|
74
|
+
*
|
|
75
|
+
* @param context - Channel identifier for domain separation (e.g., 'drone-001', 'session-abc').
|
|
76
|
+
* Both parties MUST use the same context to derive the same key.
|
|
77
|
+
* @returns 32-byte Uint8Array suitable for AES-256-GCM encryption.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // On the drone (Handle side):
|
|
81
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
82
|
+
* const encrypted = await encryptAESGCM(telemetry, channelKey);
|
|
83
|
+
*
|
|
84
|
+
* // On the control center (Identity side):
|
|
85
|
+
* const droneHandle = await centerIdentity.deriveHandle('drone-001');
|
|
86
|
+
* const channelKey = droneHandle.deriveChannelKey('telemetry-v1');
|
|
87
|
+
* const decrypted = await decryptAESGCM(encrypted, channelKey);
|
|
88
|
+
*/
|
|
89
|
+
deriveChannelKey(context: string): Uint8Array {
|
|
90
|
+
const salt = new TextEncoder().encode(`me2em/channel/${context.toLowerCase().trim()}`);
|
|
91
|
+
const info = new TextEncoder().encode('me2em/channel/v1');
|
|
92
|
+
return hkdf(sha256, this.privateKey, salt, info, 32);
|
|
93
|
+
}
|
|
94
|
+
}
|
package/src/identity.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ed, sha512 } from './crypto/init.js'; // ← sha512 теперь импортируется из init
|
|
2
|
+
import { hkdf } from '@noble/hashes/hkdf.js';
|
|
3
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
4
|
+
import { Handle, type HandleMetadata } from './handle.js';
|
|
5
|
+
|
|
6
|
+
const ME2EM_HKDF_INFO_PREFIX = 'me2em/handle/v1/';
|
|
7
|
+
|
|
8
|
+
export class Identity {
|
|
9
|
+
private readonly privateKey: Uint8Array;
|
|
10
|
+
private readonly publicKey: Uint8Array;
|
|
11
|
+
|
|
12
|
+
private constructor(privateKey: Uint8Array) {
|
|
13
|
+
this.privateKey = privateKey;
|
|
14
|
+
this.publicKey = ed.getPublicKey(privateKey); // Теперь работает синхронно
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static async fromSeed(seed: Uint8Array | string): Promise<Identity> {
|
|
18
|
+
const seedBytes = typeof seed === 'string'
|
|
19
|
+
? Uint8Array.from(Buffer.from(seed, 'hex'))
|
|
20
|
+
: seed;
|
|
21
|
+
|
|
22
|
+
if (seedBytes.length !== 32) {
|
|
23
|
+
throw new Error('Invalid seed: must be 32 bytes');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const key = hkdf(
|
|
27
|
+
sha256,
|
|
28
|
+
seedBytes,
|
|
29
|
+
new Uint8Array(0), // salt
|
|
30
|
+
new TextEncoder().encode('me2em/identity/v1/root'), // info
|
|
31
|
+
32 // length
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return new Identity(key);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async deriveHandle(name: string, metadata?: HandleMetadata): Promise<Handle> {
|
|
38
|
+
const info = new TextEncoder().encode(ME2EM_HKDF_INFO_PREFIX + name.toLowerCase().trim());
|
|
39
|
+
|
|
40
|
+
// Используем sha256 для единообразия (или sha512, если нужно)
|
|
41
|
+
// Важно: используем тот же хэш, что и в спецификации
|
|
42
|
+
const handleKey = hkdf(
|
|
43
|
+
sha256, // ← Используем sha256 для совместимости с Identity деривацией
|
|
44
|
+
this.privateKey,
|
|
45
|
+
new Uint8Array(0),
|
|
46
|
+
info,
|
|
47
|
+
32
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return new Handle(handleKey, name, metadata);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getPublicKey(): Uint8Array {
|
|
54
|
+
return this.publicKey;
|
|
55
|
+
}
|
|
56
|
+
}
|