From fc772657e48d5ef9603969aa5afc62ad13ca0355 Mon Sep 17 00:00:00 2001 From: marcosrdz Date: Tue, 1 Dec 2020 00:40:54 -0500 Subject: [PATCH] WIP --- screen/send/details.js | 4 +-- screen/send/psbtMultisig.js | 41 +++++++++++++++---------------- screen/send/psbtMultisigQRCode.js | 36 +++++---------------------- 3 files changed, 28 insertions(+), 53 deletions(-) diff --git a/screen/send/details.js b/screen/send/details.js index a30c46c34..eba50d1ca 100644 --- a/screen/send/details.js +++ b/screen/send/details.js @@ -647,7 +647,7 @@ export default class SendDetails extends Component { this.props.navigation.navigate('PsbtMultisig', { memo: this.state.memo, psbtBase64: psbt.toBase64(), - walletId: wallet.getID(), + walletID: wallet.getID(), }); this.setState({ isLoading: false }); return; @@ -976,7 +976,7 @@ export default class SendDetails extends Component { this.props.navigation.navigate('PsbtMultisig', { memo: this.state.memo, psbtBase64: psbt.toBase64(), - walletId: fromWallet.getID(), + walletID: fromWallet.getID(), }); } catch (error) { alert(loc.send.problem_with_psbt + ': ' + error.message); diff --git a/screen/send/psbtMultisig.js b/screen/send/psbtMultisig.js index eff6944a3..33d37af62 100644 --- a/screen/send/psbtMultisig.js +++ b/screen/send/psbtMultisig.js @@ -17,17 +17,14 @@ const shortenAddress = addr => { const PsbtMultisig = () => { const { wallets } = useContext(BlueStorageContext); - const navigation = useNavigation(); - const route = useRoute(); + const { navigate, setParams } = useNavigation(); const { colors } = useTheme(); const [flatListHeight, setFlatListHeight] = useState(0); - - const walletID = route.params.walletId; - const psbtBase64 = route.params.psbtBase64; - const memo = route.params.memo; - const receivedPSBTBase64 = route.params.receivedPSBTBase64; - + const { walletID, psbtBase64, memo, receivedPSBTBase64 } = useRoute().params; + /** @type MultisigHDWallet */ + const wallet = wallets.find(w => w.getID() === walletID); const [psbt, setPsbt] = useState(bitcoin.Psbt.fromBase64(psbtBase64)); + const data = new Array(wallet.getM()); const stylesHook = StyleSheet.create({ root: { backgroundColor: colors.elevated, @@ -69,8 +66,7 @@ const PsbtMultisig = () => { color: colors.msSuccessBG, }, }); - /** @type MultisigHDWallet */ - const wallet = wallets.find(w => w.getID() === walletID); + let destination = []; let totalSat = 0; const targets = []; @@ -85,25 +81,21 @@ const PsbtMultisig = () => { const totalBtc = new BigNumber(totalSat).dividedBy(100000000).toNumber(); const totalFiat = currency.satoshiToLocalCurrency(totalSat); - const howManySignaturesWeHave = () => { - return wallet.calculateHowManySignaturesWeHaveFromPsbt(psbt); - }; - const getFee = () => { return wallet.calculateFeeFromPsbt(psbt); }; const _renderItem = el => { - if (el.index >= howManySignaturesWeHave()) return _renderItemUnsigned(el); + if (el.index >= howManySignaturesWeHave) return _renderItemUnsigned(el); else return _renderItemSigned(el); }; const navigateToPSBTMultisigQRCode = () => { - navigation.navigate('PsbtMultisigQRCode', { walletID, psbtBase64 }); + navigate('PsbtMultisigQRCode', { walletID, psbtBase64, isShowOpenScanner: isConfirmEnabled() }); }; const _renderItemUnsigned = el => { - const renderProvideSignature = el.index === howManySignaturesWeHave(); + const renderProvideSignature = el.index === howManySignaturesWeHave; return ( @@ -150,9 +142,16 @@ const PsbtMultisig = () => { }; // eslint-disable-next-line react-hooks/exhaustive-deps - useEffect(() => _combinePSBT, [receivedPSBTBase64]); + useEffect(() => { + if (receivedPSBTBase64) { + _combinePSBT(); + setParams({ receivedPSBTBase64: undefined }); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [receivedPSBTBase64]); const _combinePSBT = () => { + console.warn(receivedPSBTBase64); const receivedPSBT = bitcoin.Psbt.fromBase64(receivedPSBTBase64); try { const newPsbt = psbt.combine(receivedPSBT); @@ -170,7 +169,7 @@ const PsbtMultisig = () => { try { const tx = psbt.extractTransaction().toHex(); const satoshiPerByte = Math.round(getFee() / (tx.length / 2)); - navigation.navigate('Confirm', { + navigate('Confirm', { fee: new BigNumber(getFee()).dividedBy(100000000).toNumber(), memo: memo, fromWallet: wallet, @@ -183,8 +182,9 @@ const PsbtMultisig = () => { } }; + const howManySignaturesWeHave = wallet.calculateHowManySignaturesWeHaveFromPsbt(psbt); const isConfirmEnabled = () => { - return howManySignaturesWeHave() >= wallet.getM(); + return howManySignaturesWeHave >= wallet.getM(); }; const destinationAddress = () => { @@ -252,7 +252,6 @@ const PsbtMultisig = () => { setFlatListHeight(e.nativeEvent.layout.height); }; - const data = new Array(wallet.getM()); return ( diff --git a/screen/send/psbtMultisigQRCode.js b/screen/send/psbtMultisigQRCode.js index 2d8fcf5f0..21863ea0b 100644 --- a/screen/send/psbtMultisigQRCode.js +++ b/screen/send/psbtMultisigQRCode.js @@ -1,7 +1,7 @@ /* global alert */ -import React, { useContext, useState } from 'react'; +import React, { useState } from 'react'; import { ActivityIndicator, Platform, ScrollView, StyleSheet, View } from 'react-native'; -import { BlueButtonLink, BlueNavigationStyle, BlueSpacing20, SafeBlueArea } from '../../BlueComponents'; +import { BlueNavigationStyle, BlueSpacing20, SafeBlueArea } from '../../BlueComponents'; import { DynamicQRCode } from '../../components/DynamicQRCode'; import { SquareButton } from '../../components/SquareButton'; import { getSystemName } from 'react-native-device-info'; @@ -9,17 +9,15 @@ import loc from '../../loc'; import ImagePicker from 'react-native-image-picker'; import ScanQRCode from './ScanQRCode'; import { useNavigation, useRoute, useTheme } from '@react-navigation/native'; -import { BlueStorageContext } from '../../blue_modules/storage-context'; const bitcoin = require('bitcoinjs-lib'); const fs = require('../../blue_modules/fs'); const LocalQRCode = require('@remobile/react-native-qrcode-local-image'); const isDesktop = getSystemName() === 'Mac OS X'; const PsbtMultisigQRCode = () => { - const { wallets } = useContext(BlueStorageContext); - const { navigate, goBack } = useNavigation(); + const { navigate } = useNavigation(); const { colors } = useTheme(); - const { walletID, psbtBase64 } = useRoute().params; + const { psbtBase64, isShowOpenScanner } = useRoute().params; const [isLoading, setIsLoading] = useState(false); const psbt = bitcoin.Psbt.fromBase64(psbtBase64); @@ -34,24 +32,8 @@ const PsbtMultisigQRCode = () => { backgroundColor: colors.buttonDisabledBackgroundColor, }, }); - /** @type MultisigHDWallet */ - const wallet = wallets.find(w => w.getID() === walletID); const fileName = `${Date.now()}.psbt`; - const howManySignaturesWeHave = () => { - return wallet.calculateHowManySignaturesWeHaveFromPsbt(psbt); - }; - - const _combinePSBT = receivedPSBTBase64 => { - try { - const receivedPSBT = bitcoin.Psbt.fromBase64(receivedPSBTBase64); - const newPsbt = psbt.combine(receivedPSBT); - navigate('PsbtMultisig', { receivedPSBTBase64: newPsbt }); - } catch (error) { - alert(error); - } - }; - const onBarScanned = ret => { if (!ret.data) ret = { data: ret }; if (ret.data.toUpperCase().startsWith('UR')) { @@ -61,7 +43,7 @@ const PsbtMultisigQRCode = () => { // we dont support it in this flow } else { // psbt base64? - _combinePSBT(ret.data); + navigate('PsbtMultisig', { receivedPSBTBase64: ret.data }); } }; @@ -104,16 +86,12 @@ const PsbtMultisigQRCode = () => { setTimeout(() => fs.writeFileAndExport(fileName, psbt.toBase64()).finally(() => setIsLoading(false)), 10); }; - const isConfirmEnabled = () => { - return howManySignaturesWeHave() >= wallet.getM(); - }; - return ( - {!isConfirmEnabled() && ( + {!isShowOpenScanner && ( <> { ) : ( )} - -