ADD: Handle clipboard content with both bitcoin: and lightning:

Update App.js

Update App.js

Update App.js
This commit is contained in:
Marcos Rodriguez 2019-12-15 08:16:29 -05:00 committed by Overtorment
parent 8bce843901
commit 5f50be22f2
3 changed files with 123 additions and 11 deletions

100
App.js
View file

@ -117,9 +117,10 @@ export default class App extends React.Component {
wallet.chain === Chain.ONCHAIN ? wallet.weOwnAddress(clipboard) : wallet.isInvoiceGeneratedByWallet(clipboard), wallet.chain === Chain.ONCHAIN ? wallet.weOwnAddress(clipboard) : wallet.isInvoiceGeneratedByWallet(clipboard),
); );
if ( if (
!isAddressFromStoredWallet && (!isAddressFromStoredWallet &&
this.state.clipboardContent !== clipboard && this.state.clipboardContent !== clipboard &&
(this.isBitcoinAddress(clipboard) || this.isLightningInvoice(clipboard) || this.isLnUrl(clipboard)) (this.isBitcoinAddress(clipboard) || this.isLightningInvoice(clipboard) || this.isLnUrl(clipboard))) ||
this.isBothBitcoinAndLightning(clipboard)
) { ) {
this.setState({ isClipboardContentModalVisible: true }); this.setState({ isClipboardContentModalVisible: true });
} }
@ -142,6 +143,10 @@ export default class App extends React.Component {
} }
isBitcoinAddress(address) { isBitcoinAddress(address) {
address = address
.replace('bitcoin:', '')
.replace('bitcoin=', '')
.split('?')[0];
let isValidBitcoinAddress = false; let isValidBitcoinAddress = false;
try { try {
bitcoin.address.toOutputScript(address); bitcoin.address.toOutputScript(address);
@ -150,12 +155,6 @@ export default class App extends React.Component {
} catch (err) { } catch (err) {
isValidBitcoinAddress = false; isValidBitcoinAddress = false;
} }
if (!isValidBitcoinAddress) {
if (address.indexOf('bitcoin:') === 0 || address.indexOf('BITCOIN:') === 0) {
isValidBitcoinAddress = true;
this.setState({ clipboardContentModalAddressType: bitcoinModalString });
}
}
return isValidBitcoinAddress; return isValidBitcoinAddress;
} }
@ -175,12 +174,77 @@ export default class App extends React.Component {
return false; return false;
} }
isBothBitcoinAndLightning(url) {
if (url.includes('lightning') && url.includes('bitcoin')) {
const txInfo = url.split(/(bitcoin:|lightning:|lightning=|bitcoin=)+/);
let bitcoin;
let lndInvoice;
for (const [index, value] of txInfo.entries()) {
try {
// Inside try-catch. We dont wan't to crash in case of an out-of-bounds error.
if (value.startsWith('bitcoin')) {
bitcoin = `bitcoin:${txInfo[index + 1]}`;
if (!this.isBitcoinAddress(bitcoin)) {
bitcoin = false;
break;
}
} else if (value.startsWith('lightning')) {
lndInvoice = `lightning:${txInfo[index + 1]}`;
if (!this.isLightningInvoice(lndInvoice)) {
lndInvoice = false;
break;
}
}
} catch (e) {
console.log(e);
}
if (bitcoin && lndInvoice) break;
}
if (bitcoin && lndInvoice) {
this.setState({
clipboardContent: { bitcoin, lndInvoice },
});
return { bitcoin, lndInvoice };
} else {
return undefined;
}
}
return undefined;
}
isSafelloRedirect(event) { isSafelloRedirect(event) {
let urlObject = url.parse(event.url, true) // eslint-disable-line let urlObject = url.parse(event.url, true) // eslint-disable-line
return !!urlObject.query['safello-state-token']; return !!urlObject.query['safello-state-token'];
} }
isBothBitcoinAndLightningWalletSelect = wallet => {
const clipboardContent = this.state.clipboardContent;
if (wallet.chain === Chain.ONCHAIN) {
this.navigator &&
this.navigator.dispatch(
NavigationActions.navigate({
routeName: 'SendDetails',
params: {
uri: clipboardContent.bitcoin,
fromWallet: wallet,
},
}),
);
} else if (wallet.chain === Chain.OFFCHAIN) {
this.navigator &&
this.navigator.dispatch(
NavigationActions.navigate({
routeName: 'ScanLndInvoice',
params: {
uri: clipboardContent.lndInvoice,
fromSecret: wallet.getSecret(),
},
}),
);
}
};
handleOpenURL = event => { handleOpenURL = event => {
if (event.url === null) { if (event.url === null) {
return; return;
@ -188,7 +252,23 @@ export default class App extends React.Component {
if (typeof event.url !== 'string') { if (typeof event.url !== 'string') {
return; return;
} }
if (this.isBitcoinAddress(event.url)) { let isBothBitcoinAndLightning;
try {
isBothBitcoinAndLightning = this.isBothBitcoinAndLightning(event.url);
} catch (e) {
console.log(e);
}
if (isBothBitcoinAndLightning) {
this.navigator &&
this.navigator.dispatch(
NavigationActions.navigate({
routeName: 'HandleOffchainAndOnChain',
params: {
onWalletSelect: this.isBothBitcoinAndLightningWalletSelect,
},
}),
);
} else if (this.isBitcoinAddress(event.url)) {
this.navigator && this.navigator &&
this.navigator.dispatch( this.navigator.dispatch(
NavigationActions.navigate({ NavigationActions.navigate({

View file

@ -228,6 +228,32 @@ const LightningScanInvoiceStackNavigator = createStackNavigator({
}, },
}); });
const HandleOffchainAndOnChainStackNavigator = createStackNavigator(
{
SelectWallet: {
screen: SelectWallet,
},
// LND:
ScanLndInvoice: {
screen: LightningScanInvoiceStackNavigator,
navigationOptions: {
header: null,
},
},
ScanQrAddress: {
screen: ScanQRCode,
},
SendDetails: {
screen: CreateTransactionStackNavigator,
navigationOptions: {
header: null,
},
},
},
{ headerBackTitleVisible: false },
);
const MainBottomTabs = createStackNavigator( const MainBottomTabs = createStackNavigator(
{ {
Wallets: { Wallets: {
@ -311,6 +337,12 @@ const MainBottomTabs = createStackNavigator(
header: null, header: null,
}, },
}, },
HandleOffchainAndOnChain: {
screen: HandleOffchainAndOnChainStackNavigator,
navigationOptions: {
header: null,
},
},
}, },
{ {
mode: 'modal', mode: 'modal',

View file

@ -33,7 +33,7 @@ export default class SelectWallet extends Component {
componentDidMount() { componentDidMount() {
const wallets = this.chainType const wallets = this.chainType
? BlueApp.getWallets().filter(item => item.chain === this.chainType && item.allowSend()) ? BlueApp.getWallets().filter(item => item.chain === this.chainType && item.allowSend())
: BlueApp.getWallets(); : BlueApp.getWallets().filter(item => item.allowSend());
this.setState({ this.setState({
data: wallets, data: wallets,
isLoading: false, isLoading: false,