Skip to content

Commit ead057d

Browse files
authored
feat!: update libp2p deps (#295)
1 parent ac0cc72 commit ead057d

File tree

18 files changed

+271
-256
lines changed

18 files changed

+271
-256
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"version": "0.0.0",
55
"description": "Discovery V5 monorepo",
66
"type": "module",
7-
"workspaces": ["packages/*"],
7+
"workspaces": [
8+
"packages/*"
9+
],
810
"scripts": {
911
"check-types": "lerna run check-types",
1012
"build": "lerna run build",
@@ -38,5 +40,6 @@
3840
"prettier": "^2.6.2",
3941
"ts-node": "^10.8.1",
4042
"typescript": "^4.7.3"
41-
}
43+
},
44+
"packageManager": "yarn@1.22.22+sha256.c17d3797fb9a9115bf375e31bfd30058cac6bc9c3b8807a3d8cb2094794b51ca"
4245
}

packages/discv5/package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,11 @@
6464
"url": "https://github.com/ChainSafe/discv5/issues"
6565
},
6666
"homepage": "https://github.com/ChainSafe/discv5#readme",
67-
"devDependencies": {
68-
"@libp2p/peer-id-factory": "^4.0.3"
69-
},
67+
"devDependencies": {},
7068
"dependencies": {
7169
"@chainsafe/enr": "^3.1.0",
72-
"@libp2p/crypto": "^4.0.1",
73-
"@libp2p/interface": "^1.1.1",
70+
"@libp2p/crypto": "^5.0.0",
71+
"@libp2p/interface": "^2.0.0",
7472
"@multiformats/multiaddr": "^12.1.10",
7573
"bcrypto": "^5.4.0",
7674
"bigint-buffer": "^1.1.5",

packages/discv5/src/libp2p/discv5.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import {
2-
CustomEvent,
32
PeerDiscovery,
43
PeerDiscoveryEvents,
54
peerDiscoverySymbol,
6-
PeerId,
75
PeerInfo,
6+
PrivateKey,
87
TypedEventEmitter,
98
} from "@libp2p/interface";
109
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";
@@ -58,7 +57,7 @@ export interface IDiscv5DiscoveryInputOptions extends Partial<IDiscv5Config> {
5857
}
5958

6059
export interface IDiscv5DiscoveryOptions extends IDiscv5DiscoveryInputOptions {
61-
peerId: PeerId;
60+
privateKey: PrivateKey;
6261
}
6362

6463
/**
@@ -77,7 +76,7 @@ export class Discv5Discovery extends TypedEventEmitter<PeerDiscoveryEvents> impl
7776
super();
7877
this.discv5 = Discv5.create({
7978
enr: options.enr,
80-
peerId: options.peerId,
79+
privateKey: options.privateKey,
8180
bindAddrs: {
8281
ip4: options.bindAddrs.ip4 ? multiaddr(options.bindAddrs.ip4) : undefined,
8382
ip6: options.bindAddrs.ip6 ? multiaddr(options.bindAddrs.ip6) : undefined,
@@ -142,7 +141,7 @@ export class Discv5Discovery extends TypedEventEmitter<PeerDiscoveryEvents> impl
142141
this.dispatchEvent(
143142
new CustomEvent<PeerInfo>("peer", {
144143
detail: {
145-
id: await enr.peerId(),
144+
id: enr.peerId,
146145
multiaddrs,
147146
},
148147
})

packages/discv5/src/packet/encode.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { CodeError } from "@libp2p/interface";
21
import cipher from "bcrypto/lib/cipher.js";
32
import { toBigIntBE, toBufferBE } from "bigint-buffer";
43

5-
import { bufferToNumber, fromHex, numberToBuffer, toHex } from "../util/index.js";
4+
import { bufferToNumber, CodeError, fromHex, numberToBuffer, toHex } from "../util/index.js";
65
import {
76
AUTHDATA_SIZE_SIZE,
87
EPH_KEY_SIZE_SIZE,

packages/discv5/src/service/service.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,8 @@ import { EventEmitter } from "events";
22
import debug from "debug";
33
import { randomBytes } from "@libp2p/crypto";
44
import { Multiaddr } from "@multiformats/multiaddr";
5-
import { CodeError, PeerId } from "@libp2p/interface";
6-
import {
7-
createPeerIdFromPublicKey,
8-
createPrivateKeyFromPeerId,
9-
ENR,
10-
NodeId,
11-
MAX_RECORD_SIZE,
12-
createNodeId,
13-
SignableENR,
14-
} from "@chainsafe/enr";
5+
import { PeerId, PrivateKey } from "@libp2p/interface";
6+
import { createPeerIdFromPublicKey, ENR, NodeId, MAX_RECORD_SIZE, createNodeId, SignableENR } from "@chainsafe/enr";
157

168
import { BindAddrs, IPMode, ITransportService, UDPTransportService } from "../transport/index.js";
179
import { MAX_PACKET_SIZE } from "../packet/index.js";
@@ -44,7 +36,7 @@ import {
4436
RequestId,
4537
} from "../message/index.js";
4638
import { AddrVotes } from "./addrVotes.js";
47-
import { toBuffer } from "../util/index.js";
39+
import { CodeError, toBuffer } from "../util/index.js";
4840
import { IDiscv5Config, defaultConfig } from "../config/index.js";
4941
import { createNodeContact, getNodeAddress, getNodeId, INodeAddress, NodeContact } from "../session/nodeInfo.js";
5042
import {
@@ -89,7 +81,7 @@ const log = debug("discv5:service");
8981

9082
export interface IDiscv5CreateOptions {
9183
enr: SignableENRInput;
92-
peerId: PeerId;
84+
privateKey: PrivateKey;
9385
bindAddrs: BindAddrs;
9486
config?: Partial<IDiscv5Config>;
9587
metricsRegistry?: MetricsRegister | null;
@@ -207,16 +199,15 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
207199
* Convenience method to create a new discv5 service.
208200
*
209201
* @param enr the ENR record identifying the current node.
210-
* @param peerId the PeerId with the keypair that identifies the enr
202+
* @param privateKey the PrivateKey that identifies the enr
211203
* @param multiaddr The multiaddr which contains the network interface and port to which the UDP server binds
212204
*/
213205
public static create(opts: IDiscv5CreateOptions): Discv5 {
214-
const { enr, peerId, bindAddrs, config = {}, metricsRegistry, transport } = opts;
206+
const { enr, privateKey, bindAddrs, config = {}, metricsRegistry, transport } = opts;
215207
const fullConfig = { ...defaultConfig, ...config };
216208
const metrics = metricsRegistry ? createDiscv5Metrics(metricsRegistry) : undefined;
217-
const { type, privateKey } = createPrivateKeyFromPeerId(peerId);
218-
const keypair = createKeypair({ type, privateKey });
219-
const decodedEnr = typeof enr === "string" ? SignableENR.decodeTxt(enr, privateKey) : enr;
209+
const keypair = createKeypair({ type: privateKey.type, privateKey: privateKey.raw });
210+
const decodedEnr = typeof enr === "string" ? SignableENR.decodeTxt(enr, privateKey.raw) : enr;
220211
const rateLimiter = opts.rateLimiterOpts && new RateLimiter(opts.rateLimiterOpts, metrics ?? null);
221212
const sessionService = new SessionService(
222213
fullConfig,
@@ -309,7 +300,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
309300
return this.sessionService.keypair;
310301
}
311302

312-
public peerId(): Promise<PeerId> {
303+
public get peerId(): PeerId {
313304
return createPeerIdFromPublicKey(this.keypair.type, this.keypair.publicKey);
314305
}
315306

packages/discv5/src/service/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { EventEmitter } from "events";
22
import StrictEventEmitter from "strict-event-emitter-types";
3-
import { CodeError } from "@libp2p/interface";
43
import { Multiaddr } from "@multiformats/multiaddr";
54
import { ENR, SequenceNumber, SignableENR } from "@chainsafe/enr";
65

@@ -14,7 +13,7 @@ import {
1413
} from "../message/index.js";
1514
import { INodeAddress, NodeContact } from "../session/nodeInfo.js";
1615
import { ConnectionDirection } from "../session/index.js";
17-
import { SocketAddress } from "../util/ip.js";
16+
import { CodeError, SocketAddress } from "../util/index.js";
1817

1918
export interface IDiscv5Events {
2019
/**

packages/discv5/src/session/nodeInfo.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Multiaddr, isMultiaddr } from "@multiformats/multiaddr";
22
import { peerIdFromString } from "@libp2p/peer-id";
3-
import { createPublicKeyFromPeerId, ENR, NodeId, getV4Crypto } from "@chainsafe/enr";
3+
import { ENR, NodeId, getV4Crypto } from "@chainsafe/enr";
44
import { createKeypair, IKeypair } from "../keypair/index.js";
55
import { IPMode } from "../transport/types.js";
66
import { getSocketAddressMultiaddrOnENR } from "../util/ip.js";
@@ -81,8 +81,11 @@ export function createNodeContact(input: ENR | Multiaddr, ipMode: IPMode): NodeC
8181
throw new Error("Multiaddr must specify a peer id");
8282
}
8383
const peerId = peerIdFromString(peerIdStr);
84-
const { type, publicKey } = createPublicKeyFromPeerId(peerId);
85-
const keypair = createKeypair({ type, publicKey });
84+
const publicKey = peerId.publicKey;
85+
if (!publicKey) {
86+
throw new Error("Peer ID must have a public key");
87+
}
88+
const keypair = createKeypair({ type: publicKey.type, publicKey: publicKey.raw });
8689
const nodeId = getV4Crypto().nodeId(keypair.publicKey);
8790
return {
8891
type: INodeContactType.Raw,

packages/discv5/src/util/error.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export class CodeError extends Error {
2+
code: string;
3+
constructor(message: string, code: string) {
4+
super(message);
5+
this.code = code;
6+
}
7+
}

packages/discv5/src/util/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./timeoutMap.js";
33
export * from "./toBuffer.js";
44
export * from "./crypto.js";
55
export * from "./ip.js";
6+
export * from "./error.js";

packages/discv5/test/e2e/connect.test.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* eslint-env mocha */
22
import { expect } from "chai";
3+
import { privateKeyFromProtobuf } from "@libp2p/crypto/keys";
4+
import { PrivateKey } from "@libp2p/interface";
35
import { multiaddr } from "@multiformats/multiaddr";
4-
import { PeerId } from "@libp2p/interface";
5-
import { createFromPrivKey } from "@libp2p/peer-id-factory";
6-
import { unmarshalPrivateKey } from "@libp2p/crypto/keys";
76
import { SignableENR } from "@chainsafe/enr";
87
import { Discv5 } from "../../src/index.js";
98

@@ -14,23 +13,23 @@ describe("discv5 integration test", function () {
1413
const nodes: Discv5[] = [];
1514

1615
type Node = {
17-
peerId: PeerId;
16+
privateKey: PrivateKey;
1817
enr: SignableENR;
1918
discv5: Discv5;
2019
};
2120
async function getDiscv5Node(): Promise<Node> {
2221
const idx = nodeIdx++;
2322
const port = portBase + idx;
24-
const peerId = await getPeerId(idx);
25-
const enr = SignableENR.createFromPeerId(peerId);
23+
const privateKey = getPrivateKey(idx);
24+
const enr = SignableENR.createFromPrivateKey(privateKey);
2625

2726
const bindAddrUdp = `/ip4/127.0.0.1/udp/${port}`;
2827
const multiAddrUdp = multiaddr(bindAddrUdp);
2928
enr.setLocationMultiaddr(multiAddrUdp);
3029

3130
const discv5 = Discv5.create({
3231
enr,
33-
peerId,
32+
privateKey,
3433
bindAddrs: { ip4: multiAddrUdp },
3534
config: {
3635
lookupTimeout: 2000,
@@ -41,7 +40,7 @@ describe("discv5 integration test", function () {
4140

4241
await discv5.start();
4342

44-
return { peerId, enr, discv5 };
43+
return { privateKey, enr, discv5 };
4544
}
4645

4746
// 2862ae92fa59042fd4d4a3e3bddb92b33a53b72be15deafce07dfbd7c3b12812
@@ -87,7 +86,7 @@ describe("discv5 integration test", function () {
8786
});
8887
});
8988

90-
async function getPeerId(i: number): Promise<PeerId> {
89+
function getPrivateKey(i: number): PrivateKey {
9190
const privKeysBase64 = [
9291
"CAISIF9nhmNn+vOoMPdR+adfKwjSdgqrVGmAX0AWe6Tgjj/p",
9392
"CAISIMSR1N4+3m62NGJ8pdgiUPzFR4vv8pZKG6q+iys+B2DL",
@@ -100,5 +99,5 @@ async function getPeerId(i: number): Promise<PeerId> {
10099
"CAISIHRKcVKLTpKhQOEIPwQjH2xx/nvJWWLUCr90/NOuuZ+l",
101100
"CAISIP3n7vFWZKye7duop0nhfttFJUXTVvQfd4q0dPpURLke",
102101
];
103-
return await createFromPrivKey(await unmarshalPrivateKey(Buffer.from(privKeysBase64[i], "base64")));
102+
return privateKeyFromProtobuf(Buffer.from(privKeysBase64[i], "base64"));
104103
}

0 commit comments

Comments
 (0)