-
-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathexportMetadataToBip329File.ts
More file actions
48 lines (42 loc) · 1.63 KB
/
Copy pathexportMetadataToBip329File.ts
File metadata and controls
48 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { METADATA } from '@suite/metadata';
import { type Bip329Dep } from '@suite-common/bip329-types';
import { createThunk } from '@suite-common/redux-utils';
import { triggerWebDownloadFile } from '@suite-common/suite-utils';
import { notificationsActions } from '@suite-common/toast-notifications';
import { type Account } from '@suite-common/wallet-types';
import { sanitizeFilename } from '@trezor/utils';
type ExportMetadataToBip329FileThunkDeps = {
services: Bip329Dep;
};
export const exportMetadataToBip329File = createThunk<
void,
{
account: Account;
defaultAccountLabel: string;
},
{ extra: ExportMetadataToBip329FileThunkDeps }
>(
METADATA.EXPORT_METADATA_TO_BIP329_FILE,
({ account, defaultAccountLabel }, { dispatch, extra: { services } }) => {
const showExportErrorToast = () => {
dispatch(
notificationsActions.addToast({
type: 'error',
error: 'Exporting labels BIP 329 failed',
}),
);
};
try {
const { accountLabel, labelsToExport } = services.bip329.export({
account,
});
const safeLabel = sanitizeFilename(accountLabel ?? defaultAccountLabel);
const jsonlString = labelsToExport.map(obj => JSON.stringify(obj)).join('\n');
const blob = new Blob([jsonlString], { type: 'application/jsonl' });
const filename = `${safeLabel || 'account_labels'}_export_bip329.jsonl`;
triggerWebDownloadFile(blob, filename);
} catch {
showExportErrorToast();
}
},
);