From 6e5dd44425f0fadce9d18eb9d3113c93f22542a5 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Sun, 27 Oct 2024 01:45:53 +0400 Subject: [PATCH 1/2] feat: use exodus/crypto instead of sodium where possible --- features/keychain/module/crypto/ed25519.js | 11 +++-------- features/keychain/module/crypto/sodium.js | 22 ++++++++++------------ features/keychain/package.json | 2 +- yarn.lock | 18 +++++++++++++----- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/features/keychain/module/crypto/ed25519.js b/features/keychain/module/crypto/ed25519.js index 3ec816a1..235d7d70 100644 --- a/features/keychain/module/crypto/ed25519.js +++ b/features/keychain/module/crypto/ed25519.js @@ -1,18 +1,13 @@ -import sodium from '@exodus/sodium-crypto' +import { signDetached } from '@exodus/crypto/curve25519' import { mapValues } from '@exodus/basic-utils' import assert from 'minimalistic-assert' export const create = ({ getPrivateHDKey }) => { - const getSodiumKeysFromIdentifier = async ({ seedId, keyId }) => { - const { privateKey: sodiumSeed } = getPrivateHDKey({ seedId, keyId }) - return sodium.getSodiumKeysFromSeed(sodiumSeed) - } - const createInstance = () => ({ signBuffer: async ({ seedId, keyId, data }) => { assert(keyId.keyType === 'nacl', `ED25519 signatures are not supported for ${keyId.keyType}`) - const { sign } = await getSodiumKeysFromIdentifier({ seedId, keyId }) - return sodium.signDetached({ message: data, privateKey: sign.privateKey }) + const { privateKey } = getPrivateHDKey({ seedId, keyId }) + return signDetached({ message: data, privateKey, format: 'buffer' }) }, }) diff --git a/features/keychain/module/crypto/sodium.js b/features/keychain/module/crypto/sodium.js index 374687bf..4dfe4866 100644 --- a/features/keychain/module/crypto/sodium.js +++ b/features/keychain/module/crypto/sodium.js @@ -1,5 +1,6 @@ import sodium from '@exodus/sodium-crypto' import { mapValues } from '@exodus/basic-utils' +import * as curve25519 from '@exodus/crypto/curve25519' const cloneBuffer = (buf) => { const newBuffer = Buffer.alloc(buf.length) @@ -38,20 +39,20 @@ export const create = ({ getPrivateHDKey }) => { /** @deprecated use getKeysFromSeed instead */ getSodiumKeysFromSeed: getKeysFromSeed, sign: async ({ seedId, keyId, data }) => { - const { sign } = await getSodiumKeysFromIdentifier({ seedId, keyId }) - return sodium.sign({ message: data, privateKey: sign.privateKey }) + const { privateKey } = getPrivateHDKey({ seedId, keyId }) + return curve25519.signAttached({ message: data, privateKey, format: 'buffer' }) }, signOpen: async ({ seedId, keyId, data }) => { - const { sign } = await getSodiumKeysFromIdentifier({ seedId, keyId }) - return sodium.signOpen({ signed: data, publicKey: sign.publicKey }) + const { publicKey } = getPrivateHDKey({ seedId, keyId }) + return curve25519.signOpen({ signed: data, publicKey, format: 'buffer' }) }, signDetached: async ({ seedId, keyId, data }) => { - const { sign } = await getSodiumKeysFromIdentifier({ seedId, keyId }) - return sodium.signDetached({ message: data, privateKey: sign.privateKey }) + const { privateKey } = getPrivateHDKey({ seedId, keyId }) + return curve25519.signDetached({ message: data, privateKey, format: 'buffer' }) }, verifyDetached: async ({ seedId, keyId, data, signature }) => { - const { sign } = await getSodiumKeysFromIdentifier({ seedId, keyId }) - return sodium.verifyDetached({ message: data, sig: signature, publicKey: sign.publicKey }) + const { publicKey } = getPrivateHDKey({ seedId, keyId }) + return curve25519.verifyDetached({ message: data, signature, publicKey }) }, encryptSecretBox: async ({ seedId, keyId, data }) => { const { privateKey: sodiumSeed } = getPrivateHDKey({ seedId, keyId }) @@ -90,8 +91,5 @@ export const create = ({ getPrivateHDKey }) => { } export const privToPub = async (sodiumSeed) => { - const { - sign: { publicKey }, - } = await sodium.getSodiumKeysFromSeed(sodiumSeed) - return Buffer.from(publicKey) + return curve25519.edwardsToPublic({ privateKey: sodiumSeed, format: 'buffer' }) } diff --git a/features/keychain/package.json b/features/keychain/package.json index c3334e4c..0921015a 100644 --- a/features/keychain/package.json +++ b/features/keychain/package.json @@ -32,7 +32,7 @@ "dependencies": { "@exodus/basic-utils": "^3.0.1", "@exodus/bip32": "^3.3.0", - "@exodus/crypto": "^1.0.0-rc.13", + "@exodus/crypto": "^1.0.0-rc.16", "@exodus/key-identifier": "^1.3.0", "@exodus/key-utils": "^3.7.0", "@exodus/slip10": "^2.1.0", diff --git a/yarn.lock b/yarn.lock index b67881df..15dd5b67 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2357,13 +2357,14 @@ __metadata: languageName: node linkType: hard -"@exodus/crypto@npm:^1.0.0-rc.11, @exodus/crypto@npm:^1.0.0-rc.13": - version: 1.0.0-rc.13 - resolution: "@exodus/crypto@npm:1.0.0-rc.13" +"@exodus/crypto@npm:^1.0.0-rc.11, @exodus/crypto@npm:^1.0.0-rc.13, @exodus/crypto@npm:^1.0.0-rc.16": + version: 1.0.0-rc.16 + resolution: "@exodus/crypto@npm:1.0.0-rc.16" dependencies: + "@noble/ed25519": ^1.7.3 "@noble/hashes": ^1.3.3 "@noble/secp256k1": ^1.7.1 - checksum: 06b3e73a745ed6766471052bb5571d9bf2d3c4cbabd9c21fa2bab0f1ffba63f8b41e84660cda22991ba8d90e516114dc9515da220d4ff12e5c12e5c03bd2e6d1 + checksum: 44d517770dd08c06a332baa86fe7a7c9ccbbefa19217f71c58646e6c43b44c5251f86b9cc45171cf607e38fd057c2f237be053c3432721704e7ab45691e4df7a languageName: node linkType: hard @@ -2520,7 +2521,7 @@ __metadata: dependencies: "@exodus/basic-utils": ^3.0.1 "@exodus/bip32": ^3.3.0 - "@exodus/crypto": ^1.0.0-rc.13 + "@exodus/crypto": ^1.0.0-rc.16 "@exodus/key-identifier": ^1.3.0 "@exodus/key-ids": ^1.0.0 "@exodus/key-utils": ^3.7.0 @@ -3167,6 +3168,13 @@ __metadata: languageName: node linkType: hard +"@noble/ed25519@npm:^1.7.3": + version: 1.7.3 + resolution: "@noble/ed25519@npm:1.7.3" + checksum: 45169927d51de513e47bbeebff3a603433c4ac7579e1b8c5034c380a0afedbe85e6959be3d69584a7a5ed6828d638f8f28879003b9bb2fb5f22d8aa2d88fd5fe + languageName: node + linkType: hard + "@noble/hashes@npm:^1.3.3": version: 1.5.0 resolution: "@noble/hashes@npm:1.5.0" From 2e7640dfe6c14d1dfb10059561a0292261c399e0 Mon Sep 17 00:00:00 2001 From: Nikita Skovoroda Date: Sun, 27 Oct 2024 04:11:48 +0400 Subject: [PATCH 2/2] also use getSodiumKeysFromSeed from exodus/crypto --- features/keychain/module/crypto/sodium.js | 3 ++- features/keychain/package.json | 2 +- yarn.lock | 10 +++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/features/keychain/module/crypto/sodium.js b/features/keychain/module/crypto/sodium.js index 4dfe4866..f0761836 100644 --- a/features/keychain/module/crypto/sodium.js +++ b/features/keychain/module/crypto/sodium.js @@ -1,6 +1,7 @@ import sodium from '@exodus/sodium-crypto' import { mapValues } from '@exodus/basic-utils' import * as curve25519 from '@exodus/crypto/curve25519' +import { getSodiumKeysFromSeed } from '@exodus/crypto/sodium' const cloneBuffer = (buf) => { const newBuffer = Buffer.alloc(buf.length) @@ -21,7 +22,7 @@ export const create = ({ getPrivateHDKey }) => { // garbage collected, clearing it from memory. const getSodiumKeysFromIdentifier = async ({ seedId, keyId }) => { const { privateKey: sodiumSeed } = getPrivateHDKey({ seedId, keyId }) - return sodium.getSodiumKeysFromSeed(sodiumSeed) + return getSodiumKeysFromSeed(sodiumSeed) } const getKeysFromSeed = async ({ seedId, keyId, exportPrivate }) => { diff --git a/features/keychain/package.json b/features/keychain/package.json index 0921015a..13fe74a5 100644 --- a/features/keychain/package.json +++ b/features/keychain/package.json @@ -32,7 +32,7 @@ "dependencies": { "@exodus/basic-utils": "^3.0.1", "@exodus/bip32": "^3.3.0", - "@exodus/crypto": "^1.0.0-rc.16", + "@exodus/crypto": "^1.0.0-rc.18", "@exodus/key-identifier": "^1.3.0", "@exodus/key-utils": "^3.7.0", "@exodus/slip10": "^2.1.0", diff --git a/yarn.lock b/yarn.lock index 15dd5b67..73a52153 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2357,14 +2357,14 @@ __metadata: languageName: node linkType: hard -"@exodus/crypto@npm:^1.0.0-rc.11, @exodus/crypto@npm:^1.0.0-rc.13, @exodus/crypto@npm:^1.0.0-rc.16": - version: 1.0.0-rc.16 - resolution: "@exodus/crypto@npm:1.0.0-rc.16" +"@exodus/crypto@npm:^1.0.0-rc.11, @exodus/crypto@npm:^1.0.0-rc.13, @exodus/crypto@npm:^1.0.0-rc.18": + version: 1.0.0-rc.18 + resolution: "@exodus/crypto@npm:1.0.0-rc.18" dependencies: "@noble/ed25519": ^1.7.3 "@noble/hashes": ^1.3.3 "@noble/secp256k1": ^1.7.1 - checksum: 44d517770dd08c06a332baa86fe7a7c9ccbbefa19217f71c58646e6c43b44c5251f86b9cc45171cf607e38fd057c2f237be053c3432721704e7ab45691e4df7a + checksum: c913352057cf14f1dda1302d4077299288a1cefee57535018a0bdce447d3ab1d28e594c39bd4a15ee47839ffae1801d8ba593464b4ae4c907cb69da7e5d0fb6d languageName: node linkType: hard @@ -2521,7 +2521,7 @@ __metadata: dependencies: "@exodus/basic-utils": ^3.0.1 "@exodus/bip32": ^3.3.0 - "@exodus/crypto": ^1.0.0-rc.16 + "@exodus/crypto": ^1.0.0-rc.18 "@exodus/key-identifier": ^1.3.0 "@exodus/key-ids": ^1.0.0 "@exodus/key-utils": ^3.7.0