BlueWallet/screen/transactions/details.js

298 lines
9 KiB
JavaScript
Raw Normal View History

2020-06-10 18:24:47 -04:00
/* global alert */
2018-01-30 22:42:38 +00:00
import React, { Component } from 'react';
import { View, ScrollView, TouchableOpacity, Text, TextInput, Linking, StatusBar, StyleSheet } from 'react-native';
import {
SafeBlueArea,
BlueCard,
BlueText,
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';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
2020-07-15 13:32:59 -04:00
import { BlueCurrentTheme } from '../../components/themes';
2018-03-18 02:48:23 +00:00
/** @type {AppStorage} */
const BlueApp = require('../../BlueApp');
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,
2020-07-15 13:32:59 -04:00
color: BlueCurrentTheme.colors.foregroundColor,
},
rowValue: {
marginBottom: 26,
color: 'grey',
},
txId: {
fontSize: 16,
fontWeight: '500',
2020-07-15 13:32:59 -04:00
color: BlueCurrentTheme.colors.foregroundColor,
},
txHash: {
marginBottom: 8,
color: 'grey',
},
txLink: {
marginBottom: 26,
2020-07-15 13:32:59 -04:00
color: BlueCurrentTheme.colors.alternativeTextColor2,
},
2020-06-10 18:24:47 -04:00
save: {
marginHorizontal: 16,
justifyContent: 'center',
alignItems: 'center',
},
2020-07-15 13:32:59 -04:00
saveText: {
color: BlueCurrentTheme.colors.alternativeTextColor2,
},
2020-06-10 18:24:47 -04:00
memoTextInput: {
2020-07-15 13:32:59 -04:00
flexDirection: 'row',
borderColor: BlueCurrentTheme.colors.formBorder,
borderBottomColor: BlueCurrentTheme.colors.formBorder,
borderWidth: 1,
borderBottomWidth: 0.5,
backgroundColor: BlueCurrentTheme.colors.inputBackgroundColor,
minHeight: 44,
height: 44,
alignItems: 'center',
marginVertical: 8,
borderRadius: 4,
paddingHorizontal: 8,
color: '#81868e',
2020-06-10 18:24:47 -04:00
},
});
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 {
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;
}
}
}
2020-06-10 18:24:47 -04:00
let memo = '';
if (BlueApp.tx_metadata[foundTx.hash]) {
if (BlueApp.tx_metadata[foundTx.hash].memo) {
memo = BlueApp.tx_metadata[foundTx.hash].memo;
}
}
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,
2020-06-10 18:24:47 -04:00
memo,
2018-03-17 22:39:21 +02:00
};
2020-06-10 18:24:47 -04:00
props.navigation.setParams({ handleOnSaveButtonTapped: this.handleOnSaveButtonTapped });
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
});
}
2020-06-10 18:24:47 -04:00
handleOnSaveButtonTapped = () => {
BlueApp.tx_metadata[this.state.tx.hash] = { memo: this.state.memo };
BlueApp.saveToDisk().then(_success => alert('Transaction note has been successfully saved.'));
};
handleOnMemoChangeText = value => {
this.setState({ memo: value });
};
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}>
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}`}
/>
)}
2020-07-15 13:32:59 -04:00
<StatusBar barStyle="default" />
<ScrollView style={styles.scroll}>
<BlueCard>
2020-06-10 18:24:47 -04:00
<View>
<TextInput
2020-07-20 16:38:46 +03:00
placeholder={loc.send.details_note_placeholder}
2020-06-10 18:24:47 -04:00
value={this.state.memo}
placeholderTextColor="#81868e"
style={styles.memoTextInput}
onChangeText={this.handleOnMemoChangeText}
/>
<BlueSpacing20 />
</View>
2018-01-30 22:42:38 +00:00
{'from' in this.state && (
<>
<View style={styles.rowHeader}>
2020-07-20 16:38:46 +03:00
<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}>
2020-07-20 16:38:46 +03:00
<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 && (
<>
2020-07-20 16:38:46 +03:00
<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);
}
});
}}
>
2020-07-20 16:38:46 +03:00
<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 && (
<>
2020-07-20 16:38:46 +03:00
<BlueText style={styles.rowCaption}>{loc.transactions.details_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 && (
<>
2020-07-20 16:38:46 +03:00
<BlueText style={styles.rowCaption}>{loc.transactions.details_block}</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.block_height}</BlueText>
</>
)}
{'inputs' in this.state.tx && (
<>
2020-07-20 16:38:46 +03:00
<BlueText style={styles.rowCaption}>{loc.transactions.details_inputs}</BlueText>
<BlueText style={styles.rowValue}>{this.state.tx.inputs.length}</BlueText>
</>
)}
{'outputs' in this.state.tx && this.state.tx.outputs.length > 0 && (
<>
2020-07-20 16:38:46 +03:00
<BlueText style={styles.rowCaption}>{loc.transactions.details_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
}),
}),
2020-06-10 18:24:47 -04:00
navigation: PropTypes.shape({
setParams: PropTypes.func,
}),
2018-03-18 02:48:23 +00:00
};
2020-07-15 13:32:59 -04:00
TransactionsDetails.navigationOptions = ({ navigation, route }) => ({
...BlueNavigationStyle(),
2020-07-20 16:38:46 +03:00
title: loc.transactions.details_title,
2020-07-15 13:32:59 -04:00
headerStyle: {
...BlueNavigationStyle().headerStyle,
backgroundColor: BlueCurrentTheme.colors.customHeader,
},
headerRight: () => (
<TouchableOpacity disabled={route.params.isLoading === true} style={styles.save} onPress={route.params.handleOnSaveButtonTapped}>
2020-07-20 16:38:46 +03:00
<Text style={styles.saveText}>{loc.wallets.details_save}</Text>
2020-07-15 13:32:59 -04:00
</TouchableOpacity>
),
});