@freesignal/protocol 0.2.9 → 0.3.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/index.js CHANGED
@@ -17,17 +17,29 @@
17
17
  * You should have received a copy of the GNU General Public License
18
18
  * along with this program. If not, see <https://www.gnu.org/licenses/>
19
19
  */
20
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ var desc = Object.getOwnPropertyDescriptor(m, k);
23
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
24
+ desc = { enumerable: true, get: function() { return m[k]; } };
25
+ }
26
+ Object.defineProperty(o, k2, desc);
27
+ }) : (function(o, m, k, k2) {
28
+ if (k2 === undefined) k2 = k;
29
+ o[k2] = m[k];
30
+ }));
31
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
32
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
33
+ };
20
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
21
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
22
36
  };
23
37
  Object.defineProperty(exports, "__esModule", { value: true });
24
- exports.EncryptedData = exports.Datagram = exports.Protocols = exports.IdentityKeys = exports.UserId = void 0;
25
- exports.createKeySession = createKeySession;
26
- exports.createKeyExchange = createKeyExchange;
27
- exports.createIdentityKeys = createIdentityKeys;
38
+ exports.createIdentity = createIdentity;
39
+ exports.createNode = createNode;
28
40
  const crypto_1 = __importDefault(require("@freesignal/crypto"));
29
- const double_ratchet_1 = require("./double-ratchet");
30
- const x3dh_1 = require("./x3dh");
41
+ const types_1 = require("./types");
42
+ const node_1 = require("./node");
31
43
  /**
32
44
  * Creates a new Double Ratchet session for secure message exchange.
33
45
  *
@@ -37,33 +49,34 @@ const x3dh_1 = require("./x3dh");
37
49
  * @param opts.rootKey - An optional root key to initialize the session.
38
50
  * @returns A new instance of {@link KeySession}.
39
51
  */
40
- function createKeySession(opts) {
41
- return new double_ratchet_1.KeySession(opts);
42
- }
52
+ /*export function createKeySession(storage: LocalStorage<string, ExportedKeySession>, opts?: { secretKey?: Uint8Array, remoteKey?: Uint8Array, rootKey?: Uint8Array }): KeySession {
53
+ return new KeySession(storage, opts);
54
+ }*/
43
55
  /**
44
56
  * Creates a new X3DH (Extended Triple Diffie-Hellman) key exchange session.
45
57
  *
46
- * @param signSecretKey - The EdDSA signing secret key as a Uint8Array.
47
- * @param boxSecretKey - The ECDH box secret key as a Uint8Array.
48
- * @param bundleStore - Optional local storage for key bundles.
58
+ * @param storage - Local storage for keys.
49
59
  * @returns A new instance of {@link KeyExchange}.
50
60
  */
51
- function createKeyExchange(signSecretKey, boxSecretKey, bundleStore) {
52
- return new x3dh_1.KeyExchange(signSecretKey, boxSecretKey, bundleStore);
53
- }
61
+ /*export function createKeyExchange(storage: { keys: LocalStorage<string, Crypto.KeyPair>, sessions: LocalStorage<string, ExportedKeySession> }, privateIdentityKey?: PrivateIdentityKey): KeyExchange {
62
+ return new KeyExchange(storage, privateIdentityKey);
63
+ }*/
54
64
  /**
55
- * Generates identity key pairs for signing and encryption.
65
+ * Generates identity key
56
66
  *
57
- * @param signSecretKey - Optional secret key for EdDSA signing.
58
- * @param boxSecretKey - Optional secret key for ECDH encryption.
67
+ * @param seed - Seed to generate the key.
59
68
  * @returns An object containing readonly signing and box key pairs.
60
69
  */
