Remove secp256k1 usage from entree.ts

This commit is contained in:
Franck Royer 2022-03-07 13:50:18 +11:00
parent 3f6d9fb590
commit d3671b7167
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 27 additions and 21 deletions

View File

@ -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";
@ -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");

View File

@ -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";
@ -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");
@ -399,10 +399,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);
@ -411,14 +411,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);
@ -427,11 +427,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);
@ -440,11 +440,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);
@ -453,11 +453,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);
@ -466,11 +466,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);

View File

@ -502,7 +502,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")
);