@peerbit/crypto 2.2.0 → 2.3.0-55cebfe
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.d.ts +2 -0
- package/dist/benchmark/hash.d.ts.map +1 -0
- package/dist/benchmark/hash.js +69 -0
- package/dist/benchmark/hash.js.map +1 -0
- package/dist/benchmark/index.d.ts +2 -0
- package/dist/benchmark/index.d.ts.map +1 -0
- package/dist/benchmark/index.js +64 -0
- package/dist/benchmark/index.js.map +1 -0
- package/dist/benchmark/peer-ids.d.ts +2 -0
- package/dist/benchmark/peer-ids.d.ts.map +1 -0
- package/dist/benchmark/peer-ids.js +29 -0
- package/dist/benchmark/peer-ids.js.map +1 -0
- package/{lib/esm → dist/src}/bytes.d.ts +1 -0
- package/dist/src/bytes.d.ts.map +1 -0
- package/{lib/esm → dist/src}/bytes.js.map +1 -1
- package/{lib/esm/ed25519-sign-browser.d.ts → dist/src/ed25519-sign.browser.d.ts} +3 -2
- package/dist/src/ed25519-sign.browser.d.ts.map +1 -0
- package/{lib/esm/ed25519-sign-browser.js → dist/src/ed25519-sign.browser.js} +2 -2
- package/dist/src/ed25519-sign.browser.js.map +1 -0
- package/{lib/esm → dist/src}/ed25519-sign.d.ts +3 -2
- package/dist/src/ed25519-sign.d.ts.map +1 -0
- package/{lib/esm → dist/src}/ed25519-sign.js +8 -7
- package/dist/src/ed25519-sign.js.map +1 -0
- package/{lib/esm → dist/src}/ed25519.d.ts +4 -3
- package/dist/src/ed25519.d.ts.map +1 -0
- package/{lib/esm → dist/src}/ed25519.js +14 -13
- package/dist/src/ed25519.js.map +1 -0
- package/{lib/esm → dist/src}/encryption.d.ts +15 -11
- package/dist/src/encryption.d.ts.map +1 -0
- package/{lib/esm → dist/src}/encryption.js +33 -35
- package/dist/src/encryption.js.map +1 -0
- package/{lib/esm → dist/src}/errors.d.ts +1 -1
- package/dist/src/errors.d.ts.map +1 -0
- package/dist/src/errors.js +3 -0
- package/{lib/esm → dist/src}/errors.js.map +1 -1
- package/{lib/esm/hash-browser.d.ts → dist/src/hash.browser.d.ts} +1 -0
- package/dist/src/hash.browser.d.ts.map +1 -0
- package/{lib/esm/hash-browser.js → dist/src/hash.browser.js} +2 -2
- package/{lib/esm/hash-browser.js.map → dist/src/hash.browser.js.map} +1 -1
- package/{lib/esm → dist/src}/hash.d.ts +1 -0
- package/dist/src/hash.d.ts.map +1 -0
- package/{lib/esm → dist/src}/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/{lib/esm → dist/src}/index.js +1 -1
- package/dist/src/index.js.map +1 -0
- package/{lib/esm → dist/src}/key.d.ts +2 -1
- package/dist/src/key.d.ts.map +1 -0
- package/{lib/esm → dist/src}/key.js +3 -2
- package/dist/src/key.js.map +1 -0
- package/{lib/esm → dist/src}/peerid.d.ts +2 -1
- package/dist/src/peerid.d.ts.map +1 -0
- package/{lib/esm → dist/src}/peerid.js +1 -0
- package/dist/src/peerid.js.map +1 -0
- package/{lib/esm → dist/src}/prehash.d.ts +1 -0
- package/dist/src/prehash.d.ts.map +1 -0
- package/{lib/esm → dist/src}/prehash.js +4 -4
- package/{lib/esm → dist/src}/prehash.js.map +1 -1
- package/{lib/esm/random-browser.d.ts → dist/src/random.browser.d.ts} +1 -0
- package/dist/src/random.browser.d.ts.map +1 -0
- package/{lib/esm/random-browser.js → dist/src/random.browser.js} +1 -1
- package/dist/src/random.browser.js.map +1 -0
- package/{lib/esm → dist/src}/random.d.ts +1 -1
- package/dist/src/random.d.ts.map +1 -0
- package/{lib/esm → dist/src}/random.js.map +1 -1
- package/{lib/esm → dist/src}/sepc256k1.d.ts +6 -5
- package/dist/src/sepc256k1.d.ts.map +1 -0
- package/{lib/esm → dist/src}/sepc256k1.js +19 -19
- package/dist/src/sepc256k1.js.map +1 -0
- package/{lib/esm → dist/src}/signature.d.ts +4 -7
- package/dist/src/signature.d.ts.map +1 -0
- package/dist/src/signature.js +113 -0
- package/dist/src/signature.js.map +1 -0
- package/{lib/esm → dist/src}/signer.d.ts +4 -3
- package/dist/src/signer.d.ts.map +1 -0
- package/{lib/esm → dist/src}/utils.d.ts +2 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/{lib/esm → dist/src}/utils.js +4 -0
- package/dist/src/utils.js.map +1 -0
- package/{lib/esm → dist/src}/x25519.d.ts +4 -3
- package/dist/src/x25519.d.ts.map +1 -0
- package/{lib/esm → dist/src}/x25519.js +15 -14
- package/dist/src/x25519.js.map +1 -0
- package/package.json +77 -54
- package/src/bytes.ts +1 -1
- package/src/{ed25519-sign-browser.ts → ed25519-sign.browser.ts} +7 -7
- package/src/ed25519-sign.ts +11 -11
- package/src/ed25519.ts +21 -21
- package/src/encryption.ts +69 -68
- package/src/errors.ts +1 -5
- package/src/{hash-browser.ts → hash.browser.ts} +2 -1
- package/src/index.ts +3 -1
- package/src/key.ts +6 -5
- package/src/peerid.ts +3 -3
- package/src/prehash.ts +8 -8
- package/src/random.ts +1 -0
- package/src/sepc256k1.ts +39 -37
- package/src/signature.ts +16 -190
- package/src/signer.ts +5 -5
- package/src/utils.ts +6 -0
- package/src/x25519.ts +26 -28
- package/LICENSE +0 -202
- package/lib/esm/ed25519-sign-browser.js.map +0 -1
- package/lib/esm/ed25519-sign.js.map +0 -1
- package/lib/esm/ed25519.js.map +0 -1
- package/lib/esm/encryption.js.map +0 -1
- package/lib/esm/errors.js +0 -6
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/key.js.map +0 -1
- package/lib/esm/peerid.js.map +0 -1
- package/lib/esm/random-browser.js.map +0 -1
- package/lib/esm/sepc256k1.js.map +0 -1
- package/lib/esm/signature.js +0 -285
- package/lib/esm/signature.js.map +0 -1
- package/lib/esm/utils.js.map +0 -1
- package/lib/esm/x25519.js.map +0 -1
- /package/{lib/esm → dist/src}/bytes.js +0 -0
- /package/{lib/esm → dist/src}/hash.js +0 -0
- /package/{lib/esm → dist/src}/hash.js.map +0 -0
- /package/{lib/esm → dist/src}/random.js +0 -0
- /package/{lib/esm → dist/src}/signer.js +0 -0
- /package/{lib/esm → dist/src}/signer.js.map +0 -0
- /package/src/{random-browser.ts → random.browser.ts} +0 -0
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 "@peerbit/uint8arrays";
|
|
4
|
-
import { Identity, Signer, 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,21 +1,30 @@
|
|
|
1
|
-
export * from "./errors.js";
|
|
2
|
-
|
|
3
1
|
import {
|
|
4
|
-
AbstractType,
|
|
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 { equals } from "@peerbit/uint8arrays";
|
|
13
|
-
import { AccessError } from "./errors.js";
|
|
14
10
|
import sodium from "libsodium-wrappers";
|
|
15
|
-
import {
|
|
11
|
+
import { equals as uequals } from "uint8arrays";
|
|
16
12
|
import { Ed25519PublicKey } from "./ed25519.js";
|
|
17
|
-
import {
|
|
13
|
+
import { AccessError } from "./errors.js";
|
|
18
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";
|
|
19
|
+
|
|
20
|
+
export const equals = (array1?: Uint8Array, array2?: Uint8Array) => {
|
|
21
|
+
if (!!array1 !== !!array2) return false;
|
|
22
|
+
if (!array1 || !array2) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return uequals(array1, array2);
|
|
26
|
+
};
|
|
27
|
+
|
|
19
28
|
export type MaybePromise<T> = Promise<T> | T;
|
|
20
29
|
|
|
21
30
|
export type PublicKeyEncryptionParameters = {
|
|
@@ -38,7 +47,7 @@ export type KeyExchangeOptions =
|
|
|
38
47
|
|
|
39
48
|
type EncryptReturnValue<
|
|
40
49
|
T,
|
|
41
|
-
Parameters extends KeyExchangeOptions
|
|
50
|
+
Parameters extends KeyExchangeOptions,
|
|
42
51
|
> = EncryptedThing<T, EnvelopeFromParameter<Parameters>>;
|
|
43
52
|
|
|
44
53
|
type CipherWithEnvelope<E = PublicKeyEnvelope | HashedKeyEnvelope> = {
|
|
@@ -51,14 +60,14 @@ type SymmetricKeys = Uint8Array;
|
|
|
51
60
|
type PublicKeyEncryptionKeys = X25519Keypair;
|
|
52
61
|
|
|
53
62
|
function isAsymmetriEncryptionParameters(
|
|
54
|
-
parameters: KeyExchangeOptions
|
|
63
|
+
parameters: KeyExchangeOptions,
|
|
55
64
|
): parameters is PublicKeyEncryptionParameters {
|
|
56
65
|
return (
|
|
57
66
|
(parameters as PublicKeyEncryptionParameters).receiverPublicKeys != null
|
|
58
67
|
);
|
|
59
68
|
}
|
|
60
69
|
function isAsymmetricEncryptionKeys(
|
|
61
|
-
parameters: PublicKeyEncryptionKeys | SymmetricKeys
|
|
70
|
+
parameters: PublicKeyEncryptionKeys | SymmetricKeys,
|
|
62
71
|
): parameters is PublicKeyEncryptionKeys {
|
|
63
72
|
return (parameters as PublicKeyEncryptionKeys) instanceof X25519Keypair;
|
|
64
73
|
}
|
|
@@ -70,7 +79,7 @@ type EnvelopeFromParameter<Parameters extends KeyExchangeOptions> =
|
|
|
70
79
|
|
|
71
80
|
type EncryptProvide<Parameters extends KeyExchangeOptions> = (
|
|
72
81
|
bytes: Uint8Array,
|
|
73
|
-
parameters: Parameters
|
|
82
|
+
parameters: Parameters,
|
|
74
83
|
) => Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>>;
|
|
75
84
|
|
|
76
85
|
interface KeyProvider {
|
|
@@ -81,13 +90,13 @@ export const createLocalEncryptProvider = <
|
|
|
81
90
|
K extends PublicKeyEncryptionKeys | SymmetricKeys,
|
|
82
91
|
Parameters extends KeyExchangeOptions = K extends PublicKeyEncryptionKeys
|
|
83
92
|
? PublicKeyEncryptionParameters
|
|
84
|
-
: SymmetricKeyEncryptionParameters
|
|
93
|
+
: SymmetricKeyEncryptionParameters,
|
|
85
94
|
>(
|
|
86
|
-
keys: K
|
|
95
|
+
keys: K,
|
|
87
96
|
) => {
|
|
88
97
|
return async (
|
|
89
98
|
bytes: Uint8Array,
|
|
90
|
-
parameters: Parameters
|
|
99
|
+
parameters: Parameters,
|
|
91
100
|
): Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>> => {
|
|
92
101
|
const nonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodim random
|
|
93
102
|
if (
|
|
@@ -103,7 +112,7 @@ export const createLocalEncryptProvider = <
|
|
|
103
112
|
return X25519PublicKey.from(key);
|
|
104
113
|
}
|
|
105
114
|
return key;
|
|
106
|
-
})
|
|
115
|
+
}),
|
|
107
116
|
);
|
|
108
117
|
|
|
109
118
|
const ks = receiverX25519PublicKeys.map((receiverPublicKey) => {
|
|
@@ -114,11 +123,11 @@ export const createLocalEncryptProvider = <
|
|
|
114
123
|
epheremalKey,
|
|
115
124
|
kNonce,
|
|
116
125
|
receiverPublicKey.publicKey,
|
|
117
|
-
keys.secretKey.secretKey
|
|
126
|
+
keys.secretKey.secretKey,
|
|
118
127
|
),
|
|
119
|
-
nonce: kNonce
|
|
128
|
+
nonce: kNonce,
|
|
120
129
|
}),
|
|
121
|
-
receiverPublicKey
|
|
130
|
+
receiverPublicKey,
|
|
122
131
|
});
|
|
123
132
|
});
|
|
124
133
|
|
|
@@ -127,8 +136,8 @@ export const createLocalEncryptProvider = <
|
|
|
127
136
|
nonce,
|
|
128
137
|
envelope: new PublicKeyEnvelope({
|
|
129
138
|
senderPublicKey: keys.publicKey,
|
|
130
|
-
ks
|
|
131
|
-
}) as EnvelopeFromParameter<Parameters
|
|
139
|
+
ks,
|
|
140
|
+
}) as EnvelopeFromParameter<Parameters>,
|
|
132
141
|
};
|
|
133
142
|
} else if (
|
|
134
143
|
!isAsymmetriEncryptionParameters(parameters) &&
|
|
@@ -139,8 +148,8 @@ export const createLocalEncryptProvider = <
|
|
|
139
148
|
cipher: new Uint8Array(cipher), // TODO do we need this clone?
|
|
140
149
|
nonce,
|
|
141
150
|
envelope: new HashedKeyEnvelope({
|
|
142
|
-
hash: await sha256(keys)
|
|
143
|
-
}) as EnvelopeFromParameter<Parameters
|
|
151
|
+
hash: await sha256(keys),
|
|
152
|
+
}) as EnvelopeFromParameter<Parameters>,
|
|
144
153
|
};
|
|
145
154
|
}
|
|
146
155
|
|
|
@@ -151,22 +160,22 @@ export const createLocalEncryptProvider = <
|
|
|
151
160
|
export type DecryptProvider = (
|
|
152
161
|
encrypted: Uint8Array,
|
|
153
162
|
nonce: Uint8Array,
|
|
154
|
-
exchange: Envelope
|
|
163
|
+
exchange: Envelope,
|
|
155
164
|
) => Promise<Uint8Array>;
|
|
156
165
|
|
|
157
166
|
type KeyResolver = <T extends X25519PublicKey | Uint8Array>(
|
|
158
|
-
key: T
|
|
167
|
+
key: T,
|
|
159
168
|
) => Promise<
|
|
160
169
|
(T extends X25519PublicKey ? X25519Keypair : Uint8Array) | undefined
|
|
161
170
|
>;
|
|
162
171
|
|
|
163
172
|
export const createDecrypterFromKeyResolver = (
|
|
164
|
-
keyResolver: KeyResolver
|
|
173
|
+
keyResolver: KeyResolver,
|
|
165
174
|
): DecryptProvider => {
|
|
166
175
|
return async (
|
|
167
176
|
encrypted: Uint8Array,
|
|
168
177
|
nonce: Uint8Array,
|
|
169
|
-
exchange: Envelope
|
|
178
|
+
exchange: Envelope,
|
|
170
179
|
): Promise<Uint8Array> => {
|
|
171
180
|
// We only need to open with one of the keys
|
|
172
181
|
|
|
@@ -179,7 +188,7 @@ export const createDecrypterFromKeyResolver = (
|
|
|
179
188
|
if (exported) {
|
|
180
189
|
key = {
|
|
181
190
|
index: i,
|
|
182
|
-
keypair: exported
|
|
191
|
+
keypair: exported,
|
|
183
192
|
};
|
|
184
193
|
break;
|
|
185
194
|
}
|
|
@@ -198,7 +207,7 @@ export const createDecrypterFromKeyResolver = (
|
|
|
198
207
|
k._encryptedKey.cipher,
|
|
199
208
|
k._encryptedKey.nonce,
|
|
200
209
|
exchange._senderPublicKey.publicKey,
|
|
201
|
-
secretKey.secretKey
|
|
210
|
+
secretKey.secretKey,
|
|
202
211
|
);
|
|
203
212
|
} catch (error) {
|
|
204
213
|
throw new AccessError("Failed to decrypt");
|
|
@@ -230,15 +239,16 @@ export abstract class MaybeEncrypted<T> {
|
|
|
230
239
|
}
|
|
231
240
|
|
|
232
241
|
decrypt(
|
|
233
|
-
keyResolver?: X25519Keypair | KeyProvider
|
|
242
|
+
keyResolver?: X25519Keypair | KeyProvider,
|
|
234
243
|
): MaybePromise<DecryptedThing<T>>;
|
|
235
244
|
decrypt(provider?: DecryptProvider): MaybePromise<DecryptedThing<T>>;
|
|
236
245
|
|
|
237
246
|
decrypt(
|
|
238
|
-
provider?: X25519Keypair | DecryptProvider | KeyProvider
|
|
247
|
+
provider?: X25519Keypair | DecryptProvider | KeyProvider,
|
|
239
248
|
): MaybePromise<DecryptedThing<T>> | DecryptedThing<T> {
|
|
240
249
|
throw new Error("Not implemented");
|
|
241
250
|
}
|
|
251
|
+
|
|
242
252
|
equals(other: MaybeEncrypted<T>): boolean {
|
|
243
253
|
throw new Error("Not implemented");
|
|
244
254
|
}
|
|
@@ -279,17 +289,17 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
279
289
|
|
|
280
290
|
async encrypt<Parameters extends KeyExchangeOptions>(
|
|
281
291
|
provider: EncryptProvide<Parameters>,
|
|
282
|
-
parameters: Parameters
|
|
292
|
+
parameters: Parameters,
|
|
283
293
|
): Promise<EncryptReturnValue<T, Parameters>>;
|
|
284
294
|
|
|
285
295
|
async encrypt(
|
|
286
296
|
x25519Keypair: X25519Keypair,
|
|
287
|
-
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[]
|
|
297
|
+
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[],
|
|
288
298
|
): Promise<EncryptReturnValue<T, PublicKeyEncryptionParameters>>;
|
|
289
299
|
|
|
290
300
|
async encrypt(
|
|
291
301
|
keypair: EncryptProvide<any> | X25519Keypair,
|
|
292
|
-
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[]
|
|
302
|
+
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[],
|
|
293
303
|
): Promise<EncryptReturnValue<T, any>> {
|
|
294
304
|
let provider: EncryptProvide<any>;
|
|
295
305
|
let options: KeyExchangeOptions;
|
|
@@ -300,7 +310,7 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
300
310
|
| X25519PublicKey
|
|
301
311
|
| Ed25519PublicKey
|
|
302
312
|
)[],
|
|
303
|
-
type: "publicKey"
|
|
313
|
+
type: "publicKey",
|
|
304
314
|
};
|
|
305
315
|
} else {
|
|
306
316
|
provider = keypair;
|
|
@@ -312,17 +322,17 @@ export class DecryptedThing<T> extends MaybeEncrypted<T> {
|
|
|
312
322
|
const enc = new EncryptedThing<T, EnvelopeFromParameter<any>>({
|
|
313
323
|
encrypted: cipher,
|
|
314
324
|
envelope,
|
|
315
|
-
nonce
|
|
325
|
+
nonce,
|
|
316
326
|
});
|
|
317
327
|
enc._decrypted = this;
|
|
318
328
|
return enc;
|
|
319
329
|
}
|
|
320
330
|
|
|
321
|
-
get decrypted():
|
|
331
|
+
get decrypted(): this {
|
|
322
332
|
return this;
|
|
323
333
|
}
|
|
324
334
|
|
|
325
|
-
decrypt():
|
|
335
|
+
decrypt(): this {
|
|
326
336
|
return this;
|
|
327
337
|
}
|
|
328
338
|
|
|
@@ -351,11 +361,9 @@ export class CipherWithNonce {
|
|
|
351
361
|
@field({ type: Uint8Array })
|
|
352
362
|
cipher: Uint8Array;
|
|
353
363
|
|
|
354
|
-
constructor(props
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
this.cipher = props.cipher;
|
|
358
|
-
}
|
|
364
|
+
constructor(props: { nonce: Uint8Array; cipher: Uint8Array }) {
|
|
365
|
+
this.nonce = props.nonce;
|
|
366
|
+
this.cipher = props.cipher;
|
|
359
367
|
}
|
|
360
368
|
|
|
361
369
|
equals(other: CipherWithNonce): boolean {
|
|
@@ -377,14 +385,12 @@ export class K {
|
|
|
377
385
|
@field({ type: X25519PublicKey })
|
|
378
386
|
_receiverPublicKey: X25519PublicKey;
|
|
379
387
|
|
|
380
|
-
constructor(props
|
|
388
|
+
constructor(props: {
|
|
381
389
|
encryptedKey: CipherWithNonce;
|
|
382
390
|
receiverPublicKey: X25519PublicKey;
|
|
383
391
|
}) {
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
this._receiverPublicKey = props.receiverPublicKey;
|
|
387
|
-
}
|
|
392
|
+
this._encryptedKey = props.encryptedKey;
|
|
393
|
+
this._receiverPublicKey = props.receiverPublicKey;
|
|
388
394
|
}
|
|
389
395
|
|
|
390
396
|
equals(other: K): boolean {
|
|
@@ -411,12 +417,10 @@ class PublicKeyEnvelope extends Envelope {
|
|
|
411
417
|
@field({ type: vec(K) })
|
|
412
418
|
_ks: K[];
|
|
413
419
|
|
|
414
|
-
constructor(props
|
|
420
|
+
constructor(props: { senderPublicKey: X25519PublicKey; ks: K[] }) {
|
|
415
421
|
super();
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
this._ks = props.ks;
|
|
419
|
-
}
|
|
422
|
+
this._senderPublicKey = props.senderPublicKey;
|
|
423
|
+
this._ks = props.ks;
|
|
420
424
|
}
|
|
421
425
|
|
|
422
426
|
// TODO: should this be comparable to AbstractEnvelope?
|
|
@@ -445,12 +449,11 @@ class PublicKeyEnvelope extends Envelope {
|
|
|
445
449
|
class HashedKeyEnvelope extends Envelope {
|
|
446
450
|
@field({ type: fixedArray("u8", 32) })
|
|
447
451
|
hash: Uint8Array;
|
|
452
|
+
|
|
448
453
|
// TODO: Do we need a salt here?
|
|
449
|
-
constructor(props
|
|
454
|
+
constructor(props: { hash: Uint8Array }) {
|
|
450
455
|
super();
|
|
451
|
-
|
|
452
|
-
this.hash = props.hash;
|
|
453
|
-
}
|
|
456
|
+
this.hash = props.hash;
|
|
454
457
|
}
|
|
455
458
|
|
|
456
459
|
// TODO: should this be comparable to AbstractEnvelope?
|
|
@@ -469,7 +472,7 @@ class HashedKeyEnvelope extends Envelope {
|
|
|
469
472
|
@variant(1)
|
|
470
473
|
export class EncryptedThing<
|
|
471
474
|
T,
|
|
472
|
-
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope
|
|
475
|
+
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope,
|
|
473
476
|
> extends MaybeEncrypted<T> {
|
|
474
477
|
@field({ type: Uint8Array })
|
|
475
478
|
_encrypted: Uint8Array;
|
|
@@ -480,36 +483,34 @@ export class EncryptedThing<
|
|
|
480
483
|
@field({ type: Envelope })
|
|
481
484
|
_keyexchange: E;
|
|
482
485
|
|
|
483
|
-
constructor(props
|
|
486
|
+
constructor(props: {
|
|
484
487
|
encrypted: Uint8Array;
|
|
485
488
|
nonce: Uint8Array;
|
|
486
489
|
envelope: E;
|
|
487
490
|
}) {
|
|
488
491
|
super();
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
this._keyexchange = props.envelope;
|
|
493
|
-
}
|
|
492
|
+
this._encrypted = props.encrypted;
|
|
493
|
+
this._nonce = props.nonce;
|
|
494
|
+
this._keyexchange = props.envelope;
|
|
494
495
|
}
|
|
495
496
|
|
|
496
497
|
_decrypted?: DecryptedThing<T>;
|
|
497
498
|
get decrypted(): DecryptedThing<T> {
|
|
498
499
|
if (!this._decrypted) {
|
|
499
500
|
throw new Error(
|
|
500
|
-
"Entry has not been decrypted, invoke decrypt method before"
|
|
501
|
+
"Entry has not been decrypted, invoke decrypt method before",
|
|
501
502
|
);
|
|
502
503
|
}
|
|
503
504
|
return this._decrypted;
|
|
504
505
|
}
|
|
505
506
|
|
|
506
507
|
async decrypt(
|
|
507
|
-
keyResolver?: X25519Keypair | KeyProvider
|
|
508
|
+
keyResolver?: X25519Keypair | KeyProvider,
|
|
508
509
|
): Promise<DecryptedThing<T>>;
|
|
509
510
|
async decrypt(provider?: DecryptProvider): Promise<DecryptedThing<T>>;
|
|
510
511
|
|
|
511
512
|
async decrypt(
|
|
512
|
-
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider
|
|
513
|
+
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider,
|
|
513
514
|
): Promise<DecryptedThing<T>> {
|
|
514
515
|
let provider: DecryptProvider | undefined;
|
|
515
516
|
if (typeof providerOrResolver === "function") {
|
|
@@ -544,7 +545,7 @@ export class EncryptedThing<
|
|
|
544
545
|
const decrypted = await provider(
|
|
545
546
|
this._encrypted,
|
|
546
547
|
this._nonce,
|
|
547
|
-
this._keyexchange
|
|
548
|
+
this._keyexchange,
|
|
548
549
|
);
|
|
549
550
|
if (decrypted) {
|
|
550
551
|
const der = deserialize(decrypted, DecryptedThing);
|
package/src/errors.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { toBase64 } from "./utils";
|
|
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
|
-
import {
|
|
1
|
+
import { field, serialize } from "@dao-xyz/borsh";
|
|
2
|
+
import { type PeerId } from "@libp2p/interface";
|
|
3
|
+
import { compare } from "uint8arrays";
|
|
2
4
|
import { sha256Base64Sync } from "./hash.js";
|
|
3
|
-
import { PeerId } from "@libp2p/interface";
|
|
4
|
-
import { compare } from "@peerbit/uint8arrays";
|
|
5
5
|
import { toHexString } from "./utils.js";
|
|
6
6
|
|
|
7
7
|
interface Key {
|
|
@@ -29,9 +29,10 @@ export abstract class Keypair {
|
|
|
29
29
|
// ---- SIGNATURE KEYS -----
|
|
30
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 {
|
package/src/peerid.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { PeerId } from "@libp2p/interface";
|
|
1
|
+
import { type PeerId } from "@libp2p/interface";
|
|
2
2
|
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