2018-10-28 11:13:28 +01:00
|
|
|
import React, { Component } from 'react';
|
2020-04-16 15:43:53 +02:00
|
|
|
import { BlueNavigationStyle, BlueLoading, SafeBlueArea } from '../../BlueComponents';
|
2018-10-28 11:13:28 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2019-04-21 20:50:28 +02:00
|
|
|
import { WebView } from 'react-native-webview';
|
2020-04-17 16:41:35 +02:00
|
|
|
import { AppStorage, LightningCustodianWallet, WatchOnlyWallet } from '../../class';
|
2020-04-24 18:37:46 +02:00
|
|
|
const currency = require('../../currency');
|
2020-04-17 16:41:35 +02:00
|
|
|
let BlueApp: AppStorage = require('../../BlueApp');
|
2018-10-28 11:13:28 +01:00
|
|
|
let loc = require('../../loc');
|
|
|
|
|
|
|
|
export default class BuyBitcoin extends Component {
|
2018-12-11 23:52:46 +01:00
|
|
|
static navigationOptions = ({ navigation }) => ({
|
|
|
|
...BlueNavigationStyle(navigation, true),
|
|
|
|
title: loc.buyBitcoin.header,
|
|
|
|
headerLeft: null,
|
|
|
|
});
|
2018-10-28 11:13:28 +01:00
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-04-17 16:41:35 +02:00
|
|
|
let wallet = props.navigation.state.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,
|
|
|
|
address: '',
|
2018-10-28 11:13:28 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-03-23 17:32:51 +01:00
|
|
|
console.log('buyBitcoin - componentDidMount');
|
2018-10-28 11:13:28 +01:00
|
|
|
|
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 wallet = this.state.wallet;
|
|
|
|
|
|
|
|
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();
|
2018-10-28 11:13:28 +01:00
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
address,
|
2020-04-24 18:37:46 +02:00
|
|
|
preferredCurrency,
|
2018-10-28 11:13:28 +01:00
|
|
|
});
|
2020-04-17 16:41:35 +02:00
|
|
|
return;
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
2020-04-17 16:41:35 +02:00
|
|
|
|
|
|
|
// otherwise, lets call widely-used getAddressAsync()
|
|
|
|
|
|
|
|
try {
|
|
|
|
address = await Promise.race([wallet.getAddressAsync(), BlueApp.sleep(2000)]);
|
|
|
|
} 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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
address,
|
2020-04-24 18:37:46 +02:00
|
|
|
preferredCurrency,
|
2020-04-17 16:41:35 +02:00
|
|
|
});
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return <BlueLoading />;
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
const { safelloStateToken } = this.props.navigation.state.params;
|
2019-05-09 12:17:01 +02:00
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
let uri = 'https://bluewallet.io/buy-bitcoin-redirect.html?address=' + this.state.address;
|
2019-05-10 13:52:08 +02:00
|
|
|
|
|
|
|
if (safelloStateToken) {
|
2019-05-12 22:50:08 +02:00
|
|
|
uri += '&safelloStateToken=' + safelloStateToken;
|
2019-05-09 12:17:01 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 18:37:46 +02:00
|
|
|
if (this.state.preferredCurrency) {
|
|
|
|
uri += '¤cy=' + this.state.preferredCurrency;
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:50:08 +02:00
|
|
|
return (
|
2020-04-16 15:43:53 +02:00
|
|
|
<SafeBlueArea style={{ flex: 1 }}>
|
|
|
|
<WebView
|
|
|
|
source={{
|
|
|
|
uri,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</SafeBlueArea>
|
2019-05-12 22:50:08 +02:00
|
|
|
);
|
2018-10-28 11:13:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BuyBitcoin.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
2019-02-01 16:28:43 +01:00
|
|
|
goBack: PropTypes.func,
|
2018-10-28 11:13:28 +01:00
|
|
|
state: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
2020-04-29 14:37:09 +02:00
|
|
|
wallet: PropTypes.object.isRequired,
|
2019-05-09 14:06:44 +02:00
|
|
|
safelloStateToken: PropTypes.string,
|
2018-10-28 11:13:28 +01:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|