Skip to content

Commit

Permalink
fix: Updated the account from the 0.0.2 account to an account from th… (
Browse files Browse the repository at this point in the history
#2690)

* fix: Updated the account from the 0.0.2 account to an account from the local node to track
transaction costs.

Signed-off-by: ebadiere <[email protected]>

* fix: Turning off test to potentially get through CI.
Opened issue: #2700

Signed-off-by: ebadiere <[email protected]>

* fix: WIP: Pushing up to collaborate

Signed-off-by: ebadiere <[email protected]>

fix: Updated the private key to the hex ECDSA value.

Signed-off-by: ebadiere <[email protected]>

fix: Bumped up the gasLimit to the required amount to activate a hollow account.

Signed-off-by: ebadiere <[email protected]>

* fix:  Added key format support to the serviceClient.

Signed-off-by: ebadiere <[email protected]>

* fix: Extracted the createPrivateKeyBasedOnFormat(key) to the Utils class.

Signed-off-by: ebadiere <[email protected]>

* fix: Updated import of createPrivateKeyBasedOnFormat from Utils.

Signed-off-by: ebadiere <[email protected]>

* fix: Removed memory leak check.  PR 26954 addresses memory leaks.

Signed-off-by: ebadiere <[email protected]>

* fix:  Removed null test because an env var which is a string cannot be
set to null

Signed-off-by: ebadiere <[email protected]>

* fix: Removed the comment in the GH workflow

Signed-off-by: ebadiere <[email protected]>

---------

Signed-off-by: ebadiere <[email protected]>
Signed-off-by: Eric Badiere <[email protected]>
  • Loading branch information
ebadiere authored Jul 23, 2024
1 parent a977db0 commit a75f5b5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 49 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ jobs:
run: npm install -g pnpm

- name: Build Typescript and Run tests
run: |
output=$(npm run build-and-test 2>&1)
echo "$output"
if echo "$output" | grep -q "JavaScript heap out of memory"; then
echo "Heap out of Memory error detected"
exit 1
fi
shell: bash
run: npm run build-and-test

- name: Upload coverage report
if: ${{ always() && !cancelled() }}
Expand Down
20 changes: 3 additions & 17 deletions packages/relay/src/lib/services/hapiService/hapiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SDKClient } from '../../clients/sdkClient';
import constants from '../../constants';
import HbarLimit from '../../hbarlimiter';
import { CacheService } from '../cacheService/cacheService';
import { Utils } from './../../../utils';

export default class HAPIService {
private transactionCount: number;
Expand Down Expand Up @@ -218,14 +219,14 @@ export default class HAPIService {

if (type === 'eth_sendRawTransaction') {
if (process.env.OPERATOR_ID_ETH_SENDRAWTRANSACTION && process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION) {
privateKey = this.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_ETH_SENDRAWTRANSACTION);
privateKey = Utils.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.createPrivateKeyBasedOnFormat(process.env.OPERATOR_KEY_MAIN);
privateKey = Utils.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,21 +247,6 @@ export default class HAPIService {
return client;
}

private createPrivateKeyBasedOnFormat(operatorMainKey: string): PrivateKey {
switch (process.env.OPERATOR_KEY_FORMAT) {
case 'DER':
case undefined:
case null:
return PrivateKey.fromStringDer(operatorMainKey);
case 'HEX_ED25519':
return PrivateKey.fromStringED25519(operatorMainKey);
case 'HEX_ECDSA':
return PrivateKey.fromStringECDSA(operatorMainKey);
default:
throw new Error(`Invalid OPERATOR_KEY_FORMAT provided: ${process.env.OPERATOR_KEY_FORMAT}`);
}
}

/**
* Return current main client instance
* @returns Main Client
Expand Down
20 changes: 20 additions & 0 deletions packages/relay/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*
*/

import { PrivateKey } from '@hashgraph/sdk';
import constants from './lib/constants';

export class Utils {
Expand All @@ -35,4 +36,23 @@ export class Utils {

return gasPrice;
};

/**
* @param operatorMainKey
* @returns PrivateKey
*/
public static createPrivateKeyBasedOnFormat(operatorMainKey: string): PrivateKey {
switch (process.env.OPERATOR_KEY_FORMAT) {
case 'DER':
case undefined:
case null:
return PrivateKey.fromStringDer(operatorMainKey);
case 'HEX_ED25519':
return PrivateKey.fromStringED25519(operatorMainKey);
case 'HEX_ECDSA':
return PrivateKey.fromStringECDSA(operatorMainKey);
default:
throw new Error(`Invalid OPERATOR_KEY_FORMAT provided: ${process.env.OPERATOR_KEY_FORMAT}`);
}
}
}
25 changes: 7 additions & 18 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { MAX_GAS_LIMIT_HEX } from './eth/eth-config';
import { getRequestId, signTransaction } from '../helpers';
import { TransactionReceipt } from 'ethers';
import exp from 'constants';
import { Utils } from '../../src/utils';

describe('SdkClient', async function () {
this.timeout(20000);
Expand Down Expand Up @@ -180,7 +181,7 @@ describe('SdkClient', async function () {
});

describe('HAPIService', async () => {
let originalEnv: NodeJS.ProcessEnv;
let originalEnv: NodeJS.ProcessEnv, keyFormat;

const OPERATOR_KEY_ED25519 = {
DER: '302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137',
Expand Down Expand Up @@ -218,47 +219,35 @@ describe('SdkClient', async function () {

it('Initialize the privateKey for default which is DER', async () => {
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER);
const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER);
});

it('Initialize the privateKey for default which is DER when OPERATOR_KEY_FORMAT is undefined', async () => {
delete process.env.OPERATOR_KEY_FORMAT;
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER);
});

it('Initialize the privateKey for default which is DER when OPERATOR_KEY_FORMAT is null', async () => {
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER);
const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.DER);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER);
});

it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to DER', async () => {
process.env.OPERATOR_KEY_FORMAT = 'DER';
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.DER);
const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.DER);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ECDSA.DER);
});

