@feelyourprotocol/util 8141.0.0

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 (223) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +297 -0
  3. package/dist/cjs/account.d.ts +165 -0
  4. package/dist/cjs/account.d.ts.map +1 -0
  5. package/dist/cjs/account.js +530 -0
  6. package/dist/cjs/account.js.map +1 -0
  7. package/dist/cjs/address.d.ts +67 -0
  8. package/dist/cjs/address.d.ts.map +1 -0
  9. package/dist/cjs/address.js +136 -0
  10. package/dist/cjs/address.js.map +1 -0
  11. package/dist/cjs/authorization.d.ts +41 -0
  12. package/dist/cjs/authorization.d.ts.map +1 -0
  13. package/dist/cjs/authorization.js +135 -0
  14. package/dist/cjs/authorization.js.map +1 -0
  15. package/dist/cjs/bal.d.ts +129 -0
  16. package/dist/cjs/bal.d.ts.map +1 -0
  17. package/dist/cjs/bal.js +529 -0
  18. package/dist/cjs/bal.js.map +1 -0
  19. package/dist/cjs/binaryTree.d.ts +148 -0
  20. package/dist/cjs/binaryTree.d.ts.map +1 -0
  21. package/dist/cjs/binaryTree.js +240 -0
  22. package/dist/cjs/binaryTree.js.map +1 -0
  23. package/dist/cjs/blobs.d.ts +76 -0
  24. package/dist/cjs/blobs.d.ts.map +1 -0
  25. package/dist/cjs/blobs.js +175 -0
  26. package/dist/cjs/blobs.js.map +1 -0
  27. package/dist/cjs/bytes.d.ts +291 -0
  28. package/dist/cjs/bytes.d.ts.map +1 -0
  29. package/dist/cjs/bytes.js +606 -0
  30. package/dist/cjs/bytes.js.map +1 -0
  31. package/dist/cjs/constants.d.ts +91 -0
  32. package/dist/cjs/constants.d.ts.map +1 -0
  33. package/dist/cjs/constants.js +97 -0
  34. package/dist/cjs/constants.js.map +1 -0
  35. package/dist/cjs/db.d.ts +65 -0
  36. package/dist/cjs/db.d.ts.map +1 -0
  37. package/dist/cjs/db.js +14 -0
  38. package/dist/cjs/db.js.map +1 -0
  39. package/dist/cjs/env.d.ts +9 -0
  40. package/dist/cjs/env.d.ts.map +1 -0
  41. package/dist/cjs/env.js +13 -0
  42. package/dist/cjs/env.js.map +1 -0
  43. package/dist/cjs/errors.d.ts +3 -0
  44. package/dist/cjs/errors.d.ts.map +1 -0
  45. package/dist/cjs/errors.js +19 -0
  46. package/dist/cjs/errors.js.map +1 -0
  47. package/dist/cjs/helpers.d.ts +21 -0
  48. package/dist/cjs/helpers.d.ts.map +1 -0
  49. package/dist/cjs/helpers.js +50 -0
  50. package/dist/cjs/helpers.js.map +1 -0
  51. package/dist/cjs/index.d.ts +67 -0
  52. package/dist/cjs/index.d.ts.map +1 -0
  53. package/dist/cjs/index.js +93 -0
  54. package/dist/cjs/index.js.map +1 -0
  55. package/dist/cjs/internal.d.ts +72 -0
  56. package/dist/cjs/internal.d.ts.map +1 -0
  57. package/dist/cjs/internal.js +182 -0
  58. package/dist/cjs/internal.js.map +1 -0
  59. package/dist/cjs/kzg.d.ts +14 -0
  60. package/dist/cjs/kzg.d.ts.map +1 -0
  61. package/dist/cjs/kzg.js +3 -0
  62. package/dist/cjs/kzg.js.map +1 -0
  63. package/dist/cjs/lock.d.ts +15 -0
  64. package/dist/cjs/lock.d.ts.map +1 -0
  65. package/dist/cjs/lock.js +45 -0
  66. package/dist/cjs/lock.js.map +1 -0
  67. package/dist/cjs/mapDB.d.ts +17 -0
  68. package/dist/cjs/mapDB.d.ts.map +1 -0
  69. package/dist/cjs/mapDB.js +46 -0
  70. package/dist/cjs/mapDB.js.map +1 -0
  71. package/dist/cjs/package.json +3 -0
  72. package/dist/cjs/provider.d.ts +46 -0
  73. package/dist/cjs/provider.d.ts.map +1 -0
  74. package/dist/cjs/provider.js +84 -0
  75. package/dist/cjs/provider.js.map +1 -0
  76. package/dist/cjs/request.d.ts +20 -0
  77. package/dist/cjs/request.d.ts.map +1 -0
  78. package/dist/cjs/request.js +35 -0
  79. package/dist/cjs/request.js.map +1 -0
  80. package/dist/cjs/signature.d.ts +47 -0
  81. package/dist/cjs/signature.d.ts.map +1 -0
  82. package/dist/cjs/signature.js +147 -0
  83. package/dist/cjs/signature.js.map +1 -0
  84. package/dist/cjs/tasks.d.ts +32 -0
  85. package/dist/cjs/tasks.d.ts.map +1 -0
  86. package/dist/cjs/tasks.js +51 -0
  87. package/dist/cjs/tasks.js.map +1 -0
  88. package/dist/cjs/types.d.ts +64 -0
  89. package/dist/cjs/types.d.ts.map +1 -0
  90. package/dist/cjs/types.js +78 -0
  91. package/dist/cjs/types.js.map +1 -0
  92. package/dist/cjs/units.d.ts +22 -0
  93. package/dist/cjs/units.d.ts.map +1 -0
  94. package/dist/cjs/units.js +51 -0
  95. package/dist/cjs/units.js.map +1 -0
  96. package/dist/cjs/withdrawal.d.ts +72 -0
  97. package/dist/cjs/withdrawal.d.ts.map +1 -0
  98. package/dist/cjs/withdrawal.js +93 -0
  99. package/dist/cjs/withdrawal.js.map +1 -0
  100. package/dist/esm/account.d.ts +165 -0
  101. package/dist/esm/account.d.ts.map +1 -0
  102. package/dist/esm/account.js +505 -0
  103. package/dist/esm/account.js.map +1 -0
  104. package/dist/esm/address.d.ts +67 -0
  105. package/dist/esm/address.d.ts.map +1 -0
  106. package/dist/esm/address.js +125 -0
  107. package/dist/esm/address.js.map +1 -0
  108. package/dist/esm/authorization.d.ts +41 -0
  109. package/dist/esm/authorization.d.ts.map +1 -0
  110. package/dist/esm/authorization.js +126 -0
  111. package/dist/esm/authorization.js.map +1 -0
  112. package/dist/esm/bal.d.ts +129 -0
  113. package/dist/esm/bal.d.ts.map +1 -0
  114. package/dist/esm/bal.js +522 -0
  115. package/dist/esm/bal.js.map +1 -0
  116. package/dist/esm/binaryTree.d.ts +148 -0
  117. package/dist/esm/binaryTree.d.ts.map +1 -0
  118. package/dist/esm/binaryTree.js +226 -0
  119. package/dist/esm/binaryTree.js.map +1 -0
  120. package/dist/esm/blobs.d.ts +76 -0
  121. package/dist/esm/blobs.d.ts.map +1 -0
  122. package/dist/esm/blobs.js +163 -0
  123. package/dist/esm/blobs.js.map +1 -0
  124. package/dist/esm/bytes.d.ts +291 -0
  125. package/dist/esm/bytes.d.ts.map +1 -0
  126. package/dist/esm/bytes.js +562 -0
  127. package/dist/esm/bytes.js.map +1 -0
  128. package/dist/esm/constants.d.ts +91 -0
  129. package/dist/esm/constants.d.ts.map +1 -0
  130. package/dist/esm/constants.js +94 -0
  131. package/dist/esm/constants.js.map +1 -0
  132. package/dist/esm/db.d.ts +65 -0
  133. package/dist/esm/db.d.ts.map +1 -0
  134. package/dist/esm/db.js +11 -0
  135. package/dist/esm/db.js.map +1 -0
  136. package/dist/esm/env.d.ts +9 -0
  137. package/dist/esm/env.d.ts.map +1 -0
  138. package/dist/esm/env.js +9 -0
  139. package/dist/esm/env.js.map +1 -0
  140. package/dist/esm/errors.d.ts +3 -0
  141. package/dist/esm/errors.d.ts.map +1 -0
  142. package/dist/esm/errors.js +14 -0
  143. package/dist/esm/errors.js.map +1 -0
  144. package/dist/esm/helpers.d.ts +21 -0
  145. package/dist/esm/helpers.d.ts.map +1 -0
  146. package/dist/esm/helpers.js +43 -0
  147. package/dist/esm/helpers.js.map +1 -0
  148. package/dist/esm/index.d.ts +67 -0
  149. package/dist/esm/index.d.ts.map +1 -0
  150. package/dist/esm/index.js +67 -0
  151. package/dist/esm/index.js.map +1 -0
  152. package/dist/esm/internal.d.ts +72 -0
  153. package/dist/esm/internal.d.ts.map +1 -0
  154. package/dist/esm/internal.js +170 -0
  155. package/dist/esm/internal.js.map +1 -0
  156. package/dist/esm/kzg.d.ts +14 -0
  157. package/dist/esm/kzg.d.ts.map +1 -0
  158. package/dist/esm/kzg.js +2 -0
  159. package/dist/esm/kzg.js.map +1 -0
  160. package/dist/esm/lock.d.ts +15 -0
  161. package/dist/esm/lock.d.ts.map +1 -0
  162. package/dist/esm/lock.js +41 -0
  163. package/dist/esm/lock.js.map +1 -0
  164. package/dist/esm/mapDB.d.ts +17 -0
  165. package/dist/esm/mapDB.d.ts.map +1 -0
  166. package/dist/esm/mapDB.js +42 -0
  167. package/dist/esm/mapDB.js.map +1 -0
  168. package/dist/esm/package.json +3 -0
  169. package/dist/esm/provider.d.ts +46 -0
  170. package/dist/esm/provider.d.ts.map +1 -0
  171. package/dist/esm/provider.js +79 -0
  172. package/dist/esm/provider.js.map +1 -0
  173. package/dist/esm/request.d.ts +20 -0
  174. package/dist/esm/request.d.ts.map +1 -0
  175. package/dist/esm/request.js +30 -0
  176. package/dist/esm/request.js.map +1 -0
  177. package/dist/esm/signature.d.ts +47 -0
  178. package/dist/esm/signature.d.ts.map +1 -0
  179. package/dist/esm/signature.js +137 -0
  180. package/dist/esm/signature.js.map +1 -0
  181. package/dist/esm/tasks.d.ts +32 -0
  182. package/dist/esm/tasks.d.ts.map +1 -0
  183. package/dist/esm/tasks.js +47 -0
  184. package/dist/esm/tasks.js.map +1 -0
  185. package/dist/esm/types.d.ts +64 -0
  186. package/dist/esm/types.d.ts.map +1 -0
  187. package/dist/esm/types.js +71 -0
  188. package/dist/esm/types.js.map +1 -0
  189. package/dist/esm/units.d.ts +22 -0
  190. package/dist/esm/units.d.ts.map +1 -0
  191. package/dist/esm/units.js +46 -0
  192. package/dist/esm/units.js.map +1 -0
  193. package/dist/esm/withdrawal.d.ts +72 -0
  194. package/dist/esm/withdrawal.d.ts.map +1 -0
  195. package/dist/esm/withdrawal.js +86 -0
  196. package/dist/esm/withdrawal.js.map +1 -0
  197. package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
  198. package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
  199. package/package.json +116 -0
  200. package/src/account.ts +630 -0
  201. package/src/address.ts +158 -0
  202. package/src/authorization.ts +180 -0
  203. package/src/bal.ts +761 -0
  204. package/src/binaryTree.ts +353 -0
  205. package/src/blobs.ts +209 -0
  206. package/src/bytes.ts +659 -0
  207. package/src/constants.ts +125 -0
  208. package/src/db.ts +86 -0
  209. package/src/env.ts +9 -0
  210. package/src/errors.ts +28 -0
  211. package/src/helpers.ts +46 -0
  212. package/src/index.ts +88 -0
  213. package/src/internal.ts +212 -0
  214. package/src/kzg.ts +24 -0
  215. package/src/lock.ts +42 -0
  216. package/src/mapDB.ts +57 -0
  217. package/src/provider.ts +109 -0
  218. package/src/request.ts +48 -0
  219. package/src/signature.ts +202 -0
  220. package/src/tasks.ts +59 -0
  221. package/src/types.ts +177 -0
  222. package/src/units.ts +56 -0
  223. package/src/withdrawal.ts +133 -0
