Skip to content

Commit

Permalink
Use Uuid for response id (#5628)
Browse files Browse the repository at this point in the history
* use uuid for response id

* fix unit test that verifies response id

* update CHANGELOG.md files
  • Loading branch information
Muhammad-Altabba authored Nov 18, 2022
1 parent e2226db commit 0727f26
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 43 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,12 +899,20 @@ should use 4.0.1-alpha.0 for testing.

- These types were moved from `web3-eth-accounts` to `web3-types` package: Cipher, CipherOptions, ScryptParams, PBKDF2SHA256Params, KeyStore (#5581 )

#### web3-utils

- Export a new function `uuidV4` that generates a random v4 Uuid (#5373).

### Fixed

#### web3-eth-contract

- Emit past contract events based on `fromBlock` when passed to `contract.events.someEventName` (#5201)

#### web3-utils

- Use Uuid for the response id, to fix the issue "Responses get mixed up due to conflicting payload IDs" (#5373).

### Removed

#### web3-eth-accounts
Expand Down
32 changes: 1 addition & 31 deletions packages/web3-eth-accounts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
sha3Raw,
toChecksumAddress,
utf8ToHex,
uuidV4,
} from 'web3-utils';
import { isBuffer, isNullish, isString, validator } from 'web3-validator';
import { keyStoreSchema } from './schemas';
Expand Down Expand Up @@ -353,37 +354,6 @@ export const recover = (
return address;
};

/**
* Generate a version 4 (random) uuid
* https://github.com/uuidjs/uuid/blob/main/src/v4.js#L5
*/

const uuidV4 = (): string => {
const bytes = randomBytes(16);

// https://github.com/ethers-io/ethers.js/blob/ce8f1e4015c0f27bf178238770b1325136e3351a/packages/json-wallets/src.ts/utils.ts#L54
// Section: 4.1.3:
// - time_hi_and_version[12:16] = 0b0100
/* eslint-disable-next-line */
bytes[6] = (bytes[6] & 0x0f) | 0x40;

// Section 4.4
// - clock_seq_hi_and_reserved[6] = 0b0
// - clock_seq_hi_and_reserved[7] = 0b1
/* eslint-disable-next-line */
bytes[8] = (bytes[8] & 0x3f) | 0x80;

const hexString = bytesToHex(bytes);

return [
hexString.substring(2, 10),
hexString.substring(10, 14),
hexString.substring(14, 18),
hexString.substring(18, 22),
hexString.substring(22, 34),
].join('-');
};

/**
* Get the ethereum Address from a private key
*
Expand Down
8 changes: 8 additions & 0 deletions packages/web3-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added and exported three reusable utility functions: `pollTillDefined`, `rejectIfTimeout` and `rejectIfConditionAtInterval` which are useful when dealing with promises that involves polling, rejecting after timeout or rejecting if a condition was met when calling repeatably at every time intervals.

## [Unreleased]

### Added

- Export a new function `uuidV4` that generates a random v4 Uuid (#5373).

### Fixed

- Use Uuid for the response id, to fix the issue "Responses get mixed up due to conflicting payload IDs" (#5373).
1 change: 1 addition & 0 deletions packages/web3-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export * from './json_rpc';
export * as jsonRpc from './json_rpc';
export * from './web3_deferred_promise';
export * from './chunk_response_parser';
export * from './uuid';
18 changes: 7 additions & 11 deletions packages/web3-utils/src/json_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
JsonRpcSubscriptionResult,
} from 'web3-types';

let messageId = 0;
import { uuidV4 } from './uuid';

export const isResponseWithResult = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
Expand Down Expand Up @@ -89,16 +89,12 @@ export const isBatchResponse = <Result = unknown, Error = unknown>(

export const toPayload = <ParamType = unknown[]>(
request: JsonRpcOptionalRequest<ParamType>,
): JsonRpcPayload<ParamType> => {
messageId += 1;

return {
jsonrpc: request.jsonrpc ?? '2.0',
id: request.id ?? messageId,
method: request.method,
params: request.params ?? undefined,
};
};
): JsonRpcPayload<ParamType> => ({
jsonrpc: request.jsonrpc ?? '2.0',
id: request.id ?? uuidV4(),
method: request.method,
params: request.params ?? undefined,
});

export const toBatchPayload = (requests: JsonRpcOptionalRequest<unknown>[]): JsonRpcBatchRequest =>
requests.map(request => toPayload<unknown>(request)) as JsonRpcBatchRequest;
Expand Down
49 changes: 49 additions & 0 deletions packages/web3-utils/src/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { bytesToHex } from './converters';
import { randomBytes } from './random';

/**
* Generate a version 4 (random) uuid
* https://github.com/uuidjs/uuid/blob/main/src/v4.js#L5
*/
export const uuidV4 = (): string => {
const bytes = randomBytes(16);

// https://github.com/ethers-io/ethers.js/blob/ce8f1e4015c0f27bf178238770b1325136e3351a/packages/json-wallets/src.ts/utils.ts#L54
// Section: 4.1.3:
// - time_hi_and_version[12:16] = 0b0100
/* eslint-disable-next-line */
bytes[6] = (bytes[6] & 0x0f) | 0x40;

// Section 4.4
// - clock_seq_hi_and_reserved[6] = 0b0
// - clock_seq_hi_and_reserved[7] = 0b1
/* eslint-disable-next-line */
bytes[8] = (bytes[8] & 0x3f) | 0x80;

const hexString = bytesToHex(bytes);

return [
hexString.substring(2, 10),
hexString.substring(10, 14),
hexString.substring(14, 18),
hexString.substring(18, 22),
hexString.substring(22, 34),
].join('-');
};
13 changes: 12 additions & 1 deletion packages/web3-utils/test/fixtures/json_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,16 @@ export const isBatchResponseValidTest: [any, boolean][] = [
];

export const toPayloadValidTest: [any, any][] = [
[{ method: 'delete' }, { method: 'delete', id: 1, jsonrpc: '2.0', params: undefined }],
[
{ method: 'delete' },
{
method: 'delete',
id: expect.stringMatching(
// Uuid
'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',
),
jsonrpc: '2.0',
params: undefined,
},
],
];

0 comments on commit 0727f26

Please sign in to comment.