Skip to content

Commit

Permalink
chore: bump multiaddr dependency to ^11.0.0 (#203)
Browse files Browse the repository at this point in the history
* chore: bump multiaddr dependency to ^11.0.0

* chore: fix multiaddr usage
  • Loading branch information
wemeetagain authored Sep 27, 2022
1 parent 7b651f6 commit 62fe0e2
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 54 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
},
"dependencies": {
"@libp2p/crypto": "^1.0.0",
"@libp2p/interface-peer-discovery": "^1.0.0",
"@libp2p/interface-peer-discovery": "^1.0.1",
"@libp2p/interface-peer-id": "^1.0.2",
"@libp2p/interface-peer-info": "^1.0.1",
"@libp2p/interfaces": "^3.0.2",
"@libp2p/peer-id": "^1.1.13",
"@multiformats/multiaddr": "^10.2.0",
"@multiformats/multiaddr": "^11.0.0",
"base64url": "^3.0.1",
"bcrypto": "^5.4.0",
"bigint-buffer": "^1.1.5",
Expand Down
4 changes: 2 additions & 2 deletions src/enr/enr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Multiaddr, protocols } from "@multiformats/multiaddr";
import { Multiaddr, multiaddr, protocols } from "@multiformats/multiaddr";
import base64url from "base64url";
import { toBigIntBE } from "bigint-buffer";
import * as RLP from "rlp";
Expand Down Expand Up @@ -278,7 +278,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
maBuf.set(protoBuf, 1 + ipByteLen);
maBuf.set(protoVal, 1 + ipByteLen + protoBuf.length);

return new Multiaddr(maBuf);
return multiaddr(maBuf);
}
setLocationMultiaddr(multiaddr: Multiaddr): void {
const protoNames = multiaddr.protoNames();
Expand Down
4 changes: 2 additions & 2 deletions src/libp2p/discv5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PeerId } from "@libp2p/interface-peer-id";
import { PeerDiscovery, PeerDiscoveryEvents, symbol as peerDiscoverySymbol } from "@libp2p/interface-peer-discovery";
import { PeerInfo } from "@libp2p/interface-peer-info";
import { CustomEvent, EventEmitter } from "@libp2p/interfaces/events";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";

import { Discv5, ENRInput, IDiscv5Metrics } from "../service/index.js";
import { ENR } from "../enr/index.js";
Expand Down Expand Up @@ -68,7 +68,7 @@ export class Discv5Discovery extends EventEmitter<PeerDiscoveryEvents> implement
this.discv5 = Discv5.create({
enr: options.enr,
peerId: options.peerId,
multiaddr: new Multiaddr(options.bindAddr),
multiaddr: multiaddr(options.bindAddr),
config: options,
metrics: options.metrics,
});
Expand Down
4 changes: 2 additions & 2 deletions src/message/encode.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as RLP from "rlp";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import isIp from "is-ip";