@@ -0,0 +1,125 @@
1
+ import { secp256k1 } from '@noble/curves/secp256k1.js'
2
+ import { sha256 } from '@noble/hashes/sha2.js'
3
+
4
+ import { hexToBytes } from './bytes.ts'
5
+
6
+ /**
7
+ * 2^64-1
8
+ */
9
+ export const MAX_UINT64 = BigInt('0xffffffffffffffff')
10
+
11
+ /**
12
+ * The max integer that the evm can handle (2^256-1)
13
+ */
14
+ export const MAX_INTEGER = BigInt(
15
+ '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
16
+ )
17
+
18
+ /**
19
+ * The max integer that the evm can handle (2^256-1) as a bigint
20
+ * 2^256-1 equals to 340282366920938463463374607431768211455
21
+ * We use literal value instead of calculated value for compatibility issue.
22
+ */
23
+ export const MAX_INTEGER_BIGINT = BigInt(
24
+ '115792089237316195423570985008687907853269984665640564039457584007913129639935',
25
+ )
26
+
27
+ export const SECP256K1_ORDER = secp256k1.Point.CURVE().n
28
+ export const SECP256K1_ORDER_DIV_2 = SECP256K1_ORDER / BigInt(2)
29
+
30
+ /**
31
+ * 2^256
32
+ */
33
+ export const TWO_POW256 = BigInt(
34
+ '0x10000000000000000000000000000000000000000000000000000000000000000',
35
+ )
36
+
37
+ /**
38
+ * Keccak-256 hash of null
39
+ */
40
+ export const KECCAK256_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'
41
+
42
+ /**
43
+ * Keccak-256 hash of null
44
+ */
45
+ export const KECCAK256_NULL = hexToBytes(KECCAK256_NULL_S)
46
+
47
+ /**
48
+ * Keccak-256 of an RLP of an empty array
49
+ */
50
+ export const KECCAK256_RLP_ARRAY_S =
51
+ '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347'
52
+
53
+ /**
54
+ * Keccak-256 of an RLP of an empty array
55
+ */
56
+ export const KECCAK256_RLP_ARRAY = hexToBytes(KECCAK256_RLP_ARRAY_S)
57
+
58
+ /**
59
+ * Keccak-256 hash of the RLP of null
60
+ */
61
+ export const KECCAK256_RLP_S = '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421'
62
+
63
+ /**
64
+ * Keccak-256 hash of the RLP of null
65
+ */
66
+ export const KECCAK256_RLP = hexToBytes(KECCAK256_RLP_S)
67
+
68
+ export const SHA256_NULL = sha256(new Uint8Array())
69
+
70
+ /**
71
+ * RLP encoded empty string
72
+ */
73
+ export const RLP_EMPTY_STRING = Uint8Array.from([0x80])
74
+
75
+ export const MAX_WITHDRAWALS_PER_PAYLOAD = 16
76
+
77
+ export const RIPEMD160_ADDRESS_STRING = '0000000000000000000000000000000000000003'
78
+
79
+ /**
80
+ * System address used by various EIPs (EIP-2935, EIP-7002, EIP-7251, EIP-7708, BAL).
81
+ * Single canonical definition to avoid duplication across packages.
82
+ */
83
+ export const SYSTEM_ADDRESS = '0xfffffffffffffffffffffffffffffffffffffffe'
84
+ export const SYSTEM_ADDRESS_BYTES = hexToBytes(SYSTEM_ADDRESS)
85
+
86
+ /**
87
+ * EIP-7934: RLP Execution Block Size Limit constants
88
+ * Maximum RLP-encoded block size to 10 MiB, with a 2 MiB margin for beacon block sizes
89
+ */
90
+ export const MAX_BLOCK_SIZE = 10_485_760 // 10 MiB
91
+ export const SAFETY_MARGIN = 2_097_152 // 2 MiB
92
+ export const MAX_RLP_BLOCK_SIZE = MAX_BLOCK_SIZE - SAFETY_MARGIN // 8 MiB
93
+
94
+ /**
95
+ * BigInt constants
96
+ */
97
+
98
+ export const BIGINT_NEG1 = BigInt(-1)
99
+
100
+ export const BIGINT_0 = BigInt(0)
101
+ export const BIGINT_1 = BigInt(1)
102
+ export const BIGINT_2 = BigInt(2)
103
+ export const BIGINT_3 = BigInt(3)
104
+ export const BIGINT_7 = BigInt(7)
105
+ export const BIGINT_8 = BigInt(8)
106
+
107
+ export const BIGINT_27 = BigInt(27)
108
+ export const BIGINT_28 = BigInt(28)
109
+ export const BIGINT_31 = BigInt(31)
110
+ export const BIGINT_32 = BigInt(32)
111
+ export const BIGINT_64 = BigInt(64)
112
+
113
+ export const BIGINT_128 = BigInt(128)
114
+ export const BIGINT_255 = BigInt(255)
115
+ export const BIGINT_256 = BigInt(256)
116
+
117
+ export const BIGINT_96 = BigInt(96)
118
+ export const BIGINT_100 = BigInt(100)
119
+ export const BIGINT_160 = BigInt(160)
120
+ export const BIGINT_224 = BigInt(224)
121
+ export const BIGINT_2EXP96 = BigInt(79228162514264337593543950336)
122
+ export const BIGINT_2EXP160 = BigInt(1461501637330902918203684832716283019655932542976)
123
+ export const BIGINT_2EXP224 =
124
+ BigInt(26959946667150639794667015087019630673637144422540572481103610249216)
125
+ export const BIGINT_2EXP256 = BIGINT_2 ** BIGINT_256
package/src/db.ts ADDED
@@ -0,0 +1,86 @@
1
+ export type DBObject = {
2
+ [key: string]: string | string[] | number
3
+ }
4
+ export type BatchDBOp<
5
+ TKey extends Uint8Array | string | number = Uint8Array,
6
+ TValue extends Uint8Array | string | DBObject = Uint8Array,
7
+ > = PutBatch<TKey, TValue> | DelBatch<TKey>
8
+
9
+ export type KeyEncoding = (typeof KeyEncoding)[keyof typeof KeyEncoding]
10
+
11
+ export const KeyEncoding = {
12
+ String: 'string',
13
+ Bytes: 'view',
14
+ Number: 'number',
15
+ } as const
16
+
17
+ export type ValueEncoding = (typeof ValueEncoding)[keyof typeof ValueEncoding]
18
+
19
+ export const ValueEncoding = {
20
+ String: 'string',
21
+ Bytes: 'view',
22
+ JSON: 'json',
23
+ } as const
24
+
25
+ export type EncodingOpts = {
26
+ keyEncoding?: KeyEncoding
27
+ valueEncoding?: ValueEncoding
28
+ }
29
+ export interface PutBatch<
30
+ TKey extends Uint8Array | string | number = Uint8Array,
31
+ TValue extends Uint8Array | string | DBObject = Uint8Array,
32
+ > {
33
+ type: 'put'
34
+ key: TKey
35
+ value: TValue
36
+ opts?: EncodingOpts
37
+ }
38
+
39
+ export interface DelBatch<TKey extends Uint8Array | string | number = Uint8Array> {
40
+ type: 'del'
41
+ key: TKey
42
+ opts?: EncodingOpts
43
+ }
44
+
45
+ export interface DB<
46
+ TKey extends Uint8Array | string | number = Uint8Array,
47
+ TValue extends Uint8Array | string | DBObject = Uint8Array,
48
+ > {
49
+ /**
50
+ * Retrieves a raw value from db.
51
+ * @param key
52
+ * @returns A Promise that resolves to `Uint8Array` if a value is found or `undefined` if no value is found.
53
+ */
54
+ get(key: TKey, opts?: EncodingOpts): Promise<TValue | undefined>
55
+
56
+ /**
57
+ * Writes a value directly to db.
58
+ * @param key The key as a `TValue`
59
+ * @param value The value to be stored
60
+ */
61
+ put(key: TKey, val: TValue, opts?: EncodingOpts): Promise<void>
62
+
63
+ /**
64
+ * Removes a raw value in the underlying db.
65
+ * @param keys
66
+ */
67
+ del(key: TKey, opts?: EncodingOpts): Promise<void>
68
+
69
+ /**
70
+ * Performs a batch operation on db.
71
+ * @param opStack A stack of levelup operations
72
+ */
73
+ batch(opStack: BatchDBOp<TKey, TValue>[]): Promise<void>
74
+
75
+ /**
76
+ * Returns a copy of the DB instance, with a reference
77
+ * to the **same** underlying db instance.
78
+ */
79
+ shallowCopy(): DB<TKey, TValue>
80
+
81
+ /**
82
+ * Opens the database -- if applicable
83
+ */
84
+ open(): Promise<void>
85
+ // TODO - decide if we actually need open/close - it's not required for maps and Level automatically opens the DB when you instantiate it
86
+ }
package/src/env.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Safely checks if debug logging is enabled for the given namespace string.
3
+ * Works in Node.js, browser main thread, web workers, and service workers.
4
+ *
5
+ * Uses `globalThis.process` to avoid ReferenceError in environments where
6
+ * `process` is not a declared global (e.g., web workers, service workers).
7
+ */
8
+ export const isDebugEnabled = (namespace: string): boolean =>
9
+ globalThis.process?.env?.DEBUG?.includes(namespace) ?? false
package/src/errors.ts ADDED
@@ -0,0 +1,28 @@
1
+ import {
2
+ DEFAULT_ERROR_CODE,
3
+ EthereumJSError,
4
+ type EthereumJSErrorMetaData,
5
+ type EthereumJSErrorObject,
6
+ EthereumJSErrorWithoutCode,
7
+ } from '@feelyourprotocol/rlp'
8
+
9
+ export {
10
+ DEFAULT_ERROR_CODE,
11
+ EthereumJSError,
12
+ EthereumJSErrorWithoutCode,
13
+ type EthereumJSErrorMetaData,
14
+ type EthereumJSErrorObject,
15
+ }
16
+
17
+ // Below here: specific monorepo-wide errors (examples and commented out)
18
+
19
+ /*export enum UsageErrorType {
20
+ UNSUPPORTED_FEATURE = 'unsupported feature',
21
+ }*
22
+
23
+ /**
24
+ * Error along API Usage
25
+ *
26
+ * Use directly or in a subclassed context for error comparison (`e instanceof UsageError`)
27
+ */
28
+ //export class UsageError extends EthereumJSError<{ code: UsageErrorType }> {}
package/src/helpers.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { EthereumJSErrorWithoutCode } from './errors.ts'
2
+ import { isHexString } from './internal.ts'
3
+
4
+ /**
5
+ * Throws if a string is not hex prefixed
6
+ * @param {string} input string to check hex prefix of
7
+ */
8
+ export const assertIsHexString = function (input: string): void {
9
+ if (!isHexString(input)) {
10
+ const msg = `This method only supports 0x-prefixed hex strings but input was: ${input}`
11
+ throw EthereumJSErrorWithoutCode(msg)
12
+ }
13
+ }
14
+
15
+ /**
16
+ * Throws if input is not a buffer
17
+ * @param {Buffer} input value to check
18
+ */
19
+ export const assertIsBytes = function (input: Uint8Array): void {
20
+ if (!(input instanceof Uint8Array)) {
21
+ const msg = `This method only supports Uint8Array but input was: ${input}`
22
+ throw EthereumJSErrorWithoutCode(msg)
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Throws if input is not an array
28
+ * @param {number[]} input value to check
29
+ */
30
+ export const assertIsArray = function (input: number[]): void {
31
+ if (!Array.isArray(input)) {
32
+ const msg = `This method only supports number arrays but input was: ${input}`
33
+ throw EthereumJSErrorWithoutCode(msg)
34
+ }
35
+ }
36
+
37
+ /**
38
+ * Throws if input is not a string
39
+ * @param {string} input value to check
40
+ */
41
+ export const assertIsString = function (input: string): void {
42
+ if (typeof input !== 'string') {
43
+ const msg = `This method only supports strings but input was: ${input}`
44
+ throw EthereumJSErrorWithoutCode(msg)
45
+ }
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Constants
3
+ */
4
+ export * from './constants.ts'
5
+
6
+ /**
7
+ * Errors
8
+ */
9
+ export * from './errors.ts'
10
+
11
+ /**
12
+ * Units helpers
13
+ */
14
+ export * from './units.ts'
15
+
16
+ /**
17
+ * Account class and helper functions
18
+ */
19
+ export * from './account.ts'
20
+
21
+ /**
22
+ * Block level access list
23
+ *
24
+ * EXPERIMENTAL: DO NOT USE IN PRODUCTION!
25
+ */
26
+ export * from './bal.ts'
27
+
28
+ /**
29
+ * Address type
30
+ */
31
+ export * from './address.ts'
32
+
33
+ /**
34
+ * DB type
35
+ */
36
+ export * from './db.ts'
37
+
38
+ /**
39
+ * Withdrawal type
40
+ */
41
+ export * from './withdrawal.ts'
42
+
43
+ /**
44
+ * ECDSA signature
45
+ */
46
+ export * from './signature.ts'
47
+
48
+ /**
49
+ * Utilities for manipulating bytes, Uint8Arrays, etc.
50
+ */
51
+ export * from './bytes.ts'
52
+
53
+ /**
54
+ * Helpful TypeScript types
55
+ */
56
+ export * from './types.ts'
57
+
58
+ /**
59
+ * Block level access list
60
+ *
61
+ * EXPERIMENTAL: DO NOT USE IN PRODUCTION!
62
+ */
63
+ export * from './bal.ts'
64
+
65
+ /**
66
+ * Export ethjs-util methods
67
+ */
68
+ export * from './authorization.ts'
69
+ export * from './env.ts'
70
+ export * from './binaryTree.ts'
71
+ export * from './blobs.ts'
72
+ export {
73
+ arrayContainsArray,
74
+ fromAscii,
75
+ fromUtf8,
76
+ getBinarySize,
77
+ getKeys,
78
+ isHexString,
79
+ padToEven,
80
+ stripHexPrefix,
81
+ toAscii,
82
+ } from './internal.ts'
83
+ export * from './kzg.ts'
84
+ export * from './lock.ts'
85
+ export * from './mapDB.ts'
86
+ export * from './provider.ts'
87
+ export * from './request.ts'
88
+ export * from './tasks.ts'
@@ -0,0 +1,212 @@
1
+ /*
2
+ The MIT License
3
+
4
+ Copyright (c) 2016 Nick Dodson. nickdodson.com
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in
14
+ all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
+ THE SOFTWARE
23
+ */
24
+
25
+ import { bytesToUnprefixedHex, utf8ToBytes } from './bytes.ts'
26
+ import { EthereumJSErrorWithoutCode } from './errors.ts'
27
+
28
+ import type { PrefixedHexString } from './types.ts'
29
+
30
+ /**
31
+ * Returns a boolean on whether or not the the input starts with '0x' and matches the optional length
32
+ * @param {string} value the string input value
33
+ * @param {number|undefined} length the optional length of the hex string in bytes
34
+ * @returns {boolean} Whether or not the string is a valid PrefixedHexString matching the optional length
35
+ */
36
+ export function isHexString(value: string, length?: number): value is PrefixedHexString {
37
+ if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) return false
38
+
39
+ if (typeof length !== 'undefined' && length > 0 && value.length !== 2 + 2 * length) return false
40
+
41
+ return true
42
+ }
43
+
44
+ /**
45
+ * Removes '0x' from a given `String` if present
46
+ * @param str the string value
47
+ * @returns the string without 0x prefix
48
+ */
49
+ export const stripHexPrefix = (str: string): string => {
50
+ if (typeof str !== 'string')
51
+ throw EthereumJSErrorWithoutCode(
52
+ `[stripHexPrefix] input must be type 'string', received ${typeof str}`,
53
+ )
54
+
55
+ return isHexString(str) ? str.slice(2) : str
56
+ }
57
+
58
+ /**
59
+ * Pads a `String` to have an even length
60
+ * @param value
61
+ * @return output
62
+ */
63
+ export function padToEven(value: string): string {
64
+ let a = value
65
+
66
+ if (typeof a !== 'string') {
67
+ throw EthereumJSErrorWithoutCode(
68
+ `[padToEven] value must be type 'string', received ${typeof a}`,
69
+ )
70
+ }
71
+
72
+ if (a.length % 2) a = `0${a}`
73
+
74
+ return a
75
+ }
76
+
77
+ /**
78
+ * Get the binary size of a string
79
+ * @param str
80
+ * @returns the number of bytes contained within the string
81
+ */
82
+ export function getBinarySize(str: string) {
83
+ if (typeof str !== 'string') {
84
+ throw EthereumJSErrorWithoutCode(
85
+ `[getBinarySize] method requires input type 'string', received ${typeof str}`,
86
+ )
87
+ }
88
+
89
+ return utf8ToBytes(str).byteLength
90
+ }
91
+
92
+ /**
93
+ * Returns TRUE if the first specified array contains all elements
94
+ * from the second one. FALSE otherwise.
95
+ *
96
+ * @param superset
97
+ * @param subset
98
+ *
99
+ */
100
+ export function arrayContainsArray(
101
+ superset: unknown[],
102
+ subset: unknown[],
103
+ some?: boolean,
104
+ ): boolean {
105
+ if (Array.isArray(superset) !== true) {
106
+ throw EthereumJSErrorWithoutCode(
107
+ `[arrayContainsArray] method requires input 'superset' to be an array, got type '${typeof superset}'`,
108
+ )
109
+ }
110
+ if (Array.isArray(subset) !== true) {
111
+ throw EthereumJSErrorWithoutCode(
112
+ `[arrayContainsArray] method requires input 'subset' to be an array, got type '${typeof subset}'`,
113
+ )
114
+ }
115
+
116
+ return subset[some === true ? 'some' : 'every']((value) => superset.indexOf(value) >= 0)
117
+ }
118
+
119
+ /**
120
+ * Should be called to get ascii from its hex representation
121
+ *
122
+ * @param string in hex
123
+ * @returns ascii string representation of hex value
124
+ */
125
+ export function toAscii(hex: string): string {
126
+ let str = ''
127
+ let i = 0
128
+ const l = hex.length
129
+
130
+ if (hex.substring(0, 2) === '0x') i = 2
131
+
132
+ for (; i < l; i += 2) {
133
+ const code = parseInt(hex.substr(i, 2), 16)
134
+ str += String.fromCharCode(code)
135
+ }
136
+
137
+ return str
138
+ }
139
+
140
+ /**
141
+ * Should be called to get hex representation (prefixed by 0x) of utf8 string.
142
+ * Strips leading and trailing 0's.
143
+ *
144
+ * @param string
145
+ * @param optional padding
146
+ * @returns hex representation of input string
147
+ */
148
+ export function fromUtf8(stringValue: string) {
149
+ const str = utf8ToBytes(stringValue)
150
+
151
+ // Using deprecated bytesToUnprefixedHex for performance: we're building a hex string with 0x prefix,
152
+ // so using bytesToUnprefixedHex avoids creating an intermediate prefixed string and then stripping it.
153
+ return `0x${padToEven(bytesToUnprefixedHex(str)).replace(/^0+|0+$/g, '')}`
154
+ }
155
+
156
+ /**
157
+ * Should be called to get hex representation (prefixed by 0x) of ascii string
158
+ *
159
+ * @param string
160
+ * @param optional padding
161
+ * @returns hex representation of input string
162
+ */
163
+ export function fromAscii(stringValue: string) {
164
+ let hex = ''
165
+ for (let i = 0; i < stringValue.length; i++) {
166
+ const code = stringValue.charCodeAt(i)
167
+ const n = code.toString(16)
168
+ hex += n.length < 2 ? `0${n}` : n
169
+ }
170
+
171
+ return `0x${hex}`
172
+ }
173
+
174
+ /**
175
+ * Returns the keys from an array of objects.
176
+ * @example
177
+ * ```js
178
+ * getKeys([{a: '1', b: '2'}, {a: '3', b: '4'}], 'a') => ['1', '3']
179
+ *````
180
+ * @param params
181
+ * @param key
182
+ * @param allowEmpty
183
+ * @returns output just a simple array of output keys
184
+ */
185
+ export function getKeys(params: Record<string, string>[], key: string, allowEmpty?: boolean) {
186
+ if (!Array.isArray(params)) {
187
+ throw EthereumJSErrorWithoutCode(
188
+ `[getKeys] method expects input 'params' to be an array, got ${typeof params}`,
189
+ )
190
+ }
191
+ if (typeof key !== 'string') {
192
+ throw EthereumJSErrorWithoutCode(
193
+ `[getKeys] method expects input 'key' to be type 'string', got ${typeof params}`,
194
+ )
195
+ }
196
+
197
+ const result = []
198
+
199
+ for (let i = 0; i < params.length; i++) {
200
+ let value = params[i][key]
201
+ if (allowEmpty === true && !value) {
202
+ value = ''
203
+ } else if (typeof value !== 'string') {
204
+ throw EthereumJSErrorWithoutCode(
205
+ `invalid abi - expected type 'string', received ${typeof value}`,
206
+ )
207
+ }
208
+ result.push(value)
209
+ }
210
+
211
+ return result
212
+ }
package/src/kzg.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Interface for an externally provided kzg library used when creating blob transactions
3
+ */
4
+ export interface KZG {
5
+ // eip-4844
6
+ blobToKzgCommitment(blob: string): string
7
+ computeBlobProof(blob: string, commitment: string): string
8
+ verifyProof(polynomialKZG: string, z: string, y: string, KZGProof: string): boolean
9
+ verifyBlobProofBatch(
10
+ blobs: string[],
11
+ expectedKZGCommitments: string[],
12
+ KZGProofs: string[],
13
+ ): boolean
14
+ // eip-7594
15
+ computeCells(blob: string): string[]
16
+ computeCellsAndProofs(blob: string): [string[], string[]]
17
+ recoverCellsAndProofs(indices: number[], cells: string[]): [string[], string[]]
18
+ verifyCellKzgProofBatch(
19
+ commitments: string[],
20
+ indices: number[],
21
+ cells: string[],
22
+ proofs: string[],
23
+ ): boolean
24
+ }
package/src/lock.ts ADDED
@@ -0,0 +1,42 @@
1
+ // Based on https://github.com/jsoendermann/semaphore-async-await/blob/master/src/Semaphore.ts
2
+ export class Lock {
3
+ private permits: number = 1
4
+ private promiseResolverQueue: Array<(v: boolean) => void> = []
5
+
6
+ /**
7
+ * Returns a promise used to wait for a permit to become available. This method should be awaited on.
8
+ * @returns A promise that gets resolved when execution is allowed to proceed.
9
+ */
10
+ public async acquire(): Promise<boolean> {
11
+ if (this.permits > 0) {
12
+ this.permits -= 1
13
+ return Promise.resolve(true)
14
+ }
15
+
16
+ // If there is no permit available, we return a promise that resolves once the semaphore gets
17
+ // signaled enough times that permits is equal to one.
18
+ return new Promise<boolean>((resolver) => this.promiseResolverQueue.push(resolver))
19
+ }
20
+
21
+ /**
22
+ * Increases the number of permits by one. If there are other functions waiting, one of them will
23
+ * continue to execute in a future iteration of the event loop.
24
+ */
25
+ public release(): void {
26
+ this.permits += 1
27
+
28
+ if (this.permits > 1 && this.promiseResolverQueue.length > 0) {
29
+ // eslint-disable-next-line no-console
30
+ console.warn('Lock.permits should never be > 0 when there is someone waiting.')
31
+ } else if (this.permits === 1 && this.promiseResolverQueue.length > 0) {
32
+ // If there is someone else waiting, immediately consume the permit that was released
33
+ // at the beginning of this function and let the waiting function resume.
34
+ this.permits -= 1
35
+
36
+ const nextResolver = this.promiseResolverQueue.shift()
37
+ if (nextResolver) {
38
+ nextResolver(true)
39
+ }
40
+ }
41
+ }
42
+ }