mirror of https://github.com/status-im/js-waku.git
Merge pull request #725 from status-im/clean-up-crypto
Use `@noble/secp2156k1`'s utils instead of redefining functions
This commit is contained in:
commit
7a9b3bdb5a
|
@ -1,6 +1,6 @@
|
|||
import nodeCrypto from "crypto";
|
||||
|
||||
import { concat } from "uint8arrays/concat";
|
||||
import * as secp from "@noble/secp256k1";
|
||||
|
||||
declare const self: Record<string, any> | undefined;
|
||||
const crypto: { node?: any; web?: any } = {
|
||||
|
@ -20,29 +20,5 @@ export function getSubtle(): SubtleCrypto {
|
|||
}
|
||||
}
|
||||
|
||||
export function randomBytes(bytesLength = 32): Uint8Array {
|
||||
if (crypto.web) {
|
||||
return crypto.web.getRandomValues(new Uint8Array(bytesLength));
|
||||
} else if (crypto.node) {
|
||||
const { randomBytes } = crypto.node;
|
||||
return Uint8Array.from(randomBytes(bytesLength));
|
||||
} else {
|
||||
throw new Error(
|
||||
"The environment doesn't have randomBytes function (if in the browser, be sure to use to be in a secure context, ie, https)"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function sha256(...messages: Uint8Array[]): Promise<Uint8Array> {
|
||||
if (crypto.web) {
|
||||
const buffer = await crypto.web.subtle.digest("SHA-256", concat(messages));
|
||||
return new Uint8Array(buffer);
|
||||
} else if (crypto.node) {
|
||||
const { createHash } = crypto.node;
|
||||
const hash = createHash("sha256");
|
||||
messages.forEach((m) => hash.update(m));
|
||||
return Uint8Array.from(hash.digest());
|
||||
} else {
|
||||
throw new Error("The environment doesn't have sha256 function");
|
||||
}
|
||||
}
|
||||
export const randomBytes = secp.utils.randomBytes;
|
||||
export const sha256 = secp.utils.sha256;
|
||||
|
|
|
@ -53,6 +53,8 @@ describe("Waku Message: Browser & Node", function () {
|
|||
});
|
||||
|
||||
it("Waku message round trip binary encryption [asymmetric, signature]", async function () {
|
||||
this.timeout(4000);
|
||||
|
||||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
|
@ -83,7 +85,7 @@ describe("Waku Message: Browser & Node", function () {
|
|||
await fc.assert(
|
||||
fc.asyncProperty(
|
||||
fc.uint8Array({ minLength: 1 }),
|
||||
fc.uint8Array({ minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, key) => {
|
||||
const msg = await WakuMessage.fromBytes(payload, TestContentTopic, {
|
||||
symKey: key,
|
||||
|
@ -102,8 +104,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({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
fc.uint8Array({ min: 1, minLength: 32, maxLength: 32 }),
|
||||
async (payload, sigPrivKey, symKey) => {
|
||||
const sigPubKey = getPublicKey(sigPrivKey);
|
||||
|
||||
|
|
Loading…
Reference in New Issue