web3-utils
Advanced tools
Comparing version 4.3.1-dev.7537f03.0 to 4.3.1-dev.89711ab.0
@@ -341,2 +341,10 @@ "use strict"; | ||
if ((0, web3_validator_1.isHex)(value) && !(0, web3_validator_1.isInt)(value) && (0, web3_validator_1.isUInt)(value)) { | ||
// This condition seems problematic because meeting | ||
// both conditions `!isInt(value) && isUInt(value)` should be impossible. | ||
// But a value pass for those conditions: "101611154195520776335741463917853444671577865378275924493376429267637792638729" | ||
// Note that according to the docs: it is supposed to be treated as a string (https://docs.web3js.org/guides/web3_upgrade_guide/x/web3_utils_migration_guide#conversion-to-hex) | ||
// In short, the strange is that isInt(value) is false but isUInt(value) is true for the value above. | ||
// TODO: isUInt(value) should be investigated. | ||
// However, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729', true)` is called, it will return `true`. | ||
// But, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729')` is called, it will throw inside `numberToHex`. | ||
return returnType ? 'uint' : (0, exports.numberToHex)(value); | ||
@@ -373,3 +381,3 @@ } | ||
if (typeof value === 'number') { | ||
if (value > 1e+20) { | ||
if (value > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
@@ -376,0 +384,0 @@ // JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, |
@@ -48,5 +48,4 @@ "use strict"; | ||
if (result.oneOf && previousDataPath) { | ||
const path = oneOfPath.find(function (element) { | ||
return this === element[0]; | ||
}, previousDataPath !== null && previousDataPath !== void 0 ? previousDataPath : ''); | ||
const currentDataPath = previousDataPath; | ||
const path = oneOfPath.find(([key]) => key === currentDataPath); | ||
if (path && path[0] === previousDataPath) { | ||
@@ -65,5 +64,2 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||
const node = result.items.properties; | ||
if (!node) { | ||
return undefined; | ||
} | ||
result = node[dataPart]; | ||
@@ -246,3 +242,3 @@ } | ||
if ((0, web3_validator_1.isObject)(value)) { | ||
(0, exports.convert)(value, schema, dataPath, format); | ||
(0, exports.convert)(value, schema, dataPath, format, oneOfPath); | ||
dataPath.pop(); | ||
@@ -249,0 +245,0 @@ continue; |
import { Bytes, Numbers, Sha3Input, TypedObject, TypedObjectAbbreviated } from 'web3-types'; | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export declare const keccak256Wrapper: (data: Bytes | Numbers | string | ReadonlyArray<number>) => string; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* computes the Keccak-256 hash of the input and returns a hexstring | ||
@@ -33,21 +52,2 @@ * @param data - the input to hash | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export declare const keccak256Wrapper: (data: Bytes | Numbers | string | ReadonlyArray<number>) => string; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* returns a string of the tightly packed value given based on the type | ||
@@ -54,0 +54,0 @@ * @param arg - the input to return the tightly packed value |
@@ -19,3 +19,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getStorageSlotNumForLongString = exports.soliditySha3Raw = exports.soliditySha3 = exports.encodePacked = exports.processSolidityEncodePackedArgs = exports.keccak256 = exports.keccak256Wrapper = exports.sha3Raw = exports.sha3 = void 0; | ||
exports.getStorageSlotNumForLongString = exports.soliditySha3Raw = exports.soliditySha3 = exports.encodePacked = exports.processSolidityEncodePackedArgs = exports.sha3Raw = exports.sha3 = exports.keccak256 = exports.keccak256Wrapper = void 0; | ||
/** | ||
@@ -53,2 +53,37 @@ * This package provides utility functions for Ethereum dapps and other web3.js packages. | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
const keccak256Wrapper = (data) => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = (0, utils_js_1.utf8ToBytes)(data.toString()); | ||
} | ||
else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} | ||
else if (typeof data === 'string' && !(0, web3_validator_1.isHexStrict)(data)) { | ||
processedData = (0, utils_js_1.utf8ToBytes)(data); | ||
} | ||
else { | ||
processedData = (0, converters_js_1.bytesToUint8Array)(data); | ||
} | ||
return (0, converters_js_1.bytesToHex)((0, keccak_js_1.keccak256)(web3_validator_1.utils.ensureIfUint8Array(processedData))); | ||
}; | ||
exports.keccak256Wrapper = keccak256Wrapper; | ||
exports.keccak256 = exports.keccak256Wrapper; | ||
/** | ||
* computes the Keccak-256 hash of the input and returns a hexstring | ||
@@ -80,3 +115,3 @@ * @param data - the input to hash | ||
} | ||
const hash = (0, converters_js_1.bytesToHex)((0, keccak_js_1.keccak256)(web3_validator_1.utils.ensureIfUint8Array(updatedData))); | ||
const hash = (0, exports.keccak256Wrapper)(updatedData); | ||
// EIP-1052 if hash is equal to c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470, keccak was given empty data | ||
@@ -109,37 +144,2 @@ return hash === SHA3_EMPTY_BYTES ? undefined : hash; | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
const keccak256Wrapper = (data) => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = (0, utils_js_1.utf8ToBytes)(data.toString()); | ||
} | ||
else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} | ||
else if (typeof data === 'string' && !(0, web3_validator_1.isHexStrict)(data)) { | ||
processedData = (0, utils_js_1.utf8ToBytes)(data); | ||
} | ||
else { | ||
processedData = (0, converters_js_1.bytesToUint8Array)(data); | ||
} | ||
return (0, converters_js_1.bytesToHex)((0, keccak_js_1.keccak256)(web3_validator_1.utils.ensureIfUint8Array(processedData))); | ||
}; | ||
exports.keccak256Wrapper = keccak256Wrapper; | ||
exports.keccak256 = exports.keccak256Wrapper; | ||
/** | ||
* returns type and value | ||
@@ -146,0 +146,0 @@ * @param arg - the input to return the type and value |
@@ -13,3 +13,3 @@ import { JsonRpcPayload, JsonRpcResponse, JsonRpcResponseWithResult, JsonRpcResponseWithError, JsonRpcOptionalRequest, JsonRpcBatchRequest, JsonRpcNotification, JsonRpcRequest, JsonRpcBatchResponse, JsonRpcSubscriptionResult } from 'web3-types'; | ||
* Without calling this function, the `id` will be filled with a Uuid. | ||
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable. | ||
* But after this being called with a number, the `id` will be a number starting from the provided `start` variable. | ||
* However, if `undefined` was passed to this function, the `id` will be a Uuid again. | ||
@@ -16,0 +16,0 @@ * @param start - a number to start incrementing from. |
@@ -70,3 +70,3 @@ "use strict"; | ||
* Without calling this function, the `id` will be filled with a Uuid. | ||
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable. | ||
* But after this being called with a number, the `id` will be a number starting from the provided `start` variable. | ||
* However, if `undefined` was passed to this function, the `id` will be a Uuid again. | ||
@@ -73,0 +73,0 @@ * @param start - a number to start incrementing from. |
@@ -43,3 +43,3 @@ export declare type Timer = ReturnType<typeof setInterval>; | ||
* If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned. | ||
* @param cond - The function/confition to call. | ||
* @param cond - The function/condition to call. | ||
* @param interval - The interval in milliseconds. | ||
@@ -46,0 +46,0 @@ * @returns - an array with the interval ID and the Promise. |
@@ -76,3 +76,3 @@ "use strict"; | ||
const polledRes = new Promise((resolve, reject) => { | ||
intervalId = setInterval(function intervalCallbackFunc() { | ||
intervalId = setInterval((function intervalCallbackFunc() { | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
@@ -92,4 +92,4 @@ try { | ||
return intervalCallbackFunc; | ||
}() // this will immediate invoke first call | ||
, interval); | ||
})(), // this will immediate invoke first call | ||
interval); | ||
}); | ||
@@ -137,3 +137,3 @@ return [polledRes, intervalId]; | ||
* If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned. | ||
* @param cond - The function/confition to call. | ||
* @param cond - The function/condition to call. | ||
* @param interval - The interval in milliseconds. | ||
@@ -140,0 +140,0 @@ * @returns - an array with the interval ID and the Promise. |
@@ -177,6 +177,7 @@ "use strict"; | ||
const interval = setInterval(() => { | ||
if (forceDisconnect && retryAttempt === 5) { | ||
if (forceDisconnect && retryAttempt >= 5) { | ||
this.clearQueues(); | ||
} | ||
if (this.getPendingRequestQueueSize() === 0 && this.getSentRequestsQueueSize() === 0) { | ||
if (this.getPendingRequestQueueSize() === 0 && | ||
this.getSentRequestsQueueSize() === 0) { | ||
clearInterval(interval); | ||
@@ -234,3 +235,3 @@ resolve(true); | ||
this._removeSocketListeners(); | ||
this.connect(); | ||
this.connect(); // this can error out | ||
this.isReconnecting = false; | ||
@@ -298,5 +299,12 @@ }, this._reconnectOptions.delay); | ||
for (const [id, value] of this._pendingRequestsQueue.entries()) { | ||
this._sendToSocket(value.payload); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
try { | ||
this._sendToSocket(value.payload); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
} | ||
catch (error) { | ||
// catches if sendTosocket fails | ||
this._pendingRequestsQueue.delete(id); | ||
this._eventEmitter.emit('error', error); | ||
} | ||
} | ||
@@ -303,0 +311,0 @@ } |
@@ -327,2 +327,10 @@ /* | ||
if (isHex(value) && !isInt(value) && isUInt(value)) { | ||
// This condition seems problematic because meeting | ||
// both conditions `!isInt(value) && isUInt(value)` should be impossible. | ||
// But a value pass for those conditions: "101611154195520776335741463917853444671577865378275924493376429267637792638729" | ||
// Note that according to the docs: it is supposed to be treated as a string (https://docs.web3js.org/guides/web3_upgrade_guide/x/web3_utils_migration_guide#conversion-to-hex) | ||
// In short, the strange is that isInt(value) is false but isUInt(value) is true for the value above. | ||
// TODO: isUInt(value) should be investigated. | ||
// However, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729', true)` is called, it will return `true`. | ||
// But, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729')` is called, it will throw inside `numberToHex`. | ||
return returnType ? 'uint' : numberToHex(value); | ||
@@ -358,3 +366,3 @@ } | ||
if (typeof value === 'number') { | ||
if (value > 1e+20) { | ||
if (value > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
@@ -361,0 +369,0 @@ // JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, |
@@ -44,5 +44,4 @@ /* | ||
if (result.oneOf && previousDataPath) { | ||
const path = oneOfPath.find(function (element) { | ||
return this === element[0]; | ||
}, previousDataPath !== null && previousDataPath !== void 0 ? previousDataPath : ''); | ||
const currentDataPath = previousDataPath; | ||
const path = oneOfPath.find(([key]) => key === currentDataPath); | ||
if (path && path[0] === previousDataPath) { | ||
@@ -61,5 +60,2 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||
const node = result.items.properties; | ||
if (!node) { | ||
return undefined; | ||
} | ||
result = node[dataPart]; | ||
@@ -241,3 +237,3 @@ } | ||
if (isObject(value)) { | ||
convert(value, schema, dataPath, format); | ||
convert(value, schema, dataPath, format, oneOfPath); | ||
dataPath.pop(); | ||
@@ -244,0 +240,0 @@ continue; |
@@ -49,2 +49,36 @@ /* | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export const keccak256Wrapper = (data) => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = utf8ToBytes(data.toString()); | ||
} | ||
else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} | ||
else if (typeof data === 'string' && !isHexStrict(data)) { | ||
processedData = utf8ToBytes(data); | ||
} | ||
else { | ||
processedData = bytesToUint8Array(data); | ||
} | ||
return bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(processedData))); | ||
}; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* computes the Keccak-256 hash of the input and returns a hexstring | ||
@@ -76,3 +110,3 @@ * @param data - the input to hash | ||
} | ||
const hash = bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(updatedData))); | ||
const hash = keccak256Wrapper(updatedData); | ||
// EIP-1052 if hash is equal to c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470, keccak was given empty data | ||
@@ -103,36 +137,2 @@ return hash === SHA3_EMPTY_BYTES ? undefined : hash; | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export const keccak256Wrapper = (data) => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = utf8ToBytes(data.toString()); | ||
} | ||
else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} | ||
else if (typeof data === 'string' && !isHexStrict(data)) { | ||
processedData = utf8ToBytes(data); | ||
} | ||
else { | ||
processedData = bytesToUint8Array(data); | ||
} | ||
return bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(processedData))); | ||
}; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* returns type and value | ||
@@ -139,0 +139,0 @@ * @param arg - the input to return the type and value |
@@ -59,3 +59,3 @@ /* | ||
* Without calling this function, the `id` will be filled with a Uuid. | ||
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable. | ||
* But after this being called with a number, the `id` will be a number starting from the provided `start` variable. | ||
* However, if `undefined` was passed to this function, the `id` will be a Uuid again. | ||
@@ -62,0 +62,0 @@ * @param start - a number to start incrementing from. |
@@ -71,3 +71,3 @@ /* | ||
const polledRes = new Promise((resolve, reject) => { | ||
intervalId = setInterval(function intervalCallbackFunc() { | ||
intervalId = setInterval((function intervalCallbackFunc() { | ||
(() => __awaiter(this, void 0, void 0, function* () { | ||
@@ -87,4 +87,4 @@ try { | ||
return intervalCallbackFunc; | ||
}() // this will immediate invoke first call | ||
, interval); | ||
})(), // this will immediate invoke first call | ||
interval); | ||
}); | ||
@@ -129,3 +129,3 @@ return [polledRes, intervalId]; | ||
* If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned. | ||
* @param cond - The function/confition to call. | ||
* @param cond - The function/condition to call. | ||
* @param interval - The interval in milliseconds. | ||
@@ -132,0 +132,0 @@ * @returns - an array with the interval ID and the Promise. |
@@ -151,6 +151,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
const interval = setInterval(() => { | ||
if (forceDisconnect && retryAttempt === 5) { | ||
if (forceDisconnect && retryAttempt >= 5) { | ||
this.clearQueues(); | ||
} | ||
if (this.getPendingRequestQueueSize() === 0 && this.getSentRequestsQueueSize() === 0) { | ||
if (this.getPendingRequestQueueSize() === 0 && | ||
this.getSentRequestsQueueSize() === 0) { | ||
clearInterval(interval); | ||
@@ -208,3 +209,3 @@ resolve(true); | ||
this._removeSocketListeners(); | ||
this.connect(); | ||
this.connect(); // this can error out | ||
this.isReconnecting = false; | ||
@@ -272,5 +273,12 @@ }, this._reconnectOptions.delay); | ||
for (const [id, value] of this._pendingRequestsQueue.entries()) { | ||
this._sendToSocket(value.payload); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
try { | ||
this._sendToSocket(value.payload); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
} | ||
catch (error) { | ||
// catches if sendTosocket fails | ||
this._pendingRequestsQueue.delete(id); | ||
this._eventEmitter.emit('error', error); | ||
} | ||
} | ||
@@ -277,0 +285,0 @@ } |
import { Bytes, Numbers, Sha3Input, TypedObject, TypedObjectAbbreviated } from 'web3-types'; | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export declare const keccak256Wrapper: (data: Bytes | Numbers | string | ReadonlyArray<number>) => string; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* computes the Keccak-256 hash of the input and returns a hexstring | ||
@@ -33,21 +52,2 @@ * @param data - the input to hash | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export declare const keccak256Wrapper: (data: Bytes | Numbers | string | ReadonlyArray<number>) => string; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* returns a string of the tightly packed value given based on the type | ||
@@ -54,0 +54,0 @@ * @param arg - the input to return the tightly packed value |
@@ -13,3 +13,3 @@ import { JsonRpcPayload, JsonRpcResponse, JsonRpcResponseWithResult, JsonRpcResponseWithError, JsonRpcOptionalRequest, JsonRpcBatchRequest, JsonRpcNotification, JsonRpcRequest, JsonRpcBatchResponse, JsonRpcSubscriptionResult } from 'web3-types'; | ||
* Without calling this function, the `id` will be filled with a Uuid. | ||
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable. | ||
* But after this being called with a number, the `id` will be a number starting from the provided `start` variable. | ||
* However, if `undefined` was passed to this function, the `id` will be a Uuid again. | ||
@@ -16,0 +16,0 @@ * @param start - a number to start incrementing from. |
@@ -43,3 +43,3 @@ export declare type Timer = ReturnType<typeof setInterval>; | ||
* If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned. | ||
* @param cond - The function/confition to call. | ||
* @param cond - The function/condition to call. | ||
* @param interval - The interval in milliseconds. | ||
@@ -46,0 +46,0 @@ * @returns - an array with the interval ID and the Promise. |
{ | ||
"name": "web3-utils", | ||
"sideEffects": false, | ||
"version": "4.3.1-dev.7537f03.0+7537f03", | ||
"version": "4.3.1-dev.89711ab.0+89711ab", | ||
"description": "Collection of utility functions used in web3.js.", | ||
@@ -68,7 +68,7 @@ "main": "./lib/commonjs/index.js", | ||
"eventemitter3": "^5.0.1", | ||
"web3-errors": "1.2.1-dev.7537f03.0+7537f03", | ||
"web3-types": "1.6.1-dev.7537f03.0+7537f03", | ||
"web3-validator": "2.0.7-dev.7537f03.0+7537f03" | ||
"web3-errors": "1.2.1-dev.89711ab.0+89711ab", | ||
"web3-types": "1.7.1-dev.89711ab.0+89711ab", | ||
"web3-validator": "2.0.7-dev.89711ab.0+89711ab" | ||
}, | ||
"gitHead": "7537f03db3a2932a4f5c6f13ada1c3d6ca0217cd" | ||
"gitHead": "89711ab7f6a63d910848807ee77f0a91497aa897" | ||
} |
@@ -82,3 +82,4 @@ /* | ||
const PrecisionLossWarning = 'Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods'; | ||
const PrecisionLossWarning = | ||
'Warning: Using type `number` with values that are large or contain many decimals may cause loss of precision, it is recommended to use type `string` or `BigInt` when using conversion methods'; | ||
@@ -370,3 +371,3 @@ export type EtherUnits = keyof typeof ethUnitMap; | ||
if(isUint8Array(value)) { | ||
if (isUint8Array(value)) { | ||
return returnType ? 'bytes' : bytesToHex(value); | ||
@@ -391,2 +392,11 @@ } | ||
if (isHex(value) && !isInt(value) && isUInt(value)) { | ||
// This condition seems problematic because meeting | ||
// both conditions `!isInt(value) && isUInt(value)` should be impossible. | ||
// But a value pass for those conditions: "101611154195520776335741463917853444671577865378275924493376429267637792638729" | ||
// Note that according to the docs: it is supposed to be treated as a string (https://docs.web3js.org/guides/web3_upgrade_guide/x/web3_utils_migration_guide#conversion-to-hex) | ||
// In short, the strange is that isInt(value) is false but isUInt(value) is true for the value above. | ||
// TODO: isUInt(value) should be investigated. | ||
// However, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729', true)` is called, it will return `true`. | ||
// But, if `toHex('101611154195520776335741463917853444671577865378275924493376429267637792638729')` is called, it will throw inside `numberToHex`. | ||
return returnType ? 'uint' : numberToHex(value); | ||
@@ -425,10 +435,10 @@ } | ||
if (typeof value === 'number') { | ||
if (value > 1e+20) { | ||
console.warn(PrecisionLossWarning) | ||
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, | ||
// leading to potential parsing errors and incorrect representations. | ||
// For instance, String(10000000000000000000000) yields '1e+22'. | ||
// Using BigInt prevents this | ||
return BigInt(value); | ||
} | ||
if (value > 1e20) { | ||
console.warn(PrecisionLossWarning); | ||
// JavaScript converts numbers >= 10^21 to scientific notation when coerced to strings, | ||
// leading to potential parsing errors and incorrect representations. | ||
// For instance, String(10000000000000000000000) yields '1e+22'. | ||
// Using BigInt prevents this | ||
return BigInt(value); | ||
} | ||
return value; | ||
@@ -513,6 +523,5 @@ } | ||
} | ||
denomination = bigintPower(BigInt(10),BigInt(unit)); | ||
denomination = bigintPower(BigInt(10), BigInt(unit)); | ||
} | ||
// value in wei would always be integer | ||
@@ -583,4 +592,4 @@ // 13456789, 1234 | ||
} | ||
denomination = bigintPower(BigInt(10),BigInt(unit)); | ||
denomination = bigintPower(BigInt(10), BigInt(unit)); | ||
} | ||
@@ -587,0 +596,0 @@ |
@@ -19,5 +19,4 @@ /* | ||
import EventEmitter3 from 'eventemitter3'; | ||
import EventEmitter3 from 'eventemitter3'; | ||
/** | ||
@@ -39,3 +38,2 @@ * This class copy the behavior of Node.js EventEmitter class. | ||
} | ||
} | ||
} |
@@ -60,6 +60,4 @@ /* | ||
if (result.oneOf && previousDataPath) { | ||
const path = oneOfPath.find(function (element: [string, number]) { | ||
return (this as unknown as string) === element[0]; | ||
}, previousDataPath ?? ''); | ||
const currentDataPath = previousDataPath; | ||
const path = oneOfPath.find(([key]) => key === currentDataPath); | ||
if (path && path[0] === previousDataPath) { | ||
@@ -79,6 +77,2 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access | ||
if (!node) { | ||
return undefined; | ||
} | ||
result = node[dataPart]; | ||
@@ -312,3 +306,3 @@ } else if (result.items && isObject(result.items)) { | ||
if (isObject(value)) { | ||
convert(value, schema, dataPath, format); | ||
convert(value, schema, dataPath, format, oneOfPath); | ||
dataPath.pop(); | ||
@@ -315,0 +309,0 @@ continue; |
@@ -20,21 +20,21 @@ /* | ||
* This package provides utility functions for Ethereum dapps and other web3.js packages. | ||
* | ||
* | ||
* For using Utils functions, first install Web3 package using `npm i web3` or `yarn add web3`. | ||
* After that, Web3 Utils functions will be available as mentioned below. | ||
* After that, Web3 Utils functions will be available as mentioned below. | ||
* ```ts | ||
* import { Web3 } from 'web3'; | ||
* const web3 = new Web3(); | ||
* | ||
* | ||
* const value = web3.utils.fromWei("1", "ether") | ||
* | ||
* | ||
* ``` | ||
* | ||
* | ||
* For using individual package install `web3-utils` package using `npm i web3-utils` or `yarn add web3-utils` and only import required functions. | ||
* This is more efficient approach for building lightweight applications. | ||
* This is more efficient approach for building lightweight applications. | ||
* ```ts | ||
* import { fromWei, soliditySha3Raw } from 'web3-utils'; | ||
* | ||
* | ||
* console.log(fromWei("1", "ether")); | ||
* console.log(soliditySha3Raw({ type: "string", value: "helloworld" })) | ||
* | ||
* | ||
* ``` | ||
@@ -78,2 +78,37 @@ * @module Utils | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export const keccak256Wrapper = ( | ||
data: Bytes | Numbers | string | ReadonlyArray<number>, | ||
): string => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = utf8ToBytes(data.toString()); | ||
} else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} else if (typeof data === 'string' && !isHexStrict(data)) { | ||
processedData = utf8ToBytes(data); | ||
} else { | ||
processedData = bytesToUint8Array(data as Bytes); | ||
} | ||
return bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(processedData))); | ||
}; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* computes the Keccak-256 hash of the input and returns a hexstring | ||
@@ -104,3 +139,3 @@ * @param data - the input to hash | ||
} | ||
const hash = bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(updatedData))); | ||
const hash = keccak256Wrapper(updatedData); | ||
@@ -135,37 +170,2 @@ // EIP-1052 if hash is equal to c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470, keccak was given empty data | ||
/** | ||
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array` | ||
* @param data - the input to hash | ||
* @returns - the Keccak-256 hash of the input | ||
* | ||
* @example | ||
* ```ts | ||
* console.log(web3.utils.keccak256Wrapper('web3.js')); | ||
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(1)); | ||
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 | ||
* | ||
* console.log(web3.utils.keccak256Wrapper(0xaf12fd)); | ||
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c | ||
* ``` | ||
*/ | ||
export const keccak256Wrapper = ( | ||
data: Bytes | Numbers | string | ReadonlyArray<number>, | ||
): string => { | ||
let processedData; | ||
if (typeof data === 'bigint' || typeof data === 'number') { | ||
processedData = utf8ToBytes(data.toString()); | ||
} else if (Array.isArray(data)) { | ||
processedData = new Uint8Array(data); | ||
} else if (typeof data === 'string' && !isHexStrict(data)) { | ||
processedData = utf8ToBytes(data); | ||
} else { | ||
processedData = bytesToUint8Array(data as Bytes); | ||
} | ||
return bytesToHex(keccak256(validatorUtils.ensureIfUint8Array(processedData))); | ||
}; | ||
export { keccak256Wrapper as keccak256 }; | ||
/** | ||
* returns type and value | ||
@@ -179,3 +179,2 @@ * @param arg - the input to return the type and value | ||
} | ||
let type; | ||
@@ -182,0 +181,0 @@ let value; |
@@ -102,3 +102,3 @@ /* | ||
* Without calling this function, the `id` will be filled with a Uuid. | ||
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable. | ||
* But after this being called with a number, the `id` will be a number starting from the provided `start` variable. | ||
* However, if `undefined` was passed to this function, the `id` will be a Uuid again. | ||
@@ -105,0 +105,0 @@ * @param start - a number to start incrementing from. |
@@ -23,3 +23,2 @@ /* | ||
/** | ||
@@ -78,3 +77,2 @@ * An alternative to the node function `isPromise` that exists in `util/types` because it is not available on the browser. | ||
/** | ||
@@ -90,21 +88,23 @@ * Repeatedly calls an async function with a given interval until the result of the function is defined (not undefined or null), | ||
): [Promise<Exclude<T, undefined>>, Timer] { | ||
let intervalId: Timer | undefined; | ||
const polledRes = new Promise<Exclude<T, undefined>>((resolve, reject) => { | ||
intervalId = setInterval(function intervalCallbackFunc(){ | ||
(async () => { | ||
try { | ||
const res = await waitWithTimeout(func, interval); | ||
intervalId = setInterval( | ||
(function intervalCallbackFunc() { | ||
(async () => { | ||
try { | ||
const res = await waitWithTimeout(func, interval); | ||
if (!isNullish(res)) { | ||
if (!isNullish(res)) { | ||
clearInterval(intervalId); | ||
resolve(res as unknown as Exclude<T, undefined>); | ||
} | ||
} catch (error) { | ||
clearInterval(intervalId); | ||
resolve(res as unknown as Exclude<T, undefined>); | ||
reject(error); | ||
} | ||
} catch (error) { | ||
clearInterval(intervalId); | ||
reject(error); | ||
} | ||
})() as unknown; | ||
return intervalCallbackFunc;}() // this will immediate invoke first call | ||
, interval); | ||
})() as unknown; | ||
return intervalCallbackFunc; | ||
})(), // this will immediate invoke first call | ||
interval, | ||
); | ||
}); | ||
@@ -119,3 +119,3 @@ | ||
* pollTillDefinedAndReturnIntervalId() function should be used instead of pollTillDefined if you need IntervalId in result. | ||
* This function will be deprecated in next major release so use pollTillDefinedAndReturnIntervalId(). | ||
* This function will be deprecated in next major release so use pollTillDefinedAndReturnIntervalId(). | ||
* @param func - The function to call. | ||
@@ -153,3 +153,3 @@ * @param interval - The interval in milliseconds. | ||
* If the condition is met, the interval is cleared and a Promise that rejects with the returned value is returned. | ||
* @param cond - The function/confition to call. | ||
* @param cond - The function/condition to call. | ||
* @param interval - The interval in milliseconds. | ||
@@ -176,2 +176,1 @@ * @returns - an array with the interval ID and the Promise. | ||
} | ||
@@ -190,3 +190,3 @@ /* | ||
} | ||
/** | ||
@@ -197,3 +197,3 @@ * | ||
// eslint-disable-next-line class-methods-use-this | ||
public getPendingRequestQueueSize() { | ||
public getPendingRequestQueueSize() { | ||
return this._pendingRequestsQueue.size; | ||
@@ -355,3 +355,3 @@ } | ||
* Safely disconnects the socket, async and waits for request size to be 0 before disconnecting | ||
* @param forceDisconnect - If true, will clear queue after 5 attempts of waiting for both pending and sent queue to be 0 | ||
* @param forceDisconnect - If true, will clear queue after 5 attempts of waiting for both pending and sent queue to be 0 | ||
* @param ms - Determines the ms of setInterval | ||
@@ -361,18 +361,21 @@ * @param code - The code to be sent to the server | ||
*/ | ||
public async safeDisconnect(code?: number, data?: string, forceDisconnect = false,ms = 1000) { | ||
public async safeDisconnect(code?: number, data?: string, forceDisconnect = false, ms = 1000) { | ||
let retryAttempt = 0; | ||
const checkQueue = async () => | ||
const checkQueue = async () => | ||
new Promise(resolve => { | ||
const interval = setInterval(() => { | ||
if (forceDisconnect && retryAttempt === 5) { | ||
if (forceDisconnect && retryAttempt >= 5) { | ||
this.clearQueues(); | ||
} | ||
if (this.getPendingRequestQueueSize() === 0 && this.getSentRequestsQueueSize() === 0) { | ||
if ( | ||
this.getPendingRequestQueueSize() === 0 && | ||
this.getSentRequestsQueueSize() === 0 | ||
) { | ||
clearInterval(interval); | ||
resolve(true); | ||
} | ||
retryAttempt+=1; | ||
}, ms) | ||
}) | ||
retryAttempt += 1; | ||
}, ms); | ||
}); | ||
await checkQueue(); | ||
@@ -382,3 +385,2 @@ this.disconnect(code, data); | ||
/** | ||
@@ -432,3 +434,3 @@ * Removes all listeners for the specified event type. | ||
this._removeSocketListeners(); | ||
this.connect(); | ||
this.connect(); // this can error out | ||
this.isReconnecting = false; | ||
@@ -510,5 +512,11 @@ }, this._reconnectOptions.delay); | ||
for (const [id, value] of this._pendingRequestsQueue.entries()) { | ||
this._sendToSocket(value.payload as Web3APIPayload<API, any>); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
try { | ||
this._sendToSocket(value.payload as Web3APIPayload<API, any>); | ||
this._pendingRequestsQueue.delete(id); | ||
this._sentRequestsQueue.set(id, value); | ||
} catch (error) { | ||
// catches if sendTosocket fails | ||
this._pendingRequestsQueue.delete(id); | ||
this._eventEmitter.emit('error', error); | ||
} | ||
} | ||
@@ -522,3 +530,3 @@ } | ||
} | ||
for (const response of responses) { | ||
@@ -555,3 +563,3 @@ if ( | ||
} | ||
public clearQueues(event?: ConnectionEvent) { | ||
@@ -558,0 +566,0 @@ this._clearQueues(event); |
@@ -176,3 +176,2 @@ /* | ||
export const isContractInitOptions = (options: unknown): options is ContractInitOptions => | ||
@@ -179,0 +178,0 @@ typeof options === 'object' && |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
537351
10217