Skip to content

Commit

Permalink
feat: use exodus/crypto instead of sodium where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Oct 26, 2024
1 parent 55df4ff commit 6e5dd44
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
11 changes: 3 additions & 8 deletions features/keychain/module/crypto/ed25519.js
Original file line number Diff line number Diff line change
@@ -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' })
},
})

Expand Down
22 changes: 10 additions & 12 deletions features/keychain/module/crypto/sodium.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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' })
}
2 changes: 1 addition & 1 deletion features/keychain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 13 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6e5dd44

Please sign in to comment.