1- /**
2- * Account Import Command Handler
3- * Handles importing existing accounts using the Core API
4- * Follows ADR-003 contract: returns CommandExecutionResult
5- */
61import type { CommandHandlerArgs , CommandResult } from '@/core' ;
72import type { KeyManagerName } from '@/core/services/kms/kms-types.interface' ;
83import type { AccountData } from '@/plugins/account/schema' ;
94import type { ImportAccountOutput } from './output' ;
105
11- import { StateError , ValidationError } from '@/core/errors' ;
6+ import { StateError } from '@/core/errors' ;
127import { AliasType } from '@/core/services/alias/alias-service.interface' ;
138import { composeKey } from '@/core/utils/key-composer' ;
149import { buildAccountEvmAddress } from '@/plugins/account/utils/account-address' ;
@@ -21,53 +16,40 @@ export async function importAccount(
2116) : Promise < CommandResult > {
2217 const { api, logger } = args ;
2318
24- // Initialize Zustand state helper
2519 const accountState = new ZustandAccountStateHelper ( api . state , logger ) ;
2620
27- // Parse and validate command arguments
2821 const validArgs = ImportAccountInputSchema . parse ( args . args ) ;
2922
30- const key = validArgs . key ;
3123 const alias = validArgs . name ;
3224 const keyManagerArg = validArgs . keyManager ;
33- const accountId = key . accountId ;
3425 const network = api . network . getCurrentNetwork ( ) ;
35- const accountKey = composeKey ( network , accountId ) ;
36-
37- if ( accountState . hasAccount ( accountKey ) ) {
38- throw new StateError ( 'Account with this ID is already saved in state' ) ;
39- }
4026
41- // Get keyManager from args or fallback to config
4227 const keyManager =
43- keyManagerArg ||
28+ keyManagerArg ??
4429 api . config . getOption < KeyManagerName > ( 'default_key_manager' ) ;
4530
46- // Check if account name already exists
47- api . alias . availableOrThrow ( alias , network ) ;
48-
49- // Get account info from mirror node
50- const accountInfo = await api . mirror . getAccount ( key . accountId ) ;
51-
52- const { keyRefId, publicKey } = api . kms . importAndValidatePrivateKey (
53- accountInfo . keyAlgorithm ,
54- key . privateKey ,
55- accountInfo . accountPublicKey ,
31+ const resolved = await api . keyResolver . resolveAccountCredentials (
32+ validArgs . key ,
5633 keyManager ,
34+ [ 'account:import' ] ,
5735 ) ;
5836
59- logger . info ( `Importing account: ${ accountKey } (${ accountId } )` ) ;
37+ const accountId = resolved . accountId ;
38+ const accountKey = composeKey ( network , accountId ) ;
6039
61- // Check if account name already exists
6240 if ( accountState . hasAccount ( accountKey ) ) {
63- throw new ValidationError (
64- `Account with identifier '${ accountKey } ' already exists` ,
65- ) ;
41+ throw new StateError ( 'Account with this ID is already saved in state' ) ;
6642 }
6743
44+ api . alias . availableOrThrow ( alias , network ) ;
45+
46+ const accountInfo = await api . mirror . getAccount ( accountId ) ;
47+
48+ logger . info ( `Importing account: ${ accountKey } (${ accountId } )` ) ;
49+
6850 const evmAddress = buildAccountEvmAddress ( {
6951 accountId,
70- publicKey,
52+ publicKey : resolved . publicKey ,
7153 keyType : accountInfo . keyAlgorithm ,
7254 existingEvmAddress : accountInfo . evmAddress ,
7355 } ) ;
@@ -79,27 +61,24 @@ export async function importAccount(
7961 network : api . network . getCurrentNetwork ( ) ,
8062 entityId : accountId ,
8163 evmAddress,
82- publicKey,
83- keyRefId,
64+ publicKey : resolved . publicKey ,
65+ keyRefId : resolved . keyRefId ,
8466 createdAt : new Date ( ) . toISOString ( ) ,
8567 } ) ;
8668 }
8769
88- // Create account object (no private key in plugin state)
8970 const account : AccountData = {
9071 name : alias ,
9172 accountId,
9273 type : accountInfo . keyAlgorithm ,
93- publicKey : publicKey ,
74+ publicKey : resolved . publicKey ,
9475 evmAddress,
95- keyRefId,
76+ keyRefId : resolved . keyRefId ,
9677 network : api . network . getCurrentNetwork ( ) ,
9778 } ;
9879
99- // Store account in state using the helper
10080 accountState . saveAccount ( accountKey , account ) ;
10181
102- // Prepare output data
10382 const outputData : ImportAccountOutput = {
10483 accountId,
10584 name : alias ,
0 commit comments