diff --git a/src/lib/enr/keypair/index.spec.ts b/src/lib/enr/keypair/index.spec.ts index 061e4716ce..a18c2a9661 100644 --- a/src/lib/enr/keypair/index.spec.ts +++ b/src/lib/enr/keypair/index.spec.ts @@ -2,12 +2,7 @@ import { expect } from "chai"; import { keys } from "libp2p-crypto"; import PeerId from "peer-id"; -import { - AbstractKeypair, - createPeerIdFromKeypair, - generateKeypair, - KeypairType, -} from "./index"; +import { createPeerIdFromKeypair, generateKeypair, KeypairType } from "./index"; const { supportedKeys } = keys; @@ -27,7 +22,7 @@ describe("createPeerIdFromKeypair", function () { it("should properly create a PeerId from a secp256k1 keypair without private key", async function () { const keypair = await generateKeypair(KeypairType.secp256k1); - delete (keypair as AbstractKeypair)._privateKey; + delete (keypair as any)._privateKey; const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey( keypair.publicKey ); diff --git a/src/lib/enr/keypair/secp256k1.ts b/src/lib/enr/keypair/secp256k1.ts index 781af5db69..7abef54d98 100644 --- a/src/lib/enr/keypair/secp256k1.ts +++ b/src/lib/enr/keypair/secp256k1.ts @@ -3,7 +3,7 @@ import { concat } from "uint8arrays/concat"; import { randomBytes } from "../../crypto"; -import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types"; +import { IKeypair, IKeypairClass, KeypairType } from "./types"; export function secp256k1PublicKeyToCompressed( publicKey: Uint8Array @@ -30,17 +30,24 @@ export function secp256k1PublicKeyToRaw(publicKey: Uint8Array): Uint8Array { } export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair - extends AbstractKeypair implements IKeypair { readonly type: KeypairType; + _privateKey?: Uint8Array; + readonly _publicKey?: Uint8Array; constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) { let pub = publicKey; if (pub) { pub = secp256k1PublicKeyToCompressed(pub); } - super(privateKey, pub); + if ((this._privateKey = privateKey) && !this.privateKeyVerify()) { + throw new Error("Invalid private key"); + } + if ((this._publicKey = pub) && !this.publicKeyVerify()) { + throw new Error("Invalid public key"); + } + this.type = KeypairType.secp256k1; } @@ -77,4 +84,22 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair return false; } } + + get privateKey(): Uint8Array { + if (!this._privateKey) { + throw new Error(); + } + return this._privateKey; + } + + get publicKey(): Uint8Array { + if (!this._publicKey) { + throw new Error(); + } + return this._publicKey; + } + + hasPrivateKey(): boolean { + return !!this._privateKey; + } }; diff --git a/src/lib/enr/keypair/types.ts b/src/lib/enr/keypair/types.ts index f6f13b3656..00e0696981 100644 --- a/src/lib/enr/keypair/types.ts +++ b/src/lib/enr/keypair/types.ts @@ -18,43 +18,3 @@ export interface IKeypairClass { new (privateKey?: Uint8Array, publicKey?: Uint8Array): IKeypair; generate(): Promise; } - -export abstract class AbstractKeypair { - _privateKey?: Uint8Array; - readonly _publicKey?: Uint8Array; - - constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) { - if ((this._privateKey = privateKey) && !this.privateKeyVerify()) { - throw new Error("Invalid private key"); - } - if ((this._publicKey = publicKey) && !this.publicKeyVerify()) { - throw new Error("Invalid public key"); - } - } - - get privateKey(): Uint8Array { - if (!this._privateKey) { - throw new Error(); - } - return this._privateKey; - } - - get publicKey(): Uint8Array { - if (!this._publicKey) { - throw new Error(); - } - return this._publicKey; - } - - privateKeyVerify(): boolean { - return true; - } - - publicKeyVerify(): boolean { - return true; - } - - hasPrivateKey(): boolean { - return Boolean(this._privateKey); - } -}