61
- function createIdentityKeys(signSecretKey, boxSecretKey) {
62
- return { sign: crypto_1.default.EdDSA.keyPair(signSecretKey), box: crypto_1.default.ECDH.keyPair(boxSecretKey) };
70
+ function createIdentity(seed) {
71
+ seed !== null && seed !== void 0 ? seed : (seed = crypto_1.default.randomBytes(crypto_1.default.EdDSA.seedLength));
72
+ const signatureSeed = crypto_1.default.hkdf(seed, new Uint8Array(crypto_1.default.EdDSA.seedLength).fill(0), "identity-ed25519", crypto_1.default.EdDSA.seedLength);
73
+ const exchangeSeed = crypto_1.default.hkdf(seed, new Uint8Array(crypto_1.default.ECDH.secretKeyLength).fill(0), "identity-x25519", crypto_1.default.ECDH.secretKeyLength);
74
+ const signatureKeyPair = crypto_1.default.EdDSA.keyPairFromSeed(signatureSeed);
75
+ const exchangeKeyPair = crypto_1.default.ECDH.keyPair(exchangeSeed);
76
+ return types_1.PrivateIdentityKey.from(signatureKeyPair.secretKey, exchangeKeyPair.secretKey);
77
+ }
78
+ /** */
79
+ function createNode(storage, privateIdentityKey) {
80
+ return new node_1.FreeSignalNode(storage, privateIdentityKey);
63
81
  }
64
- var types_1 = require("./types");
65
- Object.defineProperty(exports, "UserId", { enumerable: true, get: function () { return types_1.UserId; } });
66
- Object.defineProperty(exports, "IdentityKeys", { enumerable: true, get: function () { return types_1.IdentityKeys; } });
67
- Object.defineProperty(exports, "Protocols", { enumerable: true, get: function () { return types_1.Protocols; } });
68
- Object.defineProperty(exports, "Datagram", { enumerable: true, get: function () { return types_1.Datagram; } });
69
- Object.defineProperty(exports, "EncryptedData", { enumerable: true, get: function () { return types_1.EncryptedData; } });
82
+ __exportStar(require("./types"), exports);
package/node.d.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { Database, LocalStorage, Crypto, KeyExchangeDataBundle, KeyExchangeData } from "@freesignal/interfaces";
2
+ import { Datagram, IdentityKey, PrivateIdentityKey, Protocols, UserId } from "./types";
3
+ import { KeyExchange } from "./x3dh";
4
+ import { ExportedKeySession, KeySession } from "./double-ratchet";
5
+ export declare class FreeSignalNode {
6
+ protected readonly privateIdentityKey: PrivateIdentityKey;
7
+ protected readonly sessions: SessionMap;
8
+ protected readonly users: LocalStorage<string, IdentityKey>;
9
+ protected readonly keyExchange: KeyExchange;
10
+ constructor(storage: Database<{
11
+ sessions: LocalStorage<string, ExportedKeySession>;
12
+ keyExchange: LocalStorage<string, Crypto.KeyPair>;
13
+ users: LocalStorage<string, IdentityKey>;
14
+ }>, privateIdentityKey?: PrivateIdentityKey);
15
+ get userId(): UserId;
16
+ get identityKey(): IdentityKey;
17
+ generateKeyData(): Promise<KeyExchangeData>;
18
+ generateKeyBundle(length?: number): Promise<KeyExchangeDataBundle>;
19
+ encrypt(receiverId: string, protocol: Protocols, data: Uint8Array): Promise<Datagram>;
20
+ sendHandshake(data: KeyExchangeData): Promise<Datagram>;
21
+ sendData<T>(receiverId: string, data: T): Promise<Datagram>;
22
+ sendRelay(receiverId: string, data: Datagram): Promise<Datagram>;
23
+ sendDiscover(receiverId: string, discoverId: string): Promise<Datagram>;
24
+ decrypt(datagram: Datagram): Promise<Uint8Array>;
25
+ receive<T extends Uint8Array | UserId | Datagram | UserId | void>(datagram: Datagram | Uint8Array): Promise<T>;
26
+ }
27
+ declare class SessionMap implements LocalStorage<string, KeySession> {
28
+ readonly storage: LocalStorage<string, ExportedKeySession>;
29
+ readonly maxSize: number;
30
+ private readonly cache;
31
+ constructor(storage: LocalStorage<string, ExportedKeySession>, maxSize?: number);
32
+ set(key: string, value: KeySession): Promise<void>;
33
+ get(key: string): Promise<KeySession | undefined>;
34
+ has(key: string): Promise<boolean>;
35
+ delete(key: string): Promise<boolean>;
36
+ clear(): Promise<void>;
37
+ }
38
+ export {};
package/node.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.FreeSignalNode = void 0;
13
+ const types_1 = require("./types");
14
+ const x3dh_1 = require("./x3dh");
15
+ const double_ratchet_1 = require("./double-ratchet");
16
+ const _1 = require(".");
17
+ const utils_1 = require("@freesignal/utils");
18
+ class FreeSignalNode {
19
+ constructor(storage, privateIdentityKey) {
20
+ this.privateIdentityKey = privateIdentityKey !== null && privateIdentityKey !== void 0 ? privateIdentityKey : (0, _1.createIdentity)();
21
+ this.sessions = new SessionMap(storage.sessions);
22
+ this.keyExchange = new x3dh_1.KeyExchange({ keys: storage.keyExchange, sessions: storage.sessions }, this.privateIdentityKey);
23
+ this.users = storage.users;
24
+ }
25
+ get userId() {
26
+ return types_1.UserId.fromKey(this.privateIdentityKey.identityKey);
27
+ }
28
+ get identityKey() {
29
+ return this.privateIdentityKey.identityKey;
30
+ }
31
+ generateKeyData() {
32
+ return this.keyExchange.generateData();
33
+ }
34
+ ;
35
+ generateKeyBundle(length) {
36
+ return this.keyExchange.generateBundle(length);
37
+ }
38
+ ;
39
+ encrypt(receiverId, protocol, data) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const session = yield this.sessions.get(receiverId);
42
+ if (!session)
43
+ throw new Error("Session not found for user: " + receiverId);
44
+ return new types_1.Datagram(this.userId.toString(), receiverId, protocol, yield session.encrypt(data));
45
+ });
46
+ }
47
+ sendHandshake(data) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const { session, message, identityKey } = yield this.keyExchange.digestData(data);
50
+ const remoteId = types_1.UserId.fromKey(identityKey);
51
+ this.sessions.set(remoteId.toString(), session);
52
+ return new types_1.Datagram(this.userId.toString(), types_1.UserId.fromKey(data.identityKey).toString(), types_1.Protocols.HANDSHAKE, (0, utils_1.encodeData)(message));
53
+ });
54
+ }
55
+ sendData(receiverId, data) {
56
+ return this.encrypt(receiverId, types_1.Protocols.MESSAGE, (0, utils_1.encodeData)(data));
57
+ }
58
+ sendRelay(receiverId, data) {
59
+ return this.encrypt(receiverId, types_1.Protocols.RELAY, (0, utils_1.encodeData)(data));
60
+ }
61
+ sendDiscover(receiverId, discoverId) {
62
+ return this.encrypt(receiverId, types_1.Protocols.DISCOVER, (0, utils_1.encodeData)(discoverId));
63
+ }
64
+ decrypt(datagram) {
65
+ return __awaiter(this, void 0, void 0, function* () {
66
+ const userId = datagram.sender;
67
+ const session = yield this.sessions.get(userId);
68
+ if (!session)
69
+ throw new Error("Session not found for user: " + userId);
70
+ if (!datagram.payload)
71
+ throw new Error("Missing payload");
72
+ const decrypted = yield session.decrypt(datagram.payload);
73
+ if (!decrypted)
74
+ throw new Error("Decryption failed");
75
+ return decrypted;
76
+ });
77
+ }
78
+ receive(datagram) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ if (datagram instanceof Uint8Array)
81
+ datagram = types_1.Datagram.from(datagram);
82
+ switch (datagram.protocol) {
83
+ case types_1.Protocols.HANDSHAKE:
84
+ if (!datagram.payload)
85
+ throw new Error("Missing payload");
86
+ const data = (0, utils_1.decodeData)(datagram.payload);
87
+ const { session, identityKey } = yield this.keyExchange.digestMessage(data);
88
+ this.sessions.set(types_1.UserId.fromKey(identityKey).toString(), session);
89
+ return;
90
+ case types_1.Protocols.MESSAGE:
91
+ return yield this.decrypt(datagram);
92
+ case types_1.Protocols.RELAY:
93
+ return (0, utils_1.decodeData)(yield this.decrypt(datagram));
94
+ case types_1.Protocols.DISCOVER:
95
+ return types_1.UserId.from((0, utils_1.decodeData)(yield this.decrypt(datagram)));
96
+ default:
97
+ throw new Error("Invalid protocol");
98
+ }
99
+ });
100
+ }
101
+ }
102
+ exports.FreeSignalNode = FreeSignalNode;
103
+ class SessionMap {
104
+ constructor(storage, maxSize = 50) {
105
+ this.storage = storage;
106
+ this.maxSize = maxSize;
107
+ this.cache = new Map();
108
+ }
109
+ set(key, value) {
110
+ this.cache.set(key, value);
111
+ return this.storage.set(key, value.toJSON());
112
+ }
113
+ get(key) {
114
+ return __awaiter(this, void 0, void 0, function* () {
115
+ const session = this.cache.get(key);
116
+ if (!session) {
117
+ const sessionData = yield this.storage.get(key);
118
+ if (!sessionData)
119
+ return undefined;
120
+ return double_ratchet_1.KeySession.from(sessionData, this.storage);
121
+ }
122
+ return session;
123
+ });
124
+ }
125
+ has(key) {
126
+ return __awaiter(this, void 0, void 0, function* () {
127
+ return this.cache.has(key) || (yield this.storage.has(key));
128
+ });
129
+ }
130
+ delete(key) {
131
+ return __awaiter(this, void 0, void 0, function* () {
132
+ return this.cache.delete(key) || (yield this.storage.delete(key));
133
+ });
134
+ }
135
+ clear() {
136
+ this.cache.clear();
137
+ return this.storage.clear();
138
+ }
139
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freesignal/protocol",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "Signal Protocol implementation in javascript",
5
5
  "license": "GPL-3.0-or-later",
6
6
  "author": "Christian Braghette",
@@ -12,13 +12,9 @@
12
12
  },
