@freesignal/protocol 0.2.8 → 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/double-ratchet.d.ts +37 -4
- package/double-ratchet.js +133 -39
- package/index.d.ts +12 -15
- package/index.js +31 -20
- package/node.d.ts +27 -0
- package/node.js +105 -0
- package/package.json +3 -8
- package/test.js +21 -26
- package/types.d.ts +66 -136
- package/types.js +238 -366
- package/x3dh.d.ts +18 -19
- package/x3dh.js +75 -114
- package/api.d.ts +0 -54
- package/api.js +0 -195
package/types.js
CHANGED
|
@@ -17,82 +17,163 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
21
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
+
});
|
|
28
|
+
};
|
|
20
29
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
21
30
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
22
31
|
};
|
|
23
32
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
24
|
-
exports.
|
|
33
|
+
exports.AsyncMap = exports.EncryptedData = exports.Datagram = exports.Protocols = exports.PrivateIdentityKey = exports.IdentityKey = exports.UserId = void 0;
|
|
25
34
|
const utils_1 = require("@freesignal/utils");
|
|
26
35
|
const crypto_1 = __importDefault(require("@freesignal/crypto"));
|
|
27
36
|
const double_ratchet_1 = require("./double-ratchet");
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
class UserId {
|
|
38
|
+
constructor(array) {
|
|
39
|
+
this.array = array;
|
|
40
|
+
}
|
|
41
|
+
;
|
|
42
|
+
toString() {
|
|
43
|
+
return (0, utils_1.decodeBase64)(this.array);
|
|
44
|
+
}
|
|
45
|
+
toJSON() {
|
|
46
|
+
return this.toString();
|
|
47
|
+
}
|
|
48
|
+
toUint8Array() {
|
|
49
|
+
return this.array;
|
|
50
|
+
}
|
|
51
|
+
static fromKey(identityKey) {
|
|
52
|
+
if (typeof identityKey === 'string')
|
|
53
|
+
identityKey = (0, utils_1.encodeBase64)(identityKey);
|
|
54
|
+
else if (IdentityKey.isIdentityKeys(identityKey))
|
|
55
|
+
identityKey = identityKey.toBytes();
|
|
56
|
+
return new UserId(crypto_1.default.hash(identityKey));
|
|
57
|
+
}
|
|
58
|
+
static from(userId) {
|
|
59
|
+
if (typeof userId === 'string')
|
|
60
|
+
userId = (0, utils_1.encodeBase64)(userId);
|
|
61
|
+
return new UserId(userId instanceof Uint8Array ? userId : userId.array);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.UserId = UserId;
|
|
65
|
+
var IdentityKey;
|
|
66
|
+
(function (IdentityKey) {
|
|
67
|
+
IdentityKey.keyLength = crypto_1.default.EdDSA.publicKeyLength + crypto_1.default.ECDH.publicKeyLength + 1;
|
|
68
|
+
const info = 0x70;
|
|
69
|
+
IdentityKey.version = 1;
|
|
70
|
+
class IdentityKeyConstructor {
|
|
71
|
+
constructor(identityKey) {
|
|
72
|
+
if (identityKey instanceof IdentityKeyConstructor) {
|
|
73
|
+
this.info = identityKey.info;
|
|
74
|
+
this.signatureKey = identityKey.signatureKey;
|
|
75
|
+
this.exchangeKey = identityKey.exchangeKey;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
if (typeof identityKey === 'string')
|
|
79
|
+
identityKey = (0, utils_1.encodeBase64)(identityKey);
|
|
80
|
+
if (!isIdentityKeys(identityKey))
|
|
81
|
+
throw new Error("Invalid key length");
|
|
82
|
+
this.info = identityKey[0];
|
|
83
|
+
this.signatureKey = identityKey.subarray(1, crypto_1.default.EdDSA.publicKeyLength + 1);
|
|
84
|
+
this.exchangeKey = identityKey.subarray(crypto_1.default.EdDSA.publicKeyLength + 1, IdentityKey.keyLength);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
get userId() {
|
|
88
|
+
return UserId.fromKey(this.toBytes()).toString();
|
|
89
|
+
}
|
|
90
|
+
toBytes() {
|
|
91
|
+
return (0, utils_1.concatArrays)((0, utils_1.numberToArray)(this.info), this.signatureKey, this.exchangeKey);
|
|
34
92
|
}
|
|
35
|
-
;
|
|
36
93
|
toString() {
|
|
37
|
-
return (0, utils_1.decodeBase64)(this.
|
|
94
|
+
return (0, utils_1.decodeBase64)(this.toBytes());
|
|
38
95
|
}
|
|
39
96
|
toJSON() {
|
|
40
97
|
return this.toString();
|
|
41
98
|
}
|
|
42
|
-
toUint8Array() {
|
|
43
|
-
return this.array;
|
|
44
|
-
}
|
|
45
99
|
}
|
|
46
|
-
function
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function from(
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
100
|
+
function isIdentityKeys(obj) {
|
|
101
|
+
return (obj instanceof Uint8Array && obj.length === IdentityKey.keyLength) || obj instanceof IdentityKeyConstructor;
|
|
102
|
+
}
|
|
103
|
+
IdentityKey.isIdentityKeys = isIdentityKeys;
|
|
104
|
+
function from(...keys) {
|
|
105
|
+
keys = keys.map(key => {
|
|
106
|
+
if (key instanceof IdentityKeyConstructor)
|
|
107
|
+
return key.toBytes();
|
|
108
|
+
else if (typeof key === 'string')
|
|
109
|
+
return (0, utils_1.encodeBase64)(key);
|
|
110
|
+
else
|
|
111
|
+
return key;
|
|
112
|
+
});
|
|
113
|
+
return new IdentityKeyConstructor(keys.length === 2 ? (0, utils_1.concatArrays)((0, utils_1.numberToArray)(info + IdentityKey.version), ...keys) : keys[0]);
|
|
114
|
+
}
|
|
115
|
+
IdentityKey.from = from;
|
|
116
|
+
})(IdentityKey || (exports.IdentityKey = IdentityKey = {}));
|
|
117
|
+
var PrivateIdentityKey;
|
|
118
|
+
(function (PrivateIdentityKey) {
|
|
119
|
+
PrivateIdentityKey.keyLength = crypto_1.default.EdDSA.secretKeyLength + crypto_1.default.ECDH.secretKeyLength + 1;
|
|
120
|
+
const info = 0x4E;
|
|
121
|
+
PrivateIdentityKey.version = 1;
|
|
122
|
+
class PrivateIdentityKeyConstructor {
|
|
123
|
+
constructor(privateIdentityKey) {
|
|
124
|
+
this.info = info + PrivateIdentityKey.version;
|
|
125
|
+
if (privateIdentityKey instanceof PrivateIdentityKeyConstructor) {
|
|
126
|
+
this.signatureKey = privateIdentityKey.signatureKey;
|
|
127
|
+
this.exchangeKey = privateIdentityKey.exchangeKey;
|
|
128
|
+
this.identityKey = privateIdentityKey.identityKey;
|
|
65
129
|
}
|
|
66
130
|
else {
|
|
67
|
-
|
|
68
|
-
|
|
131
|
+
if (typeof privateIdentityKey === 'string')
|
|
132
|
+
privateIdentityKey = (0, utils_1.encodeBase64)(privateIdentityKey);
|
|
133
|
+
if (!isIdentityKeys(privateIdentityKey))
|
|
134
|
+
throw new Error("Invalid key length");
|
|
135
|
+
this.signatureKey = privateIdentityKey.subarray(1, crypto_1.default.EdDSA.secretKeyLength + 1);
|
|
136
|
+
this.exchangeKey = privateIdentityKey.subarray(crypto_1.default.EdDSA.secretKeyLength + 1, PrivateIdentityKey.keyLength);
|
|
137
|
+
this.identityKey = IdentityKey.from(crypto_1.default.EdDSA.keyPair(this.signatureKey).publicKey, crypto_1.default.ECDH.keyPair(this.exchangeKey).publicKey);
|
|
69
138
|
}
|
|
70
139
|
}
|
|
71
|
-
|
|
72
|
-
return
|
|
140
|
+
get userId() {
|
|
141
|
+
return UserId.fromKey(this.identityKey.toBytes()).toString();
|
|
142
|
+
}
|
|
143
|
+
toBytes() {
|
|
144
|
+
return (0, utils_1.concatArrays)((0, utils_1.numberToArray)(this.info), this.signatureKey, this.exchangeKey);
|
|
73
145
|
}
|
|
74
146
|
toString() {
|
|
75
|
-
|
|
147
|
+
return (0, utils_1.decodeBase64)(this.toBytes());
|
|
76
148
|
}
|
|
77
149
|
toJSON() {
|
|
78
|
-
|
|
150
|
+
return this.toString();
|
|
79
151
|
}
|
|
80
152
|
}
|
|
81
153
|
function isIdentityKeys(obj) {
|
|
82
|
-
return (
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function from(
|
|
86
|
-
|
|
154
|
+
return (obj instanceof Uint8Array && obj.length === PrivateIdentityKey.keyLength) || obj instanceof PrivateIdentityKeyConstructor;
|
|
155
|
+
}
|
|
156
|
+
PrivateIdentityKey.isIdentityKeys = isIdentityKeys;
|
|
157
|
+
function from(...keys) {
|
|
158
|
+
keys = keys.map(key => {
|
|
159
|
+
if (key instanceof PrivateIdentityKeyConstructor)
|
|
160
|
+
return key.toBytes();
|
|
161
|
+
else if (typeof key === 'string')
|
|
162
|
+
return (0, utils_1.encodeBase64)(key);
|
|
163
|
+
else
|
|
164
|
+
return key;
|
|
165
|
+
});
|
|
166
|
+
return new PrivateIdentityKeyConstructor(keys.length === 2 ? (0, utils_1.concatArrays)((0, utils_1.numberToArray)(info + PrivateIdentityKey.version), ...keys) : keys[0]);
|
|
87
167
|
}
|
|
88
|
-
|
|
89
|
-
})(
|
|
168
|
+
PrivateIdentityKey.from = from;
|
|
169
|
+
})(PrivateIdentityKey || (exports.PrivateIdentityKey = PrivateIdentityKey = {}));
|
|
90
170
|
var Protocols;
|
|
91
171
|
(function (Protocols) {
|
|
92
172
|
Protocols["NULL"] = "";
|
|
93
173
|
Protocols["MESSAGE"] = "/freesignal/message";
|
|
94
174
|
Protocols["RELAY"] = "/freesignal/relay";
|
|
95
175
|
Protocols["HANDSHAKE"] = "/freesignal/handshake";
|
|
176
|
+
Protocols["DISCOVER"] = "/freesignal/discover";
|
|
96
177
|
})(Protocols || (exports.Protocols = Protocols = {}));
|
|
97
178
|
(function (Protocols) {
|
|
98
179
|
function isProtocol(protocol) {
|
|
@@ -116,109 +197,92 @@ var Protocols;
|
|
|
116
197
|
}
|
|
117
198
|
Protocols.decode = decode;
|
|
118
199
|
})(Protocols || (exports.Protocols = Protocols = {}));
|
|
119
|
-
|
|
120
|
-
(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
else
|
|
148
|
-
throw new Error('Invalid constructor arguments for Datagram');
|
|
149
|
-
}
|
|
150
|
-
else if (typeof data === 'string' || data instanceof Uint8Array) {
|
|
151
|
-
this.id = crypto_1.default.UUID.generate().toString();
|
|
152
|
-
this.version = Datagram.version;
|
|
153
|
-
this.sender = typeof data === 'string' ? data : (0, utils_1.decodeBase64)(data);
|
|
154
|
-
this.receiver = typeof receiver === 'string' ? receiver : (0, utils_1.decodeBase64)(receiver);
|
|
155
|
-
this.protocol = protocol;
|
|
156
|
-
this.createdAt = Date.now();
|
|
157
|
-
this._payload = payload instanceof Uint8Array ? payload : payload === null || payload === void 0 ? void 0 : payload.encode();
|
|
158
|
-
}
|
|
159
|
-
else
|
|
160
|
-
throw new Error('Invalid constructor arguments for Datagram');
|
|
161
|
-
}
|
|
162
|
-
get signed() {
|
|
163
|
-
return !this._signature && !this.secretKey ? false : true;
|
|
164
|
-
}
|
|
165
|
-
get signature() {
|
|
166
|
-
if (this.signed) {
|
|
167
|
-
if (!this._signature)
|
|
168
|
-
this.encode();
|
|
169
|
-
return (0, utils_1.decodeBase64)(this._signature);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
set payload(data) {
|
|
173
|
-
this._signature = undefined;
|
|
174
|
-
this._payload = data;
|
|
175
|
-
}
|
|
176
|
-
get payload() {
|
|
177
|
-
return this._payload;
|
|
178
|
-
}
|
|
179
|
-
encode() {
|
|
180
|
-
var _a, _b, _c;
|
|
181
|
-
const data = (0, utils_1.concatArrays)(new Uint8Array(1).fill(this.version | (this.secretKey ? 128 : 0)), //1
|
|
182
|
-
Protocols.encode(this.protocol), //1
|
|
183
|
-
(_a = crypto_1.default.UUID.parse(this.id)) !== null && _a !== void 0 ? _a : [], //16
|
|
184
|
-
(0, utils_1.numberToArray)(this.createdAt, 8), //8
|
|
185
|
-
(0, utils_1.encodeBase64)(this.sender), //32
|
|
186
|
-
(0, utils_1.encodeBase64)(this.receiver), //32
|
|
187
|
-
(_b = this._payload) !== null && _b !== void 0 ? _b : new Uint8Array());
|
|
188
|
-
if (this.secretKey)
|
|
189
|
-
this._signature = crypto_1.default.EdDSA.sign(data, this.secretKey);
|
|
190
|
-
return (0, utils_1.concatArrays)(data, (_c = this._signature) !== null && _c !== void 0 ? _c : new Uint8Array());
|
|
191
|
-
}
|
|
192
|
-
sign(secretKey) {
|
|
193
|
-
this.secretKey = secretKey;
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
toString() {
|
|
197
|
-
return (0, utils_1.decodeBase64)(this.encode());
|
|
198
|
-
}
|
|
199
|
-
toJSON() {
|
|
200
|
-
return this.toString();
|
|
200
|
+
class Datagram {
|
|
201
|
+
constructor(sender, receiver, protocol, payload) {
|
|
202
|
+
this._id = crypto_1.default.UUID.generate().toString();
|
|
203
|
+
this._version = Datagram.version;
|
|
204
|
+
this.sender = typeof sender === 'string' ? sender : (0, utils_1.decodeBase64)(sender);
|
|
205
|
+
this.receiver = typeof receiver === 'string' ? receiver : (0, utils_1.decodeBase64)(receiver);
|
|
206
|
+
this.protocol = protocol;
|
|
207
|
+
this._createdAt = Date.now();
|
|
208
|
+
this._payload = payload instanceof Uint8Array ? payload : payload === null || payload === void 0 ? void 0 : payload.toBytes();
|
|
209
|
+
}
|
|
210
|
+
get id() {
|
|
211
|
+
return this._id;
|
|
212
|
+
}
|
|
213
|
+
get version() {
|
|
214
|
+
return this._version;
|
|
215
|
+
}
|
|
216
|
+
get createdAt() {
|
|
217
|
+
return this._createdAt;
|
|
218
|
+
}
|
|
219
|
+
get signed() {
|
|
220
|
+
return !!this._signature || !!this.secretKey;
|
|
221
|
+
}
|
|
222
|
+
get signature() {
|
|
223
|
+
if (this.signed) {
|
|
224
|
+
if (!this._signature)
|
|
225
|
+
this.toBytes();
|
|
226
|
+
return (0, utils_1.decodeBase64)(this._signature);
|
|
201
227
|
}
|
|
202
228
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
229
|
+
set payload(data) {
|
|
230
|
+
this._signature = undefined;
|
|
231
|
+
this._payload = data;
|
|
232
|
+
}
|
|
233
|
+
get payload() {
|
|
234
|
+
return this._payload;
|
|
235
|
+
}
|
|
236
|
+
toBytes() {
|
|
237
|
+
var _a, _b, _c;
|
|
238
|
+
const data = (0, utils_1.concatArrays)(new Uint8Array(1).fill(this.version | (this.secretKey ? 128 : 0)), //1
|
|
239
|
+
Protocols.encode(this.protocol), //1
|
|
240
|
+
(_a = crypto_1.default.UUID.parse(this.id)) !== null && _a !== void 0 ? _a : [], //16
|
|
241
|
+
(0, utils_1.numberToArray)(this.createdAt, 8), //8
|
|
242
|
+
(0, utils_1.encodeBase64)(this.sender), //32
|
|
243
|
+
(0, utils_1.encodeBase64)(this.receiver), //32
|
|
244
|
+
(_b = this._payload) !== null && _b !== void 0 ? _b : new Uint8Array());
|
|
245
|
+
if (this.secretKey)
|
|
246
|
+
this._signature = crypto_1.default.EdDSA.sign(data, this.secretKey);
|
|
247
|
+
return (0, utils_1.concatArrays)(data, (_c = this._signature) !== null && _c !== void 0 ? _c : new Uint8Array());
|
|
248
|
+
}
|
|
249
|
+
sign(secretKey) {
|
|
250
|
+
this.secretKey = secretKey;
|
|
251
|
+
return this;
|
|
206
252
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
return obj instanceof DatagramConstructor || (obj && typeof obj === 'object' && 'id' in obj && 'version' in obj && 'sender' in obj && 'receiver' in obj && 'protocol' in obj && 'createdAt' in obj);
|
|
253
|
+
toString() {
|
|
254
|
+
return (0, utils_1.decodeBase64)(this.toBytes());
|
|
210
255
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
256
|
+
toJSON() {
|
|
257
|
+
return this.toString();
|
|
258
|
+
}
|
|
259
|
+
static from(data) {
|
|
260
|
+
if (typeof data === 'string')
|
|
261
|
+
data = (0, utils_1.encodeBase64)(data);
|
|
262
|
+
if (data instanceof Uint8Array) {
|
|
263
|
+
const datagram = new Datagram((0, utils_1.decodeBase64)(data.subarray(26, 26 + crypto_1.default.EdDSA.publicKeyLength)), (0, utils_1.decodeBase64)(data.subarray(26 + crypto_1.default.EdDSA.publicKeyLength, Datagram.headerOffset)), Protocols.decode(data.subarray(1, 2)), data.subarray(Datagram.headerOffset, data.length));
|
|
264
|
+
datagram._version = data[0] & 127;
|
|
265
|
+
datagram._id = crypto_1.default.UUID.stringify(data.subarray(2, 18));
|
|
266
|
+
datagram._createdAt = (0, utils_1.numberFromArray)(data.subarray(18, 26));
|
|
267
|
+
if (data[0] & 128)
|
|
268
|
+
datagram._signature = data.subarray(data.length - crypto_1.default.EdDSA.signatureLength);
|
|
269
|
+
return datagram;
|
|
270
|
+
}
|
|
271
|
+
else if (data instanceof Datagram) {
|
|
272
|
+
const datagram = new Datagram(data.sender, data.receiver, data.protocol, data.payload);
|
|
273
|
+
datagram._id = datagram.id;
|
|
274
|
+
datagram._version = datagram.version;
|
|
275
|
+
datagram._createdAt = datagram.createdAt;
|
|
276
|
+
datagram._signature = datagram.signature ? (0, utils_1.encodeBase64)(datagram.signature) : undefined;
|
|
277
|
+
return datagram;
|
|
216
278
|
}
|
|
217
279
|
else
|
|
218
|
-
|
|
280
|
+
throw new Error('Invalid constructor arguments for Datagram');
|
|
219
281
|
}
|
|
220
|
-
|
|
221
|
-
|
|
282
|
+
}
|
|
283
|
+
exports.Datagram = Datagram;
|
|
284
|
+
Datagram.version = 1;
|
|
285
|
+
Datagram.headerOffset = 26 + crypto_1.default.EdDSA.publicKeyLength * 2;
|
|
222
286
|
class EncryptedData {
|
|
223
287
|
/**
|
|
224
288
|
* Static factory method that constructs an `EncryptedPayload` from a raw Uint8Array.
|
|
@@ -227,234 +291,42 @@ class EncryptedData {
|
|
|
227
291
|
* @returns An instance of `EncryptedPayload`.
|
|
228
292
|
*/
|
|
229
293
|
static from(array) {
|
|
230
|
-
return new EncryptedDataConstructor(array);
|
|
294
|
+
return new double_ratchet_1.EncryptedDataConstructor(array);
|
|
231
295
|
}
|
|
232
296
|
}
|
|
233
297
|
exports.EncryptedData = EncryptedData;
|
|
234
|
-
class
|
|
235
|
-
constructor(
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
return
|
|
266
|
-
}
|
|
267
|
-
toJSON() {
|
|
268
|
-
return {
|
|
269
|
-
version: this.version,
|
|
270
|
-
count: this.count,
|
|
271
|
-
previous: this.previous,
|
|
272
|
-
publicKey: (0, utils_1.decodeBase64)(this.publicKey),
|
|
273
|
-
nonce: (0, utils_1.decodeBase64)(this.nonce),
|
|
274
|
-
ciphertext: (0, utils_1.decodeBase64)(this.ciphertext)
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
exports.EncryptedDataConstructor = EncryptedDataConstructor;
|
|
279
|
-
EncryptedDataConstructor.secretKeyLength = crypto_1.default.ECDH.secretKeyLength;
|
|
280
|
-
EncryptedDataConstructor.publicKeyLength = crypto_1.default.ECDH.publicKeyLength;
|
|
281
|
-
EncryptedDataConstructor.keyLength = crypto_1.default.box.keyLength;
|
|
282
|
-
EncryptedDataConstructor.nonceLength = crypto_1.default.box.nonceLength;
|
|
283
|
-
EncryptedDataConstructor.maxCount = 65536; //32768;
|
|
284
|
-
EncryptedDataConstructor.countLength = 2;
|
|
285
|
-
class Offsets {
|
|
286
|
-
static set(start, length) {
|
|
287
|
-
class Offset {
|
|
288
|
-
constructor(start, length) {
|
|
289
|
-
this.start = start;
|
|
290
|
-
this.length = length;
|
|
291
|
-
if (typeof length === 'number')
|
|
292
|
-
this.end = start + length;
|
|
293
|
-
}
|
|
294
|
-
get get() {
|
|
295
|
-
return [this.start, this.length];
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return new Offset(start, length);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
Offsets.checksum = Offsets.set(0, 0);
|
|
302
|
-
Offsets.version = Offsets.set(Offsets.checksum.end, 1);
|
|
303
|
-
Offsets.count = Offsets.set(Offsets.version.end, EncryptedDataConstructor.countLength);
|
|
304
|
-
Offsets.previous = Offsets.set(Offsets.count.end, EncryptedDataConstructor.countLength);
|
|
305
|
-
Offsets.publicKey = Offsets.set(Offsets.previous.end, EncryptedDataConstructor.publicKeyLength);
|
|
306
|
-
Offsets.nonce = Offsets.set(Offsets.publicKey.end, EncryptedDataConstructor.nonceLength);
|
|
307
|
-
Offsets.ciphertext = Offsets.set(Offsets.nonce.end, undefined);
|
|
308
|
-
var DataType;
|
|
309
|
-
(function (DataType) {
|
|
310
|
-
DataType[DataType["UKNOWN"] = -1] = "UKNOWN";
|
|
311
|
-
DataType[DataType["RAW"] = 0] = "RAW";
|
|
312
|
-
DataType[DataType["NUMBER"] = 1] = "NUMBER";
|
|
313
|
-
DataType[DataType["STRING"] = 2] = "STRING";
|
|
314
|
-
DataType[DataType["ARRAY"] = 3] = "ARRAY";
|
|
315
|
-
DataType[DataType["OBJECT"] = 4] = "OBJECT";
|
|
316
|
-
})(DataType || (DataType = {}));
|
|
317
|
-
(function (DataType) {
|
|
318
|
-
function getType(type) {
|
|
319
|
-
return Object.values(DataType).indexOf(type.toLocaleUpperCase());
|
|
320
|
-
}
|
|
321
|
-
DataType.getType = getType;
|
|
322
|
-
function getName(type) {
|
|
323
|
-
return DataType[type].toLowerCase();
|
|
324
|
-
}
|
|
325
|
-
DataType.getName = getName;
|
|
326
|
-
function from(data) {
|
|
327
|
-
if (data instanceof Uint8Array)
|
|
328
|
-
return DataType.RAW;
|
|
329
|
-
return getType(typeof data);
|
|
330
|
-
}
|
|
331
|
-
DataType.from = from;
|
|
332
|
-
})(DataType || (DataType = {}));
|
|
333
|
-
class DataEncoder {
|
|
334
|
-
constructor(data) {
|
|
335
|
-
this.data = data;
|
|
336
|
-
this.type = DataType.getName(DataType.from(this.data));
|
|
337
|
-
}
|
|
338
|
-
get _type() {
|
|
339
|
-
return DataType.getType(this.type);
|
|
340
|
-
}
|
|
341
|
-
encode() {
|
|
342
|
-
let data;
|
|
343
|
-
switch (this._type) {
|
|
344
|
-
case DataType.RAW:
|
|
345
|
-
data = this.data;
|
|
346
|
-
break;
|
|
347
|
-
case DataType.NUMBER:
|
|
348
|
-
data = (0, utils_1.numberToArray)(this._type);
|
|
349
|
-
break;
|
|
350
|
-
case DataType.STRING:
|
|
351
|
-
data = (0, utils_1.encodeUTF8)(this.data);
|
|
352
|
-
break;
|
|
353
|
-
case DataType.ARRAY:
|
|
354
|
-
data = (0, utils_1.concatArrays)(...Array.from(this.data).flatMap(value => {
|
|
355
|
-
const data = new DataEncoder(value).encode();
|
|
356
|
-
return [(0, utils_1.numberToArray)(data.length, 8), data];
|
|
357
|
-
}));
|
|
358
|
-
break;
|
|
359
|
-
case DataType.OBJECT:
|
|
360
|
-
data = (0, utils_1.encodeJSON)(this.data);
|
|
361
|
-
break;
|
|
362
|
-
default:
|
|
363
|
-
throw new Error("Uknown type");
|
|
364
|
-
}
|
|
365
|
-
return (0, utils_1.concatArrays)((0, utils_1.numberToArray)(this._type), data);
|
|
366
|
-
}
|
|
367
|
-
toString() {
|
|
368
|
-
return "[Object XFreeSignalData]";
|
|
369
|
-
}
|
|
370
|
-
toJSON() {
|
|
371
|
-
return this.data;
|
|
372
|
-
}
|
|
373
|
-
static from(array) {
|
|
374
|
-
const type = array[0];
|
|
375
|
-
let rawData = array.subarray(1), data;
|
|
376
|
-
switch (type) {
|
|
377
|
-
case DataType.RAW:
|
|
378
|
-
data = rawData;
|
|
379
|
-
break;
|
|
380
|
-
case DataType.NUMBER:
|
|
381
|
-
data = (0, utils_1.numberFromArray)(rawData);
|
|
382
|
-
break;
|
|
383
|
-
case DataType.STRING:
|
|
384
|
-
data = (0, utils_1.decodeUTF8)(rawData);
|
|
385
|
-
break;
|
|
386
|
-
case DataType.ARRAY:
|
|
387
|
-
const arrayData = [];
|
|
388
|
-
let offset = 0;
|
|
389
|
-
while (offset < rawData.length) {
|
|
390
|
-
const length = rawData.subarray(offset, offset + 8);
|
|
391
|
-
if (length.length < 8)
|
|
392
|
-
throw new Error('Invalid data length');
|
|
393
|
-
const messageLength = (0, utils_1.numberFromArray)(length);
|
|
394
|
-
offset += 8;
|
|
395
|
-
if (offset + messageLength > rawData.length) {
|
|
396
|
-
throw new Error('Invalid data length');
|
|
397
|
-
}
|
|
398
|
-
arrayData.push(rawData.subarray(offset, offset + messageLength));
|
|
399
|
-
offset += messageLength;
|
|
400
|
-
}
|
|
401
|
-
data = arrayData;
|
|
402
|
-
break;
|
|
403
|
-
case DataType.OBJECT:
|
|
404
|
-
data = (0, utils_1.decodeJSON)(rawData);
|
|
405
|
-
break;
|
|
406
|
-
default:
|
|
407
|
-
throw new Error('Invalid data format');
|
|
408
|
-
}
|
|
409
|
-
return new DataEncoder(data);
|
|
298
|
+
class AsyncMap {
|
|
299
|
+
constructor(iterable) {
|
|
300
|
+
this.map = new Map(iterable);
|
|
301
|
+
}
|
|
302
|
+
set(key, value) {
|
|
303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
this.map.set(key, value);
|
|
305
|
+
return;
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
get(key) {
|
|
309
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
310
|
+
return this.map.get(key);
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
has(key) {
|
|
314
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
315
|
+
return this.map.has(key);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
delete(key) {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
320
|
+
return this.map.delete(key);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
clear() {
|
|
324
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
return this.map.clear();
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
entries() {
|
|
329
|
+
return Array.from(this.map.entries()).values();
|
|
410
330
|
}
|
|
411
331
|
}
|
|
412
|
-
exports.
|
|
413
|
-
var XFreeSignal;
|
|
414
|
-
(function (XFreeSignal) {
|
|
415
|
-
XFreeSignal.MIME = "application/x-freesignal";
|
|
416
|
-
XFreeSignal.version = 1;
|
|
417
|
-
function encodeBody(type, data, compressed = false) {
|
|
418
|
-
return new Body(type, data).encode(compressed);
|
|
419
|
-
}
|
|
420
|
-
XFreeSignal.encodeBody = encodeBody;
|
|
421
|
-
function decodeBody(body) {
|
|
422
|
-
return Body.from(body);
|
|
423
|
-
}
|
|
424
|
-
XFreeSignal.decodeBody = decodeBody;
|
|
425
|
-
let BodyType;
|
|
426
|
-
(function (BodyType) {
|
|
427
|
-
BodyType[BodyType["DATA"] = 0] = "DATA";
|
|
428
|
-
BodyType[BodyType["ERROR"] = 1] = "ERROR";
|
|
429
|
-
})(BodyType || (BodyType = {}));
|
|
430
|
-
(function (BodyType) {
|
|
431
|
-
function getName(type) {
|
|
432
|
-
return BodyType[type].toLowerCase();
|
|
433
|
-
}
|
|
434
|
-
BodyType.getName = getName;
|
|
435
|
-
})(BodyType || (BodyType = {}));
|
|
436
|
-
class Body {
|
|
437
|
-
constructor(type, data) {
|
|
438
|
-
this.type = type;
|
|
439
|
-
this.data = data;
|
|
440
|
-
}
|
|
441
|
-
encode(compressed = false) {
|
|
442
|
-
const data = new DataEncoder(this.data).encode();
|
|
443
|
-
return (0, utils_1.concatArrays)((0, utils_1.numberToArray)(((this.type === 'data' ? BodyType.DATA : BodyType.ERROR) << 6)
|
|
444
|
-
+ (compressed ? 32 : 0)
|
|
445
|
-
+ XFreeSignal.version), compressed ? fflate_1.default.deflateSync(data) : data);
|
|
446
|
-
}
|
|
447
|
-
toString() {
|
|
448
|
-
return "[Object XFreeSignalBody]";
|
|
449
|
-
}
|
|
450
|
-
toJSON() {
|
|
451
|
-
return {
|
|
452
|
-
type: this.type,
|
|
453
|
-
data: this.data
|
|
454
|
-
};
|
|
455
|
-
}
|
|
456
|
-
static from(array) {
|
|
457
|
-
return new Body(BodyType.getName((array[0] & 64) >> 6), DataEncoder.from((array[0] & 32) >> 5 === 1 ? fflate_1.default.inflateSync(array.subarray(1)) : array.subarray(1)).data);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
})(XFreeSignal || (exports.XFreeSignal = XFreeSignal = {}));
|
|
332
|
+
exports.AsyncMap = AsyncMap;
|