@peerbit/crypto 3.1.2 → 3.1.3
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/src/sepc256k1.d.ts +0 -2
- package/dist/src/sepc256k1.d.ts.map +1 -1
- package/dist/src/sepc256k1.js +30 -21
- package/dist/src/sepc256k1.js.map +1 -1
- package/package.json +5 -6
- package/src/sepc256k1.ts +36 -28
package/dist/src/sepc256k1.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type SignatureLike } from "@ethersproject/bytes";
|
|
2
|
-
import { Wallet } from "@ethersproject/wallet";
|
|
3
2
|
import { type PeerId } from "@libp2p/interface";
|
|
4
3
|
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js";
|
|
5
4
|
import { PreHash } from "./prehash.js";
|
|
@@ -29,7 +28,6 @@ export declare class Secp256k1PrivateKey extends PrivateSignKey {
|
|
|
29
28
|
export declare class Secp256k1Keypair extends Keypair implements Identity {
|
|
30
29
|
publicKey: Secp256k1PublicKey;
|
|
31
30
|
privateKey: Secp256k1PrivateKey;
|
|
32
|
-
_wallet: Wallet;
|
|
33
31
|
constructor(properties: {
|
|
34
32
|
publicKey: Secp256k1PublicKey;
|
|
35
33
|
privateKey: Secp256k1PrivateKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sepc256k1.d.ts","sourceRoot":"","sources":["../../src/sepc256k1.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,aAAa,
|
|
1
|
+
{"version":3,"file":"sepc256k1.d.ts","sourceRoot":"","sources":["../../src/sepc256k1.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,aAAa,EAKlB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAKhD,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,EA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,aAAa,KACtB,UAUF,CAAC;AAEF,eAAO,MAAM,wBAAwB,GACpC,WAAW,gBAAgB,EAC3B,MAAM,UAAU,KACd,OAAO,CAAC,OAAO,CAGjB,CAAC;AAEF,eAAO,MAAM,gCAAgC,GAC5C,WAAW,gBAAgB,EAC3B,cAAc,UAAU,KACtB,OAAO,CAAC,OAAO,CAWjB,CAAC"}
|
package/dist/src/sepc256k1.js
CHANGED
|
@@ -33,21 +33,25 @@ var __runInitializers = (this && this.__runInitializers) || function (thisArg, i
|
|
|
33
33
|
return useValue ? value : void 0;
|
|
34
34
|
};
|
|
35
35
|
import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
36
|
-
import { arrayify, joinSignature, splitSignature, } from "@ethersproject/bytes";
|
|
37
|
-
import { Wallet } from "@ethersproject/wallet";
|
|
36
|
+
import { arrayify, hexlify, joinSignature, splitSignature, } from "@ethersproject/bytes";
|
|
38
37
|
import { keys } from "@libp2p/crypto";
|
|
39
38
|
import {} from "@libp2p/interface";
|
|
40
39
|
import { peerIdFromPublicKey } from "@libp2p/peer-id";
|
|
40
|
+
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
41
41
|
import utf8 from "@protobufjs/utf8";
|
|
42
|
-
import _ec from "elliptic";
|
|
43
42
|
import { equals } from "uint8arrays";
|
|
44
43
|
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js";
|
|
45
44
|
import { PreHash, prehashFn } from "./prehash.js";
|
|
46
45
|
import { SignatureWithKey } from "./signature.js";
|
|
47
46
|
import {} from "./signer.js";
|
|
48
47
|
import { toHexString } from "./utils.js";
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const SECP256K1_DIGEST_LENGTH = 32;
|
|
49
|
+
const requireSecp256k1Digest = (digest) => {
|
|
50
|
+
if (digest.length !== SECP256K1_DIGEST_LENGTH) {
|
|
51
|
+
throw new Error(`Secp256k1 requires an exactly ${SECP256K1_DIGEST_LENGTH}-byte prepared digest`);
|
|
52
|
+
}
|
|
53
|
+
return digest;
|
|
54
|
+
};
|
|
51
55
|
let Secp256k1PublicKey = (() => {
|
|
52
56
|
let _classDecorators = [variant(1)];
|
|
53
57
|
let _classDescriptor;
|
|
@@ -181,9 +185,9 @@ let Secp256k1Keypair = (() => {
|
|
|
181
185
|
}
|
|
182
186
|
publicKey = __runInitializers(this, _publicKey_initializers, void 0);
|
|
183
187
|
privateKey = (__runInitializers(this, _publicKey_extraInitializers), __runInitializers(this, _privateKey_initializers, void 0));
|
|
184
|
-
_wallet = __runInitializers(this, _privateKey_extraInitializers);
|
|
185
188
|
constructor(properties) {
|
|
186
189
|
super();
|
|
190
|
+
__runInitializers(this, _privateKey_extraInitializers);
|
|
187
191
|
this.privateKey = properties.privateKey;
|
|
188
192
|
this.publicKey = properties.publicKey;
|
|
189
193
|
}
|
|
@@ -200,10 +204,14 @@ let Secp256k1Keypair = (() => {
|
|
|
200
204
|
return kp;
|
|
201
205
|
}
|
|
202
206
|
async sign(data, prehash = PreHash.ETH_KECCAK_256) {
|
|
203
|
-
const maybeHashed = await prehashFn(data, prehash);
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
|
|
207
|
+
const maybeHashed = requireSecp256k1Digest(await prehashFn(data, prehash));
|
|
208
|
+
const recoveredSignature = secp256k1.sign(maybeHashed, this.privateKey.privateKey, { lowS: true, prehash: false });
|
|
209
|
+
const compactSignature = recoveredSignature.toBytes("compact");
|
|
210
|
+
const signature = joinSignature({
|
|
211
|
+
r: hexlify(compactSignature.subarray(0, 32)),
|
|
212
|
+
s: hexlify(compactSignature.subarray(32)),
|
|
213
|
+
recoveryParam: recoveredSignature.recovery,
|
|
214
|
+
});
|
|
207
215
|
const signatureBytes = new Uint8Array(utf8.length(signature)); // TODO utilize Buffer allocUnsafe
|
|
208
216
|
utf8.write(signature, signatureBytes, 0);
|
|
209
217
|
return new SignatureWithKey({
|
|
@@ -227,24 +235,25 @@ let Secp256k1Keypair = (() => {
|
|
|
227
235
|
})();
|
|
228
236
|
export { Secp256k1Keypair };
|
|
229
237
|
const decoder = new TextDecoder();
|
|
230
|
-
function getCurve() {
|
|
231
|
-
if (!_curve) {
|
|
232
|
-
_curve = new EC("secp256k1");
|
|
233
|
-
}
|
|
234
|
-
return _curve;
|
|
235
|
-
}
|
|
236
238
|
export const recoverPublicKeyFromSignature = (digest, signature) => {
|
|
237
|
-
|
|
238
|
-
const
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
239
|
+
requireSecp256k1Digest(digest);
|
|
240
|
+
const { r, recoveryParam, s } = splitSignature(signature);
|
|
241
|
+
const recoveredSignature = new Uint8Array(65);
|
|
242
|
+
recoveredSignature[0] = recoveryParam;
|
|
243
|
+
recoveredSignature.set(arrayify(r), 1);
|
|
244
|
+
recoveredSignature.set(arrayify(s), 33);
|
|
245
|
+
return secp256k1.Signature.fromBytes(recoveredSignature, "recovered")
|
|
246
|
+
.recoverPublicKey(digest)
|
|
247
|
+
.toBytes(true);
|
|
242
248
|
};
|
|
243
249
|
export const verifySignatureSecp256k1 = async (signature, data) => {
|
|
244
250
|
const hashedData = await prehashFn(data, signature.prehash);
|
|
245
251
|
return verifySignatureSecp256k1Prepared(signature, hashedData);
|
|
246
252
|
};
|
|
247
253
|
export const verifySignatureSecp256k1Prepared = (signature, preparedData) => {
|
|
254
|
+
if (preparedData.length !== SECP256K1_DIGEST_LENGTH) {
|
|
255
|
+
return Promise.resolve(false);
|
|
256
|
+
}
|
|
248
257
|
const signerKey = recoverPublicKeyFromSignature(arrayify(preparedData), decoder.decode(signature.signature));
|
|
249
258
|
return Promise.resolve(equals(signerKey, signature.publicKey.publicKey));
|
|
250
259
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sepc256k1.js","sourceRoot":"","sources":["../../src/sepc256k1.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;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CAAC,SAAS,CAAC,CACxE,CAAC;AACH,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/crypto",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.3",
|
|
4
4
|
"description": "Crypto fn",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"type": "module",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"./dist/src/random.js": "./dist/src/random.browser.js",
|
|
54
54
|
"./dist/src/ed25519-sign.js": "./dist/src/ed25519-sign.browser.js"
|
|
55
55
|
},
|
|
56
|
-
"author": "
|
|
56
|
+
"author": "Peerbit contributors",
|
|
57
57
|
"license": "MIT",
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@types/elliptic": "^6.4.18",
|
|
60
59
|
"@types/libsodium-wrappers": "^0.7.14",
|
|
60
|
+
"ethers": "^6.17.0",
|
|
61
61
|
"multiformats": "^13.4.2",
|
|
62
62
|
"uint8arrays": "^5.1.0"
|
|
63
63
|
},
|
|
@@ -65,13 +65,12 @@
|
|
|
65
65
|
"@dao-xyz/borsh": "^6.0.0",
|
|
66
66
|
"@ethersproject/bytes": "^5.7.0",
|
|
67
67
|
"@ethersproject/strings": "^5.7.0",
|
|
68
|
-
"@ethersproject/wallet": "^5.8.0",
|
|
69
68
|
"@libp2p/crypto": "^5.1.14",
|
|
70
69
|
"@libp2p/interface": "^3.1.1",
|
|
71
70
|
"@libp2p/peer-id": "^6.0.5",
|
|
72
|
-
"@
|
|
71
|
+
"@noble/curves": "1.9.7",
|
|
72
|
+
"@protobufjs/utf8": "^1.1.2",
|
|
73
73
|
"@stablelib/sha256": "^2.0.1",
|
|
74
|
-
"elliptic": "^6.6.1",
|
|
75
74
|
"js-sha3": "^0.9.3",
|
|
76
75
|
"libsodium-wrappers": "0.7.15",
|
|
77
76
|
"@peerbit/cache": "3.1.0"
|
package/src/sepc256k1.ts
CHANGED
|
@@ -2,15 +2,15 @@ import { field, fixedArray, variant } from "@dao-xyz/borsh";
|
|
|
2
2
|
import {
|
|
3
3
|
type SignatureLike,
|
|
4
4
|
arrayify,
|
|
5
|
+
hexlify,
|
|
5
6
|
joinSignature,
|
|
6
7
|
splitSignature,
|
|
7
8
|
} from "@ethersproject/bytes";
|
|
8
|
-
import { Wallet } from "@ethersproject/wallet";
|
|
9
9
|
import { keys } from "@libp2p/crypto";
|
|
10
10
|
import { type PeerId } from "@libp2p/interface";
|
|
11
11
|
import { peerIdFromPublicKey } from "@libp2p/peer-id";
|
|
12
|
+
import { secp256k1 } from "@noble/curves/secp256k1";
|
|
12
13
|
import utf8 from "@protobufjs/utf8";
|
|
13
|
-
import _ec from "elliptic";
|
|
14
14
|
import { equals } from "uint8arrays";
|
|
15
15
|
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js";
|
|
16
16
|
import { PreHash, prehashFn } from "./prehash.js";
|
|
@@ -18,8 +18,16 @@ import { SignatureWithKey } from "./signature.js";
|
|
|
18
18
|
import { type Identity } from "./signer.js";
|
|
19
19
|
import { toHexString } from "./utils.js";
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
const SECP256K1_DIGEST_LENGTH = 32;
|
|
22
|
+
|
|
23
|
+
const requireSecp256k1Digest = (digest: Uint8Array): Uint8Array => {
|
|
24
|
+
if (digest.length !== SECP256K1_DIGEST_LENGTH) {
|
|
25
|
+
throw new Error(
|
|
26
|
+
`Secp256k1 requires an exactly ${SECP256K1_DIGEST_LENGTH}-byte prepared digest`,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
return digest;
|
|
30
|
+
};
|
|
23
31
|
|
|
24
32
|
@variant(1)
|
|
25
33
|
export class Secp256k1PublicKey extends PublicSignKey {
|
|
@@ -112,7 +120,6 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
112
120
|
@field({ type: Secp256k1PrivateKey })
|
|
113
121
|
privateKey: Secp256k1PrivateKey;
|
|
114
122
|
|
|
115
|
-
_wallet!: Wallet;
|
|
116
123
|
constructor(properties: {
|
|
117
124
|
publicKey: Secp256k1PublicKey;
|
|
118
125
|
privateKey: Secp256k1PrivateKey;
|
|
@@ -140,13 +147,19 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
140
147
|
data: Uint8Array,
|
|
141
148
|
prehash: PreHash = PreHash.ETH_KECCAK_256,
|
|
142
149
|
): Promise<SignatureWithKey> {
|
|
143
|
-
const maybeHashed = await prehashFn(data, prehash);
|
|
150
|
+
const maybeHashed = requireSecp256k1Digest(await prehashFn(data, prehash));
|
|
144
151
|
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
152
|
+
const recoveredSignature = secp256k1.sign(
|
|
153
|
+
maybeHashed,
|
|
154
|
+
this.privateKey.privateKey,
|
|
155
|
+
{ lowS: true, prehash: false },
|
|
149
156
|
);
|
|
157
|
+
const compactSignature = recoveredSignature.toBytes("compact");
|
|
158
|
+
const signature = joinSignature({
|
|
159
|
+
r: hexlify(compactSignature.subarray(0, 32)),
|
|
160
|
+
s: hexlify(compactSignature.subarray(32)),
|
|
161
|
+
recoveryParam: recoveredSignature.recovery,
|
|
162
|
+
});
|
|
150
163
|
const signatureBytes = new Uint8Array(utf8.length(signature)); // TODO utilize Buffer allocUnsafe
|
|
151
164
|
utf8.write(signature, signatureBytes, 0);
|
|
152
165
|
|
|
@@ -174,24 +187,19 @@ export class Secp256k1Keypair extends Keypair implements Identity {
|
|
|
174
187
|
|
|
175
188
|
const decoder = new TextDecoder();
|
|
176
189
|
|
|
177
|
-
function getCurve() {
|
|
178
|
-
if (!_curve) {
|
|
179
|
-
_curve = new EC("secp256k1");
|
|
180
|
-
}
|
|
181
|
-
return _curve;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
190
|
export const recoverPublicKeyFromSignature = (
|
|
185
191
|
digest: Uint8Array,
|
|
186
192
|
signature: SignatureLike,
|
|
187
193
|
): Uint8Array => {
|
|
188
|
-
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
)
|
|
194
|
+
requireSecp256k1Digest(digest);
|
|
195
|
+
const { r, recoveryParam, s } = splitSignature(signature);
|
|
196
|
+
const recoveredSignature = new Uint8Array(65);
|
|
197
|
+
recoveredSignature[0] = recoveryParam;
|
|
198
|
+
recoveredSignature.set(arrayify(r), 1);
|
|
199
|
+
recoveredSignature.set(arrayify(s), 33);
|
|
200
|
+
return secp256k1.Signature.fromBytes(recoveredSignature, "recovered")
|
|
201
|
+
.recoverPublicKey(digest)
|
|
202
|
+
.toBytes(true);
|
|
195
203
|
};
|
|
196
204
|
|
|
197
205
|
export const verifySignatureSecp256k1 = async (
|
|
@@ -206,14 +214,14 @@ export const verifySignatureSecp256k1Prepared = (
|
|
|
206
214
|
signature: SignatureWithKey,
|
|
207
215
|
preparedData: Uint8Array,
|
|
208
216
|
): Promise<boolean> => {
|
|
217
|
+
if (preparedData.length !== SECP256K1_DIGEST_LENGTH) {
|
|
218
|
+
return Promise.resolve(false);
|
|
219
|
+
}
|
|
209
220
|
const signerKey = recoverPublicKeyFromSignature(
|
|
210
221
|
arrayify(preparedData),
|
|
211
222
|
decoder.decode(signature.signature),
|
|
212
223
|
);
|
|
213
224
|
return Promise.resolve(
|
|
214
|
-
equals(
|
|
215
|
-
signerKey,
|
|
216
|
-
(signature.publicKey as Secp256k1PublicKey).publicKey,
|
|
217
|
-
),
|
|
225
|
+
equals(signerKey, (signature.publicKey as Secp256k1PublicKey).publicKey),
|
|
218
226
|
);
|
|
219
227
|
};
|