Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use exodus/crypto instead of sodium where possible #164

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 12 additions & 13 deletions features/keychain/module/crypto/sodium.js
Original file line number Diff line number Diff line change
@@ -1,5 +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)
Expand All @@ -20,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 }) => {
Expand All @@ -38,20 +40,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 +92,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.18",
"@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.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: 06b3e73a745ed6766471052bb5571d9bf2d3c4cbabd9c21fa2bab0f1ffba63f8b41e84660cda22991ba8d90e516114dc9515da220d4ff12e5c12e5c03bd2e6d1
checksum: c913352057cf14f1dda1302d4077299288a1cefee57535018a0bdce447d3ab1d28e594c39bd4a15ee47839ffae1801d8ba593464b4ae4c907cb69da7e5d0fb6d
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.18
"@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
Loading