@aztec/foundation 0.1.0-alpha10

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 (414) hide show
  1. package/.eslintrc.cjs +103 -0
  2. package/.prettierrc.json +6 -0
  3. package/.tsbuildinfo +1 -0
  4. package/README.md +1 -0
  5. package/dest/abi/index.d.ts +137 -0
  6. package/dest/abi/index.d.ts.map +1 -0
  7. package/dest/abi/index.js +18 -0
  8. package/dest/async-map/async_map.test.d.ts +2 -0
  9. package/dest/async-map/async_map.test.d.ts.map +1 -0
  10. package/dest/async-map/async_map.test.js +9 -0
  11. package/dest/async-map/index.d.ts +13 -0
  12. package/dest/async-map/index.d.ts.map +1 -0
  13. package/dest/async-map/index.js +19 -0
  14. package/dest/aztec-address/index.d.ts +96 -0
  15. package/dest/aztec-address/index.d.ts.map +1 -0
  16. package/dest/aztec-address/index.js +124 -0
  17. package/dest/bigint-buffer/bigint-buffer.test.d.ts +2 -0
  18. package/dest/bigint-buffer/bigint-buffer.test.d.ts.map +1 -0
  19. package/dest/bigint-buffer/bigint-buffer.test.js +18 -0
  20. package/dest/bigint-buffer/index.d.ts +35 -0
  21. package/dest/bigint-buffer/index.d.ts.map +1 -0
  22. package/dest/bigint-buffer/index.js +68 -0
  23. package/dest/collection/array.d.ts +30 -0
  24. package/dest/collection/array.d.ts.map +1 -0
  25. package/dest/collection/array.js +47 -0
  26. package/dest/collection/index.d.ts +2 -0
  27. package/dest/collection/index.d.ts.map +1 -0
  28. package/dest/collection/index.js +2 -0
  29. package/dest/crypto/index.d.ts +4 -0
  30. package/dest/crypto/index.d.ts.map +1 -0
  31. package/dest/crypto/index.js +4 -0
  32. package/dest/crypto/keccak/index.d.ts +20 -0
  33. package/dest/crypto/keccak/index.d.ts.map +1 -0
  34. package/dest/crypto/keccak/index.js +31 -0
  35. package/dest/crypto/random/index.d.ts +3 -0
  36. package/dest/crypto/random/index.d.ts.map +1 -0
  37. package/dest/crypto/random/index.js +36 -0
  38. package/dest/crypto/random/index.test.d.ts +2 -0
  39. package/dest/crypto/random/index.test.d.ts.map +1 -0
  40. package/dest/crypto/random/index.test.js +13 -0
  41. package/dest/crypto/sha256/index.d.ts +11 -0
  42. package/dest/crypto/sha256/index.d.ts.map +1 -0
  43. package/dest/crypto/sha256/index.js +14 -0
  44. package/dest/crypto/sha256/index.test.d.ts +2 -0
  45. package/dest/crypto/sha256/index.test.d.ts.map +1 -0
  46. package/dest/crypto/sha256/index.test.js +11 -0
  47. package/dest/errors/index.d.ts +8 -0
  48. package/dest/errors/index.d.ts.map +1 -0
  49. package/dest/errors/index.js +8 -0
  50. package/dest/eth-address/eth_address.test.d.ts +2 -0
  51. package/dest/eth-address/eth_address.test.d.ts.map +1 -0
  52. package/dest/eth-address/eth_address.test.js +87 -0
  53. package/dest/eth-address/index.d.ts +139 -0
  54. package/dest/eth-address/index.d.ts.map +1 -0
  55. package/dest/eth-address/index.js +221 -0
  56. package/dest/fields/coordinate.d.ts +67 -0
  57. package/dest/fields/coordinate.d.ts.map +1 -0
  58. package/dest/fields/coordinate.js +95 -0
  59. package/dest/fields/coordinate.test.d.ts +2 -0
  60. package/dest/fields/coordinate.test.d.ts.map +1 -0
  61. package/dest/fields/coordinate.test.js +22 -0
  62. package/dest/fields/fields.d.ts +170 -0
  63. package/dest/fields/fields.d.ts.map +1 -0
  64. package/dest/fields/fields.js +210 -0
  65. package/dest/fields/index.d.ts +4 -0
  66. package/dest/fields/index.d.ts.map +1 -0
  67. package/dest/fields/index.js +4 -0
  68. package/dest/fields/point.d.ts +111 -0
  69. package/dest/fields/point.d.ts.map +1 -0
  70. package/dest/fields/point.js +133 -0
  71. package/dest/fifo/bounded_serial_queue.d.ts +56 -0
  72. package/dest/fifo/bounded_serial_queue.d.ts.map +1 -0
  73. package/dest/fifo/bounded_serial_queue.js +94 -0
  74. package/dest/fifo/index.d.ts +5 -0
  75. package/dest/fifo/index.d.ts.map +1 -0
  76. package/dest/fifo/index.js +5 -0
  77. package/dest/fifo/memory_fifo.d.ts +55 -0
  78. package/dest/fifo/memory_fifo.d.ts.map +1 -0
  79. package/dest/fifo/memory_fifo.js +109 -0
  80. package/dest/fifo/semaphore.d.ts +23 -0
  81. package/dest/fifo/semaphore.d.ts.map +1 -0
  82. package/dest/fifo/semaphore.js +30 -0
  83. package/dest/fifo/serial_queue.d.ts +48 -0
  84. package/dest/fifo/serial_queue.d.ts.map +1 -0
  85. package/dest/fifo/serial_queue.js +74 -0
  86. package/dest/json-rpc/class_converter.d.ts +109 -0
  87. package/dest/json-rpc/class_converter.d.ts.map +1 -0
  88. package/dest/json-rpc/class_converter.js +85 -0
  89. package/dest/json-rpc/client/index.d.ts +2 -0
  90. package/dest/json-rpc/client/index.d.ts.map +1 -0
  91. package/dest/json-rpc/client/index.js +2 -0
  92. package/dest/json-rpc/client/json_rpc_client.d.ts +22 -0
  93. package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
  94. package/dest/json-rpc/client/json_rpc_client.js +83 -0
  95. package/dest/json-rpc/client/json_rpc_client.test.d.ts +2 -0
  96. package/dest/json-rpc/client/json_rpc_client.test.d.ts.map +1 -0
  97. package/dest/json-rpc/client/json_rpc_client.test.js +20 -0
  98. package/dest/json-rpc/convert.d.ts +22 -0
  99. package/dest/json-rpc/convert.d.ts.map +1 -0
  100. package/dest/json-rpc/convert.js +93 -0
  101. package/dest/json-rpc/convert.test.d.ts +2 -0
  102. package/dest/json-rpc/convert.test.d.ts.map +1 -0
  103. package/dest/json-rpc/convert.test.js +14 -0
  104. package/dest/json-rpc/fixtures/test_state.d.ts +47 -0
  105. package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -0
  106. package/dest/json-rpc/fixtures/test_state.js +62 -0
  107. package/dest/json-rpc/index.d.ts +4 -0
  108. package/dest/json-rpc/index.d.ts.map +1 -0
  109. package/dest/json-rpc/index.js +4 -0
  110. package/dest/json-rpc/js_utils.d.ts +13 -0
  111. package/dest/json-rpc/js_utils.d.ts.map +1 -0
  112. package/dest/json-rpc/js_utils.js +18 -0
  113. package/dest/json-rpc/server/index.d.ts +3 -0
  114. package/dest/json-rpc/server/index.d.ts.map +1 -0
  115. package/dest/json-rpc/server/index.js +3 -0
  116. package/dest/json-rpc/server/json_proxy.d.ts +18 -0
  117. package/dest/json-rpc/server/json_proxy.d.ts.map +1 -0
  118. package/dest/json-rpc/server/json_proxy.js +35 -0
  119. package/dest/json-rpc/server/json_rpc_server.d.ts +34 -0
  120. package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -0
  121. package/dest/json-rpc/server/json_rpc_server.js +109 -0
  122. package/dest/json-rpc/server/json_rpc_server.test.d.ts +2 -0
  123. package/dest/json-rpc/server/json_rpc_server.test.d.ts.map +1 -0
  124. package/dest/json-rpc/server/json_rpc_server.test.js +22 -0
  125. package/dest/log/console.d.ts +16 -0
  126. package/dest/log/console.d.ts.map +1 -0
  127. package/dest/log/console.js +39 -0
  128. package/dest/log/debug.d.ts +46 -0
  129. package/dest/log/debug.d.ts.map +1 -0
  130. package/dest/log/debug.js +75 -0
  131. package/dest/log/index.d.ts +4 -0
  132. package/dest/log/index.d.ts.map +1 -0
  133. package/dest/log/index.js +4 -0
  134. package/dest/log/log_history.d.ts +31 -0
  135. package/dest/log/log_history.d.ts.map +1 -0
  136. package/dest/log/log_history.js +42 -0
  137. package/dest/log/log_history.test.d.ts +2 -0
  138. package/dest/log/log_history.test.d.ts.map +1 -0
  139. package/dest/log/log_history.test.js +78 -0
  140. package/dest/mutex/index.d.ts +53 -0
  141. package/dest/mutex/index.d.ts.map +1 -0
  142. package/dest/mutex/index.js +74 -0
  143. package/dest/mutex/mutex.test.d.ts +9 -0
  144. package/dest/mutex/mutex.test.d.ts.map +1 -0
  145. package/dest/mutex/mutex.test.js +58 -0
  146. package/dest/mutex/mutex_database.d.ts +10 -0
  147. package/dest/mutex/mutex_database.d.ts.map +1 -0
  148. package/dest/mutex/mutex_database.js +2 -0
  149. package/dest/retry/index.d.ts +33 -0
  150. package/dest/retry/index.d.ts.map +1 -0
  151. package/dest/retry/index.js +70 -0
  152. package/dest/running-promise/index.d.ts +35 -0
  153. package/dest/running-promise/index.d.ts.map +1 -0
  154. package/dest/running-promise/index.js +59 -0
  155. package/dest/serialize/buffer_reader.d.ts +188 -0
  156. package/dest/serialize/buffer_reader.d.ts.map +1 -0
  157. package/dest/serialize/buffer_reader.js +229 -0
  158. package/dest/serialize/buffer_reader.test.d.ts +2 -0
  159. package/dest/serialize/buffer_reader.test.d.ts.map +1 -0
  160. package/dest/serialize/buffer_reader.test.js +156 -0
  161. package/dest/serialize/deserializer.d.ts +134 -0
  162. package/dest/serialize/deserializer.d.ts.map +1 -0
  163. package/dest/serialize/deserializer.js +145 -0
  164. package/dest/serialize/free_funcs.d.ts +203 -0
  165. package/dest/serialize/free_funcs.d.ts.map +1 -0
  166. package/dest/serialize/free_funcs.js +250 -0
  167. package/dest/serialize/index.d.ts +6 -0
  168. package/dest/serialize/index.d.ts.map +1 -0
  169. package/dest/serialize/index.js +6 -0
  170. package/dest/serialize/serialize.test.d.ts +2 -0
  171. package/dest/serialize/serialize.test.d.ts.map +1 -0
  172. package/dest/serialize/serialize.test.js +68 -0
  173. package/dest/serialize/serializer.d.ts +89 -0
  174. package/dest/serialize/serializer.d.ts.map +1 -0
  175. package/dest/serialize/serializer.js +111 -0
  176. package/dest/serialize/types.d.ts +33 -0
  177. package/dest/serialize/types.d.ts.map +1 -0
  178. package/dest/serialize/types.js +22 -0
  179. package/dest/sleep/index.d.ts +52 -0
  180. package/dest/sleep/index.d.ts.map +1 -0
  181. package/dest/sleep/index.js +70 -0
  182. package/dest/timer/index.d.ts +3 -0
  183. package/dest/timer/index.d.ts.map +1 -0
  184. package/dest/timer/index.js +3 -0
  185. package/dest/timer/timeout.d.ts +41 -0
  186. package/dest/timer/timeout.d.ts.map +1 -0
  187. package/dest/timer/timeout.js +62 -0
  188. package/dest/timer/timer.d.ts +33 -0
  189. package/dest/timer/timer.d.ts.map +1 -0
  190. package/dest/timer/timer.js +38 -0
  191. package/dest/transport/browser/index.d.ts +5 -0
  192. package/dest/transport/browser/index.d.ts.map +1 -0
  193. package/dest/transport/browser/index.js +5 -0
  194. package/dest/transport/browser/message_port_socket.d.ts +37 -0
  195. package/dest/transport/browser/message_port_socket.d.ts.map +1 -0
  196. package/dest/transport/browser/message_port_socket.js +46 -0
  197. package/dest/transport/browser/shared_worker_connector.d.ts +19 -0
  198. package/dest/transport/browser/shared_worker_connector.d.ts.map +1 -0
  199. package/dest/transport/browser/shared_worker_connector.js +21 -0
  200. package/dest/transport/browser/shared_worker_listener.d.ts +38 -0
  201. package/dest/transport/browser/shared_worker_listener.d.ts.map +1 -0
  202. package/dest/transport/browser/shared_worker_listener.js +37 -0
  203. package/dest/transport/browser/worker_connector.d.ts +26 -0
  204. package/dest/transport/browser/worker_connector.d.ts.map +1 -0
  205. package/dest/transport/browser/worker_connector.js +30 -0
  206. package/dest/transport/browser/worker_listener.d.ts +39 -0
  207. package/dest/transport/browser/worker_listener.d.ts.map +1 -0
  208. package/dest/transport/browser/worker_listener.js +39 -0
  209. package/dest/transport/dispatch/create_dispatch_fn.d.ts +25 -0
  210. package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -0
  211. package/dest/transport/dispatch/create_dispatch_fn.js +18 -0
  212. package/dest/transport/dispatch/create_dispatch_proxy.d.ts +102 -0
  213. package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -0
  214. package/dest/transport/dispatch/create_dispatch_proxy.js +56 -0
  215. package/dest/transport/dispatch/messages.d.ts +52 -0
  216. package/dest/transport/dispatch/messages.d.ts.map +1 -0
  217. package/dest/transport/dispatch/messages.js +12 -0
  218. package/dest/transport/index.d.ts +12 -0
  219. package/dest/transport/index.d.ts.map +1 -0
  220. package/dest/transport/index.js +12 -0
  221. package/dest/transport/interface/connector.d.ts +8 -0
  222. package/dest/transport/interface/connector.d.ts.map +1 -0
  223. package/dest/transport/interface/connector.js +2 -0
  224. package/dest/transport/interface/listener.d.ts +13 -0
  225. package/dest/transport/interface/listener.d.ts.map +1 -0
  226. package/dest/transport/interface/listener.js +2 -0
  227. package/dest/transport/interface/socket.d.ts +13 -0
  228. package/dest/transport/interface/socket.d.ts.map +1 -0
  229. package/dest/transport/interface/socket.js +2 -0
  230. package/dest/transport/interface/transferable.d.ts +68 -0
  231. package/dest/transport/interface/transferable.d.ts.map +1 -0
  232. package/dest/transport/interface/transferable.js +63 -0
  233. package/dest/transport/node/index.d.ts +3 -0
  234. package/dest/transport/node/index.d.ts.map +1 -0
  235. package/dest/transport/node/index.js +3 -0
  236. package/dest/transport/node/node_connector.d.ts +28 -0
  237. package/dest/transport/node/node_connector.d.ts.map +1 -0
  238. package/dest/transport/node/node_connector.js +28 -0
  239. package/dest/transport/node/node_connector_socket.d.ts +42 -0
  240. package/dest/transport/node/node_connector_socket.d.ts.map +1 -0
  241. package/dest/transport/node/node_connector_socket.js +48 -0
  242. package/dest/transport/node/node_listener.d.ts +26 -0
  243. package/dest/transport/node/node_listener.d.ts.map +1 -0
  244. package/dest/transport/node/node_listener.js +30 -0
  245. package/dest/transport/node/node_listener_socket.d.ts +37 -0
  246. package/dest/transport/node/node_listener_socket.d.ts.map +1 -0
  247. package/dest/transport/node/node_listener_socket.js +44 -0
  248. package/dest/transport/transport_client.d.ts +61 -0
  249. package/dest/transport/transport_client.d.ts.map +1 -0
  250. package/dest/transport/transport_client.js +94 -0
  251. package/dest/transport/transport_server.d.ts +56 -0
  252. package/dest/transport/transport_server.d.ts.map +1 -0
  253. package/dest/transport/transport_server.js +101 -0
  254. package/dest/types/index.d.ts +7 -0
  255. package/dest/types/index.d.ts.map +1 -0
  256. package/dest/types/index.js +2 -0
  257. package/dest/wasm/index.d.ts +5 -0
  258. package/dest/wasm/index.d.ts.map +1 -0
  259. package/dest/wasm/index.js +5 -0
  260. package/dest/wasm/wasm/empty_wasi_sdk.d.ts +130 -0
  261. package/dest/wasm/wasm/empty_wasi_sdk.d.ts.map +1 -0
  262. package/dest/wasm/wasm/empty_wasi_sdk.js +166 -0
  263. package/dest/wasm/wasm/index.d.ts +2 -0
  264. package/dest/wasm/wasm/index.d.ts.map +1 -0
  265. package/dest/wasm/wasm/index.js +2 -0
  266. package/dest/wasm/wasm/wasm_module.d.ts +135 -0
  267. package/dest/wasm/wasm/wasm_module.d.ts.map +1 -0
  268. package/dest/wasm/wasm/wasm_module.js +205 -0
  269. package/dest/wasm/wasm/wasm_module.test.d.ts +2 -0
  270. package/dest/wasm/wasm/wasm_module.test.d.ts.map +1 -0
  271. package/dest/wasm/wasm/wasm_module.test.js +24 -0
  272. package/dest/wasm/worker/browser/index.d.ts +3 -0
  273. package/dest/wasm/worker/browser/index.d.ts.map +1 -0
  274. package/dest/wasm/worker/browser/index.js +3 -0
  275. package/dest/wasm/worker/browser/start_web_module.d.ts +7 -0
  276. package/dest/wasm/worker/browser/start_web_module.d.ts.map +1 -0
  277. package/dest/wasm/worker/browser/start_web_module.js +22 -0
  278. package/dest/wasm/worker/browser/web_data_store.d.ts +23 -0
  279. package/dest/wasm/worker/browser/web_data_store.d.ts.map +1 -0
  280. package/dest/wasm/worker/browser/web_data_store.js +32 -0
  281. package/dest/wasm/worker/browser/web_worker.d.ts +10 -0
  282. package/dest/wasm/worker/browser/web_worker.d.ts.map +1 -0
  283. package/dest/wasm/worker/browser/web_worker.js +23 -0
  284. package/dest/wasm/worker/data_store.d.ts +9 -0
  285. package/dest/wasm/worker/data_store.d.ts.map +1 -0
  286. package/dest/wasm/worker/data_store.js +2 -0
  287. package/dest/wasm/worker/index.d.ts +3 -0
  288. package/dest/wasm/worker/index.d.ts.map +1 -0
  289. package/dest/wasm/worker/index.js +2 -0
  290. package/dest/wasm/worker/node/index.d.ts +3 -0
  291. package/dest/wasm/worker/node/index.d.ts.map +1 -0
  292. package/dest/wasm/worker/node/index.js +3 -0
  293. package/dest/wasm/worker/node/node_data_store.d.ts +22 -0
  294. package/dest/wasm/worker/node/node_data_store.d.ts.map +1 -0
  295. package/dest/wasm/worker/node/node_data_store.js +31 -0
  296. package/dest/wasm/worker/node/node_worker.d.ts +6 -0
  297. package/dest/wasm/worker/node/node_worker.d.ts.map +1 -0
  298. package/dest/wasm/worker/node/node_worker.js +20 -0
  299. package/dest/wasm/worker/node/start_node_module.d.ts +7 -0
  300. package/dest/wasm/worker/node/start_node_module.d.ts.map +1 -0
  301. package/dest/wasm/worker/node/start_node_module.js +26 -0
  302. package/dest/wasm/worker/wasm_worker.d.ts +9 -0
  303. package/dest/wasm/worker/wasm_worker.d.ts.map +1 -0
  304. package/dest/wasm/worker/wasm_worker.js +2 -0
  305. package/dest/wasm/worker/worker_pool.d.ts +40 -0
  306. package/dest/wasm/worker/worker_pool.d.ts.map +1 -0
  307. package/dest/wasm/worker/worker_pool.js +62 -0
  308. package/package.json +44 -0
  309. package/src/abi/index.ts +147 -0
  310. package/src/async-map/async_map.test.ts +9 -0
  311. package/src/async-map/index.ts +18 -0
  312. package/src/aztec-address/index.ts +135 -0
  313. package/src/bigint-buffer/bigint-buffer.test.ts +21 -0
  314. package/src/bigint-buffer/index.ts +68 -0
  315. package/src/collection/array.ts +48 -0
  316. package/src/collection/index.ts +1 -0
  317. package/src/crypto/index.ts +3 -0
  318. package/src/crypto/keccak/index.ts +33 -0
  319. package/src/crypto/random/index.test.ts +13 -0
  320. package/src/crypto/random/index.ts +38 -0
  321. package/src/crypto/sha256/index.test.ts +13 -0
  322. package/src/crypto/sha256/index.ts +15 -0
  323. package/src/errors/index.ts +6 -0
  324. package/src/eth-address/eth_address.test.ts +104 -0
  325. package/src/eth-address/index.ts +236 -0
  326. package/src/fields/coordinate.test.ts +26 -0
  327. package/src/fields/coordinate.ts +104 -0
  328. package/src/fields/fields.ts +234 -0
  329. package/src/fields/index.ts +3 -0
  330. package/src/fields/point.ts +143 -0
  331. package/src/fifo/bounded_serial_queue.ts +100 -0
  332. package/src/fifo/index.ts +4 -0
  333. package/src/fifo/memory_fifo.ts +114 -0
  334. package/src/fifo/semaphore.ts +33 -0
  335. package/src/fifo/serial_queue.ts +78 -0
  336. package/src/json-rpc/README.md +55 -0
  337. package/src/json-rpc/class_converter.ts +160 -0
  338. package/src/json-rpc/client/index.ts +1 -0
  339. package/src/json-rpc/client/json_rpc_client.test.ts +20 -0
  340. package/src/json-rpc/client/json_rpc_client.ts +103 -0
  341. package/src/json-rpc/convert.test.ts +15 -0
  342. package/src/json-rpc/convert.ts +109 -0
  343. package/src/json-rpc/fixtures/test_state.ts +59 -0
  344. package/src/json-rpc/index.ts +3 -0
  345. package/src/json-rpc/js_utils.ts +20 -0
  346. package/src/json-rpc/server/index.ts +2 -0
  347. package/src/json-rpc/server/json_proxy.ts +36 -0
  348. package/src/json-rpc/server/json_rpc_server.test.ts +23 -0
  349. package/src/json-rpc/server/json_rpc_server.ts +124 -0
  350. package/src/log/console.ts +45 -0
  351. package/src/log/debug.ts +86 -0
  352. package/src/log/index.ts +3 -0
  353. package/src/log/log_history.test.ts +87 -0
  354. package/src/log/log_history.ts +44 -0
  355. package/src/mutex/index.ts +83 -0
  356. package/src/mutex/mutex.test.ts +75 -0
  357. package/src/mutex/mutex_database.ts +9 -0
  358. package/src/retry/index.ts +78 -0
  359. package/src/running-promise/index.ts +60 -0
  360. package/src/serialize/buffer_reader.test.ts +175 -0
  361. package/src/serialize/buffer_reader.ts +272 -0
  362. package/src/serialize/deserializer.ts +180 -0
  363. package/src/serialize/free_funcs.ts +285 -0
  364. package/src/serialize/index.ts +5 -0
  365. package/src/serialize/serialize.test.ts +89 -0
  366. package/src/serialize/serializer.ts +128 -0
  367. package/src/serialize/types.ts +40 -0
  368. package/src/sleep/index.ts +71 -0
  369. package/src/timer/index.ts +2 -0
  370. package/src/timer/timeout.ts +64 -0
  371. package/src/timer/timer.ts +41 -0
  372. package/src/transport/browser/index.ts +4 -0
  373. package/src/transport/browser/message_port_socket.ts +48 -0
  374. package/src/transport/browser/shared_worker_connector.ts +21 -0
  375. package/src/transport/browser/shared_worker_listener.ts +52 -0
  376. package/src/transport/browser/worker_connector.ts +30 -0
  377. package/src/transport/browser/worker_listener.ts +53 -0
  378. package/src/transport/dispatch/create_dispatch_fn.ts +33 -0
  379. package/src/transport/dispatch/create_dispatch_proxy.ts +140 -0
  380. package/src/transport/dispatch/messages.ts +58 -0
  381. package/src/transport/index.ts +11 -0
  382. package/src/transport/interface/connector.ts +8 -0
  383. package/src/transport/interface/listener.ts +14 -0
  384. package/src/transport/interface/socket.ts +12 -0
  385. package/src/transport/interface/transferable.ts +121 -0
  386. package/src/transport/node/index.ts +2 -0
  387. package/src/transport/node/node_connector.ts +29 -0
  388. package/src/transport/node/node_connector_socket.ts +51 -0
  389. package/src/transport/node/node_listener.ts +33 -0
  390. package/src/transport/node/node_listener_socket.ts +47 -0
  391. package/src/transport/transport_client.ts +125 -0
  392. package/src/transport/transport_server.ts +108 -0
  393. package/src/types/index.ts +7 -0
  394. package/src/wasm/README.md +6 -0
  395. package/src/wasm/fixtures/gcd.wasm +0 -0
  396. package/src/wasm/fixtures/gcd.wat +27 -0
  397. package/src/wasm/index.ts +4 -0
  398. package/src/wasm/wasm/empty_wasi_sdk.ts +166 -0
  399. package/src/wasm/wasm/index.ts +1 -0
  400. package/src/wasm/wasm/wasm_module.test.ts +27 -0
  401. package/src/wasm/wasm/wasm_module.ts +257 -0
  402. package/src/wasm/worker/browser/index.ts +2 -0
  403. package/src/wasm/worker/browser/start_web_module.ts +23 -0
  404. package/src/wasm/worker/browser/web_data_store.ts +37 -0
  405. package/src/wasm/worker/browser/web_worker.ts +24 -0
  406. package/src/wasm/worker/data_store.ts +7 -0
  407. package/src/wasm/worker/index.ts +2 -0
  408. package/src/wasm/worker/node/index.ts +2 -0
  409. package/src/wasm/worker/node/node_data_store.ts +36 -0
  410. package/src/wasm/worker/node/node_worker.ts +21 -0
  411. package/src/wasm/worker/node/start_node_module.ts +28 -0
  412. package/src/wasm/worker/wasm_worker.ts +7 -0
  413. package/src/wasm/worker/worker_pool.ts +73 -0
  414. package/tsconfig.json +10 -0
