Skip to content

Commit

Permalink
fix: refactored case condition to handle bad key format.
Browse files Browse the repository at this point in the history
Signed-off-by: ebadiere <[email protected]>
  • Loading branch information
ebadiere committed Jun 12, 2024
1 parent 5dcd8f6 commit a79b453
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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}`);
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
});
});
});

0 comments on commit a79b453

Please sign in to comment.