2020-12-01 04:31:45 +01:00
|
|
|
/* global alert */
|
2020-12-01 06:40:54 +01:00
|
|
|
import React, { useState } from 'react';
|
2020-12-11 04:28:54 +01:00
|
|
|
import { ActivityIndicator, ScrollView, StyleSheet, View } from 'react-native';
|
2020-12-04 14:39:47 +01:00
|
|
|
import { BlueNavigationStyle, BlueSpacing20, SafeBlueArea } from '../../BlueComponents';
|
2020-12-01 04:31:45 +01:00
|
|
|
import { DynamicQRCode } from '../../components/DynamicQRCode';
|
|
|
|
import { SquareButton } from '../../components/SquareButton';
|
2020-12-04 14:39:47 +01:00
|
|
|
import { getSystemName } from 'react-native-device-info';
|
2020-12-01 04:31:45 +01:00
|
|
|
import loc from '../../loc';
|
2020-12-04 14:39:47 +01:00
|
|
|
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
|
2020-12-09 22:34:48 +01:00
|
|
|
import ActionSheet from '../ActionSheet';
|
2020-12-01 04:31:45 +01:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2020-12-09 22:34:48 +01:00
|
|
|
|
2020-12-01 04:31:45 +01:00
|
|
|
const fs = require('../../blue_modules/fs');
|
|
|
|
const isDesktop = getSystemName() === 'Mac OS X';
|
|
|
|
|
|
|
|
const PsbtMultisigQRCode = () => {
|
2020-12-01 06:40:54 +01:00
|
|
|
const { navigate } = useNavigation();
|
2020-12-01 04:31:45 +01:00
|
|
|
const { colors } = useTheme();
|
2020-12-01 06:40:54 +01:00
|
|
|
const { psbtBase64, isShowOpenScanner } = useRoute().params;
|
2020-12-01 04:31:45 +01:00
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
const psbt = bitcoin.Psbt.fromBase64(psbtBase64);
|
|
|
|
const stylesHook = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
modalContentShort: {
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
exportButton: {
|
|
|
|
backgroundColor: colors.buttonDisabledBackgroundColor,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const fileName = `${Date.now()}.psbt`;
|
|
|
|
|
|
|
|
const onBarScanned = ret => {
|
|
|
|
if (!ret.data) ret = { data: ret };
|
|
|
|
if (ret.data.toUpperCase().startsWith('UR')) {
|
|
|
|
alert('BC-UR not decoded. This should never happen');
|
|
|
|
} else if (ret.data.indexOf('+') === -1 && ret.data.indexOf('=') === -1 && ret.data.indexOf('=') === -1) {
|
|
|
|
// this looks like NOT base64, so maybe its transaction's hex
|
|
|
|
// we dont support it in this flow
|
2020-12-09 22:34:48 +01:00
|
|
|
alert(loc.wallets.import_error);
|
2020-12-01 04:31:45 +01:00
|
|
|
} else {
|
|
|
|
// psbt base64?
|
2020-12-01 06:40:54 +01:00
|
|
|
navigate('PsbtMultisig', { receivedPSBTBase64: ret.data });
|
2020-12-01 04:31:45 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-11 04:16:59 +01:00
|
|
|
const showActionSheet = () => {
|
|
|
|
const options = [loc._.cancel, loc.wallets.take_photo, loc.wallets.list_long_choose, loc.wallets.import_file];
|
2020-12-09 22:34:48 +01:00
|
|
|
|
2020-12-11 04:16:59 +01:00
|
|
|
ActionSheet.showActionSheetWithOptions({ options, cancelButtonIndex: 0 }, async buttonIndex => {
|
|
|
|
if (buttonIndex === 1) {
|
2020-12-11 04:28:54 +01:00
|
|
|
fs.takePhotoWithImagePickerAndReadPhoto.then(onBarScanned);
|
2020-12-11 04:16:59 +01:00
|
|
|
} else if (buttonIndex === 2) {
|
2020-12-11 04:28:54 +01:00
|
|
|
fs.showImagePickerAndReadImage(onBarScanned).catch(error => alert(error.message));
|
2020-12-11 04:16:59 +01:00
|
|
|
} else if (buttonIndex === 3) {
|
|
|
|
const { data } = await fs.showFilePickerAndReadFile();
|
|
|
|
if (data) {
|
|
|
|
onBarScanned({ data });
|
2020-12-09 22:34:48 +01:00
|
|
|
}
|
2020-12-11 04:16:59 +01:00
|
|
|
}
|
|
|
|
});
|
2020-12-09 22:34:48 +01:00
|
|
|
};
|
|
|
|
|
2020-12-01 04:31:45 +01:00
|
|
|
const openScanner = () => {
|
|
|
|
if (isDesktop) {
|
2020-12-09 22:34:48 +01:00
|
|
|
showActionSheet();
|
2020-12-01 04:31:45 +01:00
|
|
|
} else {
|
|
|
|
navigate('ScanQRCodeRoot', {
|
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params: {
|
|
|
|
onBarScanned: onBarScanned,
|
|
|
|
showFileImportButton: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const exportPSBT = () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
setTimeout(() => fs.writeFileAndExport(fileName, psbt.toBase64()).finally(() => setIsLoading(false)), 10);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeBlueArea style={[styles.root, stylesHook.root]}>
|
|
|
|
<ScrollView centerContent contentContainerStyle={styles.scrollViewContent}>
|
|
|
|
<View style={[styles.modalContentShort, stylesHook.modalContentShort]}>
|
|
|
|
<DynamicQRCode value={psbt.toHex()} capacity={666} />
|
2020-12-01 06:40:54 +01:00
|
|
|
{!isShowOpenScanner && (
|
2020-12-01 04:31:45 +01:00
|
|
|
<>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<SquareButton
|
|
|
|
testID="CosignedScanOrImportFile"
|
|
|
|
style={[styles.exportButton, stylesHook.exportButton]}
|
|
|
|
onPress={openScanner}
|
|
|
|
title={loc.multisig.scan_or_import_file}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<BlueSpacing20 />
|
|
|
|
{isLoading ? (
|
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
|
|
|
<SquareButton style={[styles.exportButton, stylesHook.exportButton]} onPress={exportPSBT} title={loc.multisig.share} />
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
scrollViewContent: {
|
|
|
|
flexGrow: 1,
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
|
|
|
modalContentShort: {
|
|
|
|
marginLeft: 20,
|
|
|
|
marginRight: 20,
|
|
|
|
},
|
|
|
|
copyToClipboard: {
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center',
|
|
|
|
},
|
|
|
|
exportButton: {
|
|
|
|
height: 48,
|
|
|
|
borderRadius: 8,
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingHorizontal: 16,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-04 14:39:47 +01:00
|
|
|
PsbtMultisigQRCode.navigationOptions = () => ({
|
|
|
|
...BlueNavigationStyle(null, false),
|
2020-12-01 04:31:45 +01:00
|
|
|
title: loc.multisig.header,
|
|
|
|
});
|
|
|
|
|
|
|
|
export default PsbtMultisigQRCode;
|