mirror of https://github.com/waku-org/js-waku.git
Consolidate compress public key functions
This commit is contained in:
parent
479081f611
commit
b93c876043
|
@ -72,3 +72,11 @@ export async function sign(
|
||||||
export function keccak256(input: Uint8Array): Uint8Array {
|
export function keccak256(input: Uint8Array): Uint8Array {
|
||||||
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import { fromString } from "uint8arrays/from-string";
|
||||||
import { toString } from "uint8arrays/to-string";
|
import { toString } from "uint8arrays/to-string";
|
||||||
import { encode as varintEncode } from "varint";
|
import { encode as varintEncode } from "varint";
|
||||||
|
|
||||||
|
import { compressPublicKey } from "../crypto";
|
||||||
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
||||||
|
|
||||||
import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants";
|
import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants";
|
||||||
|
@ -22,7 +23,6 @@ import {
|
||||||
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
|
import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec";
|
||||||
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
|
import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types";
|
||||||
import * as v4 from "./v4";
|
import * as v4 from "./v4";
|
||||||
import { compressPublicKey } from "./v4";
|
|
||||||
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec";
|
import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec";
|
||||||
|
|
||||||
const dbg = debug("waku:enr");
|
const dbg = debug("waku:enr");
|
||||||
|
|
|
@ -1,34 +1,9 @@
|
||||||
import * as secp from "@noble/secp256k1";
|
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";
|
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
|
export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
implements IKeypair
|
implements IKeypair
|
||||||
{
|
{
|
||||||
|
@ -39,7 +14,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
|
||||||
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) {
|
||||||
let pub = publicKey;
|
let pub = publicKey;
|
||||||
if (pub) {
|
if (pub) {
|
||||||
pub = secp256k1PublicKeyToCompressed(pub);
|
pub = compressPublicKey(pub);
|
||||||
}
|
}
|
||||||
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
|
if ((this._privateKey = privateKey) && !this.privateKeyVerify()) {
|
||||||
throw new Error("Invalid private key");
|
throw new Error("Invalid private key");
|
||||||
|
|
|
@ -5,11 +5,6 @@ import { bytesToHex } from "../utils";
|
||||||
|
|
||||||
import { NodeId } from "./types";
|
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(
|
export async function sign(
|
||||||
privKey: Uint8Array,
|
privKey: Uint8Array,
|
||||||
msg: Uint8Array
|
msg: Uint8Array
|
||||||
|
|
Loading…
Reference in New Issue