@peerbit/crypto 2.2.0-efee9d3 → 2.3.0-3a75d6e
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 +4 -4
- package/dist/benchmark/hash.js.map +1 -1
- package/dist/benchmark/index.js +5 -5
- package/dist/benchmark/index.js.map +1 -1
- package/dist/benchmark/peer-ids.js +6 -6
- package/dist/benchmark/peer-ids.js.map +1 -1
- package/dist/src/ed25519-sign.browser.d.ts +2 -2
- package/dist/src/ed25519-sign.browser.d.ts.map +1 -1
- package/dist/src/ed25519-sign.browser.js +2 -3
- package/dist/src/ed25519-sign.browser.js.map +1 -1
- package/dist/src/ed25519-sign.d.ts +2 -2
- package/dist/src/ed25519-sign.d.ts.map +1 -1
- package/dist/src/ed25519-sign.js +8 -8
- package/dist/src/ed25519-sign.js.map +1 -1
- package/dist/src/ed25519.d.ts +3 -3
- package/dist/src/ed25519.d.ts.map +1 -1
- package/dist/src/ed25519.js +14 -15
- package/dist/src/ed25519.js.map +1 -1
- package/dist/src/encryption.d.ts +12 -10
- package/dist/src/encryption.d.ts.map +1 -1
- package/dist/src/encryption.js +26 -36
- package/dist/src/encryption.js.map +1 -1
- package/dist/src/errors.d.ts +0 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -3
- package/dist/src/errors.js.map +1 -1
- package/dist/src/hash.browser.d.ts.map +1 -1
- package/dist/src/hash.browser.js +1 -1
- package/dist/src/hash.browser.js.map +1 -1
- package/dist/src/hash.d.ts.map +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/key.d.ts.map +1 -1
- package/dist/src/key.js +2 -2
- package/dist/src/key.js.map +1 -1
- package/dist/src/prehash.d.ts.map +1 -1
- package/dist/src/prehash.js +4 -4
- package/dist/src/prehash.js.map +1 -1
- package/dist/src/random.d.ts +0 -1
- package/dist/src/random.d.ts.map +1 -1
- package/dist/src/random.js.map +1 -1
- package/dist/src/sepc256k1.d.ts +4 -4
- package/dist/src/sepc256k1.d.ts.map +1 -1
- package/dist/src/sepc256k1.js +18 -20
- package/dist/src/sepc256k1.js.map +1 -1
- package/dist/src/signature.d.ts +1 -5
- package/dist/src/signature.d.ts.map +1 -1
- package/dist/src/signature.js +12 -185
- package/dist/src/signature.js.map +1 -1
- package/dist/src/signer.d.ts +3 -3
- package/dist/src/signer.d.ts.map +1 -1
- package/dist/src/signer.js +1 -3
- package/dist/src/signer.js.map +1 -1
- package/dist/src/utils.d.ts +1 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +4 -0
- package/dist/src/utils.js.map +1 -1
- package/dist/src/x25519.d.ts +3 -3
- package/dist/src/x25519.d.ts.map +1 -1
- package/dist/src/x25519.js +15 -15
- package/dist/src/x25519.js.map +1 -1
- package/package.json +11 -9
- package/src/ed25519-sign.browser.ts +7 -7
- package/src/ed25519-sign.ts +11 -11
- package/src/ed25519.ts +21 -21
- package/src/encryption.ts +63 -72
- package/src/errors.ts +1 -5
- package/src/hash.browser.ts +2 -1
- package/src/index.ts +3 -1
- package/src/key.ts +9 -8
- package/src/peerid.ts +2 -2
- package/src/prehash.ts +8 -8
- package/src/random.ts +1 -0
- package/src/sepc256k1.ts +37 -35
- package/src/signature.ts +14 -188
- package/src/signer.ts +5 -5
- package/src/utils.ts +6 -0
- package/src/x25519.ts +26 -27
package/src/ed25519.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
2
|
-
import { PrivateSignKey, PublicSignKey, Keypair } from "./key.js";
|
|
3
|
-
import { equals } from "uint8arrays";
|
|
4
|
-
import { type Identity, type SignWithKey } from "./signer.js";
|
|
5
|
-
import { SignatureWithKey } from "./signature.js";
|
|
6
|
-
import { toHexString } from "./utils.js";
|
|
7
|
-
import { peerIdFromKeys } from "@libp2p/peer-id";
|
|
8
2
|
import { supportedKeys } from "@libp2p/crypto/keys";
|
|
9
|
-
import { coerce } from "./bytes.js";
|
|
10
|
-
import sodium from "libsodium-wrappers";
|
|
11
3
|
import type { Ed25519PeerId, PeerId } from "@libp2p/interface";
|
|
4
|
+
import { peerIdFromKeys } from "@libp2p/peer-id";
|
|
5
|
+
import sodium from "libsodium-wrappers";
|
|
6
|
+
import { concat, equals } from "uint8arrays";
|
|
7
|
+
import { coerce } from "./bytes.js";
|
|
12
8
|
import { sign } from "./ed25519-sign.js";
|
|
9
|
+
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js";
|
|
13
10
|
import { PreHash } from "./prehash.js";
|
|
14
|
-
import {
|
|
11
|
+
import { SignatureWithKey } from "./signature.js";
|
|
12
|
+
import { type Identity, type SignWithKey } from "./signer.js";
|
|
13
|
+
import { toHexString } from "./utils.js";
|
|
15
14
|
|
|
16
15
|
@variant(0)
|
|
17
16
|
export class Ed25519PublicKey extends PublicSignKey {
|
|
@@ -32,13 +31,14 @@ export class Ed25519PublicKey extends PublicSignKey {
|
|
|
32
31
|
}
|
|
33
32
|
return false;
|
|
34
33
|
}
|
|
34
|
+
|
|
35
35
|
toString(): string {
|
|
36
36
|
return "ed25119p/" + toHexString(this.publicKey);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
toPeerId(): Promise<PeerId> {
|
|
40
40
|
return peerIdFromKeys(
|
|
41
|
-
new supportedKeys["ed25519"].Ed25519PublicKey(this.publicKey).bytes
|
|
41
|
+
new supportedKeys["ed25519"].Ed25519PublicKey(this.publicKey).bytes,
|
|
42
42
|
);
|
|
43
43
|
}
|
|
44
44
|
|
|
@@ -60,7 +60,7 @@ export class Ed25519PublicKey extends PublicSignKey {
|
|
|
60
60
|
}
|
|
61
61
|
if (id.type === "Ed25519") {
|
|
62
62
|
return new Ed25519PublicKey({
|
|
63
|
-
publicKey: coerce(id.publicKey
|
|
63
|
+
publicKey: coerce(id.publicKey.slice(4)),
|
|
64
64
|
});
|
|
65
65
|
}
|
|
66
66
|
throw new Error("Unsupported key type: " + id.type);
|
|
@@ -101,7 +101,7 @@ export class Ed25519PrivateKey extends PrivateSignKey {
|
|
|
101
101
|
}
|
|
102
102
|
if (id.type === "Ed25519") {
|
|
103
103
|
return new Ed25519PrivateKey({
|
|
104
|
-
privateKey: coerce(id.privateKey
|
|
104
|
+
privateKey: coerce(id.privateKey.slice(4, 36)),
|
|
105
105
|
});
|
|
106
106
|
}
|
|
107
107
|
throw new Error("Unsupported key type: " + id.type);
|
|
@@ -130,11 +130,11 @@ export class Ed25519Keypair extends Keypair implements Identity {
|
|
|
130
130
|
const generated = sodium.crypto_sign_keypair();
|
|
131
131
|
const kp = new Ed25519Keypair({
|
|
132
132
|
publicKey: new Ed25519PublicKey({
|
|
133
|
-
publicKey: generated.publicKey
|
|
133
|
+
publicKey: generated.publicKey,
|
|
134
134
|
}),
|
|
135
135
|
privateKey: new Ed25519PrivateKey({
|
|
136
|
-
privateKey: generated.privateKey.slice(0, 32) // Only the private key part (?)
|
|
137
|
-
})
|
|
136
|
+
privateKey: generated.privateKey.slice(0, 32), // Only the private key part (?)
|
|
137
|
+
}),
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
return kp;
|
|
@@ -142,7 +142,7 @@ export class Ed25519Keypair extends Keypair implements Identity {
|
|
|
142
142
|
|
|
143
143
|
sign(
|
|
144
144
|
data: Uint8Array,
|
|
145
|
-
prehash: PreHash = PreHash.NONE
|
|
145
|
+
prehash: PreHash = PreHash.NONE,
|
|
146
146
|
): Promise<SignatureWithKey> {
|
|
147
147
|
return sign(data, this, prehash);
|
|
148
148
|
}
|
|
@@ -166,17 +166,17 @@ export class Ed25519Keypair extends Keypair implements Identity {
|
|
|
166
166
|
static fromPeerId(peerId: PeerId | Ed25519PeerId) {
|
|
167
167
|
return new Ed25519Keypair({
|
|
168
168
|
privateKey: Ed25519PrivateKey.fromPeerID(peerId),
|
|
169
|
-
publicKey: Ed25519PublicKey.fromPeerId(peerId)
|
|
169
|
+
publicKey: Ed25519PublicKey.fromPeerId(peerId),
|
|
170
170
|
});
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
_privateKeyPublicKey
|
|
173
|
+
_privateKeyPublicKey!: Uint8Array; // length 64
|
|
174
174
|
get privateKeyPublicKey(): Uint8Array {
|
|
175
175
|
return (
|
|
176
176
|
this._privateKeyPublicKey ||
|
|
177
177
|
(this._privateKeyPublicKey = concat([
|
|
178
178
|
this.privateKey.privateKey,
|
|
179
|
-
this.publicKey.publicKey
|
|
179
|
+
this.publicKey.publicKey,
|
|
180
180
|
]))
|
|
181
181
|
);
|
|
182
182
|
}
|
|
@@ -187,8 +187,8 @@ export class Ed25519Keypair extends Keypair implements Identity {
|
|
|
187
187
|
.bytes,
|
|
188
188
|
new supportedKeys["ed25519"].Ed25519PrivateKey(
|
|
189
189
|
this.privateKeyPublicKey,
|
|
190
|
-
this.publicKey.publicKey
|
|
191
|
-
).bytes
|
|
190
|
+
this.publicKey.publicKey,
|
|
191
|
+
).bytes,
|
|
192
192
|
);
|
|
193
193
|
}
|
|
194
194
|
}
|
package/src/encryption.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
|
-
export * from "./errors.js";
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
2
|
type AbstractType,
|
|
5
3
|
deserialize,
|
|
6
4
|
field,
|
|
5
|
+
fixedArray,
|
|
7
6
|
serialize,
|
|
8
7
|
variant,
|
|
9
8
|
vec,
|
|
10
|
-
fixedArray
|
|
11
9
|
} from "@dao-xyz/borsh";
|
|
12
|
-
import { AccessError } from "./errors.js";
|
|
13
10
|
import sodium from "libsodium-wrappers";
|
|
14
|
-
import {
|
|
11
|
+
import { equals as uequals } from "uint8arrays";
|
|
15
12
|
import { Ed25519PublicKey } from "./ed25519.js";
|
|
16
|
-
import {
|
|
13
|
+
import { AccessError } from "./errors.js";
|
|
17
14
|
import { sha256 } from "./hash.js";
|
|
15
|
+
import { randomBytes } from "./random.js";
|
|
16
|
+
import { X25519Keypair, X25519PublicKey, X25519SecretKey } from "./x25519.js";
|
|
17
|
+
|
|
18
|
+
export * from "./errors.js";
|
|
18
19
|
|
|
19
|
-
import { equals as uequals } from "uint8arrays";
|
|
20
20
|
export const equals = (array1?: Uint8Array, array2?: Uint8Array) => {
|
|
21
|
-
if (!!array1
|
|
21
|
+
if (!!array1 !== !!array2) return false;
|
|
22
22
|
if (!array1 || !array2) {
|
|
23
23
|
return false;
|
|
24
24
|
}
|
|
25
25
|
return uequals(array1, array2);
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
|
|
29
28
|
export type MaybePromise<T> = Promise<T> | T;
|
|
30
29
|
|
|
31
30
|
export type PublicKeyEncryptionParameters = {
|
|
@@ -48,7 +47,7 @@ export type KeyExchangeOptions =
|
|
|
48
47
|
|
|
49
48
|
type EncryptReturnValue<
|
|
50
49
|
T,
|
|
51
|
-
Parameters extends KeyExchangeOptions
|
|
50
|
+
Parameters extends KeyExchangeOptions,
|
|
52
51
|
> = EncryptedThing<T, EnvelopeFromParameter<Parameters>>;
|
|
53
52
|
|
|
54
53
|
type CipherWithEnvelope<E = PublicKeyEnvelope | HashedKeyEnvelope> = {
|
|
@@ -61,26 +60,26 @@ type SymmetricKeys = Uint8Array;
|
|
|
61
60
|
type PublicKeyEncryptionKeys = X25519Keypair;
|
|
62
61
|
|
|
63
62
|
function isAsymmetriEncryptionParameters(
|
|
64
|
-
parameters: KeyExchangeOptions
|
|
63
|
+
parameters: KeyExchangeOptions,
|
|
65
64
|
): parameters is PublicKeyEncryptionParameters {
|
|
66
65
|
return (
|
|
67
66
|
(parameters as PublicKeyEncryptionParameters).receiverPublicKeys != null
|
|
68
67
|
);
|
|
69
68
|
}
|
|
70
69
|
function isAsymmetricEncryptionKeys(
|
|
71
|
-
parameters: PublicKeyEncryptionKeys | SymmetricKeys
|
|
70
|
+
parameters: PublicKeyEncryptionKeys | SymmetricKeys,
|
|
72
71
|
): parameters is PublicKeyEncryptionKeys {
|
|
73
72
|
return (parameters as PublicKeyEncryptionKeys) instanceof X25519Keypair;
|
|
74
73
|
}
|
|
75
74
|
|
|
76
75
|
type EnvelopeFromParameter<Parameters extends KeyExchangeOptions> =
|
|
77
76
|
Parameters extends PublicKeyEncryptionParameters
|
|
78
|
-
|
|
79
|
-
|
|
77
|
+
? PublicKeyEnvelope
|
|
78
|
+
: HashedKeyEnvelope;
|
|
80
79
|
|
|
81
80
|
type EncryptProvide<Parameters extends KeyExchangeOptions> = (
|
|
82
81
|
bytes: Uint8Array,
|
|
83
|
-
parameters: Parameters
|
|
82
|
+
parameters: Parameters,
|
|
84
83
|
) => Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>>;
|
|
85
84
|
|
|
86
85
|
interface KeyProvider {
|
|
@@ -90,14 +89,14 @@ interface KeyProvider {
|
|
|
90
89
|
export const createLocalEncryptProvider = <
|
|
91
90
|
K extends PublicKeyEncryptionKeys | SymmetricKeys,
|
|
92
91
|
Parameters extends KeyExchangeOptions = K extends PublicKeyEncryptionKeys
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
? PublicKeyEncryptionParameters
|
|
93
|
+
: SymmetricKeyEncryptionParameters,
|
|
95
94
|
>(
|
|
96
|
-
keys: K
|
|
95
|
+
keys: K,
|
|
97
96
|
) => {
|
|
98
97
|
return async (
|
|
99
98
|
bytes: Uint8Array,
|
|
100
|
-
parameters: Parameters
|
|
99
|
+
parameters: Parameters,
|
|
101
100
|
): Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>> => {
|
|
102
101
|
const nonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodim random
|
|
103
102
|
if (
|
|
@@ -113,7 +112,7 @@ export const createLocalEncryptProvider = <
|
|
|
113
112
|
return X25519PublicKey.from(key);
|
|
114
113
|
}
|
|
115
114
|
return key;
|
|
116
|
-
})
|
|
115
|
+
}),
|
|
117
116
|
);
|
|
118
117
|
|
|
119
118
|
const ks = receiverX25519PublicKeys.map((receiverPublicKey) => {
|
|
@@ -124,11 +123,11 @@ export const createLocalEncryptProvider = <
|
|
|
124
123
|
epheremalKey,
|
|
125
124
|
kNonce,
|
|
126
125
|
receiverPublicKey.publicKey,
|
|
127
|
-
keys.secretKey.secretKey
|
|
126
|
+
keys.secretKey.secretKey,
|
|
128
127
|
),
|
|
129
|
-
nonce: kNonce
|
|
128
|
+
nonce: kNonce,
|
|
130
129
|
}),
|
|
131
|
-
receiverPublicKey
|
|
130
|
+
receiverPublicKey,
|
|
132
131
|
});
|
|
133
132
|
});
|
|
134
133
|
|
|
@@ -137,8 +136,8 @@ export const createLocalEncryptProvider = <
|
|
|
137
136
|
nonce,
|
|
138
137
|
envelope: new PublicKeyEnvelope({
|
|
139
138
|
senderPublicKey: keys.publicKey,
|
|
140
|
-
ks
|
|
141
|
-
}) as EnvelopeFromParameter<Parameters
|
|
139
|
+
ks,
|
|
140
|
+
}) as EnvelopeFromParameter<Parameters>,
|
|
142
141
|
};
|
|
143
142
|
} else if (
|
|
144
143
|
!isAsymmetriEncryptionParameters(parameters) &&
|
|
@@ -149,8 +148,8 @@ export const createLocalEncryptProvider = <
|
|
|
149
148
|
cipher: new Uint8Array(cipher), // TODO do we need this clone?
|
|
150
149
|
nonce,
|
|
151
150
|
envelope: new HashedKeyEnvelope({
|
|
152
|
-
hash: await sha256(keys)
|
|
153
|
-
}) as EnvelopeFromParameter<Parameters
|
|
151
|
+
hash: await sha256(keys),
|
|
152
|
+
}) as EnvelopeFromParameter<Parameters>,
|
|
154
153
|
};
|
|
155
154
|
}
|
|
156
155
|
|
|
@@ -161,22 +160,22 @@ export const createLocalEncryptProvider = <
|
|
|
161
160
|
export type DecryptProvider = (
|
|
162
161
|
encrypted: Uint8Array,
|
|
163
162
|
nonce: Uint8Array,
|
|
164
|
-
exchange: Envelope
|
|
163
|
+
exchange: Envelope,
|
|
165
164
|
) => Promise<Uint8Array>;
|
|
166
165
|
|
|
167
166
|
type KeyResolver = <T extends X25519PublicKey | Uint8Array>(
|
|
168
|
-
key: T
|
|
167
|
+
key: T,
|
|
169
168
|
) => Promise<
|
|
170
169
|
(T extends X25519PublicKey ? X25519Keypair : Uint8Array) | undefined
|
|
171
170
|
>;
|
|
172
171
|
|
|
173
172
|
export const createDecrypterFromKeyResolver = (
|
|
174
|
-
keyResolver: KeyResolver
|
|
173
|
+
keyResolver: KeyResolver,
|
|
175
174
|
): DecryptProvider => {
|
|
176
175
|
return async (
|
|
177
176
|
encrypted: Uint8Array,
|
|
178
177
|
nonce: Uint8Array,
|
|
179
|
-
exchange: Envelope
|
|
178
|
+
exchange: Envelope,
|
|
180
179
|
): Promise<Uint8Array> => {
|
|
181
180
|
// We only need to open with one of the keys
|
|
182
181
|
|
|
@@ -189,7 +188,7 @@ export const createDecrypterFromKeyResolver = (
|
|
|
189
188
|
if (exported) {
|
|
190
189
|
key = {
|
|
191
190
|
index: i,
|
|
192
|
-
keypair: exported
|
|
191
|
+
keypair: exported,
|
|
193
192
|
};
|
|
194
193
|
break;
|
|
195
194
|
}
|
|
@@ -208,7 +207,7 @@ export const createDecrypterFromKeyResolver = (
|
|
|
208
207
|
k._encryptedKey.cipher,
|
|
209
208
|
k._encryptedKey.nonce,
|
|
210
209
|
exchange._senderPublicKey.publicKey,
|
|
211
|
-
secretKey.secretKey
|
|
210
|
+
secretKey.secretKey,
|
|
212
211
|
);
|
|
213
212
|
} catch (error) {
|
|
214
213
|
throw new AccessError("Failed to decrypt");
|
|
@@ -240,15 +239,16 @@ export abstract class MaybeEncrypted<T> {
|
|
|
240
239
|
}
|
|
241
240
|
|
|
242
241
|
decrypt(
|
|
243
|
-
keyResolver?: X25519Keypair | KeyProvider
|
|
242
|
+
keyResolver?: X25519Keypair | KeyProvider,
|
|
244
243
|
): MaybePromise<DecryptedThing<T>>;
|
|
245
244
|
decrypt(provider?: DecryptProvider): MaybePromise<DecryptedThing<T>>;
|
|
246
245
|
|
|
247
246
|
decrypt(
|
|
248
|
-
provider?: X25519Keypair | DecryptProvider | KeyProvider
|
|
247
|
+
provider?: X25519Keypair | DecryptProvider | KeyProvider,
|
|
249
248
|
): MaybePromise<DecryptedThing<T>> | DecryptedThing<T> {
|
|
250
249
|
throw new Error("Not implemented");
|
|
251
250
|
}
|
|
251
|
+
|
|
252
252
|
equals(other: MaybeEncrypted<T>): boolean {
|
|
253
253
|
throw new Error("Not implemented");
|
|
254
254
|
}
|
|
@@ -289,17 +289,17 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
289
289
|
|
|
290
290
|
async encrypt<Parameters extends KeyExchangeOptions>(
|
|
291
291
|
provider: EncryptProvide<Parameters>,
|
|
292
|
-
parameters: Parameters
|
|
292
|
+
parameters: Parameters,
|
|
293
293
|
): Promise<EncryptReturnValue<T, Parameters>>;
|
|
294
294
|
|
|
295
295
|
async encrypt(
|
|
296
296
|
x25519Keypair: X25519Keypair,
|
|
297
|
-
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[]
|
|
297
|
+
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[],
|
|
298
298
|
): Promise<EncryptReturnValue<T, PublicKeyEncryptionParameters>>;
|
|
299
299
|
|
|
300
300
|
async encrypt(
|
|
301
301
|
keypair: EncryptProvide<any> | X25519Keypair,
|
|
302
|
-
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[]
|
|
302
|
+
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[],
|
|
303
303
|
): Promise<EncryptReturnValue<T, any>> {
|
|
304
304
|
let provider: EncryptProvide<any>;
|
|
305
305
|
let options: KeyExchangeOptions;
|
|
@@ -310,7 +310,7 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
310
310
|
| X25519PublicKey
|
|
311
311
|
| Ed25519PublicKey
|
|
312
312
|
)[],
|
|
313
|
-
type: "publicKey"
|
|
313
|
+
type: "publicKey",
|
|
314
314
|
};
|
|
315
315
|
} else {
|
|
316
316
|
provider = keypair;
|
|
@@ -322,17 +322,17 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
322
322
|
const enc = new EncryptedThing<T, EnvelopeFromParameter<any>>({
|
|
323
323
|
encrypted: cipher,
|
|
324
324
|
envelope,
|
|
325
|
-
nonce
|
|
325
|
+
nonce,
|
|
326
326
|
});
|
|
327
327
|
enc._decrypted = this;
|
|
328
328
|
return enc;
|
|
329
329
|
}
|
|
330
330
|
|
|
331
|
-
get decrypted():
|
|
331
|
+
get decrypted(): this {
|
|
332
332
|
return this;
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
-
decrypt():
|
|
335
|
+
decrypt(): this {
|
|
336
336
|
return this;
|
|
337
337
|
}
|
|
338
338
|
|
|
@@ -361,11 +361,9 @@ export class CipherWithNonce {
|
|
|
361
361
|
@field({ type: Uint8Array })
|
|
362
362
|
cipher: Uint8Array;
|
|
363
363
|
|
|
364
|
-
constructor(props
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
this.cipher = props.cipher;
|
|
368
|
-
}
|
|
364
|
+
constructor(props: { nonce: Uint8Array; cipher: Uint8Array }) {
|
|
365
|
+
this.nonce = props.nonce;
|
|
366
|
+
this.cipher = props.cipher;
|
|
369
367
|
}
|
|
370
368
|
|
|
371
369
|
equals(other: CipherWithNonce): boolean {
|
|
@@ -387,14 +385,12 @@ export class K {
|
|
|
387
385
|
@field({ type: X25519PublicKey })
|
|
388
386
|
_receiverPublicKey: X25519PublicKey;
|
|
389
387
|
|
|
390
|
-
constructor(props
|
|
388
|
+
constructor(props: {
|
|
391
389
|
encryptedKey: CipherWithNonce;
|
|
392
390
|
receiverPublicKey: X25519PublicKey;
|
|
393
391
|
}) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
this._receiverPublicKey = props.receiverPublicKey;
|
|
397
|
-
}
|
|
392
|
+
this._encryptedKey = props.encryptedKey;
|
|
393
|
+
this._receiverPublicKey = props.receiverPublicKey;
|
|
398
394
|
}
|
|
399
395
|
|
|
400
396
|
equals(other: K): boolean {
|
|
@@ -421,12 +417,10 @@ class PublicKeyEnvelope extends Envelope {
|
|
|
421
417
|
@field({ type: vec(K) })
|
|
422
418
|
_ks: K[];
|
|
423
419
|
|
|
424
|
-
constructor(props
|
|
420
|
+
constructor(props: { senderPublicKey: X25519PublicKey; ks: K[] }) {
|
|
425
421
|
super();
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this._ks = props.ks;
|
|
429
|
-
}
|
|
422
|
+
this._senderPublicKey = props.senderPublicKey;
|
|
423
|
+
this._ks = props.ks;
|
|
430
424
|
}
|
|
431
425
|
|
|
432
426
|
// TODO: should this be comparable to AbstractEnvelope?
|
|
@@ -455,12 +449,11 @@ class PublicKeyEnvelope extends Envelope {
|
|
|
455
449
|
class HashedKeyEnvelope extends Envelope {
|
|
456
450
|
@field({ type: fixedArray("u8", 32) })
|
|
457
451
|
hash: Uint8Array;
|
|
452
|
+
|
|
458
453
|
// TODO: Do we need a salt here?
|
|
459
|
-
constructor(props
|
|
454
|
+
constructor(props: { hash: Uint8Array }) {
|
|
460
455
|
super();
|
|
461
|
-
|
|
462
|
-
this.hash = props.hash;
|
|
463
|
-
}
|
|
456
|
+
this.hash = props.hash;
|
|
464
457
|
}
|
|
465
458
|
|
|
466
459
|
// TODO: should this be comparable to AbstractEnvelope?
|
|
@@ -479,7 +472,7 @@ class HashedKeyEnvelope extends Envelope {
|
|
|
479
472
|
@variant(1)
|
|
480
473
|
export class EncryptedThing<
|
|
481
474
|
T,
|
|
482
|
-
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope
|
|
475
|
+
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope,
|
|
483
476
|
> extends MaybeEncrypted<T> {
|
|
484
477
|
@field({ type: Uint8Array })
|
|
485
478
|
_encrypted: Uint8Array;
|
|
@@ -490,36 +483,34 @@ export class EncryptedThing<
|
|
|
490
483
|
@field({ type: Envelope })
|
|
491
484
|
_keyexchange: E;
|
|
492
485
|
|
|
493
|
-
constructor(props
|
|
486
|
+
constructor(props: {
|
|
494
487
|
encrypted: Uint8Array;
|
|
495
488
|
nonce: Uint8Array;
|
|
496
489
|
envelope: E;
|
|
497
490
|
}) {
|
|
498
491
|
super();
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
this._keyexchange = props.envelope;
|
|
503
|
-
}
|
|
492
|
+
this._encrypted = props.encrypted;
|
|
493
|
+
this._nonce = props.nonce;
|
|
494
|
+
this._keyexchange = props.envelope;
|
|
504
495
|
}
|
|
505
496
|
|
|
506
497
|
_decrypted?: DecryptedThing<T>;
|
|
507
498
|
get decrypted(): DecryptedThing<T> {
|
|
508
499
|
if (!this._decrypted) {
|
|
509
500
|
throw new Error(
|
|
510
|
-
"Entry has not been decrypted, invoke decrypt method before"
|
|
501
|
+
"Entry has not been decrypted, invoke decrypt method before",
|
|
511
502
|
);
|
|
512
503
|
}
|
|
513
504
|
return this._decrypted;
|
|
514
505
|
}
|
|
515
506
|
|
|
516
507
|
async decrypt(
|
|
517
|
-
keyResolver?: X25519Keypair | KeyProvider
|
|
508
|
+
keyResolver?: X25519Keypair | KeyProvider,
|
|
518
509
|
): Promise<DecryptedThing<T>>;
|
|
519
510
|
async decrypt(provider?: DecryptProvider): Promise<DecryptedThing<T>>;
|
|
520
511
|
|
|
521
512
|
async decrypt(
|
|
522
|
-
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider
|
|
513
|
+
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider,
|
|
523
514
|
): Promise<DecryptedThing<T>> {
|
|
524
515
|
let provider: DecryptProvider | undefined;
|
|
525
516
|
if (typeof providerOrResolver === "function") {
|
|
@@ -554,7 +545,7 @@ export class EncryptedThing<
|
|
|
554
545
|
const decrypted = await provider(
|
|
555
546
|
this._encrypted,
|
|
556
547
|
this._nonce,
|
|
557
|
-
this._keyexchange
|
|
548
|
+
this._keyexchange,
|
|
558
549
|
);
|
|
559
550
|
if (decrypted) {
|
|
560
551
|
const der = deserialize(decrypted, DecryptedThing);
|
package/src/errors.ts
CHANGED
package/src/hash.browser.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { toBase64 } from "./utils.js";
|
|
2
1
|
import { SHA256 } from "@stablelib/sha256";
|
|
2
|
+
import { toBase64 } from "./utils.js";
|
|
3
|
+
|
|
3
4
|
export const sha256Base64 = async (bytes: Uint8Array): Promise<string> =>
|
|
4
5
|
toBase64(await sha256(bytes));
|
|
5
6
|
export const sha256Base64Sync = (bytes: Uint8Array): string =>
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import libsodium from "libsodium-wrappers";
|
|
2
|
+
|
|
1
3
|
export * from "./key.js";
|
|
2
4
|
export * from "./ed25519.js";
|
|
3
5
|
export * from "./signature.js";
|
|
@@ -10,6 +12,6 @@ export * from "./hash.js";
|
|
|
10
12
|
export * from "./random.js";
|
|
11
13
|
export * from "./prehash.js";
|
|
12
14
|
export * from "./signer.js";
|
|
13
|
-
|
|
15
|
+
|
|
14
16
|
const ready = libsodium.ready;
|
|
15
17
|
export { ready };
|
package/src/key.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { field, serialize } from "@dao-xyz/borsh";
|
|
2
|
-
import { sha256Base64Sync } from "./hash.js";
|
|
3
2
|
import { type PeerId } from "@libp2p/interface";
|
|
4
3
|
import { compare } from "uint8arrays";
|
|
4
|
+
import { sha256Base64Sync } from "./hash.js";
|
|
5
5
|
import { toHexString } from "./utils.js";
|
|
6
6
|
|
|
7
7
|
interface Key {
|
|
@@ -27,11 +27,12 @@ export abstract class Keypair {
|
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
// ---- SIGNATURE KEYS -----
|
|
30
|
-
export interface PublicSignKey extends Key {
|
|
30
|
+
export interface PublicSignKey extends Key {}
|
|
31
31
|
export abstract class PublicSignKey implements Key {
|
|
32
|
-
private _bytes
|
|
32
|
+
private _bytes!: Uint8Array;
|
|
33
|
+
|
|
33
34
|
get bytes(): Uint8Array {
|
|
34
|
-
return this._bytes || this._bytes
|
|
35
|
+
return this._bytes || (this._bytes = serialize(this));
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
hashcode(): string {
|
|
@@ -43,7 +44,7 @@ export abstract class PublicSignKey implements Key {
|
|
|
43
44
|
}
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
export interface PrivateSignKey extends Key {
|
|
47
|
+
export interface PrivateSignKey extends Key {}
|
|
47
48
|
export abstract class PrivateSignKey implements Key {
|
|
48
49
|
get bytes(): Uint8Array {
|
|
49
50
|
return serialize(this);
|
|
@@ -55,7 +56,7 @@ export abstract class PrivateSignKey implements Key {
|
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
// ---- PUBLIC KEY ENCRYPTION -----
|
|
58
|
-
export interface PublicKeyEncryptionKey extends Key {
|
|
59
|
+
export interface PublicKeyEncryptionKey extends Key {}
|
|
59
60
|
export abstract class PublicKeyEncryptionKey implements Key {
|
|
60
61
|
get bytes(): Uint8Array {
|
|
61
62
|
return serialize(this);
|
|
@@ -65,7 +66,7 @@ export abstract class PublicKeyEncryptionKey implements Key {
|
|
|
65
66
|
return this._hashcode || (this._hashcode = sha256Base64Sync(this.bytes));
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
|
-
export interface PrivateEncryptionKey extends Key {
|
|
69
|
+
export interface PrivateEncryptionKey extends Key {}
|
|
69
70
|
export abstract class PrivateEncryptionKey implements Key {
|
|
70
71
|
get bytes(): Uint8Array {
|
|
71
72
|
return serialize(this);
|
|
@@ -77,7 +78,7 @@ export abstract class PrivateEncryptionKey implements Key {
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
// ---- OTHER KEYS ----
|
|
80
|
-
export interface PlainKey extends Key {
|
|
81
|
+
export interface PlainKey extends Key {}
|
|
81
82
|
export abstract class PlainKey implements Key {
|
|
82
83
|
get bytes(): Uint8Array {
|
|
83
84
|
return serialize(this);
|
package/src/peerid.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js";
|
|
|
3
3
|
import { Secp256k1Keypair, Secp256k1PublicKey } from "./sepc256k1.js";
|
|
4
4
|
|
|
5
5
|
export const getKeypairFromPeerId = (
|
|
6
|
-
peerId: PeerId
|
|
6
|
+
peerId: PeerId,
|
|
7
7
|
): Ed25519Keypair | Secp256k1Keypair => {
|
|
8
8
|
if (peerId.type === "Ed25519") {
|
|
9
9
|
return Ed25519Keypair.fromPeerId(peerId);
|
|
@@ -15,7 +15,7 @@ export const getKeypairFromPeerId = (
|
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
export const getPublicKeyFromPeerId = (
|
|
18
|
-
peerId: PeerId
|
|
18
|
+
peerId: PeerId,
|
|
19
19
|
): Ed25519PublicKey | Secp256k1PublicKey => {
|
|
20
20
|
if (peerId.type === "Ed25519") {
|
|
21
21
|
return Ed25519PublicKey.fromPeerId(peerId);
|
package/src/prehash.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { sha256 } from "./hash.js";
|
|
2
|
-
import sha3 from "js-sha3";
|
|
3
1
|
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
2
|
+
import sha3 from "js-sha3";
|
|
4
3
|
import { concat } from "uint8arrays";
|
|
4
|
+
import { sha256 } from "./hash.js";
|
|
5
5
|
|
|
6
6
|
const messagePrefix = "\x19Ethereum Signed Message:\n";
|
|
7
7
|
const ethKeccak256Hash = (message: Uint8Array) =>
|
|
@@ -11,22 +11,22 @@ const ethKeccak256Hash = (message: Uint8Array) =>
|
|
|
11
11
|
concat([
|
|
12
12
|
toUtf8Bytes(messagePrefix),
|
|
13
13
|
toUtf8Bytes(String(message.length)),
|
|
14
|
-
message
|
|
15
|
-
])
|
|
14
|
+
message,
|
|
15
|
+
]),
|
|
16
16
|
)
|
|
17
|
-
.arrayBuffer()
|
|
17
|
+
.arrayBuffer(),
|
|
18
18
|
);
|
|
19
19
|
|
|
20
20
|
export enum PreHash {
|
|
21
21
|
NONE = 0,
|
|
22
22
|
SHA_256 = 1,
|
|
23
|
-
//BLAKE3 = 2,
|
|
24
|
-
ETH_KECCAK_256 = 3
|
|
23
|
+
// BLAKE3 = 2,
|
|
24
|
+
ETH_KECCAK_256 = 3,
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export const prehashFn = (
|
|
28
28
|
data: Uint8Array,
|
|
29
|
-
prehash: PreHash
|
|
29
|
+
prehash: PreHash,
|
|
30
30
|
): Promise<Uint8Array> | Uint8Array => {
|
|
31
31
|
if (prehash === PreHash.NONE) {
|
|
32
32
|
return data;
|
package/src/random.ts
CHANGED