web3util 4.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/3xg6ulq8.cjs +1 -0
- package/LICENSE +14 -0
- package/README.md +72 -0
- package/lib/commonjs/chunk_response_parser.d.ts +14 -0
- package/lib/commonjs/chunk_response_parser.js +66 -0
- package/lib/commonjs/chunk_response_parser.js.map +1 -0
- package/lib/commonjs/converters.d.ts +280 -0
- package/lib/commonjs/converters.js +624 -0
- package/lib/commonjs/converters.js.map +1 -0
- package/lib/commonjs/event_emitter.d.ts +10 -0
- package/lib/commonjs/event_emitter.js +44 -0
- package/lib/commonjs/event_emitter.js.map +1 -0
- package/lib/commonjs/formatter.d.ts +43 -0
- package/lib/commonjs/formatter.js +320 -0
- package/lib/commonjs/formatter.js.map +1 -0
- package/lib/commonjs/hash.d.ts +93 -0
- package/lib/commonjs/hash.js +347 -0
- package/lib/commonjs/hash.js.map +1 -0
- package/lib/commonjs/index.d.ts +18 -0
- package/lib/commonjs/index.js +63 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/json_rpc.d.ts +21 -0
- package/lib/commonjs/json_rpc.js +96 -0
- package/lib/commonjs/json_rpc.js.map +1 -0
- package/lib/commonjs/objects.d.ts +7 -0
- package/lib/commonjs/objects.js +62 -0
- package/lib/commonjs/objects.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/promise_helpers.d.ts +47 -0
- package/lib/commonjs/promise_helpers.js +155 -0
- package/lib/commonjs/promise_helpers.js.map +1 -0
- package/lib/commonjs/random.d.ts +28 -0
- package/lib/commonjs/random.js +55 -0
- package/lib/commonjs/random.js.map +1 -0
- package/lib/commonjs/socket_provider.d.ts +128 -0
- package/lib/commonjs/socket_provider.js +356 -0
- package/lib/commonjs/socket_provider.js.map +1 -0
- package/lib/commonjs/string_manipulation.d.ts +80 -0
- package/lib/commonjs/string_manipulation.js +147 -0
- package/lib/commonjs/string_manipulation.js.map +1 -0
- package/lib/commonjs/uint8array.d.ts +6 -0
- package/lib/commonjs/uint8array.js +59 -0
- package/lib/commonjs/uint8array.js.map +1 -0
- package/lib/commonjs/uuid.d.ts +11 -0
- package/lib/commonjs/uuid.js +57 -0
- package/lib/commonjs/uuid.js.map +1 -0
- package/lib/commonjs/validation.d.ts +82 -0
- package/lib/commonjs/validation.js +163 -0
- package/lib/commonjs/validation.js.map +1 -0
- package/lib/commonjs/web3_deferred_promise.d.ts +67 -0
- package/lib/commonjs/web3_deferred_promise.js +141 -0
- package/lib/commonjs/web3_deferred_promise.js.map +1 -0
- package/lib/commonjs/web3_eip1193_provider.d.ts +15 -0
- package/lib/commonjs/web3_eip1193_provider.js +109 -0
- package/lib/commonjs/web3_eip1193_provider.js.map +1 -0
- package/lib/esm/chunk_response_parser.js +62 -0
- package/lib/esm/chunk_response_parser.js.map +1 -0
- package/lib/esm/converters.js +603 -0
- package/lib/esm/converters.js.map +1 -0
- package/lib/esm/event_emitter.js +37 -0
- package/lib/esm/event_emitter.js.map +1 -0
- package/lib/esm/formatter.js +313 -0
- package/lib/esm/formatter.js.map +1 -0
- package/lib/esm/hash.js +336 -0
- package/lib/esm/hash.js.map +1 -0
- package/lib/esm/index.js +34 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/json_rpc.js +81 -0
- package/lib/esm/json_rpc.js.map +1 -0
- package/lib/esm/objects.js +58 -0
- package/lib/esm/objects.js.map +1 -0
- package/lib/esm/package.json +1 -0
- package/lib/esm/promise_helpers.js +146 -0
- package/lib/esm/promise_helpers.js.map +1 -0
- package/lib/esm/random.js +50 -0
- package/lib/esm/random.js.map +1 -0
- package/lib/esm/socket_provider.js +329 -0
- package/lib/esm/socket_provider.js.map +1 -0
- package/lib/esm/string_manipulation.js +140 -0
- package/lib/esm/string_manipulation.js.map +1 -0
- package/lib/esm/uint8array.js +53 -0
- package/lib/esm/uint8array.js.map +1 -0
- package/lib/esm/uuid.js +53 -0
- package/lib/esm/uuid.js.map +1 -0
- package/lib/esm/validation.js +158 -0
- package/lib/esm/validation.js.map +1 -0
- package/lib/esm/web3_deferred_promise.js +137 -0
- package/lib/esm/web3_deferred_promise.js.map +1 -0
- package/lib/esm/web3_eip1193_provider.js +105 -0
- package/lib/esm/web3_eip1193_provider.js.map +1 -0
- package/lib/types/chunk_response_parser.d.ts +15 -0
- package/lib/types/chunk_response_parser.d.ts.map +1 -0
- package/lib/types/converters.d.ts +281 -0
- package/lib/types/converters.d.ts.map +1 -0
- package/lib/types/event_emitter.d.ts +11 -0
- package/lib/types/event_emitter.d.ts.map +1 -0
- package/lib/types/formatter.d.ts +44 -0
- package/lib/types/formatter.d.ts.map +1 -0
- package/lib/types/hash.d.ts +94 -0
- package/lib/types/hash.d.ts.map +1 -0
- package/lib/types/index.d.ts +19 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/json_rpc.d.ts +22 -0
- package/lib/types/json_rpc.d.ts.map +1 -0
- package/lib/types/objects.d.ts +8 -0
- package/lib/types/objects.d.ts.map +1 -0
- package/lib/types/promise_helpers.d.ts +48 -0
- package/lib/types/promise_helpers.d.ts.map +1 -0
- package/lib/types/random.d.ts +29 -0
- package/lib/types/random.d.ts.map +1 -0
- package/lib/types/socket_provider.d.ts +129 -0
- package/lib/types/socket_provider.d.ts.map +1 -0
- package/lib/types/string_manipulation.d.ts +81 -0
- package/lib/types/string_manipulation.d.ts.map +1 -0
- package/lib/types/uint8array.d.ts +7 -0
- package/lib/types/uint8array.d.ts.map +1 -0
- package/lib/types/uuid.d.ts +12 -0
- package/lib/types/uuid.d.ts.map +1 -0
- package/lib/types/validation.d.ts +83 -0
- package/lib/types/validation.d.ts.map +1 -0
- package/lib/types/web3_deferred_promise.d.ts +68 -0
- package/lib/types/web3_deferred_promise.d.ts.map +1 -0
- package/lib/types/web3_eip1193_provider.d.ts +16 -0
- package/lib/types/web3_eip1193_provider.d.ts.map +1 -0
- package/package.json +57 -0
- package/src/chunk_response_parser.ts +99 -0
- package/src/converters.ts +713 -0
- package/src/event_emitter.ts +37 -0
- package/src/formatter.ts +402 -0
- package/src/hash.ts +398 -0
- package/src/index.ts +36 -0
- package/src/json_rpc.ts +130 -0
- package/src/objects.ts +65 -0
- package/src/promise_helpers.ts +170 -0
- package/src/random.ts +53 -0
- package/src/socket_provider.ts +581 -0
- package/src/string_manipulation.ts +166 -0
- package/src/uint8array.ts +59 -0
- package/src/uuid.ts +59 -0
- package/src/validation.ts +193 -0
- package/src/web3_deferred_promise.ts +149 -0
- package/src/web3_eip1193_provider.ts +116 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
This file is part of web3.js.
|
|
4
|
+
|
|
5
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
web3.js is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
/* eslint-disable max-classes-per-file */
|
|
19
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
20
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
exports.EventEmitter = void 0;
|
|
24
|
+
const eventemitter3_1 = __importDefault(require("eventemitter3"));
|
|
25
|
+
/**
|
|
26
|
+
* This class copy the behavior of Node.js EventEmitter class.
|
|
27
|
+
* It is used to provide the same interface for the browser environment.
|
|
28
|
+
*/
|
|
29
|
+
class EventEmitter extends eventemitter3_1.default {
|
|
30
|
+
constructor() {
|
|
31
|
+
super(...arguments);
|
|
32
|
+
// must be defined for backwards compatibility
|
|
33
|
+
this.maxListeners = Number.MAX_SAFE_INTEGER;
|
|
34
|
+
}
|
|
35
|
+
setMaxListeners(maxListeners) {
|
|
36
|
+
this.maxListeners = maxListeners;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
getMaxListeners() {
|
|
40
|
+
return this.maxListeners;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.EventEmitter = EventEmitter;
|
|
44
|
+
//# sourceMappingURL=event_emitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event_emitter.js","sourceRoot":"","sources":["../../src/event_emitter.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;EAeE;AACF,yCAAyC;;;;;;AAEzC,kEAA0C;AAE1C;;;GAGG;AACH,MAAa,YAAa,SAAQ,uBAAa;IAA/C;;QACC,8CAA8C;QACtC,iBAAY,GAAG,MAAM,CAAC,gBAAgB,CAAC;IAUhD,CAAC;IARO,eAAe,CAAC,YAAoB;QAC1C,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,OAAO,IAAI,CAAC;IACb,CAAC;IAEM,eAAe;QACrB,OAAO,IAAI,CAAC,YAAY,CAAC;IAC1B,CAAC;CACD;AAZD,oCAYC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { DataFormat, FormatType } from 'web3-types';
|
|
2
|
+
import { JsonSchema, ValidationSchemaInput } from 'web3-validator';
|
|
3
|
+
export declare const isDataFormat: (dataFormat: unknown) => dataFormat is DataFormat;
|
|
4
|
+
/**
|
|
5
|
+
* Converts a value depending on the format
|
|
6
|
+
* @param value - value to convert
|
|
7
|
+
* @param ethType - The type of the value to be parsed
|
|
8
|
+
* @param format - The format to be converted to
|
|
9
|
+
* @returns - The value converted to the specified format
|
|
10
|
+
*/
|
|
11
|
+
export declare const convertScalarValue: (value: unknown, ethType: string, format: DataFormat) => unknown;
|
|
12
|
+
/**
|
|
13
|
+
* Converts the data to the specified format
|
|
14
|
+
* @param data - data to convert
|
|
15
|
+
* @param schema - The JSON schema that describes the structure of the data
|
|
16
|
+
* @param dataPath - A string array that specifies the path to the data within the JSON schema
|
|
17
|
+
* @param format - The format to be converted to
|
|
18
|
+
* @param oneOfPath - An optional array of two-element tuples that specifies the "oneOf" option to choose, if the schema has oneOf and the data path can match multiple subschemas
|
|
19
|
+
* @returns - The data converted to the specified format
|
|
20
|
+
*/
|
|
21
|
+
export declare const convert: (data: Record<string, unknown> | unknown[] | unknown, schema: JsonSchema, dataPath: string[], format: DataFormat, oneOfPath?: [string, number][]) => unknown;
|
|
22
|
+
/**
|
|
23
|
+
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted
|
|
24
|
+
* according to the provided return format.
|
|
25
|
+
*
|
|
26
|
+
* @param schema - how to interpret the data
|
|
27
|
+
* @param data - data to be formatted
|
|
28
|
+
* @param returnFormat - how to format the data
|
|
29
|
+
* @returns - formatted data
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
*
|
|
33
|
+
* ```js
|
|
34
|
+
* import { FMT_NUMBER, utils } from "web3";
|
|
35
|
+
*
|
|
36
|
+
* console.log(
|
|
37
|
+
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }),
|
|
38
|
+
* );
|
|
39
|
+
* // 0xdd
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
export declare const format: <DataType extends unknown, ReturnType_1 extends DataFormat>(schema: ValidationSchemaInput | JsonSchema, data: DataType, returnFormat?: ReturnType_1) => FormatType<DataType, ReturnType_1>;
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.format = exports.convert = exports.convertScalarValue = exports.isDataFormat = void 0;
|
|
4
|
+
/*
|
|
5
|
+
This file is part of web3.js.
|
|
6
|
+
|
|
7
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
8
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
9
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
(at your option) any later version.
|
|
11
|
+
|
|
12
|
+
web3.js is distributed in the hope that it will be useful,
|
|
13
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
GNU Lesser General Public License for more details.
|
|
16
|
+
|
|
17
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
18
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
*/
|
|
20
|
+
const web3_errors_1 = require("web3-errors");
|
|
21
|
+
const web3_types_1 = require("web3-types");
|
|
22
|
+
const web3_validator_1 = require("web3-validator");
|
|
23
|
+
const converters_js_1 = require("./converters.js");
|
|
24
|
+
const objects_js_1 = require("./objects.js");
|
|
25
|
+
const string_manipulation_js_1 = require("./string_manipulation.js");
|
|
26
|
+
const uint8array_js_1 = require("./uint8array.js");
|
|
27
|
+
const { parseBaseType } = web3_validator_1.utils;
|
|
28
|
+
const isDataFormat = (dataFormat) => typeof dataFormat === 'object' &&
|
|
29
|
+
!(0, web3_validator_1.isNullish)(dataFormat) &&
|
|
30
|
+
'number' in dataFormat &&
|
|
31
|
+
'bytes' in dataFormat;
|
|
32
|
+
exports.isDataFormat = isDataFormat;
|
|
33
|
+
/**
|
|
34
|
+
* Finds the schema that corresponds to a specific data path within a larger JSON schema.
|
|
35
|
+
* It works by iterating over the dataPath array and traversing the JSON schema one step at a time until it reaches the end of the path.
|
|
36
|
+
*
|
|
37
|
+
* @param schema - represents a JSON schema, which is an object that describes the structure of JSON data
|
|
38
|
+
* @param dataPath - represents an array of strings that specifies the path to the data within the JSON schema
|
|
39
|
+
* @param oneOfPath - represents an optional array of two-element tuples that specifies the "oneOf" option to choose, if the schema has oneOf and the data path can match multiple subschemas
|
|
40
|
+
* @returns the JSON schema that matches the data path
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
const findSchemaByDataPath = (schema, dataPath, oneOfPath = []) => {
|
|
44
|
+
let result = Object.assign({}, schema);
|
|
45
|
+
let previousDataPath;
|
|
46
|
+
for (const dataPart of dataPath) {
|
|
47
|
+
if (result.oneOf && previousDataPath) {
|
|
48
|
+
const currentDataPath = previousDataPath;
|
|
49
|
+
const path = oneOfPath.find(([key]) => key === currentDataPath);
|
|
50
|
+
if (path && path[0] === previousDataPath) {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
52
|
+
result = result.oneOf[path[1]];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (!result.properties && !result.items) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
if (result.properties) {
|
|
59
|
+
result = result.properties[dataPart];
|
|
60
|
+
}
|
|
61
|
+
else if (result.items && result.items.properties) {
|
|
62
|
+
const node = result.items.properties;
|
|
63
|
+
result = node[dataPart];
|
|
64
|
+
}
|
|
65
|
+
else if (result.items && (0, web3_validator_1.isObject)(result.items)) {
|
|
66
|
+
result = result.items;
|
|
67
|
+
}
|
|
68
|
+
else if (result.items && Array.isArray(result.items)) {
|
|
69
|
+
result = result.items[parseInt(dataPart, 10)];
|
|
70
|
+
}
|
|
71
|
+
if (result && dataPart)
|
|
72
|
+
previousDataPath = dataPart;
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Converts a value depending on the format
|
|
78
|
+
* @param value - value to convert
|
|
79
|
+
* @param ethType - The type of the value to be parsed
|
|
80
|
+
* @param format - The format to be converted to
|
|
81
|
+
* @returns - The value converted to the specified format
|
|
82
|
+
*/
|
|
83
|
+
const convertScalarValue = (value, ethType, format) => {
|
|
84
|
+
try {
|
|
85
|
+
const { baseType, baseTypeSize } = parseBaseType(ethType);
|
|
86
|
+
if (baseType === 'int' || baseType === 'uint') {
|
|
87
|
+
switch (format.number) {
|
|
88
|
+
case web3_types_1.FMT_NUMBER.NUMBER:
|
|
89
|
+
return Number((0, converters_js_1.toBigInt)(value));
|
|
90
|
+
case web3_types_1.FMT_NUMBER.HEX:
|
|
91
|
+
return (0, converters_js_1.numberToHex)((0, converters_js_1.toBigInt)(value));
|
|
92
|
+
case web3_types_1.FMT_NUMBER.STR:
|
|
93
|
+
return (0, converters_js_1.toBigInt)(value).toString();
|
|
94
|
+
case web3_types_1.FMT_NUMBER.BIGINT:
|
|
95
|
+
return (0, converters_js_1.toBigInt)(value);
|
|
96
|
+
default:
|
|
97
|
+
throw new web3_errors_1.FormatterError(`Invalid format: ${String(format.number)}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (baseType === 'bytes') {
|
|
101
|
+
let paddedValue;
|
|
102
|
+
if (baseTypeSize) {
|
|
103
|
+
if (typeof value === 'string')
|
|
104
|
+
paddedValue = (0, string_manipulation_js_1.padLeft)(value, baseTypeSize * 2);
|
|
105
|
+
else if ((0, uint8array_js_1.isUint8Array)(value)) {
|
|
106
|
+
paddedValue = (0, uint8array_js_1.uint8ArrayConcat)(new Uint8Array(baseTypeSize - value.length), value);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
paddedValue = value;
|
|
111
|
+
}
|
|
112
|
+
switch (format.bytes) {
|
|
113
|
+
case web3_types_1.FMT_BYTES.HEX:
|
|
114
|
+
return (0, converters_js_1.bytesToHex)((0, converters_js_1.bytesToUint8Array)(paddedValue));
|
|
115
|
+
case web3_types_1.FMT_BYTES.UINT8ARRAY:
|
|
116
|
+
return (0, converters_js_1.bytesToUint8Array)(paddedValue);
|
|
117
|
+
default:
|
|
118
|
+
throw new web3_errors_1.FormatterError(`Invalid format: ${String(format.bytes)}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (baseType === 'string') {
|
|
122
|
+
return String(value);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
// If someone didn't use `eth` keyword we can return original value
|
|
127
|
+
// as the scope of this code is formatting not validation
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
return value;
|
|
131
|
+
};
|
|
132
|
+
exports.convertScalarValue = convertScalarValue;
|
|
133
|
+
const convertArray = ({ value, schemaProp, schema, object, key, dataPath, format, oneOfPath = [], }) => {
|
|
134
|
+
var _a, _b;
|
|
135
|
+
// If value is an array
|
|
136
|
+
if (Array.isArray(value)) {
|
|
137
|
+
let _schemaProp = schemaProp;
|
|
138
|
+
// TODO This is a naive approach to solving the issue of
|
|
139
|
+
// a schema using oneOf. This chunk of code was intended to handle
|
|
140
|
+
// BlockSchema.transactions
|
|
141
|
+
// TODO BlockSchema.transactions are not being formatted
|
|
142
|
+
if ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined) {
|
|
143
|
+
// The following code is basically saying:
|
|
144
|
+
// if the schema specifies oneOf, then we are to loop
|
|
145
|
+
// over each possible schema and check if they type of the schema
|
|
146
|
+
// matches the type of value[0], and if so we use the oneOfSchemaProp
|
|
147
|
+
// as the schema for formatting
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
149
|
+
schemaProp.oneOf.forEach((oneOfSchemaProp, index) => {
|
|
150
|
+
var _a, _b;
|
|
151
|
+
if (!Array.isArray(schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.items) &&
|
|
152
|
+
((typeof value[0] === 'object' &&
|
|
153
|
+
((_a = oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.items) === null || _a === void 0 ? void 0 : _a.type) === 'object') ||
|
|
154
|
+
(typeof value[0] === 'string' &&
|
|
155
|
+
((_b = oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.items) === null || _b === void 0 ? void 0 : _b.type) !== 'object'))) {
|
|
156
|
+
_schemaProp = oneOfSchemaProp;
|
|
157
|
+
oneOfPath.push([key, index]);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if ((0, web3_validator_1.isNullish)(_schemaProp === null || _schemaProp === void 0 ? void 0 : _schemaProp.items)) {
|
|
162
|
+
// Can not find schema for array item, delete that item
|
|
163
|
+
// eslint-disable-next-line no-param-reassign
|
|
164
|
+
delete object[key];
|
|
165
|
+
dataPath.pop();
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
// If schema for array items is a single type
|
|
169
|
+
if ((0, web3_validator_1.isObject)(_schemaProp.items) && !(0, web3_validator_1.isNullish)(_schemaProp.items.format)) {
|
|
170
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
171
|
+
// eslint-disable-next-line no-param-reassign
|
|
172
|
+
object[key][i] = (0, exports.convertScalarValue)(value[i],
|
|
173
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
174
|
+
(_a = _schemaProp === null || _schemaProp === void 0 ? void 0 : _schemaProp.items) === null || _a === void 0 ? void 0 : _a.format, format);
|
|
175
|
+
}
|
|
176
|
+
dataPath.pop();
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
// If schema for array items is an object
|
|
180
|
+
if (!Array.isArray(_schemaProp === null || _schemaProp === void 0 ? void 0 : _schemaProp.items) && ((_b = _schemaProp === null || _schemaProp === void 0 ? void 0 : _schemaProp.items) === null || _b === void 0 ? void 0 : _b.type) === 'object') {
|
|
181
|
+
for (const arrObject of value) {
|
|
182
|
+
// eslint-disable-next-line no-use-before-define
|
|
183
|
+
(0, exports.convert)(arrObject, schema, dataPath, format, oneOfPath);
|
|
184
|
+
}
|
|
185
|
+
dataPath.pop();
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
// If schema for array is a tuple
|
|
189
|
+
if (Array.isArray(_schemaProp === null || _schemaProp === void 0 ? void 0 : _schemaProp.items)) {
|
|
190
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
191
|
+
// eslint-disable-next-line no-param-reassign
|
|
192
|
+
object[key][i] = (0, exports.convertScalarValue)(value[i], _schemaProp.items[i].format, format);
|
|
193
|
+
}
|
|
194
|
+
dataPath.pop();
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return false;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Converts the data to the specified format
|
|
202
|
+
* @param data - data to convert
|
|
203
|
+
* @param schema - The JSON schema that describes the structure of the data
|
|
204
|
+
* @param dataPath - A string array that specifies the path to the data within the JSON schema
|
|
205
|
+
* @param format - The format to be converted to
|
|
206
|
+
* @param oneOfPath - An optional array of two-element tuples that specifies the "oneOf" option to choose, if the schema has oneOf and the data path can match multiple subschemas
|
|
207
|
+
* @returns - The data converted to the specified format
|
|
208
|
+
*/
|
|
209
|
+
const convert = (data, schema, dataPath, format, oneOfPath = []) => {
|
|
210
|
+
var _a;
|
|
211
|
+
// If it's a scalar value
|
|
212
|
+
if (!(0, web3_validator_1.isObject)(data) && !Array.isArray(data)) {
|
|
213
|
+
return (0, exports.convertScalarValue)(data, schema === null || schema === void 0 ? void 0 : schema.format, format);
|
|
214
|
+
}
|
|
215
|
+
const object = data;
|
|
216
|
+
// case when schema is array and `items` is object
|
|
217
|
+
if (Array.isArray(object) &&
|
|
218
|
+
(schema === null || schema === void 0 ? void 0 : schema.type) === 'array' &&
|
|
219
|
+
((_a = schema === null || schema === void 0 ? void 0 : schema.items) === null || _a === void 0 ? void 0 : _a.type) === 'object') {
|
|
220
|
+
convertArray({
|
|
221
|
+
value: object,
|
|
222
|
+
schemaProp: schema,
|
|
223
|
+
schema,
|
|
224
|
+
object,
|
|
225
|
+
key: '',
|
|
226
|
+
dataPath,
|
|
227
|
+
format,
|
|
228
|
+
oneOfPath,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
for (const [key, value] of Object.entries(object)) {
|
|
233
|
+
dataPath.push(key);
|
|
234
|
+
let schemaProp = findSchemaByDataPath(schema, dataPath, oneOfPath);
|
|
235
|
+
// If value is a scaler value
|
|
236
|
+
if ((0, web3_validator_1.isNullish)(schemaProp)) {
|
|
237
|
+
delete object[key];
|
|
238
|
+
dataPath.pop();
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
// If value is an object, recurse into it
|
|
242
|
+
if ((0, web3_validator_1.isObject)(value)) {
|
|
243
|
+
(0, exports.convert)(value, schema, dataPath, format, oneOfPath);
|
|
244
|
+
dataPath.pop();
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
// If value is an array
|
|
248
|
+
if (convertArray({
|
|
249
|
+
value,
|
|
250
|
+
schemaProp,
|
|
251
|
+
schema,
|
|
252
|
+
object,
|
|
253
|
+
key,
|
|
254
|
+
dataPath,
|
|
255
|
+
format,
|
|
256
|
+
oneOfPath,
|
|
257
|
+
})) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
// The following code is basically saying:
|
|
261
|
+
// if the schema specifies oneOf, then we are to loop
|
|
262
|
+
// over each possible schema and check if they type of the schema specifies format
|
|
263
|
+
// and if so we use the oneOfSchemaProp as the schema for formatting
|
|
264
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
265
|
+
if ((schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.format) === undefined && (schemaProp === null || schemaProp === void 0 ? void 0 : schemaProp.oneOf) !== undefined) {
|
|
266
|
+
for (const [_index, oneOfSchemaProp] of schemaProp.oneOf.entries()) {
|
|
267
|
+
if ((oneOfSchemaProp === null || oneOfSchemaProp === void 0 ? void 0 : oneOfSchemaProp.format) !== undefined) {
|
|
268
|
+
schemaProp = oneOfSchemaProp;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
object[key] = (0, exports.convertScalarValue)(value, schemaProp.format, format);
|
|
274
|
+
dataPath.pop();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return object;
|
|
278
|
+
};
|
|
279
|
+
exports.convert = convert;
|
|
280
|
+
/**
|
|
281
|
+
* Given data that can be interpreted according to the provided schema, returns equivalent data that has been formatted
|
|
282
|
+
* according to the provided return format.
|
|
283
|
+
*
|
|
284
|
+
* @param schema - how to interpret the data
|
|
285
|
+
* @param data - data to be formatted
|
|
286
|
+
* @param returnFormat - how to format the data
|
|
287
|
+
* @returns - formatted data
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
*
|
|
291
|
+
* ```js
|
|
292
|
+
* import { FMT_NUMBER, utils } from "web3";
|
|
293
|
+
*
|
|
294
|
+
* console.log(
|
|
295
|
+
* utils.format({ format: "uint" }, "221", { number: FMT_NUMBER.HEX }),
|
|
296
|
+
* );
|
|
297
|
+
* // 0xdd
|
|
298
|
+
* ```
|
|
299
|
+
*
|
|
300
|
+
*/
|
|
301
|
+
const format = (schema, data, returnFormat = web3_types_1.DEFAULT_RETURN_FORMAT) => {
|
|
302
|
+
let dataToParse;
|
|
303
|
+
if ((0, web3_validator_1.isObject)(data)) {
|
|
304
|
+
dataToParse = (0, objects_js_1.mergeDeep)({}, data);
|
|
305
|
+
}
|
|
306
|
+
else if (Array.isArray(data)) {
|
|
307
|
+
dataToParse = [...data];
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
dataToParse = data;
|
|
311
|
+
}
|
|
312
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
313
|
+
const jsonSchema = (0, web3_validator_1.isObject)(schema) ? schema : web3_validator_1.utils.ethAbiToJsonSchema(schema);
|
|
314
|
+
if (!jsonSchema.properties && !jsonSchema.items && !jsonSchema.format) {
|
|
315
|
+
throw new web3_errors_1.FormatterError('Invalid json schema for formatting');
|
|
316
|
+
}
|
|
317
|
+
return (0, exports.convert)(dataToParse, jsonSchema, [], returnFormat);
|
|
318
|
+
};
|
|
319
|
+
exports.format = format;
|
|
320
|
+
//# sourceMappingURL=formatter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"formatter.js","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;EAeE;AACF,6CAA6C;AAC7C,2CAOoB;AACpB,mDAA+F;AAC/F,mDAAuF;AACvF,6CAAyC;AACzC,qEAAmD;AACnD,mDAAiE;AAEjE,MAAM,EAAE,aAAa,EAAE,GAAG,sBAAK,CAAC;AAEzB,MAAM,YAAY,GAAG,CAAC,UAAmB,EAA4B,EAAE,CAC7E,OAAO,UAAU,KAAK,QAAQ;IAC9B,CAAC,IAAA,0BAAS,EAAC,UAAU,CAAC;IACtB,QAAQ,IAAI,UAAU;IACtB,OAAO,IAAI,UAAU,CAAC;AAJV,QAAA,YAAY,gBAIF;AAEvB;;;;;;;;;GASG;AACH,MAAM,oBAAoB,GAAG,CAC5B,MAAkB,EAClB,QAAkB,EAClB,YAAgC,EAAE,EACT,EAAE;IAC3B,IAAI,MAAM,GAAe,kBAAK,MAAM,CAAgB,CAAC;IACrD,IAAI,gBAAoC,CAAC;IAEzC,KAAK,MAAM,QAAQ,IAAI,QAAQ,EAAE;QAChC,IAAI,MAAM,CAAC,KAAK,IAAI,gBAAgB,EAAE;YACrC,MAAM,eAAe,GAAG,gBAAgB,CAAC;YACzC,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,GAAG,KAAK,eAAe,CAAC,CAAC;YAChE,IAAI,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,gBAAgB,EAAE;gBACzC,+GAA+G;gBAC/G,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;aAC/B;SACD;QACD,IAAI,CAAC,MA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Numbers, Sha3Input, TypedObject, TypedObjectAbbreviated } from 'web3-types';
|
|
2
|
+
/**
|
|
3
|
+
* A wrapper for ethereum-cryptography/keccak256 to allow hashing a `string` and a `bigint` in addition to `UInt8Array`
|
|
4
|
+
* @param data - the input to hash
|
|
5
|
+
* @returns - the Keccak-256 hash of the input
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* console.log(web3.utils.keccak256Wrapper('web3.js'));
|
|
10
|
+
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a
|
|
11
|
+
*
|
|
12
|
+
* console.log(web3.utils.keccak256Wrapper(1));
|
|
13
|
+
* > 0xc89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6
|
|
14
|
+
*
|
|
15
|
+
* console.log(web3.utils.keccak256Wrapper(0xaf12fd));
|
|
16
|
+
* > 0x358640fd4719fa923525d74ab5ae80a594301aba5543e3492b052bf4598b794c
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export declare const keccak256Wrapper: (data: Bytes | Numbers | string | ReadonlyArray<number>) => string;
|
|
20
|
+
export { keccak256Wrapper as keccak256 };
|
|
21
|
+
/**
|
|
22
|
+
* computes the Keccak-256 hash of the input and returns a hexstring
|
|
23
|
+
* @param data - the input to hash
|
|
24
|
+
* @returns - the Keccak-256 hash of the input
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* console.log(web3.utils.sha3('web3.js'));
|
|
29
|
+
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a
|
|
30
|
+
*
|
|
31
|
+
* console.log(web3.utils.sha3(''));
|
|
32
|
+
* > undefined
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const sha3: (data: Bytes) => string | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Will calculate the sha3 of the input but does return the hash value instead of null if for example a empty string is passed.
|
|
38
|
+
* @param data - the input to hash
|
|
39
|
+
* @returns - the Keccak-256 hash of the input
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* conosle.log(web3.utils.sha3Raw('web3.js'));
|
|
44
|
+
* > 0x63667efb1961039c9bb0d6ea7a5abdd223a3aca7daa5044ad894226e1f83919a
|
|
45
|
+
*
|
|
46
|
+
* console.log(web3.utils.sha3Raw(''));
|
|
47
|
+
* > 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare const sha3Raw: (data: Bytes) => string;
|
|
51
|
+
/**
|
|
52
|
+
* returns a string of the tightly packed value given based on the type
|
|
53
|
+
* @param arg - the input to return the tightly packed value
|
|
54
|
+
* @returns - the tightly packed value
|
|
55
|
+
*/
|
|
56
|
+
export declare const processSolidityEncodePackedArgs: (arg: Sha3Input) => string;
|
|
57
|
+
/**
|
|
58
|
+
* Encode packed arguments to a hexstring
|
|
59
|
+
*/
|
|
60
|
+
export declare const encodePacked: (...values: Sha3Input[]) => string;
|
|
61
|
+
/**
|
|
62
|
+
* Will tightly pack values given in the same way solidity would then hash.
|
|
63
|
+
* returns a hash string, or null if input is empty
|
|
64
|
+
* @param values - the input to return the tightly packed values
|
|
65
|
+
* @returns - the keccack246 of the tightly packed values
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* console.log(web3.utils.soliditySha3({ type: "string", value: "31323334" }));
|
|
70
|
+
* > 0xf15f8da2ad27e486d632dc37d24912f634398918d6f9913a0a0ff84e388be62b
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare const soliditySha3: (...values: Sha3Input[]) => string | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Will tightly pack values given in the same way solidity would then hash.
|
|
76
|
+
* returns a hash string, if input is empty will return `0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470`
|
|
77
|
+
* @param values - the input to return the tightly packed values
|
|
78
|
+
* @returns - the keccack246 of the tightly packed values
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* console.log(web3.utils.soliditySha3Raw({ type: "string", value: "helloworld" }))
|
|
83
|
+
* > 0xfa26db7ca85ead399216e7c6316bc50ed24393c3122b582735e7f3b0f91b93f0
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare const soliditySha3Raw: (...values: TypedObject[] | TypedObjectAbbreviated[]) => string;
|
|
87
|
+
/**
|
|
88
|
+
* Get slot number for storage long string in contract. Basically for getStorage method
|
|
89
|
+
* returns slotNumber where will data placed
|
|
90
|
+
* @param mainSlotNumber - the slot number where will be stored hash of long string
|
|
91
|
+
* @returns - the slot number where will be stored long string
|
|
92
|
+
*/
|
|
93
|
+
export declare const getStorageSlotNumForLongString: (mainSlotNumber: number | string) => string | undefined;
|