BlueWallet/screen/wallets/add.js

528 lines
16 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useContext, useReducer } from 'react';
import {
Text,
ScrollView,
ActivityIndicator,
Keyboard,
KeyboardAvoidingView,
Platform,
View,
TextInput,
StyleSheet,
2021-09-27 13:18:06 -04:00
useColorScheme,
} from 'react-native';
2020-12-15 22:15:57 -05:00
import AsyncStorage from '@react-native-async-storage/async-storage';
2018-07-14 19:54:27 +01:00
import {
2020-11-22 03:04:04 -05:00
BlueText,
2020-09-21 21:53:28 -04:00
BlueListItem,
2018-07-14 19:54:27 +01:00
LightningButton,
BitcoinButton,
VaultButton,
2018-07-14 19:54:27 +01:00
BlueFormLabel,
2020-11-29 23:18:54 -05:00
BlueButtonLink,
2018-10-31 20:34:12 +00:00
BlueSpacing20,
2018-07-14 19:54:27 +01:00
} from '../../BlueComponents';
2020-12-25 19:09:53 +03:00
import navigationStyle from '../../components/navigationStyle';
2023-04-21 16:39:12 +01:00
import { HDSegwitBech32Wallet, SegwitP2SHWallet, HDSegwitP2SHWallet, LightningCustodianWallet, LightningLdkWallet } from '../../class';
2018-12-13 11:50:18 -05:00
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
2023-10-23 21:28:44 -04:00
import { useNavigation } from '@react-navigation/native';
import { Chain } from '../../models/bitcoinUnits';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2021-09-09 12:00:11 +01:00
import { LdkButton } from '../../components/LdkButton';
import alert from '../../components/Alert';
2023-10-21 19:20:18 -04:00
import useAsyncPromise from '../../hooks/useAsyncPromise';
2023-10-23 21:28:44 -04:00
import { useTheme } from '../../components/themes';
2023-11-15 08:07:54 -04:00
import Button from '../../components/Button';
2023-04-21 16:39:12 +01:00
const BlueApp = require('../../BlueApp');
const AppStorage = BlueApp.AppStorage;
const A = require('../../blue_modules/analytics');
const ButtonSelected = Object.freeze({
ONCHAIN: Chain.ONCHAIN,
OFFCHAIN: Chain.OFFCHAIN,
VAULT: 'VAULT',
2021-09-09 12:00:11 +01:00
LDK: 'LDK',
});
const initialState = {
isLoading: true,
walletBaseURI: '',
selectedIndex: 0,
label: '',
selectedWalletType: ButtonSelected.ONCHAIN,
backdoorPressed: 1,
entropy: undefined,
entropyButtonText: loc.wallets.add_entropy_provide,
};
const walletReducer = (state, action) => {
switch (action.type) {
case 'SET_LOADING':
return { ...state, isLoading: action.payload };
case 'SET_WALLET_BASE_URI':
return { ...state, walletBaseURI: action.payload };
case 'SET_SELECTED_INDEX':
return { ...state, selectedIndex: action.payload };
case 'SET_LABEL':
return { ...state, label: action.payload };
case 'SET_SELECTED_WALLET_TYPE':
return { ...state, selectedWalletType: action.payload };
case 'INCREMENT_BACKDOOR_PRESSED':
return { ...state, backdoorPressed: state.backdoorPressed + 1 };
case 'SET_ENTROPY':
return { ...state, entropy: action.payload };
case 'SET_ENTROPY_BUTTON_TEXT':
return { ...state, entropyButtonText: action.payload };
default:
return state;
}
};
const WalletsAdd = () => {
const { colors } = useTheme();
// State
const [state, dispatch] = useReducer(walletReducer, initialState);
const isLoading = state.isLoading;
const walletBaseURI = state.walletBaseURI;
const selectedIndex = state.selectedIndex;
const label = state.label;
const selectedWalletType = state.selectedWalletType;
const backdoorPressed = state.backdoorPressed;
const entropy = state.entropy;
const entropyButtonText = state.entropyButtonText;
//
2023-10-20 11:15:54 -04:00
const colorScheme = useColorScheme();
2023-03-04 11:30:51 -04:00
const { addWallet, saveToDisk, isAdvancedModeEnabled, wallets } = useContext(BlueStorageContext);
2023-10-22 08:25:48 -04:00
const isAdvancedOptionsEnabled = useAsyncPromise(isAdvancedModeEnabled);
2023-10-20 11:15:54 -04:00
const { navigate, goBack, setOptions } = useNavigation();
const stylesHook = {
advancedText: {
color: colors.feeText,
},
label: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
noPadding: {
backgroundColor: colors.elevated,
},
root: {
backgroundColor: colors.elevated,
},
lndUri: {
borderColor: colors.formBorder,
borderBottomColor: colors.formBorder,
backgroundColor: colors.inputBackgroundColor,
},
2019-05-19 20:49:42 +01:00
};
useEffect(() => {
AsyncStorage.getItem(AppStorage.LNDHUB)
2023-12-14 12:52:15 -04:00
.then(url => setWalletBaseURI(url))
.catch(() => setWalletBaseURI(''))
.finally(() => setIsLoading(false));
}, []);
2018-01-30 22:42:38 +00:00
2023-10-20 11:15:54 -04:00
useEffect(() => {
setOptions({
statusBarStyle: Platform.select({ ios: 'light', default: colorScheme === 'dark' ? 'light' : 'dark' }),
});
}, [colorScheme, setOptions]);
const entropyGenerated = newEntropy => {
2020-06-29 15:58:43 +03:00
let entropyTitle;
if (!newEntropy) {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.wallets.add_entropy_provide;
} else if (newEntropy.length < 32) {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.formatString(loc.wallets.add_entropy_remain, {
gen: newEntropy.length,
rem: 32 - newEntropy.length,
2020-06-29 15:58:43 +03:00
});
} else {
2020-07-20 16:38:46 +03:00
entropyTitle = loc.formatString(loc.wallets.add_entropy_generated, {
gen: newEntropy.length,
2020-06-29 15:58:43 +03:00
});
}
setEntropy(newEntropy);
setEntropyButtonText(entropyTitle);
};
2018-07-14 19:54:27 +01:00
const setIsLoading = value => {
dispatch({ type: 'SET_LOADING', payload: value });
};
const setWalletBaseURI = value => {
dispatch({ type: 'SET_WALLET_BASE_URI', payload: value });
};
const setSelectedIndex = value => {
dispatch({ type: 'SET_SELECTED_INDEX', payload: value });
};
const setLabel = value => {
dispatch({ type: 'SET_LABEL', payload: value });
};
const setSelectedWalletType = value => {
dispatch({ type: 'SET_SELECTED_WALLET_TYPE', payload: value });
};
const setBackdoorPressed = value => {
dispatch({ type: 'INCREMENT_BACKDOOR_PRESSED', payload: value });
};
const setEntropy = value => {
dispatch({ type: 'SET_ENTROPY', payload: value });
};
const setEntropyButtonText = value => {
dispatch({ type: 'SET_ENTROPY_BUTTON_TEXT', payload: value });
};
const createWallet = async () => {
setIsLoading(true);
2020-08-07 07:31:01 -04:00
let w;
2020-07-20 16:38:46 +03:00
if (selectedWalletType === ButtonSelected.OFFCHAIN) {
createLightningWallet(w);
} else if (selectedWalletType === ButtonSelected.ONCHAIN) {
if (selectedIndex === 2) {
// zero index radio - HD segwit
w = new HDSegwitP2SHWallet();
w.setLabel(label || loc.wallets.details_title);
} else if (selectedIndex === 1) {
// btc was selected
// index 1 radio - segwit single address
w = new SegwitP2SHWallet();
w.setLabel(label || loc.wallets.details_title);
} else {
// btc was selected
// index 2 radio - hd bip84
w = new HDSegwitBech32Wallet();
w.setLabel(label || loc.wallets.details_title);
}
if (selectedWalletType === ButtonSelected.ONCHAIN) {
if (entropy) {
try {
await w.generateFromEntropy(entropy);
} catch (e) {
console.log(e.toString());
alert(e.toString());
goBack();
return;
}
} else {
await w.generate();
}
addWallet(w);
await saveToDisk();
A(A.ENUM.CREATED_WALLET);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
if (w.type === HDSegwitP2SHWallet.type || w.type === HDSegwitBech32Wallet.type) {
navigate('PleaseBackup', {
2020-10-29 05:26:39 -04:00
walletID: w.getID(),
});
} else {
goBack();
}
}
} else if (selectedWalletType === ButtonSelected.VAULT) {
setIsLoading(false);
2021-01-09 23:43:40 -05:00
navigate('WalletsAddMultisig', { walletLabel: label.trim().length > 0 ? label : loc.multisig.default_label });
2021-09-09 12:00:11 +01:00
} else if (selectedWalletType === ButtonSelected.LDK) {
setIsLoading(false);
createLightningLdkWallet(w);
}
};
const createLightningLdkWallet = async wallet => {
const foundLdk = wallets.find(w => w.type === LightningLdkWallet.type);
if (foundLdk) {
return alert('LDK wallet already exists');
}
2021-09-09 12:00:11 +01:00
setIsLoading(true);
wallet = new LightningLdkWallet();
wallet.setLabel(label || loc.wallets.details_title);
await wallet.generate();
await wallet.init();
setIsLoading(false);
addWallet(wallet);
await saveToDisk();
A(A.ENUM.CREATED_WALLET);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
navigate('PleaseBackupLdk', {
walletID: wallet.getID(),
});
};
const createLightningWallet = async wallet => {
wallet = new LightningCustodianWallet();
wallet.setLabel(label || loc.wallets.details_title);
try {
2021-07-08 13:39:03 -04:00
const lndhub = walletBaseURI?.trim();
if (lndhub) {
const isValidNodeAddress = await LightningCustodianWallet.isValidNodeAddress(lndhub);
if (isValidNodeAddress) {
wallet.setBaseURI(lndhub);
await wallet.init();
} else {
throw new Error('The provided node address is not valid LNDHub node.');
}
}
await wallet.createAccount();
await wallet.authorize();
} catch (Err) {
setIsLoading(false);
console.warn('lnd create failure', Err);
if (Err.message) {
return alert(Err.message);
} else {
return alert(loc.wallets.add_lndhub_error);
}
// giving app, not adding anything
}
A(A.ENUM.CREATED_LIGHTNING_WALLET);
await wallet.generate();
addWallet(wallet);
await saveToDisk();
A(A.ENUM.CREATED_WALLET);
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
navigate('PleaseBackupLNDHub', {
2020-12-02 20:36:48 -05:00
walletID: wallet.getID(),
});
};
2020-08-07 07:31:01 -04:00
const navigateToEntropy = () => {
navigate('ProvideEntropy', { onGenerated: entropyGenerated });
};
2020-08-07 07:31:01 -04:00
const navigateToImportWallet = () => {
navigate('ImportWallet');
};
const handleOnVaultButtonPressed = () => {
2020-12-07 22:25:00 +00:00
Keyboard.dismiss();
setSelectedWalletType(ButtonSelected.VAULT);
};
const handleOnBitcoinButtonPressed = () => {
Keyboard.dismiss();
setSelectedWalletType(ButtonSelected.ONCHAIN);
};
2020-08-07 07:31:01 -04:00
const handleOnLightningButtonPressed = () => {
2021-09-09 12:00:11 +01:00
setBackdoorPressed(prevState => {
return prevState + 1;
});
Keyboard.dismiss();
setSelectedWalletType(ButtonSelected.OFFCHAIN);
};
2021-09-09 12:00:11 +01:00
const handleOnLdkButtonPressed = async () => {
Keyboard.dismiss();
setSelectedWalletType(ButtonSelected.LDK);
};
return (
2023-11-22 00:25:37 -04:00
<ScrollView style={stylesHook.root} testID="ScrollView">
<BlueSpacing20 />
<KeyboardAvoidingView enabled behavior={Platform.OS === 'ios' ? 'padding' : null} keyboardVerticalOffset={62}>
<BlueFormLabel>{loc.wallets.add_wallet_name}</BlueFormLabel>
<View style={[styles.label, stylesHook.label]}>
<TextInput
testID="WalletNameInput"
value={label}
placeholderTextColor="#81868e"
2021-10-17 10:59:02 -04:00
placeholder={loc.wallets.add_placeholder}
onChangeText={setLabel}
style={styles.textInputCommon}
editable={!isLoading}
underlineColorAndroid="transparent"
/>
</View>
<BlueFormLabel>{loc.wallets.add_wallet_type}</BlueFormLabel>
<View style={styles.buttons}>
<BitcoinButton
testID="ActivateBitcoinButton"
active={selectedWalletType === ButtonSelected.ONCHAIN}
onPress={handleOnBitcoinButtonPressed}
style={styles.button}
/>
<LightningButton
active={selectedWalletType === ButtonSelected.OFFCHAIN}
onPress={handleOnLightningButtonPressed}
style={styles.button}
/>
2021-09-09 12:00:11 +01:00
{backdoorPressed > 10 ? (
2021-09-24 15:50:06 +01:00
<LdkButton
active={selectedWalletType === ButtonSelected.LDK}
onPress={handleOnLdkButtonPressed}
style={styles.button}
subtext={LightningLdkWallet.getPackageVersion()}
text="LDK"
/>
2021-09-09 12:00:11 +01:00
) : null}
2020-12-07 22:25:00 +00:00
<VaultButton active={selectedWalletType === ButtonSelected.VAULT} onPress={handleOnVaultButtonPressed} style={styles.button} />
</View>
<View style={styles.advanced}>
{(() => {
2023-10-21 19:01:24 -04:00
if (selectedWalletType === ButtonSelected.ONCHAIN && isAdvancedOptionsEnabled.data) {
return (
<View>
<BlueSpacing20 />
<Text style={[styles.advancedText, stylesHook.advancedText]}>{loc.settings.advanced_options}</Text>
2020-09-21 21:53:28 -04:00
<BlueListItem
containerStyle={[styles.noPadding, stylesHook.noPadding]}
bottomDivider={false}
onPress={() => setSelectedIndex(0)}
title={HDSegwitBech32Wallet.typeReadable}
checkmark={selectedIndex === 0}
/>
2020-09-21 21:53:28 -04:00
<BlueListItem
containerStyle={[styles.noPadding, stylesHook.noPadding]}
bottomDivider={false}
onPress={() => setSelectedIndex(1)}
title={SegwitP2SHWallet.typeReadable}
checkmark={selectedIndex === 1}
/>
2020-09-21 21:53:28 -04:00
<BlueListItem
containerStyle={[styles.noPadding, stylesHook.noPadding]}
bottomDivider={false}
onPress={() => setSelectedIndex(2)}
title={HDSegwitP2SHWallet.typeReadable}
checkmark={selectedIndex === 2}
/>
</View>
);
2021-07-08 13:39:03 -04:00
} else if (selectedWalletType === ButtonSelected.OFFCHAIN) {
return (
<>
<BlueSpacing20 />
2020-09-21 21:53:28 -04:00
<Text style={[styles.advancedText, stylesHook.advancedText]}>{loc.settings.advanced_options}</Text>
<BlueSpacing20 />
2020-12-21 21:27:49 +03:00
<BlueText>{loc.wallets.add_lndhub}</BlueText>
<View style={[styles.lndUri, stylesHook.lndUri]}>
<TextInput
value={walletBaseURI}
onChangeText={setWalletBaseURI}
onSubmitEditing={Keyboard.dismiss}
2020-12-21 21:27:49 +03:00
placeholder={loc.wallets.add_lndhub_placeholder}
clearButtonMode="while-editing"
autoCapitalize="none"
textContentType="URL"
autoCorrect={false}
placeholderTextColor="#81868e"
style={styles.textInputCommon}
editable={!isLoading}
underlineColorAndroid="transparent"
/>
</View>
</>
);
}
})()}
2023-10-21 19:01:24 -04:00
{isAdvancedOptionsEnabled.data && selectedWalletType === ButtonSelected.ONCHAIN && !isLoading && (
2020-11-29 23:18:54 -05:00
<BlueButtonLink style={styles.import} title={entropyButtonText} onPress={navigateToEntropy} />
)}
2020-11-05 12:38:04 +01:00
<BlueSpacing20 />
<View style={styles.createButton}>
{!isLoading ? (
2023-11-15 08:07:54 -04:00
<Button
2021-07-08 13:39:03 -04:00
testID="Create"
title={loc.wallets.add_create}
disabled={!selectedWalletType || (selectedWalletType === Chain.OFFCHAIN && (walletBaseURI ?? '').trim().length === 0)}
onPress={createWallet}
/>
) : (
<ActivityIndicator />
)}
</View>
{!isLoading && (
2020-11-29 23:18:54 -05:00
<BlueButtonLink
2020-07-15 13:32:59 -04:00
testID="ImportWallet"
style={styles.import}
2020-07-20 16:38:46 +03:00
title={loc.wallets.add_import_wallet}
onPress={navigateToImportWallet}
2020-07-15 13:32:59 -04:00
/>
)}
</View>
</KeyboardAvoidingView>
</ScrollView>
);
};
2018-03-18 02:48:23 +00:00
2021-02-15 11:03:54 +03:00
WalletsAdd.navigationOptions = navigationStyle(
{
closeButton: true,
2023-11-11 07:33:50 -04:00
headerBackVisible: false,
2021-02-15 11:03:54 +03:00
},
opts => ({ ...opts, title: loc.wallets.add_title }),
);
2020-05-30 01:30:43 -04:00
const styles = StyleSheet.create({
2020-11-05 12:38:04 +01:00
createButton: {
flex: 1,
},
label: {
flexDirection: 'row',
borderWidth: 1,
borderBottomWidth: 0.5,
minHeight: 44,
height: 44,
marginHorizontal: 20,
alignItems: 'center',
marginVertical: 16,
borderRadius: 4,
},
textInputCommon: {
flex: 1,
marginHorizontal: 8,
color: '#81868e',
},
buttons: {
flexDirection: 'column',
marginHorizontal: 20,
marginTop: 16,
borderWidth: 0,
minHeight: 100,
},
button: {
width: '100%',
height: 'auto',
},
advanced: {
marginHorizontal: 20,
},
advancedText: {
fontWeight: '500',
},
lndUri: {
flexDirection: 'row',
borderWidth: 1,
borderBottomWidth: 0.5,
minHeight: 44,
height: 44,
alignItems: 'center',
marginVertical: 16,
borderRadius: 4,
},
import: {
marginBottom: 0,
marginTop: 24,
},
noPadding: {
paddingHorizontal: 0,
},
});
export default WalletsAdd;