Skip to content

Commit e6af632

Browse files
feat: replace bcrypto (#302)
Co-authored-by: Cayman <[email protected]>
1 parent bfcc6d2 commit e6af632

File tree

19 files changed

+137
-137
lines changed

19 files changed

+137
-137
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@
4242
"typescript": "^4.7.3"
4343
},
4444
"packageManager": "[email protected]+sha256.c17d3797fb9a9115bf375e31bfd30058cac6bc9c3b8807a3d8cb2094794b51ca"
45-
}
45+
}

packages/discv5/README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,6 @@ const libp2p = createLibp2p({
3636
});
3737
```
3838

39-
## Additional features
40-
41-
By default, importing this library will, as a side-effect, change the enr crypto implementation to use `bcrypto`.
42-
If you'd like to remain using `@chainsafe/enr`'s default crypto you can add this after importing `@chainsafe/discv5`:
43-
```ts
44-
import {setV4Crypto, defaultCrypto} from "@chainsafe/enr";
45-
46-
setV4Crypto(defaultCrypto)
47-
```
48-
4939
## License
5040

5141
Apache-2.0

packages/discv5/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
"@libp2p/crypto": "^5.0.1",
7171
"@libp2p/interface": "^2.0.1",
7272
"@multiformats/multiaddr": "^12.1.10",
73-
"bcrypto": "^5.4.0",
73+
"@noble/hashes": "^1.7.0",
74+
"@noble/secp256k1": "^2.2.2",
7475
"bigint-buffer": "^1.1.5",
7576
"debug": "^4.3.1",
7677
"lru-cache": "^10.1.0",

packages/discv5/src/enr/bcryptoV4Crypto.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/discv5/src/enr/setV4Crypto.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/discv5/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,3 @@ export * from "./service/index.js";
55
export * from "./session/index.js";
66
export * from "./transport/index.js";
77
export * from "./util/index.js";
8-
9-
// side effect: set the enr crypto implementation
10-
import "./enr/setV4Crypto.js";
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import secp256k1 from "bcrypto/lib/secp256k1.js";
21
import { KeyType } from "@libp2p/interface";
32
import { AbstractKeypair, IKeypair, IKeypairClass } from "./types.js";
43
import { ERR_INVALID_KEYPAIR_TYPE } from "./constants.js";
4+
import { getDiscv5Crypto } from "../util/crypto.js";
5+
import { toBuffer } from "../util/index.js";
56

67
export function secp256k1PublicKeyToCompressed(publicKey: Buffer): Buffer {
78
if (publicKey.length === 64) {
89
publicKey = Buffer.concat([Buffer.from([4]), publicKey]);
910
}
10-
return secp256k1.publicKeyConvert(publicKey, true);
11-
}
12-
13-
export function secp256k1PublicKeyToFull(publicKey: Buffer): Buffer {
14-
if (publicKey.length === 64) {
15-
return Buffer.concat([Buffer.from([4]), publicKey]);
16-
}
17-
return secp256k1.publicKeyConvert(publicKey, false);
11+
return toBuffer(getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, true));
1812
}
1913

2014
export function secp256k1PublicKeyToRaw(publicKey: Buffer): Buffer {
21-
return secp256k1.publicKeyConvert(publicKey, false).slice(1);
15+
return toBuffer(getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, false));
2216
}
2317

2418
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends AbstractKeypair implements IKeypair {
2519
readonly type: KeyType;
2620

2721
constructor(privateKey?: Buffer, publicKey?: Buffer) {
28-
let pub = publicKey ?? secp256k1.publicKeyCreate(privateKey!);
22+
let pub = publicKey ?? toBuffer(getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey!));
2923
if (pub) {
3024
pub = secp256k1PublicKeyToCompressed(pub);
3125
}
@@ -34,33 +28,33 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
3428
}
3529

3630
static generate(): Secp256k1Keypair {
37-
const privateKey = secp256k1.privateKeyGenerate();
38-
const publicKey = secp256k1.publicKeyCreate(privateKey);
31+
const privateKey = toBuffer(getDiscv5Crypto().secp256k1.generatePrivateKey());
32+
const publicKey = toBuffer(getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey));
3933
return new Secp256k1Keypair(privateKey, publicKey);
4034
}
4135

4236
privateKeyVerify(key = this._privateKey): boolean {
4337
if (key) {
44-
return secp256k1.privateKeyVerify(key);
38+
return getDiscv5Crypto().secp256k1.privateKeyVerify(key);
4539
}
4640
return true;
4741
}
4842
publicKeyVerify(key = this._publicKey): boolean {
4943
if (key) {
50-
return secp256k1.publicKeyVerify(key);
44+
return getDiscv5Crypto().secp256k1.publicKeyVerify(key);
5145
}
5246
return true;
5347
}
5448
sign(msg: Buffer): Buffer {
55-
return secp256k1.sign(msg, this.privateKey);
49+
return toBuffer(getDiscv5Crypto().secp256k1.sign(msg, this.privateKey));
5650
}
5751
verify(msg: Buffer, sig: Buffer): boolean {
58-
return secp256k1.verify(msg, sig, this.publicKey);
52+
return getDiscv5Crypto().secp256k1.verify(this.publicKey, msg, sig);
5953
}
6054
deriveSecret(keypair: IKeypair): Buffer {
6155
if (keypair.type !== this.type) {
6256
throw new Error(ERR_INVALID_KEYPAIR_TYPE);
6357
}
64-
return secp256k1.derive(keypair.publicKey, this.privateKey);
58+
return toBuffer(getDiscv5Crypto().secp256k1.deriveSecret(this.privateKey, keypair.publicKey));
6559
}
6660
};

packages/discv5/src/message/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { randomBytes } from "bcrypto/lib/random.js";
1+
import { randomBytes } from "@noble/hashes/utils";
22
import { toBigIntBE } from "bigint-buffer";
33
import { SequenceNumber, ENR } from "@chainsafe/enr";
44

@@ -11,9 +11,10 @@ import {
1111
ITalkReqMessage,
1212
ITalkRespMessage,
1313
} from "./types.js";
14+
import { toBuffer } from "../index.js";
1415

1516
export function createRequestId(): RequestId {
16-
return toBigIntBE(randomBytes(8));
17+
return toBigIntBE(toBuffer(randomBytes(8)));
1718
}
1819

1920
export function createPingMessage(enrSeq: SequenceNumber): IPingMessage {
@@ -53,6 +54,6 @@ export function createTalkResponseMessage(requestId: RequestId, payload: Uint8Ar
5354
return {
5455
type: MessageType.TALKRESP,
5556
id: requestId,
56-
response: Buffer.from(payload),
57+
response: toBuffer(payload),
5758
};
5859
}

packages/discv5/src/packet/create.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { randomBytes } from "bcrypto/lib/random.js";
1+
import { randomBytes } from "@noble/hashes/utils";
22
import { NodeId, SequenceNumber } from "@chainsafe/enr";
33
import { ID_NONCE_SIZE, MASKING_IV_SIZE, NONCE_SIZE } from "./constants.js";
44
import { encodeMessageAuthdata, encodeWhoAreYouAuthdata } from "./encode.js";
55
import { IHeader, IPacket, PacketType } from "./types.js";
6+
import { toBuffer } from "../index.js";
67

78
export function createHeader(flag: PacketType, authdata: Buffer, nonce = randomBytes(NONCE_SIZE)): IHeader {
89
return {
910
protocolId: "discv5",
1011
version: 1,
1112
flag,
12-
nonce,
13+
nonce: toBuffer(nonce),
1314
authdataSize: authdata.length,
1415
authdata,
1516
};
@@ -18,8 +19,8 @@ export function createHeader(flag: PacketType, authdata: Buffer, nonce = randomB
1819
export function createRandomPacket(srcId: NodeId): IPacket {
1920
const authdata = encodeMessageAuthdata({ srcId });
2021
const header = createHeader(PacketType.Message, authdata);
21-
const maskingIv = randomBytes(MASKING_IV_SIZE);
22-
const message = randomBytes(44);
22+
const maskingIv = toBuffer(randomBytes(MASKING_IV_SIZE));
23+
const message = toBuffer(randomBytes(44));
2324
return {
2425
maskingIv,
2526
header,
@@ -28,10 +29,10 @@ export function createRandomPacket(srcId: NodeId): IPacket {
2829
}
2930

3031
export function createWhoAreYouPacket(nonce: Buffer, enrSeq: SequenceNumber): IPacket {
31-
const idNonce = randomBytes(ID_NONCE_SIZE);
32+
const idNonce = toBuffer(randomBytes(ID_NONCE_SIZE));
3233
const authdata = encodeWhoAreYouAuthdata({ idNonce, enrSeq });
3334
const header = createHeader(PacketType.WhoAreYou, authdata, nonce);
34-
const maskingIv = randomBytes(MASKING_IV_SIZE);
35+
const maskingIv = toBuffer(randomBytes(MASKING_IV_SIZE));
3536
const message = Buffer.alloc(0);
3637
return {
3738
maskingIv,

packages/discv5/src/packet/encode.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import cipher from "bcrypto/lib/cipher.js";
1+
import Crypto from "node:crypto";
22
import { toBigIntBE, toBufferBE } from "bigint-buffer";
33

44
import { bufferToNumber, CodeError, fromHex, numberToBuffer, toHex } from "../util/index.js";
@@ -33,8 +33,7 @@ export function encodePacket(destId: string, packet: IPacket): Buffer {
3333
}
3434

3535
export function encodeHeader(destId: string, maskingIv: Buffer, header: IHeader): Buffer {
36-
const ctx = new cipher.Cipher("AES-128-CTR");
37-
ctx.init(fromHex(destId).slice(0, MASKING_KEY_SIZE), maskingIv);
36+
const ctx = Crypto.createCipheriv("aes-128-ctr", fromHex(destId).slice(0, MASKING_KEY_SIZE), maskingIv);
3837
return ctx.update(
3938
Buffer.concat([
4039
// static header
@@ -73,8 +72,7 @@ export function decodePacket(srcId: string, data: Buffer): IPacket {
7372
* Return the decoded header and the header as a buffer
7473
*/
7574
export function decodeHeader(srcId: string, maskingIv: Buffer, data: Buffer): [IHeader, Buffer] {
76-
const ctx = new cipher.Decipher("AES-128-CTR");
77-
ctx.init(fromHex(srcId).slice(0, MASKING_KEY_SIZE), maskingIv);
75+
const ctx = Crypto.createDecipheriv("aes-128-ctr", fromHex(srcId).slice(0, MASKING_KEY_SIZE), maskingIv);
7876
// unmask the static header
7977
const staticHeaderBuf = ctx.update(data.slice(0, STATIC_HEADER_SIZE));
8078

0 commit comments

Comments
 (0)