BlueWallet/screen/send/create.js

207 lines
6.1 KiB
JavaScript
Raw Normal View History

2018-01-30 23:42:38 +01:00
import React, { Component } from 'react';
2018-03-18 03:48:23 +01:00
import { TextInput } from 'react-native';
import { Text, FormValidationMessage } from 'react-native-elements';
import { BlueLoading, BlueSpacing20, BlueButton, SafeBlueArea, BlueCard, BlueText, BlueSpacing } from '../../BlueComponents';
2018-03-18 03:48:23 +01:00
import PropTypes from 'prop-types';
2018-03-17 21:39:21 +01:00
let BigNumber = require('bignumber.js');
2018-03-18 03:48:23 +01:00
let BlueApp = require('../../BlueApp');
2018-05-28 21:18:11 +02:00
let loc = require('../../loc');
2018-01-30 23:42:38 +01:00
export default class SendCreate extends Component {
static navigationOptions = {
2018-06-28 03:43:28 +02:00
tabBarVisible: false,
2018-03-17 21:39:21 +01:00
};
2018-01-30 23:42:38 +01:00
constructor(props) {
super(props);
2018-03-17 21:39:21 +01:00
console.log('send/create constructor');
2018-01-30 23:42:38 +01:00
this.state = {
2018-03-17 21:39:21 +01:00
isLoading: true,
2018-01-30 23:42:38 +01:00
amount: props.navigation.state.params.amount,
fee: props.navigation.state.params.fee,
address: props.navigation.state.params.address,
memo: props.navigation.state.params.memo,
fromAddress: props.navigation.state.params.fromAddress,
2018-03-17 21:39:21 +01:00
broadcastErrorMessage: '',
};
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
let fromWallet = false;
2018-01-30 23:42:38 +01:00
for (let w of BlueApp.getWallets()) {
if (w.getAddress() === this.state.fromAddress) {
2018-03-17 21:39:21 +01:00
fromWallet = w;
2018-01-30 23:42:38 +01:00
break;
}
}
this.state['fromWallet'] = fromWallet;
}
async componentDidMount() {
2018-03-17 21:39:21 +01:00
console.log('send/create - componentDidMount');
console.log('address = ', this.state.address);
let utxo;
let satoshiPerByte;
let tx;
try {
await this.state.fromWallet.fetchUtxo();
utxo = this.state.fromWallet.utxo;
let startTime = Date.now();
2018-07-07 15:04:32 +02:00
tx = this.state.fromWallet.createTx(utxo, this.state.amount, this.state.fee, this.state.address, this.state.memo);
2018-03-17 21:39:21 +01:00
let endTime = Date.now();
console.log('create tx ', (endTime - startTime) / 1000, 'sec');
let bitcoin = require('bitcoinjs-lib');
let txDecoded = bitcoin.Transaction.fromHex(tx);
let txid = txDecoded.getId();
console.log('txid', txid);
console.log('txhex', tx);
BlueApp.tx_metadata = BlueApp.tx_metadata || {};
BlueApp.tx_metadata[txid] = {
txhex: tx,
memo: this.state.memo,
};
BlueApp.saveToDisk();
let feeSatoshi = new BigNumber(this.state.fee);
feeSatoshi = feeSatoshi.mul(100000000);
satoshiPerByte = feeSatoshi.div(Math.round(tx.length / 2));
satoshiPerByte = Math.round(satoshiPerByte.toString(10));
} catch (err) {
console.log(err);
return this.setState({
isError: true,
errorMessage: JSON.stringify(err.message),
});
}
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
this.setState({
isLoading: false,
size: Math.round(tx.length / 2),
tx,
satoshiPerByte,
});
2018-01-30 23:42:38 +01:00
}
async broadcast() {
2018-03-17 21:39:21 +01:00
let result = await this.state.fromWallet.broadcastTx(this.state.tx);
console.log('broadcast result = ', result);
2018-01-30 23:42:38 +01:00
if (typeof result === 'string') {
2018-03-17 21:39:21 +01:00
result = JSON.parse(result);
2018-01-30 23:42:38 +01:00
}
2018-03-17 21:39:21 +01:00
if (result && result.error) {
this.setState({
broadcastErrorMessage: JSON.stringify(result.error),
broadcastSuccessMessage: '',
});
2018-01-30 23:42:38 +01:00
} else {
2018-03-17 21:39:21 +01:00
this.setState({ broadcastErrorMessage: '' });
this.setState({
2018-07-07 15:04:32 +02:00
broadcastSuccessMessage: 'Success! TXID: ' + JSON.stringify(result.result),
2018-03-17 21:39:21 +01:00
});
2018-01-30 23:42:38 +01:00
}
}
render() {
if (this.state.isError) {
return (
2018-03-17 21:39:21 +01:00
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
<BlueSpacing />
2018-07-07 15:04:32 +02:00
<BlueCard title={loc.send.create.title} style={{ alignItems: 'center', flex: 1 }}>
2018-05-28 21:18:11 +02:00
<BlueText>{loc.send.create.error}</BlueText>
2018-07-07 15:04:32 +02:00
<FormValidationMessage>{this.state.errorMessage}</FormValidationMessage>
2018-01-30 23:42:38 +01:00
</BlueCard>
2018-07-07 15:04:32 +02:00
<BlueButton onPress={() => this.props.navigation.goBack()} title={loc.send.create.go_back} />
2018-01-30 23:42:38 +01:00
</SafeBlueArea>
);
}
if (this.state.isLoading) {
2018-03-17 21:39:21 +01:00
return <BlueLoading />;
2018-01-30 23:42:38 +01:00
}
return (
2018-03-17 21:39:21 +01:00
<SafeBlueArea style={{ flex: 1, paddingTop: 20 }}>
<BlueSpacing />
2018-07-07 15:04:32 +02:00
<BlueCard title={loc.send.create.title} style={{ alignItems: 'center', flex: 1 }}>
2018-05-28 21:18:11 +02:00
<BlueText>{loc.send.create.this_is_hex}</BlueText>
2018-01-30 23:42:38 +01:00
<TextInput
2018-03-17 21:39:21 +01:00
style={{
borderColor: '#ebebeb',
borderWidth: 1,
marginTop: 20,
color: '#ebebeb',
}}
2018-01-30 23:42:38 +01:00
maxHeight={70}
2018-03-17 21:39:21 +01:00
multiline
2018-01-30 23:42:38 +01:00
editable={false}
value={this.state.tx}
/>
2018-03-17 21:39:21 +01:00
<BlueSpacing20 />
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
<BlueText style={{ paddingTop: 20 }}>
2018-05-28 21:18:11 +02:00
{loc.send.create.to}: {this.state.address}
</BlueText>
<BlueText>
{loc.send.create.amount}: {this.state.amount} BTC
</BlueText>
<BlueText>
{loc.send.create.fee}: {this.state.fee} BTC
</BlueText>
<BlueText>
{loc.send.create.tx_size}: {this.state.size} Bytes
</BlueText>
<BlueText>
2018-07-07 15:04:32 +02:00
{loc.send.create.satoshi_per_byte}: {this.state.satoshiPerByte} Sat/B
2018-05-28 21:18:11 +02:00
</BlueText>
<BlueText>
{loc.send.create.memo}: {this.state.memo}
2018-03-17 21:39:21 +01:00
</BlueText>
2018-01-30 23:42:38 +01:00
</BlueCard>
<BlueButton
2018-06-25 00:22:46 +02:00
icon={{
name: 'megaphone',
type: 'octicon',
color: BlueApp.settings.buttonTextColor,
}}
2018-03-17 21:39:21 +01:00
onPress={() => this.broadcast()}
2018-05-28 21:18:11 +02:00
title={loc.send.create.broadcast}
2018-01-30 23:42:38 +01:00
/>
<BlueButton
2018-06-25 00:22:46 +02:00
icon={{
name: 'arrow-left',
type: 'octicon',
color: BlueApp.settings.buttonTextColor,
}}
2018-03-17 21:39:21 +01:00
onPress={() => this.props.navigation.goBack()}
2018-05-28 21:18:11 +02:00
title={loc.send.create.go_back}
2018-01-30 23:42:38 +01:00
/>
2018-07-07 15:04:32 +02:00
<FormValidationMessage>{this.state.broadcastErrorMessage}</FormValidationMessage>
<Text style={{ padding: 0, color: '#0f0' }}>{this.state.broadcastSuccessMessage}</Text>
2018-01-30 23:42:38 +01:00
</SafeBlueArea>
);
}
}
2018-03-18 03:48:23 +01:00
SendCreate.propTypes = {
navigation: PropTypes.shape({
goBack: PropTypes.function,
state: PropTypes.shape({
params: PropTypes.shape({
amount: PropTypes.string,
fee: PropTypes.string,
address: PropTypes.string,
memo: PropTypes.string,
fromAddress: PropTypes.string,
}),
}),
}),
};