From a79b453eec26691666f512359ea4bc50e7001915 Mon Sep 17 00:00:00 2001 From: ebadiere Date: Wed, 12 Jun 2024 13:38:35 -0600 Subject: [PATCH] fix: refactored case condition to handle bad key format. Signed-off-by: ebadiere --- .../src/lib/services/hapiService/hapiService.ts | 12 ++++++++---- packages/relay/tests/lib/sdkClient.spec.ts | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/relay/src/lib/services/hapiService/hapiService.ts b/packages/relay/src/lib/services/hapiService/hapiService.ts index eceea9c063..e4720a61f4 100644 --- a/packages/relay/src/lib/services/hapiService/hapiService.ts +++ b/packages/relay/src/lib/services/hapiService/hapiService.ts @@ -218,14 +218,14 @@ export default class HAPIService { if (type === 'eth_sendRawTransaction') { if (process.env.OPERATOR_ID_ETH_SENDRAWTRANSACTION && process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION) { - privateKey = this.initPrivateKey(process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION); + privateKey = this.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION); client = client.setOperator(AccountId.fromString(process.env.OPERATOR_ID_ETH_SENDRAWTRANSACTION), privateKey); } else { logger.warn(`Invalid 'ETH_SENDRAWTRANSACTION' env variables provided`); } } else { if (process.env.OPERATOR_ID_MAIN && process.env.OPERATOR_KEY_MAIN) { - privateKey = this.initPrivateKey(process.env.OPERATOR_KEY_MAIN); + privateKey = this.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_MAIN); client = client.setOperator(AccountId.fromString(process.env.OPERATOR_ID_MAIN.trim()), privateKey); } else { logger.warn(`Invalid 'OPERATOR' env variables provided`); @@ -246,16 +246,20 @@ export default class HAPIService { return client; } - private initPrivateKey(operatorMainKey: string): PrivateKey { + private createPrivateKeyBasedOnFormat(operatorMainKey: string): PrivateKey { switch (process.env.OPERATOR_KEY_FORMAT) { case 'DER': return PrivateKey.fromStringDer(operatorMainKey); + case undefined: + return PrivateKey.fromStringDer(operatorMainKey); + case null: + return PrivateKey.fromStringDer(operatorMainKey); case 'HEX_ED25519': return PrivateKey.fromStringED25519(operatorMainKey); case 'HEX_ECDSA': return PrivateKey.fromStringECDSA(operatorMainKey); default: - return PrivateKey.fromStringDer(operatorMainKey); + throw new Error(`Invalid OPERATOR_KEY_FORMAT provided: ${process.env.OPERATOR_KEY_FORMAT}`); } } diff --git a/packages/relay/tests/lib/sdkClient.spec.ts b/packages/relay/tests/lib/sdkClient.spec.ts index b836505066..7766c0c414 100644 --- a/packages/relay/tests/lib/sdkClient.spec.ts +++ b/packages/relay/tests/lib/sdkClient.spec.ts @@ -50,6 +50,7 @@ import { CacheService } from '../../src/lib/services/cacheService/cacheService'; import { MAX_GAS_LIMIT_HEX } from './eth/eth-config'; import { getRequestId, signTransaction } from '../helpers'; import { TransactionReceipt } from 'ethers'; +import exp from 'constants'; describe('SdkClient', async function () { this.timeout(20000); @@ -213,5 +214,15 @@ describe('SdkClient', async function () { const privateKey = (hapiService as any).initPrivateKey.call(hapiService, OPERATOR_KEY_ECDSA.HEX_ECDSA); expect(privateKey.toString()).to.eq(OPERATOR_KEY_ECDSA.DER); }); + + it('It should throw an Error when an unexpected string is set', async () => { + process.env.OPERATOR_KEY_FORMAT = 'BAD_FORMAT'; + try { + const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry)); + expect(true).to.be.false; // Should not make it here + } catch (e: any) { + expect(e.message).to.eq('Invalid OPERATOR_KEY_FORMAT provided: BAD_FORMAT'); + } + }); }); });