import {
Expand Down Expand Up @@ -56,7 +56,7 @@ export function encodePingMessage(m: IPingMessage): Buffer {
}

export function encodePongMessage(m: IPongMessage): Buffer {
const ipMultiaddr = new Multiaddr(`/${isIp.v4(m.recipientIp) ? "ip4" : "ip6"}/${m.recipientIp}`);
const ipMultiaddr = multiaddr(`/${isIp.v4(m.recipientIp) ? "ip4" : "ip6"}/${m.recipientIp}`);
const tuple = ipMultiaddr.tuples()[0][1];
if (!tuple) {
throw new Error("invalid address for encoding");
Expand Down
4 changes: 2 additions & 2 deletions src/service/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from "events";
import debug from "debug";
import { randomBytes } from "@libp2p/crypto";
import { Multiaddr } from "@multiformats/multiaddr";
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";
import { PeerId } from "@libp2p/interface-peer-id";

import { ITransportService, UDPTransportService } from "../transport/index.js";
Expand Down Expand Up @@ -838,7 +838,7 @@ export class Discv5 extends (EventEmitter as { new (): Discv5EventEmitter }) {
const currentAddr = this.enr.getLocationMultiaddr("udp");
if (winningVote && (!currentAddr || winningVote.multiaddrStr !== currentAddr.toString())) {
log("Local ENR (IP & UDP) updated: %s", winningVote.multiaddrStr);
const votedAddr = new Multiaddr(winningVote.multiaddrStr);
const votedAddr = multiaddr(winningVote.multiaddrStr);
this.enr.setLocationMultiaddr(votedAddr);
this.emit("multiaddrUpdated", votedAddr);

Expand Down
4 changes: 2 additions & 2 deletions src/session/nodeInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Multiaddr } from "@multiformats/multiaddr";
import { Multiaddr, isMultiaddr } from "@multiformats/multiaddr";
import { peerIdFromString } from "@libp2p/peer-id";
import { createKeypairFromPeerId, IKeypair } from "../keypair/index.js";
import { ENR, NodeId, v4 } from "../enr/index.js";
Expand Down Expand Up @@ -51,7 +51,7 @@ export type NodeContact =
};

export function createNodeContact(input: ENR | Multiaddr): NodeContact {
if (Multiaddr.isMultiaddr(input)) {
if (isMultiaddr(input)) {
const options = input.toOptions();
if (options.transport !== "udp") {
throw new Error("Multiaddr must specify a UDP port");
Expand Down
10 changes: 4 additions & 6 deletions src/transport/udp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dgram from "dgram";
import { EventEmitter } from "events";
import { Multiaddr } from "@multiformats/multiaddr";
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";

import { decodePacket, encodePacket, IPacket, MAX_PACKET_SIZE } from "../packet/index.js";
import { IRemoteInfo, ITransportService, TransportEventEmitter } from "./types.js";
Expand Down Expand Up @@ -50,14 +50,12 @@ export class UDPTransportService
}

public handleIncoming = (data: Buffer, rinfo: IRemoteInfo): void => {
const multiaddr = new Multiaddr(
`/${String(rinfo.family).endsWith("4") ? "ip4" : "ip6"}/${rinfo.address}/udp/${rinfo.port}`
);
const mu = multiaddr(`/${String(rinfo.family).endsWith("4") ? "ip4" : "ip6"}/${rinfo.address}/udp/${rinfo.port}`);
try {
const packet = decodePacket(this.srcId, data);
this.emit("packet", multiaddr, packet);
this.emit("packet", mu, packet);
} catch (e: unknown) {
this.emit("decodeError", e as Error, multiaddr);
this.emit("decodeError", e as Error, mu);
}
};
}
4 changes: 2 additions & 2 deletions test/e2e/connect.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import { PeerId } from "@libp2p/interface-peer-id";
import { createFromPrivKey } from "@libp2p/peer-id-factory";
import { unmarshalPrivateKey } from "@libp2p/crypto/keys";
Expand All @@ -24,7 +24,7 @@ describe("discv5 integration test", function () {
const enr = ENR.createFromPeerId(peerId);

const bindAddrUdp = `/ip4/127.0.0.1/udp/${port}`;
const multiAddrUdp = new Multiaddr(bindAddrUdp);
const multiAddrUdp = multiaddr(bindAddrUdp);
enr.setLocationMultiaddr(multiAddrUdp);

const discv5 = Discv5.create({
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/mainnetBootnodes.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import { createSecp256k1PeerId, createFromPrivKey } from "@libp2p/peer-id-factory";
import { unmarshalPrivateKey } from "@libp2p/crypto/keys";
import { Discv5, ENR } from "../../src/index.js";
Expand All @@ -26,7 +26,7 @@ describe("discv5 integration test", function () {
const enr = ENR.createFromPeerId(peerId);

const bindAddrUdp = `/ip4/0.0.0.0/udp/${port++}`;
const multiAddrUdp = new Multiaddr(bindAddrUdp);
const multiAddrUdp = multiaddr(bindAddrUdp);

const discv5 = Discv5.create({
enr,
Expand Down
30 changes: 15 additions & 15 deletions test/unit/enr.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { ENR, v4 } from "../../src/enr/index.js";

Expand Down Expand Up @@ -48,7 +48,7 @@ describe("ENR Multiformats support", () => {
});

it("should get / set UDP multiaddr", () => {
const multi0 = new Multiaddr("/ip4/127.0.0.1/udp/30303");
const multi0 = multiaddr("/ip4/127.0.0.1/udp/30303");
const tuples0 = multi0.tuples();

if (!tuples0[0][1] || !tuples0[1][1]) {
Expand All @@ -60,7 +60,7 @@ describe("ENR Multiformats support", () => {
// and get the multiaddr
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(multi0.toString());
// set the multiaddr
const multi1 = new Multiaddr("/ip4/0.0.0.0/udp/30300");
const multi1 = multiaddr("/ip4/0.0.0.0/udp/30300");
record.setLocationMultiaddr(multi1);
// and get the multiaddr
expect(record.getLocationMultiaddr("udp")!.toString()).to.equal(multi1.toString());
Expand All @@ -70,7 +70,7 @@ describe("ENR Multiformats support", () => {
expect(record.get("udp")).to.deep.equal(tuples1[1][1]);
});
it("should get / set TCP multiaddr", () => {
const multi0 = new Multiaddr("/ip4/127.0.0.1/tcp/30303");
const multi0 = multiaddr("/ip4/127.0.0.1/tcp/30303");
const tuples0 = multi0.tuples();

if (!tuples0[0][1] || !tuples0[1][1]) {
Expand All @@ -83,7 +83,7 @@ describe("ENR Multiformats support", () => {
// and get the multiaddr
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(multi0.toString());
// set the multiaddr
const multi1 = new Multiaddr("/ip4/0.0.0.0/tcp/30300");
const multi1 = multiaddr("/ip4/0.0.0.0/tcp/30300");
record.setLocationMultiaddr(multi1);
// and get the multiaddr
expect(record.getLocationMultiaddr("tcp")!.toString()).to.equal(multi1.toString());
Expand All @@ -109,44 +109,44 @@ describe("ENR Multiformats support", () => {
enr.udp6 = udp;

it("should properly create location multiaddrs - udp4", () => {
expect(enr.getLocationMultiaddr("udp4")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/udp/${udp}`));
expect(enr.getLocationMultiaddr("udp4")).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
});

it("should properly create location multiaddrs - tcp4", () => {
expect(enr.getLocationMultiaddr("tcp4")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
expect(enr.getLocationMultiaddr("tcp4")).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
});

it("should properly create location multiaddrs - udp6", () => {
expect(enr.getLocationMultiaddr("udp6")).to.deep.equal(new Multiaddr(`/ip6/${ip6}/udp/${udp}`));
expect(enr.getLocationMultiaddr("udp6")).to.deep.equal(multiaddr(`/ip6/${ip6}/udp/${udp}`));
});

it("should properly create location multiaddrs - tcp6", () => {
expect(enr.getLocationMultiaddr("tcp6")).to.deep.equal(new Multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
expect(enr.getLocationMultiaddr("tcp6")).to.deep.equal(multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
});

it("should properly create location multiaddrs - udp", () => {
// default to ip4
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/udp/${udp}`));
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
// if ip6 is set, use it
enr.ip = undefined;
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(new Multiaddr(`/ip6/${ip6}/udp/${udp}`));
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(multiaddr(`/ip6/${ip6}/udp/${udp}`));
// if ip6 does not exist, use ip4
enr.ip6 = undefined;
enr.ip = ip4;
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/udp/${udp}`));
expect(enr.getLocationMultiaddr("udp")).to.deep.equal(multiaddr(`/ip4/${ip4}/udp/${udp}`));
enr.ip6 = ip6;
});

it("should properly create location multiaddrs - tcp", () => {
// default to ip4
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
// if ip6 is set, use it
enr.ip = undefined;
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(new Multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(multiaddr(`/ip6/${ip6}/tcp/${tcp}`));
// if ip6 does not exist, use ip4
enr.ip6 = undefined;
enr.ip = ip4;
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(new Multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
expect(enr.getLocationMultiaddr("tcp")).to.deep.equal(multiaddr(`/ip4/${ip4}/tcp/${tcp}`));
enr.ip6 = ip6;
});
});
Expand Down
10 changes: 5 additions & 5 deletions test/unit/enr/enr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ENR } from "../../../src/enr/enr.js";
import { createKeypairFromPeerId } from "../../../src/keypair/index.js";
import { toHex } from "../../../src/util/index.js";
import { ERR_INVALID_ID } from "../../../src/enr/constants.js";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";

describe("ENR", function () {
Expand All @@ -12,13 +12,13 @@ describe("ENR", function () {
const peerId = await createSecp256k1PeerId();
const enr = ENR.createFromPeerId(peerId);
const keypair = createKeypairFromPeerId(peerId);
enr.setLocationMultiaddr(new Multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
const txt = enr.encodeTxt(keypair.privateKey);
expect(txt.slice(0, 4)).to.be.equal("enr:");
const enr2 = ENR.decodeTxt(txt);
expect(toHex(enr2.signature as Buffer)).to.be.equal(toHex(enr.signature as Buffer));
const multiaddr = enr2.getLocationMultiaddr("udp")!;
expect(multiaddr.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
const mu = enr2.getLocationMultiaddr("udp")!;
expect(mu.toString()).to.be.equal("/ip4/18.223.219.100/udp/9000");
});

it("should decode valid enr successfully", () => {
Expand All @@ -33,7 +33,7 @@ describe("ENR", function () {
it("should encodeTxt and decodeTxt ipv6 enr successfully", async () => {
const peerId = await createSecp256k1PeerId();
const enr = ENR.createFromPeerId(peerId);
enr.setLocationMultiaddr(new Multiaddr("/ip6/aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa/udp/9000"));
enr.setLocationMultiaddr(multiaddr("/ip6/aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa:aaaa/udp/9000"));
const keypair = createKeypairFromPeerId(peerId);
const enr2 = ENR.decodeTxt(enr.encodeTxt(keypair.privateKey));
expect(enr2.udp6).to.equal(9000);
Expand Down
6 changes: 3 additions & 3 deletions test/unit/service/addrVotes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";
import { createNodeId } from "../../../src/enr/index.js";
import { AddrVotes } from "../../../src/service/addrVotes.js";

Expand All @@ -13,7 +13,7 @@ describe("AddrVotes", () => {
it("should return winning vote after 3 same votes", () => {
const recipientIp = "127.0.0.1";
const recipientPort = 30303;
const multi0 = new Multiaddr(`/ip4/${recipientIp}/udp/${recipientPort}`);
const multi0 = multiaddr(`/ip4/${recipientIp}/udp/${recipientPort}`);
const nodeId = createNodeId(Buffer.alloc(32));
const vote = { recipientIp, recipientPort };
expect(addVotes.addVote(nodeId, vote)).to.be.undefined;
Expand All @@ -31,7 +31,7 @@ describe("AddrVotes", () => {
it("1 node adds 2 different vote", () => {
const recipientIp = "127.0.0.1";
const recipientPort = 30303;
const multi0 = new Multiaddr(`/ip4/${recipientIp}/udp/${recipientPort}`);
const multi0 = multiaddr(`/ip4/${recipientIp}/udp/${recipientPort}`);
const nodeId = createNodeId(Buffer.alloc(32));
const vote = { recipientIp, recipientPort };
expect(addVotes.addVote(nodeId, vote)).to.be.undefined;
Expand Down
6 changes: 3 additions & 3 deletions test/unit/service/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { multiaddr } from "@multiformats/multiaddr";

import { Discv5 } from "../../../src/service/service.js";
import { ENR } from "../../../src/enr/index.js";
Expand All @@ -10,7 +10,7 @@ describe("Discv5", async () => {
const kp0 = generateKeypair(KeypairType.Secp256k1);
const peerId0 = await createPeerIdFromKeypair(kp0);
const enr0 = ENR.createV4(kp0.publicKey);
const mu0 = new Multiaddr("/ip4/127.0.0.1/udp/40000");
const mu0 = multiaddr("/ip4/127.0.0.1/udp/40000");

const service0 = Discv5.create({ enr: enr0, peerId: peerId0, multiaddr: mu0 });

Expand Down Expand Up @@ -43,7 +43,7 @@ describe("Discv5", async () => {
const kp1 = generateKeypair(KeypairType.Secp256k1);
const peerId1 = await createPeerIdFromKeypair(kp1);
const enr1 = ENR.createV4(kp1.publicKey);
const mu1 = new Multiaddr("/ip4/127.0.0.1/udp/10360");
const mu1 = multiaddr("/ip4/127.0.0.1/udp/10360");
const addr1 = mu1.tuples();

if (!addr1[0][1] || !addr1[1][1]) {
Expand Down
6 changes: 3 additions & 3 deletions test/unit/session/service.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
import { Multiaddr, multiaddr } from "@multiformats/multiaddr";

import { createKeypair, KeypairType } from "../../../src/keypair/index.js";
import { ENR } from "../../../src/enr/index.js";
Expand All @@ -23,8 +23,8 @@ describe("session service", () => {
Buffer.from("03eae9945b354e9212566bc3f2740f3a62b3e1eb227dbed809f6dc2d3ea848c82e", "hex")
);

const addr0 = new Multiaddr("/ip4/127.0.0.1/udp/49020");
const addr1 = new Multiaddr("/ip4/127.0.0.1/udp/49021");
const addr0 = multiaddr("/ip4/127.0.0.1/udp/49020");
const addr1 = multiaddr("/ip4/127.0.0.1/udp/49021");

const enr0 = ENR.createV4(kp0.publicKey);
const enr1 = ENR.createV4(kp1.publicKey);
Expand Down
6 changes: 3 additions & 3 deletions test/unit/transport/udp.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env mocha */
import { expect } from "chai";
import { Multiaddr } from "@multiformats/multiaddr";
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";
Expand All @@ -10,12 +10,12 @@ describe("UDP transport", () => {
const address = "127.0.0.1";
const nodeIdA = toHex(Buffer.alloc(32, 1));
const portA = 49523;
const multiaddrA = new Multiaddr(`/ip4/${address}/udp/${portA}`);
const multiaddrA = multiaddr(`/ip4/${address}/udp/${portA}`);
const a = new UDPTransportService(multiaddrA, nodeIdA);

const nodeIdB = toHex(Buffer.alloc(32, 2));
const portB = portA + 1;
const multiaddrB = new Multiaddr(`/ip4/${address}/udp/${portB}`);
const multiaddrB = multiaddr(`/ip4/${address}/udp/${portB}`);
const b = new UDPTransportService(multiaddrB, nodeIdB);

before(async () => {
Expand Down

0 comments on commit 62fe0e2

Please sign in to comment.