Skip to content

Commit

Permalink
Remove more react-native-bip39
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanregisser committed Sep 2, 2024
1 parent 1324dbd commit 4290d87
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
17 changes: 8 additions & 9 deletions src/backup/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as bip39 from 'react-native-bip39'
import { formatBackupPhraseOnEdit } from 'src/backup/utils'
import { normalizeMnemonic, validateMnemonic } from 'src/utils/account'

Expand Down Expand Up @@ -52,41 +51,41 @@ inner surprise invest`

it('validates spanish successfully', () => {
const mnemonic = normalizeMnemonic(SPANISH_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeTruthy()
expect(validateMnemonic(mnemonic)).toBeTruthy()
})

it('validates spanish successfully without mnemonic accents', () => {
const mnemonic = normalizeMnemonic(SPANISH_MNEMONIC_NO_ACCENTS)
expect(validateMnemonic(mnemonic, bip39)).toBeTruthy()
expect(validateMnemonic(mnemonic)).toBeTruthy()
})

it('validates portuguese successfully', () => {
const mnemonic = normalizeMnemonic(PORTUGUESE_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeTruthy()
expect(validateMnemonic(mnemonic)).toBeTruthy()
})

it('validates english successfully', () => {
const mnemonic = normalizeMnemonic(ENGLISH_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeTruthy()
expect(validateMnemonic(mnemonic)).toBeTruthy()
})

it('validates english multiline successfully', () => {
const mnemonic = normalizeMnemonic(MULTILINE_ENGLISH_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeTruthy()
expect(validateMnemonic(mnemonic)).toBeTruthy()
})

it('does not validate bad english', () => {
const mnemonic = normalizeMnemonic(BAD_ENGLISH_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeFalsy()
expect(validateMnemonic(mnemonic)).toBeFalsy()
})

it('does not validate bad spanish', () => {
const mnemonic = normalizeMnemonic(BAD_SPANISH_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeFalsy()
expect(validateMnemonic(mnemonic)).toBeFalsy()
})

it('does not validate bad portuguese', () => {
const mnemonic = normalizeMnemonic(BAD_PORTUGUESE_MNEMONIC)
expect(validateMnemonic(mnemonic, bip39)).toBeFalsy()
expect(validateMnemonic(mnemonic)).toBeFalsy()
})
})
3 changes: 1 addition & 2 deletions src/import/saga.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { privateKeyToAddress } from '@celo/utils/lib/address'
import { Task } from '@redux-saga/types'
import * as bip39 from 'react-native-bip39'
import { setBackupCompleted } from 'src/account/actions'
import { initializeAccountSaga } from 'src/account/saga'
import { recoveringFromStoreWipeSelector } from 'src/account/selectors'
Expand Down Expand Up @@ -52,7 +51,7 @@ export function* importBackupPhraseSaga({ phrase, useEmptyWallet }: ImportBackup
Logger.debug(TAG + '@importBackupPhraseSaga', 'Importing backup phrase')
try {
const normalizedPhrase = normalizeMnemonic(phrase)
const phraseIsValid = validateMnemonic(normalizedPhrase, bip39)
const phraseIsValid = validateMnemonic(normalizedPhrase)
const invalidWords = phraseIsValid ? [] : invalidMnemonicWords(normalizedPhrase)

if (!phraseIsValid) {
Expand Down
8 changes: 3 additions & 5 deletions src/web3/saga.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { isValidChecksumAddress } from '@celo/utils/lib/address'
import * as bip39 from 'react-native-bip39'
import { expectSaga } from 'redux-saga-test-plan'
import * as matchers from 'redux-saga-test-plan/matchers'
import { call, select } from 'redux-saga/effects'
Expand All @@ -8,15 +7,15 @@ import { ErrorMessages } from 'src/app/ErrorMessages'
import { storeMnemonic } from 'src/backup/utils'
import { currentLanguageSelector } from 'src/i18n/selectors'
import { getPasswordSaga, retrieveSignedMessage } from 'src/pincode/authentication'
import { generateMnemonic, MnemonicLanguages, MnemonicStrength } from 'src/utils/account'
import { MnemonicLanguages, MnemonicStrength, generateMnemonic } from 'src/utils/account'
import { setAccount, setDataEncryptionKey } from 'src/web3/actions'
import {
UnlockResult,
getConnectedAccount,
getConnectedUnlockedAccount,
getOrCreateAccount,
getWalletAddress,
unlockAccount,
UnlockResult,
} from 'src/web3/saga'
import { currentAccountSelector, walletAddressSelector } from 'src/web3/selectors'
import { createMockStore } from 'test/utils'
Expand Down Expand Up @@ -101,8 +100,7 @@ describe(getOrCreateAccount, () => {
.call(
generateMnemonic,
MnemonicStrength.s128_12words,
MnemonicLanguages[expectedMnemonicLang] as unknown as MnemonicLanguages,
bip39
MnemonicLanguages[expectedMnemonicLang] as unknown as MnemonicLanguages
)
.run()

Expand Down
13 changes: 6 additions & 7 deletions src/web3/saga.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { privateKeyToAddress } from '@celo/utils/lib/address'
import { UnlockableWallet } from '@celo/wallet-base'
import { RpcWalletErrors } from '@celo/wallet-rpc/lib/rpc-wallet'
import * as bip39 from 'react-native-bip39'
import { setAccountCreationTime } from 'src/account/actions'
import { generateSignedMessage } from 'src/account/saga'
import { ErrorMessages } from 'src/app/ErrorMessages'
import { generateKeysFromMnemonic, storeMnemonic } from 'src/backup/utils'
import { clearPasswordCaches } from 'src/pincode/PasswordCache'
import {
CANCELLED_PIN_INPUT,
getPasswordSaga,
retrieveSignedMessage,
} from 'src/pincode/authentication'
import { clearPasswordCaches } from 'src/pincode/PasswordCache'
import { generateMnemonic, MnemonicLanguages, MnemonicStrength } from 'src/utils/account'
import { ensureError } from 'src/utils/ensureError'
import Logger from 'src/utils/Logger'
import { Actions, setAccount, SetAccountAction } from 'src/web3/actions'
import { MnemonicLanguages, MnemonicStrength, generateMnemonic } from 'src/utils/account'
import { ensureError } from 'src/utils/ensureError'
import { Actions, SetAccountAction, setAccount } from 'src/web3/actions'
import { UNLOCK_DURATION } from 'src/web3/consts'
import { getWallet, initContractKit } from 'src/web3/contracts'
import { createAccountDek } from 'src/web3/dataEncryptionKey'
Expand Down Expand Up @@ -45,7 +44,7 @@ export function* getOrCreateAccount() {

const mnemonicBitLength = MnemonicStrength.s128_12words
const mnemonicLanguage = MnemonicLanguages.english
let mnemonic: string = yield* call(generateMnemonic, mnemonicBitLength, mnemonicLanguage, bip39)
let mnemonic: string = yield* call(generateMnemonic, mnemonicBitLength, mnemonicLanguage)

// Ensure no duplicates in mnemonic
const checkDuplicate = (someString: string) => {
Expand All @@ -54,7 +53,7 @@ export function* getOrCreateAccount() {
let duplicateInMnemonic = checkDuplicate(mnemonic)
while (duplicateInMnemonic) {
Logger.debug(TAG + '@getOrCreateAccount', 'Regenerating mnemonic to avoid duplicates')
mnemonic = yield* call(generateMnemonic, mnemonicBitLength, mnemonicLanguage, bip39)
mnemonic = yield* call(generateMnemonic, mnemonicBitLength, mnemonicLanguage)

Check warning on line 56 in src/web3/saga.ts

View check run for this annotation

Codecov / codecov/patch

src/web3/saga.ts#L56

Added line #L56 was not covered by tests
duplicateInMnemonic = checkDuplicate(mnemonic)
}

Expand Down

0 comments on commit 4290d87

Please sign in to comment.