13
13
  "dependencies": {
14
14
  "@freesignal/crypto": "^0.3.0",
15
- "@freesignal/interfaces": "^0.1.1",
16
- "@freesignal/utils": "^1.2.0",
17
- "base64-js": "^1.5.1",
18
- "fflate": "^0.8.2",
19
- "js-sha3": "^0.9.3",
20
- "tweetnacl": "^1.0.3",
21
- "uuid": "^11.1.0"
15
+ "@freesignal/interfaces": "^0.2.0",
16
+ "@freesignal/utils": "^1.3.0",
17
+ "semaphore.ts": "^0.2.0"
22
18
  },
23
19
  "devDependencies": {
24
20
  "@types/node": "^24.2.1"
package/test.js CHANGED
@@ -1,30 +1,28 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
4
10
  };
5
11
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const _1 = require(".");
7
- const crypto_1 = __importDefault(require("@freesignal/crypto"));
8
12
  const utils_1 = require("@freesignal/utils");
9
- const bob = (0, _1.createKeyExchange)(crypto_1.default.EdDSA.keyPair().secretKey, crypto_1.default.ECDH.keyPair().secretKey);
10
- const alice = (0, _1.createKeyExchange)(crypto_1.default.EdDSA.keyPair().secretKey, crypto_1.default.ECDH.keyPair().secretKey);
11
- const bobmessage = bob.generateData();
12
- const { session: alicesession, message: aliceack } = alice.digestData(bobmessage);
13
- bob.digestMessage(aliceack).then(({ session: bobsession, identityKeys }) => {
14
- var _a;
15
- if (bobsession && identityKeys) {
16
- console.log("Session established successfully between Alice and Bob.");
17
- const datagram = _1.Datagram.create(bob.signatureKey, alice.signatureKey, _1.Protocols.MESSAGE, (_a = bobsession.encrypt((0, utils_1.encodeUTF8)("Hi Alice!"))) === null || _a === void 0 ? void 0 : _a.encode());
18
- //console.log(datagram.payload);
19
- const msg = datagram.encode();
20
- console.log((0, utils_1.decodeUTF8)(alicesession.decrypt(_1.Datagram.from(msg).payload)));
21
- if (alicesession.handshaked && bobsession.handshaked)
22
- console.log("Successfully handshaked");
23
- else
24
- console.log("Error during handshake");
25
- const longmsg = _1.Datagram.create(alice.signatureKey, bob.signatureKey, _1.Protocols.MESSAGE, alicesession.encrypt(new Uint8Array(1000000).fill(33).map(val => val + Math.floor(Math.random() * 93))));
26
- console.log(longmsg.encode().length);
27
- }
28
- else
29
- console.log("Error");
30
- });
13
+ const _1 = require(".");
14
+ const bob = (0, _1.createNode)({ keyExchange: new _1.AsyncMap(), sessions: new _1.AsyncMap(), users: new _1.AsyncMap() });
15
+ const alice = (0, _1.createNode)({ keyExchange: new _1.AsyncMap(), sessions: new _1.AsyncMap(), users: new _1.AsyncMap() });
16
+ setImmediate(() => __awaiter(void 0, void 0, void 0, function* () {
17
+ const aliceHandshake = yield alice.sendHandshake(yield bob.generateKeyData());
18
+ yield bob.receive(aliceHandshake);
19
+ console.log("Session established successfully between Alice and Bob.");
20
+ const first = (yield bob.sendData(alice.userId.toString(), (0, utils_1.encodeData)("Hi Alice!"))).toBytes();
21
+ console.log("Bob: ", (0, utils_1.decodeData)(yield alice.receive(first)));
22
+ const second = yield alice.sendData(bob.userId.toString(), (0, utils_1.encodeData)("Hi Bob!"));
23
+ console.log("Alice: ", (0, utils_1.decodeData)(yield bob.receive(second)));
24
+ const third = yield Promise.all(["How are you?", "How are this days?", "For me it's a good time"].map(msg => bob.sendData(alice.userId.toString(), (0, utils_1.encodeData)(msg))));
25
+ third.forEach((value) => __awaiter(void 0, void 0, void 0, function* () {
26
+ console.log("Bob: ", (0, utils_1.decodeData)(yield alice.receive(value)));
27
+ }));
28
+ }));
package/types.d.ts CHANGED
@@ -16,43 +16,47 @@
16
16
  * You should have received a copy of the GNU General Public License
