Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit c5072a3

Browse files
author
Alex
authored
update formatter (#6068)
* update formatter * update convertscalar method * update schema * update schema * update padding format and tests * remove arraybuffer from byte types * remove else conditional * update formatter unit tests
1 parent 0e5c06f commit c5072a3

File tree

9 files changed

+45
-20
lines changed

9 files changed

+45
-20
lines changed

packages/web3-eth/src/rpc_method_wrappers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ export async function getBlock<ReturnFormat extends DataFormat>(
257257
hydrated,
258258
);
259259
}
260-
261260
return format(blockSchema, response as unknown as Block, returnFormat);
262261
}
263262

packages/web3-eth/src/schemas.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ export const transactionSchema = {
158158
format: 'uint',
159159
},
160160
r: {
161-
format: 'bytes',
161+
format: 'bytes32',
162162
},
163163
s: {
164-
format: 'bytes',
164+
format: 'bytes32',
165165
},
166166
},
167167
};
@@ -228,10 +228,10 @@ export const transactionInfoSchema = {
228228
format: 'uint',
229229
},
230230
r: {
231-
format: 'bytes',
231+
format: 'bytes32',
232232
},
233233
s: {
234-
format: 'bytes',
234+
format: 'bytes32',
235235
},
236236
},
237237
};

