Move ecdsa sign to crypto.ts

This commit is contained in:
Franck Royer 2022-05-20 10:58:58 +10:00
parent b3f4686da1
commit ee8ba791cc
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 23 additions and 16 deletions

View File

@ -2,6 +2,7 @@ import nodeCrypto from "crypto";
import * as secp from "@noble/secp256k1";
import * as sha3 from "js-sha3";
import { concat } from "uint8arrays/concat";
import * as symmetric from "./waku_message/symmetric";
import { PrivateKeySize } from "./waku_message/version_1";
@ -49,6 +50,25 @@ export function generateSymmetricKey(): Uint8Array {
*/
export const getPublicKey = secp.getPublicKey;
/**
* ECDSA Sign a message with the given private key.
*
* @param message The message to sign, usually a hash.
* @param privateKey The ECDSA private key to use to sign the message.
*
* @returns The signature and the recovery id concatenated.
*/
export async function sign(
message: Uint8Array,
privateKey: Uint8Array
): Promise<Uint8Array> {
const [signature, recoveryId] = await secp.sign(message, privateKey, {
recovered: true,
der: false,
});
return concat([signature, [recoveryId]], signature.length + 1);
}
export function keccak256(input: Uint8Array): Uint8Array {
return new Uint8Array(sha3.keccak256.arrayBuffer(input));
}

View File

@ -69,14 +69,6 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair
return true;
}
async sign(msg: Uint8Array): Promise<Uint8Array> {
const [signature, recid] = await secp.sign(msg, this.privateKey, {
recovered: true,
der: false,
});
return concat([signature, [recid]], signature.length + 1);
}
verify(msg: Uint8Array, sig: Uint8Array): boolean {
try {
const _sig = secp.Signature.fromCompact(sig.slice(0, 64));

View File

@ -10,7 +10,6 @@ export interface IKeypair {
publicKey: Uint8Array;
privateKeyVerify(): boolean;
publicKeyVerify(): boolean;
sign(msg: Uint8Array): Promise<Uint8Array>;
verify(msg: Uint8Array, sig: Uint8Array): boolean;
hasPrivateKey(): boolean;
}

View File

@ -1,7 +1,7 @@
import * as secp from "@noble/secp256k1";
import { concat } from "uint8arrays/concat";
import { keccak256, randomBytes } from "../crypto";
import { keccak256, randomBytes, sign } from "../crypto";
import { hexToBytes } from "../utils";
import * as ecies from "./ecies";
@ -61,12 +61,8 @@ export async function clearEncode(
if (sigPrivKey) {
envelope[0] |= IsSignedMask;
const hash = keccak256(envelope);
const [hexSignature, recid] = await secp.sign(hash, sigPrivKey, {
recovered: true,
der: false,
});
const bytesSignature = hexToBytes(hexSignature);
envelope = concat([envelope, bytesSignature, [recid]]);
const bytesSignature = await sign(hash, sigPrivKey);
envelope = concat([envelope, bytesSignature]);
sig = {
signature: bytesSignature,
publicKey: secp.getPublicKey(sigPrivKey, false),