viem 0.4.0-main.20230418T011420 → 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.
Files changed (48) hide show
  1. package/README.md +6 -0
  2. package/dist/cjs/errors/encoding.js +13 -1
  3. package/dist/cjs/errors/encoding.js.map +1 -1
  4. package/dist/cjs/errors/index.js +3 -2
  5. package/dist/cjs/errors/index.js.map +1 -1
  6. package/dist/cjs/errors/version.js +1 -1
  7. package/dist/cjs/utils/encoding/fromBytes.js +29 -12
  8. package/dist/cjs/utils/encoding/fromBytes.js.map +1 -1
  9. package/dist/cjs/utils/encoding/fromHex.js +32 -10
  10. package/dist/cjs/utils/encoding/fromHex.js.map +1 -1
  11. package/dist/cjs/utils/encoding/toBytes.js +31 -15
  12. package/dist/cjs/utils/encoding/toBytes.js.map +1 -1
  13. package/dist/cjs/utils/encoding/toHex.js +31 -20
  14. package/dist/cjs/utils/encoding/toHex.js.map +1 -1
  15. package/dist/esm/errors/encoding.js +11 -0
  16. package/dist/esm/errors/encoding.js.map +1 -1
  17. package/dist/esm/errors/index.js +1 -1
  18. package/dist/esm/errors/index.js.map +1 -1
  19. package/dist/esm/errors/version.js +1 -1
  20. package/dist/esm/utils/encoding/fromBytes.js +30 -13
  21. package/dist/esm/utils/encoding/fromBytes.js.map +1 -1
  22. package/dist/esm/utils/encoding/fromHex.js +31 -10
  23. package/dist/esm/utils/encoding/fromHex.js.map +1 -1
  24. package/dist/esm/utils/encoding/toBytes.js +31 -15
  25. package/dist/esm/utils/encoding/toBytes.js.map +1 -1
  26. package/dist/esm/utils/encoding/toHex.js +28 -17
  27. package/dist/esm/utils/encoding/toHex.js.map +1 -1
  28. package/dist/types/errors/encoding.d.ts +7 -0
  29. package/dist/types/errors/encoding.d.ts.map +1 -1
  30. package/dist/types/errors/index.d.ts +1 -1
  31. package/dist/types/errors/index.d.ts.map +1 -1
  32. package/dist/types/errors/version.d.ts +1 -1
  33. package/dist/types/utils/encoding/fromBytes.d.ts +95 -10
  34. package/dist/types/utils/encoding/fromBytes.d.ts.map +1 -1
  35. package/dist/types/utils/encoding/fromHex.d.ts +122 -12
  36. package/dist/types/utils/encoding/fromHex.d.ts.map +1 -1
  37. package/dist/types/utils/encoding/toBytes.d.ts +113 -9
  38. package/dist/types/utils/encoding/toBytes.d.ts.map +1 -1
  39. package/dist/types/utils/encoding/toHex.d.ts +120 -10
  40. package/dist/types/utils/encoding/toHex.d.ts.map +1 -1
  41. package/package.json +1 -1
  42. package/src/errors/encoding.ts +9 -0
  43. package/src/errors/index.ts +1 -0
  44. package/src/errors/version.ts +1 -1
  45. package/src/utils/encoding/fromBytes.ts +147 -18
  46. package/src/utils/encoding/fromHex.ts +162 -19
  47. package/src/utils/encoding/toBytes.ts +148 -18
  48. 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
- /** @description Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array. */
4
- export declare function toBytes(value: string | bigint | number | boolean | Hex): ByteArray;
3
+ export type ToBytesParameters = {
4
+ /** Size of the output bytes. */
5
+ size?: number;
6
+ };
5
7
  /**
6
- * @description Encodes a boolean into a byte array.
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 boolToBytes(value: boolean): Uint8Array;
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
- * @description Encodes a hex string into a byte array.
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 hexToBytes(hex_: Hex): ByteArray;
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
- * @description Encodes a number into a byte array.
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
- * @description Encodes a UTF-8 string into a byte array.
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;AAE1D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAKjD,iGAAiG;AACjG,wBAAgB,OAAO,CACrB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,GAAG,GAC9C,SAAS,CAMX;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,cAIzC;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,GAAG,GAAG,SAAS,CAe/C;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,CAAC,EAAE,eAAe,cAG3E;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAEtD"}
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
- * @description Encodes a boolean into a hex string
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 boolToHex(value: boolean): Hex;
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
- * @description Encodes a bytes array into a hex string
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 bytesToHex(value: ByteArray): Hex;
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
- * @description Encodes a string, number, bigint, or ByteArray into a hex string
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 toHex(value: string | number | bigint | boolean | ByteArray): Hex;
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?: never;
92
+ /** The size (in bytes) of the output hex value. */
93
+ size?: number;
20
94
  };
21
95
  /**
22
- * @description Encodes a number or bigint into a hex string
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
- * @description Encodes a UTF-8 string into a hex string
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;AAO1D;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,GAAG,CAE7C;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAMhD;AAED;;GAEG;AACH,wBAAgB,KAAK,CACnB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,SAAS,GACpD,GAAG,CAQL;AAED,MAAM,MAAM,eAAe,GACvB;IAEE,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB,IAAI,EAAE,MAAM,CAAA;CACb,GACD;IACE,MAAM,CAAC,EAAE,KAAK,CAAA;IACd,IAAI,CAAC,EAAE,KAAK,CAAA;CACb,CAAA;AAEL;;GAEG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,IAAI,GAAE,eAAoB,GACzB,GAAG,CAgCL;AAID;;GAEG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,CAG/C"}
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
@@ -12,7 +12,7 @@
12
12
  "isomorphic-ws": "5.0.0",
13
13
  "ws": "8.12.0"
14
14
  },
15
- "version": "0.4.0-main.20230418T011420",
15
+ "version": "0.4.0-main.20230419T202802",
16
16
  "files": [
17
17
  "dist",
18
18
  "!dist/**/*.tsbuildinfo",
@@ -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
+ }
@@ -58,6 +58,7 @@ export {
58
58
  InvalidHexBooleanError,
59
59
  InvalidHexValueError,
60
60
  OffsetOutOfBoundsError,
61
+ SizeOverflowError,
61
62
  } from './encoding.js'
62
63
 
63
64
  export {
@@ -1 +1 @@
1
- export const version = '0.4.0-main.20230418T011420'
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 { hexToBigInt, hexToNumber } from './fromHex.js'
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
- * @description Decodes a byte array into a UTF-8 string, hex value, number, bigint or boolean.
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
- >(bytes: ByteArray, to: TTo): FromBytesReturnType<TTo> {
24
- if (to === 'number') return bytesToNumber(bytes) as FromBytesReturnType<TTo>
25
- if (to === 'bigint') return bytesToBigint(bytes) as FromBytesReturnType<TTo>
26
- if (to === 'boolean') return bytesToBool(bytes) as FromBytesReturnType<TTo>
27
- if (to === 'string') return bytesToString(bytes) as FromBytesReturnType<TTo>
28
- return bytesToHex(bytes) as FromBytesReturnType<TTo>
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
- * @description Decodes a byte array into a bigint.
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(bytes: ByteArray): bigint {
35
- const hex = bytesToHex(bytes)
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
- * @description Decodes a byte array into a boolean.
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(bytes: ByteArray): boolean {
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
- * @description Decodes a byte array into a number.
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(bytes: ByteArray): number {
54
- const hex = bytesToHex(bytes)
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
- * @description Decodes a byte array into a UTF-8 string.
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(bytes: ByteArray): string {
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
  }