Skip to content

Commit

Permalink
Remove secp256k1 usage from entree.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
D4nte committed May 9, 2022
1 parent a1c970f commit b148605
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
18 changes: 12 additions & 6 deletions src/lib/discovery/enrtree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from "assert";

import * as secp from "@noble/secp256k1";
import * as base32 from "hi-base32";
import { ecdsaVerify } from "secp256k1";
import { fromString } from "uint8arrays/from-string";

import { ENR } from "../enr";
Expand Down Expand Up @@ -48,11 +48,17 @@ export class ENRTree {
64
);

const isVerified = ecdsaVerify(
signatureBuffer,
keccak256Buf(signedComponentBuffer),
new Uint8Array(decodedPublicKey)
);
let isVerified;
try {
const _sig = secp.Signature.fromCompact(signatureBuffer.slice(0, 64));
isVerified = secp.verify(
_sig,
keccak256Buf(signedComponentBuffer),
new Uint8Array(decodedPublicKey)
);
} catch {
isVerified = false;
}

assert(isVerified, "Unable to verify ENRTree root signature");

Expand Down
28 changes: 14 additions & 14 deletions src/lib/enr/enr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("ENR", function () {
lightPush: false,
};

const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const enr2 = ENR.decodeTxt(txt);

if (!enr.signature) throw "enr.signature is undefined";
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("ENR", function () {
enr.setLocationMultiaddr(new Multiaddr("/ip4/18.223.219.100/udp/9000"));

enr.set("id", new Uint8Array([0]));
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);

ENR.decodeTxt(txt);
assert.fail("Expect error here");
Expand Down Expand Up @@ -397,10 +397,10 @@ describe("ENR", function () {
};
});

it("should set field with all protocols disabled", () => {
it("should set field with all protocols disabled", async () => {
enr.waku2 = waku2Protocols;

const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(false);
Expand All @@ -409,14 +409,14 @@ describe("ENR", function () {
expect(decoded.lightPush).to.equal(false);
});

it("should set field with all protocols enabled", () => {
it("should set field with all protocols enabled", async () => {
waku2Protocols.relay = true;
waku2Protocols.store = true;
waku2Protocols.filter = true;
waku2Protocols.lightPush = true;

enr.waku2 = waku2Protocols;
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(true);
Expand All @@ -425,11 +425,11 @@ describe("ENR", function () {
expect(decoded.lightPush).to.equal(true);
});

it("should set field with only RELAY enabled", () => {
it("should set field with only RELAY enabled", async () => {
waku2Protocols.relay = true;

enr.waku2 = waku2Protocols;
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(true);
Expand All @@ -438,11 +438,11 @@ describe("ENR", function () {
expect(decoded.lightPush).to.equal(false);
});

it("should set field with only STORE enabled", () => {
it("should set field with only STORE enabled", async () => {
waku2Protocols.store = true;

enr.waku2 = waku2Protocols;
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(false);
Expand All @@ -451,11 +451,11 @@ describe("ENR", function () {
expect(decoded.lightPush).to.equal(false);
});

it("should set field with only FILTER enabled", () => {
it("should set field with only FILTER enabled", async () => {
waku2Protocols.filter = true;

enr.waku2 = waku2Protocols;
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(false);
Expand All @@ -464,11 +464,11 @@ describe("ENR", function () {
expect(decoded.lightPush).to.equal(false);
});

it("should set field with only LIGHTPUSH enabled", () => {
it("should set field with only LIGHTPUSH enabled", async () => {
waku2Protocols.lightPush = true;

enr.waku2 = waku2Protocols;
const txt = enr.encodeTxt(keypair.privateKey);
const txt = await enr.encodeTxt(keypair.privateKey);
const decoded = ENR.decodeTxt(txt).waku2!;

expect(decoded.relay).to.equal(false);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/enr/enr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
return encoded;
}

encodeTxt(privateKey?: Uint8Array): string {
async encodeTxt(privateKey?: Uint8Array): Promise<string> {
return (
ENR.RECORD_PREFIX + toString(await this.encode(privateKey), "base64url")
);
Expand Down

0 comments on commit b148605

Please sign in to comment.