diff --git a/CHANGELOG.md b/CHANGELOG.md index 1967a7d8df1..e94a44e3b24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/web3-eth-accounts/src/account.ts b/packages/web3-eth-accounts/src/account.ts index c7360e991fd..a0763cc9fee 100644 --- a/packages/web3-eth-accounts/src/account.ts +++ b/packages/web3-eth-accounts/src/account.ts @@ -52,6 +52,7 @@ import { sha3Raw, toChecksumAddress, utf8ToHex, + uuidV4, } from 'web3-utils'; import { isBuffer, isNullish, isString, validator } from 'web3-validator'; import { keyStoreSchema } from './schemas'; @@ -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 * diff --git a/packages/web3-utils/CHANGELOG.md b/packages/web3-utils/CHANGELOG.md index f4699771532..fb6a67553e5 100644 --- a/packages/web3-utils/CHANGELOG.md +++ b/packages/web3-utils/CHANGELOG.md @@ -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). diff --git a/packages/web3-utils/src/index.ts b/packages/web3-utils/src/index.ts index 48a509f92dd..4d7eeba2aa7 100644 --- a/packages/web3-utils/src/index.ts +++ b/packages/web3-utils/src/index.ts @@ -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'; diff --git a/packages/web3-utils/src/json_rpc.ts b/packages/web3-utils/src/json_rpc.ts index 1105e0b1456..6993fe97d67 100644 --- a/packages/web3-utils/src/json_rpc.ts +++ b/packages/web3-utils/src/json_rpc.ts @@ -29,7 +29,7 @@ import { JsonRpcSubscriptionResult, } from 'web3-types'; -let messageId = 0; +import { uuidV4 } from './uuid'; export const isResponseWithResult = ( response: JsonRpcResponse, @@ -89,16 +89,12 @@ export const isBatchResponse = ( export const toPayload = ( request: JsonRpcOptionalRequest, -): JsonRpcPayload => { - messageId += 1; - - return { - jsonrpc: request.jsonrpc ?? '2.0', - id: request.id ?? messageId, - method: request.method, - params: request.params ?? undefined, - }; -}; +): JsonRpcPayload => ({ + jsonrpc: request.jsonrpc ?? '2.0', + id: request.id ?? uuidV4(), + method: request.method, + params: request.params ?? undefined, +}); export const toBatchPayload = (requests: JsonRpcOptionalRequest[]): JsonRpcBatchRequest => requests.map(request => toPayload(request)) as JsonRpcBatchRequest; diff --git a/packages/web3-utils/src/uuid.ts b/packages/web3-utils/src/uuid.ts new file mode 100644 index 00000000000..152ee0d02d9 --- /dev/null +++ b/packages/web3-utils/src/uuid.ts @@ -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 . +*/ + +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('-'); +}; diff --git a/packages/web3-utils/test/fixtures/json_rpc.ts b/packages/web3-utils/test/fixtures/json_rpc.ts index 8591ecae252..516711abceb 100644 --- a/packages/web3-utils/test/fixtures/json_rpc.ts +++ b/packages/web3-utils/test/fixtures/json_rpc.ts @@ -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, + }, + ], ];