2019-01-06 08:42:53 +01:00
|
|
|
/* global alert */
|
2018-12-25 17:34:51 +01:00
|
|
|
import React, { Component } from 'react';
|
2019-08-03 14:27:09 +02:00
|
|
|
import { Dimensions, ActivityIndicator, View, TextInput, KeyboardAvoidingView, Keyboard, TouchableWithoutFeedback, TouchableOpacity, Text } from 'react-native';
|
|
|
|
import { BlueNavigationStyle, BlueButton, BlueBitcoinAmount, BlueDismissKeyboardInputAccessory } from '../../BlueComponents';
|
2018-12-25 17:34:51 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2019-07-28 19:22:58 +02:00
|
|
|
import bech32 from 'bech32';
|
2018-12-25 17:34:51 +01:00
|
|
|
import { BitcoinUnit } from '../../models/bitcoinUnits';
|
2019-08-03 14:27:09 +02:00
|
|
|
import NavigationService from '../../NavigationService';
|
2018-12-25 17:34:51 +01:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2019-08-03 14:27:09 +02:00
|
|
|
let BlueApp = require('../../BlueApp');
|
2018-12-27 07:12:19 +01:00
|
|
|
let EV = require('../../events');
|
2018-12-25 17:34:51 +01:00
|
|
|
let loc = require('../../loc');
|
2019-08-03 14:27:09 +02:00
|
|
|
const { width } = Dimensions.get('window');
|
2018-12-25 17:34:51 +01:00
|
|
|
|
|
|
|
export default class LNDCreateInvoice extends Component {
|
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
...BlueNavigationStyle(navigation, true),
|
|
|
|
title: loc.receive.header,
|
|
|
|
});
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
// fallback to first wallet if it exists
|
|
|
|
|
|
|
|
const fromWallet = props.navigation.getParam('fromWallet');
|
|
|
|
this.state = {
|
|
|
|
fromWallet,
|
2019-07-28 19:22:58 +02:00
|
|
|
amount: '',
|
2019-01-06 08:42:53 +01:00
|
|
|
description: '',
|
2018-12-25 17:34:51 +01:00
|
|
|
isLoading: false,
|
2019-07-28 19:22:58 +02:00
|
|
|
lnurl: '',
|
|
|
|
lnurlParams: null,
|
2018-12-25 17:34:51 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async createInvoice() {
|
|
|
|
this.setState({ isLoading: true }, async () => {
|
2019-01-06 08:42:53 +01:00
|
|
|
try {
|
|
|
|
const invoiceRequest = await this.state.fromWallet.addInvoice(this.state.amount, this.state.description);
|
2018-12-27 07:12:19 +01:00
|
|
|
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
|
2019-07-28 19:22:58 +02:00
|
|
|
|
|
|
|
// send to lnurl-withdraw callback url if that exists
|
|
|
|
if (this.state.lnurlParams) {
|
|
|
|
let {callback, k1} = this.state.lnurlParams;
|
|
|
|
let callbackUrl = callback + (callback.indexOf('?') !== -1 ? '&' : '?') + 'k1=' + k1 + '&pr=' + invoiceRequest;
|
|
|
|
let resp = await fetch(callbackUrl, {method: 'GET'});
|
|
|
|
if (resp.status >= 300) {
|
|
|
|
let text = await resp.text();
|
|
|
|
throw new Error(text);
|
|
|
|
}
|
|
|
|
let reply = await resp.json();
|
|
|
|
if (reply.status === 'ERROR') {
|
|
|
|
throw new Error('Reply from server: ' + reply.reason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-25 17:34:51 +01:00
|
|
|
this.props.navigation.navigate('LNDViewInvoice', {
|
|
|
|
invoice: invoiceRequest,
|
2018-12-28 00:33:39 +01:00
|
|
|
fromWallet: this.state.fromWallet,
|
2019-01-14 07:55:55 +01:00
|
|
|
isModal: true,
|
2018-12-25 17:34:51 +01:00
|
|
|
});
|
2019-07-28 19:22:58 +02:00
|
|
|
} catch (Err) {
|
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
this.setState({ isLoading: false });
|
|
|
|
alert(Err.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
processLnurl = data => {
|
|
|
|
this.setState({ isLoading: true }, async () => {
|
|
|
|
if (!this.state.fromWallet) {
|
2019-05-03 14:36:11 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-07-28 19:22:58 +02:00
|
|
|
alert('Before paying a Lightning invoice, you must first add a Lightning wallet.');
|
|
|
|
return this.props.navigation.goBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// handling fallback lnurl
|
|
|
|
let ind = data.indexOf('lightning=');
|
|
|
|
if (ind !== -1) {
|
|
|
|
data = data.substring(ind + 10).split('&')[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
data = data.replace('LIGHTNING:', '').replace('lightning:', '');
|
|
|
|
console.log(data);
|
|
|
|
|
|
|
|
// decoding the lnurl
|
|
|
|
let decoded = bech32.decode(data, 1500);
|
|
|
|
let url = Buffer.from(bech32.fromWords(decoded.words)).toString();
|
|
|
|
|
|
|
|
// calling the url
|
|
|
|
try {
|
|
|
|
let resp = await fetch(url, {method: 'GET'})
|
|
|
|
if (resp.status >= 300) {
|
|
|
|
throw new Error("Bad response from server");
|
|
|
|
}
|
|
|
|
let reply = await resp.json();
|
|
|
|
if (reply.status === 'ERROR') {
|
|
|
|
throw new Error('Reply from server: ' + reply.reason);
|
|
|
|
}
|
|
|
|
|
2019-07-28 21:12:42 +02:00
|
|
|
if (reply.tag !== 'withdrawRequest') {
|
|
|
|
throw new Error('Unsupported lnurl');
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:22:58 +02:00
|
|
|
// setting the invoice creating screen with the parameters
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
2019-08-03 03:37:18 +02:00
|
|
|
lnurlParams: {
|
|
|
|
k1: reply.k1,
|
|
|
|
callback: reply.callback,
|
|
|
|
fixed: reply.minWithdrawable === reply.maxWithdrawable,
|
2019-08-03 14:27:09 +02:00
|
|
|
min: (reply.minWithdrawable || 0) / 1000,
|
2019-08-03 03:37:18 +02:00
|
|
|
max: reply.maxWithdrawable / 1000
|
|
|
|
},
|
2019-07-28 19:22:58 +02:00
|
|
|
amount: (reply.maxWithdrawable / 1000).toString(),
|
|
|
|
description: reply.defaultDescription,
|
|
|
|
});
|
|
|
|
} catch (Err) {
|
|
|
|
Keyboard.dismiss();
|
2019-01-06 08:42:53 +01:00
|
|
|
this.setState({ isLoading: false });
|
2019-07-28 19:22:58 +02:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
|
|
|
alert(Err.message);
|
2018-12-25 17:34:51 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderCreateButton = () => {
|
|
|
|
return (
|
2019-02-14 05:44:47 +01:00
|
|
|
<View style={{ marginHorizontal: 56, marginVertical: 16, minHeight: 45, alignContent: 'center', backgroundColor: '#FFFFFF' }}>
|
2018-12-25 17:34:51 +01:00
|
|
|
{this.state.isLoading ? (
|
|
|
|
<ActivityIndicator />
|
|
|
|
) : (
|
2019-01-24 20:31:30 +01:00
|
|
|
<BlueButton disabled={!this.state.amount > 0} onPress={() => this.createInvoice()} title={loc.send.details.create} />
|
2018-12-25 17:34:51 +01:00
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-08-03 05:10:50 +02:00
|
|
|
renderScanClickable = () => {
|
2019-07-28 19:22:58 +02:00
|
|
|
return (
|
2019-08-03 05:10:50 +02:00
|
|
|
<View style={{ marginHorizontal: 0, marginVertical: 16, minHeight: 25, alignContent: 'center' }}>
|
2019-08-03 14:27:09 +02:00
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => NavigationService.navigate('ScanQrAddress', { onBarScanned: this.processLnurl }) }
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
minWidth: width,
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Text style={{color: BlueApp.settings.buttonTextColor, textAlign: 'center'}}>
|
|
|
|
{loc.receive.scan_lnurl}
|
|
|
|
</Text>
|
|
|
|
</TouchableOpacity>
|
2019-07-28 19:22:58 +02:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-12-25 17:34:51 +01:00
|
|
|
render() {
|
|
|
|
if (!this.state.fromWallet) {
|
|
|
|
return (
|
|
|
|
<View style={{ flex: 1, paddingTop: 20 }}>
|
|
|
|
<Text>System error: Source wallet not found (this should never happen)</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TouchableWithoutFeedback onPress={Keyboard.dismiss} accessible={false}>
|
|
|
|
<View style={{ flex: 1, justifyContent: 'space-between' }}>
|
|
|
|
<View style={{ flex: 1, backgroundColor: '#FFFFFF' }}>
|
|
|
|
<KeyboardAvoidingView behavior="position">
|
2019-01-06 08:42:53 +01:00
|
|
|
<BlueBitcoinAmount
|
|
|
|
isLoading={this.state.isLoading}
|
|
|
|
amount={this.state.amount}
|
|
|
|
onChangeText={text => {
|
2019-08-03 03:37:18 +02:00
|
|
|
if (this.state.lnurlParams) {
|
|
|
|
// in this case we prevent the user from changing the amount to < min or > max
|
|
|
|
let {min, max} = this.state.lnurlParams;
|
|
|
|
let nextAmount = parseInt(text)
|
|
|
|
if (nextAmount < min) {
|
|
|
|
text = min.toString()
|
|
|
|
} else if (nextAmount > max) {
|
|
|
|
text = max.toString()
|
|
|
|
}
|
2019-07-28 19:22:58 +02:00
|
|
|
}
|
2019-08-03 03:37:18 +02:00
|
|
|
|
2019-01-06 08:42:53 +01:00
|
|
|
this.setState({ amount: text });
|
|
|
|
}}
|
2019-08-03 03:37:18 +02:00
|
|
|
disabled={this.state.isLoading || (this.state.lnurlParams && this.state.lnurlParams.fixed)}
|
2019-01-06 08:42:53 +01:00
|
|
|
unit={BitcoinUnit.SATS}
|
2019-02-17 10:23:12 +01:00
|
|
|
inputAccessoryViewID={BlueDismissKeyboardInputAccessory.InputAccessoryViewID}
|
2019-01-06 08:42:53 +01:00
|
|
|
/>
|
2018-12-25 17:34:51 +01: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
|
2019-01-06 08:42:53 +01:00
|
|
|
onChangeText={text => this.setState({ description: text })}
|
2019-01-01 01:41:15 +01:00
|
|
|
placeholder={loc.receive.details.label}
|
2019-01-06 08:42:53 +01:00
|
|
|
value={this.state.description}
|
2018-12-25 17:34:51 +01:00
|
|
|
numberOfLines={1}
|
|
|
|
style={{ flex: 1, marginHorizontal: 8, minHeight: 33 }}
|
|
|
|
editable={!this.state.isLoading}
|
2019-02-09 21:46:43 +01:00
|
|
|
onSubmitEditing={Keyboard.dismiss}
|
2019-02-17 10:23:12 +01:00
|
|
|
inputAccessoryViewID={BlueDismissKeyboardInputAccessory.InputAccessoryViewID}
|
2018-12-25 17:34:51 +01:00
|
|
|
/>
|
|
|
|
</View>
|
2019-02-17 10:23:12 +01:00
|
|
|
<BlueDismissKeyboardInputAccessory />
|
2018-12-25 17:34:51 +01:00
|
|
|
{this.renderCreateButton()}
|
|
|
|
</KeyboardAvoidingView>
|
|
|
|
</View>
|
2019-08-03 05:10:50 +02:00
|
|
|
{this.state.lnurlParams ? null : this.renderScanClickable()}
|
2018-12-25 17:34:51 +01:00
|
|
|
</View>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LNDCreateInvoice.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
2019-02-01 16:28:43 +01:00
|
|
|
goBack: PropTypes.func,
|
2018-12-25 17:34:51 +01:00
|
|
|
navigate: PropTypes.func,
|
|
|
|
getParam: PropTypes.func,
|
|
|
|
}),
|
|
|
|
};
|