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