mirror of https://github.com/status-im/js-waku.git
chore: remove unused keypair api
This commit is contained in:
parent
46a020c6b4
commit
93ba160791
|
@ -8,7 +8,7 @@ import { equals } from "uint8arrays/equals";
|
|||
import { ERR_INVALID_ID } from "./constants.js";
|
||||
import { getPublicKey } from "./crypto.js";
|
||||
import { ENR } from "./enr.js";
|
||||
import { getPrivateKeyFromPeerId } from "./keypair/index.js";
|
||||
import { getPrivateKeyFromPeerId } from "./peer_id.js";
|
||||
|
||||
describe("ENR", function () {
|
||||
describe("Txt codec", () => {
|
||||
|
|
|
@ -24,15 +24,12 @@ import {
|
|||
MAX_RECORD_SIZE,
|
||||
} from "./constants.js";
|
||||
import { compressPublicKey, keccak256, verifySignature } from "./crypto.js";
|
||||
import {
|
||||
createKeypair,
|
||||
createPeerIdFromPublicKey,
|
||||
getPublicKeyFromPeerId,
|
||||
IKeypair,
|
||||
KeypairType,
|
||||
} from "./keypair/index.js";
|
||||
import { multiaddrFromFields } from "./multiaddr_from_fields.js";
|
||||
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
|
||||
import {
|
||||
createPeerIdFromPublicKey,
|
||||
getPublicKeyFromPeerId,
|
||||
} from "./peer_id.js";
|
||||
import * as v4 from "./v4.js";
|
||||
import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
|
||||
|
||||
|
@ -161,15 +158,6 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
|||
return bytesToUtf8(id);
|
||||
}
|
||||
|
||||
get keypairType(): KeypairType {
|
||||
switch (this.id) {
|
||||
case "v4":
|
||||
return KeypairType.secp256k1;
|
||||
default:
|
||||
throw new Error(ERR_INVALID_ID);
|
||||
}
|
||||
}
|
||||
|
||||
get publicKey(): Uint8Array | undefined {
|
||||
switch (this.id) {
|
||||
case "v4":
|
||||
|
@ -179,14 +167,6 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
|
|||
}
|
||||
}
|
||||
|
||||
get keypair(): IKeypair | undefined {
|
||||
if (this.publicKey) {
|
||||
const publicKey = this.publicKey;
|
||||
return createKeypair(this.keypairType, undefined, publicKey);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
get nodeId(): NodeId | undefined {
|
||||
switch (this.id) {
|
||||
case "v4":
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export * from "./constants.js";
|
||||
export * from "./enr.js";
|
||||
export * from "./keypair/index.js";
|
||||
export * from "./peer_id.js";
|
||||
export * from "./waku2_codec.js";
|
||||
export * from "./crypto.js";
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
import * as secp from "@noble/secp256k1";
|
||||
|
||||
import { compressPublicKey, randomBytes } from "../crypto.js";
|
||||
|
||||
import { IKeypair, KeypairType } from "./types.js";
|
||||
|
||||
export class Secp256k1Keypair implements IKeypair {
|
||||
readonly type: KeypairType;
|
||||
_privateKey?: Uint8Array;
|
||||
readonly _publicKey?: Uint8Array;
|
||||
|
||||
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
||||
let pub = publicKey;
|
||||
if (pub) {
|
||||
pub = compressPublicKey(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;
|
||||
}
|
||||
|
||||
static async generate(): Promise<Secp256k1Keypair> {
|
||||
const privateKey = randomBytes(32);
|
||||
const publicKey = secp.getPublicKey(privateKey);
|
||||
return new Secp256k1Keypair(privateKey, publicKey);
|
||||
}
|
||||
|
||||
privateKeyVerify(key = this._privateKey): boolean {
|
||||
if (key) {
|
||||
return secp.utils.isValidPrivateKey(key);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
publicKeyVerify(key = this._publicKey): boolean {
|
||||
if (key) {
|
||||
try {
|
||||
secp.Point.fromHex(key);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,14 +0,0 @@
|
|||
export enum KeypairType {
|
||||
rsa = 0,
|
||||
ed25519 = 1,
|
||||
secp256k1 = 2,
|
||||
}
|
||||
|
||||
export interface IKeypair {
|
||||
type: KeypairType;
|
||||
privateKey: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
privateKeyVerify(): boolean;
|
||||
publicKeyVerify(): boolean;
|
||||
hasPrivateKey(): boolean;
|
||||
}
|
|
@ -3,13 +3,6 @@ import { supportedKeys } from "@libp2p/crypto/keys";
|
|||
import type { PeerId } from "@libp2p/interface-peer-id";
|
||||
import { peerIdFromKeys } from "@libp2p/peer-id";
|
||||
|
||||
import { Secp256k1Keypair } from "./secp256k1.js";
|
||||
import { IKeypair, KeypairType } from "./types.js";
|
||||
|
||||
export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented";
|
||||
export * from "./types.js";
|
||||
export * from "./secp256k1.js";
|
||||
|
||||
export function createPeerIdFromPublicKey(
|
||||
publicKey: Uint8Array
|
||||
): Promise<PeerId> {
|
||||
|
@ -17,19 +10,6 @@ export function createPeerIdFromPublicKey(
|
|||
return peerIdFromKeys(_publicKey.bytes, undefined);
|
||||
}
|
||||
|
||||
export function createKeypair(
|
||||
type: KeypairType,
|
||||
privateKey?: Uint8Array,
|
||||
publicKey?: Uint8Array
|
||||
): IKeypair {
|
||||
switch (type) {
|
||||
case KeypairType.secp256k1:
|
||||
return new Secp256k1Keypair(privateKey, publicKey);
|
||||
default:
|
||||
throw new Error(ERR_TYPE_NOT_IMPLEMENTED);
|
||||
}
|
||||
}
|
||||
|
||||
export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
|
||||
if (peerId.type !== "secp256k1") {
|
||||
throw new Error("Unsupported peer id type");
|
Loading…
Reference in New Issue