From 39a1225745ddb63ba3ae6dadb71116df48407444 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Sun, 4 Aug 2024 13:47:03 +0000 Subject: [PATCH] utils: switch deps from stablelib to noble --- package-lock.json | 205 ++++++++++------------------------- packages/utils/package.json | 8 +- packages/utils/src/crypto.ts | 37 ++++--- 3 files changed, 79 insertions(+), 171 deletions(-) diff --git a/package-lock.json b/package-lock.json index 49369419f4..dd2d538b59 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4232,6 +4232,39 @@ "tslib": "^2.3.1" } }, + "node_modules/@noble/ciphers": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-0.5.3.tgz", + "integrity": "sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==", + "license": "MIT", + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@noble/curves": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.4.2.tgz", + "integrity": "sha512-TavHr8qycMChk8UwMld0ZDRvatedkzWfH8IiaeGCfymOP5i0hSCozz9vHOL0nkwk7HRMlFnAiKpS2jrUmSybcw==", + "license": "MIT", + "dependencies": { + "@noble/hashes": "1.4.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@noble/hashes": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", + "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==", + "license": "MIT", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "dev": true, @@ -7195,10 +7228,6 @@ "dev": true, "license": "MIT" }, - "node_modules/@stablelib/aead": { - "version": "1.0.1", - "license": "MIT" - }, "node_modules/@stablelib/binary": { "version": "1.0.1", "license": "MIT", @@ -7206,34 +7235,6 @@ "@stablelib/int": "^1.0.1" } }, - "node_modules/@stablelib/bytes": { - "version": "1.0.1", - "license": "MIT" - }, - "node_modules/@stablelib/chacha": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/binary": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "node_modules/@stablelib/chacha20poly1305": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/aead": "^1.0.1", - "@stablelib/binary": "^1.0.1", - "@stablelib/chacha": "^1.0.1", - "@stablelib/constant-time": "^1.0.1", - "@stablelib/poly1305": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "node_modules/@stablelib/constant-time": { - "version": "1.0.1", - "license": "MIT" - }, "node_modules/@stablelib/ed25519": { "version": "1.0.3", "license": "MIT", @@ -7247,43 +7248,10 @@ "version": "1.0.1", "license": "MIT" }, - "node_modules/@stablelib/hkdf": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/hash": "^1.0.1", - "@stablelib/hmac": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "node_modules/@stablelib/hmac": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/constant-time": "^1.0.1", - "@stablelib/hash": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, "node_modules/@stablelib/int": { "version": "1.0.1", "license": "MIT" }, - "node_modules/@stablelib/keyagreement": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/bytes": "^1.0.1" - } - }, - "node_modules/@stablelib/poly1305": { - "version": "1.0.1", - "license": "MIT", - "dependencies": { - "@stablelib/constant-time": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, "node_modules/@stablelib/random": { "version": "1.0.2", "license": "MIT", @@ -7314,15 +7282,6 @@ "version": "1.0.1", "license": "MIT" }, - "node_modules/@stablelib/x25519": { - "version": "1.0.3", - "license": "MIT", - "dependencies": { - "@stablelib/keyagreement": "^1.0.1", - "@stablelib/random": "^1.0.2", - "@stablelib/wipe": "^1.0.1" - } - }, "node_modules/@szmarczak/http-timer": { "version": "5.0.1", "dev": true, @@ -27580,11 +27539,9 @@ "version": "2.14.0", "license": "Apache-2.0", "dependencies": { - "@stablelib/chacha20poly1305": "1.0.1", - "@stablelib/hkdf": "1.0.1", - "@stablelib/random": "1.0.2", - "@stablelib/sha256": "1.0.1", - "@stablelib/x25519": "1.0.3", + "@noble/ciphers": "0.5.3", + "@noble/curves": "1.4.2", + "@noble/hashes": "1.4.0", "@walletconnect/relay-api": "1.0.10", "@walletconnect/safe-json": "1.0.2", "@walletconnect/time": "1.0.2", @@ -30986,6 +30943,24 @@ "tslib": "^2.3.1" } }, + "@noble/ciphers": { + "version": "0.5.3", + "resolved": "https://registry.npmjs.org/@noble/ciphers/-/ciphers-0.5.3.tgz", + "integrity": "sha512-B0+6IIHiqEs3BPMT0hcRmHvEj2QHOLu+uwt+tqDDeVd0oyVzh7BPrDcPjRnV1PV/5LaknXJJQvOuRGR0zQJz+w==" + }, + "@noble/curves": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/@noble/curves/-/curves-1.4.2.tgz", + "integrity": "sha512-TavHr8qycMChk8UwMld0ZDRvatedkzWfH8IiaeGCfymOP5i0hSCozz9vHOL0nkwk7HRMlFnAiKpS2jrUmSybcw==", + "requires": { + "@noble/hashes": "1.4.0" + } + }, + "@noble/hashes": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", + "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==" + }, "@nodelib/fs.scandir": { "version": "2.1.5", "dev": true, @@ -33185,39 +33160,12 @@ "version": "0.11.1", "dev": true }, - "@stablelib/aead": { - "version": "1.0.1" - }, "@stablelib/binary": { "version": "1.0.1", "requires": { "@stablelib/int": "^1.0.1" } }, - "@stablelib/bytes": { - "version": "1.0.1" - }, - "@stablelib/chacha": { - "version": "1.0.1", - "requires": { - "@stablelib/binary": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "@stablelib/chacha20poly1305": { - "version": "1.0.1", - "requires": { - "@stablelib/aead": "^1.0.1", - "@stablelib/binary": "^1.0.1", - "@stablelib/chacha": "^1.0.1", - "@stablelib/constant-time": "^1.0.1", - "@stablelib/poly1305": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "@stablelib/constant-time": { - "version": "1.0.1" - }, "@stablelib/ed25519": { "version": "1.0.3", "requires": { @@ -33229,38 +33177,9 @@ "@stablelib/hash": { "version": "1.0.1" }, - "@stablelib/hkdf": { - "version": "1.0.1", - "requires": { - "@stablelib/hash": "^1.0.1", - "@stablelib/hmac": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, - "@stablelib/hmac": { - "version": "1.0.1", - "requires": { - "@stablelib/constant-time": "^1.0.1", - "@stablelib/hash": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, "@stablelib/int": { "version": "1.0.1" }, - "@stablelib/keyagreement": { - "version": "1.0.1", - "requires": { - "@stablelib/bytes": "^1.0.1" - } - }, - "@stablelib/poly1305": { - "version": "1.0.1", - "requires": { - "@stablelib/constant-time": "^1.0.1", - "@stablelib/wipe": "^1.0.1" - } - }, "@stablelib/random": { "version": "1.0.2", "requires": { @@ -33287,14 +33206,6 @@ "@stablelib/wipe": { "version": "1.0.1" }, - "@stablelib/x25519": { - "version": "1.0.3", - "requires": { - "@stablelib/keyagreement": "^1.0.1", - "@stablelib/random": "^1.0.2", - "@stablelib/wipe": "^1.0.1" - } - }, "@szmarczak/http-timer": { "version": "5.0.1", "dev": true, @@ -34127,11 +34038,9 @@ "@walletconnect/utils": { "version": "file:packages/utils", "requires": { - "@stablelib/chacha20poly1305": "1.0.1", - "@stablelib/hkdf": "1.0.1", - "@stablelib/random": "1.0.2", - "@stablelib/sha256": "1.0.1", - "@stablelib/x25519": "1.0.3", + "@noble/ciphers": "0.5.3", + "@noble/curves": "1.4.2", + "@noble/hashes": "1.4.0", "@types/lodash.isequal": "4.5.6", "@walletconnect/relay-api": "1.0.10", "@walletconnect/safe-json": "1.0.2", diff --git a/packages/utils/package.json b/packages/utils/package.json index 7dacfe7198..4208e0840e 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -31,11 +31,9 @@ "prettier": "prettier --check '{src,test}/**/*.{js,ts,jsx,tsx}'" }, "dependencies": { - "@stablelib/chacha20poly1305": "1.0.1", - "@stablelib/hkdf": "1.0.1", - "@stablelib/random": "1.0.2", - "@stablelib/sha256": "1.0.1", - "@stablelib/x25519": "1.0.3", + "@noble/ciphers": "0.5.3", + "@noble/curves": "1.4.2", + "@noble/hashes": "1.4.0", "@walletconnect/relay-api": "1.0.10", "@walletconnect/safe-json": "1.0.2", "@walletconnect/time": "1.0.2", diff --git a/packages/utils/src/crypto.ts b/packages/utils/src/crypto.ts index d8ea8999e4..04fd0adcec 100644 --- a/packages/utils/src/crypto.ts +++ b/packages/utils/src/crypto.ts @@ -1,8 +1,8 @@ -import { ChaCha20Poly1305 } from "@stablelib/chacha20poly1305"; -import { HKDF } from "@stablelib/hkdf"; -import { randomBytes } from "@stablelib/random"; -import { hash, SHA256 } from "@stablelib/sha256"; -import * as x25519 from "@stablelib/x25519"; +import { chacha20poly1305 } from "@noble/ciphers/chacha"; +import { hkdf } from "@noble/hashes/hkdf"; +import { randomBytes } from "@noble/hashes/utils"; +import { sha256 } from "@noble/hashes/sha256"; +import { x25519 } from "@noble/curves/ed25519"; import { CryptoTypes } from "@walletconnect/types"; import { concat, fromString, toString } from "uint8arrays"; @@ -20,10 +20,11 @@ const IV_LENGTH = 12; const KEY_LENGTH = 32; export function generateKeyPair(): CryptoTypes.KeyPair { - const keyPair = x25519.generateKeyPair(); + const privateKey = x25519.utils.randomPrivateKey(); + const publicKey = x25519.getPublicKey(privateKey); return { - privateKey: toString(keyPair.secretKey, BASE16), - publicKey: toString(keyPair.publicKey, BASE16), + privateKey: toString(privateKey, BASE16), + publicKey: toString(publicKey, BASE16), }; } @@ -33,23 +34,21 @@ export function generateRandomBytes32(): string { } export function deriveSymKey(privateKeyA: string, publicKeyB: string): string { - const sharedKey = x25519.sharedKey( + const sharedKey = x25519.getSharedSecret( fromString(privateKeyA, BASE16), fromString(publicKeyB, BASE16), - true, ); - const hkdf = new HKDF(SHA256, sharedKey); - const symKey = hkdf.expand(KEY_LENGTH); + const symKey = hkdf(sha256, sharedKey, undefined, undefined, KEY_LENGTH); return toString(symKey, BASE16); } export function hashKey(key: string): string { - const result = hash(fromString(key, BASE16)); + const result = sha256(fromString(key, BASE16)); return toString(result, BASE16); } export function hashMessage(message: string): string { - const result = hash(fromString(message, UTF8)); + const result = sha256(fromString(message, UTF8)); return toString(result, BASE16); } @@ -73,15 +72,17 @@ export function encrypt(params: CryptoTypes.EncryptParams): string { const iv = typeof params.iv !== "undefined" ? fromString(params.iv, BASE16) : randomBytes(IV_LENGTH); - const box = new ChaCha20Poly1305(fromString(params.symKey, BASE16)); - const sealed = box.seal(iv, fromString(params.message, UTF8)); + const key = fromString(params.symKey, BASE16); + const box = chacha20poly1305(key, iv) + const sealed = box.encrypt(fromString(params.message, UTF8)); return serialize({ type, sealed, iv, senderPublicKey }); } export function decrypt(params: CryptoTypes.DecryptParams): string { - const box = new ChaCha20Poly1305(fromString(params.symKey, BASE16)); + const key = fromString(params.symKey, BASE16); const { sealed, iv } = deserialize(params.encoded); - const message = box.open(iv, sealed); + const box = chacha20poly1305(key, iv); + const message = box.decrypt(sealed); if (message === null) throw new Error("Failed to decrypt"); return toString(message, UTF8); }