chore: split function as one part was only used in test

This commit is contained in:
fryorcraken.eth 2023-03-03 13:06:24 +11:00
parent 05b122e646
commit 46a020c6b4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 36 additions and 46 deletions

View File

@ -8,14 +8,14 @@ import { equals } from "uint8arrays/equals";
import { ERR_INVALID_ID } from "./constants.js"; import { ERR_INVALID_ID } from "./constants.js";
import { getPublicKey } from "./crypto.js"; import { getPublicKey } from "./crypto.js";
import { ENR } from "./enr.js"; import { ENR } from "./enr.js";
import { createKeypairFromPeerId, IKeypair } from "./keypair/index.js"; import { getPrivateKeyFromPeerId } from "./keypair/index.js";
describe("ENR", function () { describe("ENR", function () {
describe("Txt codec", () => { describe("Txt codec", () => {
it("should encodeTxt and decodeTxt", async () => { it("should encodeTxt and decodeTxt", async () => {
const peerId = await createSecp256k1PeerId(); const peerId = await createSecp256k1PeerId();
const enr = await ENR.createFromPeerId(peerId); const enr = await ENR.createFromPeerId(peerId);
const keypair = await createKeypairFromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.multiaddrs = [ enr.multiaddrs = [
multiaddr("/dns4/node1.do-ams.wakuv2.test.statusim.net/tcp/443/wss"), multiaddr("/dns4/node1.do-ams.wakuv2.test.statusim.net/tcp/443/wss"),
@ -32,7 +32,7 @@ describe("ENR", function () {
lightPush: false, lightPush: false,
}; };
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const enr2 = await ENR.decodeTxt(txt); const enr2 = await ENR.decodeTxt(txt);
if (!enr.signature) throw "enr.signature is undefined"; if (!enr.signature) throw "enr.signature is undefined";
@ -107,11 +107,11 @@ describe("ENR", function () {
try { try {
const peerId = await createSecp256k1PeerId(); const peerId = await createSecp256k1PeerId();
const enr = await ENR.createFromPeerId(peerId); const enr = await ENR.createFromPeerId(peerId);
const keypair = await createKeypairFromPeerId(peerId); const privateKey = await getPrivateKeyFromPeerId(peerId);
enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000")); enr.setLocationMultiaddr(multiaddr("/ip4/18.223.219.100/udp/9000"));
enr.set("id", new Uint8Array([0])); enr.set("id", new Uint8Array([0]));
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
await ENR.decodeTxt(txt); await ENR.decodeTxt(txt);
assert.fail("Expect error here"); assert.fail("Expect error here");
@ -384,12 +384,12 @@ describe("ENR", function () {
let peerId; let peerId;
let enr: ENR; let enr: ENR;
let waku2Protocols: Waku2; let waku2Protocols: Waku2;
let keypair: IKeypair; let privateKey: Uint8Array;
beforeEach(async function () { beforeEach(async function () {
peerId = await createSecp256k1PeerId(); peerId = await createSecp256k1PeerId();
enr = await ENR.createFromPeerId(peerId); enr = await ENR.createFromPeerId(peerId);
keypair = await createKeypairFromPeerId(peerId); privateKey = await getPrivateKeyFromPeerId(peerId);
waku2Protocols = { waku2Protocols = {
relay: false, relay: false,
store: false, store: false,
@ -401,7 +401,7 @@ describe("ENR", function () {
it("should set field with all protocols disabled", async () => { it("should set field with all protocols disabled", async () => {
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(false); expect(decoded.relay).to.equal(false);
@ -417,7 +417,7 @@ describe("ENR", function () {
waku2Protocols.lightPush = true; waku2Protocols.lightPush = true;
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(true); expect(decoded.relay).to.equal(true);
@ -430,7 +430,7 @@ describe("ENR", function () {
waku2Protocols.relay = true; waku2Protocols.relay = true;
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(true); expect(decoded.relay).to.equal(true);
@ -443,7 +443,7 @@ describe("ENR", function () {
waku2Protocols.store = true; waku2Protocols.store = true;
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(false); expect(decoded.relay).to.equal(false);
@ -456,7 +456,7 @@ describe("ENR", function () {
waku2Protocols.filter = true; waku2Protocols.filter = true;
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(false); expect(decoded.relay).to.equal(false);
@ -469,7 +469,7 @@ describe("ENR", function () {
waku2Protocols.lightPush = true; waku2Protocols.lightPush = true;
enr.waku2 = waku2Protocols; enr.waku2 = waku2Protocols;
const txt = await enr.encodeTxt(keypair.privateKey); const txt = await enr.encodeTxt(privateKey);
const decoded = (await ENR.decodeTxt(txt)).waku2!; const decoded = (await ENR.decodeTxt(txt)).waku2!;
expect(decoded.relay).to.equal(false); expect(decoded.relay).to.equal(false);

View File

@ -26,8 +26,8 @@ import {
import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; import { compressPublicKey, keccak256, verifySignature } from "./crypto.js";
import { import {
createKeypair, createKeypair,
createKeypairFromPeerId,
createPeerIdFromPublicKey, createPeerIdFromPublicKey,
getPublicKeyFromPeerId,
IKeypair, IKeypair,
KeypairType, KeypairType,
} from "./keypair/index.js"; } from "./keypair/index.js";
@ -91,10 +91,9 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
peerId: PeerId, peerId: PeerId,
kvs: Record<ENRKey, ENRValue> = {} kvs: Record<ENRKey, ENRValue> = {}
): Promise<ENR> { ): Promise<ENR> {
const keypair = await createKeypairFromPeerId(peerId); switch (peerId.type) {
switch (keypair.type) { case "secp256k1":
case KeypairType.secp256k1: return ENR.createV4(getPublicKeyFromPeerId(peerId), kvs);
return ENR.createV4(keypair.publicKey, kvs);
default: default:
throw new Error(); throw new Error();
} }

View File

@ -30,34 +30,25 @@ export function createKeypair(
} }
} }
export async function createKeypairFromPeerId( export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
peerId: PeerId if (peerId.type !== "secp256k1") {
): Promise<IKeypair> { throw new Error("Unsupported peer id type");
let keypairType;
switch (peerId.type) {
case "RSA":
keypairType = KeypairType.rsa;
break;
case "Ed25519":
keypairType = KeypairType.ed25519;
break;
case "secp256k1":
keypairType = KeypairType.secp256k1;
break;
default:
throw new Error("Unsupported peer id type");
} }
const publicKey = peerId.publicKey return unmarshalPublicKey(peerId.publicKey).marshal();
? unmarshalPublicKey(peerId.publicKey) }
: undefined;
const privateKey = peerId.privateKey // Only used in tests
? await unmarshalPrivateKey(peerId.privateKey) export async function getPrivateKeyFromPeerId(
: undefined; peerId: PeerId
): Promise<Uint8Array> {
return createKeypair( if (peerId.type !== "secp256k1") {
keypairType, throw new Error("Unsupported peer id type");
privateKey?.marshal(), }
publicKey?.marshal() if (!peerId.privateKey) {
); throw new Error("Private key not present on peer id");
}
const privateKey = await unmarshalPrivateKey(peerId.privateKey);
return privateKey.marshal();
} }