2018-09-01 01:28:19 +02:00
|
|
|
/* global alert */
|
|
|
|
import React from 'react';
|
2019-02-26 02:33:29 +01:00
|
|
|
import { Text, ActivityIndicator, View, TouchableWithoutFeedback, TouchableOpacity, Keyboard } from 'react-native';
|
2018-09-01 01:28:19 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2019-01-24 08:36:01 +01:00
|
|
|
import {
|
|
|
|
BlueButton,
|
|
|
|
SafeBlueArea,
|
|
|
|
BlueCard,
|
2019-02-26 02:33:29 +01:00
|
|
|
BlueDismissKeyboardInputAccessory,
|
2019-01-24 08:36:01 +01:00
|
|
|
BlueNavigationStyle,
|
|
|
|
BlueAddressInput,
|
|
|
|
BlueBitcoinAmount,
|
|
|
|
} from '../../BlueComponents';
|
2018-12-24 16:29:33 +01:00
|
|
|
import { LightningCustodianWallet } from '../../class/lightning-custodian-wallet';
|
2019-02-26 02:33:29 +01:00
|
|
|
import { BitcoinUnit, Chain } from '../../models/bitcoinUnits';
|
|
|
|
import { Icon } from 'react-native-elements';
|
2019-03-05 02:14:28 +01:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2019-09-25 05:42:21 +02:00
|
|
|
import Biometric from '../../class/biometrics';
|
2018-09-01 01:28:19 +02:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
let BlueApp = require('../../BlueApp');
|
2018-10-03 02:36:00 +02:00
|
|
|
let EV = require('../../events');
|
2019-05-02 22:33:03 +02:00
|
|
|
const loc = require('../../loc');
|
2018-09-01 01:28:19 +02:00
|
|
|
|
|
|
|
export default class ScanLndInvoice extends React.Component {
|
2018-12-25 21:53:24 +01:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
...BlueNavigationStyle(navigation, true),
|
|
|
|
title: loc.send.header,
|
|
|
|
headerLeft: null,
|
|
|
|
});
|
2018-09-01 01:28:19 +02:00
|
|
|
|
|
|
|
state = {
|
|
|
|
isLoading: false,
|
2019-01-06 10:30:32 +01:00
|
|
|
isAmountInitiallyEmpty: false,
|
2019-02-26 02:33:29 +01:00
|
|
|
renderWalletSelectionButtonHidden: false,
|
2018-09-01 01:28:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-12-29 19:24:51 +01:00
|
|
|
|
|
|
|
if (!BlueApp.getWallets().some(item => item.type === LightningCustodianWallet.type)) {
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2018-12-29 19:24:51 +01:00
|
|
|
alert('Before paying a Lightning invoice, you must first add a Lightning wallet.');
|
2019-01-25 21:22:51 +01:00
|
|
|
props.navigation.dismiss();
|
2018-12-29 19:24:51 +01:00
|
|
|
} else {
|
|
|
|
let fromSecret;
|
|
|
|
if (props.navigation.state.params.fromSecret) fromSecret = props.navigation.state.params.fromSecret;
|
|
|
|
let fromWallet = {};
|
|
|
|
|
|
|
|
if (!fromSecret) {
|
|
|
|
const lightningWallets = BlueApp.getWallets().filter(item => item.type === LightningCustodianWallet.type);
|
|
|
|
if (lightningWallets.length > 0) {
|
|
|
|
fromSecret = lightningWallets[0].getSecret();
|
2019-01-15 21:18:08 +01:00
|
|
|
console.warn('warning: using ln wallet index 0');
|
2018-12-29 19:24:51 +01:00
|
|
|
}
|
2018-12-24 16:29:33 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:24:51 +01:00
|
|
|
for (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.getSecret() === fromSecret) {
|
|
|
|
fromWallet = w;
|
|
|
|
break;
|
|
|
|
}
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 19:24:51 +01:00
|
|
|
this.state = {
|
|
|
|
fromWallet,
|
|
|
|
fromSecret,
|
|
|
|
destination: '',
|
|
|
|
};
|
|
|
|
}
|
2018-12-11 23:52:46 +01:00
|
|
|
}
|
2018-10-23 00:51:30 +02:00
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
async componentDidMount() {
|
2019-02-26 02:33:29 +01:00
|
|
|
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
|
|
|
|
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
|
2018-12-24 16:29:33 +01:00
|
|
|
if (this.props.navigation.state.params.uri) {
|
|
|
|
this.processTextForInvoice(this.props.navigation.getParam('uri'));
|
|
|
|
}
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
|
2019-02-26 02:33:29 +01:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.keyboardDidShowListener.remove();
|
|
|
|
this.keyboardDidHideListener.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
_keyboardDidShow = () => {
|
|
|
|
this.setState({ renderWalletSelectionButtonHidden: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
_keyboardDidHide = () => {
|
|
|
|
this.setState({ renderWalletSelectionButtonHidden: false });
|
|
|
|
};
|
|
|
|
|
2019-01-24 08:36:01 +01:00
|
|
|
processInvoice = data => {
|
2019-01-06 21:21:04 +01:00
|
|
|
this.setState({ isLoading: true }, async () => {
|
|
|
|
if (!this.state.fromWallet) {
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-01-06 21:21:04 +01:00
|
|
|
alert('Before paying a Lightning invoice, you must first add a Lightning wallet.');
|
|
|
|
return this.props.navigation.goBack();
|
|
|
|
}
|
|
|
|
|
2019-01-19 22:18:19 +01:00
|
|
|
// handling BIP21 w/BOLT11 support
|
|
|
|
let ind = data.indexOf('lightning=');
|
|
|
|
if (ind !== -1) {
|
|
|
|
data = data.substring(ind + 10).split('&')[0];
|
|
|
|
}
|
|
|
|
|
2019-01-06 21:21:04 +01:00
|
|
|
data = data.replace('LIGHTNING:', '').replace('lightning:', '');
|
|
|
|
console.log(data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {LightningCustodianWallet}
|
|
|
|
*/
|
|
|
|
let w = this.state.fromWallet;
|
|
|
|
let decoded;
|
|
|
|
try {
|
|
|
|
decoded = await w.decodeInvoice(data);
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-06 21:21:04 +01:00
|
|
|
let expiresIn = (decoded.timestamp * 1 + decoded.expiry * 1) * 1000; // ms
|
|
|
|
if (+new Date() > expiresIn) {
|
|
|
|
expiresIn = 'expired';
|
|
|
|
} else {
|
|
|
|
expiresIn = Math.round((expiresIn - +new Date()) / (60 * 1000)) + ' min';
|
|
|
|
}
|
|
|
|
Keyboard.dismiss();
|
|
|
|
this.setState({
|
|
|
|
invoice: data,
|
|
|
|
decoded,
|
|
|
|
expiresIn,
|
|
|
|
destination: data,
|
|
|
|
isAmountInitiallyEmpty: decoded.num_satoshis === '0',
|
|
|
|
isLoading: false,
|
|
|
|
});
|
|
|
|
} catch (Err) {
|
2019-02-10 12:39:26 +01:00
|
|
|
Keyboard.dismiss();
|
2019-01-06 21:21:04 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-01-06 21:21:04 +01:00
|
|
|
alert(Err.message);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
2019-01-06 21:21:04 +01:00
|
|
|
});
|
2019-01-24 08:36:01 +01:00
|
|
|
};
|
2018-09-01 01:28:19 +02:00
|
|
|
|
|
|
|
async pay() {
|
2018-10-23 00:51:30 +02:00
|
|
|
if (!this.state.hasOwnProperty('decoded')) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-09-25 05:42:21 +02:00
|
|
|
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
|
|
|
|
|
|
|
|
if (isBiometricsEnabled) {
|
|
|
|
if (!(await Biometric.unlockWithBiometrics())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-05 01:10:59 +01:00
|
|
|
this.setState(
|
|
|
|
{
|
2019-01-06 08:28:23 +01:00
|
|
|
isLoading: true,
|
2019-01-05 01:10:59 +01:00
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
let decoded = this.state.decoded;
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-05 01:10:59 +01:00
|
|
|
/** @type {LightningCustodianWallet} */
|
|
|
|
let fromWallet = this.state.fromWallet;
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-05 01:10:59 +01:00
|
|
|
let expiresIn = (decoded.timestamp * 1 + decoded.expiry * 1) * 1000; // ms
|
|
|
|
if (+new Date() > expiresIn) {
|
2019-01-06 08:28:23 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-01-05 01:10:59 +01:00
|
|
|
return alert('Invoice expired');
|
|
|
|
}
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-10 17:16:44 +01:00
|
|
|
const currentUserInvoices = fromWallet.user_invoices_raw; // not fetching invoices, as we assume they were loaded previously
|
2019-01-05 01:10:59 +01:00
|
|
|
if (currentUserInvoices.some(invoice => invoice.payment_hash === decoded.payment_hash)) {
|
2019-01-06 08:28:23 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-01-05 01:10:59 +01:00
|
|
|
return alert(loc.lnd.sameWalletAsInvoiceError);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-01-06 21:55:50 +01:00
|
|
|
await fromWallet.payInvoice(this.state.invoice, this.state.decoded.num_satoshis);
|
2019-01-05 01:10:59 +01:00
|
|
|
} catch (Err) {
|
|
|
|
console.log(Err.message);
|
2019-01-06 08:28:23 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-02-04 22:09:51 +01:00
|
|
|
return alert(Err.message);
|
2019-01-05 01:10:59 +01:00
|
|
|
}
|
2018-09-01 01:28:19 +02:00
|
|
|
|
2019-01-05 01:10:59 +01:00
|
|
|
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
|
2019-02-01 01:59:49 +01:00
|
|
|
this.props.navigation.navigate('Success', {
|
|
|
|
amount: this.state.decoded.num_satoshis,
|
|
|
|
amountUnit: BitcoinUnit.SATS,
|
2019-02-01 08:08:37 +01:00
|
|
|
invoiceDescription: this.state.decoded.description,
|
2019-02-01 01:59:49 +01:00
|
|
|
});
|
2019-01-05 01:10:59 +01:00
|
|
|
},
|
|
|
|
);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
|
2018-12-24 16:29:33 +01:00
|
|
|
processTextForInvoice = text => {
|
|
|
|
if (text.toLowerCase().startsWith('lnb') || text.toLowerCase().startsWith('lightning:lnb')) {
|
|
|
|
this.processInvoice(text);
|
|
|
|
} else {
|
2018-12-25 17:34:51 +01:00
|
|
|
this.setState({ decoded: undefined, expiresIn: undefined, destination: text });
|
2018-12-24 16:29:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-06 08:28:23 +01:00
|
|
|
shouldDisablePayButton = () => {
|
|
|
|
if (typeof this.state.decoded !== 'object') {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!this.state.decoded.hasOwnProperty('num_satoshis')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2019-01-06 10:30:32 +01:00
|
|
|
return this.state.decoded.num_satoshis <= 0 || this.state.isLoading || isNaN(this.state.decoded.num_satoshis);
|
2019-01-06 08:28:23 +01:00
|
|
|
};
|
|
|
|
|
2019-02-26 02:33:29 +01:00
|
|
|
renderWalletSelectionButton = () => {
|
|
|
|
if (this.state.renderWalletSelectionButtonHidden) return;
|
|
|
|
return (
|
2019-09-17 21:39:21 +02:00
|
|
|
<View style={{ marginBottom: 16, alignItems: 'center' }}>
|
2019-02-26 02:33:29 +01:00
|
|
|
{!this.state.isLoading && (
|
|
|
|
<TouchableOpacity
|
2019-09-17 21:39:21 +02:00
|
|
|
style={{ flexDirection: 'row', alignItems: 'center' }}
|
2019-02-26 02:33:29 +01:00
|
|
|
onPress={() =>
|
|
|
|
this.props.navigation.navigate('SelectWallet', { onWalletSelect: this.onWalletSelect, chainType: Chain.OFFCHAIN })
|
|
|
|
}
|
|
|
|
>
|
2019-10-06 13:40:21 +02:00
|
|
|
<Text style={{ color: '#9aa0aa', fontSize: 14, marginRight: 8 }}>{loc.wallets.select_wallet.toLowerCase()}</Text>
|
2019-09-17 21:39:21 +02:00
|
|
|
<Icon name="angle-right" size={18} type="font-awesome" color="#9aa0aa" />
|
2019-02-26 02:33:29 +01:00
|
|
|
</TouchableOpacity>
|
|
|
|
)}
|
|
|
|
<View style={{ flexDirection: 'row', alignItems: 'center', marginVertical: 4 }}>
|
2019-09-17 21:39:21 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
style={{ flexDirection: 'row', alignItems: 'center' }}
|
|
|
|
onPress={() =>
|
|
|
|
this.props.navigation.navigate('SelectWallet', { onWalletSelect: this.onWalletSelect, chainType: Chain.OFFCHAIN })
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Text style={{ color: '#0c2550', fontSize: 14 }}>{this.state.fromWallet.getLabel()}</Text>
|
|
|
|
<Text style={{ color: '#0c2550', fontSize: 14, fontWeight: '600', marginLeft: 8, marginRight: 4 }}>
|
2019-09-17 22:11:03 +02:00
|
|
|
{loc.formatBalanceWithoutSuffix(this.state.fromWallet.getBalance(), BitcoinUnit.SATS, false)}
|
2019-09-17 21:39:21 +02:00
|
|
|
</Text>
|
|
|
|
<Text style={{ color: '#0c2550', fontSize: 11, fontWeight: '600', textAlignVertical: 'bottom', marginTop: 2 }}>
|
2019-09-17 22:11:03 +02:00
|
|
|
{BitcoinUnit.SATS}
|
2019-09-17 21:39:21 +02:00
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
2019-02-26 02:33:29 +01:00
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
onWalletSelect = wallet => {
|
|
|
|
this.setState({ fromSecret: wallet.getSecret(), fromWallet: wallet }, () => {
|
|
|
|
this.props.navigation.pop();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-09-01 01:28:19 +02:00
|
|
|
render() {
|
2018-10-23 00:51:30 +02:00
|
|
|
return (
|
2019-01-06 08:28:23 +01:00
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
|
2018-09-01 01:28:19 +02:00
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
2019-02-26 02:33:29 +01:00
|
|
|
<View style={{ flex: 1 }}>
|
|
|
|
<View style={{ flex: 1 }}>
|
2019-09-17 23:26:19 +02:00
|
|
|
<View style={{ marginTop: 60 }}>
|
|
|
|
<BlueBitcoinAmount
|
|
|
|
pointerEvents={this.state.isAmountInitiallyEmpty ? 'auto' : 'none'}
|
|
|
|
isLoading={this.state.isLoading}
|
|
|
|
amount={typeof this.state.decoded === 'object' ? this.state.decoded.num_satoshis : 0}
|
|
|
|
onChangeText={text => {
|
|
|
|
if (typeof this.state.decoded === 'object') {
|
|
|
|
text = parseInt(text || 0);
|
|
|
|
let decoded = this.state.decoded;
|
|
|
|
decoded.num_satoshis = text;
|
|
|
|
this.setState({ decoded: decoded });
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
disabled={typeof this.state.decoded !== 'object' || this.state.isLoading}
|
|
|
|
unit={BitcoinUnit.SATS}
|
|
|
|
inputAccessoryViewID={BlueDismissKeyboardInputAccessory.InputAccessoryViewID}
|
|
|
|
/>
|
|
|
|
</View>
|
2019-10-06 13:40:21 +02:00
|
|
|
|
2019-02-26 02:33:29 +01:00
|
|
|
<BlueCard>
|
|
|
|
<BlueAddressInput
|
|
|
|
onChangeText={text => {
|
|
|
|
this.setState({ destination: text });
|
|
|
|
this.processTextForInvoice(text);
|
2019-02-01 01:59:49 +01:00
|
|
|
}}
|
2019-02-26 02:33:29 +01:00
|
|
|
onBarScanned={this.processInvoice}
|
|
|
|
address={this.state.destination}
|
|
|
|
isLoading={this.state.isLoading}
|
|
|
|
placeholder={loc.lnd.placeholder}
|
|
|
|
inputAccessoryViewID={BlueDismissKeyboardInputAccessory.InputAccessoryViewID}
|
2019-02-01 01:59:49 +01:00
|
|
|
/>
|
2019-02-26 02:33:29 +01:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
marginHorizontal: 20,
|
|
|
|
alignItems: 'center',
|
2019-09-17 23:26:19 +02:00
|
|
|
marginVertical: 0,
|
2019-02-26 02:33:29 +01:00
|
|
|
borderRadius: 4,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Text numberOfLines={0} style={{ color: '#81868e', fontWeight: '500', fontSize: 14 }}>
|
|
|
|
{this.state.hasOwnProperty('decoded') && this.state.decoded !== undefined ? this.state.decoded.description : ''}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
{this.state.expiresIn !== undefined && (
|
|
|
|
<Text style={{ color: '#81868e', fontSize: 12, left: 20, top: 10 }}>Expires in: {this.state.expiresIn}</Text>
|
|
|
|
)}
|
2019-10-06 13:40:21 +02:00
|
|
|
|
2019-02-26 02:33:29 +01:00
|
|
|
<BlueCard>
|
|
|
|
{this.state.isLoading ? (
|
|
|
|
<View>
|
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
) : (
|
|
|
|
<BlueButton
|
|
|
|
title={'Pay'}
|
|
|
|
onPress={() => {
|
|
|
|
this.pay();
|
|
|
|
}}
|
|
|
|
disabled={this.shouldDisablePayButton()}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</BlueCard>
|
|
|
|
</BlueCard>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
{this.renderWalletSelectionButton()}
|
|
|
|
</View>
|
|
|
|
<BlueDismissKeyboardInputAccessory />
|
2018-10-23 00:51:30 +02:00
|
|
|
</SafeBlueArea>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScanLndInvoice.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
2019-02-01 16:28:43 +01:00
|
|
|
goBack: PropTypes.func,
|
|
|
|
navigate: PropTypes.func,
|
2019-02-26 02:33:29 +01:00
|
|
|
pop: PropTypes.func,
|
2019-02-01 16:28:43 +01:00
|
|
|
getParam: PropTypes.func,
|
|
|
|
dismiss: PropTypes.func,
|
2018-09-01 01:28:19 +02:00
|
|
|
state: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
2018-12-24 16:29:33 +01:00
|
|
|
uri: PropTypes.string,
|
2018-09-01 01:28:19 +02:00
|
|
|
fromSecret: PropTypes.string,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|