Skip to content

Commit

Permalink
[Issue-1826] Implement Export Account
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhquang committed Oct 31, 2024
1 parent 507bb2a commit e7a64be
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 765 deletions.
50 changes: 18 additions & 32 deletions src/components/common/SelectExportType/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isEthereumAddress } from '@polkadot/util-crypto';
import { AccountJson } from '@subwallet/extension-base/background/types';
import { BackgroundIcon } from 'components/design-system-ui';
import React, { useCallback, useMemo } from 'react';
import { Text, View, ViewStyle } from 'react-native';
Expand All @@ -8,7 +6,7 @@ import { useSubWalletTheme } from 'hooks/useSubWalletTheme';
import SelectExportTypeStyles from './style';
import { FileJs, IconProps, Leaf, QrCode, Wallet } from 'phosphor-react-native';
import i18n from 'utils/i18n/i18n';
import AlertBox from 'components/design-system-ui/alert-box/simple';
import { AccountActions, AccountProxy } from '@subwallet/extension-base/types';

export enum ExportType {
JSON_FILE = 'json-file',
Expand All @@ -30,14 +28,14 @@ interface ExportTypeItem {
interface SelectAccountTypeProps {
selectedItems: ExportType[];
setSelectedItems: React.Dispatch<React.SetStateAction<ExportType[]>>;
account: AccountJson | null;
accountProxy: AccountProxy | null;
title?: string;
styles?: ViewStyle;
loading: boolean;
}

export const SelectExportType = (props: SelectAccountTypeProps) => {
const { title, selectedItems, setSelectedItems, styles, account, loading } = props;
const { title, selectedItems, setSelectedItems, styles, accountProxy, loading } = props;
const theme = useSubWalletTheme().swThemes;
const _style = SelectExportTypeStyles(theme);

Expand Down Expand Up @@ -67,7 +65,7 @@ export const SelectExportType = (props: SelectAccountTypeProps) => {
label: i18n.exportAccount.exportSeedPhrase,
onClick: onClickItem(ExportType.SEED_PHRASE),
bgColor: theme['orange-7'],
disable: !account || account.isExternal || !account.isMasterAccount,
disable: !accountProxy || !accountProxy.accountActions.includes(AccountActions.EXPORT_MNEMONIC),
hidden: false,
},
{
Expand All @@ -76,7 +74,7 @@ export const SelectExportType = (props: SelectAccountTypeProps) => {
label: i18n.exportAccount.exportJsonFile,
onClick: onClickItem(ExportType.JSON_FILE),
bgColor: theme['geekblue-7'],
disable: !account || !!account.isExternal,
disable: !accountProxy || !accountProxy.accountActions.includes(AccountActions.EXPORT_JSON),
hidden: false,
},
{
Expand All @@ -85,45 +83,33 @@ export const SelectExportType = (props: SelectAccountTypeProps) => {
label: i18n.exportAccount.exportPrivateKey,
onClick: onClickItem(ExportType.PRIVATE_KEY),
bgColor: theme['magenta-7'],
disable: !account || account.isExternal || !isEthereumAddress(account.address),
hidden: !isEthereumAddress(account?.address || ''),
disable: !accountProxy || !accountProxy.accountActions.includes(AccountActions.EXPORT_PRIVATE_KEY),
hidden: false,
},
{
icon: QrCode,
key: ExportType.QR_CODE,
label: i18n.exportAccount.exportQRCode,
onClick: onClickItem(ExportType.QR_CODE),
bgColor: theme['green-7'],
disable: !account || !!account?.isExternal,
disable: !accountProxy || !accountProxy.accountActions.includes(AccountActions.EXPORT_QR),
hidden: false,
},
],
[onClickItem, theme, account],
[accountProxy, onClickItem, theme],
);

const accountType = useMemo(() => {
if (account?.isExternal) {
if (account?.isReadOnly) {
return 'watch-only';
} else if (account?.isHardware && account?.hardwareType === 'ledger') {
return 'ledger';
} else {
return 'QR-signer';
}
}
}, [account]);

return (
<View style={[styles]}>
{!!account?.isExternal && (
<View style={{ paddingBottom: theme.paddingSM }}>
<AlertBox
title={i18n.warningTitle.payAttention}
description={`Your account is ${accountType} account, which doesn't support export and back up. Change to another account to use this feature`}
type="warning"
/>
</View>
)}
{/*{!!account?.isExternal && (*/}
{/* <View style={{ paddingBottom: theme.paddingSM }}>*/}
{/* <AlertBox*/}
{/* title={i18n.warningTitle.payAttention}*/}
{/* description={`Your account is ${accountProxy?.accountType} account, which doesn't support export and back up. Change to another account to use this feature`}*/}
{/* type="warning"*/}
{/* />*/}
{/* </View>*/}
{/*)}*/}
{title && (
<View>
<Text style={_style.titleStyle}>{title}</Text>
Expand Down
36 changes: 36 additions & 0 deletions src/messaging/accounts/export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2019-2022 @subwallet/extension-koni-ui authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { ResponseAccountExportPrivateKey } from '@subwallet/extension-base/background/KoniTypes';
import {
RequestAccountBatchExportV2,
RequestExportAccountProxyMnemonic,
ResponseExportAccountProxyMnemonic,
} from '@subwallet/extension-base/types';
import { KeyringPair$Json } from '@subwallet/keyring/types';
import { KeyringPairs$Json } from '@subwallet/ui-keyring/types';
import { sendMessage } from 'messaging/index';

// JSON
export async function exportAccount(address: string, password: string): Promise<{ exportedJson: KeyringPair$Json }> {
return sendMessage('pri(accounts.export.json)', { address, password });
}

export async function exportAccountPrivateKey(
address: string,
password: string,
): Promise<ResponseAccountExportPrivateKey> {
return sendMessage('pri(accounts.export.privateKey)', { address, password });
}

export async function exportAccountBatch(
request: RequestAccountBatchExportV2,
): Promise<{ exportedJson: KeyringPairs$Json }> {
return sendMessage('pri(accounts.export.json.batch)', request);
}

export async function exportAccountMnemonic(
request: RequestExportAccountProxyMnemonic,
): Promise<ResponseExportAccountProxyMnemonic> {
return sendMessage('pri(accounts.export.mnemonic)', request);
}
1 change: 1 addition & 0 deletions src/messaging/accounts/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './validate';
export * from './create';
export * from './json';
export * from './export';
Loading

0 comments on commit e7a64be

Please sign in to comment.