Skip to content

Commit 1b69e9b

Browse files
committed
feat enable naming imported accounts
1 parent 94ad11c commit 1b69e9b

File tree

9 files changed

+51
-18
lines changed

9 files changed

+51
-18
lines changed

src/app/pages/ImportAccount.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React, { FC, ReactNode, useCallback, useEffect, useRef, useState } from 'react';
1+
import React, { FC, ReactNode, useCallback, useEffect, useMemo, useRef, useState } from 'react';
22

33
import { useForm } from 'react-hook-form';
44
import { useTranslation } from 'react-i18next';
55

66
import Alert from 'app/atoms/Alert';
77
import FormField from 'app/atoms/FormField';
88
import FormSubmitButton from 'app/atoms/FormSubmitButton';
9+
import { ACCOUNT_NAME_PATTERN } from 'app/defaults';
910
import PageLayout from 'app/layouts/PageLayout';
1011
import { useMidenContext, useAllAccounts } from 'lib/miden/front';
1112
import { navigate } from 'lib/woozie';
@@ -39,7 +40,7 @@ const ImportAccount: FC<ImportAccountProps> = ({ tabSlug }) => {
3940
</>
4041
}
4142
>
42-
<div className="p-4">
43+
<div className="px-4">
4344
<ByPrivateKeyForm />
4445
</div>
4546
</PageLayout>
@@ -50,27 +51,37 @@ export default ImportAccount;
5051

5152
interface ByPrivateKeyFormData {
5253
privateKey: string;
54+
name?: string;
5355
encPassword?: string;
5456
}
5557

