BlueWallet/screen/wallets/buyBitcoin.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

2018-10-28 11:13:28 +01:00
import React, { Component } from 'react';
import { BlueNavigationStyle, BlueLoading, SafeBlueArea } from '../../BlueComponents';
2018-10-28 11:13:28 +01:00
import PropTypes from 'prop-types';
import { WebView } from 'react-native-webview';
import { AppStorage, LightningCustodianWallet, WatchOnlyWallet } from '../../class';
const currency = require('../../currency');
let BlueApp: AppStorage = require('../../BlueApp');
2018-10-28 11:13:28 +01:00
let loc = require('../../loc');
export default class BuyBitcoin extends Component {
static navigationOptions = ({ navigation }) => ({
...BlueNavigationStyle(navigation, true),
title: loc.buyBitcoin.header,
headerLeft: null,
});
2018-10-28 11:13:28 +01:00
constructor(props) {
super(props);
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,
wallet,
address: '',
2018-10-28 11:13:28 +01:00
};
}
async componentDidMount() {
console.log('buyBitcoin - componentDidMount');
2018-10-28 11:13:28 +01:00
let preferredCurrency = await currency.getPreferredCurrency();
preferredCurrency = preferredCurrency.endPointKey;
/** @type {AbstractHDWallet|WatchOnlyWallet|LightningCustodianWallet} */
let wallet = this.state.wallet;
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();
2018-10-28 11:13:28 +01:00
this.setState({
isLoading: false,
address,
preferredCurrency,
2018-10-28 11:13:28 +01:00
});
return;
2018-10-28 11:13:28 +01: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,
preferredCurrency,
});
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-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;
}
if (this.state.preferredCurrency) {
uri += '&currency=' + this.state.preferredCurrency;
}
2019-05-12 22:50:08 +02:00
return (
<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({
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
}),
}),
}),
};