Consolidate compress public key functions

This commit is contained in:
Franck Royer 2022-05-20 11:42:01 +10:00
parent 479081f611
commit b93c876043
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 11 additions and 33 deletions

View File

@ -72,3 +72,11 @@ export async function sign(
export function keccak256(input: Uint8Array): Uint8Array {
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
}
export function compressPublicKey(publicKey: Uint8Array): Uint8Array {
if (publicKey.length === 64) {
publicKey = concat([[4], publicKey], 65);
}
const point = secp.Point.fromHex(publicKey);
return point.toRawBytes(true);
}

View File

@ -9,6 +9,7 @@ import { fromString } from "uint8arrays/from-string";
import { toString } from "uint8arrays/to-string";
import { encode as varintEncode } from "varint";
import { compressPublicKey } from "../crypto";
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants";
@ -22,7 +23,6 @@ import {
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
import * as v4 from "./v4";
import { compressPublicKey } from "./v4";
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec";
const dbg = debug("waku:enr");

View File

@ -1,34 +1,9 @@
import * as secp from "@noble/secp256k1";
import { concat } from "uint8arrays/concat";
import { randomBytes } from "../../crypto";
import { compressPublicKey, randomBytes } from "../../crypto";
import { IKeypair, IKeypairClass, KeypairType } from "./types";
export function secp256k1PublicKeyToCompressed(
publicKey: Uint8Array
): Uint8Array {
if (publicKey.length === 64) {
publicKey = concat([[4], publicKey], 65);
}
const point = secp.Point.fromHex(publicKey);
return point.toRawBytes(true);
}
export function secp256k1PublicKeyToFull(publicKey: Uint8Array): Uint8Array {
if (publicKey.length === 64) {
publicKey = concat([[4], publicKey], 65);
}
const point = secp.Point.fromHex(publicKey);
return point.toRawBytes(false);
}
export function secp256k1PublicKeyToRaw(publicKey: Uint8Array): Uint8Array {
const point = secp.Point.fromHex(publicKey);
return point.toRawBytes(false).slice(1);
}
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
implements IKeypair
{
@ -39,7 +14,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
let pub = publicKey;
if (pub) {
pub = secp256k1PublicKeyToCompressed(pub);
pub = compressPublicKey(pub);
}
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
throw new Error("Invalid private key");

View File

@ -5,11 +5,6 @@ import { bytesToHex } from "../utils";
import { NodeId } from "./types";
export function compressPublicKey(publicKey: Uint8Array): Uint8Array {
const point = secp.Point.fromHex(bytesToHex(publicKey));
return point.toRawBytes(true);
}
export async function sign(
privKey: Uint8Array,
msg: Uint8Array