mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-27 11:30:49 +00:00
Remove secp256k1 usage from entree.ts
This commit is contained in:
parent
3f6d9fb590
commit
d3671b7167
@ -1,7 +1,7 @@
|
|||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
|
|
||||||
|
import * as secp from "@noble/secp256k1";
|
||||||
import * as base32 from "hi-base32";
|
import * as base32 from "hi-base32";
|
||||||
import { ecdsaVerify } from "secp256k1";
|
|
||||||
import { fromString } from "uint8arrays/from-string";
|
import { fromString } from "uint8arrays/from-string";
|
||||||
|
|
||||||
import { ENR } from "../enr";
|
import { ENR } from "../enr";
|
||||||
@ -48,11 +48,17 @@ export class ENRTree {
|
|||||||
64
|
64
|
||||||
);
|
);
|
||||||
|
|
||||||
const isVerified = ecdsaVerify(
|
let isVerified;
|
||||||
signatureBuffer,
|
try {
|
||||||
keccak256Buf(signedComponentBuffer),
|
const _sig = secp.Signature.fromCompact(signatureBuffer.slice(0, 64));
|
||||||
new Uint8Array(decodedPublicKey)
|
isVerified = secp.verify(
|
||||||
);
|
_sig,
|
||||||
|
keccak256Buf(signedComponentBuffer),
|
||||||
|
new Uint8Array(decodedPublicKey)
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
isVerified = false;
|
||||||
|
}
|
||||||
|
|
||||||
assert(isVerified, "Unable to verify ENRTree root signature");
|
assert(isVerified, "Unable to verify ENRTree root signature");
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ describe("ENR", function () {
|
|||||||
lightPush: false,
|
lightPush: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const enr2 = ENR.decodeTxt(txt);
|
const enr2 = ENR.decodeTxt(txt);
|
||||||
|
|
||||||
if (!enr.signature) throw "enr.signature is undefined";
|
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.setLocationMultiaddr(new Multiaddr("/ip4/18.223.219.100/udp/9000"));
|
||||||
|
|
||||||
enr.set("id", new Uint8Array([0]));
|
enr.set("id", new Uint8Array([0]));
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
|
|
||||||
ENR.decodeTxt(txt);
|
ENR.decodeTxt(txt);
|
||||||
assert.fail("Expect error here");
|
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;
|
enr.waku2 = waku2Protocols;
|
||||||
|
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(false);
|
expect(decoded.relay).to.equal(false);
|
||||||
@ -411,14 +411,14 @@ describe("ENR", function () {
|
|||||||
expect(decoded.lightPush).to.equal(false);
|
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.relay = true;
|
||||||
waku2Protocols.store = true;
|
waku2Protocols.store = true;
|
||||||
waku2Protocols.filter = true;
|
waku2Protocols.filter = true;
|
||||||
waku2Protocols.lightPush = true;
|
waku2Protocols.lightPush = true;
|
||||||
|
|
||||||
enr.waku2 = waku2Protocols;
|
enr.waku2 = waku2Protocols;
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(true);
|
expect(decoded.relay).to.equal(true);
|
||||||
@ -427,11 +427,11 @@ describe("ENR", function () {
|
|||||||
expect(decoded.lightPush).to.equal(true);
|
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;
|
waku2Protocols.relay = true;
|
||||||
|
|
||||||
enr.waku2 = waku2Protocols;
|
enr.waku2 = waku2Protocols;
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(true);
|
expect(decoded.relay).to.equal(true);
|
||||||
@ -440,11 +440,11 @@ describe("ENR", function () {
|
|||||||
expect(decoded.lightPush).to.equal(false);
|
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;
|
waku2Protocols.store = true;
|
||||||
|
|
||||||
enr.waku2 = waku2Protocols;
|
enr.waku2 = waku2Protocols;
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(false);
|
expect(decoded.relay).to.equal(false);
|
||||||
@ -453,11 +453,11 @@ describe("ENR", function () {
|
|||||||
expect(decoded.lightPush).to.equal(false);
|
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;
|
waku2Protocols.filter = true;
|
||||||
|
|
||||||
enr.waku2 = waku2Protocols;
|
enr.waku2 = waku2Protocols;
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(false);
|
expect(decoded.relay).to.equal(false);
|
||||||
@ -466,11 +466,11 @@ describe("ENR", function () {
|
|||||||
expect(decoded.lightPush).to.equal(false);
|
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;
|
waku2Protocols.lightPush = true;
|
||||||
|
|
||||||
enr.waku2 = waku2Protocols;
|
enr.waku2 = waku2Protocols;
|
||||||
const txt = enr.encodeTxt(keypair.privateKey);
|
const txt = await enr.encodeTxt(keypair.privateKey);
|
||||||
const decoded = ENR.decodeTxt(txt).waku2!;
|
const decoded = ENR.decodeTxt(txt).waku2!;
|
||||||
|
|
||||||
expect(decoded.relay).to.equal(false);
|
expect(decoded.relay).to.equal(false);
|
||||||
|
@ -502,7 +502,7 @@ export class ENR extends Map<ENRKey, ENRValue> {
|
|||||||
return encoded;
|
return encoded;
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeTxt(privateKey?: Uint8Array): string {
|
async encodeTxt(privateKey?: Uint8Array): Promise<string> {
|
||||||
return (
|
return (
|
||||||
ENR.RECORD_PREFIX + toString(await this.encode(privateKey), "base64url")
|
ENR.RECORD_PREFIX + toString(await this.encode(privateKey), "base64url")
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user