diff --git a/src/index.ts b/src/index.ts index 85052167d2..443b9f40b4 100644 --- a/src/index.ts +++ b/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, diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index b563f1abd2..cc28642942 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -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 | 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; diff --git a/src/lib/waku.node.spec.ts b/src/lib/waku.node.spec.ts index 81f3b25d53..d854c634d4 100644 --- a/src/lib/waku.node.spec.ts +++ b/src/lib/waku.node.spec.ts @@ -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"; diff --git a/src/lib/waku_message/index.node.spec.ts b/src/lib/waku_message/index.node.spec.ts index 59a115d9de..e3e0283924 100644 --- a/src/lib/waku_message/index.node.spec.ts +++ b/src/lib/waku_message/index.node.spec.ts @@ -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"; diff --git a/src/lib/waku_message/index.spec.ts b/src/lib/waku_message/index.spec.ts index df9073398a..bd5297f560 100644 --- a/src/lib/waku_message/index.spec.ts +++ b/src/lib/waku_message/index.spec.ts @@ -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"; diff --git a/src/lib/waku_message/version_1.spec.ts b/src/lib/waku_message/version_1.spec.ts index e9553a37a8..7351895a0b 100644 --- a/src/lib/waku_message/version_1.spec.ts +++ b/src/lib/waku_message/version_1.spec.ts @@ -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 () { diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index faddc2c4de..0b410db4e6 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -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/). */ diff --git a/src/lib/waku_relay/index.node.spec.ts b/src/lib/waku_relay/index.node.spec.ts index c6b0b3a571..a78bfb4a65 100644 --- a/src/lib/waku_relay/index.node.spec.ts +++ b/src/lib/waku_relay/index.node.spec.ts @@ -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"); diff --git a/src/lib/waku_store/index.node.spec.ts b/src/lib/waku_store/index.node.spec.ts index 0d907ad5f2..187ebdde7d 100644 --- a/src/lib/waku_store/index.node.spec.ts +++ b/src/lib/waku_store/index.node.spec.ts @@ -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";