BlueWallet/screen/transactions/details.js

245 lines
7.2 KiB
JavaScript
Raw Normal View History

2018-01-30 22:42:38 +00:00
import React, { Component } from 'react';
import { View, ScrollView, TouchableOpacity, Linking, StyleSheet, StatusBar } from 'react-native';
import {
SafeBlueArea,
BlueCard,
BlueText,
BlueHeaderDefaultSub,
BlueLoading,
BlueSpacing20,
BlueCopyToClipboardButton,
BlueNavigationStyle,
} from '../../BlueComponents';
2020-03-30 09:50:19 -04:00
import HandoffSettings from '../../class/handoff';
import Handoff from 'react-native-handoff';
2018-03-18 02:48:23 +00:00
import PropTypes from 'prop-types';
/** @type {AppStorage} */
const BlueApp = require('../../BlueApp');
const loc = require('../../loc');
const dayjs = require('dayjs');
const styles = StyleSheet.create({
root: {
flex: 1,
},
scroll: {
flex: 1,
},
rowHeader: {
flex: 1,
flexDirection: 'row',
marginBottom: 4,
justifyContent: 'space-between',
},
rowCaption: {
fontSize: 16,
fontWeight: '500',
marginBottom: 4,
},
rowValue: {
marginBottom: 26,
color: 'grey',
},
txId: {
fontSize: 16,
fontWeight: '500',
},
txHash: {
marginBottom: 8,
color: 'grey',
},
txLink: {
marginBottom: 26,
color: '#2f5fb3',
},
});
2018-09-11 19:17:24 +01:00
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
function arrDiff(a1, a2) {
const ret = [];
for (const v of a2) {
2018-09-11 19:17:24 +01:00
if (a1.indexOf(v) === -1) {
ret.push(v);
}
}
return ret;
}
2018-01-30 22:42:38 +00:00
export default class TransactionsDetails extends Component {
static navigationOptions = () => ({
...BlueNavigationStyle(),
});
2018-01-30 22:42:38 +00:00
constructor(props) {
super(props);
const hash = props.route.params.hash;
2018-03-17 22:39:21 +02:00
let foundTx = {};
let from = [];
let to = [];
for (const tx of BlueApp.getTransactions()) {
2018-01-30 22:42:38 +00:00
if (tx.hash === hash) {
foundTx = tx;
for (const input of foundTx.inputs) {
2018-03-17 22:39:21 +02:00
from = from.concat(input.addresses);
2018-01-30 22:42:38 +00:00
}
for (const output of foundTx.outputs) {
if (output.addresses) to = to.concat(output.addresses);
2019-06-11 00:18:40 +01:00
if (output.scriptPubKey && output.scriptPubKey.addresses) to = to.concat(output.scriptPubKey.addresses);
2018-01-30 22:42:38 +00:00
}
}
}
2018-10-31 20:14:28 +00:00
let wallet = false;
for (const w of BlueApp.getWallets()) {
for (const t of w.getTransactions()) {
2018-10-31 20:14:28 +00:00
if (t.hash === hash) {
console.log('tx', hash, 'belongs to', w.getLabel());
wallet = w;
}
}
}
2018-01-30 22:42:38 +00:00
this.state = {
isLoading: true,
tx: foundTx,
from,
to,
2018-10-31 20:14:28 +00:00
wallet,
2020-03-30 09:50:19 -04:00
isHandOffUseEnabled: false,
2018-03-17 22:39:21 +02:00
};
2018-01-30 22:42:38 +00:00
}
async componentDidMount() {
2018-03-17 22:39:21 +02:00
console.log('transactions/details - componentDidMount');
2020-03-30 09:50:19 -04:00
const isHandOffUseEnabled = await HandoffSettings.isHandoffUseEnabled();
2018-01-30 22:42:38 +00:00
this.setState({
isLoading: false,
2020-03-30 09:50:19 -04:00
isHandOffUseEnabled,
2018-03-17 22:39:21 +02:00
});
}
2018-01-30 22:42:38 +00:00
render() {
if (this.state.isLoading || !('tx' in this.state)) {
2018-03-17 22:39:21 +02:00
return <BlueLoading />;
2018-01-30 22:42:38 +00:00
}
return (
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
<StatusBar barStyle="dark-content" />
2020-03-30 09:50:19 -04:00
{this.state.isHandOffUseEnabled && (
<Handoff
title={`Bitcoin Transaction ${this.state.tx.hash}`}
type="io.bluewallet.bluewallet"
url={`https://blockstream.info/tx/${this.state.tx.hash}`}
/>
)}
<BlueHeaderDefaultSub leftText={loc.transactions.details.title} rightComponent={null} />
<ScrollView style={styles.scroll}>
<BlueCard>
{(() => {
if (BlueApp.tx_metadata[this.state.tx.hash]) {
if (BlueApp.tx_metadata[this.state.tx.hash].memo) {
return (
<View>
<BlueText h4>{BlueApp.tx_metadata[this.state.tx.hash].memo}</BlueText>
<BlueSpacing20 />
</View>
);
}
2018-01-30 22:42:38 +00:00
}
})()}
2018-01-30 22:42:38 +00:00
{'from' in this.state && (
<>
<View style={styles.rowHeader}>
<BlueText style={styles.rowCaption}>{loc.transactions.details.from}</BlueText>
<BlueCopyToClipboardButton stringToCopy={this.state.from.filter(onlyUnique).join(', ')} />
</View>
<BlueText style={styles.rowValue}>{this.state.from.filter(onlyUnique).join(', ')}</BlueText>
</>
)}
{'to' in this.state && (
<>
<View style={styles.rowHeader}>
<BlueText style={styles.rowCaption}>{loc.transactions.details.to}</BlueText>
<BlueCopyToClipboardButton stringToCopy={this.state.to.filter(onlyUnique).join(', ')} />
</View>
<BlueText style={styles.rowValue}>{arrDiff(this.state.from, this.state.to.filter(onlyUnique)).join(', ')}</BlueText>
</>
)}
{'fee' in this.state.tx && (
<>
<BlueText style={styles.rowCaption}>{loc.send.create.fee}</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.fee + ' sats'}</BlueText>
</>
2018-12-18 20:46:06 -05:00
)}
{'hash' in this.state.tx && (
<>
<View style={styles.rowHeader}>
<BlueText style={styles.txId}>Txid</BlueText>
<BlueCopyToClipboardButton stringToCopy={this.state.tx.hash} />
</View>
<BlueText style={styles.txHash}>{this.state.tx.hash}</BlueText>
<TouchableOpacity
onPress={() => {
const url = `https://blockstream.info/tx/${this.state.tx.hash}`;
Linking.canOpenURL(url).then(supported => {
if (supported) {
Linking.openURL(url);
}
});
}}
>
<BlueText style={styles.txLink}>{loc.transactions.details.show_in_block_explorer}</BlueText>
</TouchableOpacity>
</>
)}
2018-12-18 20:46:06 -05:00
{'received' in this.state.tx && (
<>
<BlueText style={styles.rowCaption}>Received</BlueText>
<BlueText style={styles.rowValue}>{dayjs(this.state.tx.received).format('MM/DD/YYYY h:mm A')}</BlueText>
</>
)}
{'block_height' in this.state.tx && this.state.tx.block_height > 0 && (
<>
<BlueText style={styles.rowCaption}>Block Height</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.block_height}</BlueText>
</>
)}
{'inputs' in this.state.tx && (
<>
<BlueText style={styles.rowCaption}>Inputs</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.inputs.length}</BlueText>
</>
)}
{'outputs' in this.state.tx && this.state.tx.outputs.length > 0 && (
<>
<BlueText style={styles.rowCaption}>Outputs</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.outputs.length}</BlueText>
</>
)}
</BlueCard>
</ScrollView>
2018-01-30 22:42:38 +00:00
</SafeBlueArea>
);
}
}
2018-03-18 02:48:23 +00:00
TransactionsDetails.propTypes = {
2020-05-27 14:12:17 +03:00
route: PropTypes.shape({
name: PropTypes.string,
params: PropTypes.shape({
hash: PropTypes.string,
2018-03-18 02:48:23 +00:00
}),
}),
};