2018-09-01 01:28:19 +02:00
|
|
|
/* global alert */
|
|
|
|
import React from 'react';
|
2018-10-23 00:51:30 +02:00
|
|
|
import { Text, Dimensions, ActivityIndicator, View, TouchableOpacity, TouchableWithoutFeedback, TextInput, Keyboard } from 'react-native';
|
|
|
|
import { Icon } from 'react-native-elements';
|
2018-09-01 01:28:19 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2018-10-23 00:51:30 +02:00
|
|
|
import { BlueSpacing20, BlueButton, SafeBlueArea, BlueCard, BlueHeaderDefaultSub } from '../../BlueComponents';
|
2018-09-01 01:28:19 +02:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
let BlueApp = require('../../BlueApp');
|
|
|
|
let currency = require('../../currency');
|
2018-10-03 02:36:00 +02:00
|
|
|
let EV = require('../../events');
|
2018-10-23 00:51:30 +02:00
|
|
|
let loc = require('../../loc');
|
2018-09-01 01:28:19 +02:00
|
|
|
const { width } = Dimensions.get('window');
|
|
|
|
|
|
|
|
export default class ScanLndInvoice extends React.Component {
|
|
|
|
static navigationOptions = {
|
2018-09-26 23:16:52 +02:00
|
|
|
header: ({ navigation }) => {
|
2018-10-23 00:51:30 +02:00
|
|
|
return <BlueHeaderDefaultSub leftText={'Pay invoice'} onClose={() => navigation.goBack(null)} />;
|
2018-09-26 23:16:52 +02:00
|
|
|
},
|
2018-09-01 01:28:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
isLoading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
let fromSecret;
|
|
|
|
if (props.navigation.state.params.fromSecret) fromSecret = props.navigation.state.params.fromSecret;
|
|
|
|
let fromWallet = {};
|
|
|
|
|
|
|
|
for (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.getSecret() === fromSecret) {
|
|
|
|
fromWallet = w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
fromWallet,
|
|
|
|
fromSecret,
|
|
|
|
};
|
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() {
|
|
|
|
EV(
|
|
|
|
EV.enum.CREATE_TRANSACTION_NEW_DESTINATION_ADDRESS,
|
|
|
|
data => {
|
|
|
|
this.processInvoice(data);
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:51:30 +02:00
|
|
|
async processInvoice(data) {
|
2018-09-01 01:28:19 +02:00
|
|
|
if (this.ignoreRead) return;
|
|
|
|
this.ignoreRead = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.ignoreRead = false;
|
|
|
|
}, 6000);
|
|
|
|
|
|
|
|
if (!this.state.fromWallet) {
|
|
|
|
alert('Error: cant find source wallet (this should never happen)');
|
|
|
|
return this.props.navigation.goBack();
|
|
|
|
}
|
|
|
|
|
2018-10-23 00:51:30 +02:00
|
|
|
data = data.replace('LIGHTNING:', '').replace('lightning:', '');
|
|
|
|
console.log(data);
|
2018-09-01 01:28:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {LightningCustodianWallet}
|
|
|
|
*/
|
|
|
|
let w = this.state.fromWallet;
|
|
|
|
let decoded = false;
|
|
|
|
try {
|
2018-10-23 00:51:30 +02:00
|
|
|
decoded = await w.decodeInvoice(data);
|
2018-09-01 01:28:19 +02: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';
|
|
|
|
}
|
2018-10-23 00:51:30 +02:00
|
|
|
Keyboard.dismiss();
|
2018-09-01 01:28:19 +02:00
|
|
|
this.setState({
|
|
|
|
isPaying: true,
|
2018-10-23 00:51:30 +02:00
|
|
|
invoice: data,
|
2018-09-01 01:28:19 +02:00
|
|
|
decoded,
|
|
|
|
expiresIn,
|
|
|
|
});
|
|
|
|
} catch (Err) {
|
|
|
|
alert(Err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
let decoded = this.state.decoded;
|
|
|
|
|
|
|
|
/** @type {LightningCustodianWallet} */
|
|
|
|
let fromWallet = this.state.fromWallet;
|
|
|
|
|
|
|
|
let expiresIn = (decoded.timestamp * 1 + decoded.expiry * 1) * 1000; // ms
|
|
|
|
if (+new Date() > expiresIn) {
|
|
|
|
return alert('Invoice expired');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isPayingInProgress: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
let start = +new Date();
|
|
|
|
let end;
|
|
|
|
try {
|
|
|
|
await fromWallet.payInvoice(this.state.invoice);
|
|
|
|
end = +new Date();
|
|
|
|
} catch (Err) {
|
|
|
|
console.log(Err.message);
|
2018-09-29 00:45:35 +02:00
|
|
|
this.props.navigation.goBack();
|
2018-09-01 01:28:19 +02:00
|
|
|
return alert('Error');
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('payInvoice took', (end - start) / 1000, 'sec');
|
2018-10-09 06:25:36 +02:00
|
|
|
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // someone should fetch txs
|
2018-09-01 01:28:19 +02:00
|
|
|
|
|
|
|
alert('Success');
|
|
|
|
this.props.navigation.goBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-10-23 00:51:30 +02:00
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
|
2018-09-01 01:28:19 +02:00
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={{ flex: 1 }}>
|
|
|
|
<Text style={{ textAlign: 'center', fontSize: 50, fontWeight: '700', color: '#2f5fb3' }}>
|
2018-10-23 00:51:30 +02:00
|
|
|
{this.state.hasOwnProperty('decoded') &&
|
|
|
|
this.state.decoded !== undefined &&
|
|
|
|
currency.satoshiToLocalCurrency(this.state.decoded.num_satoshis)}
|
2018-09-01 01:28:19 +02:00
|
|
|
</Text>
|
|
|
|
<Text style={{ textAlign: 'center', fontSize: 25, fontWeight: '600', color: '#d4d4d4' }}>
|
2018-10-23 00:51:30 +02:00
|
|
|
{this.state.hasOwnProperty('decoded') &&
|
|
|
|
this.state.decoded !== undefined &&
|
|
|
|
currency.satoshiToBTC(this.state.decoded.num_satoshis)}
|
2018-09-01 01:28:19 +02:00
|
|
|
</Text>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
|
|
|
|
<BlueCard>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
2018-10-23 00:51:30 +02:00
|
|
|
borderColor: '#d2d2d2',
|
|
|
|
borderBottomColor: '#d2d2d2',
|
|
|
|
borderWidth: 1.0,
|
|
|
|
borderBottomWidth: 0.5,
|
|
|
|
backgroundColor: '#f5f5f5',
|
|
|
|
minHeight: 44,
|
|
|
|
height: 44,
|
|
|
|
marginHorizontal: 20,
|
|
|
|
alignItems: 'center',
|
|
|
|
marginVertical: 8,
|
|
|
|
borderRadius: 4,
|
2018-09-01 01:28:19 +02:00
|
|
|
}}
|
|
|
|
>
|
2018-10-23 00:51:30 +02:00
|
|
|
<TextInput
|
|
|
|
onChangeText={text => {
|
2018-12-23 04:51:58 +01:00
|
|
|
if (text.toLowerCase().startsWith('lnb') || text.toLowerCase().startsWith('lntb')) {
|
2018-10-23 00:51:30 +02:00
|
|
|
this.processInvoice(text);
|
|
|
|
} else {
|
|
|
|
this.setState({ decoded: undefined, expiresIn: undefined });
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
placeholder={loc.wallets.details.destination}
|
|
|
|
numberOfLines={1}
|
|
|
|
value={this.state.hasOwnProperty('decoded') && this.state.decoded !== undefined ? this.state.decoded.destination : ''}
|
|
|
|
style={{ flex: 1, marginHorizontal: 8, minHeight: 33, height: 33 }}
|
|
|
|
editable={!this.state.isLoading}
|
|
|
|
/>
|
2018-09-01 01:28:19 +02:00
|
|
|
<TouchableOpacity
|
2018-10-23 00:51:30 +02:00
|
|
|
disabled={this.state.isLoading}
|
|
|
|
onPress={() => this.props.navigation.navigate('ScanQrAddress')}
|
2018-09-01 01:28:19 +02:00
|
|
|
style={{
|
2018-10-23 00:51:30 +02:00
|
|
|
width: 75,
|
|
|
|
height: 36,
|
|
|
|
flexDirection: 'row',
|
2018-09-01 01:28:19 +02:00
|
|
|
alignItems: 'center',
|
2018-10-23 00:51:30 +02:00
|
|
|
justifyContent: 'space-between',
|
|
|
|
backgroundColor: '#bebebe',
|
|
|
|
borderRadius: 4,
|
|
|
|
paddingVertical: 4,
|
|
|
|
paddingHorizontal: 8,
|
|
|
|
marginHorizontal: 4,
|
2018-09-01 01:28:19 +02:00
|
|
|
}}
|
|
|
|
>
|
2018-10-23 00:51:30 +02:00
|
|
|
<Icon name="qrcode" size={22} type="font-awesome" color="#FFFFFF" />
|
|
|
|
<Text style={{ color: '#FFFFFF' }}>{loc.send.details.scan}</Text>
|
2018-09-01 01:28:19 +02:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
2018-10-23 00:51:30 +02:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flexDirection: 'row',
|
|
|
|
borderColor: '#d2d2d2',
|
|
|
|
borderBottomColor: '#d2d2d2',
|
|
|
|
borderWidth: 1.0,
|
|
|
|
borderBottomWidth: 0.5,
|
|
|
|
backgroundColor: '#f5f5f5',
|
|
|
|
minHeight: 44,
|
|
|
|
height: 44,
|
|
|
|
marginHorizontal: 20,
|
|
|
|
alignItems: 'center',
|
|
|
|
marginVertical: 8,
|
|
|
|
borderRadius: 4,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<TextInput
|
|
|
|
onChangeText={text => {}}
|
|
|
|
placeholder={loc.wallets.details.description}
|
|
|
|
numberOfLines={1}
|
|
|
|
value={this.state.hasOwnProperty('decoded') && this.state.decoded !== undefined ? this.state.decoded.description : ''}
|
|
|
|
style={{ flex: 1, marginHorizontal: 8, minHeight: 33, height: 33 }}
|
|
|
|
editable={!this.state.isLoading}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
{this.state.expiresIn !== undefined && (
|
|
|
|
<Text style={{ color: '#81868e', fontSize: 12, left: 20, top: 10 }}>Expires in: {this.state.expiresIn}</Text>
|
|
|
|
)}
|
|
|
|
</BlueCard>
|
|
|
|
|
|
|
|
<BlueSpacing20 />
|
|
|
|
|
|
|
|
{this.state.hasOwnProperty('decoded') &&
|
|
|
|
this.state.decoded !== undefined &&
|
|
|
|
(() => {
|
|
|
|
if (this.state.isPayingInProgress) {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<BlueButton
|
|
|
|
icon={{
|
|
|
|
name: 'bolt',
|
|
|
|
type: 'font-awesome',
|
|
|
|
color: BlueApp.settings.buttonTextColor,
|
|
|
|
}}
|
|
|
|
title={'Pay'}
|
|
|
|
buttonStyle={{ width: 150, left: (width - 150) / 2 - 20 }}
|
|
|
|
onPress={() => {
|
|
|
|
this.pay();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
</SafeBlueArea>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
2018-09-01 01:28:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ScanLndInvoice.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
|
|
|
goBack: PropTypes.function,
|
2018-10-23 00:51:30 +02:00
|
|
|
navigate: PropTypes.function,
|
2018-09-01 01:28:19 +02:00
|
|
|
state: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
fromSecret: PropTypes.string,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|