packages/web3-eth/test/unit/rpc_method_wrappers/get_block.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ describe('getBlock', () => {
5555
} else {
5656
inputBlockFormatted = format({ format: 'uint' }, inputBlock, ETH_DATA_FORMAT);
5757
}
58-
5958
await getBlock(web3Context, ...inputParameters, DEFAULT_RETURN_FORMAT);
6059
expect(
6160
inputBlockIsBytes ? ethRpcMethods.getBlockByHash : ethRpcMethods.getBlockByNumber,

packages/web3-types/src/primitives_types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
export type HexString = string;
19-
export type Bytes = Uint8Array | ArrayBuffer | HexString;
19+
export type Bytes = Uint8Array | HexString;
2020
export type Numbers = number | bigint | string | HexString;
2121

2222
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment

packages/web3-utils/src/formatter.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { Bytes, DataFormat, FMT_BYTES, FMT_NUMBER, FormatType } from 'web3-types
1919
import { isNullish, isObject, JsonSchema, utils, ValidationSchemaInput } from 'web3-validator';
2020
import { bytesToUint8Array, bytesToHex, numberToHex, toBigInt } from './converters';
2121
import { mergeDeep } from './objects';
22+
import { padLeft } from './string_manipulation';
23+
import { uint8ArrayConcat } from './uint8array';
2224

2325
const { parseBaseType } = utils;
2426

@@ -34,7 +36,7 @@ export const isDataFormat = (dataFormat: unknown): dataFormat is DataFormat =>
3436
*
3537
* @param schema - represents a JSON schema, which is an object that describes the structure of JSON data
3638
* @param dataPath - represents an array of strings that specifies the path to the data within the JSON schema
37-
* @param oneOfPath - epresents 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
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
3840
* @returns the JSON schema that matches the data path
3941
*
4042
*/
@@ -91,8 +93,7 @@ const findSchemaByDataPath = (
9193
*/
9294
export const convertScalarValue = (value: unknown, ethType: string, format: DataFormat) => {
9395
try {
94-
const { baseType } = parseBaseType(ethType);
95-
96+
const { baseType, baseTypeSize } = parseBaseType(ethType);
9697
if (baseType === 'int' || baseType === 'uint') {
9798
switch (format.number) {
9899
case FMT_NUMBER.NUMBER:
@@ -107,13 +108,24 @@ export const convertScalarValue = (value: unknown, ethType: string, format: Data
107108
throw new FormatterError(`Invalid format: ${String(format.number)}`);
108109
}
109110
}
110-
111111
if (baseType === 'bytes') {
112+
let paddedValue;
113+
if (baseTypeSize) {
114+
if (typeof value === 'string') paddedValue = padLeft(value, baseTypeSize * 2);
115+
else if (value instanceof Uint8Array) {
116+
paddedValue = uint8ArrayConcat(
117+
new Uint8Array(baseTypeSize - value.length),
118+
value,
119+
);
120+
}
121+
} else {
122+
paddedValue = value;
123+
}
112124
switch (format.bytes) {
113125
case FMT_BYTES.HEX:
114-
return bytesToHex(bytesToUint8Array(value as Bytes));
126+
return bytesToHex(bytesToUint8Array(paddedValue as Bytes));
115127
case FMT_BYTES.UINT8ARRAY:
116-
return bytesToUint8Array(value as Bytes);
128+
return bytesToUint8Array(paddedValue as Bytes);
117129
default:
118130
throw new FormatterError(`Invalid format: ${String(format.bytes)}`);
119131
}

packages/web3-utils/src/hash.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ export const sha3 = (data: Bytes): string | undefined => {
7171
} else {
7272
updatedData = utf8ToBytes(data);
7373
}
74-
} else if (data instanceof ArrayBuffer || Array.isArray(data)) {
75-
updatedData = new Uint8Array(data);
7674
} else {
7775
updatedData = data;
7876
}

packages/web3-utils/test/fixtures/formatter.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,35 @@ export const isDataFormatValid: [any, boolean][] = [
2424
];
2525

2626
export const convertScalarValueValid: [[any, any, any], any][] = [
27+
[[new Uint8Array(hexToBytes('FF')), 'bytes', { bytes: FMT_BYTES.HEX }], '0xff'],
28+
[
29+
[
30+
'0xe84375b25f38de0e68f7f4884b7342e0814747143c790f48088d22e802cf7a3',
31+
'bytes32',
32+
{ bytes: FMT_BYTES.HEX },
33+
],
34+
'0x0e84375b25f38de0e68f7f4884b7342e0814747143c790f48088d22e802cf7a3',
35+
],
2736
[[100, 'int', { number: FMT_NUMBER.NUMBER, bytes: FMT_BYTES.HEX }], 100],
37+
[[100, 'int', { number: 'unknown', bytes: FMT_BYTES.HEX }], 100],
2838
[[100, 'uint', { number: FMT_NUMBER.HEX, bytes: FMT_BYTES.HEX }], '0x64'],
2939
[[64, 'uint8', { number: FMT_NUMBER.STR }], '64'],
30-
31-
[[new Uint8Array(hexToBytes('FF')), 'bytes', { bytes: FMT_BYTES.HEX }], '0xff'],
3240
[
3341
[new Uint8Array(hexToBytes('FF')), 'bytes', { bytes: FMT_BYTES.UINT8ARRAY }],
3442
new Uint8Array(new Uint8Array(hexToBytes('FF'))),
3543
],
3644
[
37-
[new Uint8Array(hexToBytes('FF')), 'bytes', { bytes: FMT_BYTES }],
45+
[new Uint8Array(hexToBytes('FF')), 'bytes32', { bytes: FMT_BYTES.HEX }],
46+
'0x00000000000000000000000000000000000000000000000000000000000000ff',
47+
],
48+
[
49+
[new Uint8Array(hexToBytes('FF')), 'unknown', { bytes: FMT_BYTES.HEX }],
3850
new Uint8Array(hexToBytes('FF')),
3951
],
52+
[
53+
[new Uint8Array(hexToBytes('FF')), 'bytes32', { bytes: FMT_BYTES.UINT8ARRAY }],
54+
new Uint8Array(
55+
hexToBytes('0x00000000000000000000000000000000000000000000000000000000000000ff'),
56+
),
57+
],
4058
];

packages/web3-validator/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export type Web3ValidationErrorObject<
3535
data?: unknown;
3636
};
3737

38-
export type ValidInputTypes = ArrayBuffer | Uint8Array | bigint | string | number | boolean;
38+
export type ValidInputTypes = Uint8Array | bigint | string | number | boolean;
3939
export type EthBaseTypes = 'bool' | 'bytes' | 'string' | 'uint' | 'int' | 'address' | 'tuple';
4040
export type EthBaseTypesWithMeta =
4141
| `string${string}`

packages/web3-validator/test/fixtures/validation.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ export const validStringNumbersWithHex: [string, string][] = [
297297
];
298298

299299
export const invalidStringNumbers: ValidInputTypes[] = [
300-
new ArrayBuffer(23255),
301300
new Uint8Array([0x97, 0x98, 0x99]),
302301
new Uint8Array(hexToBytes('abcd')),
303302
];

0 commit comments

Comments
 (0)