Skip to content

Commit

Permalink
Merge branch 'main' into dependabot-npm_and_yarn-eslint-eslintrc-3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiorigam authored Jan 10, 2025
2 parents 73e4d0a + f35dfc3 commit 272407b
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 95 deletions.
6 changes: 5 additions & 1 deletion packages/network/src/http/SimpleHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SimpleHttpClient implements HttpClient {
* Executes an HTTP request with the specified method, path, and optional parameters.
*
* @param {HttpMethod} method - The HTTP method to use for the request (e.g., GET, POST).
* @param {string} path - The URL path for the request.
* @param {string} path - The URL path for the request. Leading slashes will be automatically removed.
* @param {HttpParams} [params] - Optional parameters for the request,
* including query parameters, headers, body, and response validation.
* {@link HttpParams.headers} override {@link SimpleHttpClient.headers}.
Expand All @@ -82,6 +82,10 @@ class SimpleHttpClient implements HttpClient {
controller.abort();
}, this.timeout);
try {
// Remove leading slash from path
if (path.startsWith('/')) {
path = path.slice(1);
}
const url = new URL(path, this.baseURL);
if (params?.query != null) {
Object.entries(params.query).forEach(([key, value]) => {
Expand Down
14 changes: 13 additions & 1 deletion packages/network/tests/http/SimpleHttpClient.testnet.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from '@jest/globals';
import { SimpleHttpClient } from '../../src/http';
import { HttpMethod, SimpleHttpClient } from '../../src/http';
import { TESTNET_URL } from '../../src';
import { ZERO_ADDRESS } from '../fixture';
import { stringifyData } from '@vechain/sdk-errors';
Expand Down Expand Up @@ -52,6 +52,18 @@ describe('SimpleHttpClient testnet tests', () => {
TIMEOUT
);

test('Test http without leading slash', async () => {
const httpClient = new SimpleHttpClient(TESTNET_URL);
const resp = await httpClient.http(HttpMethod.GET, 'blocks/best');
expect(resp).toBeDefined();
});

test('Test http with leading slash', async () => {
const httpClient = new SimpleHttpClient(TESTNET_URL);
const resp = await httpClient.http(HttpMethod.GET, '/blocks/best');
expect(resp).toBeDefined();
});

/*
NOTE: this test doesn't succeed in CI/CD.
Enable locally to challenge a real time-out calling testnet.
Expand Down
14 changes: 7 additions & 7 deletions packages/rpc-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,21 @@ So you can run the rpc-proxy with:
OR `npx rpc-proxy --accounts "7f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158 8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158"`

- `-m, --mnemonic <mnemonic>`: The mnemonic that the proxy server will use to sign transactions.
- `-mc, --mnemonicCount <mnemonicCount>`: The number of accounts to derive from the mnemonic.
- `-mi, --mnemonicInitialIndex <mnemonicInitialIndex>`: The index from which to start deriving accounts from the
- `--mnemonicCount <mnemonicCount>`: The number of accounts to derive from the mnemonic.
- `--mnemonicInitialIndex <mnemonicInitialIndex>`: The index from which to start deriving accounts from the
mnemonic.
- -e.g.- `npx rpc-proxy -m "denial kitchen pet squirrel other broom bar gas better priority spoil cross" -mc 10 -mi 1`
- -e.g.- `npx rpc-proxy -m "denial kitchen pet squirrel other broom bar gas better priority spoil cross" --mnemonicCount 10 --mnemonicInitialIndex 1`
OR `npx rpc-proxy --mnemonic "denial kitchen pet squirrel other broom bar gas better priority spoil cross" --mnemonicCount 10 --mnemonicInitialIndex 1`
- **NOTE**: --mnemonic, --mnemonicCount, and --mnemonicInitialIndex MUST be used together.

#### Use delegation

- `-e, --enableDelegation`: Whether to enable delegation.
- `-dp, --delegatorPrivateKey <delegatorPrivateKey>`: The private key of the delegator.
- `-du, --delegatorUrl <delegatorUrl>`: The URL of the delegator.
- -e.g.- `npx rpc-proxy -e -dp 8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158`
- `--delegatorPrivateKey <delegatorPrivateKey>`: The private key of the delegator.
- `-d, --delegatorUrl <delegatorUrl>`: The URL of the delegator.
- -e.g.- `npx rpc-proxy -e --delegatorPrivateKey 8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158`
OR `npx rpc-proxy --enableDelegation --delegatorPrivateKey 8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158`
- -e.g.- `npx rpc-proxy -e -du https://sponsor-testnet.vechain.energy/by/...`
- -e.g.- `npx rpc-proxy -e -d https://sponsor-testnet.vechain.energy/by/...`
OR `npx rpc-proxy --enableDelegation --delegatorUrl https://sponsor-testnet.vechain.energy/by/...`
- **NOTE**: --delegatorPrivateKey and --delegatorUrl are mutually exclusive.
- **NOTE**: if --enableDelegation is used, --delegatorPrivateKey OR --delegatorUrl MUST be used.
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@vechain/sdk-errors": "2.0.0-beta.1",
"@vechain/sdk-logging": "2.0.0-beta.1",
"@vechain/sdk-network": "2.0.0-beta.1",
"commander": "^12.1.0",
"commander": "^13.0.0",
"cors": "^2.8.5",
"express": "^4.21.2"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ const ArgsValidatorAndGetter = {
'ArgsValidatorAndGetter.delegation()',
'Both delegator private key and delegator URL are provided. Only one can be provided',
{
flag: '{-dp , --delegatorPrivateKey}, {-du , --delegatorUrl}',
flag: '{--delegatorPrivateKey}, {-d , --delegatorUrl}',
value: `{value not provided for security reason} , {${options.delegatorUrl as string}}`
}
);
Expand Down
8 changes: 4 additions & 4 deletions packages/rpc-proxy/src/utils/args-validator/args-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ const ArgsValidator = {
'ArgsValidator.mnemonicFields()',
'Invalid count provided. The parameter must be an integer',
{
flag: '-mc , --mnemonicCount',
flag: '--mnemonicCount',
value: String(mnemonicCount)
}
);
Expand All @@ -236,7 +236,7 @@ const ArgsValidator = {
'ArgsValidator.mnemonicFields()',
'Invalid initial index provided. The parameter must be an integer',
{
flag: '-mi , --mnemonicInitialIndex',
flag: '--mnemonicInitialIndex',
value: String(mnemonicInitialIndex)
}
);
Expand Down Expand Up @@ -266,7 +266,7 @@ const ArgsValidator = {
'ArgsValidator.delegatorPrivateKey()',
'An invalid delegator private key provided.',
{
flag: '-dp , --delegatorPrivateKey',
flag: '--delegatorPrivateKey',
value: 'Value will not be shown for security reasons'
}
);
Expand All @@ -290,7 +290,7 @@ const ArgsValidator = {
'ArgsValidator.delegatorUrl()',
'Invalid delegator url provided. The parameter must be a valid url',
{
flag: '-du , --delegatorUrl',
flag: '-d , --delegatorUrl',
value: delegatorUrl
}
);
Expand Down
16 changes: 8 additions & 8 deletions packages/rpc-proxy/src/utils/args/args-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import { Command, Option, type OptionValues } from 'commander';
* * Where accounts is a space separated list of private keys (e.g. "PK1 PK2 PK3 ...")
*
* rpc-proxy {-m|--mnemonic} <mnemonic> - Mnemonic to use for signing transactions
* rpc-proxy {-mc|--mnemonicCount} <count> - Number of accounts to derive from the mnemonic
* rpc-proxy {-mi|--mnemonicInitialIndex} <index> - Initial index to start deriving accounts from the mnemonic
* rpc-proxy {--mnemonicCount} <count> - Number of accounts to derive from the mnemonic
* rpc-proxy {--mnemonicInitialIndex} <index> - Initial index to start deriving accounts from the mnemonic
*
* rpc-proxy {-e|--enableDelegation} - Enable delegation
* rpc-proxy {-dp|--delegatorPrivateKey} <delegatorPrivateKey> - Delegator private key
* rpc-proxy {-du|--delegatorUrl} <delegatorUrl> - Delegator URL
* rpc-proxy {--delegatorPrivateKey} <delegatorPrivateKey> - Delegator private key
* rpc-proxy {-d|--delegatorUrl} <delegatorUrl> - Delegator URL
*
* rpc-proxy {-v|--verbose} - Enable verbose logging
*
Expand Down Expand Up @@ -63,13 +63,13 @@ function getOptionsFromCommandLine(
)
.addOption(
new Option(
'-mc, --mnemonicCount <count>',
'--mnemonicCount <count>',
'Number of accounts to derive from the mnemonic'
)
)
.addOption(
new Option(
'-mi, --mnemonicInitialIndex <index>',
'--mnemonicInitialIndex <index>',
'Initial index to start deriving accounts from the mnemonic'
)
)
Expand All @@ -80,14 +80,14 @@ function getOptionsFromCommandLine(
// Delegator configuration (private key)
.addOption(
new Option(
'-dp, --delegatorPrivateKey <delegatorPrivateKey>',
'--delegatorPrivateKey <delegatorPrivateKey>',
'Delegator private key'
)
)

// Delegator configuration (url)
.addOption(
new Option('-du, --delegatorUrl <delegatorUrl>', 'Delegator URL')
new Option('-d, --delegatorUrl <delegatorUrl>', 'Delegator URL')
)

// Enable verbose logging
Expand Down
12 changes: 8 additions & 4 deletions packages/rpc-proxy/src/utils/args/env-to-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ function getArgsFromEnv(): string[] {
getCliFieldFromEnv('-p', process.env.PORT, 'PORT'),
getCliFieldFromEnv('-a', process.env.ACCOUNTS, 'ACCOUNTS'),
getCliFieldFromEnv('-m', process.env.MNEMONIC, 'MNEMONIC'),
getCliFieldFromEnv('-mc', process.env.MNEMONIC_COUNT, 'MNEMONIC_COUNT'),
getCliFieldFromEnv(
'-mi',
'--mnemonicCount',
process.env.MNEMONIC_COUNT,
'MNEMONIC_COUNT'
),
getCliFieldFromEnv(
'--mnemonicInitialIndex',
process.env.MNEMONIC_INITIAL_INDEX,
'MNEMONIC_INITIAL_INDEX'
),
Expand All @@ -45,11 +49,11 @@ function getArgsFromEnv(): string[] {
'ENABLE_DELEGATION'
),
getCliFieldFromEnv(
'-dp',
'--delegatorPrivateKey',
process.env.DELEGATOR_PRIVATE_KEY,
'DELEGATOR_PRIVATE_KEY'
),
getCliFieldFromEnv('-du', process.env.DELEGATOR_URL, 'DELEGATOR_URL'),
getCliFieldFromEnv('-d', process.env.DELEGATOR_URL, 'DELEGATOR_URL'),
getCliFieldFromEnv('-v', process.env.VERBOSE, 'VERBOSE'),
getCliFieldFromEnv(
'-c',
Expand Down
13 changes: 3 additions & 10 deletions packages/rpc-proxy/tests/utils/args/args-options.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ describe('Args options tests', () => {
'program',
'-m',
'expire pair material agent north ostrich fortune level cousin snow mixture nurse',
'-mi',
'--mnemonicInitialIndex',
'1',
'-mc',
'--mnemonicCount',
'2'
]
].forEach((args) => {
Expand Down Expand Up @@ -161,13 +161,6 @@ describe('Args options tests', () => {
'program',
'--delegatorPrivateKey',
'8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158'
],
// Short syntax
[
'path',
'program',
'-dp',
'8f9290cc44c5fd2b95fe21d6ad6fe5fa9c177e1cd6f3b4c96a97b13e09eaa158'
]
].forEach((args) => {
const delegatorPrivateKeyOption = getOptionsFromCommandLine(
Expand All @@ -190,7 +183,7 @@ describe('Args options tests', () => {
// Normal syntax
['path', 'program', '--delegatorUrl', 'http://localhost:8080'],
// Short syntax
['path', 'program', '-du', 'http://localhost:8080']
['path', 'program', '-d', 'http://localhost:8080']
].forEach((args) => {
const delegatorUrlOption = getOptionsFromCommandLine(
'1.0.0',
Expand Down
Loading

0 comments on commit 272407b

Please sign in to comment.