Skip to content

Commit

Permalink
fix: tests / chacha
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Dec 3, 2024
1 parent e53b5a2 commit 85ef68b
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 117 deletions.

This file was deleted.

1 change: 0 additions & 1 deletion apps/wallet-mobile/__mocks__/react-native-cardano.js

This file was deleted.

10 changes: 0 additions & 10 deletions apps/wallet-mobile/__mocks__/react-native-config.js

This file was deleted.

1 change: 0 additions & 1 deletion apps/wallet-mobile/__mocks__/react-native-randombytes.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import mockAsyncStorage from '@react-native-async-storage/async-storage/jest/async-storage-mock'

const mock = jest.mock(
'@react-native-async-storage/async-storage',
() => mockAsyncStorage,
)

exports.default = mock
1 change: 1 addition & 0 deletions apps/wallet/__mocks__/react-native-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
6 changes: 6 additions & 0 deletions apps/wallet/__mocks__/react-native-randombytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
randomBytes: jest.fn((size, callback) => {
const mockBuffer = Buffer.alloc(size, 0)
callback(null, mockBuffer)
}),
}
File renamed without changes.
3 changes: 1 addition & 2 deletions apps/wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@formatjs/intl-relativetimeformat": "^11.2.1",
"@jamsinclair/react-native-notifications": "^5.1.1",
"@ledgerhq/react-native-hw-transport-ble": "6.27.3",
"@noble/ciphers": "^1.1.3",
"@react-native-async-storage/async-storage": "2.0.0",
"@react-native-clipboard/clipboard": "^1.15.0",
"@react-native-masked-view/masked-view": "^0.3.2",
Expand Down Expand Up @@ -70,7 +71,6 @@
"buffer": "^6.0.3",
"cbor": "^10.0.3",
"cbor-rn-prereqs": "^10.0.3",
"chacha": "^2.1.0",
"d3-shape": "^3.2.0",
"expo": "^51.0.0",
"expo-background-fetch": "~12.0.1",
Expand Down Expand Up @@ -114,7 +114,6 @@
"react-native-svg-charts": "^5.4.0",
"react-native-system-navigation-bar": "^2.6.4",
"react-native-tab-view": "^4.0.5",
"react-native-timezone": "^3.0.1",
"react-native-url-polyfill": "^2.0.0",
"react-native-view-shot": "^4.0.2",
"react-native-webview": "^13.12.4",
Expand Down
40 changes: 35 additions & 5 deletions apps/wallet/src/components/ConfirmTx/ConfirmTx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {debugWalletInfo, features} from '../../kernel/features'
import {
confirmationMessages,
errorMessages,
ledgerMessages,
txLabels,
} from '../../kernel/i18n/global-messages'
import {LocalizableError} from '../../kernel/i18n/LocalizableError'
Expand All @@ -23,6 +24,13 @@ import {delay} from '../../yoroi-wallets/utils/timeUtils'
import {Button, ButtonProps} from '../Button/Button'
import {ValidatedTextInput} from '../ValidatedTextInput'
import {Dialog, Step as DialogStep} from './Dialog'
import {
AdaAppClosedError,
BluetoothDisabledError,
GeneralConnectionError,
LedgerUserError,
RejectedByUserError,
} from '../../yoroi-wallets/hw/hw'

