Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
acolytec3 committed Jan 10, 2025
1 parent 729f733 commit e19e20a
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 57 deletions.
7 changes: 3 additions & 4 deletions packages/discv5/src/kademlia/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { toBigIntBE } from "bigint-buffer";
import { NodeId } from "@chainsafe/enr";
import { bytesToBigint, NodeId } from "@chainsafe/enr";

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

/**
* Computes the xor distance between two NodeIds
*/
export function distance(a: NodeId, b: NodeId): bigint {
return toBigIntBE(fromHex(a)) ^ toBigIntBE(fromHex(b));
return bytesToBigint(hexToBytes(a)) ^ bytesToBigint(hexToBytes(b));
}

export function log2Distance(a: NodeId, b: NodeId): number {
Expand Down
5 changes: 2 additions & 3 deletions packages/discv5/src/message/create.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { randomBytes, toBytes } from "@noble/hashes/utils";
import { toBigIntBE } from "bigint-buffer";
import { SequenceNumber, ENR } from "@chainsafe/enr";
import { bytesToBigint, SequenceNumber, ENR } from "@chainsafe/enr";

import {
RequestId,
Expand All @@ -13,7 +12,7 @@ import {
} from "./types.js";

export function createRequestId(): RequestId {
return toBigIntBE(randomBytes(8));
return bytesToBigint(randomBytes(8));
}

export function createPingMessage(enrSeq: SequenceNumber): IPingMessage {
Expand Down
39 changes: 19 additions & 20 deletions packages/discv5/src/message/decode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as RLP from "@ethereumjs/rlp";
import { toBigIntBE } from "bigint-buffer";
import { ENR } from "@chainsafe/enr";
import { bytesToBigint, ENR } from "@chainsafe/enr";
import {
IPingMessage,
IPongMessage,
Expand Down Expand Up @@ -54,8 +53,8 @@ function decodePing(data: Uint8Array): IPingMessage {
}
return {
type: MessageType.PING,
id: toBigIntBE(rlpRaw[0]),
enrSeq: toBigIntBE(rlpRaw[1]),
id: bytesToBigint(rlpRaw[0]),
enrSeq: bytesToBigint(rlpRaw[1]),
};
}

Expand All @@ -75,12 +74,12 @@ function decodePong(data: Uint8Array): IPongMessage {
if (rlpRaw[3].length > 2) {
throw new Error(ERR_INVALID_MESSAGE);
}
const port = rlpRaw[3].length ? rlpRaw[3].readUIntBE(0, rlpRaw[3].length) : 0;
const port = rlpRaw[3].length ? new DataView(rlpRaw[3].buffer).getUint16(0, false) : 0;

return {
type: MessageType.PONG,
id: toBigIntBE(rlpRaw[0]),
enrSeq: toBigIntBE(rlpRaw[1]),
id: bytesToBigint(rlpRaw[0]),
enrSeq: bytesToBigint(rlpRaw[1]),
addr: { ip, port },
};
}
Expand All @@ -93,24 +92,24 @@ function decodeFindNode(data: Uint8Array): IFindNodeMessage {
if (!Array.isArray(rlpRaw[1])) {
throw new Error(ERR_INVALID_MESSAGE);
}
const distances = (rlpRaw[1] as Uint8Array[]).map((x) => (x.length ? x.readUIntBE(0, x.length) : 0));
const distances = (rlpRaw[1] as Uint8Array[]).map((x) => (x.length ? Number(bytesToBigint(x)) : 0));
return {
type: MessageType.FINDNODE,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
distances,
};
}

function decodeNodes(data: Uint8Array): INodesMessage {
const rlpRaw = RLP.decode(data.slice(1)) as Uint8Array[];
const rlpRaw = RLP.decode(data.slice(1)) as RLP.NestedUint8Array;
if (!Array.isArray(rlpRaw) || rlpRaw.length !== 3 || !Array.isArray(rlpRaw[2])) {
throw new Error(ERR_INVALID_MESSAGE);
}
return {
type: MessageType.NODES,
id: toBigIntBE(rlpRaw[0]),
total: rlpRaw[1].length ? rlpRaw[1].readUIntBE(0, rlpRaw[1].length) : 0,
enrs: rlpRaw[2].map((enrRaw) => ENR.decodeFromValues(enrRaw)),
id: bytesToBigint(rlpRaw[0] as Uint8Array),
total: rlpRaw[1].length ? Number(bytesToBigint(rlpRaw[1] as Uint8Array)) : 0,
enrs: rlpRaw[2].map((enrRaw) => ENR.decodeFromValues(enrRaw as Uint8Array[])),
};
}

Expand All @@ -121,7 +120,7 @@ function decodeTalkReq(data: Uint8Array): ITalkReqMessage {
}
return {
type: MessageType.TALKREQ,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
protocol: rlpRaw[1],
request: rlpRaw[2],
};
Expand All @@ -134,7 +133,7 @@ function decodeTalkResp(data: Uint8Array): ITalkRespMessage {
}
return {
type: MessageType.TALKRESP,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
response: rlpRaw[1],
};
}
Expand All @@ -146,7 +145,7 @@ function decodeRegTopic(data: Uint8Array): IRegTopicMessage {
}
return {
type: MessageType.REGTOPIC,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
topic: rlpRaw[1],
enr: ENR.decodeFromValues(rlpRaw[2] as Uint8Array[]),
ticket: rlpRaw[3],
Expand All @@ -160,9 +159,9 @@ function decodeTicket(data: Uint8Array): ITicketMessage {
}
return {
type: MessageType.TICKET,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
ticket: rlpRaw[1],
waitTime: rlpRaw[2].length ? rlpRaw[2].readUIntBE(0, rlpRaw[2].length) : 0,
waitTime: rlpRaw[2].length ? Number(bytesToBigint(rlpRaw[2] as Uint8Array)) : 0,
};
}

Expand All @@ -173,7 +172,7 @@ function decodeRegConfirmation(data: Uint8Array): IRegConfirmationMessage {
}
return {
type: MessageType.REGCONFIRMATION,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
topic: rlpRaw[1],
};
}
Expand All @@ -185,7 +184,7 @@ function decodeTopicQuery(data: Uint8Array): ITopicQueryMessage {
}
return {
type: MessageType.TOPICQUERY,
id: toBigIntBE(rlpRaw[0]),
id: bytesToBigint(rlpRaw[0]),
topic: rlpRaw[1],
};
}
4 changes: 2 additions & 2 deletions packages/discv5/src/message/encode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as RLP from "@ethereumjs/rlp";
import { ipToBytes } from "../util/ip.js";
import { concatBytes, hexToBytes } from "@noble/hashes/utils";
import { concatBytes } from "@noble/hashes/utils";
import {
IPingMessage,
IPongMessage,
Expand All @@ -15,6 +15,7 @@ import {
ITalkReqMessage,
ITalkRespMessage,
} from "./types.js";
import { bigintToBytes } from "@chainsafe/enr";

export function encode(message: Message): Uint8Array {
switch (message.type) {
Expand All @@ -41,7 +42,6 @@ export function encode(message: Message): Uint8Array {
}
}


export function encodePingMessage(m: IPingMessage): Uint8Array {
return concatBytes(Uint8Array.from([MessageType.PING]), RLP.encode([bigintToBytes(m.id), bigintToBytes(m.enrSeq)]));
}
Expand Down
8 changes: 4 additions & 4 deletions packages/discv5/src/packet/encode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Crypto from "node:crypto";
import { toBigIntBE, toBufferBE } from "bigint-buffer";

import { bufferToNumber, CodeError, numberToBuffer } from "../util/index.js";
import {
Expand Down Expand Up @@ -27,7 +26,8 @@ import {
MIN_HANDSHAKE_AUTHDATA_SIZE,
} from "./constants.js";
import { IHandshakeAuthdata, IHeader, IMessageAuthdata, IPacket, IWhoAreYouAuthdata, PacketType } from "./types.js";
import { bytesToHex, concatBytes, hexToBytes } from "@noble/hashes/utils.js";
import { bytesToHex, concatBytes, hexToBytes } from "ethereum-cryptography/utils.js";
import { bigintToBytes, bytesToBigint } from "@chainsafe/enr";

export function encodePacket(destId: string, packet: IPacket): Uint8Array {
return concatBytes(packet.maskingIv, encodeHeader(destId, packet.maskingIv, packet.header), packet.message);
Expand Down Expand Up @@ -125,7 +125,7 @@ export function decodeHeader(srcId: string, maskingIv: Uint8Array, data: Uint8Ar
// authdata

export function encodeWhoAreYouAuthdata(authdata: IWhoAreYouAuthdata): Uint8Array {
return concatBytes(authdata.idNonce, toBufferBE(authdata.enrSeq, 8));
return concatBytes(authdata.idNonce, bigintToBytes(authdata.enrSeq));
}

export function encodeMessageAuthdata(authdata: IMessageAuthdata): Uint8Array {
Expand All @@ -149,7 +149,7 @@ export function decodeWhoAreYouAuthdata(data: Uint8Array): IWhoAreYouAuthdata {
}
return {
idNonce: data.slice(0, ID_NONCE_SIZE),
enrSeq: toBigIntBE(data.slice(ID_NONCE_SIZE)),
enrSeq: bytesToBigint(data.slice(ID_NONCE_SIZE)),
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/discv5/src/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type PongResponse = {
addr: SocketAddress;
};

export type ResponseType = Buffer | ENR[] | PongResponse;
export type ResponseType = Uint8Array | ENR[] | PongResponse;

export function toResponseType(response: IPongMessage | INodesMessage | ITalkRespMessage): ResponseType {
switch (response.type) {
Expand Down
2 changes: 1 addition & 1 deletion packages/discv5/src/session/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NodeId } from "@chainsafe/enr";

import { generateKeypair, IKeypair, createKeypair } from "../keypair/index.js";
import { getDiscv5Crypto } from "../util/crypto.js";
import { concatBytes, hexToBytes, utf8ToBytes } from "@noble/hashes/utils.js";
import { concatBytes, hexToBytes, utf8ToBytes } from "ethereum-cryptography/utils.js";

// Implementation for generating session keys in the Discv5 protocol.
// Currently, Diffie-Hellman key agreement is performed with known public key types. Session keys
Expand Down
3 changes: 1 addition & 2 deletions packages/discv5/src/session/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import StrictEventEmitter from "strict-event-emitter-types";
import debug from "debug";
import { Multiaddr } from "@multiformats/multiaddr";
import { ENR, SignableENR } from "@chainsafe/enr";

import { bytesToHex, equalsBytes } from "ethereum-cryptography/utils.js";
import { IPMode, ITransportService } from "../transport/index.js";
import {
PacketType,
Expand Down Expand Up @@ -43,7 +43,6 @@ import { LRUCache } from "lru-cache";
import { TimeoutMap } from "../util/index.js";
import { IDiscv5Metrics } from "../metrics.js";
import { getSocketAddressMultiaddrOnENR } from "../util/ip.js";
import { bytesToHex } from "@noble/hashes/utils.js";

const log = debug("discv5:sessionService");

Expand Down
2 changes: 1 addition & 1 deletion packages/discv5/test/unit/message/codec.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect } from "chai";
import { ENR } from "@chainsafe/enr";
import { Message, MessageType, decode, encode } from "../../../src/message/index.js";
import { hexToBytes } from "@noble/hashes/utils.js";
import { hexToBytes } from "ethereum-cryptography/utils.js";

describe("message", () => {
const testCases: {
Expand Down
2 changes: 1 addition & 1 deletion packages/discv5/test/unit/session/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SessionService } from "../../../src/session/index.js";
import { createFindNodeMessage } from "../../../src/message/index.js";
import { defaultConfig } from "../../../src/config/index.js";
import { createNodeContact } from "../../../src/session/nodeInfo.js";
import { hexToBytes } from "@noble/hashes/utils.js";
import { hexToBytes } from "ethereum-cryptography/utils.js";

describe("session service", () => {
const kp0 = createKeypair({
Expand Down
2 changes: 1 addition & 1 deletion packages/discv5/test/unit/transport/udp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Multiaddr, multiaddr } from "@multiformats/multiaddr";

import { PacketType, IPacket, NONCE_SIZE, MASKING_IV_SIZE } from "../../../src/packet/index.js";
import { UDPTransportService } from "../../../src/transport/index.js";
import { bytesToHex } from "@noble/hashes/utils.js";
import { bytesToHex } from "ethereum-cryptography/utils.js";

describe("UDP4 transport", () => {
const address = "127.0.0.1";
Expand Down
4 changes: 2 additions & 2 deletions packages/enr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
"@libp2p/interface": "^2.0.1",
"@libp2p/peer-id": "^5.0.1",
"@multiformats/multiaddr": "^12.1.10",
"@scure/base": "^1.2.1",
"ethereum-cryptography": "^2.2.0",
"uint8-varint": "^2.0.2",
"uint8arrays": "^5.0.1"
"uint8-varint": "^2.0.2"
}
}
6 changes: 3 additions & 3 deletions packages/enr/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NodeId } from "./types.js";
import { bytesToHex, hexToBytes } from "ethereum-cryptography/utils";

import { base64url } from "@scure/base";
// multiaddr 8.0.0 expects an Uint8Array with internal buffer starting at 0 offset
export function toNewUint8Array(buf: Uint8Array): Uint8Array {
const arrayBuffer = buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
Expand All @@ -11,14 +11,14 @@ export function toBase64url(buf: Uint8Array): string {
if (globalThis.Buffer != null) {
return globalThis.Buffer.from(buf).toString("base64url");
}
return toString(buf, "base64url");
return base64url.encode(buf);
}

export function fromBase64url(str: string): Uint8Array {
if (globalThis.Buffer != null) {
return globalThis.Buffer.from(str, "base64url");
}
return fromString(str, "base64url");
return base64url.decode(str);
}

export function createNodeId(buf: Uint8Array): NodeId {
Expand Down
22 changes: 10 additions & 12 deletions packages/enr/test/unit/enr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import { generateKeyPair } from "@libp2p/crypto/keys";
import { multiaddr } from "@multiformats/multiaddr";
import { BaseENR, ENR, SignableENR, getV4Crypto } from "../../src/index.js";
import { peerIdFromString } from "@libp2p/peer-id";
import { utf8ToBytes } from "@noble/hashes/utils.js";
import { bytesToHex } from "ccxt/js/src/static_dependencies/noble-hashes/utils.js";
import { bytesToHex, hexToBytes, utf8ToBytes } from "ethereum-cryptography/utils.js";

describe("ENR spec test vector", () => {
// spec enr https://eips.ethereum.org/EIPS/eip-778
const privateKey = Buffer.from("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", "hex");
const privateKey = hexToBytes("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
const publicKey = getV4Crypto().publicKey(privateKey);
const text =
"enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8";
const seq = BigInt(1);
const signature = Buffer.from(
"7098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c",
"hex"
const signature = hexToBytes(
"7098ad865b00a582051940cb9cf36836572411a47278783077011599ed5cd16b76f2635f4e234738f30813a89eb9137e3e3df5266e3a1f11df72ecf1145ccb9c"
);
const kvs = new Map(
Object.entries({
Expand Down Expand Up @@ -66,7 +64,7 @@ describe("ENR spec test vector", () => {
});

describe("ENR multiaddr support", () => {
const privateKey = Buffer.from("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291", "hex");
const privateKey = hexToBytes("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291");
let record: SignableENR;

beforeEach(() => {
Expand Down Expand Up @@ -242,7 +240,7 @@ describe("ENR", function () {
const txt = enr.encodeTxt();
expect(txt.slice(0, 4)).to.be.equal("enr:");
const enr2 = ENR.decodeTxt(txt);
expect(bytesToHex(enr2.signature as Buffer)).to.be.equal(bytesToHex(enr.signature as Buffer));
expect(bytesToHex(enr2.signature as Uint8Array)).to.be.equal(bytesToHex(enr.signature as Uint8Array));
const mu = enr2.getLocationMultiaddr("udp")!;
expect(mu.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
});
Expand All @@ -251,7 +249,7 @@ describe("ENR", function () {
const txt =
"enr:-Ku4QMh15cIjmnq-co5S3tYaNXxDzKTgj0ufusA-QfZ66EWHNsULt2kb0eTHoo1Dkjvvf6CAHDS1Di-htjiPFZzaIPcLh2F0dG5ldHOIAAAAAAAAAACEZXRoMpD2d10HAAABE________x8AgmlkgnY0gmlwhHZFkMSJc2VjcDI1NmsxoQIWSDEWdHwdEA3Lw2B_byeFQOINTZ0GdtF9DBjes6JqtIN1ZHCCIyg";
const enr = ENR.decodeTxt(txt);
const eth2 = enr.kvs.get("eth2") as Buffer;
const eth2 = enr.kvs.get("eth2") as Uint8Array;
expect(eth2).to.not.be.undefined;
expect(bytesToHex(eth2)).to.be.equal("f6775d0700000113ffffffffffff1f00");
});
Expand All @@ -271,7 +269,7 @@ describe("ENR", function () {
ENR.decodeTxt(txt);
expect.fail("Expect error here");
} catch (err: any) {
expect(err.message).to.be.equal("id not found");
expect(err.message).to.be.equal("invalid RLP: encoded list too short");
}
});

Expand All @@ -295,7 +293,7 @@ describe("ENR fuzzing testcases", () => {
try {
ENR.decodeTxt(txt);
} catch (e: any) {
expect(e.message).to.equal("Decoded ENR invalid signature: must be a byte array");
expect(e.message).to.equal("invalid RLP (safeSlice): end slice of Uint8Array out-of-bounds");
}
});
it("should throw error in invalid sequence number", () => {
Expand All @@ -304,7 +302,7 @@ describe("ENR fuzzing testcases", () => {
try {
ENR.decodeTxt(txt);
} catch (e: any) {
expect(e.message).to.equal("Decoded ENR invalid sequence number: must be a byte array");
expect(e.message).to.equal("invalid RLP (safeSlice): end slice of Uint8Array out-of-bounds");
}
});
});
Loading

0 comments on commit e19e20a

Please sign in to comment.