2018-10-28 11:13:28 +01:00
|
|
|
import React, { Component } from 'react';
|
2020-12-04 10:50:23 +01:00
|
|
|
import { StyleSheet, StatusBar, Linking, Platform } from 'react-native';
|
2020-12-04 14:39:47 +01:00
|
|
|
import { BlueNavigationStyle, BlueLoading, SafeBlueArea } from '../../BlueComponents';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-04-21 20:50:28 +02:00
|
|
|
import { WebView } from 'react-native-webview';
|
2020-12-04 10:50:23 +01:00
|
|
|
import { LightningCustodianWallet, WatchOnlyWallet } from '../../class';
|
2020-12-04 14:39:47 +01:00
|
|
|
import InAppBrowser from 'react-native-inappbrowser-reborn';
|
2020-09-07 17:35:51 +02:00
|
|
|
import * as NavigationService from '../../NavigationService';
|
2020-10-24 19:20:59 +02:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2020-06-09 16:08:18 +02:00
|
|
|
const currency = require('../../blue_modules/currency');
|
2020-05-24 11:17:26 +02:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-10-28 11:13:28 +01:00
|
|
|
export default class BuyBitcoin extends Component {
|
2020-10-24 19:20:59 +02:00
|
|
|
static contextType = BlueStorageContext;
|
2018-10-28 11:13:28 +01:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-06-01 14:54:23 +02:00
|
|
|
const wallet = props.route.params.wallet;
|
2020-04-29 14:37:09 +02:00
|
|
|
if (!wallet) console.warn('wallet was not passed to buyBitcoin');
|
2018-10-28 11:13:28 +01:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
2020-04-17 16:41:35 +02:00
|
|
|
wallet,
|
2020-08-12 17:30:37 +02:00
|
|
|
uri: '',
|
2018-10-28 11:13:28 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:47:34 +02:00
|
|
|
static async generateURL(wallet) {
|
2020-04-24 18:37:46 +02:00
|
|
|
let preferredCurrency = await currency.getPreferredCurrency();
|
|
|
|
preferredCurrency = preferredCurrency.endPointKey;
|
|
|
|
|
2020-04-17 16:41:35 +02:00
|
|
|
/** @type {AbstractHDWallet|WatchOnlyWallet|LightningCustodianWallet} */
|
|
|
|
|
|
|
|
let address = '';
|
2018-10-28 11:13:28 +01:00
|
|
|
|
2020-04-17 16:41:35 +02:00
|
|
|
if (WatchOnlyWallet.type === wallet.type && !wallet.isHd()) {
|
|
|
|
// plain watchonly - just get the address
|
|
|
|
address = wallet.getAddress();
|
2020-08-12 17:30:37 +02:00
|
|
|
} else {
|
|
|
|
// otherwise, lets call widely-used getAddressAsync()
|
|
|
|
try {
|
2020-10-24 19:20:59 +02:00
|
|
|
address = await Promise.race([wallet.getAddressAsync(), this.context.sleep(2000)]);
|
2020-08-12 17:30:37 +02:00
|
|
|
} catch (_) {}
|
|
|
|
|
|
|
|
if (!address) {
|
|
|
|
// either sleep expired or getAddressAsync threw an exception
|
|
|
|
if (LightningCustodianWallet.type === wallet.type) {
|
|
|
|
// not much we can do, lets hope refill address was cached previously
|
|
|
|
address = wallet.getAddress() || '';
|
|
|
|
} else {
|
|
|
|
// plain hd wallet (either HD or watchonly-wrapped). trying next free address
|
|
|
|
address = wallet._getExternalAddressByIndex(wallet.getNextFreeAddressIndex());
|
|
|
|
}
|
|
|
|
}
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
2020-04-17 16:41:35 +02:00
|
|
|
|
2020-08-12 17:30:37 +02:00
|
|
|
let uri = 'https://bluewallet.io/buy-bitcoin-redirect.html?address=' + address;
|
2020-04-17 16:41:35 +02:00
|
|
|
|
2020-08-12 17:30:37 +02:00
|
|
|
if (preferredCurrency) {
|
|
|
|
uri += '¤cy=' + preferredCurrency;
|
|
|
|
}
|
2020-09-05 02:47:34 +02:00
|
|
|
return uri;
|
|
|
|
}
|
2020-08-12 17:30:37 +02:00
|
|
|
|
2020-09-05 02:47:34 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
console.log('buyBitcoin - componentDidMount');
|
|
|
|
|
|
|
|
let uri = await BuyBitcoin.generateURL(this.state.wallet);
|
|
|
|
|
|
|
|
const { safelloStateToken } = this.props.route.params;
|
|
|
|
if (safelloStateToken) {
|
|
|
|
uri += '&safelloStateToken=' + safelloStateToken;
|
2020-08-12 17:30:37 +02:00
|
|
|
}
|
2020-09-05 02:47:34 +02:00
|
|
|
this.setState({ uri, isLoading: false });
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return <BlueLoading />;
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<SafeBlueArea style={styles.root}>
|
2020-07-15 19:32:59 +02:00
|
|
|
<StatusBar barStyle="default" />
|
2020-04-16 15:43:53 +02:00
|
|
|
<WebView
|
|
|
|
source={{
|
2020-08-12 17:30:37 +02:00
|
|
|
uri: this.state.uri,
|
2020-04-16 15:43:53 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</SafeBlueArea>
|
2019-05-12 22:50:08 +02:00
|
|
|
);
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BuyBitcoin.propTypes = {
|
2020-05-27 13:12:17 +02:00
|
|
|
route: PropTypes.shape({
|
|
|
|
name: PropTypes.string,
|
|
|
|
params: PropTypes.shape({
|
|
|
|
wallet: PropTypes.object.isRequired,
|
|
|
|
safelloStateToken: PropTypes.string,
|
2018-10-28 11:13:28 +01:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|
2020-07-15 19:32:59 +02:00
|
|
|
|
2020-12-04 14:39:47 +01:00
|
|
|
BuyBitcoin.navigationOptions = ({ navigation }) => ({
|
|
|
|
...BlueNavigationStyle(navigation, true),
|
2020-07-15 19:32:59 +02:00
|
|
|
title: '',
|
|
|
|
headerLeft: null,
|
|
|
|
});
|
2020-09-07 17:35:51 +02:00
|
|
|
|
|
|
|
BuyBitcoin.navigate = async wallet => {
|
|
|
|
const uri = await BuyBitcoin.generateURL(wallet);
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
InAppBrowser.isAvailable()
|
2020-10-24 19:20:59 +02:00
|
|
|
.then(_value => {
|
2020-10-24 23:55:17 +02:00
|
|
|
InAppBrowser.open(uri, { dismissButtonStyle: 'done', modalEnabled: true, animated: true });
|
2020-10-24 19:20:59 +02:00
|
|
|
})
|
2020-09-07 17:35:51 +02:00
|
|
|
.catch(error => {
|
|
|
|
console.log(error);
|
|
|
|
Linking.openURL(uri);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
NavigationService.navigate('BuyBitcoin', {
|
|
|
|
wallet,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|