mirror of https://github.com/waku-org/js-waku.git
Move common crypto functions to crypto.ts
This commit is contained in:
parent
e46369d968
commit
20b3b5b667
12
src/index.ts
12
src/index.ts
|
@ -1,3 +1,9 @@
|
|||
export {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "./lib/crypto";
|
||||
|
||||
export { getPredefinedBootstrapNodes } from "./lib/discovery";
|
||||
export * as discovery from "./lib/discovery";
|
||||
|
||||
|
@ -11,12 +17,6 @@ export { Waku, DefaultPubSubTopic, Protocols } from "./lib/waku";
|
|||
export * as waku_message from "./lib/waku_message";
|
||||
export { WakuMessage } from "./lib/waku_message";
|
||||
|
||||
export {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "./lib/waku_message/version_1";
|
||||
|
||||
export * as waku_light_push from "./lib/waku_light_push";
|
||||
export {
|
||||
WakuLightPush,
|
||||
|
|
|
@ -2,6 +2,9 @@ import nodeCrypto from "crypto";
|
|||
|
||||
import * as secp from "@noble/secp256k1";
|
||||
|
||||
import * as symmetric from "./waku_message/symmetric";
|
||||
import { PrivateKeySize } from "./waku_message/version_1";
|
||||
|
||||
declare const self: Record<string, any> | undefined;
|
||||
const crypto: { node?: any; web?: any } = {
|
||||
node: nodeCrypto,
|
||||
|
@ -22,3 +25,25 @@ export function getSubtle(): SubtleCrypto {
|
|||
|
||||
export const randomBytes = secp.utils.randomBytes;
|
||||
export const sha256 = secp.utils.sha256;
|
||||
|
||||
/**
|
||||
* Generate a new private key to be used for asymmetric encryption.
|
||||
*
|
||||
* Use {@link getPublicKey} to get the corresponding Public Key.
|
||||
*/
|
||||
export function generatePrivateKey(): Uint8Array {
|
||||
return randomBytes(PrivateKeySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new symmetric key to be used for symmetric encryption.
|
||||
*/
|
||||
export function generateSymmetricKey(): Uint8Array {
|
||||
return randomBytes(symmetric.KeySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the public key for the given private key, to be used for asymmetric
|
||||
* encryption.
|
||||
*/
|
||||
export const getPublicKey = secp.getPublicKey;
|
||||
|
|
|
@ -9,9 +9,9 @@ import {
|
|||
} from "../test_utils/";
|
||||
import { delay } from "../test_utils/delay";
|
||||
|
||||
import { generateSymmetricKey } from "./crypto";
|
||||
import { Protocols, Waku } from "./waku";
|
||||
import { WakuMessage } from "./waku_message";
|
||||
import { generateSymmetricKey } from "./waku_message/version_1";
|
||||
|
||||
const TestContentTopic = "/test/1/waku/utf8";
|
||||
|
||||
|
|
|
@ -8,14 +8,13 @@ import {
|
|||
WakuRelayMessage,
|
||||
} from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "./version_1";
|
||||
} from "../crypto";
|
||||
import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
|
||||
import { DecryptionMethod, WakuMessage } from "./index";
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { expect } from "chai";
|
||||
import fc from "fast-check";
|
||||
|
||||
import { getPublicKey } from "./version_1";
|
||||
import { getPublicKey } from "../crypto";
|
||||
|
||||
import { WakuMessage } from "./index";
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import { expect } from "chai";
|
||||
import fc from "fast-check";
|
||||
|
||||
import { getPublicKey } from "../crypto";
|
||||
|
||||
import {
|
||||
clearDecode,
|
||||
clearEncode,
|
||||
|
@ -8,7 +10,6 @@ import {
|
|||
decryptSymmetric,
|
||||
encryptAsymmetric,
|
||||
encryptSymmetric,
|
||||
getPublicKey,
|
||||
} from "./version_1";
|
||||
|
||||
describe("Waku Message Version 1", function () {
|
||||
|
|
|
@ -70,7 +70,7 @@ export async function clearEncode(
|
|||
envelope = concat([envelope, bytesSignature, [recid]]);
|
||||
sig = {
|
||||
signature: bytesSignature,
|
||||
publicKey: getPublicKey(sigPrivKey),
|
||||
publicKey: secp.getPublicKey(sigPrivKey, false),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -199,30 +199,6 @@ export async function decryptSymmetric(
|
|||
return symmetric.decrypt(iv, hexToBytes(key), cipher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new private key to be used for asymmetric encryption.
|
||||
*
|
||||
* Use {@link getPublicKey} to get the corresponding Public Key.
|
||||
*/
|
||||
export function generatePrivateKey(): Uint8Array {
|
||||
return randomBytes(PrivateKeySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a new symmetric key to be used for symmetric encryption.
|
||||
*/
|
||||
export function generateSymmetricKey(): Uint8Array {
|
||||
return randomBytes(symmetric.KeySize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the public key for the given private key, to be used for asymmetric
|
||||
* encryption.
|
||||
*/
|
||||
export function getPublicKey(privateKey: Uint8Array): Uint8Array {
|
||||
return secp.getPublicKey(privateKey, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the flags & auxiliary-field as per [26/WAKU-PAYLOAD](https://rfc.vac.dev/spec/26/).
|
||||
*/
|
||||
|
|
|
@ -8,13 +8,13 @@ import {
|
|||
Nwaku,
|
||||
} from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { DefaultPubSubTopic, Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "../waku_message/version_1";
|
||||
} from "../crypto";
|
||||
import { DefaultPubSubTopic, Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
|
||||
const log = debug("waku:test");
|
||||
|
||||
|
|
|
@ -8,13 +8,13 @@ import {
|
|||
Nwaku,
|
||||
} from "../../test_utils";
|
||||
import { delay } from "../../test_utils/delay";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
generateSymmetricKey,
|
||||
getPublicKey,
|
||||
} from "../waku_message/version_1";
|
||||
} from "../crypto";
|
||||
import { Protocols, Waku } from "../waku";
|
||||
import { DecryptionMethod, WakuMessage } from "../waku_message";
|
||||
|
||||
import { PageDirection } from "./history_rpc";
|
||||
|
||||
|
|
Loading…
Reference in New Issue