Skip to content

Commit efafdb9

Browse files
committed
feat: validateAccountId fn in evm utils
ticket: win-8570
1 parent 6a84b4a commit efafdb9

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

modules/sdk-coin-evm/src/lib/utils.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,52 @@ async function getGasLimitFromRPC(query: Record<string, string>, rpcUrl: string)
223223

224224
return response.body;
225225
}
226+
227+
export function validateHederaAccountId(address: string): { valid: boolean; error: string | null } {
228+
const parts = address.split('.');
229+
if (parts.length !== 3) {
230+
return {
231+
valid: false,
232+
error: 'Invalid Hedera Account ID format. Use format: 0.0.12345',
233+
};
234+
}
235+
const [shardStr, realmStr, accountStr] = parts;
236+
if (!shardStr || !realmStr || !accountStr) {
237+
return {
238+
valid: false,
239+
error: 'Invalid Hedera Account ID. All parts are required.',
240+
};
241+
}
242+
243+
const shard = Number(shardStr);
244+
const realm = Number(realmStr);
245+
const account = Number(accountStr);
246+
247+
// Validate all parts are valid non-negative integers within safe range
248+
if (
249+
!Number.isInteger(shard) ||
250+
!Number.isInteger(realm) ||
251+
!Number.isInteger(account) ||
252+
shard < 0 ||
253+
realm < 0 ||
254+
account < 0
255+
) {
256+
return {
257+
valid: false,
258+
error: 'Invalid Hedera Account ID. All parts must be non-negative integers.',
259+
};
260+
}
261+
262+
// Check for JavaScript safe integer limits (prevents precision loss)
263+
if (!Number.isSafeInteger(shard) || !Number.isSafeInteger(realm) || !Number.isSafeInteger(account)) {
264+
return {
265+
valid: false,
266+
error: 'Invalid Hedera Account ID. Values are too large.',
267+
};
268+
}
269+
270+
return {
271+
valid: true,
272+
error: null,
273+
};
274+
}

0 commit comments

Comments
 (0)