fix: import discovery ts

This commit is contained in:
Ivan Vershigora 2024-11-09 12:40:42 +00:00
parent 89f4f55fb7
commit a5da81d4e1
No known key found for this signature in database
GPG key ID: DCCF7FB5ED2CEBD7
2 changed files with 11 additions and 17 deletions

View file

@ -27,12 +27,17 @@ import type { TWallet } from './wallets/types';
// https://github.com/bitcoinjs/bip32/blob/master/ts-src/bip32.ts#L43
export const validateBip32 = (path: string) => path.match(/^(m\/)?(\d+'?\/)*\d+'?$/) !== null;
type TReturn = {
type TStatus = {
cancelled: boolean;
stopped: boolean;
wallets: TWallet[];
};
export type TImport = {
promise: Promise<TStatus>;
stop: () => void;
};
/**
* Function that starts wallet search and import process. It has async generator inside, so
* that the process can be stoped at any time. It reporst all the progress through callbacks.
@ -51,13 +56,13 @@ const startImport = (
onProgress: (name: string) => void,
onWallet: (wallet: TWallet) => void,
onPassword: (title: string, text: string) => Promise<string>,
): { promise: Promise<TReturn>; stop: () => void } => {
): TImport => {
// state
let promiseResolve: (arg: TReturn) => void;
let promiseResolve: (arg: TStatus) => void;
let promiseReject: (reason?: any) => void;
let running = true; // if you put it to false, internal generator stops
const wallets: TWallet[] = [];
const promise = new Promise<TReturn>((resolve, reject) => {
const promise = new Promise<TStatus>((resolve, reject) => {
promiseResolve = resolve;
promiseReject = reject;
});

View file

@ -4,7 +4,7 @@ import { ActivityIndicator, FlatList, LayoutAnimation, StyleSheet, View } from '
import triggerHapticFeedback, { HapticFeedbackTypes } from '../../blue_modules/hapticFeedback';
import { BlueButtonLink, BlueFormLabel, BlueSpacing10, BlueSpacing20, BlueText } from '../../BlueComponents';
import { HDSegwitBech32Wallet, WatchOnlyWallet } from '../../class';
import startImport from '../../class/wallet-import';
import startImport, { TImport } from '../../class/wallet-import';
import presentAlert from '../../components/Alert';
import Button from '../../components/Button';
import SafeArea from '../../components/SafeArea';
@ -23,17 +23,6 @@ import { keepAwake, disallowScreenshot } from 'react-native-screen-capture';
type RouteProps = RouteProp<AddWalletStackParamList, 'ImportWalletDiscovery'>;
type NavigationProp = NativeStackNavigationProp<AddWalletStackParamList, 'ImportWalletDiscovery'>;
type TReturn = {
cancelled?: boolean;
stopped?: boolean;
wallets: TWallet[];
};
type ImportTask = {
promise: Promise<TReturn>;
stop: () => void;
};
type WalletEntry = {
wallet: TWallet | THDWalletForWatchOnly;
subtitle: string;
@ -45,7 +34,7 @@ const ImportWalletDiscovery: React.FC = () => {
const { colors } = useTheme();
const route = useRoute<RouteProps>();
const { importText, askPassphrase, searchAccounts } = route.params;
const task = useRef<ImportTask | null>(null);
const task = useRef<TImport | null>(null);
const { addAndSaveWallet } = useStorage();
const [loading, setLoading] = useState<boolean>(true);
const [wallets, setWallets] = useState<WalletEntry[]>([]);