BlueWallet/screen/wallets/buyBitcoin.js

121 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-10-28 11:13:28 +01:00
import React, { Component } from 'react';
2020-12-04 14:39:47 +01:00
import PropTypes from 'prop-types';
2021-04-16 19:05:03 +02:00
import { StatusBar, Linking } from 'react-native';
import { WebView } from 'react-native-webview';
2020-12-04 14:39:47 +01:00
import InAppBrowser from 'react-native-inappbrowser-reborn';
2020-12-25 17:09:53 +01:00
import { BlueLoading, SafeBlueArea } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
import { LightningCustodianWallet, WatchOnlyWallet } from '../../class';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2020-06-09 16:08:18 +02:00
const currency = require('../../blue_modules/currency');
2020-12-25 17:09:53 +01:00
2018-10-28 11:13:28 +01:00
export default class BuyBitcoin extends Component {
static contextType = BlueStorageContext;
2018-10-28 11:13:28 +01:00
constructor(props) {
super(props);
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,
wallet,
uri: '',
2018-10-28 11:13:28 +01:00
};
}
2020-09-05 02:47:34 +02:00
static async generateURL(wallet) {
let preferredCurrency = await currency.getPreferredCurrency();
preferredCurrency = preferredCurrency.endPointKey;
/** @type {AbstractHDWallet|WatchOnlyWallet|LightningCustodianWallet} */
let address = '';
2018-10-28 11:13:28 +01:00
if (WatchOnlyWallet.type === wallet.type && !wallet.isHd()) {
// plain watchonly - just get the address
address = wallet.getAddress();
} else {
// otherwise, lets call widely-used getAddressAsync()
try {
address = await Promise.race([wallet.getAddressAsync(), new Promise(resolve => setTimeout(resolve, 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());
}
}
2018-10-28 11:13:28 +01:00
}
let uri = 'https://bluewallet.io/buy-bitcoin-redirect.html?address=' + address;
if (preferredCurrency) {
uri += '&currency=' + preferredCurrency;
}
2020-09-05 02:47:34 +02:00
return uri;
}
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-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 (
<SafeBlueArea>
2020-07-15 19:32:59 +02:00
<StatusBar barStyle="default" />
<WebView
source={{
uri: this.state.uri,
}}
/>
</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-25 17:09:53 +01:00
BuyBitcoin.navigationOptions = navigationStyle({
closeButton: 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);
2021-04-16 19:04:33 +02:00
InAppBrowser.isAvailable()
.then(_value => {
InAppBrowser.open(uri, { dismissButtonStyle: 'done', modalEnabled: true, animated: true });
})
.catch(error => {
console.log(error);
Linking.openURL(uri);
2020-09-07 17:35:51 +02:00
});
};