viem 0.4.0-main.20230417T195122 → 0.4.0-main.20230419T202802
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/README.md +6 -0
- package/dist/cjs/errors/encoding.js +13 -1
- package/dist/cjs/errors/encoding.js.map +1 -1
- package/dist/cjs/errors/index.js +3 -2
- package/dist/cjs/errors/index.js.map +1 -1
- package/dist/cjs/errors/version.js +1 -1
- package/dist/cjs/utils/encoding/fromBytes.js +29 -12
- package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/fromHex.js +32 -10
- package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
- package/dist/cjs/utils/encoding/toBytes.js +31 -15
- package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
- package/dist/cjs/utils/encoding/toHex.js +31 -20
- package/dist/cjs/utils/encoding/toHex.js.map +1 -1
- package/dist/esm/errors/encoding.js +11 -0
- package/dist/esm/errors/encoding.js.map +1 -1
- package/dist/esm/errors/index.js +1 -1
- package/dist/esm/errors/index.js.map +1 -1
- package/dist/esm/errors/version.js +1 -1
- package/dist/esm/utils/encoding/fromBytes.js +30 -13
- package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
- package/dist/esm/utils/encoding/fromHex.js +31 -10
- package/dist/esm/utils/encoding/fromHex.js.map +1 -1
- package/dist/esm/utils/encoding/toBytes.js +31 -15
- package/dist/esm/utils/encoding/toBytes.js.map +1 -1
- package/dist/esm/utils/encoding/toHex.js +28 -17
- package/dist/esm/utils/encoding/toHex.js.map +1 -1
- package/dist/types/errors/encoding.d.ts +7 -0
- package/dist/types/errors/encoding.d.ts.map +1 -1
- package/dist/types/errors/index.d.ts +1 -1
- package/dist/types/errors/index.d.ts.map +1 -1
- package/dist/types/errors/version.d.ts +1 -1
- package/dist/types/utils/encoding/fromBytes.d.ts +95 -10
- package/dist/types/utils/encoding/fromBytes.d.ts.map +1 -1
- package/dist/types/utils/encoding/fromHex.d.ts +122 -12
- package/dist/types/utils/encoding/fromHex.d.ts.map +1 -1
- package/dist/types/utils/encoding/toBytes.d.ts +113 -9
- package/dist/types/utils/encoding/toBytes.d.ts.map +1 -1
- package/dist/types/utils/encoding/toHex.d.ts +120 -10
- package/dist/types/utils/encoding/toHex.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/errors/encoding.ts +9 -0
- package/src/errors/index.ts +1 -0
- package/src/errors/version.ts +1 -1
- package/src/utils/encoding/fromBytes.ts +147 -18
- package/src/utils/encoding/fromHex.ts +162 -19
- package/src/utils/encoding/toBytes.ts +148 -18
- package/src/utils/encoding/toHex.ts +151 -26
@@ -1,21 +1,125 @@
|
|
1
1
|
import type { ByteArray, Hex } from '../../types/index.js';
|
2
2
|
import type { NumberToHexOpts } from './toHex.js';
|
3
|
-
|
4
|
-
|
3
|
+
export type ToBytesParameters = {
|
4
|
+
/** Size of the output bytes. */
|
5
|
+
size?: number;
|
6
|
+
};
|
5
7
|
/**
|
6
|
-
*
|
8
|
+
* Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.
|
9
|
+
*
|
10
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html
|
11
|
+
* - Example: https://viem.sh/docs/utilities/toBytes.html#usage
|
12
|
+
*
|
13
|
+
* @param value Value to encode.
|
14
|
+
* @param opts Options.
|
15
|
+
* @returns Byte array value.
|
16
|
+
*
|
17
|
+
* @example
|
18
|
+
* import { toBytes } from 'viem'
|
19
|
+
* const data = toBytes('Hello world')
|
20
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
21
|
+
*
|
22
|
+
* @example
|
23
|
+
* import { toBytes } from 'viem'
|
24
|
+
* const data = toBytes(420)
|
25
|
+
* // Uint8Array([1, 164])
|
26
|
+
*
|
27
|
+
* @example
|
28
|
+
* import { toBytes } from 'viem'
|
29
|
+
* const data = toBytes(420, { size: 4 })
|
30
|
+
* // Uint8Array([0, 0, 1, 164])
|
7
31
|
*/
|
8
|
-
export declare function
|
32
|
+
export declare function toBytes(value: string | bigint | number | boolean | Hex, opts?: ToBytesParameters): ByteArray;
|
33
|
+
export type BoolToHexOpts = {
|
34
|
+
/** Size of the output bytes. */
|
35
|
+
size?: number;
|
36
|
+
};
|
9
37
|
/**
|
10
|
-
*
|
38
|
+
* Encodes a boolean into a byte array.
|
39
|
+
*
|
40
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#booltobytes
|
41
|
+
*
|
42
|
+
* @param value Boolean value to encode.
|
43
|
+
* @param opts Options.
|
44
|
+
* @returns Byte array value.
|
45
|
+
*
|
46
|
+
* @example
|
47
|
+
* import { boolToBytes } from 'viem'
|
48
|
+
* const data = boolToBytes(true)
|
49
|
+
* // Uint8Array([1])
|
50
|
+
*
|
51
|
+
* @example
|
52
|
+
* import { boolToBytes } from 'viem'
|
53
|
+
* const data = boolToBytes(true, { size: 32 })
|
54
|
+
* // Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
|
11
55
|
*/
|
12
|
-
export declare function
|
56
|
+
export declare function boolToBytes(value: boolean, opts?: BoolToHexOpts): Uint8Array;
|
57
|
+
export type HexToBytesOpts = {
|
58
|
+
/** Size of the output bytes. */
|
59
|
+
size?: number;
|
60
|
+
};
|
13
61
|
/**
|
14
|
-
*
|
62
|
+
* Encodes a hex string into a byte array.
|
63
|
+
*
|
64
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#hextobytes
|
65
|
+
*
|
66
|
+
* @param hex Hex string to encode.
|
67
|
+
* @param opts Options.
|
68
|
+
* @returns Byte array value.
|
69
|
+
*
|
70
|
+
* @example
|
71
|
+
* import { hexToBytes } from 'viem'
|
72
|
+
* const data = hexToBytes('0x48656c6c6f20776f726c6421')
|
73
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
74
|
+
*
|
75
|
+
* @example
|
76
|
+
* import { hexToBytes } from 'viem'
|
77
|
+
* const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
|
78
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
79
|
+
*/
|
80
|
+
export declare function hexToBytes(hex_: Hex, opts?: HexToBytesOpts): ByteArray;
|
81
|
+
/**
|
82
|
+
* Encodes a number into a byte array.
|
83
|
+
*
|
84
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#numbertobytes
|
85
|
+
*
|
86
|
+
* @param value Number to encode.
|
87
|
+
* @param opts Options.
|
88
|
+
* @returns Byte array value.
|
89
|
+
*
|
90
|
+
* @example
|
91
|
+
* import { numberToBytes } from 'viem'
|
92
|
+
* const data = numberToBytes(420)
|
93
|
+
* // Uint8Array([1, 164])
|
94
|
+
*
|
95
|
+
* @example
|
96
|
+
* import { numberToBytes } from 'viem'
|
97
|
+
* const data = numberToBytes(420, { size: 4 })
|
98
|
+
* // Uint8Array([0, 0, 1, 164])
|
15
99
|
*/
|
16
100
|
export declare function numberToBytes(value: bigint | number, opts?: NumberToHexOpts): Uint8Array;
|
101
|
+
export type StringToBytesOpts = {
|
102
|
+
/** Size of the output bytes. */
|
103
|
+
size?: number;
|
104
|
+
};
|
17
105
|
/**
|
18
|
-
*
|
106
|
+
* Encodes a UTF-8 string into a byte array.
|
107
|
+
*
|
108
|
+
* - Docs: https://viem.sh/docs/utilities/toBytes.html#stringtobytes
|
109
|
+
*
|
110
|
+
* @param value String to encode.
|
111
|
+
* @param opts Options.
|
112
|
+
* @returns Byte array value.
|
113
|
+
*
|
114
|
+
* @example
|
115
|
+
* import { stringToBytes } from 'viem'
|
116
|
+
* const data = stringToBytes('Hello world!')
|
117
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33])
|
118
|
+
*
|
119
|
+
* @example
|
120
|
+
* import { stringToBytes } from 'viem'
|
121
|
+
* const data = stringToBytes('Hello world!', { size: 32 })
|
122
|
+
* // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
|
19
123
|
*/
|
20
|
-
export declare function stringToBytes(value: string): ByteArray;
|
124
|
+
export declare function stringToBytes(value: string, opts?: StringToBytesOpts): ByteArray;
|
21
125
|
//# sourceMappingURL=toBytes.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"toBytes.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toBytes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;
|
1
|
+
{"version":3,"file":"toBytes.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toBytes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAI1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAKjD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,gCAAgC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,GAAG,EAC/C,IAAI,GAAE,iBAAsB,GAC3B,SAAS,CAMX;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,gCAAgC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,aAAkB,cAQnE;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,gCAAgC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,GAAE,cAAmB,GAAG,SAAS,CAsB1E;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,eAAe,cAG3E;AAED,MAAM,MAAM,iBAAiB,GAAG;IAC9B,gCAAgC;IAChC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,iBAAsB,GAC3B,SAAS,CAOX"}
|
@@ -1,29 +1,139 @@
|
|
1
1
|
import type { ByteArray, Hex } from '../../types/index.js';
|
2
|
+
export type ToHexParameters = {
|
3
|
+
/** The size (in bytes) of the output hex value. */
|
4
|
+
size?: number;
|
5
|
+
};
|
2
6
|
/**
|
3
|
-
*
|
7
|
+
* Encodes a string, number, bigint, or ByteArray into a hex string
|
8
|
+
*
|
9
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html
|
10
|
+
* - Example: https://viem.sh/docs/utilities/toHex.html#usage
|
11
|
+
*
|
12
|
+
* @param value Value to encode.
|
13
|
+
* @param opts Options.
|
14
|
+
* @returns Hex value.
|
15
|
+
*
|
16
|
+
* @example
|
17
|
+
* import { toHex } from 'viem'
|
18
|
+
* const data = toHex('Hello world')
|
19
|
+
* // '0x48656c6c6f20776f726c6421'
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* import { toHex } from 'viem'
|
23
|
+
* const data = toHex(420)
|
24
|
+
* // '0x1a4'
|
25
|
+
*
|
26
|
+
* @example
|
27
|
+
* import { toHex } from 'viem'
|
28
|
+
* const data = toHex('Hello world', { size: 32 })
|
29
|
+
* // '0x48656c6c6f20776f726c64210000000000000000000000000000000000000000'
|
4
30
|
*/
|
5
|
-
export declare function
|
31
|
+
export declare function toHex(value: string | number | bigint | boolean | ByteArray, opts?: ToHexParameters): Hex;
|
32
|
+
export type BoolToHexOpts = {
|
33
|
+
/** The size (in bytes) of the output hex value. */
|
34
|
+
size?: number;
|
35
|
+
};
|
6
36
|
/**
|
7
|
-
*
|
37
|
+
* Encodes a boolean into a hex string
|
38
|
+
*
|
39
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#booltohex
|
40
|
+
*
|
41
|
+
* @param value Value to encode.
|
42
|
+
* @param opts Options.
|
43
|
+
* @returns Hex value.
|
44
|
+
*
|
45
|
+
* @example
|
46
|
+
* import { boolToHex } from 'viem'
|
47
|
+
* const data = boolToHex(true)
|
48
|
+
* // '0x1'
|
49
|
+
*
|
50
|
+
* @example
|
51
|
+
* import { boolToHex } from 'viem'
|
52
|
+
* const data = boolToHex(false)
|
53
|
+
* // '0x0'
|
54
|
+
*
|
55
|
+
* @example
|
56
|
+
* import { boolToHex } from 'viem'
|
57
|
+
* const data = boolToHex(true, { size: 32 })
|
58
|
+
* // '0x0000000000000000000000000000000000000000000000000000000000000001'
|
8
59
|
*/
|
9
|
-
export declare function
|
60
|
+
export declare function boolToHex(value: boolean, opts?: BoolToHexOpts): Hex;
|
61
|
+
export type BytesToHexOpts = {
|
62
|
+
/** The size (in bytes) of the output hex value. */
|
63
|
+
size?: number;
|
64
|
+
};
|
10
65
|
/**
|
11
|
-
*
|
66
|
+
* Encodes a bytes array into a hex string
|
67
|
+
*
|
68
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#bytestohex
|
69
|
+
*
|
70
|
+
* @param value Value to encode.
|
71
|
+
* @param opts Options.
|
72
|
+
* @returns Hex value.
|
73
|
+
*
|
74
|
+
* @example
|
75
|
+
* import { bytesToHex } from 'viem'
|
76
|
+
* const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
|
77
|
+
* // '0x48656c6c6f20576f726c6421'
|
78
|
+
*
|
79
|
+
* @example
|
80
|
+
* import { bytesToHex } from 'viem'
|
81
|
+
* const data = bytesToHex(Uint8Array.from([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]), { size: 32 })
|
82
|
+
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
|
12
83
|
*/
|
13
|
-
export declare function
|
84
|
+
export declare function bytesToHex(value: ByteArray, opts?: BytesToHexOpts): Hex;
|
14
85
|
export type NumberToHexOpts = {
|
86
|
+
/** Whether or not the number of a signed representation. */
|
15
87
|
signed?: boolean;
|
88
|
+
/** The size (in bytes) of the output hex value. */
|
16
89
|
size: number;
|
17
90
|
} | {
|
18
91
|
signed?: never;
|
19
|
-
size
|
92
|
+
/** The size (in bytes) of the output hex value. */
|
93
|
+
size?: number;
|
20
94
|
};
|
21
95
|
/**
|
22
|
-
*
|
96
|
+
* Encodes a number or bigint into a hex string
|
97
|
+
*
|
98
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#numbertohex
|
99
|
+
*
|
100
|
+
* @param value Value to encode.
|
101
|
+
* @param opts Options.
|
102
|
+
* @returns Hex value.
|
103
|
+
*
|
104
|
+
* @example
|
105
|
+
* import { numberToHex } from 'viem'
|
106
|
+
* const data = numberToHex(420)
|
107
|
+
* // '0x1a4'
|
108
|
+
*
|
109
|
+
* @example
|
110
|
+
* import { numberToHex } from 'viem'
|
111
|
+
* const data = numberToHex(420, { size: 32 })
|
112
|
+
* // '0x00000000000000000000000000000000000000000000000000000000000001a4'
|
23
113
|
*/
|
24
114
|
export declare function numberToHex(value_: number | bigint, opts?: NumberToHexOpts): Hex;
|
115
|
+
export type StringToHexOpts = {
|
116
|
+
/** The size (in bytes) of the output hex value. */
|
117
|
+
size?: number;
|
118
|
+
};
|
25
119
|
/**
|
26
|
-
*
|
120
|
+
* Encodes a UTF-8 string into a hex string
|
121
|
+
*
|
122
|
+
* - Docs: https://viem.sh/docs/utilities/toHex.html#stringtohex
|
123
|
+
*
|
124
|
+
* @param value Value to encode.
|
125
|
+
* @param opts Options.
|
126
|
+
* @returns Hex value.
|
127
|
+
*
|
128
|
+
* @example
|
129
|
+
* import { stringToHex } from 'viem'
|
130
|
+
* const data = stringToHex('Hello World!')
|
131
|
+
* // '0x48656c6c6f20576f726c6421'
|
132
|
+
*
|
133
|
+
* @example
|
134
|
+
* import { stringToHex } from 'viem'
|
135
|
+
* const data = stringToHex('Hello World!', { size: 32 })
|
136
|
+
* // '0x48656c6c6f20576f726c64210000000000000000000000000000000000000000'
|
27
137
|
*/
|
28
|
-
export declare function stringToHex(value_: string): Hex;
|
138
|
+
export declare function stringToHex(value_: string, opts?: StringToHexOpts): Hex;
|
29
139
|
//# sourceMappingURL=toHex.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"toHex.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toHex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;
|
1
|
+
{"version":3,"file":"toHex.d.ts","sourceRoot":"","sources":["../../../../src/utils/encoding/toHex.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAA;AAQ1D,MAAM,MAAM,eAAe,GAAG;IAC5B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,KAAK,CACnB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,EACrD,IAAI,GAAE,eAAoB,GACzB,GAAG,CAQL;AAED,MAAM,MAAM,aAAa,GAAG;IAC1B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,GAAE,aAAkB,GAAG,GAAG,CAOvE;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,EAAE,IAAI,GAAE,cAAmB,GAAG,GAAG,CAY3E;AAED,MAAM,MAAM,eAAe,GACvB;IACE,4DAA4D;IAC5D,MAAM,CAAC,EAAE,OAAO,CAAA;IAChB,mDAAmD;IACnD,IAAI,EAAE,MAAM,CAAA;CACb,GACD;IACE,MAAM,CAAC,EAAE,KAAK,CAAA;IACd,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAEL;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,IAAI,GAAE,eAAoB,GACzB,GAAG,CAgCL;AAED,MAAM,MAAM,eAAe,GAAG;IAC5B,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAID;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,eAAoB,GAAG,GAAG,CAG3E"}
|
package/package.json
CHANGED
package/src/errors/encoding.ts
CHANGED
@@ -81,3 +81,12 @@ export class OffsetOutOfBoundsError extends BaseError {
|
|
81
81
|
)
|
82
82
|
}
|
83
83
|
}
|
84
|
+
|
85
|
+
export class SizeOverflowError extends BaseError {
|
86
|
+
override name = 'SizeOverflowError'
|
87
|
+
constructor({ givenSize, maxSize }: { givenSize: number; maxSize: number }) {
|
88
|
+
super(
|
89
|
+
`Size cannot exceed ${maxSize} bytes. Given size: ${givenSize} bytes.`,
|
90
|
+
)
|
91
|
+
}
|
92
|
+
}
|
package/src/errors/index.ts
CHANGED
package/src/errors/version.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export const version = '0.4.0-main.
|
1
|
+
export const version = '0.4.0-main.20230419T202802'
|
@@ -1,8 +1,20 @@
|
|
1
1
|
import { InvalidBytesBooleanError } from '../../errors/index.js'
|
2
2
|
import type { ByteArray, Hex } from '../../types/index.js'
|
3
|
-
import {
|
3
|
+
import { trim } from '../data/trim.js'
|
4
|
+
import { assertSize, hexToBigInt, hexToNumber } from './fromHex.js'
|
4
5
|
import { bytesToHex } from './toHex.js'
|
5
6
|
|
7
|
+
export type FromBytesParameters<
|
8
|
+
TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
|
9
|
+
> =
|
10
|
+
| TTo
|
11
|
+
| {
|
12
|
+
/** Size of the bytes. */
|
13
|
+
size?: number
|
14
|
+
/** Type to convert to. */
|
15
|
+
to: TTo
|
16
|
+
}
|
17
|
+
|
6
18
|
type FromBytesReturnType<TTo> = TTo extends 'string'
|
7
19
|
? string
|
8
20
|
: TTo extends 'hex'
|
@@ -16,30 +28,106 @@ type FromBytesReturnType<TTo> = TTo extends 'string'
|
|
16
28
|
: never
|
17
29
|
|
18
30
|
/**
|
19
|
-
*
|
31
|
+
* Decodes a byte array into a UTF-8 string, hex value, number, bigint or boolean.
|
32
|
+
*
|
33
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html
|
34
|
+
* - Example: https://viem.sh/docs/utilities/fromBytes.html#usage
|
35
|
+
*
|
36
|
+
* @param bytes Byte array to decode.
|
37
|
+
* @param toOrOpts Type to convert to or options.
|
38
|
+
* @returns Decoded value.
|
39
|
+
*
|
40
|
+
* @example
|
41
|
+
* import { fromBytes } from 'viem'
|
42
|
+
* const data = fromBytes(new Uint8Array([1, 164]), 'number')
|
43
|
+
* // 420
|
44
|
+
*
|
45
|
+
* @example
|
46
|
+
* import { fromBytes } from 'viem'
|
47
|
+
* const data = fromBytes(
|
48
|
+
* new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
|
49
|
+
* 'string'
|
50
|
+
* )
|
51
|
+
* // 'Hello world'
|
20
52
|
*/
|
21
53
|
export function fromBytes<
|
22
54
|
TTo extends 'string' | 'hex' | 'bigint' | 'number' | 'boolean',
|
23
|
-
>(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
55
|
+
>(
|
56
|
+
bytes: ByteArray,
|
57
|
+
toOrOpts: FromBytesParameters<TTo>,
|
58
|
+
): FromBytesReturnType<TTo> {
|
59
|
+
const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
|
60
|
+
const to = opts.to
|
61
|
+
|
62
|
+
if (to === 'number')
|
63
|
+
return bytesToNumber(bytes, opts) as FromBytesReturnType<TTo>
|
64
|
+
if (to === 'bigint')
|
65
|
+
return bytesToBigint(bytes, opts) as FromBytesReturnType<TTo>
|
66
|
+
if (to === 'boolean')
|
67
|
+
return bytesToBool(bytes, opts) as FromBytesReturnType<TTo>
|
68
|
+
if (to === 'string')
|
69
|
+
return bytesToString(bytes, opts) as FromBytesReturnType<TTo>
|
70
|
+
return bytesToHex(bytes, opts) as FromBytesReturnType<TTo>
|
71
|
+
}
|
72
|
+
|
73
|
+
export type BytesToBigIntOpts = {
|
74
|
+
/** Whether or not the number of a signed representation. */
|
75
|
+
signed?: boolean
|
76
|
+
/** Size of the bytes. */
|
77
|
+
size?: number
|
29
78
|
}
|
30
79
|
|
31
80
|
/**
|
32
|
-
*
|
81
|
+
* Decodes a byte array into a bigint.
|
82
|
+
*
|
83
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobigint
|
84
|
+
*
|
85
|
+
* @param bytes Byte array to decode.
|
86
|
+
* @param opts Options.
|
87
|
+
* @returns BigInt value.
|
88
|
+
*
|
89
|
+
* @example
|
90
|
+
* import { bytesToBigint } from 'viem'
|
91
|
+
* const data = bytesToBigint(new Uint8Array([1, 164]))
|
92
|
+
* // 420n
|
33
93
|
*/
|
34
|
-
export function bytesToBigint(
|
35
|
-
|
94
|
+
export function bytesToBigint(
|
95
|
+
bytes: ByteArray,
|
96
|
+
opts: BytesToBigIntOpts = {},
|
97
|
+
): bigint {
|
98
|
+
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
99
|
+
const hex = bytesToHex(bytes, opts)
|
36
100
|
return hexToBigInt(hex)
|
37
101
|
}
|
38
102
|
|
103
|
+
export type BytesToBoolOpts = {
|
104
|
+
/** Size of the bytes. */
|
105
|
+
size?: number
|
106
|
+
}
|
107
|
+
|
39
108
|
/**
|
40
|
-
*
|
109
|
+
* Decodes a byte array into a boolean.
|
110
|
+
*
|
111
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestobool
|
112
|
+
*
|
113
|
+
* @param bytes Byte array to decode.
|
114
|
+
* @param opts Options.
|
115
|
+
* @returns Boolean value.
|
116
|
+
*
|
117
|
+
* @example
|
118
|
+
* import { bytesToBool } from 'viem'
|
119
|
+
* const data = bytesToBool(new Uint8Array([1]))
|
120
|
+
* // true
|
41
121
|
*/
|
42
|
-
export function bytesToBool(
|
122
|
+
export function bytesToBool(
|
123
|
+
bytes_: ByteArray,
|
124
|
+
opts: BytesToBoolOpts = {},
|
125
|
+
): boolean {
|
126
|
+
let bytes = bytes_
|
127
|
+
if (typeof opts.size !== 'undefined') {
|
128
|
+
assertSize(bytes, { size: opts.size })
|
129
|
+
bytes = trim(bytes)
|
130
|
+
}
|
43
131
|
if (bytes.length > 1 || bytes[0] > 1)
|
44
132
|
throw new InvalidBytesBooleanError(bytes)
|
45
133
|
return Boolean(bytes[0])
|
@@ -47,17 +135,58 @@ export function bytesToBool(bytes: ByteArray): boolean {
|
|
47
135
|
|
48
136
|
export { bytesToHex }
|
49
137
|
|
138
|
+
export type BytesToNumberOpts = BytesToBigIntOpts
|
139
|
+
|
50
140
|
/**
|
51
|
-
*
|
141
|
+
* Decodes a byte array into a number.
|
142
|
+
*
|
143
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestonumber
|
144
|
+
*
|
145
|
+
* @param bytes Byte array to decode.
|
146
|
+
* @param opts Options.
|
147
|
+
* @returns Number value.
|
148
|
+
*
|
149
|
+
* @example
|
150
|
+
* import { bytesToNumber } from 'viem'
|
151
|
+
* const data = bytesToNumber(new Uint8Array([1, 164]))
|
152
|
+
* // 420
|
52
153
|
*/
|
53
|
-
export function bytesToNumber(
|
54
|
-
|
154
|
+
export function bytesToNumber(
|
155
|
+
bytes: ByteArray,
|
156
|
+
opts: BytesToNumberOpts = {},
|
157
|
+
): number {
|
158
|
+
if (typeof opts.size !== 'undefined') assertSize(bytes, { size: opts.size })
|
159
|
+
const hex = bytesToHex(bytes, opts)
|
55
160
|
return hexToNumber(hex)
|
56
161
|
}
|
57
162
|
|
163
|
+
export type BytesToStringOpts = {
|
164
|
+
/** Size of the bytes. */
|
165
|
+
size?: number
|
166
|
+
}
|
167
|
+
|
58
168
|
/**
|
59
|
-
*
|
169
|
+
* Decodes a byte array into a UTF-8 string.
|
170
|
+
*
|
171
|
+
* - Docs: https://viem.sh/docs/utilities/fromBytes.html#bytestostring
|
172
|
+
*
|
173
|
+
* @param bytes Byte array to decode.
|
174
|
+
* @param opts Options.
|
175
|
+
* @returns String value.
|
176
|
+
*
|
177
|
+
* @example
|
178
|
+
* import { bytesToString } from 'viem'
|
179
|
+
* const data = bytesToString(new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]))
|
180
|
+
* // 'Hello world'
|
60
181
|
*/
|
61
|
-
export function bytesToString(
|
182
|
+
export function bytesToString(
|
183
|
+
bytes_: ByteArray,
|
184
|
+
opts: BytesToStringOpts = {},
|
185
|
+
): string {
|
186
|
+
let bytes = bytes_
|
187
|
+
if (typeof opts.size !== 'undefined') {
|
188
|
+
assertSize(bytes, { size: opts.size })
|
189
|
+
bytes = trim(bytes, { dir: 'right' })
|
190
|
+
}
|
62
191
|
return new TextDecoder().decode(bytes)
|
63
192
|
}
|