type ErrorData = {
errorMessage: string
Expand Down Expand Up @@ -376,12 +384,34 @@ const useStyles = () => {
const useStrings = () => {
const intl = useIntl()

const appOpenedError = intl.formatMessage(ledgerMessages.appOpened)
const bluetoothError = intl.formatMessage(
ledgerMessages.bluetoothDisabledError,
)
const connectionError = intl.formatMessage(ledgerMessages.connectionError)
const onDeviceError = intl.formatMessage(ledgerMessages.noDeviceInfoError)
const rejectedByUserError = intl.formatMessage(
ledgerMessages.rejectedByUserError,
)

return {
errorMessage: (error: LocalizableError) =>
intl.formatMessage(
{id: error.id, defaultMessage: error.defaultMessage},
(error as any).values,
),
errorMessage: (error: LocalizableError) => {
let message = intl.formatMessage(errorMessages.generalTxError.message)

if (error instanceof BluetoothDisabledError) {
message = bluetoothError
} else if (error instanceof GeneralConnectionError) {
message = connectionError
} else if (error instanceof LedgerUserError) {
message = onDeviceError
} else if (error instanceof RejectedByUserError) {
message = rejectedByUserError
} else if (error instanceof AdaAppClosedError) {
message = appOpenedError
}

return message
},
password: intl.formatMessage(txLabels.password),
confirmButton: intl.formatMessage(
confirmationMessages.commonButtons.confirmButton,
Expand Down
5 changes: 2 additions & 3 deletions apps/wallet/src/kernel/i18n/LanguageProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {parseSafe, useAsyncStorage} from '@yoroi/common'
import React, {useMemo} from 'react'
import {IntlProvider} from 'react-intl'
import {Text} from 'react-native'
// @ts-ignore
import TimeZone from 'react-native-timezone'
import * as RNLocalize from 'react-native-localize'

import {numberLocale, systemLocale} from './initialization'
import {LanguageCode, NumberLocale, supportedLanguages} from './languages'
Expand Down Expand Up @@ -63,7 +62,7 @@ const useTimezone = () => {
queryKey: ['timeZone'],
queryFn: async () => {
const defaultTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone
const timeZone = await TimeZone.getTimeZone()
const timeZone = RNLocalize.getTimeZone()
return timeZone ?? defaultTimeZone
},
suspense: true,
Expand Down
53 changes: 17 additions & 36 deletions apps/wallet/src/yoroi-wallets/cardano/catalyst/catalystCipher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-ignore
import chacha from 'chacha'
import {chacha20poly1305} from '@noble/ciphers/chacha'
import pbkdf2 from 'pbkdf2'

import {cryptoRandomString} from '../../utils/crypto-random-string'
Expand All @@ -9,7 +8,7 @@ const SALT_SIZE = 16
const KEY_SIZE = 32
const DIGEST = 'sha512'
const NONCE_SIZE = 12
const TAG_SIZE = 16
// const TAG_SIZE = 16
const PROTO_SIZE = 1
const PROTO_VERSION = Buffer.from('01', 'hex')

Expand Down Expand Up @@ -42,29 +41,19 @@ export async function encryptWithPassword(
passwordBuf: Uint8Array,
dataBytes: Uint8Array,
): Promise<string> {
const salt = Buffer.from(cryptoRandomString(2 * 16), 'hex')
const nonce = Buffer.from(cryptoRandomString(2 * 12), 'hex')
const data = Buffer.from(dataBytes)
const salt = Buffer.from(cryptoRandomString(16), 'hex')
const nonce = Buffer.from(cryptoRandomString(12), 'hex')
const aad = Buffer.from('', 'hex')

const key = await promisifyPbkdf2(passwordBuf, salt)
const encrypted = chacha20poly1305(key, nonce, aad).encrypt(dataBytes)

const cipher = chacha.createCipher(key, nonce)
cipher.setAAD(aad, {plaintextLength: data.length})

const head = cipher.update(data)
const final = cipher.final()
const tag = cipher.getAuthTag()

const cipherText = Buffer.concat([
return Buffer.concat([
PROTO_VERSION,
salt,
nonce,
head,
final,
tag,
])
return cipherText.toString('hex')
Buffer.from(encrypted),
]).toString('hex')
}

export async function decryptWithPassword(
Expand All @@ -78,27 +67,19 @@ export async function decryptWithPassword(
SALT_SIZE + PROTO_SIZE,
SALT_SIZE + NONCE_SIZE + PROTO_SIZE,
)
const cipherdata = ciphertext.slice(
SALT_SIZE + NONCE_SIZE + PROTO_SIZE,
-TAG_SIZE,
)
const tag = ciphertext.slice(
SALT_SIZE + PROTO_SIZE + NONCE_SIZE + cipherdata.length,
)

const aad = Buffer.from('', 'hex')
const cipherdata = ciphertext.slice(SALT_SIZE + NONCE_SIZE + PROTO_SIZE)

if (ciphertext.length <= SALT_SIZE + NONCE_SIZE + TAG_SIZE) {
throw new Error('not enough data to decrypt')
if (ciphertext.length <= SALT_SIZE + NONCE_SIZE) {
throw new Error('Not enough data to decrypt')
}

const key = await promisifyPbkdf2(passwordBuf, salt)
const cipher = chacha20poly1305(key, nonce)
const decrypted = cipher.decrypt(nonce, cipherdata)

const decipher = chacha.createDecipher(key, nonce)
decipher.setAAD(aad)
decipher.setAuthTag(tag)
if (!decrypted) {
throw new Error('Decryption failed or data tampered')
}

let decrypted = decipher.update(cipherdata, 'ignored', 'hex')
decrypted += decipher.final('hex')
return decrypted
return Buffer.from(decrypted).toString('hex')
}
65 changes: 7 additions & 58 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2942,6 +2942,11 @@
dependencies:
eslint-scope "5.1.1"

"@noble/ciphers@^1.1.3":
version "1.1.3"
resolved "https://registry.yarnpkg.com/@noble/ciphers/-/ciphers-1.1.3.tgz#eb27085aa7ce94d8c6eaeb64299bab0589920ec1"
integrity sha512-Ygv6WnWJHLLiW4fnNDC1z+i13bud+enXOFRBlpxI+NJliPWx5wdR+oWlTjLuBPTqjUjtHXtjkU6w3kuuH6upZA==

"@noble/hashes@^1.2.0", "@noble/hashes@^1.3.2":
version "1.6.1"
resolved "https://registry.yarnpkg.com/@noble/hashes/-/hashes-1.6.1.tgz#df6e5943edcea504bac61395926d6fd67869a0d5"
Expand Down Expand Up @@ -5748,13 +5753,6 @@ binary-extensions@^2.0.0:
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.3.0.tgz#f6e14a97858d327252200242d4ccfe522c445522"
integrity sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==

bindings@^1.2.1:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"

bip39@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/bip39/-/bip39-3.1.0.tgz#c55a418deaf48826a6ceb34ac55b3ee1577e18a3"
Expand Down Expand Up @@ -6316,25 +6314,6 @@ cbor@^10.0.3:
dependencies:
nofilter "^3.0.2"

chacha-native@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/chacha-native/-/chacha-native-2.0.3.tgz#435259a015a46816d1bc11376bd4c09e35c4bf5f"
integrity sha512-93h+osfjhR2sMHAaapTLlL/COoBPEZ6upicPBQ4GfUyadoMb8t9/M0PKK8kC+F+DEA/Oy3Kg9w3HzY3J1foP3g==
dependencies:
bindings "^1.2.1"
inherits "^2.0.1"
nan "^2.4.0"

chacha@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/chacha/-/chacha-2.1.0.tgz#f4d57c8768a1b4206e9967f858b48da7dd28eaa6"
integrity sha512-FhVtqaZOiHlOKUkAWfDlJ+oe/O8iPQbCC0pFXJqphr4YQBCZPXa8Mv3j35+W4eWFWCoTUcW2u5IWDDkknygvVA==
dependencies:
inherits "^2.0.1"
readable-stream "^1.0.33"
optionalDependencies:
chacha-native "^2.0.0"

chalk@4, chalk@^4.0.0, chalk@^4.0.2, chalk@^4.1.0, chalk@^4.1.1, chalk@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/chalk/-/chalk-4.1.2.tgz#aac4e2b7734a740867aeb16bf02aad556a1e7a01"
Expand Down Expand Up @@ -8974,11 +8953,6 @@ file-entry-cache@^6.0.1:
dependencies:
flat-cache "^3.0.4"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==

filelist@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/filelist/-/filelist-1.0.4.tgz#f78978a1e944775ff9e62e744424f215e58352b5"
Expand Down Expand Up @@ -10088,7 +10062,7 @@ inflight@^1.0.4:
once "^1.3.0"
wrappy "1"

inherits@2, [email protected], inherits@^2.0.1, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.1, inherits@~2.0.3, inherits@~2.0.4:
inherits@2, [email protected], inherits@^2.0.1, inherits@^2.0.3, inherits@^2.0.4, inherits@~2.0.3, inherits@~2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
Expand Down Expand Up @@ -10684,11 +10658,6 @@ is-yarn-global@^0.4.0:
resolved "https://registry.yarnpkg.com/is-yarn-global/-/is-yarn-global-0.4.1.tgz#b312d902b313f81e4eaf98b6361ba2b45cd694bb"
integrity sha512-/kppl+R+LO5VmhYSEWARUFjodS25D68gvj8W7z0I7OWhUla5xWu8KL6CtB2V0R6yqhnRgbcaREMr4EEM6htLPQ==

[email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
integrity sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==

isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
Expand Down Expand Up @@ -13061,7 +13030,7 @@ mz@^2.7.0:
object-assign "^4.0.1"
thenify-all "^1.0.0"

nan@^2.14.0, nan@^2.15.0, nan@^2.4.0:
nan@^2.14.0, nan@^2.15.0:
version "2.22.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.22.0.tgz#31bc433fc33213c97bad36404bb68063de604de3"
integrity sha512-nbajikzWTMwsW+eSsNm3QwlOs7het9gGJU5dDZzRTQGk03vyBOauxgI4VakDzE0PtsGTmXPsXTbbjVhRwR5mpw==
Expand Down Expand Up @@ -14902,11 +14871,6 @@ react-native-tab-view@^4.0.5:
dependencies:
use-latest-callback "^0.2.1"

react-native-timezone@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/react-native-timezone/-/react-native-timezone-3.0.1.tgz#028fe136a572b97a8011f175e5501c5efd0b63db"
integrity sha512-EElJM9zIfs6yPY2zwZn9JzCpSgXcUg+ZqR057Jlx3dnsVw65fF/2MNiEfwt+x9FaWBKXVDthLOtqNsQ4cs2w4g==

react-native-url-polyfill@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/react-native-url-polyfill/-/react-native-url-polyfill-2.0.0.tgz#db714520a2985cff1d50ab2e66279b9f91ffd589"
Expand Down Expand Up @@ -15150,16 +15114,6 @@ readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.0.2, readable-stre
string_decoder "^1.1.1"
util-deprecate "^1.0.1"

readable-stream@^1.0.33:
version "1.1.14"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.1.14.tgz#7cf4c54ef648e3813084c636dd2079e166c081d9"
integrity sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"

readable-stream@^2.0.2, readable-stream@^2.3.8, readable-stream@~2.3.6:
version "2.3.8"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-2.3.8.tgz#91125e8042bba1b9887f49345f6277027ce8be9b"
Expand Down Expand Up @@ -16349,11 +16303,6 @@ string_decoder@^1.1.1:
dependencies:
safe-buffer "~5.2.0"

string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
integrity sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==

string_decoder@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
Expand Down

0 comments on commit 85ef68b

Please sign in to comment.