5658
const ByPrivateKeyForm: FC = () => {
5759
const { t } = useTranslation();
5860
const { importPublicAccountByPrivateKey } = useMidenContext();
59-
61+
const allAccounts = useAllAccounts();
6062
const {
6163
register,
6264
handleSubmit,
65+
setValue,
6366
formState: { errors, isSubmitting }
6467
} = useForm<ByPrivateKeyFormData>();
6568
const [error, setError] = useState<ReactNode>(null);
6669

70+
const computedDefaultName = useMemo(() => {
71+
return `Imported Acc ${allAccounts.length + 1}`;
72+
}, [allAccounts]);
73+
74+
useEffect(() => {
75+
setValue('name', computedDefaultName);
76+
}, [computedDefaultName, setValue]);
77+
6778
const onSubmit = useCallback(
68-
async ({ privateKey }: ByPrivateKeyFormData) => {
79+
async ({ privateKey, name }: ByPrivateKeyFormData) => {
6980
if (isSubmitting) return;
7081

7182
setError(null);
7283
try {
73-
await importPublicAccountByPrivateKey(privateKey);
84+
await importPublicAccountByPrivateKey(privateKey, name);
7485
} catch (err: any) {
7586
console.error(err);
7687

@@ -84,13 +95,34 @@ const ByPrivateKeyForm: FC = () => {
8495

8596
return (
8697
<form className="w-full max-w-sm mx-auto my-8" onSubmit={handleSubmit(onSubmit)} style={{ minHeight: '325px' }}>
98+
<FormField
99+
{...register('name', {
100+
pattern: {
101+
value: ACCOUNT_NAME_PATTERN,
102+
message: t('accountNameInputTitle')
103+
}
104+
})}
105+
label={
106+
<div className="font-medium -mb-2" style={{ fontSize: '14px', lineHeight: '20px' }}>
107+
{t('accountName')}
108+
</div>
109+
}
110+
id="create-account-name"
111+
type="text"
112+
placeholder={computedDefaultName}
113+
errorCaption={errors.name?.message}
114+
autoFocus
115+
/>
116+
<div className="text-gray-200 mb-8" style={{ fontSize: '12px', lineHeight: '16px' }}>
117+
{t('accountNameInputDescription')}
118+
</div>
87119
{error && <Alert type="error" title={t('error')} autoFocus description={error} className="mb-6" />}
88120

89121
<FormField
90122
{...register('privateKey', { required: t('required') })}
91123
secret
92124
textarea
93-
rows={1}
125+
rows={5}
94126
name="privateKey"
95127
id="importacc-privatekey"
96128
label={

src/lib/miden/back/actions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ export function editAccount(accPublicKey: string, name: string) {
163163
});
164164
}
165165

166-
export function importAccount(privateKey: string) {
167-
console.log('importAccount called');
166+
export function importAccount(privateKey: string, name?: string) {
168167
return withUnlocked(async ({ vault }) => {
169-
const accounts = await vault.importAccount(privateKey);
168+
const accounts = await vault.importAccount(privateKey, name);
170169
accountsUpdated({ accounts });
171170
});
172171
}

src/lib/miden/back/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ async function processRequest(req: WalletRequest, port: Runtime.Port): Promise<W
8787
type: WalletMessageType.EditAccountResponse
8888
};
8989
case WalletMessageType.ImportAccountRequest:
90-
await Actions.importAccount(req.privateKey);
90+
await Actions.importAccount(req.privateKey, req.name);
9191
return {
9292
type: WalletMessageType.ImportAccountResponse
9393
};

src/lib/miden/back/vault.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ export class Vault {
375375
});
376376
}
377377

378-
async importAccount(privateKey: string) {
378+
async importAccount(privateKey: string, name?: string): Promise<WalletAccount[]> {
379379
return withError('Failed to import account', async () => {
380380
const allAccounts = await fetchAndDecryptOneWithLegacyFallBack<WalletAccount[]>(accountsStrgKey, this.passKey);
381381
const secretKey = SecretKey.deserialize(new Uint8Array(Buffer.from(privateKey, 'hex')));
@@ -388,7 +388,7 @@ export class Vault {
388388
});
389389
const newAccount: WalletAccount = {
390390
publicKey,
391-
name: getNewAccountName(allAccounts),
391+
name: name || `Imported Account ${allAccounts.length + 1}`,
392392
isPublic: true,
393393
type: WalletType.OnChain,
394394
hdIndex: -1 // -1 indicates imported account

src/lib/miden/front/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ export const [MidenContextProvider, useMidenContext] = constate(() => {
275275
const confirmDAppDeploy = useCallback(async (id: string, confirmed: boolean, delegate: boolean) => {}, []);
276276
const getOwnedRecords = useCallback(async (accPublicKey: string) => {}, []);
277277
const importPublicAccountByPrivateKey = useCallback(
278-
async (privateKey: string) => {
279-
await storeImportAccountByPrivateKey(privateKey);
278+
async (privateKey: string, name?: string) => {
279+
await storeImportAccountByPrivateKey(privateKey, name);
280280
},
281281
[storeImportAccountByPrivateKey]
282282
);

src/lib/shared/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export interface EditAccountResponse extends WalletMessageBase {
267267
export interface ImportAccountRequest extends WalletMessageBase {
268268
type: WalletMessageType.ImportAccountRequest;
269269
privateKey: string;
270+
name?: string;
270271
encPassword?: string;
271272
}
272273

src/lib/store/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,11 @@ export const useWalletStore = create<WalletStore>()(
187187
return res.privateKey;
188188
},
189189

190-
importPublicAccountByPrivateKey: async privateKey => {
190+
importPublicAccountByPrivateKey: async (privateKey, name) => {
191191
const res = await request({
192192
type: WalletMessageType.ImportAccountRequest,
193-
privateKey
193+
privateKey,
194+
name
194195
});
195196
assertResponse(res.type === WalletMessageType.ImportAccountResponse);
196197
},

src/lib/store/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export interface WalletActions {
8787
editAccountName: (accountPublicKey: string, name: string) => Promise<void>;
8888
revealMnemonic: (password: string) => Promise<string>;
8989
revealPrivateKey: (accountPublicKey: string, password: string) => Promise<string>;
90-
importPublicAccountByPrivateKey: (privateKey: string) => Promise<void>;
90+
importPublicAccountByPrivateKey: (privateKey: string, name?: string) => Promise<void>;
9191

9292
// Settings actions
9393
updateSettings: (newSettings: Partial<WalletSettings>) => Promise<void>;

src/screens/encrypted-file-flow/ExportFileComplete.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const ExportFileComplete: React.FC<ExportFileCompleteProps> = ({
9696

9797
// Revoke the object URL to free up resources
9898
URL.revokeObjectURL(url);
99-
}, [midenClient, accounts, walletPassword, filePassword, fileName, revealMnemonic]);
99+
}, [midenClient, accounts, walletPassword, filePassword, fileName, revealMnemonic, revealPrivateKey]);
100100

101101
useEffect(() => {
102102
if (midenClientLoading || !midenClient) return;

0 commit comments

Comments
 (0)