Skip to content

Commit 1cdc424

Browse files
authored
feat!: Convert buffer usage to Uint8Array (#306)
1 parent e6af632 commit 1cdc424

File tree

34 files changed

+373
-418
lines changed

34 files changed

+373
-418
lines changed

packages/discv5/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@
6767
"devDependencies": {},
6868
"dependencies": {
6969
"@chainsafe/enr": "^4.0.1",
70+
"@ethereumjs/rlp": "^5.0.2",
7071
"@libp2p/crypto": "^5.0.1",
7172
"@libp2p/interface": "^2.0.1",
7273
"@multiformats/multiaddr": "^12.1.10",
7374
"@noble/hashes": "^1.7.0",
7475
"@noble/secp256k1": "^2.2.2",
75-
"bigint-buffer": "^1.1.5",
7676
"debug": "^4.3.1",
7777
"lru-cache": "^10.1.0",
78-
"rlp": "^2.2.6",
7978
"strict-event-emitter-types": "^2.0.0"
8079
}
8180
}

packages/discv5/src/kademlia/util.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { toBigIntBE } from "bigint-buffer";
2-
import { NodeId } from "@chainsafe/enr";
1+
import { bytesToBigint, NodeId } from "@chainsafe/enr";
32

4-
import { fromHex } from "../util/index.js";
53
import { NUM_BUCKETS } from "./constants.js";
4+
import { hexToBytes } from "ethereum-cryptography/utils.js";
65

76
/**
87
* Computes the xor distance between two NodeIds
98
*/
109
export function distance(a: NodeId, b: NodeId): bigint {
11-
return toBigIntBE(fromHex(a)) ^ toBigIntBE(fromHex(b));
10+
return bytesToBigint(hexToBytes(a)) ^ bytesToBigint(hexToBytes(b));
1211
}
1312

1413
export function log2Distance(a: NodeId, b: NodeId): number {

packages/discv5/src/keypair/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { KeyType } from "@libp2p/interface";
33
import { IKeypair } from "./types.js";
44
import { ERR_TYPE_NOT_IMPLEMENTED } from "./constants.js";
55
import { Secp256k1Keypair } from "./secp256k1.js";
6-
import { toBuffer } from "../util/index.js";
76

87
export * from "./types.js";
98
export * from "./secp256k1.js";
@@ -33,8 +32,8 @@ export function createKeypair(init: KeypairInit): IKeypair {
3332
switch (init.type) {
3433
case "secp256k1":
3534
return new Secp256k1Keypair(
36-
init.privateKey ? toBuffer(init.privateKey) : undefined,
37-
init.publicKey ? toBuffer(init.publicKey) : undefined
35+
init.privateKey ? init.privateKey : undefined,
36+
init.publicKey ? init.publicKey : undefined
3837
);
3938
default:
4039
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);

packages/discv5/src/keypair/secp256k1.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ import { KeyType } from "@libp2p/interface";
22
import { AbstractKeypair, IKeypair, IKeypairClass } from "./types.js";
33
import { ERR_INVALID_KEYPAIR_TYPE } from "./constants.js";
44
import { getDiscv5Crypto } from "../util/crypto.js";
5-
import { toBuffer } from "../util/index.js";
6-
7-
export function secp256k1PublicKeyToCompressed(publicKey: Buffer): Buffer {
5+
import { concatBytes } from "@noble/hashes/utils";
6+
export function secp256k1PublicKeyToCompressed(publicKey: Uint8Array): Uint8Array {
87
if (publicKey.length === 64) {
9-
publicKey = Buffer.concat([Buffer.from([4]), publicKey]);
8+
publicKey = concatBytes(Uint8Array.from([4]), publicKey);
109
}
11-
return toBuffer(getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, true));
10+
return getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, true);
1211
}
1312

14-
export function secp256k1PublicKeyToRaw(publicKey: Buffer): Buffer {
15-
return toBuffer(getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, false));
13+
export function secp256k1PublicKeyToRaw(publicKey: Uint8Array): Uint8Array {
14+
return getDiscv5Crypto().secp256k1.publicKeyConvert(publicKey, false);
1615
}
1716

1817
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends AbstractKeypair implements IKeypair {
1918
readonly type: KeyType;
2019

21-
constructor(privateKey?: Buffer, publicKey?: Buffer) {
22-
let pub = publicKey ?? toBuffer(getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey!));
20+
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
21+
let pub = publicKey ?? getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey!);
2322
if (pub) {
2423
pub = secp256k1PublicKeyToCompressed(pub);
2524
}
@@ -28,8 +27,8 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
2827
}
2928

3029
static generate(): Secp256k1Keypair {
31-
const privateKey = toBuffer(getDiscv5Crypto().secp256k1.generatePrivateKey());
32-
const publicKey = toBuffer(getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey));
30+
const privateKey = getDiscv5Crypto().secp256k1.generatePrivateKey();
31+
const publicKey = getDiscv5Crypto().secp256k1.publicKeyCreate(privateKey);
3332
return new Secp256k1Keypair(privateKey, publicKey);
3433
}
3534

