Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: convert to esm only #184

Merged
merged 2 commits into from
Jun 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const path = require('path');

module.exports = {
root: true,
env: {
Expand All @@ -14,7 +12,8 @@ module.exports = {
},
plugins: [
"@typescript-eslint",
"prettier"
"prettier",
"@chainsafe/eslint-plugin-node",
],
extends: [
"eslint:recommended",
Expand All @@ -24,13 +23,13 @@ module.exports = {
"prettier/prettier": "error",
//doesnt work, it reports false errors
"constructor-super": "off",
"@typescript-eslint/class-name-casing": "error",
//"@typescript-eslint/class-name-casing": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {
"allowExpressions": true
}],
"@typescript-eslint/func-call-spacing": "error",
"@typescript-eslint/interface-name-prefix": ["error", "always"],
"@typescript-eslint/member-ordering": "error",
//"@typescript-eslint/interface-name-prefix": ["error", "always"],
//"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-unused-vars": ["error", {
"varsIgnorePattern": "^_"
Expand All @@ -40,16 +39,14 @@ module.exports = {
"@typescript-eslint/type-annotation-spacing": "error",
"camelcase": "error",
"func-call-spacing": "off",
"max-len": ["error", {
"code": 120
}],
"new-parens": "error",
"no-caller": "error",
"no-bitwise": "off",
"no-console": "warn",
"no-var": "error",
"prefer-const": "error",
"quotes": ["error", "double"]
"quotes": ["error", "double"],
"@chainsafe/node/file-extension-in-import": ["error", "always", {esm: true}],
},
"overrides": [
{
Expand Down
5 changes: 3 additions & 2 deletions .mocharc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
colors: true
exit: true
require:
- ts-node/register
extension: ["ts"]
node-option:
- loader=ts-node/esm
File renamed without changes.
62 changes: 45 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,42 @@
"name": "@chainsafe/discv5",
"version": "0.8.1",
"description": "Discovery V5",
"main": "lib/index.js",
"type": "module",
"files": [
"lib"
],
"exports": {
".": {
"import": "./lib/index.js"
},
"./enr": {
"import": "./lib/enr/index.js"
},
"./kademlia": {
"import": "./lib/kademlia/index.js"
},
"./keypair": {
"import": "./lib/keypair/index.js"
},
"./libp2p": {
"import": "./lib/libp2p/index.js"
}
},
"typesVersions": {
"*": {
"*": [
"*",
"lib/*",
"lib/*/index"
]
}
},
"types": "./lib/index.d.ts",
"scripts": {
"check-types": "tsc --noEmit",
"build": "tsc --declaration --outDir lib",
"build": "tsc -p tsconfig.build.json",
"prepublishOnly": "yarn build",
"lint": "eslint --color --ext .ts src/",
"lint": "eslint --color --ext .ts src/ test/",
"test": "yarn test:unit && yarn test:e2e",
"test:unit": "mocha 'test/unit/**/*.test.ts'",
"test:e2e": "mocha 'test/e2e/**/*.test.ts'"
Expand All @@ -36,7 +63,9 @@
},
"homepage": "https://github.com/ChainSafe/discv5#readme",
"devDependencies": {
"@chainsafe/eslint-plugin-node": "^11.2.3",
"@dapplion/benchmark": "^0.1.6",
"@libp2p/peer-id-factory": "^1.0.11",
"@types/bn.js": "^4.11.5",
"@types/chai": "^4.2.0",
"@types/debug": "^4.1.5",
Expand All @@ -46,21 +75,24 @@
"@types/mocha": "^8.0.3",
"@types/node": "^12.0.10",
"@types/varint": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^2.7.0",
"@typescript-eslint/parser": "^2.7.0",
"chai": "^4.2.0",
"eslint": "^6.6.0",
"eslint-plugin-prettier": "^3.1.4",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"chai": "^4.3.6",
"eslint": "^8.17.0",
"eslint-plugin-prettier": "^4.0.0",
"karma": "^4.3.0",
"lru-cache": "^6.0.0",
"mocha": "^8.3.0",
"mocha": "^10.0.0",
"nyc": "^14.1.1",
"prettier": "^2.0.5",
"ts-node": "^8.3.0",
"typescript": "^3.8.3"
"prettier": "^2.6.2",
"ts-node": "^10.8.1",
"typescript": "^4.7.3"
},
"dependencies": {
"@chainsafe/abort-controller": "^3.0.1",
"@libp2p/crypto": "^0.22.14",
"@libp2p/interfaces": "2.0.3",
"@libp2p/peer-id": "^1.1.11",
"@multiformats/multiaddr": "^10.2.0",
"base64url": "^3.0.1",
"bcrypto": "^5.4.0",
"bigint-buffer": "^1.1.5",
Expand All @@ -69,10 +101,6 @@
"err-code": "^3.0.1",
"ip6addr": "^0.2.3",
"is-ip": "^3.1.0",
"libp2p-crypto": "^0.19.7",
"multiaddr": "^10.0.1",
"multihashes": "^4.0.3",
"peer-id": "^0.16.0",
"rlp": "^2.2.6",
"strict-event-emitter-types": "^2.0.0",
"varint": "^6.0.0"
Expand Down
4 changes: 2 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ISessionConfig } from "../session";
import { ILookupConfig } from "../kademlia";
import { ISessionConfig } from "../session/index.js";
import { ILookupConfig } from "../kademlia/index.js";

export type IDiscv5Config = ISessionConfig &
ILookupConfig & {
Expand Down
4 changes: 2 additions & 2 deletions src/enr/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { toHex } from "../util";
import { NodeId } from "./types";
import { toHex } from "../util/index.js";
import { NodeId } from "./types.js";

export function createNodeId(buffer: Buffer): NodeId {
if (buffer.length !== 32) {
Expand Down
56 changes: 31 additions & 25 deletions src/enr/enr.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { Multiaddr, protocols } from "multiaddr";
import { Multiaddr, protocols } from "@multiformats/multiaddr";
import base64url from "base64url";
import { toBigIntBE } from "bigint-buffer";
import * as RLP from "rlp";
import PeerId from "peer-id";
import muConvert from "multiaddr/src/convert";
import { PeerId } from "@libp2p/interfaces/peer-id";
import { convertToString, convertToBytes } from "@multiformats/multiaddr/convert";
import { encode as varintEncode } from "varint";

import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants";
import * as v4 from "./v4";
import { ENRKey, ENRValue, SequenceNumber, NodeId } from "./types";
import { createKeypair, KeypairType, IKeypair, createPeerIdFromKeypair, createKeypairFromPeerId } from "../keypair";
import { toNewUint8Array } from "../util";
import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants.js";
import * as v4 from "./v4.js";
import { ENRKey, ENRValue, SequenceNumber, NodeId } from "./types.js";
import {
createKeypair,
KeypairType,
IKeypair,
createPeerIdFromKeypair,
createKeypairFromPeerId,
} from "../keypair/index.js";
import { toNewUint8Array } from "../util/index.js";

export class ENR extends Map<ENRKey, ENRValue> {
public seq: SequenceNumber;
Expand All @@ -32,7 +38,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
static createFromPeerId(peerId: PeerId, kvs: Record<ENRKey, ENRValue> = {}): ENR {
const keypair = createKeypairFromPeerId(peerId);
switch (keypair.type) {
case KeypairType.secp256k1:
case KeypairType.Secp256k1:
return ENR.createV4(keypair.publicKey, kvs);
default:
throw new Error();
Expand Down Expand Up @@ -94,7 +100,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
get keypairType(): KeypairType {
switch (this.id) {
case "v4":
return KeypairType.secp256k1;
return KeypairType.Secp256k1;
default:
throw new Error(ERR_INVALID_ID);
}
Expand Down Expand Up @@ -128,15 +134,15 @@ export class ENR extends Map<ENRKey, ENRValue> {
get ip(): string | undefined {
const raw = this.get("ip");
if (raw) {
return muConvert.toString(protocols.names.ip4.code, toNewUint8Array(raw)) as string;
return convertToString("ip4", toNewUint8Array(raw)) as string;
} else {
return undefined;
}
}

set ip(ip: string | undefined) {
if (ip) {
this.set("ip", muConvert.toBytes(protocols.names.ip4.code, ip));
this.set("ip", convertToBytes("ip4", ip));
} else {
this.delete("ip");
}
Expand All @@ -145,7 +151,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
get tcp(): number | undefined {
const raw = this.get("tcp");
if (raw) {
return Number(muConvert.toString(protocols.names.tcp.code, toNewUint8Array(raw)));
return Number(convertToString("tcp", toNewUint8Array(raw)));
} else {
return undefined;
}
Expand All @@ -155,14 +161,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
if (port === undefined) {
this.delete("tcp");
} else {
this.set("tcp", muConvert.toBytes(protocols.names.tcp.code, port));
this.set("tcp", convertToBytes("tcp", String(port)));
}
}

get udp(): number | undefined {
const raw = this.get("udp");
if (raw) {
return Number(muConvert.toString(protocols.names.udp.code, toNewUint8Array(raw)));
return Number(convertToString("udp", toNewUint8Array(raw)));
} else {
return undefined;
}
Expand All @@ -172,22 +178,22 @@ export class ENR extends Map<ENRKey, ENRValue> {
if (port === undefined) {
this.delete("udp");
} else {
this.set("udp", muConvert.toBytes(protocols.names.udp.code, port));
this.set("udp", convertToBytes("udp", String(port)));
}
}

get ip6(): string | undefined {
const raw = this.get("ip6");
if (raw) {
return muConvert.toString(protocols.names.ip6.code, toNewUint8Array(raw)) as string;
return convertToString("ip6", toNewUint8Array(raw)) as string;
} else {
return undefined;
}
}

set ip6(ip: string | undefined) {
if (ip) {
this.set("ip6", muConvert.toBytes(protocols.names.ip6.code, ip));
this.set("ip6", convertToBytes("ip6", ip));
} else {
this.delete("ip6");
}
Expand All @@ -196,7 +202,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
get tcp6(): number | undefined {
const raw = this.get("tcp6");
if (raw) {
return Number(muConvert.toString(protocols.names.tcp.code, toNewUint8Array(raw)));
return Number(convertToString("tcp", toNewUint8Array(raw)));
} else {
return undefined;
}
Expand All @@ -206,14 +212,14 @@ export class ENR extends Map<ENRKey, ENRValue> {
if (port === undefined) {
this.delete("tcp6");
} else {
this.set("tcp6", muConvert.toBytes(protocols.names.tcp.code, port));
this.set("tcp6", convertToBytes("tcp", String(port)));
}
}

get udp6(): number | undefined {
const raw = this.get("udp6");
if (raw) {
return Number(muConvert.toString(protocols.names.udp.code, toNewUint8Array(raw)));
return Number(convertToString("udp", toNewUint8Array(raw)));
} else {
return undefined;
}
Expand All @@ -223,7 +229,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
if (port === undefined) {
this.delete("udp6");
} else {
this.set("udp6", muConvert.toBytes(protocols.names.udp.code, port));
this.set("udp6", convertToBytes("udp", String(port)));
}
}

Expand Down Expand Up @@ -262,9 +268,9 @@ export class ENR extends Map<ENRKey, ENRValue> {
// N bytes for the ip address
// 1 or 2 bytes for the protocol as buffer (tcp or udp)
// 2 bytes for the port
const ipMa = protocols.names[isIpv6 ? "ip6" : "ip4"];
const ipMa = protocols(isIpv6 ? "ip6" : "ip4");
const ipByteLen = ipMa.size / 8;
const protoMa = protocols.names[protoName];
const protoMa = protocols(protoName);
const protoBuf = varintEncode(protoMa.code);
const maBuf = new Uint8Array(3 + ipByteLen + protoBuf.length);
maBuf[0] = ipMa.code;
Expand Down Expand Up @@ -297,7 +303,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
const locationMultiaddr = this.getLocationMultiaddr(protocol);
if (locationMultiaddr) {
const peerId = await this.peerId();
return locationMultiaddr.encapsulate(`/p2p/${peerId.toB58String()}`);
return locationMultiaddr.encapsulate(`/p2p/${peerId.toString()}`);
}
}
verify(data: Buffer, signature: Buffer): boolean {
Expand Down
10 changes: 5 additions & 5 deletions src/enr/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as v4Crypto from "./v4";
import * as v4Crypto from "./v4.js";
export const v4 = v4Crypto;
export * from "./constants";
export * from "./enr";
export * from "./types";
export * from "./create";
export * from "./constants.js";
export * from "./enr.js";
export * from "./types.js";
export * from "./create.js";
8 changes: 4 additions & 4 deletions src/enr/v4.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import keccak = require("bcrypto/lib/keccak");
import secp256k1 = require("bcrypto/lib/secp256k1");
import keccak from "bcrypto/lib/keccak.js";
import secp256k1 from "bcrypto/lib/secp256k1.js";

import { NodeId } from "./types";
import { createNodeId } from "./create";
import { NodeId } from "./types.js";
import { createNodeId } from "./create.js";

export function hash(input: Buffer): Buffer {
return keccak.digest(input);
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from "./enr";
export * from "./kademlia";
export * from "./keypair";
export * from "./libp2p";
export * from "./service";
export * from "./session";
export * from "./transport";
export * from "./util";
export * from "./enr/index.js";
export * from "./kademlia/index.js";
export * from "./keypair/index.js";
export * from "./libp2p/index.js";
export * from "./service/index.js";
export * from "./session/index.js";
export * from "./transport/index.js";
export * from "./util/index.js";
Loading