@freesignal/protocol 0.2.9 → 0.2.11

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/test.js CHANGED
@@ -1,30 +1,25 @@
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 node_1 = require("./node");
15
+ const bob = new node_1.FreeSignalNode({ keyExchange: new _1.AsyncMap(), sessions: new _1.AsyncMap(), users: new _1.AsyncMap() });
16
+ const alice = new node_1.FreeSignalNode({ keyExchange: new _1.AsyncMap(), sessions: new _1.AsyncMap(), users: new _1.AsyncMap() });
17
+ setImmediate(() => __awaiter(void 0, void 0, void 0, function* () {
18
+ const aliceHandshake = yield alice.sendHandshake(yield bob.generateKeyData());
19
+ yield bob.receive(aliceHandshake);
20
+ console.log("Session established successfully between Alice and Bob.");
21
+ const data = (yield bob.encrypt(alice.userId.toString(), _1.Protocols.MESSAGE, (0, utils_1.encodeData)("Hi Alice!"))).toBytes();
22
+ console.log((0, utils_1.decodeData)(yield alice.receive(data)));
23
+ const longmsg = yield alice.sendData(bob.userId.toString(), new Uint8Array(1000000).fill(33).map(val => val + Math.floor(Math.random() * 93)));
24
+ console.log(longmsg.toBytes().length);
25
+ }));
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 {};