17
17
  * along with this program. If not, see <https://www.gnu.org/licenses/>
18
18
  */
19
- import { Encodable } from "@freesignal/interfaces";
20
- export type UserId = string;
21
- export declare namespace UserId {
22
- class UserIdConstructor {
23
- private readonly array;
24
- constructor(array: Uint8Array);
25
- toString(): string;
26
- toJSON(): string;
27
- toUint8Array(): Uint8Array;
28
- }
29
- export function getUserId(publicKey: string | Uint8Array): UserIdConstructor;
30
- export function from(userId: string | Uint8Array): UserIdConstructor;
31
- export {};
19
+ import { LocalStorage, Encodable } from "@freesignal/interfaces";
20
+ export declare class UserId {
21
+ private readonly array;
22
+ private constructor();
23
+ toString(): string;
24
+ toJSON(): string;
25
+ toUint8Array(): Uint8Array;
26
+ static fromKey(identityKey: string | Uint8Array | IdentityKey): UserId;
27
+ static from(userId: string | Uint8Array | UserId): UserId;
28
+ }
29
+ export interface IdentityKey extends Encodable {
30
+ readonly info: number;
31
+ readonly signatureKey: Uint8Array;
32
+ readonly exchangeKey: Uint8Array;
33
+ }
34
+ export declare namespace IdentityKey {
35
+ const keyLength: number;
36
+ const version = 1;
37
+ function isIdentityKeys(obj: any): boolean;
38
+ function from(identityKey: IdentityKey | Uint8Array | string): IdentityKey;
39
+ function from(signatureKey: Uint8Array | string, exchangeKey: Uint8Array | string): IdentityKey;
32
40
  }
33
- export interface IdentityKeys {
34
- readonly publicKey: string;
35
- readonly identityKey: string;
41
+ export interface PrivateIdentityKey {
42
+ readonly info: number;
43
+ readonly signatureKey: Uint8Array;
44
+ readonly exchangeKey: Uint8Array;
45
+ readonly identityKey: IdentityKey;
36
46
  }
37
- export declare namespace IdentityKeys {
38
- export const keyLength: number;
39
- class IdentityKeysConstructor implements IdentityKeys, Encodable {
40
- readonly publicKey: string;
41
- readonly identityKey: string;
42
- constructor(identityKeys: IdentityKeys | Uint8Array | string);
43
- encode(): Uint8Array;
44
- toString(): string;
45
- toJSON(): string;
46
- }
47
- export function isIdentityKeys(obj: any): boolean;
48
- export function from(identityKeys: IdentityKeys): IdentityKeysConstructor;
49
- export {};
47
+ export declare namespace PrivateIdentityKey {
48
+ const keyLength: number;
49
+ const version = 1;
50
+ function isIdentityKeys(obj: any): boolean;
51
+ function from(identityKey: PrivateIdentityKey | Uint8Array | string): PrivateIdentityKey;
52
+ function from(signatureKey: Uint8Array | string, exchangeKey: Uint8Array | string): PrivateIdentityKey;
50
53
  }
51
54
  export declare enum Protocols {
52
55
  NULL = "",
53
56
  MESSAGE = "/freesignal/message",
54
57
  RELAY = "/freesignal/relay",
55
- HANDSHAKE = "/freesignal/handshake"
58
+ HANDSHAKE = "/freesignal/handshake",
59
+ DISCOVER = "/freesignal/discover"
56
60
  }
57
61
  export declare namespace Protocols {
58
62
  function isProtocol(protocol: any): boolean;
@@ -61,44 +65,31 @@ export declare namespace Protocols {
61
65
  function encode(protocol: Protocols, length?: number): Uint8Array;
62
66
  function decode(array: Uint8Array): Protocols;
63
67
  }
64
- export interface Datagram {
65
- readonly id: string;
66
- readonly version: number;
68
+ export declare class Datagram implements Encodable {
69
+ static version: number;
70
+ private _id;
71
+ private _version;
67
72
  readonly sender: string;
68
73
  readonly receiver: string;
69
74
  readonly protocol: Protocols;
70
- readonly createdAt: number;
71
- payload?: Uint8Array;
72
- readonly signature?: string;
73
- }
74
- export declare namespace Datagram {
75
- export const version = 1;
76
- class DatagramConstructor implements Encodable, Datagram {
77
- readonly id: string;
78
- readonly version: number;
79
- readonly sender: UserId;
80
- readonly receiver: UserId;
81
- readonly protocol: Protocols;
82
- readonly createdAt: number;
83
- _payload?: Uint8Array;
84
- _signature?: Uint8Array;
85
- private secretKey?;
86
- private static headerOffset;
87
- constructor(sender: Uint8Array | string, receiver: Uint8Array | string, protocol: Protocols, payload?: Uint8Array | Encodable);
88
- constructor(data: Uint8Array | Datagram);
89
- get signed(): boolean;
90
- get signature(): string | undefined;
91
- set payload(data: Uint8Array);
92
- get payload(): Uint8Array | undefined;
93
- encode(): Uint8Array;
94
- sign(secretKey: Uint8Array): this;
95
- toString(): string;
96
- toJSON(): string;
97
- }
98
- export function create(sender: Uint8Array | string, receiver: Uint8Array | string, protocol: Protocols, payload?: Uint8Array | Encodable): DatagramConstructor;
99
- export function isDatagram(obj: any): boolean;
100
- export function from(data: Uint8Array | Datagram | string): DatagramConstructor;
101
- export {};
75
+ private _createdAt;
76
+ private _payload?;
77
+ private _signature?;
78
+ private secretKey?;
79
+ private static headerOffset;
80
+ constructor(sender: Uint8Array | string, receiver: Uint8Array | string, protocol: Protocols, payload?: Uint8Array | Encodable);
81
+ get id(): string;
82
+ get version(): number;
83
+ get createdAt(): number;
84
+ get signed(): boolean;
85
+ get signature(): string | undefined;
86
+ set payload(data: Uint8Array);
87
+ get payload(): Uint8Array | undefined;
88
+ toBytes(): Uint8Array;
89
+ sign(secretKey: Uint8Array): this;
90
+ toString(): string;
91
+ toJSON(): string;
92
+ static from(data: Uint8Array | Datagram | string): Datagram;
102
93
  }
103
94
  /**
104
95
  * Interface representing an encrypted payload.
@@ -136,7 +127,7 @@ export interface EncryptedData extends Encodable {
136
127
  /**
137
128
  * Serializes the payload into a Uint8Array for transport.
138
129
  */
139
- encode(): Uint8Array;
130
+ toBytes(): Uint8Array;
140
131
  /**
141
132
  * Returns the payload as a Base64 string.
142
133
  */
@@ -162,74 +153,13 @@ export declare class EncryptedData {
162
153
  */
163
154
  static from(array: Uint8Array | EncryptedData): EncryptedData;
164
155
  }
165
- export declare class EncryptedDataConstructor implements EncryptedData {
166
- static readonly secretKeyLength: number;
167
- static readonly publicKeyLength: number;
168
- static readonly keyLength: number;
169
- static readonly nonceLength: number;
170
- static readonly maxCount = 65536;
171
- static readonly countLength = 2;
172
- private raw;
173
- constructor(count: number | Uint8Array, previous: number | Uint8Array, publicKey: Uint8Array, nonce: Uint8Array, ciphertext: Uint8Array, version?: number | Uint8Array);
174
- constructor(encrypted: Uint8Array | EncryptedData);
175
- get length(): number;
176
- get version(): number;
177
- get count(): number;
178
- get previous(): number;
179
- get publicKey(): Uint8Array;
180
- get nonce(): Uint8Array;
181
- get ciphertext(): Uint8Array;
182
- encode(): Uint8Array;
183
- toString(): string;
184
- toJSON(): {
185
- version: number;
186
- count: number;
187
- previous: number;
188
- publicKey: string;
189
- nonce: string;
190
- ciphertext: string;
191
- };
192
- }
193
- declare enum DataType {
194
- UKNOWN = -1,
195
- RAW = 0,
196
- NUMBER = 1,
197
- STRING = 2,
198
- ARRAY = 3,
199
- OBJECT = 4
200
- }
201
- declare namespace DataType {
202
- function getType(type: string): DataType;
203
- function getName(type: DataType): string;
204
- function from(data: any): DataType;
205
- }
206
- export declare class DataEncoder<T> implements Encodable {
207
- readonly data: T;
208
- readonly type: string;
209
- constructor(data: T);
210
- protected get _type(): DataType;
211
- encode(): Uint8Array;
212
- toString(): string;
213
- toJSON(): T;
214
- static from<T = any>(array: Uint8Array): DataEncoder<T>;
215
- }
216
- export declare namespace XFreeSignal {
217
- export const MIME = "application/x-freesignal";
218
- export const version = 1;
219
- export function encodeBody(type: 'data' | 'error', data: any, compressed?: boolean): BodyInit;
220
- export function decodeBody<T = any>(body: Uint8Array): Body<T>;
221
- class Body<T> implements Encodable {
222
- readonly type: 'data' | 'error';
223
- readonly data: T;
224
- constructor(type: 'data' | 'error', data: T);
225
- encode(compressed?: boolean): Uint8Array;
226
- toString(): string;
227
- toJSON(): {
228
- type: "data" | "error";
229
- data: T;
230
- };
231
- static from<T = any>(array: Uint8Array): Body<T>;
232
- }
233
- export {};
156
+ export declare class AsyncMap<K, V> implements LocalStorage<K, V> {
157
+ private readonly map;
158
+ constructor(iterable?: Iterable<readonly [K, V]>);
159
+ set(key: K, value: V): Promise<void>;
160
+ get(key: K): Promise<V | undefined>;
161
+ has(key: K): Promise<boolean>;
162
+ delete(key: K): Promise<boolean>;
163
+ clear(): Promise<void>;
164
+ entries(): ArrayIterator<[K, V]>;
234
165
  }
235
- export {};