Skip to content

Commit

Permalink
REF: export wallet history to CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
Overtorment committed Oct 12, 2022
1 parent 92880d4 commit afab9b8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions loc/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
"details_from": "Input",
"details_inputs": "Inputs",
"details_outputs": "Outputs",
"date": "Date",
"details_received": "Received",
"transaction_note_saved": "Transaction note has been successfully saved.",
"details_show_in_block_explorer": "View in Block Explorer",
Expand Down Expand Up @@ -392,6 +393,7 @@
"details_derivation_path": "derivation path",
"details_display": "Display in Wallets List",
"details_export_backup": "Export/Backup",
"details_export_history": "Export History to CSV",
"details_master_fingerprint": "Master Fingerprint",
"details_multisig_type": "multisig",
"details_no_cancel": "No, cancel",
Expand Down
33 changes: 30 additions & 3 deletions screen/wallets/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
HDAezeedWallet,
LightningLdkWallet,
} from '../../class';
import loc from '../../loc';
import loc, { formatBalanceWithoutSuffix } from '../../loc';
import { useTheme, useRoute, useNavigation } from '@react-navigation/native';
import RNFS from 'react-native-fs';
import Share from 'react-native-share';
Expand All @@ -43,6 +43,8 @@ import Notifications from '../../blue_modules/notifications';
import { isDesktop } from '../../blue_modules/environment';
import { AbstractHDElectrumWallet } from '../../class/wallets/abstract-hd-electrum-wallet';
import alert from '../../components/Alert';
import { BitcoinUnit, Chain } from '../../models/bitcoinUnits';
import { writeFileAndExport } from '../../blue_modules/fs';

const prompt = require('../../helpers/prompt');

Expand Down Expand Up @@ -117,7 +119,7 @@ const styles = StyleSheet.create({
});

const WalletDetails = () => {
const { saveToDisk, wallets, deleteWallet, setSelectedWallet } = useContext(BlueStorageContext);
const { saveToDisk, wallets, deleteWallet, setSelectedWallet, txMetadata } = useContext(BlueStorageContext);
const { walletID } = useRoute().params;
const [isLoading, setIsLoading] = useState(false);
const [backdoorPressed, setBackdoorPressed] = useState(0);
Expand All @@ -130,6 +132,7 @@ const WalletDetails = () => {
const { goBack, navigate, setOptions, popToTop } = useNavigation();
const { colors } = useTheme();
const [masterFingerprint, setMasterFingerprint] = useState();
const walletTransactionsLength = useMemo(() => wallet.getTransactions().length, [wallet]);
const derivationPath = useMemo(() => {
try {
const path = wallet.getDerivationPath();
Expand Down Expand Up @@ -402,6 +405,25 @@ const WalletDetails = () => {
}
};

const onExportHistoryPressed = async () => {
let csvFile = [
loc.transactions.date,
loc.transactions.txid,
`${loc.send.create_amount} (${BitcoinUnit.BTC})`,
loc.send.create_memo,
].join(','); // CSV header
const transactions = wallet.getTransactions();

for (const transaction of transactions) {
const value = formatBalanceWithoutSuffix(transaction.value, BitcoinUnit.BTC, true);
csvFile +=
'\n' +
[new Date(transaction.received).toString(), transaction.hash, value, txMetadata[transaction.hash]?.memo?.trim() ?? ''].join(','); // CSV line
}

await writeFileAndExport(`${wallet.label.replace(' ', '-')}-history.csv`, csvFile);
};

const handleDeleteButtonTapped = () => {
ReactNativeHapticFeedback.trigger('notificationWarning', { ignoreAndroidSystemSettings: false });
Alert.alert(
Expand Down Expand Up @@ -579,7 +601,12 @@ const WalletDetails = () => {
<View>
<BlueSpacing20 />
<SecondButton onPress={navigateToWalletExport} testID="WalletExport" title={loc.wallets.details_export_backup} />

{wallet.chain === Chain.ONCHAIN && walletTransactionsLength > 0 && (
<>
<BlueSpacing20 />
<SecondButton onPress={onExportHistoryPressed} title={loc.wallets.details_export_history} />
</>
)}
{wallet.type === MultisigHDWallet.type && (
<>
<BlueSpacing20 />
Expand Down

0 comments on commit afab9b8

Please sign in to comment.