@@ -0,0 +1,40 @@
1
+ import { WasmWorker } from './wasm_worker.js';
2
+ /**
3
+ * Type of a worker factory.
4
+ * Used to customize WorkerPool worker construction.
5
+ */
6
+ export type CreateWorker = (name: string, minMem: number, maxMem: number) => WasmWorker;
7
+ /**
8
+ * Allocates a pool of WasmWorker's.
9
+ * Worker 0 is allocated MAX_PAGES memory pages. This is because worker 0 will need to hold the proving key
10
+ * (i.e. Has state), whereas the others are pure compute (they hold a little crs state).
11
+ */
12
+ export declare class WorkerPool {
13
+ /**
14
+ * The maximum number of memory pages to be used by the webassembly.
15
+ */
16
+ static MAX_PAGES: number;
17
+ /**
18
+ * The workers in the pool.
19
+ */
20
+ private workers;
21
+ /**
22
+ * Create an instance and initialize the workers.
23
+ * @param createWorker - Worker factory.
24
+ * @param poolSize - Pool size.
25
+ * @returns An initialized WorkerPool.
26
+ */
27
+ static new(createWorker: CreateWorker, poolSize: number): Promise<WorkerPool>;
28
+ /**
29
+ * Initialize the workers.
30
+ * @param createWorker - Worker factory().
31
+ * @param poolSize - Pool size.
32
+ * @param maxMem - Max memory pages.
33
+ */
34
+ init(createWorker: CreateWorker, poolSize: number, maxMem?: number): Promise<void>;
35
+ /**
36
+ * Tell all workers in the pool to stop processing.
37
+ */
38
+ destroy(): Promise<void>;
39
+ }
40
+ //# sourceMappingURL=worker_pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"worker_pool.d.ts","sourceRoot":"","sources":["../../../src/wasm/worker/worker_pool.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAI9C;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,UAAU,CAAC;AACxF;;;;GAIG;AACH,qBAAa,UAAU;IAYrB;;OAEG;IACH,OAAc,SAAS,SAAQ;IAC/B;;OAEG;IACH,OAAO,CAAC,OAAO,CAAoB;IAEnC;;;;;OAKG;WACU,GAAG,CAAC,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM;IAM7D;;;;;OAKG;IACU,IAAI,CAAC,YAAY,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,SAAuB;IAY7F;;OAEG;IACU,OAAO;CAGrB"}
@@ -0,0 +1,62 @@
1
+ import { createDebugLogger } from '../../log/index.js';
2
+ const debug = createDebugLogger('bb:worker_pool');
3
+ /**
4
+ * Allocates a pool of WasmWorker's.
5
+ * Worker 0 is allocated MAX_PAGES memory pages. This is because worker 0 will need to hold the proving key
6
+ * (i.e. Has state), whereas the others are pure compute (they hold a little crs state).
7
+ */
8
+ export class WorkerPool {
9
+ constructor() {
10
+ /**
11
+ * The workers in the pool.
12
+ */
13
+ this.workers = [];
14
+ }
15
+ /**
16
+ * Create an instance and initialize the workers.
17
+ * @param createWorker - Worker factory.
18
+ * @param poolSize - Pool size.
19
+ * @returns An initialized WorkerPool.
20
+ */
21
+ static async new(createWorker, poolSize) {
22
+ const pool = new WorkerPool();
23
+ await pool.init(createWorker, poolSize);
24
+ return pool;
25
+ }
26
+ /**
27
+ * Initialize the workers.
28
+ * @param createWorker - Worker factory().
29
+ * @param poolSize - Pool size.
30
+ * @param maxMem - Max memory pages.
31
+ */
32
+ async init(createWorker, poolSize, maxMem = WorkerPool.MAX_PAGES) {
33
+ debug(`creating ${poolSize} workers...`);
34
+ const start = new Date().getTime();
35
+ this.workers = await Promise.all(Array(poolSize)
36
+ .fill(0)
37
+ .map((_, i) => createWorker(`${i}`, i === 0 ? Math.min(WorkerPool.MAX_PAGES, maxMem) : 768, maxMem)));
38
+ debug(`created workers: ${new Date().getTime() - start}ms`);
39
+ }
40
+ /**
41
+ * Tell all workers in the pool to stop processing.
42
+ */
43
+ async destroy() {
44
+ await Promise.all(this.workers.map(w => w.destroyWorker()));
45
+ }
46
+ }
47
+ // TODO(AD): Revisit what this means in aztec 3 context
48
+ // --
49
+ // Introduction of low mem prover work (polynomial cache) may actually increase mem usage when the backing store isn't
50
+ // enabled. We were seeing intermittent failings related to memory in production for some users when limiting to
51
+ // 6660 (416MB). It would be nice to understand why this is (the non determinism and/or the increased mem usage).
52
+ // For now, increasing mem usage to 512MB. This maybe preferable to backing out the low mem work, but
53
+ // ironically may break the chance of us using it in mobile.
54
+ // We *could* enable the low memory backing store, but this needs a little bit of work to actually
55
+ // read/write from indexeddb, performance testing, and actual further memory load testing.
56
+ // At this point it's hard to know what our memory savings would be relative to just fully reverting the LMP.
57
+ // public static MAX_PAGES = 6660;
58
+ /**
59
+ * The maximum number of memory pages to be used by the webassembly.
60
+ */
61
+ WorkerPool.MAX_PAGES = 8192;
62
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid29ya2VyX3Bvb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi9zcmMvd2FzbS93b3JrZXIvd29ya2VyX3Bvb2wudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLGlCQUFpQixFQUFFLE1BQU0sb0JBQW9CLENBQUM7QUFHdkQsTUFBTSxLQUFLLEdBQUcsaUJBQWlCLENBQUMsZ0JBQWdCLENBQUMsQ0FBQztBQU9sRDs7OztHQUlHO0FBQ0gsTUFBTSxPQUFPLFVBQVU7SUFBdkI7UUFnQkU7O1dBRUc7UUFDSyxZQUFPLEdBQWlCLEVBQUUsQ0FBQztJQXNDckMsQ0FBQztJQXBDQzs7Ozs7T0FLRztJQUNILE1BQU0sQ0FBQyxLQUFLLENBQUMsR0FBRyxDQUFDLFlBQTBCLEVBQUUsUUFBZ0I7UUFDM0QsTUFBTSxJQUFJLEdBQUcsSUFBSSxVQUFVLEVBQUUsQ0FBQztRQUM5QixNQUFNLElBQUksQ0FBQyxJQUFJLENBQUMsWUFBWSxFQUFFLFFBQVEsQ0FBQyxDQUFDO1FBQ3hDLE9BQU8sSUFBSSxDQUFDO0lBQ2QsQ0FBQztJQUVEOzs7OztPQUtHO0lBQ0ksS0FBSyxDQUFDLElBQUksQ0FBQyxZQUEwQixFQUFFLFFBQWdCLEVBQUUsTUFBTSxHQUFHLFVBQVUsQ0FBQyxTQUFTO1FBQzNGLEtBQUssQ0FBQyxZQUFZLFFBQVEsYUFBYSxDQUFDLENBQUM7UUFDekMsTUFBTSxLQUFLLEdBQUcsSUFBSSxJQUFJLEVBQUUsQ0FBQyxPQUFPLEVBQUUsQ0FBQztRQUNuQyxJQUFJLENBQUMsT0FBTyxHQUFHLE1BQU0sT0FBTyxDQUFDLEdBQUcsQ0FDOUIsS0FBSyxDQUFDLFFBQVEsQ0FBQzthQUNaLElBQUksQ0FBQyxDQUFDLENBQUM7YUFDUCxHQUFHLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxFQUFFLEVBQUUsQ0FBQyxZQUFZLENBQUMsR0FBRyxDQUFDLEVBQUUsRUFBRSxDQUFDLEtBQUssQ0FBQyxDQUFDLENBQUMsQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLFVBQVUsQ0FBQyxTQUFTLEVBQUUsTUFBTSxDQUFDLENBQUMsQ0FBQyxDQUFDLEdBQUcsRUFBRSxNQUFNLENBQUMsQ0FBQyxDQUN2RyxDQUFDO1FBRUYsS0FBSyxDQUFDLG9CQUFvQixJQUFJLElBQUksRUFBRSxDQUFDLE9BQU8sRUFBRSxHQUFHLEtBQUssSUFBSSxDQUFDLENBQUM7SUFDOUQsQ0FBQztJQUVEOztPQUVHO0lBQ0ksS0FBSyxDQUFDLE9BQU87UUFDbEIsTUFBTSxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsQ0FBQyxDQUFDLGFBQWEsRUFBRSxDQUFDLENBQUMsQ0FBQztJQUM5RCxDQUFDOztBQXZERCx1REFBdUQ7QUFDdkQsS0FBSztBQUNMLHNIQUFzSDtBQUN0SCxnSEFBZ0g7QUFDaEgsaUhBQWlIO0FBQ2pILHFHQUFxRztBQUNyRyw0REFBNEQ7QUFDNUQsa0dBQWtHO0FBQ2xHLDBGQUEwRjtBQUMxRiw2R0FBNkc7QUFDN0csa0NBQWtDO0FBQ2xDOztHQUVHO0FBQ1csb0JBQVMsR0FBRyxJQUFJLEFBQVAsQ0FBUSJ9
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@aztec/foundation",
3
+ "version": "0.1.0-alpha10",
4
+ "exports": {
5
+ "./eslint": "./.eslintrc.cjs",
6
+ "./prettier": "./.prettierrc.json",
7
+ "./abi": "./dest/abi/index.js",
8
+ "./async-map": "./dest/async-map/index.js",
9
+ "./aztec-address": "./dest/aztec-address/index.js",
10
+ "./collection": "./dest/collection/index.js",
11
+ "./crypto": "./dest/crypto/index.js",
12
+ "./error": "./dest/error/index.js",
13
+ "./eth-address": "./dest/eth-address/index.js",
14
+ "./fifo": "./dest/fifo/index.js",
15
+ "./json-rpc": "./dest/json-rpc/index.js",
16
+ "./log": "./dest/log/index.js",
17
+ "./mutex": "./dest/mutex/index.js",
18
+ "./fields": "./dest/fields/index.js",
19
+ "./retry": "./dest/retry/index.js",
20
+ "./running-promise": "./dest/running-promise/index.js",
21
+ "./serialize": "./dest/serialize/index.js",
22
+ "./sleep": "./dest/sleep/index.js",
23
+ "./timer": "./dest/timer/index.js",
24
+ "./transport": "./dest/transport/index.js",
25
+ "./wasm": "./dest/wasm/index.js",
26
+ "./bigint-buffer": "./dest/bigint-buffer/index.js",
27
+ "./types": "./dest/types/index.js"
28
+ },
29
+ "type": "module",
30
+ "dependencies": {
31
+ "@koa/cors": "^4.0.0",
32
+ "debug": "^4.3.4",
33
+ "detect-node": "^2.1.0",
34
+ "hash.js": "^1.1.7",
35
+ "koa": "^2.14.1",
36
+ "koa-bodyparser": "^4.4.0",
37
+ "koa-compress": "^5.1.0",
38
+ "koa-router": "^12.0.0",
39
+ "leveldown": "^6.1.1",
40
+ "levelup": "^5.1.1",
41
+ "memdown": "^6.1.1",
42
+ "sha3": "^2.1.4"
43
+ }
44
+ }
@@ -0,0 +1,147 @@
1
+ /**
2
+ * A named type.
3
+ */
4
+ export interface ABIVariable {
5
+ /**
6
+ * The name of the variable.
7
+ */
8
+ name: string;
9
+ /**
10
+ * The type of the variable.
11
+ */
12
+ type: ABIType;
13
+ }
14
+
15
+ /**
16
+ * Indicates whether a parameter is public or secret/private.
17
+ */
18
+ export enum ABIParameterVisibility {
19
+ PUBLIC = 'public',
20
+ SECRET = 'secret',
21
+ }
22
+
23
+ /**
24
+ * A function parameter.
25
+ */
26
+ export interface ABIParameter extends ABIVariable {
27
+ /**
28
+ * Indicates whether a parameter is public or secret/private.
29
+ */
30
+ visibility: ABIParameterVisibility;
31
+ }
32
+
33
+ /**
34
+ * A basic type.
35
+ */
36
+ export interface BasicType<T extends string> {
37
+ /**
38
+ * The kind of the type.
39
+ */
40
+ kind: T;
41
+ }
42
+
43
+ /**
44
+ * A variable type.
45
+ */
46
+ export type ABIType = BasicType<'field'> | BasicType<'boolean'> | IntegerType | ArrayType | StringType | StructType;
47
+
48
+ /**
49
+ * An integer type.
50
+ */
51
+ export interface IntegerType extends BasicType<'integer'> {
52
+ /**
53
+ * The sign of the integer.
54
+ */
55
+ sign: string;
56
+ /**
57
+ * The width of the integer in bits.
58
+ */
59
+ width: number;
60
+ }
61
+
62
+ /**
63
+ * An array type.
64
+ */
65
+ export interface ArrayType extends BasicType<'array'> {
66
+ /**
67
+ * The length of the array.
68
+ */
69
+ length: number;
70
+ /**
71
+ * The type of the array elements.
72
+ */
73
+ type: ABIType;
74
+ }
75
+
76
+ /**
77
+ * A string type.
78
+ */
79
+ export interface StringType extends BasicType<'string'> {
80
+ /**
81
+ * The length of the string.
82
+ */
83
+ length: number;
84
+ }
85
+
86
+ /**
87
+ * A struct type.
88
+ */
89
+ export interface StructType extends BasicType<'struct'> {
90
+ /**
91
+ * The fields of the struct.
92
+ */
93
+ fields: ABIVariable[];
94
+ }
95
+
96
+ /**
97
+ * Noir function types.
98
+ */
99
+ export enum FunctionType {
100
+ SECRET = 'secret',
101
+ OPEN = 'open',
102
+ UNCONSTRAINED = 'unconstrained',
103
+ }
104
+
105
+ /**
106
+ * The ABI entry of a function.
107
+ */
108
+ export interface FunctionAbi {
109
+ /**
110
+ * The name of the function.
111
+ */
112
+ name: string;
113
+ /**
114
+ * Whether the function is secret.
115
+ */
116
+ functionType: FunctionType;
117
+ /**
118
+ * Function parameters.
119
+ */
120
+ parameters: ABIParameter[];
121
+ /**
122
+ * The types of the return values.
123
+ */
124
+ returnTypes: ABIType[];
125
+ /**
126
+ * The ACIR bytecode of the function.
127
+ */
128
+ bytecode: string;
129
+ /**
130
+ * The verification key of the function.
131
+ */
132
+ verificationKey?: string;
133
+ }
134
+
135
+ /**
136
+ * Defines ABI of a contract.
137
+ */
138
+ export interface ContractAbi {
139
+ /**
140
+ * The name of the contract.
141
+ */
142
+ name: string;
143
+ /**
144
+ * The functions of the contract.
145
+ */
146
+ functions: FunctionAbi[];
147
+ }
@@ -0,0 +1,9 @@
1
+ import { asyncMap } from './index.js';
2
+
3
+ describe('asyncMap', () => {
4
+ test('execute list item sequentially', async () => {
5
+ const sleepAndLog = (ms: number, idx: number) => new Promise(resolve => setTimeout(() => resolve(idx), ms));
6
+ const result = await asyncMap([100, 0, 30, 1], (ms, i) => sleepAndLog(ms, i));
7
+ expect(result).toEqual([0, 1, 2, 3]);
8
+ });
9
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Maps an array of elements by applying an asynchronous function to each element in sequence,
3
+ * and returns a new array with the results. The function receives each element of the array
4
+ * and its index as arguments, and should return a Promise that resolves to the desired value.
5
+ *
6
+ * @typeParam T - The original array element type.
7
+ * @typeParam U - The resulting array element type.
8
+ * @param arr - The array to map.
9
+ * @param fn - The async function to apply on each element of the array.
10
+ * @returns A Promise that resolves to a new array containing the mapped values.
11
+ */
12
+ export async function asyncMap<T, U>(arr: T[], fn: (e: T, i: number) => Promise<U>): Promise<U[]> {
13
+ const results: U[] = [];
14
+ for (let i = 0; i < arr.length; ++i) {
15
+ results.push(await fn(arr[i], i));
16
+ }
17
+ return results;
18
+ }
@@ -0,0 +1,135 @@
1
+ import { toBigIntBE, toBufferBE } from '../bigint-buffer/index.js';
2
+ import { Fr } from '../fields/index.js';
3
+ import { BufferReader } from '../serialize/buffer_reader.js';
4
+
5
+ /**
6
+ * AztecAddress represents a 32-byte address in the Aztec Protocol. It provides methods to create, manipulate, and
7
+ * compare addresses. The maximum value of an address is determined by the field modulus and all instances of AztecAddress
8
+ * should have a value less than or equal to this max value. This class also provides helper functions to convert
9
+ * addresses from strings, buffers, and other formats.
10
+ */
11
+ export class AztecAddress {
12
+ static SIZE_IN_BYTES = 32;
13
+ static ZERO = new AztecAddress(Buffer.alloc(AztecAddress.SIZE_IN_BYTES));
14
+ static MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001n;
15
+ static MAX_VALUE = AztecAddress.MODULUS - 1n;
16
+
17
+ constructor(
18
+ /**
19
+ * The buffer field.
20
+ */
21
+ public readonly buffer: Buffer,
22
+ ) {
23
+ const value = toBigIntBE(buffer);
24
+ if (value > AztecAddress.MAX_VALUE) {
25
+ throw new Error(`AztecAddress out of range ${value}.`);
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Generates a random AztecAddress instance, using the Fr field (a finite field) to create a random value
31
+ * within a valid range and then converting it into a Buffer of a fixed size in bytes.
32
+ *
33
+ * @returns A new AztecAddress instance with a random value.
34
+ */
35
+ static random() {
36
+ return new AztecAddress(toBufferBE(Fr.random().value, AztecAddress.SIZE_IN_BYTES));
37
+ }
38
+
39
+ /**
40
+ * Creates an AztecAddress instance from a given buffer or BufferReader.
41
+ * If the input is a Buffer, it wraps it in a BufferReader before processing.
42
+ * Throws an error if the input length is not equal to the expected size.
43
+ *
44
+ * @param buffer - The input buffer or BufferReader containing the address data.
45
+ * @returns - A new AztecAddress instance with the extracted address data.
46
+ */
47
+ static fromBuffer(buffer: Buffer | BufferReader) {
48
+ const reader = BufferReader.asReader(buffer);
49
+ return new this(reader.readBytes(this.SIZE_IN_BYTES));
50
+ }
51
+
52
+ /**
53
+ * Create an AztecAddress instance from a hex-encoded string.
54
+ * The input 'address' should be prefixed with '0x' or not, and have exactly 64 hex characters.
55
+ * Throws an error if the input length is invalid or address value is out of range.
56
+ *
57
+ * @param address - The hex-encoded string representing the Aztec address.
58
+ * @returns An AztecAddress instance.
59
+ */
60
+ static fromString(address: string) {
61
+ const buf = Buffer.from(address.replace(/^0x/i, ''), 'hex');
62
+ if (buf.length !== AztecAddress.SIZE_IN_BYTES) {
63
+ throw new Error(`Invalid length ${buf.length}.`);
64
+ }
65
+ return new AztecAddress(buf);
66
+ }
67
+
68
+ /**
69
+ * Converts the AztecAddress instance into a Buffer.
70
+ * This method should be used when encoding the address for storage, transmission or serialization purposes.
71
+ *
72
+ * @returns A Buffer representation of the AztecAddress instance.
73
+ */
74
+ toBuffer() {
75
+ return this.buffer;
76
+ }
77
+
78
+ /**
79
+ * Convert the AztecAddress to a hexadecimal string representation, with a "0x" prefix.
80
+ * The resulting string will have a length of 66 characters (including the prefix).
81
+ *
82
+ * @returns A hexadecimal string representation of the AztecAddress.
83
+ */
84
+ toString(): `0x${string}` {
85
+ return `0x${this.buffer.toString('hex')}`;
86
+ }
87
+
88
+ /**
89
+ * Returns a shortened string representation of the AztecAddress, displaying only the first 10 characters and last 4 characters.
90
+ * This is useful for human-readable displays where the full address is not necessary.
91
+ *
92
+ * @returns A shortened string representation of the address.
93
+ */
94
+ toShortString() {
95
+ const str = this.toString();
96
+ return `${str.slice(0, 10)}...${str.slice(-4)}`;
97
+ }
98
+
99
+ /**
100
+ * Returns this address as a field element.
101
+ * @returns A field element with the same value as the address.
102
+ */
103
+ toField() {
104
+ return Fr.fromBuffer(this.toBuffer());
105
+ }
106
+
107
+ /**
108
+ * Determines if this AztecAddress instance is equal to the given AztecAddress instance.
109
+ * Equality is based on the content of their respective buffers.
110
+ *
111
+ * @param rhs - The AztecAddress instance to compare against.
112
+ * @returns True if the buffers of both instances are equal, false otherwise.
113
+ */
114
+ equals(rhs: AztecAddress) {
115
+ return this.buffer.equals(rhs.buffer);
116
+ }
117
+
118
+ /**
119
+ * Checks if the AztecAddress is zero.
120
+ *
121
+ * @returns Returns true if the AztecAddress is equal to the zero address, otherwise returns false.
122
+ */
123
+ isZero() {
124
+ return this.equals(AztecAddress.ZERO);
125
+ }
126
+
127
+ /**
128
+ * Friendly representation for debugging purposes.
129
+ *
130
+ * @returns A hex string representing the address.
131
+ */
132
+ toFriendlyJSON() {
133
+ return this.toString();
134
+ }
135
+ }
@@ -0,0 +1,21 @@
1
+ import { toHex } from './index.js';
2
+
3
+ describe('bigint-buffer', () => {
4
+ describe('toHex', () => {
5
+ it('does not pad even length', () => {
6
+ expect(toHex(16n)).toEqual('0x10');
7
+ });
8
+
9
+ it('pads odd length hex to even length', () => {
10
+ expect(toHex(10n)).toEqual('0x0a');
11
+ });
12
+
13
+ it('pads zero to even length', () => {
14
+ expect(toHex(0n)).toEqual('0x00');
15
+ });
16
+
17
+ it('pads zero to 32 bytes', () => {
18
+ expect(toHex(0n, true)).toEqual('0x0000000000000000000000000000000000000000000000000000000000000000');
19
+ });
20
+ });
21
+ });
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Convert a little-endian buffer into a BigInt.
3
+ * @param buf - The little-endian buffer to convert.
4
+ * @returns A BigInt with the little-endian representation of buf.
5
+ */
6
+ export function toBigIntLE(buf: Buffer): bigint {
7
+ const reversed = Buffer.from(buf);
8
+ reversed.reverse();
9
+ const hex = reversed.toString('hex');
10
+ if (hex.length === 0) {
11
+ return BigInt(0);
12
+ }
13
+ return BigInt(`0x${hex}`);
14
+ }
15
+
16
+ /**
17
+ * Convert a big-endian buffer into a BigInt.
18
+ * @param buf - The big-endian buffer to convert.
19
+ * @returns A BigInt with the big-endian representation of buf.
20
+ */
21
+ export function toBigIntBE(buf: Buffer): bigint {
22
+ const hex = buf.toString('hex');
23
+ if (hex.length === 0) {
24
+ return BigInt(0);
25
+ }
26
+ return BigInt(`0x${hex}`);
27
+ }
28
+
29
+ /**
30
+ * Convert a BigInt to a little-endian buffer.
31
+ * @param num - The BigInt to convert.
32
+ * @param width - The number of bytes that the resulting buffer should be.
33
+ * @returns A little-endian buffer representation of num.
34
+ */
35
+ export function toBufferLE(num: bigint, width: number): Buffer {
36
+ if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferLE.`);
37
+ const hex = num.toString(16);
38
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
39
+ buffer.reverse();
40
+ return buffer;
41
+ }
42
+
43
+ /**
44
+ * Convert a BigInt to a big-endian buffer.
45
+ * @param num - The BigInt to convert.
46
+ * @param width - The number of bytes that the resulting buffer should be.
47
+ * @returns A big-endian buffer representation of num.
48
+ */
49
+ export function toBufferBE(num: bigint, width: number): Buffer {
50
+ if (num < BigInt(0)) throw new Error(`Cannot convert negative bigint ${num.toString()} to buffer with toBufferBE.`);
51
+ const hex = num.toString(16);
52
+ const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
53
+ if (buffer.length > width) throw new Error(`Number ${num.toString(16)} does not fit in ${width}`);
54
+ return buffer;
55
+ }
56
+
57
+ /**
58
+ * Converts a BigInt to its hex representation.
59
+ * @param num - The BigInt to convert.
60
+ * @param padTo32 - Whether to pad the resulting string to 32 bytes.
61
+ * @returns An even-length 0x-prefixed string.
62
+ */
63
+ export function toHex(num: bigint, padTo32 = false): `0x${string}` {
64
+ const str = num.toString(16);
65
+ const targetLen = str.length % 2 === 0 ? str.length : str.length + 1;
66
+ const paddedStr = str.padStart(padTo32 ? 64 : targetLen, '0');
67
+ return `0x${paddedStr}`;
68
+ }
@@ -0,0 +1,48 @@
1
+ import { Tuple } from '../serialize/types.js';
2
+
3
+ /**
4
+ * Pads an array to the target length by appending an element to its end. Throws if target length exceeds the input array length. Does not modify the input array.
5
+ * @param arr - Array with elements to pad.
6
+ * @param elem - Element to use for padding.
7
+ * @param length - Target length.
8
+ * @returns A new padded array.
9
+ */
10
+ export function padArrayEnd<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
11
+ if (arr.length > length) throw new Error(`Array size exceeds target length`);
12
+ // Since typescript cannot always deduce that something is a tuple, we cast
13
+ return [...arr, ...Array(length - arr.length).fill(elem)] as Tuple<T, N>;
14
+ }
15
+
16
+ /**
17
+ * Pads an array to the target length by prepending elements at the beginning. Throws if target length exceeds the input array length. Does not modify the input array.
18
+ * @param arr - Array with elements to pad.
19
+ * @param elem - Element to use for padding.
20
+ * @param length - Target length.
21
+ * @returns A new padded array.
22
+ */
23
+ export function padArrayStart<T, N extends number>(arr: T[], elem: T, length: N): Tuple<T, N> {
24
+ if (arr.length > length) throw new Error(`Array size exceeds target length`);
25
+ // Since typescript cannot always deduce that something is a tuple, we cast
26
+ return [...Array(length - arr.length).fill(elem), ...arr] as Tuple<T, N>;
27
+ }
28
+
29
+ /**
30
+ * Returns if an array is composed of empty items.
31
+ * @param arr - Array to check.
32
+ * @returns True if every item in the array isEmpty.
33
+ */
34
+ export function isArrayEmpty<T>(arr: T[], isEmpty: (item: T) => boolean): boolean {
35
+ for (const item of arr) {
36
+ if (!isEmpty(item)) return false;
37
+ }
38
+ return true;
39
+ }
40
+
41
+ /**
42
+ * Returns the number of non-empty items in an array.
43
+ * @param arr - Array to check.
44
+ * @returns Number of non-empty items in an array.
45
+ */
46
+ export function arrayNonEmptyLength<T>(arr: T[], isEmpty: (item: T) => boolean): number {
47
+ return arr.reduce((sum, item) => (isEmpty(item) ? sum : sum + 1), 0);
48
+ }
@@ -0,0 +1 @@
1
+ export * from './array.js';
@@ -0,0 +1,3 @@
1
+ export * from './keccak/index.js';
2
+ export * from './random/index.js';
3
+ export * from './sha256/index.js';
@@ -0,0 +1,33 @@
1
+ import { Keccak } from 'sha3';
2
+
3
+ /**
4
+ * Computes the Keccak-256 hash of the given input buffer.
5
+ *
6
+ * @param input - The input buffer to be hashed.
7
+ * @returns The computed Keccak-256 hash as a Buffer.
8
+ */
9
+ export function keccak(input: Buffer) {
10
+ const hash = new Keccak(256);
11
+ return hash.update(input).digest();
12
+ }
13
+
14
+ /**
15
+ * Computes the keccak-256 hash of a given input string and returns the result as a hexadecimal string.
16
+ */
17
+ export function keccak256String(input: string) {
18
+ const hash = new Keccak(256);
19
+ hash.reset();
20
+ hash.update(input);
21
+ return hash.digest('hex');
22
+ }
23
+
24
+ /**
25
+ * Computes the Keccak-224 hash of the given input buffer.
26
+ *
27
+ * @param input - The input buffer to be hashed.
28
+ * @returns The computed Keccak-224 hash as a Buffer.
29
+ */
30
+ export function keccak224(input: Buffer) {
31
+ const hash = new Keccak(224);
32
+ return hash.update(input).digest();
33
+ }
@@ -0,0 +1,13 @@
1
+ import { randomBytes } from './index.js';
2
+
3
+ describe('random', () => {
4
+ it('randomBytes returns a filled byte array', () => {
5
+ const data = randomBytes(32);
6
+ expect(data.length).toEqual(32);
7
+ let identical = true;
8
+ for (let i = 1; i < data.length; ++i) {
9
+ identical = identical && data[i] == data[i - 1];
10
+ }
11
+ expect(identical).toEqual(false);
12
+ });
13
+ });