viem 0.4.0-main.20230418T011420 → 0.4.0-main.20230419T210450
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/actions/public/call.js +97 -10
- package/dist/cjs/actions/public/call.js.map +1 -1
- package/dist/cjs/actions/public/simulateContract.js +1 -0
- package/dist/cjs/actions/public/simulateContract.js.map +1 -1
- package/dist/cjs/chains.js +1 -24
- package/dist/cjs/chains.js.map +1 -1
- package/dist/cjs/clients/createPublicClient.js +2 -1
- package/dist/cjs/clients/createPublicClient.js.map +1 -1
- package/dist/cjs/constants/contract.js +5 -0
- package/dist/cjs/constants/contract.js.map +1 -0
- package/dist/cjs/constants/index.js +3 -1
- package/dist/cjs/constants/index.js.map +1 -1
- package/dist/cjs/errors/chain.js +13 -1
- package/dist/cjs/errors/chain.js.map +1 -1
- package/dist/cjs/errors/contract.js +1 -1
- package/dist/cjs/errors/contract.js.map +1 -1
- package/dist/cjs/errors/encoding.js +13 -1
- package/dist/cjs/errors/encoding.js.map +1 -1
- package/dist/cjs/errors/index.js +5 -2
- package/dist/cjs/errors/index.js.map +1 -1
- package/dist/cjs/errors/version.js +1 -1
- package/dist/cjs/index.js +5 -4
- package/dist/cjs/index.js.map +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/cjs/utils/errors/getContractError.js +7 -5
- package/dist/cjs/utils/errors/getContractError.js.map +1 -1
- package/dist/cjs/utils/promise/createBatchScheduler.js +47 -0
- package/dist/cjs/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/cjs/utils/promise/index.js +3 -1
- package/dist/cjs/utils/promise/index.js.map +1 -1
- package/dist/esm/actions/public/call.js +91 -4
- package/dist/esm/actions/public/call.js.map +1 -1
- package/dist/esm/actions/public/simulateContract.js +1 -0
- package/dist/esm/actions/public/simulateContract.js.map +1 -1
- package/dist/esm/clients/createPublicClient.js +2 -1
- package/dist/esm/clients/createPublicClient.js.map +1 -1
- package/dist/esm/constants/contract.js +2 -0
- package/dist/esm/constants/contract.js.map +1 -0
- package/dist/esm/constants/index.js +1 -0
- package/dist/esm/constants/index.js.map +1 -1
- package/dist/esm/errors/chain.js +11 -0
- package/dist/esm/errors/chain.js.map +1 -1
- package/dist/esm/errors/contract.js +1 -1
- package/dist/esm/errors/contract.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 +2 -2
- package/dist/esm/errors/index.js.map +1 -1
- package/dist/esm/errors/version.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +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/esm/utils/errors/getContractError.js +7 -5
- package/dist/esm/utils/errors/getContractError.js.map +1 -1
- package/dist/esm/utils/promise/createBatchScheduler.js +43 -0
- package/dist/esm/utils/promise/createBatchScheduler.js.map +1 -0
- package/dist/esm/utils/promise/index.js +1 -0
- package/dist/esm/utils/promise/index.js.map +1 -1
- package/dist/types/actions/public/call.d.ts +1 -0
- package/dist/types/actions/public/call.d.ts.map +1 -1
- package/dist/types/actions/public/simulateContract.d.ts +1 -1
- package/dist/types/actions/public/simulateContract.d.ts.map +1 -1
- package/dist/types/clients/createPublicClient.d.ts +15 -3
- package/dist/types/clients/createPublicClient.d.ts.map +1 -1
- package/dist/types/constants/contract.d.ts +2 -0
- package/dist/types/constants/contract.d.ts.map +1 -0
- package/dist/types/constants/index.d.ts +1 -0
- package/dist/types/constants/index.d.ts.map +1 -1
- package/dist/types/errors/chain.d.ts +4 -0
- package/dist/types/errors/chain.d.ts.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 +2 -2
- package/dist/types/errors/index.d.ts.map +1 -1
- package/dist/types/errors/version.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/eip1193.d.ts +1 -1
- package/dist/types/types/eip1193.d.ts.map +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/dist/types/utils/errors/getContractError.d.ts.map +1 -1
- package/dist/types/utils/promise/createBatchScheduler.d.ts +15 -0
- package/dist/types/utils/promise/createBatchScheduler.d.ts.map +1 -0
- package/dist/types/utils/promise/index.d.ts +1 -0
- package/dist/types/utils/promise/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/actions/public/call.ts +141 -7
- package/src/actions/public/simulateContract.ts +2 -1
- package/src/clients/createPublicClient.ts +17 -1
- package/src/constants/contract.ts +1 -0
- package/src/constants/index.ts +2 -0
- package/src/errors/chain.ts +8 -0
- package/src/errors/contract.ts +1 -1
- package/src/errors/encoding.ts +9 -0
- package/src/errors/index.ts +2 -0
- package/src/errors/version.ts +1 -1
- package/src/index.ts +1 -0
- package/src/types/eip1193.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
- package/src/utils/errors/getContractError.ts +9 -5
- package/src/utils/promise/createBatchScheduler.ts +82 -0
- package/src/utils/promise/index.ts +1 -0
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
@@ -37,6 +37,7 @@ export {
|
|
37
37
|
ChainDoesNotSupportContract,
|
38
38
|
ChainMismatchError,
|
39
39
|
ChainNotFoundError,
|
40
|
+
ClientChainNotConfiguredError,
|
40
41
|
InvalidChainIdError,
|
41
42
|
} from './chain.js'
|
42
43
|
|
@@ -58,6 +59,7 @@ export {
|
|
58
59
|
InvalidHexBooleanError,
|
59
60
|
InvalidHexValueError,
|
60
61
|
OffsetOutOfBoundsError,
|
62
|
+
SizeOverflowError,
|
61
63
|
} from './encoding.js'
|
62
64
|
|
63
65
|
export {
|
package/src/errors/version.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export const version = '0.4.0-main.
|
1
|
+
export const version = '0.4.0-main.20230419T210450'
|
package/src/index.ts
CHANGED
package/src/types/eip1193.ts
CHANGED
@@ -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
|
}
|
@@ -1,9 +1,35 @@
|
|
1
|
-
import {
|
1
|
+
import {
|
2
|
+
InvalidHexBooleanError,
|
3
|
+
SizeOverflowError,
|
4
|
+
} from '../../errors/index.js'
|
2
5
|
import type { ByteArray, Hex } from '../../types/index.js'
|
6
|
+
import { size as size_ } from '../data/size.js'
|
3
7
|
import { trim } from '../data/index.js'
|
4
8
|
import { hexToBytes } from './toBytes.js'
|
5
9
|
|
6
|
-
|
10
|
+
export function assertSize(
|
11
|
+
hexOrBytes: Hex | ByteArray,
|
12
|
+
{ size }: { size: number },
|
13
|
+
): void {
|
14
|
+
if (size_(hexOrBytes) > size)
|
15
|
+
throw new SizeOverflowError({
|
16
|
+
givenSize: size_(hexOrBytes),
|
17
|
+
maxSize: size,
|
18
|
+
})
|
19
|
+
}
|
20
|
+
|
21
|
+
export type FromHexParameters<
|
22
|
+
TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
|
23
|
+
> =
|
24
|
+
| TTo
|
25
|
+
| {
|
26
|
+
/** Size (in bytes) of the hex value. */
|
27
|
+
size?: number
|
28
|
+
/** Type to convert to. */
|
29
|
+
to: TTo
|
30
|
+
}
|
31
|
+
|
32
|
+
export type FromHexReturnType<TTo> = TTo extends 'string'
|
7
33
|
? string
|
8
34
|
: TTo extends 'bigint'
|
9
35
|
? bigint
|
@@ -16,29 +42,77 @@ type FromHexReturnType<TTo> = TTo extends 'string'
|
|
16
42
|
: never
|
17
43
|
|
18
44
|
/**
|
19
|
-
*
|
45
|
+
* Decodes a hex string into a string, number, bigint, boolean, or byte array.
|
46
|
+
*
|
47
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html
|
48
|
+
* - Example: https://viem.sh/docs/utilities/fromHex.html#usage
|
49
|
+
*
|
50
|
+
* @param hex Hex string to decode.
|
51
|
+
* @param toOrOpts Type to convert to or options.
|
52
|
+
* @returns Decoded value.
|
53
|
+
*
|
54
|
+
* @example
|
55
|
+
* import { fromHex } from 'viem'
|
56
|
+
* const data = fromHex('0x1a4', 'number')
|
57
|
+
* // 420
|
58
|
+
*
|
59
|
+
* @example
|
60
|
+
* import { fromHex } from 'viem'
|
61
|
+
* const data = fromHex('0x48656c6c6f20576f726c6421', 'string')
|
62
|
+
* // 'Hello world'
|
63
|
+
*
|
64
|
+
* @example
|
65
|
+
* import { fromHex } from 'viem'
|
66
|
+
* const data = fromHex('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
|
67
|
+
* size: 32,
|
68
|
+
* to: 'string'
|
69
|
+
* })
|
70
|
+
* // 'Hello world'
|
20
71
|
*/
|
21
72
|
export function fromHex<
|
22
73
|
TTo extends 'string' | 'bigint' | 'number' | 'bytes' | 'boolean',
|
23
|
-
>(hex: Hex,
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
if (to === '
|
28
|
-
return
|
74
|
+
>(hex: Hex, toOrOpts: FromHexParameters<TTo>): FromHexReturnType<TTo> {
|
75
|
+
const opts = typeof toOrOpts === 'string' ? { to: toOrOpts } : toOrOpts
|
76
|
+
const to = opts.to
|
77
|
+
|
78
|
+
if (to === 'number') return hexToNumber(hex, opts) as FromHexReturnType<TTo>
|
79
|
+
if (to === 'bigint') return hexToBigInt(hex, opts) as FromHexReturnType<TTo>
|
80
|
+
if (to === 'string') return hexToString(hex, opts) as FromHexReturnType<TTo>
|
81
|
+
if (to === 'boolean') return hexToBool(hex, opts) as FromHexReturnType<TTo>
|
82
|
+
return hexToBytes(hex, opts) as FromHexReturnType<TTo>
|
29
83
|
}
|
30
84
|
|
31
85
|
export type HexToBigIntOpts = {
|
32
|
-
|
86
|
+
/** Whether or not the number of a signed representation. */
|
33
87
|
signed?: boolean
|
88
|
+
/** Size (in bytes) of the hex value. */
|
89
|
+
size?: number
|
34
90
|
}
|
35
91
|
|
36
92
|
/**
|
37
|
-
*
|
93
|
+
* Decodes a hex value into a bigint.
|
94
|
+
*
|
95
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobigint
|
96
|
+
*
|
97
|
+
* @param hex Hex value to decode.
|
98
|
+
* @param opts Options.
|
99
|
+
* @returns BigInt value.
|
100
|
+
*
|
101
|
+
* @example
|
102
|
+
* import { hexToBigInt } from 'viem'
|
103
|
+
* const data = hexToBigInt('0x1a4', { signed: true })
|
104
|
+
* // 420n
|
105
|
+
*
|
106
|
+
* @example
|
107
|
+
* import { hexToBigInt } from 'viem'
|
108
|
+
* const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
109
|
+
* // 420n
|
38
110
|
*/
|
39
111
|
export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
|
40
112
|
const { signed } = opts
|
41
113
|
|
114
|
+
if (opts.size) assertSize(hex, { size: opts.size })
|
115
|
+
|
42
116
|
const value = BigInt(hex)
|
43
117
|
if (!signed) return value
|
44
118
|
|
@@ -49,28 +123,97 @@ export function hexToBigInt(hex: Hex, opts: HexToBigIntOpts = {}): bigint {
|
|
49
123
|
return value - BigInt(`0x${'f'.padStart(size * 2, 'f')}`) - 1n
|
50
124
|
}
|
51
125
|
|
126
|
+
export type HexToBoolOpts = {
|
127
|
+
/** Size (in bytes) of the hex value. */
|
128
|
+
size?: number
|
129
|
+
}
|
130
|
+
|
52
131
|
/**
|
53
|
-
*
|
132
|
+
* Decodes a hex value into a boolean.
|
133
|
+
*
|
134
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextobool
|
135
|
+
*
|
136
|
+
* @param hex Hex value to decode.
|
137
|
+
* @param opts Options.
|
138
|
+
* @returns Boolean value.
|
139
|
+
*
|
140
|
+
* @example
|
141
|
+
* import { hexToBool } from 'viem'
|
142
|
+
* const data = hexToBool('0x1')
|
143
|
+
* // true
|
144
|
+
*
|
145
|
+
* @example
|
146
|
+
* import { hexToBool } from 'viem'
|
147
|
+
* const data = hexToBool('0x0000000000000000000000000000000000000000000000000000000000000001', { size: 32 })
|
148
|
+
* // true
|
54
149
|
*/
|
55
|
-
export function hexToBool(
|
150
|
+
export function hexToBool(hex_: Hex, opts: HexToBoolOpts = {}): boolean {
|
151
|
+
let hex = hex_
|
152
|
+
if (opts.size) {
|
153
|
+
assertSize(hex, { size: opts.size })
|
154
|
+
hex = trim(hex)
|
155
|
+
}
|
56
156
|
if (trim(hex) === '0x0') return false
|
57
157
|
if (trim(hex) === '0x1') return true
|
58
158
|
throw new InvalidHexBooleanError(hex)
|
59
159
|
}
|
60
160
|
|
61
|
-
type
|
161
|
+
export type HexToNumberOpts = HexToBigIntOpts
|
62
162
|
|
63
163
|
/**
|
64
|
-
*
|
164
|
+
* Decodes a hex string into a number.
|
165
|
+
*
|
166
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextonumber
|
167
|
+
*
|
168
|
+
* @param hex Hex value to decode.
|
169
|
+
* @param opts Options.
|
170
|
+
* @returns Number value.
|
171
|
+
*
|
172
|
+
* @example
|
173
|
+
* import { hexToNumber } from 'viem'
|
174
|
+
* const data = hexToNumber('0x1a4')
|
175
|
+
* // 420
|
176
|
+
*
|
177
|
+
* @example
|
178
|
+
* import { hexToNumber } from 'viem'
|
179
|
+
* const data = hexToBigInt('0x00000000000000000000000000000000000000000000000000000000000001a4', { size: 32 })
|
180
|
+
* // 420
|
65
181
|
*/
|
66
|
-
export function hexToNumber(hex: Hex, opts:
|
182
|
+
export function hexToNumber(hex: Hex, opts: HexToNumberOpts = {}): number {
|
67
183
|
return Number(hexToBigInt(hex, opts))
|
68
184
|
}
|
69
185
|
|
186
|
+
export type HexToStringOpts = {
|
187
|
+
/** Size (in bytes) of the hex value. */
|
188
|
+
size?: number
|
189
|
+
}
|
190
|
+
|
70
191
|
/**
|
71
|
-
*
|
192
|
+
* Decodes a hex value into a UTF-8 string.
|
193
|
+
*
|
194
|
+
* - Docs: https://viem.sh/docs/utilities/fromHex.html#hextostring
|
195
|
+
*
|
196
|
+
* @param hex Hex value to decode.
|
197
|
+
* @param opts Options.
|
198
|
+
* @returns String value.
|
199
|
+
*
|
200
|
+
* @example
|
201
|
+
* import { hexToString } from 'viem'
|
202
|
+
* const data = hexToString('0x48656c6c6f20576f726c6421')
|
203
|
+
* // 'Hello world!'
|
204
|
+
*
|
205
|
+
* @example
|
206
|
+
* import { hexToString } from 'viem'
|
207
|
+
* const data = hexToString('0x48656c6c6f20576f726c64210000000000000000000000000000000000000000', {
|
208
|
+
* size: 32,
|
209
|
+
* })
|
210
|
+
* // 'Hello world'
|
72
211
|
*/
|
73
|
-
export function hexToString(hex: Hex): string {
|
74
|
-
|
212
|
+
export function hexToString(hex: Hex, opts: HexToStringOpts = {}): string {
|
213
|
+
let bytes = hexToBytes(hex)
|
214
|
+
if (opts.size) {
|
215
|
+
assertSize(bytes, { size: opts.size })
|
216
|
+
bytes = trim(bytes, { dir: 'right' })
|
217
|
+
}
|
75
218
|
return new TextDecoder().decode(bytes)
|
76
219
|
}
|