Skip to content

Commit

Permalink
feat(HW-709): refactor providers usage in mobile app (#2106)
Browse files Browse the repository at this point in the history
* feat(HW-709): refactor providers usage in mobile app

* chore: install release @haqq/rn-wallet-providers

---------

Co-authored-by: iGroza <[email protected]>
  • Loading branch information
iGroza and iGroza committed Sep 25, 2024
1 parent bc660e9 commit 65fd80e
Show file tree
Hide file tree
Showing 61 changed files with 279 additions and 331 deletions.
21 changes: 10 additions & 11 deletions __tests__/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,19 @@ jest.mock('react-native-fs', jest.fn);
jest.mock('@react-navigation/native', () => ({
createNavigationContainerRef: jest.fn,
}));
jest.mock('@haqq/provider-ledger-react-native', () => ({
getBleManager: jest.fn,
}));
jest.mock('react-native-ble-plx', () => ({
State: jest.fn,
}));
jest.mock('@haqq/provider-mnemonic-react-native', () => ({
ProviderMnemonicReactNative: jest.fn,
}));
jest.mock('@haqq/provider-sss-react-native', () => ({
ProviderSSSReactNative: jest.fn,
}));
jest.mock('@haqq/provider-hot-react-native', () => ({
ProviderHotReactNative: jest.fn,
jest.mock('@haqq/rn-wallet-providers', () => ({
utils: {
getBleManager: jest.fn,
},
ProviderMnemonicBase: jest.fn,
ProviderMnemonicEvm: jest.fn,
ProviderSSSBase: jest.fn,
ProviderSSSEvm: jest.fn,
ProviderHotBase: jest.fn,
ProviderHotEvm: jest.fn,
}));
jest.mock('@react-native-async-storage/async-storage', jest.fn);
jest.mock('@haqq/shared-react-native', () => ({
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@
"@expo/react-native-action-sheet": "4.0.1",
"@haqq/encryption-react-native": "0.0.4",
"@haqq/format-number-with-subscript-zeros": "1.0.1",
"@haqq/provider-base": "0.0.23",
"@haqq/provider-hot-react-native": "0.0.10",
"@haqq/provider-keystone-react-native": "0.0.7",
"@haqq/provider-ledger-react-native": "0.0.28",
"@haqq/provider-mnemonic-react-native": "0.0.14",
"@haqq/provider-sss-react-native": "0.0.14",
"@haqq/provider-web3-utils": "0.0.14",
"@haqq/rn-wallet-providers": "0.0.2",
"@haqq/shared-react-native": "0.0.12",
"@invertase/react-native-apple-authentication": "2.2.2",
"@keystonehq/bc-ur-registry-eth": "0.7.7",
Expand Down
2 changes: 1 addition & 1 deletion src/components/ledger-scan/ledger-scan-row.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback} from 'react';

import {Device} from '@haqq/provider-ledger-react-native';
import {Device} from '@haqq/rn-wallet-providers';
import {ActivityIndicator, TouchableOpacity} from 'react-native';

import {Color} from '@app/colors';
Expand Down
2 changes: 1 addition & 1 deletion src/components/ledger-scan/ledger-scan.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback} from 'react';

import {Device} from '@haqq/provider-ledger-react-native';
import {Device} from '@haqq/rn-wallet-providers';
import {FlatList, ListRenderItem} from 'react-native';

import {PopupContainer} from '@app/components/ui';
Expand Down
4 changes: 2 additions & 2 deletions src/components/modals/cloud-share-not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback, useEffect} from 'react';

import {ProviderSSSReactNative} from '@haqq/provider-sss-react-native';
import {ProviderSSSBase} from '@haqq/rn-wallet-providers';
import {observer} from 'mobx-react';
import {Image, Platform, View} from 'react-native';

Expand Down Expand Up @@ -63,7 +63,7 @@ export const CloudShareNotFound = observer(
);
const storage = await getProviderStorage('', 'cloud');

await ProviderSSSReactNative.initialize(
await ProviderSSSBase.initialize(
creds.privateKey,
null,
localShare,
Expand Down
7 changes: 3 additions & 4 deletions src/event-actions/on-app-backup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ProviderMnemonicReactNative} from '@haqq/provider-mnemonic-react-native';
import {ProviderSSSReactNative} from '@haqq/provider-sss-react-native';
import {ProviderMnemonicBase, ProviderSSSBase} from '@haqq/rn-wallet-providers';
import {isAfter} from 'date-fns';

import {app} from '@app/contexts';
Expand All @@ -19,10 +18,10 @@ export async function onAppBackup() {
return;
}

const mnemonics = await ProviderMnemonicReactNative.getAccounts();
const mnemonics = await ProviderMnemonicBase.getAccounts();

if (isFeatureEnabled(Feature.sss)) {
const sss = await ProviderSSSReactNative.getAccounts();
const sss = await ProviderSSSBase.getAccounts();
if (mnemonics.length && !sss.length) {
navigator.navigate(SettingsStackRoutes.BackupSssSuggestion, {
accountId: mnemonics[0],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {cosmosAddress} from '@haqq/provider-base';

import {AddressUtils} from '@app/helpers/address-utils';
import {VariablesBool} from '@app/models/variables-bool';
import {VariablesString} from '@app/models/variables-string';
import {Wallet} from '@app/models/wallet';
import {Backend} from '@app/services/backend';
import {PushNotificationTopicsEnum} from '@app/services/push-notifications';
import {COSMOS_PREFIX} from '@app/variables/common';

export async function onPushSubscriptionTransactionsSubscribe() {
VariablesBool.set(
Expand All @@ -22,7 +20,7 @@ export async function onPushSubscriptionTransactionsSubscribe() {
wallets.map(async w =>
Backend.instance.createNotificationSubscription(
subscription,
cosmosAddress(w.address, COSMOS_PREFIX),
AddressUtils.toHaqq(w.address),
),
),
);
Expand Down
4 changes: 2 additions & 2 deletions src/event-actions/on-wallet-create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderMnemonicReactNative} from '@haqq/provider-mnemonic-react-native';
import {ProviderMnemonicBase} from '@haqq/rn-wallet-providers';

import {app} from '@app/contexts';
import {onStakingSync} from '@app/event-actions/on-staking-sync';
Expand Down Expand Up @@ -43,7 +43,7 @@ export async function onWalletCreate(wallet: Wallet) {
case WalletType.mnemonic:
const providerMnemonic = (await getProviderInstanceForWallet(
wallet,
)) as ProviderMnemonicReactNative;
)) as ProviderMnemonicBase;
if (typeof providerMnemonic.isMnemonicSaved === 'function') {
mnemonicSaved = await providerMnemonic.isMnemonicSaved();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/event-actions/on-wallet-mnemonic-check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderMnemonicReactNative} from '@haqq/provider-mnemonic-react-native';
import {ProviderMnemonicBase} from '@haqq/rn-wallet-providers';
import {isAfter} from 'date-fns';

import {app} from '@app/contexts';
Expand All @@ -20,7 +20,7 @@ export async function onWalletMnemonicCheck(snoozeBackup: Date) {
);

for (const accountId of accounts) {
const provider = new ProviderMnemonicReactNative({
const provider = new ProviderMnemonicBase({
account: accountId,
getPassword: app.getPassword.bind(app),
});
Expand Down
4 changes: 2 additions & 2 deletions src/event-actions/on-wallet-mnemonic-saved.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderMnemonicReactNative} from '@haqq/provider-mnemonic-react-native';
import {ProviderMnemonicBase} from '@haqq/rn-wallet-providers';

import {app} from '@app/contexts';
import {Wallet} from '@app/models/wallet';
Expand All @@ -7,7 +7,7 @@ import {WalletType} from '@app/types';
export async function onWalletMnemonicSaved(accountId: string) {
const wallets = Wallet.getAll();

const provider = new ProviderMnemonicReactNative({
const provider = new ProviderMnemonicBase({
account: accountId,
getPassword: app.getPassword.bind(app),
});
Expand Down
4 changes: 2 additions & 2 deletions src/event-actions/on-wallet-sss-check.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderSSSReactNative} from '@haqq/provider-sss-react-native';
import {ProviderSSSBase} from '@haqq/rn-wallet-providers';
import {isAfter} from 'date-fns';

import {app} from '@app/contexts';
Expand All @@ -23,7 +23,7 @@ export async function onWalletSssCheck(snoozeBackup: Date) {

for (const accountId of accounts) {
const storage = await getProviderStorage(accountId);
const provider = new ProviderSSSReactNative({
const provider = new ProviderSSSBase({
storage,
account: accountId,
getPassword: app.getPassword.bind(app),
Expand Down
8 changes: 3 additions & 5 deletions src/event-actions/on-wallet-sss-saved.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderSSSReactNative} from '@haqq/provider-sss-react-native';
import {ProviderSSSBase} from '@haqq/rn-wallet-providers';

import {app} from '@app/contexts';
import {getProviderStorage} from '@app/helpers/get-provider-storage';
Expand All @@ -9,15 +9,13 @@ export async function onWalletSssSaved(accountId: string) {
const wallets = Wallet.getAll();

const storage = await getProviderStorage(accountId);
const provider = new ProviderSSSReactNative({
const provider = new ProviderSSSBase({
account: accountId,
storage,
getPassword: app.getPassword.bind(app),
});

const storages = await ProviderSSSReactNative.getStoragesForAccount(
accountId,
);
const storages = await ProviderSSSBase.getStoragesForAccount(accountId);

const isShareSaved = await Promise.all(
storages.map(async s => {
Expand Down
Empty file added src/helpers/address.ts
Empty file.
4 changes: 2 additions & 2 deletions src/helpers/await-for-bluetooth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getBleManager} from '@haqq/provider-ledger-react-native';
import {utils} from '@haqq/rn-wallet-providers';
import {State} from 'react-native-ble-plx';

import {hideModal, showModal} from '@app/helpers/modal';
Expand All @@ -10,7 +10,7 @@ export const awaitForBluetooth = () => {
return new Promise<void>((resolve, reject) => {
let currentState = State.Unknown;
let currentModal: ModalName | null = null;
const manager = getBleManager();
const manager = utils.getBleManager();

const onClose = () => {
reject(new Error('bluetooth_popup_closed'));
Expand Down
12 changes: 9 additions & 3 deletions src/helpers/await-for-ledger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {ProviderInterface} from '@haqq/provider-base';
import {ProviderLedgerReactNative} from '@haqq/provider-ledger-react-native';
import {
ProviderInterface,
ProviderLedgerBase,
ProviderLedgerEvm,
} from '@haqq/rn-wallet-providers';
import {Keyboard} from 'react-native';

import {app} from '@app/contexts';
Expand All @@ -19,7 +22,10 @@ const LEDGER_PROVIDER_EVENTS = [
];

export const awaitForLedger = async (transport: ProviderInterface) => {
if (transport instanceof ProviderLedgerReactNative) {
if (
transport instanceof ProviderLedgerBase ||
transport instanceof ProviderLedgerEvm
) {
Keyboard.dismiss();
await awaitForBluetooth();
return new Promise<void>((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/create-wallets-for-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderInterface} from '@haqq/provider-base';
import {ProviderInterface} from '@haqq/rn-wallet-providers';

import {app} from '@app/contexts';
import {I18N, getText} from '@app/i18n';
Expand Down
6 changes: 4 additions & 2 deletions src/helpers/decrypt-local-share.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {ITEM_KEY} from '@haqq/provider-sss-react-native/dist/constants';
import {accountInfo} from '@haqq/provider-web3-utils';
import {constants} from '@haqq/rn-wallet-providers';
import {decryptShare} from '@haqq/shared-react-native';
import EncryptedStorage from 'react-native-encrypted-storage';

Expand All @@ -9,7 +9,9 @@ export const decryptLocalShare = async (
) => {
const account = await accountInfo(sssPrivateKey);
const _value = await EncryptedStorage.getItem(
`${ITEM_KEY}_${account.address.toLowerCase()}`,
`${
constants.ITEM_KEYS[constants.WalletType.sss]
}_${account.address.toLowerCase()}`,
);
const shareStore = await decryptShare(JSON.parse(_value || ''), password);

Expand Down
15 changes: 7 additions & 8 deletions src/helpers/get-provider-for-new-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {ProviderMnemonicReactNative} from '@haqq/provider-mnemonic-react-native';
import {ProviderSSSReactNative} from '@haqq/provider-sss-react-native';
import {ProviderMnemonicBase, ProviderSSSBase} from '@haqq/rn-wallet-providers';

import {app} from '@app/contexts';
import {getProviderStorage} from '@app/helpers/get-provider-storage';
Expand All @@ -12,7 +11,7 @@ export async function getProviderForNewWallet(params?: WalletInitialData) {

if (params && params.type === 'sss') {
const storage = await getProviderStorage('', params.provider);
return await ProviderSSSReactNative.initialize(
return await ProviderSSSBase.initialize(
params.action === 'restore' ? params.sssPrivateKey || null : null,
params.sssCloudShare || null,
params.sssLocalShare || null,
Expand All @@ -28,26 +27,26 @@ export async function getProviderForNewWallet(params?: WalletInitialData) {
).catch(err => ErrorHandler.handle('sssLimitReached', err));
}

const keysSss = await ProviderSSSReactNative.getAccounts();
const keysSss = await ProviderSSSBase.getAccounts();

if (keysSss.length) {
const storage = await getProviderStorage(keysSss[0]);

return new ProviderSSSReactNative({
return new ProviderSSSBase({
storage,
account: keysSss[0],
getPassword,
});
}

const keysMnemonic = await ProviderMnemonicReactNative.getAccounts();
const keysMnemonic = await ProviderMnemonicBase.getAccounts();

if (keysMnemonic.length) {
return new ProviderMnemonicReactNative({
return new ProviderMnemonicBase({
account: keysMnemonic[0],
getPassword,
});
}

return await ProviderMnemonicReactNative.initialize(null, getPassword, {});
return await ProviderMnemonicBase.initialize(null, getPassword, {});
}
7 changes: 2 additions & 5 deletions src/helpers/get-provider-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
ProviderSSSReactNative,
StorageInterface,
} from '@haqq/provider-sss-react-native';
import {ProviderSSSBase, StorageInterface} from '@haqq/rn-wallet-providers';

import {AsyncLocalStorage} from '@app/services/async-local-storage';
import {Cloud} from '@app/services/cloud';
Expand All @@ -13,7 +10,7 @@ export async function getProviderStorage(
): Promise<StorageInterface> {
const storages = storage
? [storage]
: await ProviderSSSReactNative.getStoragesForAccount(accountId);
: await ProviderSSSBase.getStoragesForAccount(accountId);

const cloudEnabled = await Cloud.isEnabled();
if (
Expand Down
12 changes: 9 additions & 3 deletions src/helpers/get-wallets-from-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {ProviderInterface} from '@haqq/provider-base';
import {ProviderKeystoneReactNative} from '@haqq/provider-keystone-react-native';
import {
ProviderInterface,
ProviderKeystoneBase,
ProviderKeystoneEvm,
} from '@haqq/rn-wallet-providers';

import {ChooseAccountTabNames} from '@app/components/choose-account/choose-account';
import {Wallet} from '@app/models/wallet';
Expand Down Expand Up @@ -28,7 +31,10 @@ export async function* getWalletsFromProvider(
})[] = [];

const genHdPath = (_index: number) => {
if (provider instanceof ProviderKeystoneReactNative) {
if (
provider instanceof ProviderKeystoneBase ||
provider instanceof ProviderKeystoneEvm
) {
return provider.buildPath(_index);
}
if (mnemonicType === ChooseAccountTabNames.Basic) {
Expand Down
12 changes: 9 additions & 3 deletions src/helpers/ledger-transport-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {ProviderInterface} from '@haqq/provider-base';
import {ProviderLedgerReactNative} from '@haqq/provider-ledger-react-native';
import {
ProviderInterface,
ProviderLedgerBase,
ProviderLedgerEvm,
} from '@haqq/rn-wallet-providers';

import {ModalName, awaitForBluetooth, awaitForLedger, hideModal} from './';
import {ExtractPromiseType} from '../types';
Expand All @@ -18,7 +21,10 @@ export const ledgerTransportCbWrapper = async <ReturnType>(
): Promise<ExtractPromiseType<ReturnType>> => {
let result;
try {
if (transport instanceof ProviderLedgerReactNative) {
if (
transport instanceof ProviderLedgerBase ||
transport instanceof ProviderLedgerEvm
) {
await awaitForBluetooth();
// no need await here
awaitForLedger(transport);
Expand Down
Loading

0 comments on commit 65fd80e

Please sign in to comment.