@peerbit/crypto 2.3.13 → 2.4.1-e209d2e
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/dist/benchmark/hash.js +68 -27
- package/dist/benchmark/hash.js.map +1 -1
- package/dist/src/bytes.d.ts.map +1 -1
- package/dist/src/bytes.js +4 -2
- package/dist/src/bytes.js.map +1 -1
- package/dist/src/ed25519.js +209 -144
- package/dist/src/ed25519.js.map +1 -1
- package/dist/src/encryption.d.ts.map +1 -1
- package/dist/src/encryption.js +417 -295
- package/dist/src/encryption.js.map +1 -1
- package/dist/src/key.js +60 -25
- package/dist/src/key.js.map +1 -1
- package/dist/src/random.d.ts +1 -1
- package/dist/src/random.d.ts.map +1 -1
- package/dist/src/sepc256k1.js +202 -136
- package/dist/src/sepc256k1.js.map +1 -1
- package/dist/src/signature.js +139 -86
- package/dist/src/signature.js.map +1 -1
- package/dist/src/x25519.js +208 -141
- package/dist/src/x25519.js.map +1 -1
- package/package.json +5 -5
- package/src/bytes.ts +6 -2
- package/src/encryption.ts +3 -1
package/dist/src/encryption.js
CHANGED
|
@@ -1,13 +1,37 @@
|
|
|
1
|
-
var
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
|
|
2
|
+
function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
|
|
3
|
+
var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
|
|
4
|
+
var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
|
|
5
|
+
var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
|
|
6
|
+
var _, done = false;
|
|
7
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
8
|
+
var context = {};
|
|
9
|
+
for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
|
|
10
|
+
for (var p in contextIn.access) context.access[p] = contextIn.access[p];
|
|
11
|
+
context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
|
|
12
|
+
var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
|
|
13
|
+
if (kind === "accessor") {
|
|
14
|
+
if (result === void 0) continue;
|
|
15
|
+
if (result === null || typeof result !== "object") throw new TypeError("Object expected");
|
|
16
|
+
if (_ = accept(result.get)) descriptor.get = _;
|
|
17
|
+
if (_ = accept(result.set)) descriptor.set = _;
|
|
18
|
+
if (_ = accept(result.init)) initializers.unshift(_);
|
|
19
|
+
}
|
|
20
|
+
else if (_ = accept(result)) {
|
|
21
|
+
if (kind === "field") initializers.unshift(_);
|
|
22
|
+
else descriptor[key] = _;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (target) Object.defineProperty(target, contextIn.name, descriptor);
|
|
26
|
+
done = true;
|
|
6
27
|
};
|
|
7
|
-
var
|
|
8
|
-
|
|
28
|
+
var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
|
|
29
|
+
var useValue = arguments.length > 2;
|
|
30
|
+
for (var i = 0; i < initializers.length; i++) {
|
|
31
|
+
value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
|
|
32
|
+
}
|
|
33
|
+
return useValue ? value : void 0;
|
|
9
34
|
};
|
|
10
|
-
var DecryptedThing_1, CipherWithNonce_1, K_1, PublicKeyEnvelope_1, HashedKeyEnvelope_1, EncryptedThing_1;
|
|
11
35
|
import { deserialize, field, fixedArray, serialize, variant, vec, } from "@dao-xyz/borsh";
|
|
12
36
|
import sodium from "libsodium-wrappers";
|
|
13
37
|
import { equals as uequals } from "uint8arrays";
|
|
@@ -18,8 +42,9 @@ import { randomBytes } from "./random.js";
|
|
|
18
42
|
import { X25519Keypair, X25519PublicKey, X25519SecretKey } from "./x25519.js";
|
|
19
43
|
export * from "./errors.js";
|
|
20
44
|
export const equals = (array1, array2) => {
|
|
21
|
-
if (!!array1 !== !!array2)
|
|
45
|
+
if (!!array1 !== !!array2) {
|
|
22
46
|
return false;
|
|
47
|
+
}
|
|
23
48
|
if (!array1 || !array2) {
|
|
24
49
|
return false;
|
|
25
50
|
}
|
|
@@ -124,327 +149,424 @@ export const createDecrypterFromKeyResolver = (keyResolver) => {
|
|
|
124
149
|
};
|
|
125
150
|
};
|
|
126
151
|
const NONCE_LENGTH = 24;
|
|
127
|
-
let MaybeEncrypted =
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Clear cached data
|
|
142
|
-
*/
|
|
143
|
-
clear() {
|
|
144
|
-
throw new Error("Not implemented");
|
|
145
|
-
}
|
|
146
|
-
};
|
|
147
|
-
MaybeEncrypted = __decorate([
|
|
148
|
-
variant(0)
|
|
149
|
-
], MaybeEncrypted);
|
|
150
|
-
export { MaybeEncrypted };
|
|
151
|
-
let DecryptedThing = DecryptedThing_1 = class DecryptedThing extends MaybeEncrypted {
|
|
152
|
-
_data;
|
|
153
|
-
constructor(props) {
|
|
154
|
-
super();
|
|
155
|
-
if (props) {
|
|
156
|
-
this._data = props.data;
|
|
157
|
-
this._value = props.value;
|
|
152
|
+
let MaybeEncrypted = (() => {
|
|
153
|
+
let _classDecorators = [variant(0)];
|
|
154
|
+
let _classDescriptor;
|
|
155
|
+
let _classExtraInitializers = [];
|
|
156
|
+
let _classThis;
|
|
157
|
+
var MaybeEncrypted = class {
|
|
158
|
+
static { _classThis = this; }
|
|
159
|
+
static {
|
|
160
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
161
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
162
|
+
MaybeEncrypted = _classThis = _classDescriptor.value;
|
|
163
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
164
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
158
165
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Will throw error if not decrypted
|
|
168
|
+
*/
|
|
169
|
+
get decrypted() {
|
|
170
|
+
throw new Error("Not implented");
|
|
164
171
|
}
|
|
165
|
-
|
|
166
|
-
throw new Error("
|
|
172
|
+
decrypt(provider) {
|
|
173
|
+
throw new Error("Not implemented");
|
|
167
174
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
async encrypt(keypair, parameters) {
|
|
171
|
-
let provider;
|
|
172
|
-
let options;
|
|
173
|
-
if (keypair instanceof X25519Keypair) {
|
|
174
|
-
provider = createLocalEncryptProvider(keypair);
|
|
175
|
-
options = {
|
|
176
|
-
receiverPublicKeys: parameters,
|
|
177
|
-
type: "publicKey",
|
|
178
|
-
};
|
|
175
|
+
equals(other) {
|
|
176
|
+
throw new Error("Not implemented");
|
|
179
177
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const { cipher, envelope, nonce } = await provider(bytes, options);
|
|
186
|
-
const enc = new EncryptedThing({
|
|
187
|
-
encrypted: cipher,
|
|
188
|
-
envelope,
|
|
189
|
-
nonce,
|
|
190
|
-
});
|
|
191
|
-
enc._decrypted = this;
|
|
192
|
-
return enc;
|
|
193
|
-
}
|
|
194
|
-
get decrypted() {
|
|
195
|
-
return this;
|
|
196
|
-
}
|
|
197
|
-
decrypt() {
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
equals(other) {
|
|
201
|
-
if (other instanceof DecryptedThing_1) {
|
|
202
|
-
return equals(this._data, other._data);
|
|
178
|
+
/**
|
|
179
|
+
* Clear cached data
|
|
180
|
+
*/
|
|
181
|
+
clear() {
|
|
182
|
+
throw new Error("Not implemented");
|
|
203
183
|
}
|
|
204
|
-
|
|
205
|
-
|
|
184
|
+
};
|
|
185
|
+
return MaybeEncrypted = _classThis;
|
|
186
|
+
})();
|
|
187
|
+
export { MaybeEncrypted };
|
|
188
|
+
let DecryptedThing = (() => {
|
|
189
|
+
let _classDecorators = [variant(0)];
|
|
190
|
+
let _classDescriptor;
|
|
191
|
+
let _classExtraInitializers = [];
|
|
192
|
+
let _classThis;
|
|
193
|
+
let _classSuper = MaybeEncrypted;
|
|
194
|
+
let __data_decorators;
|
|
195
|
+
let __data_initializers = [];
|
|
196
|
+
let __data_extraInitializers = [];
|
|
197
|
+
var DecryptedThing = class extends _classSuper {
|
|
198
|
+
static { _classThis = this; }
|
|
199
|
+
static {
|
|
200
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
201
|
+
__data_decorators = [field({ type: Uint8Array })];
|
|
202
|
+
__esDecorate(null, null, __data_decorators, { kind: "field", name: "_data", static: false, private: false, access: { has: obj => "_data" in obj, get: obj => obj._data, set: (obj, value) => { obj._data = value; } }, metadata: _metadata }, __data_initializers, __data_extraInitializers);
|
|
203
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
204
|
+
DecryptedThing = _classThis = _classDescriptor.value;
|
|
205
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
206
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
206
207
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
};
|
|
215
|
-
__decorate([
|
|
216
|
-
field({ type: Uint8Array }),
|
|
217
|
-
__metadata("design:type", Uint8Array)
|
|
218
|
-
], DecryptedThing.prototype, "_data", void 0);
|
|
219
|
-
DecryptedThing = DecryptedThing_1 = __decorate([
|
|
220
|
-
variant(0),
|
|
221
|
-
__metadata("design:paramtypes", [Object])
|
|
222
|
-
], DecryptedThing);
|
|
223
|
-
export { DecryptedThing };
|
|
224
|
-
let CipherWithNonce = CipherWithNonce_1 = class CipherWithNonce {
|
|
225
|
-
nonce;
|
|
226
|
-
cipher;
|
|
227
|
-
constructor(props) {
|
|
228
|
-
this.nonce = props.nonce;
|
|
229
|
-
this.cipher = props.cipher;
|
|
230
|
-
}
|
|
231
|
-
equals(other) {
|
|
232
|
-
if (other instanceof CipherWithNonce_1) {
|
|
233
|
-
return (equals(this.nonce, other.nonce) && equals(this.cipher, other.cipher));
|
|
208
|
+
_data = __runInitializers(this, __data_initializers, void 0);
|
|
209
|
+
constructor(props) {
|
|
210
|
+
super();
|
|
211
|
+
if (props) {
|
|
212
|
+
this._data = props.data;
|
|
213
|
+
this._value = props.value;
|
|
214
|
+
}
|
|
234
215
|
}
|
|
235
|
-
|
|
236
|
-
|
|
216
|
+
_value = __runInitializers(this, __data_extraInitializers);
|
|
217
|
+
getValue(clazz) {
|
|
218
|
+
if (this._value) {
|
|
219
|
+
return this._value;
|
|
220
|
+
}
|
|
221
|
+
if (!this._data) {
|
|
222
|
+
throw new Error("Missing data");
|
|
223
|
+
}
|
|
224
|
+
return deserialize(this._data, clazz);
|
|
237
225
|
}
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (other instanceof K_1) {
|
|
262
|
-
return (this._encryptedKey.equals(other._encryptedKey) &&
|
|
263
|
-
this._receiverPublicKey.equals(other._receiverPublicKey));
|
|
226
|
+
async encrypt(keypair, parameters) {
|
|
227
|
+
let provider;
|
|
228
|
+
let options;
|
|
229
|
+
if (keypair instanceof X25519Keypair) {
|
|
230
|
+
provider = createLocalEncryptProvider(keypair);
|
|
231
|
+
options = {
|
|
232
|
+
receiverPublicKeys: parameters,
|
|
233
|
+
type: "publicKey",
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
provider = keypair;
|
|
238
|
+
options = parameters;
|
|
239
|
+
}
|
|
240
|
+
const bytes = serialize(this);
|
|
241
|
+
const { cipher, envelope, nonce } = await provider(bytes, options);
|
|
242
|
+
const enc = new EncryptedThing({
|
|
243
|
+
encrypted: cipher,
|
|
244
|
+
envelope,
|
|
245
|
+
nonce,
|
|
246
|
+
});
|
|
247
|
+
enc._decrypted = this;
|
|
248
|
+
return enc;
|
|
264
249
|
}
|
|
265
|
-
|
|
266
|
-
return
|
|
250
|
+
get decrypted() {
|
|
251
|
+
return this;
|
|
267
252
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
__decorate([
|
|
275
|
-
field({ type: X25519PublicKey }),
|
|
276
|
-
__metadata("design:type", X25519PublicKey)
|
|
277
|
-
], K.prototype, "_receiverPublicKey", void 0);
|
|
278
|
-
K = K_1 = __decorate([
|
|
279
|
-
variant(0),
|
|
280
|
-
__metadata("design:paramtypes", [Object])
|
|
281
|
-
], K);
|
|
282
|
-
export { K };
|
|
283
|
-
class Envelope {
|
|
284
|
-
}
|
|
285
|
-
let PublicKeyEnvelope = PublicKeyEnvelope_1 = class PublicKeyEnvelope extends Envelope {
|
|
286
|
-
_senderPublicKey;
|
|
287
|
-
_ks;
|
|
288
|
-
constructor(props) {
|
|
289
|
-
super();
|
|
290
|
-
this._senderPublicKey = props.senderPublicKey;
|
|
291
|
-
this._ks = props.ks;
|
|
292
|
-
}
|
|
293
|
-
// TODO: should this be comparable to AbstractEnvelope?
|
|
294
|
-
equals(other) {
|
|
295
|
-
if (other instanceof PublicKeyEnvelope_1) {
|
|
296
|
-
if (!this._senderPublicKey.equals(other._senderPublicKey)) {
|
|
297
|
-
return false;
|
|
253
|
+
decrypt() {
|
|
254
|
+
return this;
|
|
255
|
+
}
|
|
256
|
+
equals(other) {
|
|
257
|
+
if (other instanceof DecryptedThing) {
|
|
258
|
+
return equals(this._data, other._data);
|
|
298
259
|
}
|
|
299
|
-
|
|
260
|
+
else {
|
|
300
261
|
return false;
|
|
301
262
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
263
|
+
}
|
|
264
|
+
clear() {
|
|
265
|
+
this._value = undefined;
|
|
266
|
+
}
|
|
267
|
+
get byteLength() {
|
|
268
|
+
return this._data.byteLength;
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
return DecryptedThing = _classThis;
|
|
272
|
+
})();
|
|
273
|
+
export { DecryptedThing };
|
|
274
|
+
let CipherWithNonce = (() => {
|
|
275
|
+
let _classDecorators = [variant(0)];
|
|
276
|
+
let _classDescriptor;
|
|
277
|
+
let _classExtraInitializers = [];
|
|
278
|
+
let _classThis;
|
|
279
|
+
let _nonce_decorators;
|
|
280
|
+
let _nonce_initializers = [];
|
|
281
|
+
let _nonce_extraInitializers = [];
|
|
282
|
+
let _cipher_decorators;
|
|
283
|
+
let _cipher_initializers = [];
|
|
284
|
+
let _cipher_extraInitializers = [];
|
|
285
|
+
var CipherWithNonce = class {
|
|
286
|
+
static { _classThis = this; }
|
|
287
|
+
static {
|
|
288
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
289
|
+
_nonce_decorators = [field({ type: Uint8Array })];
|
|
290
|
+
_cipher_decorators = [field({ type: Uint8Array })];
|
|
291
|
+
__esDecorate(null, null, _nonce_decorators, { kind: "field", name: "nonce", static: false, private: false, access: { has: obj => "nonce" in obj, get: obj => obj.nonce, set: (obj, value) => { obj.nonce = value; } }, metadata: _metadata }, _nonce_initializers, _nonce_extraInitializers);
|
|
292
|
+
__esDecorate(null, null, _cipher_decorators, { kind: "field", name: "cipher", static: false, private: false, access: { has: obj => "cipher" in obj, get: obj => obj.cipher, set: (obj, value) => { obj.cipher = value; } }, metadata: _metadata }, _cipher_initializers, _cipher_extraInitializers);
|
|
293
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
294
|
+
CipherWithNonce = _classThis = _classDescriptor.value;
|
|
295
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
296
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
297
|
+
}
|
|
298
|
+
nonce = __runInitializers(this, _nonce_initializers, void 0);
|
|
299
|
+
cipher = (__runInitializers(this, _nonce_extraInitializers), __runInitializers(this, _cipher_initializers, void 0));
|
|
300
|
+
constructor(props) {
|
|
301
|
+
__runInitializers(this, _cipher_extraInitializers);
|
|
302
|
+
this.nonce = props.nonce;
|
|
303
|
+
this.cipher = props.cipher;
|
|
304
|
+
}
|
|
305
|
+
equals(other) {
|
|
306
|
+
if (other instanceof CipherWithNonce) {
|
|
307
|
+
return (equals(this.nonce, other.nonce) && equals(this.cipher, other.cipher));
|
|
306
308
|
}
|
|
307
|
-
|
|
309
|
+
else {
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
return CipherWithNonce = _classThis;
|
|
315
|
+
})();
|
|
316
|
+
export { CipherWithNonce };
|
|
317
|
+
let K = (() => {
|
|
318
|
+
let _classDecorators = [variant(0)];
|
|
319
|
+
let _classDescriptor;
|
|
320
|
+
let _classExtraInitializers = [];
|
|
321
|
+
let _classThis;
|
|
322
|
+
let __encryptedKey_decorators;
|
|
323
|
+
let __encryptedKey_initializers = [];
|
|
324
|
+
let __encryptedKey_extraInitializers = [];
|
|
325
|
+
let __receiverPublicKey_decorators;
|
|
326
|
+
let __receiverPublicKey_initializers = [];
|
|
327
|
+
let __receiverPublicKey_extraInitializers = [];
|
|
328
|
+
var K = class {
|
|
329
|
+
static { _classThis = this; }
|
|
330
|
+
static {
|
|
331
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
332
|
+
__encryptedKey_decorators = [field({ type: CipherWithNonce })];
|
|
333
|
+
__receiverPublicKey_decorators = [field({ type: X25519PublicKey })];
|
|
334
|
+
__esDecorate(null, null, __encryptedKey_decorators, { kind: "field", name: "_encryptedKey", static: false, private: false, access: { has: obj => "_encryptedKey" in obj, get: obj => obj._encryptedKey, set: (obj, value) => { obj._encryptedKey = value; } }, metadata: _metadata }, __encryptedKey_initializers, __encryptedKey_extraInitializers);
|
|
335
|
+
__esDecorate(null, null, __receiverPublicKey_decorators, { kind: "field", name: "_receiverPublicKey", static: false, private: false, access: { has: obj => "_receiverPublicKey" in obj, get: obj => obj._receiverPublicKey, set: (obj, value) => { obj._receiverPublicKey = value; } }, metadata: _metadata }, __receiverPublicKey_initializers, __receiverPublicKey_extraInitializers);
|
|
336
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
337
|
+
K = _classThis = _classDescriptor.value;
|
|
338
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
339
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
308
340
|
}
|
|
309
|
-
|
|
310
|
-
|
|
341
|
+
_encryptedKey = __runInitializers(this, __encryptedKey_initializers, void 0);
|
|
342
|
+
_receiverPublicKey = (__runInitializers(this, __encryptedKey_extraInitializers), __runInitializers(this, __receiverPublicKey_initializers, void 0));
|
|
343
|
+
constructor(props) {
|
|
344
|
+
__runInitializers(this, __receiverPublicKey_extraInitializers);
|
|
345
|
+
this._encryptedKey = props.encryptedKey;
|
|
346
|
+
this._receiverPublicKey = props.receiverPublicKey;
|
|
311
347
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
__decorate([
|
|
319
|
-
field({ type: vec(K) }),
|
|
320
|
-
__metadata("design:type", Array)
|
|
321
|
-
], PublicKeyEnvelope.prototype, "_ks", void 0);
|
|
322
|
-
PublicKeyEnvelope = PublicKeyEnvelope_1 = __decorate([
|
|
323
|
-
variant(0),
|
|
324
|
-
__metadata("design:paramtypes", [Object])
|
|
325
|
-
], PublicKeyEnvelope);
|
|
326
|
-
let HashedKeyEnvelope = HashedKeyEnvelope_1 = class HashedKeyEnvelope extends Envelope {
|
|
327
|
-
hash;
|
|
328
|
-
// TODO: Do we need a salt here?
|
|
329
|
-
constructor(props) {
|
|
330
|
-
super();
|
|
331
|
-
this.hash = props.hash;
|
|
332
|
-
}
|
|
333
|
-
// TODO: should this be comparable to AbstractEnvelope?
|
|
334
|
-
equals(other) {
|
|
335
|
-
if (other instanceof HashedKeyEnvelope_1) {
|
|
336
|
-
if (!equals(this.hash, other.hash)) {
|
|
348
|
+
equals(other) {
|
|
349
|
+
if (other instanceof K) {
|
|
350
|
+
return (this._encryptedKey.equals(other._encryptedKey) &&
|
|
351
|
+
this._receiverPublicKey.equals(other._receiverPublicKey));
|
|
352
|
+
}
|
|
353
|
+
else {
|
|
337
354
|
return false;
|
|
338
355
|
}
|
|
339
|
-
return true;
|
|
340
356
|
}
|
|
341
|
-
|
|
342
|
-
|
|
357
|
+
};
|
|
358
|
+
return K = _classThis;
|
|
359
|
+
})();
|
|
360
|
+
export { K };
|
|
361
|
+
class Envelope {
|
|
362
|
+
}
|
|
363
|
+
let PublicKeyEnvelope = (() => {
|
|
364
|
+
let _classDecorators = [variant(0)];
|
|
365
|
+
let _classDescriptor;
|
|
366
|
+
let _classExtraInitializers = [];
|
|
367
|
+
let _classThis;
|
|
368
|
+
let _classSuper = Envelope;
|
|
369
|
+
let __senderPublicKey_decorators;
|
|
370
|
+
let __senderPublicKey_initializers = [];
|
|
371
|
+
let __senderPublicKey_extraInitializers = [];
|
|
372
|
+
let __ks_decorators;
|
|
373
|
+
let __ks_initializers = [];
|
|
374
|
+
let __ks_extraInitializers = [];
|
|
375
|
+
var PublicKeyEnvelope = class extends _classSuper {
|
|
376
|
+
static { _classThis = this; }
|
|
377
|
+
static {
|
|
378
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
379
|
+
__senderPublicKey_decorators = [field({ type: X25519PublicKey })];
|
|
380
|
+
__ks_decorators = [field({ type: vec(K) })];
|
|
381
|
+
__esDecorate(null, null, __senderPublicKey_decorators, { kind: "field", name: "_senderPublicKey", static: false, private: false, access: { has: obj => "_senderPublicKey" in obj, get: obj => obj._senderPublicKey, set: (obj, value) => { obj._senderPublicKey = value; } }, metadata: _metadata }, __senderPublicKey_initializers, __senderPublicKey_extraInitializers);
|
|
382
|
+
__esDecorate(null, null, __ks_decorators, { kind: "field", name: "_ks", static: false, private: false, access: { has: obj => "_ks" in obj, get: obj => obj._ks, set: (obj, value) => { obj._ks = value; } }, metadata: _metadata }, __ks_initializers, __ks_extraInitializers);
|
|
383
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
384
|
+
PublicKeyEnvelope = _classThis = _classDescriptor.value;
|
|
385
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
386
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
343
387
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
variant(1),
|
|
352
|
-
__metadata("design:paramtypes", [Object])
|
|
353
|
-
], HashedKeyEnvelope);
|
|
354
|
-
let EncryptedThing = EncryptedThing_1 = class EncryptedThing extends MaybeEncrypted {
|
|
355
|
-
_encrypted;
|
|
356
|
-
_nonce;
|
|
357
|
-
_keyexchange;
|
|
358
|
-
constructor(props) {
|
|
359
|
-
super();
|
|
360
|
-
this._encrypted = props.encrypted;
|
|
361
|
-
this._nonce = props.nonce;
|
|
362
|
-
this._keyexchange = props.envelope;
|
|
363
|
-
}
|
|
364
|
-
_decrypted;
|
|
365
|
-
get decrypted() {
|
|
366
|
-
if (!this._decrypted) {
|
|
367
|
-
throw new Error("Entry has not been decrypted, invoke decrypt method before");
|
|
388
|
+
_senderPublicKey = __runInitializers(this, __senderPublicKey_initializers, void 0);
|
|
389
|
+
_ks = (__runInitializers(this, __senderPublicKey_extraInitializers), __runInitializers(this, __ks_initializers, void 0));
|
|
390
|
+
constructor(props) {
|
|
391
|
+
super();
|
|
392
|
+
__runInitializers(this, __ks_extraInitializers);
|
|
393
|
+
this._senderPublicKey = props.senderPublicKey;
|
|
394
|
+
this._ks = props.ks;
|
|
368
395
|
}
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if (
|
|
380
|
-
return
|
|
396
|
+
// TODO: should this be comparable to AbstractEnvelope?
|
|
397
|
+
equals(other) {
|
|
398
|
+
if (other instanceof PublicKeyEnvelope) {
|
|
399
|
+
if (!this._senderPublicKey.equals(other._senderPublicKey)) {
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
if (this._ks.length !== other._ks.length) {
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
for (let i = 0; i < this._ks.length; i++) {
|
|
406
|
+
if (!this._ks[i].equals(other._ks[i])) {
|
|
407
|
+
return false;
|
|
381
408
|
}
|
|
382
409
|
}
|
|
383
|
-
|
|
384
|
-
}
|
|
385
|
-
|
|
410
|
+
return true;
|
|
411
|
+
}
|
|
412
|
+
else {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
386
415
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
416
|
+
};
|
|
417
|
+
return PublicKeyEnvelope = _classThis;
|
|
418
|
+
})();
|
|
419
|
+
let HashedKeyEnvelope = (() => {
|
|
420
|
+
let _classDecorators = [variant(1)];
|
|
421
|
+
let _classDescriptor;
|
|
422
|
+
let _classExtraInitializers = [];
|
|
423
|
+
let _classThis;
|
|
424
|
+
let _classSuper = Envelope;
|
|
425
|
+
let _hash_decorators;
|
|
426
|
+
let _hash_initializers = [];
|
|
427
|
+
let _hash_extraInitializers = [];
|
|
428
|
+
var HashedKeyEnvelope = class extends _classSuper {
|
|
429
|
+
static { _classThis = this; }
|
|
430
|
+
static {
|
|
431
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
432
|
+
_hash_decorators = [field({ type: fixedArray("u8", 32) })];
|
|
433
|
+
__esDecorate(null, null, _hash_decorators, { kind: "field", name: "hash", static: false, private: false, access: { has: obj => "hash" in obj, get: obj => obj.hash, set: (obj, value) => { obj.hash = value; } }, metadata: _metadata }, _hash_initializers, _hash_extraInitializers);
|
|
434
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
435
|
+
HashedKeyEnvelope = _classThis = _classDescriptor.value;
|
|
436
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
437
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
438
|
+
}
|
|
439
|
+
hash = __runInitializers(this, _hash_initializers, void 0);
|
|
440
|
+
// TODO: Do we need a salt here?
|
|
441
|
+
constructor(props) {
|
|
442
|
+
super();
|
|
443
|
+
__runInitializers(this, _hash_extraInitializers);
|
|
444
|
+
this.hash = props.hash;
|
|
445
|
+
}
|
|
446
|
+
// TODO: should this be comparable to AbstractEnvelope?
|
|
447
|
+
equals(other) {
|
|
448
|
+
if (other instanceof HashedKeyEnvelope) {
|
|
449
|
+
if (!equals(this.hash, other.hash)) {
|
|
450
|
+
return false;
|
|
392
451
|
}
|
|
393
|
-
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
return false;
|
|
456
|
+
}
|
|
394
457
|
}
|
|
395
|
-
|
|
396
|
-
|
|
458
|
+
};
|
|
459
|
+
return HashedKeyEnvelope = _classThis;
|
|
460
|
+
})();
|
|
461
|
+
let EncryptedThing = (() => {
|
|
462
|
+
let _classDecorators = [variant(1)];
|
|
463
|
+
let _classDescriptor;
|
|
464
|
+
let _classExtraInitializers = [];
|
|
465
|
+
let _classThis;
|
|
466
|
+
let _classSuper = MaybeEncrypted;
|
|
467
|
+
let __encrypted_decorators;
|
|
468
|
+
let __encrypted_initializers = [];
|
|
469
|
+
let __encrypted_extraInitializers = [];
|
|
470
|
+
let __nonce_decorators;
|
|
471
|
+
let __nonce_initializers = [];
|
|
472
|
+
let __nonce_extraInitializers = [];
|
|
473
|
+
let __keyexchange_decorators;
|
|
474
|
+
let __keyexchange_initializers = [];
|
|
475
|
+
let __keyexchange_extraInitializers = [];
|
|
476
|
+
var EncryptedThing = class extends _classSuper {
|
|
477
|
+
static { _classThis = this; }
|
|
478
|
+
static {
|
|
479
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
|
|
480
|
+
__encrypted_decorators = [field({ type: Uint8Array })];
|
|
481
|
+
__nonce_decorators = [field({ type: Uint8Array })];
|
|
482
|
+
__keyexchange_decorators = [field({ type: Envelope })];
|
|
483
|
+
__esDecorate(null, null, __encrypted_decorators, { kind: "field", name: "_encrypted", static: false, private: false, access: { has: obj => "_encrypted" in obj, get: obj => obj._encrypted, set: (obj, value) => { obj._encrypted = value; } }, metadata: _metadata }, __encrypted_initializers, __encrypted_extraInitializers);
|
|
484
|
+
__esDecorate(null, null, __nonce_decorators, { kind: "field", name: "_nonce", static: false, private: false, access: { has: obj => "_nonce" in obj, get: obj => obj._nonce, set: (obj, value) => { obj._nonce = value; } }, metadata: _metadata }, __nonce_initializers, __nonce_extraInitializers);
|
|
485
|
+
__esDecorate(null, null, __keyexchange_decorators, { kind: "field", name: "_keyexchange", static: false, private: false, access: { has: obj => "_keyexchange" in obj, get: obj => obj._keyexchange, set: (obj, value) => { obj._keyexchange = value; } }, metadata: _metadata }, __keyexchange_initializers, __keyexchange_extraInitializers);
|
|
486
|
+
__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
|
|
487
|
+
EncryptedThing = _classThis = _classDescriptor.value;
|
|
488
|
+
if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
489
|
+
__runInitializers(_classThis, _classExtraInitializers);
|
|
397
490
|
}
|
|
398
|
-
|
|
399
|
-
|
|
491
|
+
_encrypted = __runInitializers(this, __encrypted_initializers, void 0);
|
|
492
|
+
_nonce = (__runInitializers(this, __encrypted_extraInitializers), __runInitializers(this, __nonce_initializers, void 0));
|
|
493
|
+
_keyexchange = (__runInitializers(this, __nonce_extraInitializers), __runInitializers(this, __keyexchange_initializers, void 0));
|
|
494
|
+
constructor(props) {
|
|
495
|
+
super();
|
|
496
|
+
this._encrypted = props.encrypted;
|
|
497
|
+
this._nonce = props.nonce;
|
|
498
|
+
this._keyexchange = props.envelope;
|
|
400
499
|
}
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
500
|
+
_decrypted = __runInitializers(this, __keyexchange_extraInitializers);
|
|
501
|
+
get decrypted() {
|
|
502
|
+
if (!this._decrypted) {
|
|
503
|
+
throw new Error("Entry has not been decrypted, invoke decrypt method before");
|
|
504
|
+
}
|
|
405
505
|
return this._decrypted;
|
|
406
506
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
if (!equals(this._encrypted, other._encrypted)) {
|
|
412
|
-
return false;
|
|
507
|
+
async decrypt(providerOrResolver) {
|
|
508
|
+
let provider;
|
|
509
|
+
if (typeof providerOrResolver === "function") {
|
|
510
|
+
provider = providerOrResolver;
|
|
413
511
|
}
|
|
414
|
-
if (
|
|
415
|
-
|
|
512
|
+
else if (providerOrResolver instanceof X25519Keypair) {
|
|
513
|
+
const resolver = (key) => {
|
|
514
|
+
if (key instanceof X25519PublicKey) {
|
|
515
|
+
if (key.equals(providerOrResolver.publicKey)) {
|
|
516
|
+
return providerOrResolver;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
throw new Error("Missing keypair");
|
|
520
|
+
};
|
|
521
|
+
provider = createDecrypterFromKeyResolver(resolver);
|
|
522
|
+
}
|
|
523
|
+
else if (providerOrResolver) {
|
|
524
|
+
provider = createDecrypterFromKeyResolver(async (key) => {
|
|
525
|
+
if (key instanceof X25519PublicKey) {
|
|
526
|
+
const keypair = await providerOrResolver.exportByKey(key);
|
|
527
|
+
return keypair;
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
if (this._decrypted) {
|
|
532
|
+
return this._decrypted;
|
|
533
|
+
}
|
|
534
|
+
if (!provider) {
|
|
535
|
+
throw new AccessError("Expecting decryption provider");
|
|
536
|
+
}
|
|
537
|
+
const decrypted = await provider(this._encrypted, this._nonce, this._keyexchange);
|
|
538
|
+
if (decrypted) {
|
|
539
|
+
const der = deserialize(decrypted, DecryptedThing);
|
|
540
|
+
this._decrypted = der;
|
|
541
|
+
return this._decrypted;
|
|
416
542
|
}
|
|
417
|
-
|
|
543
|
+
throw new AccessError("Failed to resolve decryption key");
|
|
544
|
+
}
|
|
545
|
+
equals(other) {
|
|
546
|
+
if (other instanceof EncryptedThing) {
|
|
547
|
+
if (!equals(this._encrypted, other._encrypted)) {
|
|
548
|
+
return false;
|
|
549
|
+
}
|
|
550
|
+
if (!equals(this._nonce, other._nonce)) {
|
|
551
|
+
return false;
|
|
552
|
+
}
|
|
553
|
+
if (!this._keyexchange.equals(other._keyexchange)) {
|
|
554
|
+
return false;
|
|
555
|
+
}
|
|
556
|
+
return true;
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
418
559
|
return false;
|
|
419
560
|
}
|
|
420
|
-
return true;
|
|
421
561
|
}
|
|
422
|
-
|
|
423
|
-
|
|
562
|
+
clear() {
|
|
563
|
+
this._decrypted = undefined;
|
|
424
564
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
}
|
|
432
|
-
};
|
|
433
|
-
__decorate([
|
|
434
|
-
field({ type: Uint8Array }),
|
|
435
|
-
__metadata("design:type", Uint8Array)
|
|
436
|
-
], EncryptedThing.prototype, "_encrypted", void 0);
|
|
437
|
-
__decorate([
|
|
438
|
-
field({ type: Uint8Array }),
|
|
439
|
-
__metadata("design:type", Uint8Array)
|
|
440
|
-
], EncryptedThing.prototype, "_nonce", void 0);
|
|
441
|
-
__decorate([
|
|
442
|
-
field({ type: Envelope }),
|
|
443
|
-
__metadata("design:type", Object)
|
|
444
|
-
], EncryptedThing.prototype, "_keyexchange", void 0);
|
|
445
|
-
EncryptedThing = EncryptedThing_1 = __decorate([
|
|
446
|
-
variant(1),
|
|
447
|
-
__metadata("design:paramtypes", [Object])
|
|
448
|
-
], EncryptedThing);
|
|
565
|
+
get byteLength() {
|
|
566
|
+
return this._encrypted.byteLength; // ignore other metdata for now in the size calculation
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
return EncryptedThing = _classThis;
|
|
570
|
+
})();
|
|
449
571
|
export { EncryptedThing };
|
|
450
572
|
//# sourceMappingURL=encryption.js.map
|