@peerbit/crypto 1.0.3 → 1.0.5
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/lib/esm/encryption.d.ts +3 -3
- package/lib/esm/encryption.js +11 -11
- package/lib/esm/keychain.js +1 -1
- package/package.json +3 -3
- package/src/encryption.ts +12 -12
- package/src/keychain.ts +1 -1
package/lib/esm/encryption.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
24
24
|
});
|
|
25
25
|
_value?: T;
|
|
26
26
|
getValue(clazz: AbstractType<T>): T;
|
|
27
|
-
encrypt(x25519Keypair: X25519Keypair, ...
|
|
27
|
+
encrypt(x25519Keypair: X25519Keypair, ...receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[]): Promise<EncryptedThing<T>>;
|
|
28
28
|
get decrypted(): DecryptedThing<T>;
|
|
29
29
|
decrypt(): DecryptedThing<T>;
|
|
30
30
|
equals(other: MaybeEncrypted<T>): boolean;
|
|
@@ -42,10 +42,10 @@ export declare class CipherWithNonce {
|
|
|
42
42
|
}
|
|
43
43
|
export declare class K {
|
|
44
44
|
_encryptedKey: CipherWithNonce;
|
|
45
|
-
|
|
45
|
+
_receiverPublicKey: X25519PublicKey;
|
|
46
46
|
constructor(props?: {
|
|
47
47
|
encryptedKey: CipherWithNonce;
|
|
48
|
-
|
|
48
|
+
receiverPublicKey: X25519PublicKey;
|
|
49
49
|
});
|
|
50
50
|
equals(other: K): boolean;
|
|
51
51
|
}
|
package/lib/esm/encryption.js
CHANGED
|
@@ -59,25 +59,25 @@ export let DecryptedThing = DecryptedThing_1 = class DecryptedThing extends Mayb
|
|
|
59
59
|
}
|
|
60
60
|
return deserialize(this._data, clazz);
|
|
61
61
|
}
|
|
62
|
-
async encrypt(x25519Keypair, ...
|
|
62
|
+
async encrypt(x25519Keypair, ...receiverPublicKeys) {
|
|
63
63
|
const bytes = serialize(this);
|
|
64
64
|
const epheremalKey = sodium.crypto_secretbox_keygen();
|
|
65
65
|
const nonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodim random
|
|
66
66
|
const cipher = sodium.crypto_secretbox_easy(bytes, nonce, epheremalKey);
|
|
67
|
-
const
|
|
67
|
+
const receiverX25519PublicKeys = await Promise.all(receiverPublicKeys.map((key) => {
|
|
68
68
|
if (key instanceof Ed25519PublicKey) {
|
|
69
69
|
return X25519PublicKey.from(key);
|
|
70
70
|
}
|
|
71
71
|
return key;
|
|
72
72
|
}));
|
|
73
|
-
const ks =
|
|
73
|
+
const ks = receiverX25519PublicKeys.map((receiverPublicKey) => {
|
|
74
74
|
const kNonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodium random
|
|
75
75
|
return new K({
|
|
76
76
|
encryptedKey: new CipherWithNonce({
|
|
77
|
-
cipher: sodium.crypto_box_easy(epheremalKey, kNonce,
|
|
77
|
+
cipher: sodium.crypto_box_easy(epheremalKey, kNonce, receiverPublicKey.publicKey, x25519Keypair.secretKey.secretKey),
|
|
78
78
|
nonce: kNonce,
|
|
79
79
|
}),
|
|
80
|
-
|
|
80
|
+
receiverPublicKey,
|
|
81
81
|
});
|
|
82
82
|
});
|
|
83
83
|
const enc = new EncryptedThing({
|
|
@@ -152,17 +152,17 @@ CipherWithNonce = CipherWithNonce_1 = __decorate([
|
|
|
152
152
|
], CipherWithNonce);
|
|
153
153
|
export let K = K_1 = class K {
|
|
154
154
|
_encryptedKey;
|
|
155
|
-
|
|
155
|
+
_receiverPublicKey;
|
|
156
156
|
constructor(props) {
|
|
157
157
|
if (props) {
|
|
158
158
|
this._encryptedKey = props.encryptedKey;
|
|
159
|
-
this.
|
|
159
|
+
this._receiverPublicKey = props.receiverPublicKey;
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
equals(other) {
|
|
163
163
|
if (other instanceof K_1) {
|
|
164
164
|
return (this._encryptedKey.equals(other._encryptedKey) &&
|
|
165
|
-
this.
|
|
165
|
+
this._receiverPublicKey.equals(other._receiverPublicKey));
|
|
166
166
|
}
|
|
167
167
|
else {
|
|
168
168
|
return false;
|
|
@@ -176,7 +176,7 @@ __decorate([
|
|
|
176
176
|
__decorate([
|
|
177
177
|
field({ type: X25519PublicKey }),
|
|
178
178
|
__metadata("design:type", X25519PublicKey)
|
|
179
|
-
], K.prototype, "
|
|
179
|
+
], K.prototype, "_receiverPublicKey", void 0);
|
|
180
180
|
K = K_1 = __decorate([
|
|
181
181
|
variant(0),
|
|
182
182
|
__metadata("design:paramtypes", [Object])
|
|
@@ -252,7 +252,7 @@ export let EncryptedThing = EncryptedThing_1 = class EncryptedThing extends Mayb
|
|
|
252
252
|
let key;
|
|
253
253
|
if (keyResolver instanceof X25519Keypair) {
|
|
254
254
|
for (const [i, k] of this._envelope._ks.entries()) {
|
|
255
|
-
if (k.
|
|
255
|
+
if (k._receiverPublicKey.equals(keyResolver.publicKey)) {
|
|
256
256
|
key = {
|
|
257
257
|
index: i,
|
|
258
258
|
keypair: keyResolver,
|
|
@@ -262,7 +262,7 @@ export let EncryptedThing = EncryptedThing_1 = class EncryptedThing extends Mayb
|
|
|
262
262
|
}
|
|
263
263
|
else {
|
|
264
264
|
for (const [i, k] of this._envelope._ks.entries()) {
|
|
265
|
-
const exported = await keyResolver.exportByKey(k.
|
|
265
|
+
const exported = await keyResolver.exportByKey(k._receiverPublicKey);
|
|
266
266
|
if (exported) {
|
|
267
267
|
key = {
|
|
268
268
|
index: i,
|
package/lib/esm/keychain.js
CHANGED
|
@@ -112,7 +112,7 @@ export class Libp2pKeychain {
|
|
|
112
112
|
this.cacheKey(keypair, id);
|
|
113
113
|
// import as ed
|
|
114
114
|
await this.keychain.importPeer(base58btc.encode(id), receiverKeyPeerId);
|
|
115
|
-
// import as x so we can decrypt messages with this public key (if
|
|
115
|
+
// import as x so we can decrypt messages with this public key (if received any)
|
|
116
116
|
const xKeypair = await X25519Keypair.from(keypair);
|
|
117
117
|
this.cacheKey(xKeypair);
|
|
118
118
|
await this.keychain.importPeer(base58btc.encode(xKeypair.publicKey.bytes), receiverKeyPeerId);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/crypto",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Crypto fn",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
"@ethersproject/wallet": "^5.7.0",
|
|
48
48
|
"@libp2p/crypto": "^1.0.17",
|
|
49
49
|
"@libp2p/peer-id": "^2.0.3",
|
|
50
|
-
"@peerbit/cache": "^1.0.
|
|
50
|
+
"@peerbit/cache": "^1.0.2",
|
|
51
51
|
"@peerbit/uint8arrays": "3.0.1",
|
|
52
52
|
"libsodium-wrappers": "^0.7.11"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "464e807d679e24b897b7811ac99d6f85fbd756f9"
|
|
55
55
|
}
|
package/src/encryption.ts
CHANGED
|
@@ -71,15 +71,15 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
71
71
|
|
|
72
72
|
async encrypt(
|
|
73
73
|
x25519Keypair: X25519Keypair,
|
|
74
|
-
...
|
|
74
|
+
...receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[]
|
|
75
75
|
): Promise<EncryptedThing<T>> {
|
|
76
76
|
const bytes = serialize(this);
|
|
77
77
|
const epheremalKey = sodium.crypto_secretbox_keygen();
|
|
78
78
|
const nonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodim random
|
|
79
79
|
const cipher = sodium.crypto_secretbox_easy(bytes, nonce, epheremalKey);
|
|
80
80
|
|
|
81
|
-
const
|
|
82
|
-
|
|
81
|
+
const receiverX25519PublicKeys = await Promise.all(
|
|
82
|
+
receiverPublicKeys.map((key) => {
|
|
83
83
|
if (key instanceof Ed25519PublicKey) {
|
|
84
84
|
return X25519PublicKey.from(key);
|
|
85
85
|
}
|
|
@@ -87,19 +87,19 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
87
87
|
})
|
|
88
88
|
);
|
|
89
89
|
|
|
90
|
-
const ks =
|
|
90
|
+
const ks = receiverX25519PublicKeys.map((receiverPublicKey) => {
|
|
91
91
|
const kNonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodium random
|
|
92
92
|
return new K({
|
|
93
93
|
encryptedKey: new CipherWithNonce({
|
|
94
94
|
cipher: sodium.crypto_box_easy(
|
|
95
95
|
epheremalKey,
|
|
96
96
|
kNonce,
|
|
97
|
-
|
|
97
|
+
receiverPublicKey.publicKey,
|
|
98
98
|
x25519Keypair.secretKey.secretKey
|
|
99
99
|
),
|
|
100
100
|
nonce: kNonce,
|
|
101
101
|
}),
|
|
102
|
-
|
|
102
|
+
receiverPublicKey,
|
|
103
103
|
});
|
|
104
104
|
});
|
|
105
105
|
|
|
@@ -172,15 +172,15 @@ export class K {
|
|
|
172
172
|
_encryptedKey: CipherWithNonce;
|
|
173
173
|
|
|
174
174
|
@field({ type: X25519PublicKey })
|
|
175
|
-
|
|
175
|
+
_receiverPublicKey: X25519PublicKey;
|
|
176
176
|
|
|
177
177
|
constructor(props?: {
|
|
178
178
|
encryptedKey: CipherWithNonce;
|
|
179
|
-
|
|
179
|
+
receiverPublicKey: X25519PublicKey;
|
|
180
180
|
}) {
|
|
181
181
|
if (props) {
|
|
182
182
|
this._encryptedKey = props.encryptedKey;
|
|
183
|
-
this.
|
|
183
|
+
this._receiverPublicKey = props.receiverPublicKey;
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
|
|
@@ -188,7 +188,7 @@ export class K {
|
|
|
188
188
|
if (other instanceof K) {
|
|
189
189
|
return (
|
|
190
190
|
this._encryptedKey.equals(other._encryptedKey) &&
|
|
191
|
-
this.
|
|
191
|
+
this._receiverPublicKey.equals(other._receiverPublicKey)
|
|
192
192
|
);
|
|
193
193
|
} else {
|
|
194
194
|
return false;
|
|
@@ -281,7 +281,7 @@ export class EncryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
281
281
|
let key: { index: number; keypair: X25519Keypair } | undefined;
|
|
282
282
|
if (keyResolver instanceof X25519Keypair) {
|
|
283
283
|
for (const [i, k] of this._envelope._ks.entries()) {
|
|
284
|
-
if (k.
|
|
284
|
+
if (k._receiverPublicKey.equals(keyResolver.publicKey)) {
|
|
285
285
|
key = {
|
|
286
286
|
index: i,
|
|
287
287
|
keypair: keyResolver,
|
|
@@ -290,7 +290,7 @@ export class EncryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
290
290
|
}
|
|
291
291
|
} else {
|
|
292
292
|
for (const [i, k] of this._envelope._ks.entries()) {
|
|
293
|
-
const exported = await keyResolver.exportByKey(k.
|
|
293
|
+
const exported = await keyResolver.exportByKey(k._receiverPublicKey);
|
|
294
294
|
if (exported) {
|
|
295
295
|
key = {
|
|
296
296
|
index: i,
|
package/src/keychain.ts
CHANGED
|
@@ -165,7 +165,7 @@ export class Libp2pKeychain implements Keychain {
|
|
|
165
165
|
// import as ed
|
|
166
166
|
await this.keychain.importPeer(base58btc.encode(id), receiverKeyPeerId);
|
|
167
167
|
|
|
168
|
-
// import as x so we can decrypt messages with this public key (if
|
|
168
|
+
// import as x so we can decrypt messages with this public key (if received any)
|
|
169
169
|
const xKeypair = await X25519Keypair.from(keypair);
|
|
170
170
|
this.cacheKey(xKeypair);
|
|
171
171
|
await this.keychain.importPeer(
|