mirror of https://github.com/waku-org/js-waku.git
Remove unnecessary abstract class
This commit is contained in:
parent
5e1cd78288
commit
479081f611
|
@ -2,12 +2,7 @@ import { expect } from "chai";
|
||||||
import { keys } from "libp2p-crypto";
|
import { keys } from "libp2p-crypto";
|
||||||
import PeerId from "peer-id";
|
import PeerId from "peer-id";
|
||||||
|
|
||||||
import {
|
import { createPeerIdFromKeypair, generateKeypair, KeypairType } from "./index";
|
||||||
AbstractKeypair,
|
|
||||||
createPeerIdFromKeypair,
|
|
||||||
generateKeypair,
|
|
||||||
KeypairType,
|
|
||||||
} from "./index";
|
|
||||||
|
|
||||||
const { supportedKeys } = keys;
|
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 () {
|
it("should properly create a PeerId from a secp256k1 keypair without private key", async function () {
|
||||||
const keypair = await generateKeypair(KeypairType.secp256k1);
|
const keypair = await generateKeypair(KeypairType.secp256k1);
|
||||||
delete (keypair as AbstractKeypair)._privateKey;
|
delete (keypair as any)._privateKey;
|
||||||
const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey(
|
const pubKey = new supportedKeys.secp256k1.Secp256k1PublicKey(
|
||||||
keypair.publicKey
|
keypair.publicKey
|
||||||
);
|
);
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { concat } from "uint8arrays/concat";
|
||||||
|
|
||||||
import { randomBytes } from "../../crypto";
|
import { randomBytes } from "../../crypto";
|
||||||
|
|
||||||
import { AbstractKeypair, IKeypair, IKeypairClass, KeypairType } from "./types";
|
import { IKeypair, IKeypairClass, KeypairType } from "./types";
|
||||||
|
|
||||||
export function secp256k1PublicKeyToCompressed(
|
export function secp256k1PublicKeyToCompressed(
|
||||||
publicKey: Uint8Array
|
publicKey: Uint8Array
|
||||||
|
@ -30,17 +30,24 @@ export function secp256k1PublicKeyToRaw(publicKey: Uint8Array): Uint8Array {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
extends AbstractKeypair
|
|
||||||
implements IKeypair
|
implements IKeypair
|
||||||
{
|
{
|
||||||
readonly type: KeypairType;
|
readonly type: KeypairType;
|
||||||
|
_privateKey?: Uint8Array;
|
||||||
|
readonly _publicKey?: Uint8Array;
|
||||||
|
|
||||||
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
||||||
let pub = publicKey;
|
let pub = publicKey;
|
||||||
if (pub) {
|
if (pub) {
|
||||||
pub = secp256k1PublicKeyToCompressed(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;
|
this.type = KeypairType.secp256k1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,4 +84,22 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
return false;
|
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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -18,43 +18,3 @@ export interface IKeypairClass {
|
||||||
new (privateKey?: Uint8Array, publicKey?: Uint8Array): IKeypair;
|
new (privateKey?: Uint8Array, publicKey?: Uint8Array): IKeypair;
|
||||||
generate(): Promise<IKeypair>;
|
generate(): Promise<IKeypair>;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue