mirror of https://github.com/waku-org/js-waku.git
Remove secp256k1 usage from version_1
This commit is contained in:
parent
12528acaeb
commit
8e6f9e320e
|
@ -56,8 +56,8 @@ describe("Waku Message: Browser & Node", function () {
|
|||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32, min: 1 }),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32, min: 1 }),
|
||||
async (payload, sigPrivKey, encPrivKey) => {
|
||||
const sigPubKey = getPublicKey(sigPrivKey);
|
||||
const encPubKey = getPublicKey(encPrivKey);
|
||||
|
|
|
@ -89,12 +89,12 @@ export class WakuMessage {
|
|||
}
|
||||
|
||||
if (encPublicKey) {
|
||||
const enc = version_1.clearEncode(_payload, sigPrivKey);
|
||||
const enc = await version_1.clearEncode(_payload, sigPrivKey);
|
||||
_payload = await version_1.encryptAsymmetric(enc.payload, encPublicKey);
|
||||
sig = enc.sig;
|
||||
version = 1;
|
||||
} else if (symKey) {
|
||||
const enc = version_1.clearEncode(_payload, sigPrivKey);
|
||||
const enc = await version_1.clearEncode(_payload, sigPrivKey);
|
||||
_payload = await version_1.encryptSymmetric(enc.payload, symKey);
|
||||
sig = enc.sig;
|
||||
version = 1;
|
||||
|
|
|
@ -14,11 +14,11 @@ import {
|
|||
describe("Waku Message Version 1", function () {
|
||||
it("Sign & Recover", function () {
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.asyncProperty(
|
||||
fc.uint8Array(),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
||||
(message, privKey) => {
|
||||
const enc = clearEncode(message, privKey);
|
||||
async (message, privKey) => {
|
||||
const enc = await clearEncode(message, privKey);
|
||||
const res = clearDecode(enc.payload);
|
||||
|
||||
const pubKey = getPublicKey(privKey);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Buffer } from "buffer";
|
||||
|
||||
import * as secp from "@noble/secp256k1";
|
||||
import { keccak256 } from "js-sha3";
|
||||
import * as secp256k1 from "secp256k1";
|
||||
|
||||
import { randomBytes } from "../crypto";
|
||||
import { hexToBytes } from "../utils";
|
||||
|
@ -26,10 +26,10 @@ export const PrivateKeySize = 32;
|
|||
* @returns The encoded payload, ready for encryption using {@link encryptAsymmetric}
|
||||
* or {@link encryptSymmetric}.
|
||||
*/
|
||||
export function clearEncode(
|
||||
export async function clearEncode(
|
||||
messagePayload: Uint8Array,
|
||||
sigPrivKey?: Uint8Array
|
||||
): { payload: Uint8Array; sig?: Signature } {
|
||||
): Promise<{ payload: Uint8Array; sig?: Signature }> {
|
||||
let envelope = Buffer.from([0]); // No flags
|
||||
envelope = addPayloadSizeField(envelope, messagePayload);
|
||||
envelope = Buffer.concat([envelope, Buffer.from(messagePayload)]);
|
||||
|
@ -58,10 +58,17 @@ export function clearEncode(
|
|||
if (sigPrivKey) {
|
||||
envelope[0] |= IsSignedMask;
|
||||
const hash = keccak256(envelope);
|
||||
const s = secp256k1.ecdsaSign(hexToBytes(hash), sigPrivKey);
|
||||
envelope = Buffer.concat([envelope, s.signature, Buffer.from([s.recid])]);
|
||||
const [signature, recid] = await secp.sign(hash, sigPrivKey, {
|
||||
recovered: true,
|
||||
der: false,
|
||||
});
|
||||
envelope = Buffer.concat([
|
||||
envelope,
|
||||
hexToBytes(signature),
|
||||
Buffer.from([recid]),
|
||||
]);
|
||||
sig = {
|
||||
signature: Buffer.from(s.signature),
|
||||
signature: Buffer.from(signature),
|
||||
publicKey: getPublicKey(sigPrivKey),
|
||||
};
|
||||
}
|
||||
|
@ -71,7 +78,7 @@ export function clearEncode(
|
|||
|
||||
export type Signature = {
|
||||
signature: Uint8Array;
|
||||
publicKey: Uint8Array;
|
||||
publicKey: Uint8Array | undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -198,7 +205,7 @@ export function generateSymmetricKey(): Uint8Array {
|
|||
* encryption.
|
||||
*/
|
||||
export function getPublicKey(privateKey: Uint8Array | Buffer): Uint8Array {
|
||||
return secp256k1.publicKeyCreate(privateKey, false);
|
||||
return secp.getPublicKey(privateKey, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -249,12 +256,19 @@ function getHash(message: Buffer, isSigned: boolean): string {
|
|||
return keccak256(message);
|
||||
}
|
||||
|
||||
function ecRecoverPubKey(messageHash: string, signature: Buffer): Uint8Array {
|
||||
function ecRecoverPubKey(
|
||||
messageHash: string,
|
||||
signature: Buffer
|
||||
): Uint8Array | undefined {
|
||||
const recovery = signature.slice(64).readIntBE(0, 1);
|
||||
return secp256k1.ecdsaRecover(
|
||||
signature.slice(0, 64),
|
||||
recovery,
|
||||
const _signature = secp.Signature.fromCompact(signature.slice(0, 64));
|
||||
|
||||
return secp.recoverPublicKey(
|
||||
hexToBytes(messageHash),
|
||||
_signature,
|
||||
recovery,
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore: compressed: false
|
||||
false
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue