Skip to content

Commit 09127c4

Browse files
committed
feat(1540): Replace outdated key schemas with keySchema
Signed-off-by: matevszm <mateusz.marcinkowski@blockydevs.com>
1 parent 8c14969 commit 09127c4

File tree

25 files changed

+93
-114
lines changed

25 files changed

+93
-114
lines changed

src/core/services/token/token-service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,11 @@ export class TokenServiceImpl implements TokenService {
112112
.setTokenType(TokenTypeMap[tokenType])
113113
.setInitialSupply(initialSupplyRaw)
114114
.setSupplyType(tokenSupplyType)
115-
.setTreasuryAccountId(AccountId.fromString(treasuryId))
116-
.setAdminKey(adminPublicKey);
115+
.setTreasuryAccountId(AccountId.fromString(treasuryId));
116+
117+
if (adminPublicKey) {
118+
tokenCreateTx.setAdminKey(adminPublicKey);
119+
}
117120

118121
// Set max supply for finite supply tokens
119122
if (supplyType === SupplyType.FINITE && maxSupplyRaw !== undefined) {

src/core/types/token.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export interface TokenCreateParams {
6363
tokenType: HederaTokenType;
6464
supplyType: SupplyType;
6565
maxSupplyRaw?: bigint; // Required for FINITE supply type
66-
adminPublicKey: PublicKey;
66+
adminPublicKey?: PublicKey;
6767
supplyPublicKey?: PublicKey;
6868
wipePublicKey?: PublicKey;
6969
kycPublicKey?: PublicKey;

src/core/utils/resolve-payer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CoreApi } from '@/core';
22
import type { KeyManagerName } from '@/core/services/kms/kms-types.interface';
33

4-
import { PrivateKeySchema } from '@/core';
4+
import { KeySchema } from '@/core';
55

66
/**
77
* Resolves payer from string (alias or account-id:private-key format)
@@ -17,7 +17,7 @@ export async function resolvePayer(
1717
): Promise<void> {
1818
const keyManager =
1919
coreApi.config.getOption<KeyManagerName>('default_key_manager') || 'local';
20-
const parsedPayer = PrivateKeySchema.parse(payerString);
20+
const parsedPayer = KeySchema.parse(payerString);
2121
const resolvedPayer = await coreApi.keyResolver.resolveAccountCredentials(
2222
parsedPayer,
2323
keyManager,

src/plugins/account/__tests__/unit/import.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,14 @@ describe('account plugin - import command (ADR-003)', () => {
6363

6464
const result = await importAccount(args);
6565

66-
expect(kms.importAndValidatePrivateKey).toHaveBeenCalledWith(
67-
KeyAlgorithm.ECDSA,
68-
'abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890',
69-
'0230a1f42abc4794541e4a4389ec7e822666b8a7693c4cc3dedd2746b32f9c015b',
70-
'local',
71-
);
7266
expect(mirrorMock.getAccount).toHaveBeenCalledWith('0.0.9999');
7367
expect(alias.register).toHaveBeenCalledWith(
7468
expect.objectContaining({
7569
alias: 'imported',
7670
type: AliasType.Account,
7771
network: 'testnet',
7872
entityId: '0.0.9999',
79-
publicKey: 'pub-key-test',
73+
publicKey: expect.any(String),
8074
keyRefId: 'kr_test123',
8175
}),
8276
);

src/plugins/account/commands/import/handler.ts

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
/**
2-
* Account Import Command Handler
3-
* Handles importing existing accounts using the Core API
4-
* Follows ADR-003 contract: returns CommandExecutionResult
5-
*/
61
import type { CommandHandlerArgs, CommandResult } from '@/core';
72
import type { KeyManagerName } from '@/core/services/kms/kms-types.interface';
83
import type { AccountData } from '@/plugins/account/schema';
94
import type { ImportAccountOutput } from './output';
105

11-
import { StateError, ValidationError } from '@/core/errors';
6+
import { StateError } from '@/core/errors';
127
import { AliasType } from '@/core/services/alias/alias-service.interface';
138
import { composeKey } from '@/core/utils/key-composer';
149
import { 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,

src/plugins/account/commands/import/input.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { z } from 'zod';
22

33
import {
4-
AccountIdWithPrivateKeySchema,
54
AccountNameSchema,
65
KeyManagerTypeSchema,
6+
KeySchema,
77
} from '@/core/schemas';
88

99
/**
1010
* Input schema for account import command
1111
* Validates arguments for importing an existing account
1212
*/
1313
export const ImportAccountInputSchema = z.object({
14-
key: AccountIdWithPrivateKeySchema.describe(
15-
'Account ID with private key in format accountId:privateKey',
14+
key: KeySchema.describe(
15+
'Account credentials. Can be accountId:privateKey pair, key reference or account alias.',
1616
),
1717
name: AccountNameSchema.optional().describe('Optional account name/alias'),
1818
keyManager: KeyManagerTypeSchema.optional().describe(

src/plugins/network/commands/set-operator/input.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { z } from 'zod';
22

3-
import {
4-
KeyManagerTypeSchema,
5-
NetworkSchema,
6-
PrivateKeyWithAccountIdSchema,
7-
} from '@/core/schemas';
3+
import { KeyManagerTypeSchema, KeySchema, NetworkSchema } from '@/core/schemas';
84

95
/**
106
* Input schema for network set-operator command
117
* Validates arguments for setting operator credentials
128
*/
139
export const SetOperatorInputSchema = z.object({
14-
operator: PrivateKeyWithAccountIdSchema.describe(
10+
operator: KeySchema.describe(
1511
'Operator credentials. Can be accountId:privateKey pair, key reference or account alias.',
1612
),
1713
network: NetworkSchema.optional().describe(

src/plugins/token/__tests__/unit/create-nft.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ describe('createNftHandler', () => {
124124
saveToken: mockSaveToken,
125125
}));
126126

127-
const { api, tokenTransactions, signing } = makeApiMocks({
127+
const { api, tokenTransactions, signing, keyResolver } = makeApiMocks({
128128
tokenTransactions: {
129129
createTokenTransaction: jest
130130
.fn()
@@ -135,11 +135,17 @@ describe('createNftHandler', () => {
135135
},
136136
});
137137

138+
keyResolver.resolveSigningKey.mockResolvedValue({
139+
keyRefId: 'supply-key-ref-id',
140+
publicKey: '302a300506032b6570032100' + '0'.repeat(64),
141+
});
142+
138143
const logger = makeLogger();
139144
const args: CommandHandlerArgs = {
140145
args: {
141146
tokenName: 'TestToken',
142147
symbol: 'TEST',
148+
supplyKey: 'test-supply-key',
143149
},
144150
api,
145151
state: api.state,
@@ -160,7 +166,7 @@ describe('createNftHandler', () => {
160166
maxSupplyRaw: undefined,
161167
treasuryId: '0.0.100000',
162168
tokenType: HederaTokenType.NON_FUNGIBLE_TOKEN,
163-
adminPublicKey: expect.any(Object),
169+
adminPublicKey: undefined,
164170
supplyPublicKey: expect.any(Object),
165171
memo: undefined,
166172
});
@@ -181,12 +187,16 @@ describe('createNftHandler', () => {
181187
keyResolver.resolveAccountCredentialsWithFallback.mockImplementation(() =>
182188
Promise.reject(new Error('No operator set')),
183189
);
190+
keyResolver.resolveSigningKey.mockImplementation(() =>
191+
Promise.reject(new Error('No operator set')),
192+
);
184193

185194
const logger = makeLogger();
186195
const args: CommandHandlerArgs = {
187196
args: {
188197
tokenName: 'TestToken',
189198
symbol: 'TEST',
199+
supplyKey: 'test-supply-key',
190200
},
191201
api,
192202
state: api.state,

src/plugins/token/__tests__/unit/create.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('createTokenHandler', () => {
158158
supplyType: SupplyType.INFINITE,
159159
maxSupplyRaw: undefined,
160160
treasuryId: '0.0.100000',
161-
adminPublicKey: expect.any(Object),
161+
adminPublicKey: undefined,
162162
tokenType: HederaTokenType.FUNGIBLE_COMMON,
163163
memo: undefined,
164164
});

src/plugins/token/__tests__/unit/createFromFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ describe('createTokenFromFileHandler', () => {
612612

613613
// Act & Assert
614614
await expect(createTokenFromFile(args)).rejects.toThrow(
615-
'Private key with account ID must be a valid account ID and private key pair in {account-id:private-key} format, key reference or alias name',
615+
'Key must be a valid account ID and private key pair in format {account-id:private-key}, account ID, private key in format {ed25519|ecdsa}:{private-key}, public key in format {ed25519|ecdsa}:{public-key}, key reference, EVM address (0x...) or alias name',
616616
);
617617
});
618618

0 commit comments

Comments
 (0)