Skip to content

Commit

Permalink
feat: add accounts and delegation flags (#1226)
Browse files Browse the repository at this point in the history
* feat: add accounts flag and validate empty cases

* feat: add accounts with mnemonics

* feat: add delegation

* feat: last edge case

* fix: improve RPC logs

---------

Co-authored-by: Fabio Rigamonti <[email protected]>
  • Loading branch information
rodolfopietro97 and fabiorigam authored Sep 3, 2024
1 parent 40a2c37 commit af4475c
Show file tree
Hide file tree
Showing 8 changed files with 986 additions and 408 deletions.
4 changes: 3 additions & 1 deletion packages/rpc-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function startProxy(): void {
// Parse the SEMANTIC of the arguments and throw an error if the options are not valid
const config = parseAndGetFinalConfig(options, defaultConfiguration);

console.log(`[rpc-proxy]: Using configuration ${JSON.stringify(config)}`);

// Log the RPC Proxy start
console.log('[rpc-proxy]: Starting VeChain RPC Proxy');

Expand Down Expand Up @@ -91,7 +93,7 @@ function startProxy(): void {
app.listen(config.port, () => {
console.log(`[rpc-proxy]: Proxy is running on port ${config.port}`);
}).on('error', (err: Error) => {
console.error(`[rpc-proxy]: Error starting proxy: ${err.message}`);
console.error(`[rpc-proxy]: Error starting proxy ${err.message}`);
});

function handleRequest(req: Request, res: Response): void {
Expand Down
230 changes: 94 additions & 136 deletions packages/rpc-proxy/src/utils/args-validator/args-validator-and-getter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getConfigObjectFromFile
} from '../config-validator';
import { ArgsValidator } from './args-validator';
import { InvalidCommandLineArguments } from '@vechain/sdk-errors';

/**
* Main args validator AND getter object.
Expand Down Expand Up @@ -50,21 +51,19 @@ const ArgsValidatorAndGetter = {
* @throws {InvalidCommandLineArguments}
*/
port: (options: OptionValues, currentConfiguration: Config): Config => {
const field = ArgsValidator.port(options.port as string);
if (field !== null) {
const port = ArgsValidator.port(
options.port as string | null | undefined
);
if (port !== null) {
return {
...currentConfiguration,
port: field
port
} satisfies Config;
}
return currentConfiguration;
},

/**
* ********* START: TEMPORARY COMMENT *********
* This method will be implemented in the future.
* ********* END: TEMPORARY COMMENT ********
*
* Validate 'url' configuration field
*
* @param options Command line arguments options
Expand All @@ -73,167 +72,126 @@ const ArgsValidatorAndGetter = {
* @throws {InvalidCommandLineArguments}
*/
url: (options: OptionValues, currentConfiguration: Config): Config => {
const field = ArgsValidator.url(options.url as string);
if (field !== null) {
const url = ArgsValidator.url(options.url as string | null | undefined);
if (url !== null) {
return {
...currentConfiguration,
url: field
url
} satisfies Config;
}
return currentConfiguration;
}
},

/**
* ********* START: TEMPORARY COMMENT *********
* This method will be implemented in the future.
* ********* END: TEMPORARY COMMENT ********
*
* Validate 'accounts' configuration field
*
* @param options Command line arguments options
* @param currentConfiguration Default configuration to use if no field is provided
* @returns Configuration object
* @throws {InvalidCommandLineArguments}
*/
// accounts: (options: OptionValues, currentConfiguration: Config): Config => {
// const field = ArgsValidator.accounts(options.accounts as string);
// if (field !== null) {
// return {
// ...currentConfiguration,
// accounts: field
// } satisfies Config;
// }
// return currentConfiguration;
// }
accounts: (options: OptionValues, currentConfiguration: Config): Config => {
const accounts = ArgsValidator.accounts(
options.accounts as string | null | undefined
);
if (accounts !== null) {
return {
...currentConfiguration,
accounts
} satisfies Config;
}
return currentConfiguration;
},

/**
* ********* START: TEMPORARY COMMENT *********
* This method will be implemented in the future.
* ********* END: TEMPORARY COMMENT ********
*
* Validate 'mnemonic' configuration field
*
* @param options Command line arguments options
* @param currentConfiguration Default configuration to use if no field is provided
* @returns Configuration object
* @throws {InvalidCommandLineArguments}
*/
// mnemonic: (options: OptionValues, currentConfiguration: Config): Config => {
// const mnemonicToUse = ArgsValidator.mnemonic(
// options.mnemonic as string
// );
//
// // Mnemonic count must be provided if mnemonic is provided
// if (
// mnemonicToUse !== null &&
// (options.mnemonicCount === undefined ||
// options.mnemonicCount === null)
// ) {
// throw new InvalidCommandLineArguments(
// 'ArgsValidatorAndGetter.mnemonic()',
// 'No mnemonic count provided. A mnemonic count must be provided',
// {
// flag: '-mc , --mnemonicCount',
// value: 'not provided'
// }
// );
// }
//
// // Mnemonic initial index must be provided if mnemonic is provided
// if (
// mnemonicToUse !== null &&
// (options.mnemonicInitialIndex === undefined ||
// options.mnemonicInitialIndex === null)
// ) {
// throw new InvalidCommandLineArguments(
// 'ArgsValidatorAndGetter.mnemonic()',
// 'No mnemonic initial index provided. A mnemonic initial index must be provided',
// {
// flag: '-mi , --mnemonicInitialIndex',
// value: 'not provided'
// }
// );
// }
//
// // @note: This field must be provided otherwise previous validation fails!
// if (
// options.mnemonicCount !== undefined &&
// options.mnemonicCount !== null &&
// options.mnemonicInitialIndex !== undefined &&
// options.mnemonicInitialIndex !== null
// ) {
// const field2 = ArgsValidator.mnemonicCount(
// options.mnemonicCount as string
// );
//
// // @note: This field must be provided otherwise previous validation fails!
// const field3 = ArgsValidator.mnemonicInitialIndex(
// options.mnemonicInitialIndex as string
// );
//
// if (mnemonicToUse !== null) {
// return {
// ...currentConfiguration,
// accounts: {
// mnemonic: mnemonicToUse,
// count: field2,
// initialIndex: field3
// }
// } satisfies Config;
// }
// }
// return currentConfiguration;
// }
mnemonicFields: (
options: OptionValues,
currentConfiguration: Config
): Config => {
const accounts = ArgsValidator.mnemonicFields(
options.mnemonic as string | null | undefined,
options.mnemonicCount as string | null | undefined,
options.mnemonicInitialIndex as string | null | undefined
);

if (accounts !== null) {
return {
...currentConfiguration,
accounts
} satisfies Config;
}

return currentConfiguration;
},

/**
* ********* START: TEMPORARY COMMENT *********
* This method will be implemented in the future.
* ********* END: TEMPORARY COMMENT ********
*
* Validate 'delegation' configuration fields
*
* @param options Command line arguments options
* @param currentConfiguration Default configuration to use if no field is provided
* @returns Configuration object
* @throws {InvalidCommandLineArguments}
*/
// delegation: (
// options: OptionValues,
// currentConfiguration: Config
// ): Config => {
// // Both delegation fields are provided - throw an error
// if (
// options.delegatorPrivateKey !== undefined &&
// options.delegatorUrl !== undefined &&
// options.delegatorPrivateKey !== null &&
// options.delegatorUrl !== null
// ) {
// throw new InvalidCommandLineArguments(
// 'ArgsValidatorAndGetter.delegation()',
// 'Both delegator private key and delegator URL are provided. Only one can be provided',
// {
// flag: '{-dp , --delegatorPrivateKey}, {-du , --delegatorUrl}',
// value: `{value not provided for security reason} , {${options.delegatorUrl as string}}`
// }
// );
// }
//
// // Delegation is made with a private key
// if (
// options.delegatePrivateKey !== undefined &&
// options.delegatePrivateKey !== null
// ) {
// }
//
// // Delegation is made with a delegator URL
// if (
// options.delegatorUrl !== undefined &&
// options.delegatorUrl !== null
// ) {
// }
//
// return currentConfiguration;
// }
delegation: (
options: OptionValues,
currentConfiguration: Config
): Config => {
// Both delegation fields are provided - throw an error
if (
options.delegatorPrivateKey !== undefined &&
options.delegatorUrl !== undefined &&
options.delegatorPrivateKey !== null &&
options.delegatorUrl !== null
) {
throw new InvalidCommandLineArguments(
'ArgsValidatorAndGetter.delegation()',
'Both delegator private key and delegator URL are provided. Only one can be provided',
{
flag: '{-dp , --delegatorPrivateKey}, {-du , --delegatorUrl}',
value: `{value not provided for security reason} , {${options.delegatorUrl as string}}`
}
);
}

// Delegation is made with a private key
if (
options.delegatorPrivateKey !== undefined &&
options.delegatorPrivateKey !== null
) {
return {
...currentConfiguration,
delegator: {
delegatorPrivateKey: ArgsValidator.delegatorPrivateKey(
options.delegatorPrivateKey as string
)
}
};
}

// Delegation is made with a delegator URL
if (
options.delegatorUrl !== undefined &&
options.delegatorUrl !== null
) {
return {
...currentConfiguration,
delegator: {
delegatorUrl: ArgsValidator.delegatorUrl(
options.delegatorUrl as string
)
}
};
}

return currentConfiguration;
}
};

export { ArgsValidatorAndGetter };
Loading

1 comment on commit af4475c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 99%
99.43% (3838/3860) 97.87% (1103/1127) 99.86% (764/765)
Title Tests Skipped Failures Errors Time
core 548 0 💤 0 ❌ 0 🔥 1m 28s ⏱️
network 686 0 💤 0 ❌ 0 🔥 3m 47s ⏱️
errors 43 0 💤 0 ❌ 0 🔥 14.794s ⏱️

Please sign in to comment.