Skip to content

Commit

Permalink
Merge pull request #101 from ChainSafe/tuyen/multiaddr-port
Browse files Browse the repository at this point in the history
Fix multiaddr port after decoding enr from a txt
  • Loading branch information
wemeetagain committed Sep 26, 2020
2 parents a4b38f9 + cae04bb commit d81ac3c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ lib
# swp file
*.swp

# ide
.vscode

# others
package-lock.json
5 changes: 3 additions & 2 deletions src/enr/enr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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";

export class ENR extends Map<ENRKey, ENRValue> {
public seq: SequenceNumber;
Expand Down Expand Up @@ -128,7 +129,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
get tcp(): number | undefined {
const raw = this.get("tcp");
if (raw) {
return muConvert.toString(Multiaddr.protocols.names.tcp.code, raw) as number;
return muConvert.toString(Multiaddr.protocols.names.tcp.code, toNewUint8Array(raw)) as number;
} else {
return undefined;
}
Expand All @@ -145,7 +146,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
get udp(): number | undefined {
const raw = this.get("udp");
if (raw) {
return muConvert.toString(Multiaddr.protocols.names.udp.code, raw) as number;
return muConvert.toString(Multiaddr.protocols.names.udp.code, toNewUint8Array(raw)) as number;
} else {
return undefined;
}
Expand Down
6 changes: 6 additions & 0 deletions src/util/toBuffer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function toBuffer(arr: Uint8Array): Buffer {
return Buffer.from(arr.buffer, arr.byteOffset, arr.length);
}

// 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);
return new Uint8Array(arrayBuffer);
}
3 changes: 3 additions & 0 deletions test/enr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ describe("ENR", () => {
});

it("should encode/decode to text encoding", () => {
// spec enr https://eips.ethereum.org/EIPS/eip-778
const testTxt =
"enr:-IS4QHCYrYZbAKWCBRlAy5zzaDZXJBGkcnh4MHcBFZntXNFrdvJjX04jRzjzCBOonrkTfj499SZuOh8R33Ls8RRcy5wBgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQPKY0yuDUmstAHYpMa2_oxVtw0RW_QAdpzBQA8yWM0xOIN1ZHCCdl8";
const decoded = ENR.decodeTxt(testTxt);
expect(decoded.udp).to.be.equal(30303);
expect(decoded.ip).to.be.equal("127.0.0.1");
expect(decoded).to.deep.equal(record);
expect(record.encodeTxt(privateKey)).to.equal(testTxt);
});
Expand Down
4 changes: 4 additions & 0 deletions test/enr/enr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import { ENR } from "../../src/enr/enr";
import { createKeypairFromPeerId } from "../../src/keypair";
import { toHex } from "../../src/util";
import { ERR_INVALID_ID } from "../../src/enr/constants";
import Multiaddr from "multiaddr";

describe("ENR", function () {
describe("decodeTxt", () => {
it("should encodeTxt and decodeTxt", async () => {
const peerId = await PeerId.create({ keyType: "secp256k1" });
const enr = ENR.createFromPeerId(peerId);
const keypair = createKeypairFromPeerId(peerId);
enr.setLocationMultiaddr(new 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");
});

it("should decode valid enr successfully", () => {
Expand Down

0 comments on commit d81ac3c

Please sign in to comment.