@@ -4,7 +4,7 @@ import type { KeyManagerName } from '@/core/services/kms/kms-types.interface';
44import type { AccountData } from '@/plugins/account/schema' ;
55import type { ImportAccountOutput } from './output' ;
66
7- import { StateError , ValidationError } from '@/core/errors' ;
7+ import { StateError } from '@/core/errors' ;
88import { AliasType } from '@/core/services/alias/alias-service.interface' ;
99import { composeKey } from '@/core/utils/key-composer' ;
1010import { buildAccountEvmAddress } from '@/plugins/account/utils/account-address' ;
@@ -20,43 +20,36 @@ export class ImportAccountCommand implements Command {
2020
2121 const validArgs = ImportAccountInputSchema . parse ( args . args ) ;
2222
23- const key = validArgs . key ;
2423 const alias = validArgs . name ;
2524 const keyManagerArg = validArgs . keyManager ;
26- const accountId = key . accountId ;
2725 const network = api . network . getCurrentNetwork ( ) ;
28- const accountKey = composeKey ( network , accountId ) ;
29-
30- if ( accountState . hasAccount ( accountKey ) ) {
31- throw new StateError ( 'Account with this ID is already saved in state' ) ;
32- }
3326
3427 const keyManager =
35- keyManagerArg ||
28+ keyManagerArg ??
3629 api . config . getOption < KeyManagerName > ( 'default_key_manager' ) ;
3730
38- api . alias . availableOrThrow ( alias , network ) ;
39-
40- const accountInfo = await api . mirror . getAccount ( key . accountId ) ;
41-
42- const { keyRefId, publicKey } = api . kms . importAndValidatePrivateKey (
43- accountInfo . keyAlgorithm ,
44- key . privateKey ,
45- accountInfo . accountPublicKey ,
31+ const resolved = await api . keyResolver . resolveAccountCredentials (
32+ validArgs . key ,
4633 keyManager ,
34+ [ 'account:import' ] ,
4735 ) ;
4836
49- logger . info ( `Importing account: ${ accountKey } (${ accountId } )` ) ;
37+ const accountId = resolved . accountId ;
38+ const accountKey = composeKey ( network , accountId ) ;
5039
5140 if ( accountState . hasAccount ( accountKey ) ) {
52- throw new ValidationError (
53- `Account with identifier '${ accountKey } ' already exists` ,
54- ) ;
41+ throw new StateError ( 'Account with this ID is already saved in state' ) ;
5542 }
5643
44+ api . alias . availableOrThrow ( alias , network ) ;
45+
46+ const accountInfo = await api . mirror . getAccount ( accountId ) ;
47+
48+ logger . info ( `Importing account: ${ accountKey } (${ accountId } )` ) ;
49+
5750 const evmAddress = buildAccountEvmAddress ( {
5851 accountId,
59- publicKey,
52+ publicKey : resolved . publicKey ,
6053 keyType : accountInfo . keyAlgorithm ,
6154 existingEvmAddress : accountInfo . evmAddress ,
6255 } ) ;
@@ -68,8 +61,8 @@ export class ImportAccountCommand implements Command {
6861 network : api . network . getCurrentNetwork ( ) ,
6962 entityId : accountId ,
7063 evmAddress,
71- publicKey,
72- keyRefId,
64+ publicKey : resolved . publicKey ,
65+ keyRefId : resolved . keyRefId ,
7366 createdAt : new Date ( ) . toISOString ( ) ,
7467 } ) ;
7568 }
@@ -78,9 +71,9 @@ export class ImportAccountCommand implements Command {
7871 name : alias ,
7972 accountId,
8073 type : accountInfo . keyAlgorithm ,
81- publicKey : publicKey ,
74+ publicKey : resolved . publicKey ,
8275 evmAddress,
83- keyRefId,
76+ keyRefId : resolved . keyRefId ,
8477 network : api . network . getCurrentNetwork ( ) ,
8578 } ;
8679
0 commit comments