@@ -45,16 +44,16 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair extends Ab
4544
}
4645
return true;
4746
}
48-
sign(msg: Buffer): Buffer {
49-
return toBuffer(getDiscv5Crypto().secp256k1.sign(msg, this.privateKey));
47+
sign(msg: Uint8Array): Uint8Array {
48+
return getDiscv5Crypto().secp256k1.sign(msg, this.privateKey);
5049
}
51-
verify(msg: Buffer, sig: Buffer): boolean {
50+
verify(msg: Uint8Array, sig: Uint8Array): boolean {
5251
return getDiscv5Crypto().secp256k1.verify(this.publicKey, msg, sig);
5352
}
54-
deriveSecret(keypair: IKeypair): Buffer {
53+
deriveSecret(keypair: IKeypair): Uint8Array {
5554
if (keypair.type !== this.type) {
5655
throw new Error(ERR_INVALID_KEYPAIR_TYPE);
5756
}
58-
return toBuffer(getDiscv5Crypto().secp256k1.deriveSecret(this.privateKey, keypair.publicKey));
57+
return getDiscv5Crypto().secp256k1.deriveSecret(this.privateKey, keypair.publicKey);
5958
}
6059
};

packages/discv5/src/keypair/types.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ import { KeyType } from "@libp2p/interface";
22

33
export interface IKeypair {
44
type: KeyType;
5-
privateKey: Buffer;
6-
publicKey: Buffer;
5+
privateKey: Uint8Array;
6+
publicKey: Uint8Array;
77
privateKeyVerify(): boolean;
88
publicKeyVerify(): boolean;
9-
sign(msg: Buffer): Buffer;
10-
verify(msg: Buffer, sig: Buffer): boolean;
11-
deriveSecret(keypair: IKeypair): Buffer;
9+
sign(msg: Uint8Array): Uint8Array;
10+
verify(msg: Uint8Array, sig: Uint8Array): boolean;
11+
deriveSecret(keypair: IKeypair): Uint8Array;
1212
hasPrivateKey(): boolean;
1313
}
1414

1515
export interface IKeypairClass {
16-
new (privateKey?: Buffer, publicKey?: Buffer): IKeypair;
16+
new (privateKey?: Uint8Array, publicKey?: Uint8Array): IKeypair;
1717
generate(): IKeypair;
1818
}
1919

2020
export abstract class AbstractKeypair {
21-
readonly _privateKey?: Buffer;
22-
readonly _publicKey?: Buffer;
23-
constructor(privateKey?: Buffer, publicKey?: Buffer) {
21+
readonly _privateKey?: Uint8Array;
22+
readonly _publicKey?: Uint8Array;
23+
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
2424
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
2525
throw new Error("Invalid private key");
2626
}
2727
if ((this._publicKey = publicKey) && !this.publicKeyVerify()) {
2828
throw new Error("Invalid private key");
2929
}
3030
}
31-
get privateKey(): Buffer {
31+
get privateKey(): Uint8Array {
3232
if (!this._privateKey) {
3333
throw new Error();
3434
}
3535
return this._privateKey;
3636
}
37-
get publicKey(): Buffer {
37+
get publicKey(): Uint8Array {
3838
if (!this._publicKey) {
3939
throw new Error();
4040
}

packages/discv5/src/message/create.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { randomBytes } from "@noble/hashes/utils";
2-
import { toBigIntBE } from "bigint-buffer";
3-
import { SequenceNumber, ENR } from "@chainsafe/enr";
1+
import { randomBytes, toBytes } from "@noble/hashes/utils";
2+
import { bytesToBigint, SequenceNumber, ENR } from "@chainsafe/enr";
43

54
import {
65
RequestId,
@@ -11,10 +10,9 @@ import {
1110
ITalkReqMessage,
1211
ITalkRespMessage,
1312
} from "./types.js";
14-
import { toBuffer } from "../index.js";
1513

1614
export function createRequestId(): RequestId {
17-
return toBigIntBE(toBuffer(randomBytes(8)));
15+
return bytesToBigint(randomBytes(8));
1816
}
1917

2018
export function createPingMessage(enrSeq: SequenceNumber): IPingMessage {
@@ -46,14 +44,14 @@ export function createTalkRequestMessage(request: string | Uint8Array, protocol:
4644
return {
4745
type: MessageType.TALKREQ,
4846
id: createRequestId(),
49-
protocol: Buffer.from(protocol),
50-
request: Buffer.from(request),
47+
protocol: toBytes(protocol),
48+
request: toBytes(request),
5149
};
5250
}
5351
export function createTalkResponseMessage(requestId: RequestId, payload: Uint8Array): ITalkRespMessage {
5452
return {
5553
type: MessageType.TALKRESP,
5654
id: requestId,
57-
response: toBuffer(payload),
55+
response: payload,
5856
};
5957
}

0 commit comments

Comments
 (0)