2018-10-28 10:13:28 +00:00
|
|
|
import React, { Component } from 'react';
|
2020-12-04 13:39:47 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2021-07-21 13:42:24 +02:00
|
|
|
import { StatusBar } from 'react-native';
|
2019-04-21 19:50:28 +01:00
|
|
|
import { WebView } from 'react-native-webview';
|
2020-12-25 19:09:53 +03:00
|
|
|
import { BlueLoading, SafeBlueArea } from '../../BlueComponents';
|
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
|
|
|
import { LightningCustodianWallet, WatchOnlyWallet } from '../../class';
|
2020-10-24 13:20:59 -04:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2021-05-29 01:44:50 -04:00
|
|
|
import * as NavigationService from '../../NavigationService';
|
2021-09-25 18:45:13 +01:00
|
|
|
import { FiatUnit } from '../../models/fiatUnit';
|
2021-05-29 01:44:50 -04:00
|
|
|
|
2020-06-09 15:08:18 +01:00
|
|
|
const currency = require('../../blue_modules/currency');
|
2020-12-25 19:09:53 +03:00
|
|
|
|
2018-10-28 10:13:28 +00:00
|
|
|
export default class BuyBitcoin extends Component {
|
2020-10-24 13:20:59 -04:00
|
|
|
static contextType = BlueStorageContext;
|
2021-06-17 17:24:00 -04:00
|
|
|
constructor(props, context) {
|
2018-10-28 10:13:28 +00:00
|
|
|
super(props);
|
2021-06-17 17:24:00 -04:00
|
|
|
const wallet = context.wallets.find(w => w.getID() === props.route.params.walletID);
|
2020-04-29 13:37:09 +01:00
|
|
|
if (!wallet) console.warn('wallet was not passed to buyBitcoin');
|
2018-10-28 10:13:28 +00:00
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isLoading: true,
|
2020-04-17 15:41:35 +01:00
|
|
|
wallet,
|
2020-08-12 11:30:37 -04:00
|
|
|
uri: '',
|
2018-10-28 10:13:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-04 20:47:34 -04:00
|
|
|
static async generateURL(wallet) {
|
2021-09-25 18:45:13 +01:00
|
|
|
let preferredCurrency;
|
|
|
|
try {
|
|
|
|
preferredCurrency = await currency.getPreferredCurrency();
|
|
|
|
} catch (_) {
|
|
|
|
preferredCurrency = FiatUnit.USD;
|
|
|
|
}
|
2020-04-24 17:37:46 +01:00
|
|
|
preferredCurrency = preferredCurrency.endPointKey;
|
|
|
|
|
2020-04-17 15:41:35 +01:00
|
|
|
/** @type {AbstractHDWallet|WatchOnlyWallet|LightningCustodianWallet} */
|
|
|
|
|
|
|
|
let address = '';
|
2018-10-28 10:13:28 +00:00
|
|
|
|
2020-04-17 15:41:35 +01:00
|
|
|
if (WatchOnlyWallet.type === wallet.type && !wallet.isHd()) {
|
|
|
|
// plain watchonly - just get the address
|
|
|
|
address = wallet.getAddress();
|
2020-08-12 11:30:37 -04:00
|
|
|
} else {
|
|
|
|
// otherwise, lets call widely-used getAddressAsync()
|
|
|
|
try {
|
2021-02-24 19:32:57 +00:00
|
|
|
address = await Promise.race([wallet.getAddressAsync(), new Promise(resolve => setTimeout(resolve, 2000))]);
|
2020-08-12 11:30:37 -04: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 10:13:28 +00:00
|
|
|
}
|
2020-04-17 15:41:35 +01:00
|
|
|
|
2020-08-12 11:30:37 -04:00
|
|
|
let uri = 'https://bluewallet.io/buy-bitcoin-redirect.html?address=' + address;
|
2020-04-17 15:41:35 +01:00
|
|
|
|
2020-08-12 11:30:37 -04:00
|
|
|
if (preferredCurrency) {
|
|
|
|
uri += '¤cy=' + preferredCurrency;
|
|
|
|
}
|
2020-09-04 20:47:34 -04:00
|
|
|
return uri;
|
|
|
|
}
|
2020-08-12 11:30:37 -04:00
|
|
|
|
2020-09-04 20:47:34 -04: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 11:30:37 -04:00
|
|
|
}
|
2020-09-04 20:47:34 -04:00
|
|
|
this.setState({ uri, isLoading: false });
|
2018-10-28 10:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return <BlueLoading />;
|
|
|
|
}
|
|
|
|
|
2019-05-12 21:50:08 +01:00
|
|
|
return (
|
2021-03-22 07:54:17 -04:00
|
|
|
<SafeBlueArea>
|
2020-07-15 13:32:59 -04:00
|
|
|
<StatusBar barStyle="default" />
|
2021-05-29 01:44:50 -04:00
|
|
|
|
2020-04-16 15:43:53 +02:00
|
|
|
<WebView
|
2021-05-29 01:44:50 -04:00
|
|
|
mediaPlaybackRequiresUserAction={false}
|
|
|
|
enableApplePay
|
2020-04-16 15:43:53 +02:00
|
|
|
source={{
|
2020-08-12 11:30:37 -04:00
|
|
|
uri: this.state.uri,
|
2020-04-16 15:43:53 +02:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</SafeBlueArea>
|
2019-05-12 21:50:08 +01:00
|
|
|
);
|
2018-10-28 10:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BuyBitcoin.propTypes = {
|
2020-05-27 14:12:17 +03:00
|
|
|
route: PropTypes.shape({
|
|
|
|
name: PropTypes.string,
|
|
|
|
params: PropTypes.shape({
|
2021-06-17 17:24:00 -04:00
|
|
|
walletID: PropTypes.string.isRequired,
|
2020-05-27 14:12:17 +03:00
|
|
|
safelloStateToken: PropTypes.string,
|
2018-10-28 10:13:28 +00:00
|
|
|
}),
|
|
|
|
}),
|
|
|
|
};
|
2020-07-15 13:32:59 -04:00
|
|
|
|
2020-12-25 19:09:53 +03:00
|
|
|
BuyBitcoin.navigationOptions = navigationStyle({
|
|
|
|
closeButton: true,
|
2020-07-15 13:32:59 -04:00
|
|
|
title: '',
|
2021-09-13 13:43:26 -04:00
|
|
|
stackPresentation: 'modal',
|
|
|
|
headerHideBackButton: true,
|
2020-07-15 13:32:59 -04:00
|
|
|
});
|
2020-09-07 11:35:51 -04:00
|
|
|
|
|
|
|
BuyBitcoin.navigate = async wallet => {
|
2021-07-21 13:42:24 +02:00
|
|
|
NavigationService.navigate('BuyBitcoin', {
|
|
|
|
walletID: wallet.getID(),
|
|
|
|
});
|
2020-09-07 11:35:51 -04:00
|
|
|
};
|