it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to HEX_ED25519', async () => {
process.env.OPERATOR_KEY_FORMAT = 'HEX_ED25519';
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(
hapiService,
OPERATOR_KEY_ED25519.HEX_ED25519,
);
const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ED25519.HEX_ED25519);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ED25519.DER);
});

it('Initialize the privateKey for OPERATOR_KEY_FORMAT set to HEX_ECDSA', async () => {
process.env.OPERATOR_KEY_FORMAT = 'HEX_ECDSA';
const hapiService = new HAPIService(logger, registry, hbarLimiter, new CacheService(logger, registry));
const privateKey = (hapiService as any).createPrivateKeyBasedOnFormat.call(
hapiService,
OPERATOR_KEY_ECDSA.HEX_ECDSA,
);
const privateKey = Utils.createPrivateKeyBasedOnFormat.call(hapiService, OPERATOR_KEY_ECDSA.HEX_ECDSA);
expect(privateKey.toString()).to.eq(OPERATOR_KEY_ECDSA.DER);
});

Expand Down
3 changes: 2 additions & 1 deletion packages/server/tests/clients/servicesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { Logger } from 'pino';
import { ethers } from 'ethers';
import { Utils } from '../helpers/utils';
import { AliasAccount } from '../types/AliasAccount';
import { Utils as relayUtils } from '@hashgraph/json-rpc-relay/dist/utils';

const supportedEnvs = ['previewnet', 'testnet', 'mainnet'];

Expand All @@ -75,7 +76,7 @@ export default class ServicesClient {
this.network = network;

if (!network) network = '{}';
const opPrivateKey = PrivateKey.fromString(key);
const opPrivateKey = relayUtils.createPrivateKeyBasedOnFormat(key);
if (supportedEnvs.includes(network.toLowerCase())) {
this.client = Client.forName(network);
} else {
Expand Down
8 changes: 4 additions & 4 deletions packages/server/tests/localAcceptance.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
HEDERA_NETWORK={"127.0.0.1:50211":"0.0.3"}

#This is a static acount created at startup in the local node
OPERATOR_ID_MAIN=0.0.1022
OPERATOR_KEY_MAIN=302e020100300506032b657004220420a608e2130a0a3cb34f86e757303c862bee353d9ab77ba4387ec084f881d420d4
# Account from local node. This is the account that will be used to pay for transactions.
OPERATOR_ID_MAIN=0.0.1002
OPERATOR_KEY_MAIN=0x7f109a9e3b0d8ecfba9cc23a3614433ce0fa7ddcc80f2a8f10b222179a5a80d6
OPERATOR_KEY_FORMAT=HEX_ECDSA
CHAIN_ID=0x12a
MIRROR_NODE_URL_WEB3=http://127.0.0.1:8545
REDIS_ENABLED=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('@web-socket-batch-2 eth_sendRawTransaction', async function () {
value: (10 * 10 ** 18).toString(), // 10hbar - the gasPrice to deploy deterministic proxy contract
to: constants.DETERMINISTIC_DEPLOYMENT_SIGNER,
gasPrice: await global.relay.gasPrice(),
gasLimit: numberTo0x(5000000),
gasLimit: constants.TX_HOLLOW_ACCOUNT_CREATION_GAS,
};
});

Expand Down

0 comments on commit a75f5b5

Please sign in to comment.