@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/sepc256k1.ts
CHANGED
|
@@ -1,24 +1,26 @@
|
|
|
1
|
-
import { field, fixedArray, variant
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
2
|
+
import {
|
|
3
|
+
type SignatureLike,
|
|
4
|
+
arrayify,
|
|
5
|
+
joinSignature,
|
|
6
|
+
splitSignature,
|
|
7
|
+
} from "@ethersproject/bytes";
|
|
4
8
|
import { Wallet } from "@ethersproject/wallet";
|
|
5
|
-
import { arrayify } from "@ethersproject/bytes";
|
|
6
|
-
import { joinSignature } from "@ethersproject/bytes";
|
|
7
|
-
import { SignatureLike, splitSignature } from "@ethersproject/bytes";
|
|
8
|
-
import _ec from "elliptic";
|
|
9
|
-
import EC = _ec.ec;
|
|
10
|
-
let _curve: EC;
|
|
11
|
-
|
|
12
|
-
import { equals } from "@peerbit/uint8arrays";
|
|
13
|
-
import { toHexString } from "./utils.js";
|
|
14
|
-
import { PeerId } from "@libp2p/interface";
|
|
15
|
-
import { Identity } from "./signer.js";
|
|
16
|
-
import { coerce } from "./bytes.js";
|
|
17
9
|
import { generateKeyPair, supportedKeys } from "@libp2p/crypto/keys";
|
|
10
|
+
import { type PeerId } from "@libp2p/interface";
|
|
11
|
+
import { peerIdFromKeys } from "@libp2p/peer-id";
|
|
18
12
|
import utf8 from "@protobufjs/utf8";
|
|
19
|
-
import
|
|
13
|
+
import _ec from "elliptic";
|
|
14
|
+
import { equals } from "uint8arrays";
|
|
15
|
+
import { coerce } from "./bytes.js";
|
|
16
|
+
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js";
|
|
20
17
|
import { PreHash, prehashFn } from "./prehash.js";
|
|
21
|
-
import {
|
|
18
|
+
import { SignatureWithKey } from "./signature.js";
|
|
19
|
+
import { type Identity } from "./signer.js";
|
|
20
|
+
import { toHexString } from "./utils.js";
|
|
21
|
+
|
|
22
|
+
import EC = _ec.ec;
|
|
23
|
+
let _curve: EC;
|
|
22
24
|
|
|
23
25
|
@variant(1)
|
|
24
26
|
export class Secp256k1PublicKey extends PublicSignKey {
|
|
@@ -43,7 +45,7 @@ export class Secp256k1PublicKey extends PublicSignKey {
|
|
|
43
45
|
// So we can recover the public key
|
|
44
46
|
const publicKey = recoverPublicKeyFromSignature(
|
|
45
47
|
await prehashFn(toSign, PreHash.ETH_KECCAK_256),
|
|
46
|
-
signature
|
|
48
|
+
signature,
|
|
47
49
|
);
|
|
48
50
|
|
|
49
51
|
return new Secp256k1PublicKey({ publicKey });
|
|
@@ -62,7 +64,7 @@ export class Secp256k1PublicKey extends PublicSignKey {
|
|
|
62
64
|
|
|
63
65
|
toPeerId(): Promise<PeerId> {
|
|
64
66
|
return peerIdFromKeys(
|
|
65
|
-
new supportedKeys["secp256k1"].Secp256k1PublicKey(this.publicKey).bytes
|
|
67
|
+
new supportedKeys["secp256k1"].Secp256k1PublicKey(this.publicKey).bytes,
|
|
66
68
|
);
|
|
67
69
|
}
|
|
68
70
|
|
|
@@ -72,7 +74,7 @@ export class Secp256k1PublicKey extends PublicSignKey {
|
|
|
72
74
|
}
|
|
73
75
|
if (id.type === "secp256k1") {
|
|
74
76
|
return new Secp256k1PublicKey({
|
|
75
|
-
publicKey: id.publicKey.slice(4) // computeAddress(!.slice(4)),
|
|
77
|
+
publicKey: id.publicKey.slice(4), // computeAddress(!.slice(4)),
|
|
76
78
|
});
|
|
77
79
|
}
|
|
78
80
|
throw new Error("Unsupported key type: " + id.type);
|
|
@@ -110,7 +112,7 @@ export class Secp256k1PrivateKey extends PrivateSignKey {
|
|
|
110
112
|
}
|
|
111
113
|
if (id.type === "secp256k1") {
|
|
112
114
|
return new Secp256k1PrivateKey({
|
|
113
|
-
privateKey: coerce(id.privateKey
|
|
115
|
+
privateKey: coerce(id.privateKey.slice(4)),
|
|
114
116
|
});
|
|
115
117
|
}
|
|
116
118
|
throw new Error("Unsupported key type: " + id.type);
|
|
@@ -125,7 +127,7 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
125
127
|
@field({ type: Secp256k1PrivateKey })
|
|
126
128
|
privateKey: Secp256k1PrivateKey;
|
|
127
129
|
|
|
128
|
-
_wallet
|
|
130
|
+
_wallet!: Wallet;
|
|
129
131
|
constructor(properties: {
|
|
130
132
|
publicKey: Secp256k1PublicKey;
|
|
131
133
|
privateKey: Secp256k1PrivateKey;
|
|
@@ -139,11 +141,11 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
139
141
|
const generated = await generateKeyPair("secp256k1");
|
|
140
142
|
const kp = new Secp256k1Keypair({
|
|
141
143
|
publicKey: new Secp256k1PublicKey({
|
|
142
|
-
publicKey: generated.public.marshal()
|
|
144
|
+
publicKey: generated.public.marshal(),
|
|
143
145
|
}),
|
|
144
146
|
privateKey: new Secp256k1PrivateKey({
|
|
145
|
-
privateKey: generated.marshal()
|
|
146
|
-
})
|
|
147
|
+
privateKey: generated.marshal(),
|
|
148
|
+
}),
|
|
147
149
|
});
|
|
148
150
|
|
|
149
151
|
return kp;
|
|
@@ -151,14 +153,14 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
151
153
|
|
|
152
154
|
async sign(
|
|
153
155
|
data: Uint8Array,
|
|
154
|
-
prehash: PreHash = PreHash.ETH_KECCAK_256
|
|
156
|
+
prehash: PreHash = PreHash.ETH_KECCAK_256,
|
|
155
157
|
): Promise<SignatureWithKey> {
|
|
156
158
|
const maybeHashed = await prehashFn(data, prehash);
|
|
157
159
|
|
|
158
160
|
const signature = joinSignature(
|
|
159
161
|
(this._wallet || (this._wallet = new Wallet(this.privateKey.privateKey)))
|
|
160
162
|
._signingKey()
|
|
161
|
-
.signDigest(maybeHashed)
|
|
163
|
+
.signDigest(maybeHashed),
|
|
162
164
|
);
|
|
163
165
|
const signatureBytes = new Uint8Array(utf8.length(signature)); // TODO utilize Buffer allocUnsafe
|
|
164
166
|
utf8.write(signature, signatureBytes, 0);
|
|
@@ -166,7 +168,7 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
166
168
|
return new SignatureWithKey({
|
|
167
169
|
prehash,
|
|
168
170
|
publicKey: this.publicKey,
|
|
169
|
-
signature: signatureBytes
|
|
171
|
+
signature: signatureBytes,
|
|
170
172
|
});
|
|
171
173
|
}
|
|
172
174
|
|
|
@@ -183,19 +185,19 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
183
185
|
static fromPeerId(peerId: PeerId) {
|
|
184
186
|
return new Secp256k1Keypair({
|
|
185
187
|
privateKey: Secp256k1PrivateKey.from(peerId),
|
|
186
|
-
publicKey: Secp256k1PublicKey.fromPeerId(peerId)
|
|
188
|
+
publicKey: Secp256k1PublicKey.fromPeerId(peerId),
|
|
187
189
|
});
|
|
188
190
|
}
|
|
189
191
|
|
|
190
192
|
toPeerId(): Promise<PeerId> {
|
|
191
193
|
return peerIdFromKeys(
|
|
192
194
|
new supportedKeys["secp256k1"].Secp256k1PublicKey(
|
|
193
|
-
this.publicKey.publicKey
|
|
195
|
+
this.publicKey.publicKey,
|
|
194
196
|
).bytes,
|
|
195
197
|
new supportedKeys["secp256k1"].Secp256k1PrivateKey(
|
|
196
198
|
this.privateKey.privateKey,
|
|
197
|
-
this.publicKey.publicKey
|
|
198
|
-
).bytes
|
|
199
|
+
this.publicKey.publicKey,
|
|
200
|
+
).bytes,
|
|
199
201
|
);
|
|
200
202
|
}
|
|
201
203
|
}
|
|
@@ -211,28 +213,28 @@ function getCurve() {
|
|
|
211
213
|
|
|
212
214
|
export const recoverPublicKeyFromSignature = (
|
|
213
215
|
digest: Uint8Array,
|
|
214
|
-
signature: SignatureLike
|
|
216
|
+
signature: SignatureLike,
|
|
215
217
|
): Uint8Array => {
|
|
216
218
|
const sig = splitSignature(signature);
|
|
217
219
|
const rs = { r: arrayify(sig.r), s: arrayify(sig.s) };
|
|
218
220
|
return new Uint8Array(
|
|
219
221
|
getCurve()
|
|
220
222
|
.recoverPubKey(arrayify(digest), rs, sig.recoveryParam)
|
|
221
|
-
.encodeCompressed()
|
|
223
|
+
.encodeCompressed(),
|
|
222
224
|
);
|
|
223
225
|
};
|
|
224
226
|
|
|
225
227
|
export const verifySignatureSecp256k1 = async (
|
|
226
228
|
signature: SignatureWithKey,
|
|
227
|
-
data: Uint8Array
|
|
229
|
+
data: Uint8Array,
|
|
228
230
|
): Promise<boolean> => {
|
|
229
231
|
const hashedData = await prehashFn(data, signature.prehash);
|
|
230
232
|
const signerKey = recoverPublicKeyFromSignature(
|
|
231
233
|
arrayify(hashedData),
|
|
232
|
-
decoder.decode(signature.signature)
|
|
234
|
+
decoder.decode(signature.signature),
|
|
233
235
|
);
|
|
234
236
|
return equals(
|
|
235
237
|
signerKey,
|
|
236
|
-
(signature.publicKey as Secp256k1PublicKey).publicKey
|
|
238
|
+
(signature.publicKey as Secp256k1PublicKey).publicKey,
|
|
237
239
|
);
|
|
238
240
|
};
|
package/src/signature.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type AbstractType,
|
|
2
3
|
deserialize,
|
|
3
|
-
variant,
|
|
4
4
|
field,
|
|
5
5
|
option,
|
|
6
6
|
serialize,
|
|
7
|
-
|
|
7
|
+
variant,
|
|
8
8
|
} from "@dao-xyz/borsh";
|
|
9
|
-
import { equals } from "
|
|
9
|
+
import { compare, equals } from "uint8arrays";
|
|
10
10
|
import { verifySignatureEd25519 } from "./ed25519-sign.js";
|
|
11
11
|
import { Ed25519PublicKey } from "./ed25519.js";
|
|
12
12
|
import { PublicSignKey } from "./key.js";
|
|
13
13
|
import { PreHash } from "./prehash.js";
|
|
14
14
|
import { Secp256k1PublicKey, verifySignatureSecp256k1 } from "./sepc256k1.js";
|
|
15
|
-
import { SignWithKey } from "./signer.js";
|
|
15
|
+
import { type SignWithKey } from "./signer.js";
|
|
16
16
|
|
|
17
17
|
@variant(0)
|
|
18
18
|
export class SignatureWithKey {
|
|
@@ -30,11 +30,9 @@ export class SignatureWithKey {
|
|
|
30
30
|
publicKey: PublicSignKey;
|
|
31
31
|
prehash: PreHash;
|
|
32
32
|
}) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.prehash = props.prehash;
|
|
37
|
-
}
|
|
33
|
+
this.signature = props.signature;
|
|
34
|
+
this.publicKey = props.publicKey;
|
|
35
|
+
this.prehash = props.prehash;
|
|
38
36
|
}
|
|
39
37
|
|
|
40
38
|
equals(other: SignatureWithKey): boolean {
|
|
@@ -42,8 +40,8 @@ export class SignatureWithKey {
|
|
|
42
40
|
return false;
|
|
43
41
|
}
|
|
44
42
|
return (
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
compare(serialize(this.publicKey), serialize(other.publicKey)) === 0 &&
|
|
44
|
+
this.prehash === other.prehash
|
|
47
45
|
);
|
|
48
46
|
}
|
|
49
47
|
}
|
|
@@ -56,16 +54,14 @@ export class MaybeSigned<T> {
|
|
|
56
54
|
@field({ type: option(SignatureWithKey) })
|
|
57
55
|
signature?: SignatureWithKey;
|
|
58
56
|
|
|
59
|
-
constructor(props
|
|
57
|
+
constructor(props: {
|
|
60
58
|
data: Uint8Array;
|
|
61
59
|
value?: T;
|
|
62
60
|
signature?: SignatureWithKey;
|
|
63
61
|
}) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
this._value = props.value;
|
|
68
|
-
}
|
|
62
|
+
this.data = props.data;
|
|
63
|
+
this.signature = props.signature;
|
|
64
|
+
this._value = props.value;
|
|
69
65
|
}
|
|
70
66
|
|
|
71
67
|
_value?: T;
|
|
@@ -94,10 +90,9 @@ export class MaybeSigned<T> {
|
|
|
94
90
|
return true;
|
|
95
91
|
}
|
|
96
92
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*/
|
|
93
|
+
/*
|
|
94
|
+
In place signing
|
|
95
|
+
*/
|
|
101
96
|
async sign(signer: SignWithKey): Promise<MaybeSigned<T>> {
|
|
102
97
|
const signatureResult = await signer(this.data);
|
|
103
98
|
this.signature = signatureResult;
|
|
@@ -113,172 +108,3 @@ export const verify = async (signature: SignatureWithKey, data: Uint8Array) => {
|
|
|
113
108
|
}
|
|
114
109
|
return false;
|
|
115
110
|
};
|
|
116
|
-
|
|
117
|
-
/*
|
|
118
|
-
|
|
119
|
-
@variant(0)
|
|
120
|
-
export class SignatureWithKey {
|
|
121
|
-
@field({ type: Uint8Array })
|
|
122
|
-
signature: Uint8Array;
|
|
123
|
-
|
|
124
|
-
@field({ type: PublicSignKey })
|
|
125
|
-
publicKey: PublicSignKey;
|
|
126
|
-
|
|
127
|
-
constructor(props?: { signature: Uint8Array; publicKey: PublicSignKey }) {
|
|
128
|
-
if (props) {
|
|
129
|
-
this.signature = props.signature;
|
|
130
|
-
this.publicKey = props.publicKey;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
equals(other: SignatureWithKey): boolean {
|
|
135
|
-
if (!equals(this.signature, other.signature)) {
|
|
136
|
-
return false;
|
|
137
|
-
}
|
|
138
|
-
return (
|
|
139
|
-
Buffer.compare(
|
|
140
|
-
serialize(this.publicKey),
|
|
141
|
-
serialize(other.publicKey)
|
|
142
|
-
) === 0
|
|
143
|
-
);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
@variant(0)
|
|
148
|
-
export abstract class MaybeSigned<T> {
|
|
149
|
-
|
|
150
|
-
@field({ type: Uint8Array })
|
|
151
|
-
data: Uint8Array;
|
|
152
|
-
|
|
153
|
-
constructor(props: {
|
|
154
|
-
data: Uint8Array;
|
|
155
|
-
value?: T;
|
|
156
|
-
}) {
|
|
157
|
-
this.data = props.data;
|
|
158
|
-
this._value = props.value;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
_value?: T;
|
|
162
|
-
|
|
163
|
-
getValue(constructor: AbstractType<T>): T {
|
|
164
|
-
return deserialize(this.data, constructor);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
abstract verify(): Promise<boolean>
|
|
168
|
-
|
|
169
|
-
equals(other: MaybeSigned<T>): boolean {
|
|
170
|
-
if (!equals(this.data, other.data)) {
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
173
|
-
return true;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
@variant(0)
|
|
179
|
-
export class Signed<T> extends MaybeSigned<T> {
|
|
180
|
-
|
|
181
|
-
@field({ type: option(SignatureWithKey) })
|
|
182
|
-
signature: SignatureWithKey;
|
|
183
|
-
|
|
184
|
-
constructor(props: {
|
|
185
|
-
data: Uint8Array;
|
|
186
|
-
value?: T;
|
|
187
|
-
signature: SignatureWithKey;
|
|
188
|
-
}) {
|
|
189
|
-
super(props)
|
|
190
|
-
this.signature = props.signature;
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async verify(): Promise<boolean> {
|
|
195
|
-
return verify(
|
|
196
|
-
this.signature.signature,
|
|
197
|
-
this.signature.publicKey,
|
|
198
|
-
this.data
|
|
199
|
-
);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
equals(other: MaybeSigned<T>): boolean {
|
|
203
|
-
if (other instanceof Signed<T>) {
|
|
204
|
-
if (!this.signature !== !other.signature) {
|
|
205
|
-
return false;
|
|
206
|
-
}
|
|
207
|
-
if (this.signature && other.signature) {
|
|
208
|
-
return this.signature.equals(other.signature);
|
|
209
|
-
}
|
|
210
|
-
return super.equals(other)
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
return false;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
async sign(signer: SignWithKey): Promise<MaybeSigned<T>> {
|
|
219
|
-
const signatureResult = await signer(this.data);
|
|
220
|
-
this.signature = new SignatureWithKey({
|
|
221
|
-
publicKey: signatureResult.publicKey,
|
|
222
|
-
signature: signatureResult.signature,
|
|
223
|
-
});
|
|
224
|
-
return this;
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
@variant(0)
|
|
230
|
-
export class Unsigned<T> extends MaybeSigned<T> {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
constructor(props: {
|
|
234
|
-
data: Uint8Array;
|
|
235
|
-
value?: T;
|
|
236
|
-
}) {
|
|
237
|
-
super(props)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
equals(other: MaybeSigned<T>): boolean {
|
|
241
|
-
if (other instanceof Unsigned) {
|
|
242
|
-
return super.equals(other)
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
async sign(signer: SignWithKey): Promise<Signed<T>> {
|
|
251
|
-
const signatureResult = await signer(this.data);
|
|
252
|
-
return new Signed({
|
|
253
|
-
data: this.data,
|
|
254
|
-
signature: new SignatureWithKey({
|
|
255
|
-
publicKey: signatureResult.publicKey,
|
|
256
|
-
signature: signatureResult.signature,
|
|
257
|
-
}),
|
|
258
|
-
value: this._value
|
|
259
|
-
});
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
async verify(): Promise<boolean> {
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
export const verify = (
|
|
269
|
-
signature: Uint8Array,
|
|
270
|
-
publicKey: PublicSignKey,
|
|
271
|
-
data: Uint8Array
|
|
272
|
-
) => {
|
|
273
|
-
if (!signature) {
|
|
274
|
-
return true;
|
|
275
|
-
}
|
|
276
|
-
if (publicKey instanceof Ed25519PublicKey) {
|
|
277
|
-
return verifySignatureEd25519(signature, publicKey, data);
|
|
278
|
-
} else if (publicKey instanceof Secp256k1PublicKey) {
|
|
279
|
-
return verifySignatureSecp256k1(signature, publicKey, data);
|
|
280
|
-
}
|
|
281
|
-
return false;
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
*/
|
package/src/signer.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { PublicSignKey } from "./key.js";
|
|
2
|
-
import { PreHash } from "./prehash.js";
|
|
3
|
-
import { SignatureWithKey } from "./signature.js";
|
|
1
|
+
import type { PublicSignKey } from "./key.js";
|
|
2
|
+
import type { PreHash } from "./prehash.js";
|
|
3
|
+
import type { SignatureWithKey } from "./signature.js";
|
|
4
4
|
|
|
5
5
|
export interface Signer {
|
|
6
6
|
sign: (
|
|
7
7
|
bytes: Uint8Array,
|
|
8
|
-
prehash?: PreHash
|
|
8
|
+
prehash?: PreHash,
|
|
9
9
|
) => Promise<SignatureWithKey> | SignatureWithKey;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export type SignWithKey = (
|
|
13
|
-
bytes: Uint8Array
|
|
13
|
+
bytes: Uint8Array,
|
|
14
14
|
) => Promise<SignatureWithKey> | SignatureWithKey;
|
|
15
15
|
|
|
16
16
|
export type Identity<T extends PublicSignKey = PublicSignKey> = Signer & {
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import sodium from "libsodium-wrappers";
|
|
2
|
+
import { base58btc } from "multiformats/bases/base58";
|
|
3
|
+
|
|
2
4
|
export const fromHexString = (hexString: string) => sodium.from_hex(hexString);
|
|
3
5
|
|
|
4
6
|
export const toHexString = (bytes: Uint8Array) => sodium.to_hex(bytes);
|
|
@@ -16,3 +18,7 @@ export const toBase64URL = (arr: Uint8Array) => {
|
|
|
16
18
|
export const fromBase64URL = (base64: string) => {
|
|
17
19
|
return sodium.from_base64(base64, sodium.base64_variants.URLSAFE);
|
|
18
20
|
};
|
|
21
|
+
|
|
22
|
+
export const toBase58 = (arr: Uint8Array) => {
|
|
23
|
+
return base58btc.baseEncode(arr);
|
|
24
|
+
};
|
package/src/x25519.ts
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
export * from "./errors.js";
|
|
2
1
|
import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
3
|
-
import {
|
|
2
|
+
import { type PeerId } from "@libp2p/interface";
|
|
4
3
|
import sodium from "libsodium-wrappers";
|
|
4
|
+
import { compare } from "uint8arrays";
|
|
5
|
+
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js";
|
|
5
6
|
import {
|
|
6
7
|
Keypair,
|
|
7
8
|
PrivateEncryptionKey,
|
|
8
|
-
PublicKeyEncryptionKey
|
|
9
|
+
PublicKeyEncryptionKey,
|
|
9
10
|
} from "./key.js";
|
|
10
|
-
import {
|
|
11
|
-
Ed25519Keypair,
|
|
12
|
-
Ed25519PublicKey,
|
|
13
|
-
Ed25519PrivateKey
|
|
14
|
-
} from "./ed25519.js";
|
|
15
11
|
import { toHexString } from "./utils.js";
|
|
16
|
-
|
|
12
|
+
|
|
13
|
+
export * from "./errors.js";
|
|
14
|
+
|
|
17
15
|
@variant(0)
|
|
18
16
|
export class X25519PublicKey extends PublicKeyEncryptionKey {
|
|
19
17
|
@field({ type: fixedArray("u8", 32) })
|
|
@@ -33,31 +31,32 @@ export class X25519PublicKey extends PublicKeyEncryptionKey {
|
|
|
33
31
|
}
|
|
34
32
|
return false;
|
|
35
33
|
}
|
|
34
|
+
|
|
36
35
|
toString(): string {
|
|
37
36
|
return "x25519p/" + toHexString(this.publicKey);
|
|
38
37
|
}
|
|
39
38
|
|
|
40
39
|
static async from(
|
|
41
|
-
ed25119PublicKey: Ed25519PublicKey
|
|
40
|
+
ed25119PublicKey: Ed25519PublicKey,
|
|
42
41
|
): Promise<X25519PublicKey> {
|
|
43
42
|
await sodium.ready;
|
|
44
43
|
return new X25519PublicKey({
|
|
45
44
|
publicKey: sodium.crypto_sign_ed25519_pk_to_curve25519(
|
|
46
|
-
ed25119PublicKey.publicKey
|
|
47
|
-
)
|
|
45
|
+
ed25119PublicKey.publicKey,
|
|
46
|
+
),
|
|
48
47
|
});
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
static async fromPeerId(peerId: PeerId): Promise<X25519PublicKey> {
|
|
52
51
|
await sodium.ready;
|
|
53
|
-
const ed =
|
|
52
|
+
const ed = Ed25519PublicKey.fromPeerId(peerId);
|
|
54
53
|
return X25519PublicKey.from(ed);
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
static async create(): Promise<X25519PublicKey> {
|
|
58
57
|
await sodium.ready;
|
|
59
58
|
return new X25519PublicKey({
|
|
60
|
-
publicKey: sodium.crypto_box_keypair().publicKey
|
|
59
|
+
publicKey: sodium.crypto_box_keypair().publicKey,
|
|
61
60
|
});
|
|
62
61
|
}
|
|
63
62
|
}
|
|
@@ -77,34 +76,34 @@ export class X25519SecretKey extends PrivateEncryptionKey {
|
|
|
77
76
|
|
|
78
77
|
equals(other: PublicKeyEncryptionKey): boolean {
|
|
79
78
|
if (other instanceof X25519SecretKey) {
|
|
80
|
-
return (
|
|
81
|
-
compare(this.secretKey, (other as X25519SecretKey).secretKey) === 0
|
|
82
|
-
);
|
|
79
|
+
return compare(this.secretKey, other.secretKey) === 0;
|
|
83
80
|
}
|
|
84
81
|
return false;
|
|
85
82
|
}
|
|
83
|
+
|
|
86
84
|
toString(): string {
|
|
87
85
|
return "x25519s" + toHexString(this.secretKey);
|
|
88
86
|
}
|
|
89
87
|
|
|
90
88
|
async publicKey(): Promise<X25519PublicKey> {
|
|
91
89
|
return new X25519PublicKey({
|
|
92
|
-
publicKey: sodium.crypto_scalarmult_base(this.secretKey)
|
|
90
|
+
publicKey: sodium.crypto_scalarmult_base(this.secretKey),
|
|
93
91
|
});
|
|
94
92
|
}
|
|
93
|
+
|
|
95
94
|
static async from(ed25119Keypair: Ed25519Keypair): Promise<X25519SecretKey> {
|
|
96
95
|
await sodium.ready;
|
|
97
96
|
return new X25519SecretKey({
|
|
98
97
|
secretKey: sodium.crypto_sign_ed25519_sk_to_curve25519(
|
|
99
|
-
ed25119Keypair.privateKeyPublicKey
|
|
100
|
-
)
|
|
98
|
+
ed25119Keypair.privateKeyPublicKey,
|
|
99
|
+
),
|
|
101
100
|
});
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
static async create(): Promise<X25519SecretKey> {
|
|
105
104
|
await sodium.ready;
|
|
106
105
|
return new X25519SecretKey({
|
|
107
|
-
secretKey: sodium.crypto_box_keypair().privateKey
|
|
106
|
+
secretKey: sodium.crypto_box_keypair().privateKey,
|
|
108
107
|
});
|
|
109
108
|
}
|
|
110
109
|
}
|
|
@@ -131,11 +130,11 @@ export class X25519Keypair extends Keypair {
|
|
|
131
130
|
const generated = sodium.crypto_box_keypair();
|
|
132
131
|
const kp = new X25519Keypair({
|
|
133
132
|
publicKey: new X25519PublicKey({
|
|
134
|
-
publicKey: generated.publicKey
|
|
133
|
+
publicKey: generated.publicKey,
|
|
135
134
|
}),
|
|
136
135
|
secretKey: new X25519SecretKey({
|
|
137
|
-
secretKey: generated.privateKey
|
|
138
|
-
})
|
|
136
|
+
secretKey: generated.privateKey,
|
|
137
|
+
}),
|
|
139
138
|
});
|
|
140
139
|
|
|
141
140
|
return kp;
|
|
@@ -144,7 +143,7 @@ export class X25519Keypair extends Keypair {
|
|
|
144
143
|
static async from(ed25119Keypair: Ed25519Keypair): Promise<X25519Keypair> {
|
|
145
144
|
const kp = new X25519Keypair({
|
|
146
145
|
publicKey: await X25519PublicKey.from(ed25119Keypair.publicKey),
|
|
147
|
-
secretKey: await X25519SecretKey.from(ed25119Keypair)
|
|
146
|
+
secretKey: await X25519SecretKey.from(ed25119Keypair),
|
|
148
147
|
});
|
|
149
148
|
return kp;
|
|
150
149
|
}
|
|
@@ -153,13 +152,12 @@ export class X25519Keypair extends Keypair {
|
|
|
153
152
|
const ed = Ed25519Keypair.fromPeerId(peerId);
|
|
154
153
|
return X25519Keypair.from(ed);
|
|
155
154
|
}
|
|
155
|
+
|
|
156
156
|
equals(other: Keypair) {
|
|
157
157
|
if (other instanceof X25519Keypair) {
|
|
158
158
|
return (
|
|
159
159
|
this.publicKey.equals(other.publicKey) &&
|
|
160
|
-
this.secretKey.equals(
|
|
161
|
-
(other as X25519Keypair).secretKey as X25519SecretKey as any
|
|
162
|
-
)
|
|
160
|
+
this.secretKey.equals(other.secretKey)
|
|
163
161
|
);
|
|
164
162
|
}
|
|
165
163
|
return false;
|