@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.
- package/.eslintrc.cjs +103 -0
- package/.prettierrc.json +6 -0
- package/.tsbuildinfo +1 -0
- package/README.md +1 -0
- package/dest/abi/index.d.ts +137 -0
- package/dest/abi/index.d.ts.map +1 -0
- package/dest/abi/index.js +18 -0
- package/dest/async-map/async_map.test.d.ts +2 -0
- package/dest/async-map/async_map.test.d.ts.map +1 -0
- package/dest/async-map/async_map.test.js +9 -0
- package/dest/async-map/index.d.ts +13 -0
- package/dest/async-map/index.d.ts.map +1 -0
- package/dest/async-map/index.js +19 -0
- package/dest/aztec-address/index.d.ts +96 -0
- package/dest/aztec-address/index.d.ts.map +1 -0
- package/dest/aztec-address/index.js +124 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts +2 -0
- package/dest/bigint-buffer/bigint-buffer.test.d.ts.map +1 -0
- package/dest/bigint-buffer/bigint-buffer.test.js +18 -0
- package/dest/bigint-buffer/index.d.ts +35 -0
- package/dest/bigint-buffer/index.d.ts.map +1 -0
- package/dest/bigint-buffer/index.js +68 -0
- package/dest/collection/array.d.ts +30 -0
- package/dest/collection/array.d.ts.map +1 -0
- package/dest/collection/array.js +47 -0
- package/dest/collection/index.d.ts +2 -0
- package/dest/collection/index.d.ts.map +1 -0
- package/dest/collection/index.js +2 -0
- package/dest/crypto/index.d.ts +4 -0
- package/dest/crypto/index.d.ts.map +1 -0
- package/dest/crypto/index.js +4 -0
- package/dest/crypto/keccak/index.d.ts +20 -0
- package/dest/crypto/keccak/index.d.ts.map +1 -0
- package/dest/crypto/keccak/index.js +31 -0
- package/dest/crypto/random/index.d.ts +3 -0
- package/dest/crypto/random/index.d.ts.map +1 -0
- package/dest/crypto/random/index.js +36 -0
- package/dest/crypto/random/index.test.d.ts +2 -0
- package/dest/crypto/random/index.test.d.ts.map +1 -0
- package/dest/crypto/random/index.test.js +13 -0
- package/dest/crypto/sha256/index.d.ts +11 -0
- package/dest/crypto/sha256/index.d.ts.map +1 -0
- package/dest/crypto/sha256/index.js +14 -0
- package/dest/crypto/sha256/index.test.d.ts +2 -0
- package/dest/crypto/sha256/index.test.d.ts.map +1 -0
- package/dest/crypto/sha256/index.test.js +11 -0
- package/dest/errors/index.d.ts +8 -0
- package/dest/errors/index.d.ts.map +1 -0
- package/dest/errors/index.js +8 -0
- package/dest/eth-address/eth_address.test.d.ts +2 -0
- package/dest/eth-address/eth_address.test.d.ts.map +1 -0
- package/dest/eth-address/eth_address.test.js +87 -0
- package/dest/eth-address/index.d.ts +139 -0
- package/dest/eth-address/index.d.ts.map +1 -0
- package/dest/eth-address/index.js +221 -0
- package/dest/fields/coordinate.d.ts +67 -0
- package/dest/fields/coordinate.d.ts.map +1 -0
- package/dest/fields/coordinate.js +95 -0
- package/dest/fields/coordinate.test.d.ts +2 -0
- package/dest/fields/coordinate.test.d.ts.map +1 -0
- package/dest/fields/coordinate.test.js +22 -0
- package/dest/fields/fields.d.ts +170 -0
- package/dest/fields/fields.d.ts.map +1 -0
- package/dest/fields/fields.js +210 -0
- package/dest/fields/index.d.ts +4 -0
- package/dest/fields/index.d.ts.map +1 -0
- package/dest/fields/index.js +4 -0
- package/dest/fields/point.d.ts +111 -0
- package/dest/fields/point.d.ts.map +1 -0
- package/dest/fields/point.js +133 -0
- package/dest/fifo/bounded_serial_queue.d.ts +56 -0
- package/dest/fifo/bounded_serial_queue.d.ts.map +1 -0
- package/dest/fifo/bounded_serial_queue.js +94 -0
- package/dest/fifo/index.d.ts +5 -0
- package/dest/fifo/index.d.ts.map +1 -0
- package/dest/fifo/index.js +5 -0
- package/dest/fifo/memory_fifo.d.ts +55 -0
- package/dest/fifo/memory_fifo.d.ts.map +1 -0
- package/dest/fifo/memory_fifo.js +109 -0
- package/dest/fifo/semaphore.d.ts +23 -0
- package/dest/fifo/semaphore.d.ts.map +1 -0
- package/dest/fifo/semaphore.js +30 -0
- package/dest/fifo/serial_queue.d.ts +48 -0
- package/dest/fifo/serial_queue.d.ts.map +1 -0
- package/dest/fifo/serial_queue.js +74 -0
- package/dest/json-rpc/class_converter.d.ts +109 -0
- package/dest/json-rpc/class_converter.d.ts.map +1 -0
- package/dest/json-rpc/class_converter.js +85 -0
- package/dest/json-rpc/client/index.d.ts +2 -0
- package/dest/json-rpc/client/index.d.ts.map +1 -0
- package/dest/json-rpc/client/index.js +2 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts +22 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.js +83 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts +2 -0
- package/dest/json-rpc/client/json_rpc_client.test.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.test.js +20 -0
- package/dest/json-rpc/convert.d.ts +22 -0
- package/dest/json-rpc/convert.d.ts.map +1 -0
- package/dest/json-rpc/convert.js +93 -0
- package/dest/json-rpc/convert.test.d.ts +2 -0
- package/dest/json-rpc/convert.test.d.ts.map +1 -0
- package/dest/json-rpc/convert.test.js +14 -0
- package/dest/json-rpc/fixtures/test_state.d.ts +47 -0
- package/dest/json-rpc/fixtures/test_state.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/test_state.js +62 -0
- package/dest/json-rpc/index.d.ts +4 -0
- package/dest/json-rpc/index.d.ts.map +1 -0
- package/dest/json-rpc/index.js +4 -0
- package/dest/json-rpc/js_utils.d.ts +13 -0
- package/dest/json-rpc/js_utils.d.ts.map +1 -0
- package/dest/json-rpc/js_utils.js +18 -0
- package/dest/json-rpc/server/index.d.ts +3 -0
- package/dest/json-rpc/server/index.d.ts.map +1 -0
- package/dest/json-rpc/server/index.js +3 -0
- package/dest/json-rpc/server/json_proxy.d.ts +18 -0
- package/dest/json-rpc/server/json_proxy.d.ts.map +1 -0
- package/dest/json-rpc/server/json_proxy.js +35 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts +34 -0
- package/dest/json-rpc/server/json_rpc_server.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.js +109 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts +2 -0
- package/dest/json-rpc/server/json_rpc_server.test.d.ts.map +1 -0
- package/dest/json-rpc/server/json_rpc_server.test.js +22 -0
- package/dest/log/console.d.ts +16 -0
- package/dest/log/console.d.ts.map +1 -0
- package/dest/log/console.js +39 -0
- package/dest/log/debug.d.ts +46 -0
- package/dest/log/debug.d.ts.map +1 -0
- package/dest/log/debug.js +75 -0
- package/dest/log/index.d.ts +4 -0
- package/dest/log/index.d.ts.map +1 -0
- package/dest/log/index.js +4 -0
- package/dest/log/log_history.d.ts +31 -0
- package/dest/log/log_history.d.ts.map +1 -0
- package/dest/log/log_history.js +42 -0
- package/dest/log/log_history.test.d.ts +2 -0
- package/dest/log/log_history.test.d.ts.map +1 -0
- package/dest/log/log_history.test.js +78 -0
- package/dest/mutex/index.d.ts +53 -0
- package/dest/mutex/index.d.ts.map +1 -0
- package/dest/mutex/index.js +74 -0
- package/dest/mutex/mutex.test.d.ts +9 -0
- package/dest/mutex/mutex.test.d.ts.map +1 -0
- package/dest/mutex/mutex.test.js +58 -0
- package/dest/mutex/mutex_database.d.ts +10 -0
- package/dest/mutex/mutex_database.d.ts.map +1 -0
- package/dest/mutex/mutex_database.js +2 -0
- package/dest/retry/index.d.ts +33 -0
- package/dest/retry/index.d.ts.map +1 -0
- package/dest/retry/index.js +70 -0
- package/dest/running-promise/index.d.ts +35 -0
- package/dest/running-promise/index.d.ts.map +1 -0
- package/dest/running-promise/index.js +59 -0
- package/dest/serialize/buffer_reader.d.ts +188 -0
- package/dest/serialize/buffer_reader.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.js +229 -0
- package/dest/serialize/buffer_reader.test.d.ts +2 -0
- package/dest/serialize/buffer_reader.test.d.ts.map +1 -0
- package/dest/serialize/buffer_reader.test.js +156 -0
- package/dest/serialize/deserializer.d.ts +134 -0
- package/dest/serialize/deserializer.d.ts.map +1 -0
- package/dest/serialize/deserializer.js +145 -0
- package/dest/serialize/free_funcs.d.ts +203 -0
- package/dest/serialize/free_funcs.d.ts.map +1 -0
- package/dest/serialize/free_funcs.js +250 -0
- package/dest/serialize/index.d.ts +6 -0
- package/dest/serialize/index.d.ts.map +1 -0
- package/dest/serialize/index.js +6 -0
- package/dest/serialize/serialize.test.d.ts +2 -0
- package/dest/serialize/serialize.test.d.ts.map +1 -0
- package/dest/serialize/serialize.test.js +68 -0
- package/dest/serialize/serializer.d.ts +89 -0
- package/dest/serialize/serializer.d.ts.map +1 -0
- package/dest/serialize/serializer.js +111 -0
- package/dest/serialize/types.d.ts +33 -0
- package/dest/serialize/types.d.ts.map +1 -0
- package/dest/serialize/types.js +22 -0
- package/dest/sleep/index.d.ts +52 -0
- package/dest/sleep/index.d.ts.map +1 -0
- package/dest/sleep/index.js +70 -0
- package/dest/timer/index.d.ts +3 -0
- package/dest/timer/index.d.ts.map +1 -0
- package/dest/timer/index.js +3 -0
- package/dest/timer/timeout.d.ts +41 -0
- package/dest/timer/timeout.d.ts.map +1 -0
- package/dest/timer/timeout.js +62 -0
- package/dest/timer/timer.d.ts +33 -0
- package/dest/timer/timer.d.ts.map +1 -0
- package/dest/timer/timer.js +38 -0
- package/dest/transport/browser/index.d.ts +5 -0
- package/dest/transport/browser/index.d.ts.map +1 -0
- package/dest/transport/browser/index.js +5 -0
- package/dest/transport/browser/message_port_socket.d.ts +37 -0
- package/dest/transport/browser/message_port_socket.d.ts.map +1 -0
- package/dest/transport/browser/message_port_socket.js +46 -0
- package/dest/transport/browser/shared_worker_connector.d.ts +19 -0
- package/dest/transport/browser/shared_worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_connector.js +21 -0
- package/dest/transport/browser/shared_worker_listener.d.ts +38 -0
- package/dest/transport/browser/shared_worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/shared_worker_listener.js +37 -0
- package/dest/transport/browser/worker_connector.d.ts +26 -0
- package/dest/transport/browser/worker_connector.d.ts.map +1 -0
- package/dest/transport/browser/worker_connector.js +30 -0
- package/dest/transport/browser/worker_listener.d.ts +39 -0
- package/dest/transport/browser/worker_listener.d.ts.map +1 -0
- package/dest/transport/browser/worker_listener.js +39 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts +25 -0
- package/dest/transport/dispatch/create_dispatch_fn.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_fn.js +18 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts +102 -0
- package/dest/transport/dispatch/create_dispatch_proxy.d.ts.map +1 -0
- package/dest/transport/dispatch/create_dispatch_proxy.js +56 -0
- package/dest/transport/dispatch/messages.d.ts +52 -0
- package/dest/transport/dispatch/messages.d.ts.map +1 -0
- package/dest/transport/dispatch/messages.js +12 -0
- package/dest/transport/index.d.ts +12 -0
- package/dest/transport/index.d.ts.map +1 -0
- package/dest/transport/index.js +12 -0
- package/dest/transport/interface/connector.d.ts +8 -0
- package/dest/transport/interface/connector.d.ts.map +1 -0
- package/dest/transport/interface/connector.js +2 -0
- package/dest/transport/interface/listener.d.ts +13 -0
- package/dest/transport/interface/listener.d.ts.map +1 -0
- package/dest/transport/interface/listener.js +2 -0
- package/dest/transport/interface/socket.d.ts +13 -0
- package/dest/transport/interface/socket.d.ts.map +1 -0
- package/dest/transport/interface/socket.js +2 -0
- package/dest/transport/interface/transferable.d.ts +68 -0
- package/dest/transport/interface/transferable.d.ts.map +1 -0
- package/dest/transport/interface/transferable.js +63 -0
- package/dest/transport/node/index.d.ts +3 -0
- package/dest/transport/node/index.d.ts.map +1 -0
- package/dest/transport/node/index.js +3 -0
- package/dest/transport/node/node_connector.d.ts +28 -0
- package/dest/transport/node/node_connector.d.ts.map +1 -0
- package/dest/transport/node/node_connector.js +28 -0
- package/dest/transport/node/node_connector_socket.d.ts +42 -0
- package/dest/transport/node/node_connector_socket.d.ts.map +1 -0
- package/dest/transport/node/node_connector_socket.js +48 -0
- package/dest/transport/node/node_listener.d.ts +26 -0
- package/dest/transport/node/node_listener.d.ts.map +1 -0
- package/dest/transport/node/node_listener.js +30 -0
- package/dest/transport/node/node_listener_socket.d.ts +37 -0
- package/dest/transport/node/node_listener_socket.d.ts.map +1 -0
- package/dest/transport/node/node_listener_socket.js +44 -0
- package/dest/transport/transport_client.d.ts +61 -0
- package/dest/transport/transport_client.d.ts.map +1 -0
- package/dest/transport/transport_client.js +94 -0
- package/dest/transport/transport_server.d.ts +56 -0
- package/dest/transport/transport_server.d.ts.map +1 -0
- package/dest/transport/transport_server.js +101 -0
- package/dest/types/index.d.ts +7 -0
- package/dest/types/index.d.ts.map +1 -0
- package/dest/types/index.js +2 -0
- package/dest/wasm/index.d.ts +5 -0
- package/dest/wasm/index.d.ts.map +1 -0
- package/dest/wasm/index.js +5 -0
- package/dest/wasm/wasm/empty_wasi_sdk.d.ts +130 -0
- package/dest/wasm/wasm/empty_wasi_sdk.d.ts.map +1 -0
- package/dest/wasm/wasm/empty_wasi_sdk.js +166 -0
- package/dest/wasm/wasm/index.d.ts +2 -0
- package/dest/wasm/wasm/index.d.ts.map +1 -0
- package/dest/wasm/wasm/index.js +2 -0
- package/dest/wasm/wasm/wasm_module.d.ts +135 -0
- package/dest/wasm/wasm/wasm_module.d.ts.map +1 -0
- package/dest/wasm/wasm/wasm_module.js +205 -0
- package/dest/wasm/wasm/wasm_module.test.d.ts +2 -0
- package/dest/wasm/wasm/wasm_module.test.d.ts.map +1 -0
- package/dest/wasm/wasm/wasm_module.test.js +24 -0
- package/dest/wasm/worker/browser/index.d.ts +3 -0
- package/dest/wasm/worker/browser/index.d.ts.map +1 -0
- package/dest/wasm/worker/browser/index.js +3 -0
- package/dest/wasm/worker/browser/start_web_module.d.ts +7 -0
- package/dest/wasm/worker/browser/start_web_module.d.ts.map +1 -0
- package/dest/wasm/worker/browser/start_web_module.js +22 -0
- package/dest/wasm/worker/browser/web_data_store.d.ts +23 -0
- package/dest/wasm/worker/browser/web_data_store.d.ts.map +1 -0
- package/dest/wasm/worker/browser/web_data_store.js +32 -0
- package/dest/wasm/worker/browser/web_worker.d.ts +10 -0
- package/dest/wasm/worker/browser/web_worker.d.ts.map +1 -0
- package/dest/wasm/worker/browser/web_worker.js +23 -0
- package/dest/wasm/worker/data_store.d.ts +9 -0
- package/dest/wasm/worker/data_store.d.ts.map +1 -0
- package/dest/wasm/worker/data_store.js +2 -0
- package/dest/wasm/worker/index.d.ts +3 -0
- package/dest/wasm/worker/index.d.ts.map +1 -0
- package/dest/wasm/worker/index.js +2 -0
- package/dest/wasm/worker/node/index.d.ts +3 -0
- package/dest/wasm/worker/node/index.d.ts.map +1 -0
- package/dest/wasm/worker/node/index.js +3 -0
- package/dest/wasm/worker/node/node_data_store.d.ts +22 -0
- package/dest/wasm/worker/node/node_data_store.d.ts.map +1 -0
- package/dest/wasm/worker/node/node_data_store.js +31 -0
- package/dest/wasm/worker/node/node_worker.d.ts +6 -0
- package/dest/wasm/worker/node/node_worker.d.ts.map +1 -0
- package/dest/wasm/worker/node/node_worker.js +20 -0
- package/dest/wasm/worker/node/start_node_module.d.ts +7 -0
- package/dest/wasm/worker/node/start_node_module.d.ts.map +1 -0
- package/dest/wasm/worker/node/start_node_module.js +26 -0
- package/dest/wasm/worker/wasm_worker.d.ts +9 -0
- package/dest/wasm/worker/wasm_worker.d.ts.map +1 -0
- package/dest/wasm/worker/wasm_worker.js +2 -0
- package/dest/wasm/worker/worker_pool.d.ts +40 -0
- package/dest/wasm/worker/worker_pool.d.ts.map +1 -0
- package/dest/wasm/worker/worker_pool.js +62 -0
- package/package.json +44 -0
- package/src/abi/index.ts +147 -0
- package/src/async-map/async_map.test.ts +9 -0
- package/src/async-map/index.ts +18 -0
- package/src/aztec-address/index.ts +135 -0
- package/src/bigint-buffer/bigint-buffer.test.ts +21 -0
- package/src/bigint-buffer/index.ts +68 -0
- package/src/collection/array.ts +48 -0
- package/src/collection/index.ts +1 -0
- package/src/crypto/index.ts +3 -0
- package/src/crypto/keccak/index.ts +33 -0
- package/src/crypto/random/index.test.ts +13 -0
- package/src/crypto/random/index.ts +38 -0
- package/src/crypto/sha256/index.test.ts +13 -0
- package/src/crypto/sha256/index.ts +15 -0
- package/src/errors/index.ts +6 -0
- package/src/eth-address/eth_address.test.ts +104 -0
- package/src/eth-address/index.ts +236 -0
- package/src/fields/coordinate.test.ts +26 -0
- package/src/fields/coordinate.ts +104 -0
- package/src/fields/fields.ts +234 -0
- package/src/fields/index.ts +3 -0
- package/src/fields/point.ts +143 -0
- package/src/fifo/bounded_serial_queue.ts +100 -0
- package/src/fifo/index.ts +4 -0
- package/src/fifo/memory_fifo.ts +114 -0
- package/src/fifo/semaphore.ts +33 -0
- package/src/fifo/serial_queue.ts +78 -0
- package/src/json-rpc/README.md +55 -0
- package/src/json-rpc/class_converter.ts +160 -0
- package/src/json-rpc/client/index.ts +1 -0
- package/src/json-rpc/client/json_rpc_client.test.ts +20 -0
- package/src/json-rpc/client/json_rpc_client.ts +103 -0
- package/src/json-rpc/convert.test.ts +15 -0
- package/src/json-rpc/convert.ts +109 -0
- package/src/json-rpc/fixtures/test_state.ts +59 -0
- package/src/json-rpc/index.ts +3 -0
- package/src/json-rpc/js_utils.ts +20 -0
- package/src/json-rpc/server/index.ts +2 -0
- package/src/json-rpc/server/json_proxy.ts +36 -0
- package/src/json-rpc/server/json_rpc_server.test.ts +23 -0
- package/src/json-rpc/server/json_rpc_server.ts +124 -0
- package/src/log/console.ts +45 -0
- package/src/log/debug.ts +86 -0
- package/src/log/index.ts +3 -0
- package/src/log/log_history.test.ts +87 -0
- package/src/log/log_history.ts +44 -0
- package/src/mutex/index.ts +83 -0
- package/src/mutex/mutex.test.ts +75 -0
- package/src/mutex/mutex_database.ts +9 -0
- package/src/retry/index.ts +78 -0
- package/src/running-promise/index.ts +60 -0
- package/src/serialize/buffer_reader.test.ts +175 -0
- package/src/serialize/buffer_reader.ts +272 -0
- package/src/serialize/deserializer.ts +180 -0
- package/src/serialize/free_funcs.ts +285 -0
- package/src/serialize/index.ts +5 -0
- package/src/serialize/serialize.test.ts +89 -0
- package/src/serialize/serializer.ts +128 -0
- package/src/serialize/types.ts +40 -0
- package/src/sleep/index.ts +71 -0
- package/src/timer/index.ts +2 -0
- package/src/timer/timeout.ts +64 -0
- package/src/timer/timer.ts +41 -0
- package/src/transport/browser/index.ts +4 -0
- package/src/transport/browser/message_port_socket.ts +48 -0
- package/src/transport/browser/shared_worker_connector.ts +21 -0
- package/src/transport/browser/shared_worker_listener.ts +52 -0
- package/src/transport/browser/worker_connector.ts +30 -0
- package/src/transport/browser/worker_listener.ts +53 -0
- package/src/transport/dispatch/create_dispatch_fn.ts +33 -0
- package/src/transport/dispatch/create_dispatch_proxy.ts +140 -0
- package/src/transport/dispatch/messages.ts +58 -0
- package/src/transport/index.ts +11 -0
- package/src/transport/interface/connector.ts +8 -0
- package/src/transport/interface/listener.ts +14 -0
- package/src/transport/interface/socket.ts +12 -0
- package/src/transport/interface/transferable.ts +121 -0
- package/src/transport/node/index.ts +2 -0
- package/src/transport/node/node_connector.ts +29 -0
- package/src/transport/node/node_connector_socket.ts +51 -0
- package/src/transport/node/node_listener.ts +33 -0
- package/src/transport/node/node_listener_socket.ts +47 -0
- package/src/transport/transport_client.ts +125 -0
- package/src/transport/transport_server.ts +108 -0
- package/src/types/index.ts +7 -0
- package/src/wasm/README.md +6 -0
- package/src/wasm/fixtures/gcd.wasm +0 -0
- package/src/wasm/fixtures/gcd.wat +27 -0
- package/src/wasm/index.ts +4 -0
- package/src/wasm/wasm/empty_wasi_sdk.ts +166 -0
- package/src/wasm/wasm/index.ts +1 -0
- package/src/wasm/wasm/wasm_module.test.ts +27 -0
- package/src/wasm/wasm/wasm_module.ts +257 -0
- package/src/wasm/worker/browser/index.ts +2 -0
- package/src/wasm/worker/browser/start_web_module.ts +23 -0
- package/src/wasm/worker/browser/web_data_store.ts +37 -0
- package/src/wasm/worker/browser/web_worker.ts +24 -0
- package/src/wasm/worker/data_store.ts +7 -0
- package/src/wasm/worker/index.ts +2 -0
- package/src/wasm/worker/node/index.ts +2 -0
- package/src/wasm/worker/node/node_data_store.ts +36 -0
- package/src/wasm/worker/node/node_worker.ts +21 -0
- package/src/wasm/worker/node/start_node_module.ts +28 -0
- package/src/wasm/worker/wasm_worker.ts +7 -0
- package/src/wasm/worker/worker_pool.ts +73 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import isNode from 'detect-node';
|
|
2
|
+
import nodeCrypto from 'crypto';
|
|
3
|
+
|
|
4
|
+
// limit of Crypto.getRandomValues()
|
|
5
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
|
6
|
+
const MAX_BYTES = 65536;
|
|
7
|
+
|
|
8
|
+
const getWebCrypto = () => {
|
|
9
|
+
if (typeof window !== 'undefined' && window.crypto) return window.crypto;
|
|
10
|
+
if (typeof self !== 'undefined' && self.crypto) return self.crypto;
|
|
11
|
+
return undefined;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const randomBytes = (len: number) => {
|
|
15
|
+
if (isNode) {
|
|
16
|
+
return nodeCrypto.randomBytes(len) as Buffer;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const crypto = getWebCrypto();
|
|
20
|
+
if (!crypto) {
|
|
21
|
+
throw new Error('randomBytes UnsupportedEnvironment');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const buf = Buffer.allocUnsafe(len);
|
|
25
|
+
if (len > MAX_BYTES) {
|
|
26
|
+
// this is the max bytes crypto.getRandomValues
|
|
27
|
+
// can do at once see https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
|
|
28
|
+
for (let generated = 0; generated < len; generated += MAX_BYTES) {
|
|
29
|
+
// buffer.slice automatically checks if the end is past the end of
|
|
30
|
+
// the buffer so we don't have to here
|
|
31
|
+
crypto.getRandomValues(buf.slice(generated, generated + MAX_BYTES));
|
|
32
|
+
}
|
|
33
|
+
} else {
|
|
34
|
+
crypto.getRandomValues(buf);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return buf;
|
|
38
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { sha256 } from './index.js';
|
|
2
|
+
import { randomBytes, createHash } from 'crypto';
|
|
3
|
+
|
|
4
|
+
describe('sha256', () => {
|
|
5
|
+
it('should correctly hash data using hash.js', () => {
|
|
6
|
+
const data = randomBytes(67);
|
|
7
|
+
|
|
8
|
+
const expected = createHash('sha256').update(data).digest();
|
|
9
|
+
|
|
10
|
+
const result = sha256(data);
|
|
11
|
+
expect(result).toEqual(expected);
|
|
12
|
+
});
|
|
13
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { default as hash } from 'hash.js';
|
|
2
|
+
import { toBigIntBE, toBufferBE } from '../../bigint-buffer/index.js';
|
|
3
|
+
import { Fr } from '../../fields/fields.js';
|
|
4
|
+
|
|
5
|
+
export const sha256 = (data: Buffer) => Buffer.from(hash.sha256().update(data).digest());
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Squashes the output of sha256 into a field element.
|
|
9
|
+
* WARNING: if you have not thought about why you are using this, or talked to somebody who has do not use it.
|
|
10
|
+
* @param buf - Input buffer
|
|
11
|
+
* @returns Returns a sha256 output squashed into a field element.
|
|
12
|
+
*/
|
|
13
|
+
export const sha256ToField = (buf: Buffer): Fr => {
|
|
14
|
+
return Fr.fromBuffer(toBufferBE(toBigIntBE(sha256(buf)) % Fr.MODULUS, 32));
|
|
15
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents an error thrown when an operation is interrupted unexpectedly.
|
|
3
|
+
* This custom error class extends the built-in Error class in JavaScript and
|
|
4
|
+
* can be used to handle cases where a process or task is terminated before completion.
|
|
5
|
+
*/
|
|
6
|
+
export class InterruptError extends Error {}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { EthAddress } from './index.js';
|
|
2
|
+
|
|
3
|
+
describe('address', () => {
|
|
4
|
+
it('should return correct string', () => {
|
|
5
|
+
const address = EthAddress.fromString('0xc6d9d2cd449a754c494264e1809c50e34d64562b');
|
|
6
|
+
expect(address.toChecksumString()).toBe('0xc6d9d2cD449A754c494264e1809c50e34D64562b');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('should return correct buffer', () => {
|
|
10
|
+
const address = EthAddress.fromString('0xc6d9d2cd449a754c494264e1809c50e34d64562b');
|
|
11
|
+
expect(address.toBuffer()).toEqual(Buffer.from('c6d9d2cD449A754c494264e1809c50e34D64562b', 'hex'));
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should return correct 32 byte buffer', () => {
|
|
15
|
+
const address = EthAddress.fromString('0xc6d9d2cd449a754c494264e1809c50e34d64562b');
|
|
16
|
+
expect(address.toBuffer32()).toEqual(
|
|
17
|
+
Buffer.from('000000000000000000000000c6d9d2cD449A754c494264e1809c50e34D64562b', 'hex'),
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should create address from 32 byte buffer', () => {
|
|
22
|
+
const buffer = Buffer.from('000000000000000000000000c6d9d2cD449A754c494264e1809c50e34D64562b', 'hex');
|
|
23
|
+
expect(new EthAddress(buffer)).toEqual(EthAddress.fromString('0xc6d9d2cD449A754c494264e1809c50e34D64562b'));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should not create address from 32 byte buffer that does not start with 12 0 bytes', () => {
|
|
27
|
+
const buffer = Buffer.from('010000000000000000000000c6d9d2cD449A754c494264e1809c50e34D64562b', 'hex');
|
|
28
|
+
expect(() => new EthAddress(buffer)).toThrowError();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should have correct zero address', () => {
|
|
32
|
+
expect(EthAddress.ZERO.toString()).toBe('0x0000000000000000000000000000000000000000');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('isAddress', () => {
|
|
36
|
+
it(`should return true for valid prefixed address`, () => {
|
|
37
|
+
expect(EthAddress.isAddress('0xc6d9d2cd449a754c494264e1809c50e34d64562b')).toBe(true);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it(`should return true for valid unprefixed address`, () => {
|
|
41
|
+
expect(EthAddress.isAddress('c6d9d2cd449a754c494264e1809c50e34d64562b')).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it(`should return true for valid all uppercase address`, () => {
|
|
45
|
+
expect(EthAddress.isAddress('0xC6D9D2CD449A754C494264E1809C50E34D64562B')).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it(`should return true for correctly checksummed address`, () => {
|
|
49
|
+
expect(EthAddress.isAddress('0xE247A45c287191d435A8a5D72A7C8dc030451E9F')).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it(`should return false for badly checksummed address`, () => {
|
|
53
|
+
expect(EthAddress.isAddress('0xE247A45C287191d435A8a5D72A7C8dc030451E9F')).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it(`should return false for non hex characters`, () => {
|
|
57
|
+
expect(EthAddress.isAddress('0xg6d9d2cd449a754c494264e1809c50e34d64562b')).toBe(false);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('checkAddressChecksum', () => {
|
|
62
|
+
const tests = [
|
|
63
|
+
{ value: '0x52908400098527886E0F7030069857D2E4169EE7', is: true },
|
|
64
|
+
{ value: '0x8617E340B3D01FA5F11F306F4090FD50E238070D', is: true },
|
|
65
|
+
{ value: '0xde709f2102306220921060314715629080e2fb77', is: true },
|
|
66
|
+
{ value: '0x27b1fdb04752bbc536007a920d24acb045561c26', is: true },
|
|
67
|
+
{ value: '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', is: true },
|
|
68
|
+
{ value: '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', is: true },
|
|
69
|
+
{ value: '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', is: true },
|
|
70
|
+
{ value: '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb', is: true },
|
|
71
|
+
{ value: '0XD1220A0CF47C7B9BE7A2E6BA89F429762E7B9ADB', is: false },
|
|
72
|
+
{ value: '0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb', is: false },
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
tests.forEach(test => {
|
|
76
|
+
it(`should return ${test.is} for address ${test.value}`, () => {
|
|
77
|
+
expect(EthAddress.checkAddressChecksum(test.value)).toBe(test.is);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe('toChecksumAddress', () => {
|
|
83
|
+
const tests = [
|
|
84
|
+
{
|
|
85
|
+
value: '0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed',
|
|
86
|
+
is: '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
value: '0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359',
|
|
90
|
+
is: '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
value: '0xdbf03b407c01e7cd3cbea99509d93f8dddc8c6fb',
|
|
94
|
+
is: '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
|
|
95
|
+
},
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
tests.forEach(test => {
|
|
99
|
+
it(`should return ${test.is} for address ${test.value}`, () => {
|
|
100
|
+
expect(EthAddress.toChecksumAddress(test.value)).toBe(test.is);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { keccak256String, randomBytes } from '../crypto/index.js';
|
|
2
|
+
import { Fr } from '../fields/index.js';
|
|
3
|
+
import { BufferReader } from '../serialize/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Represents an Ethereum address as a 20-byte buffer and provides various utility methods
|
|
7
|
+
* for converting between different representations, generating random addresses, validating
|
|
8
|
+
* checksums, and comparing addresses. EthAddress can be instantiated using a buffer or string,
|
|
9
|
+
* and can be serialized/deserialized from a buffer or BufferReader.
|
|
10
|
+
*/
|
|
11
|
+
export class EthAddress {
|
|
12
|
+
/**
|
|
13
|
+
* The size of an Ethereum address in bytes.
|
|
14
|
+
*/
|
|
15
|
+
public static SIZE_IN_BYTES = 20;
|
|
16
|
+
/**
|
|
17
|
+
* Represents a zero Ethereum address with 20 bytes filled with zeros.
|
|
18
|
+
*/
|
|
19
|
+
public static ZERO = new EthAddress(Buffer.alloc(EthAddress.SIZE_IN_BYTES));
|
|
20
|
+
|
|
21
|
+
constructor(private buffer: Buffer) {
|
|
22
|
+
if (buffer.length === 32) {
|
|
23
|
+
if (!buffer.slice(0, 12).equals(Buffer.alloc(12))) {
|
|
24
|
+
throw new Error('Invalid address buffer.');
|
|
25
|
+
} else {
|
|
26
|
+
this.buffer = buffer.slice(12);
|
|
27
|
+
}
|
|
28
|
+
} else if (buffer.length !== EthAddress.SIZE_IN_BYTES) {
|
|
29
|
+
throw new Error(`Expect buffer size to be ${EthAddress.SIZE_IN_BYTES}. Got ${buffer.length}.`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates an EthAddress instance from a valid Ethereum address string.
|
|
35
|
+
* The input 'address' can be either in checksum format or lowercase, and it can be prefixed with '0x'.
|
|
36
|
+
* Throws an error if the input is not a valid Ethereum address.
|
|
37
|
+
*
|
|
38
|
+
* @param address - The string representing the Ethereum address.
|
|
39
|
+
* @returns An EthAddress instance.
|
|
40
|
+
*/
|
|
41
|
+
public static fromString(address: string) {
|
|
42
|
+
if (!EthAddress.isAddress(address)) {
|
|
43
|
+
throw new Error(`Invalid address string: ${address}`);
|
|
44
|
+
}
|
|
45
|
+
return new EthAddress(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Create a random EthAddress instance with 20 random bytes.
|
|
50
|
+
* This method generates a new Ethereum address with a randomly generated set of 20 bytes.
|
|
51
|
+
* It is useful for generating test addresses or unique identifiers.
|
|
52
|
+
*
|
|
53
|
+
* @returns A randomly generated EthAddress instance.
|
|
54
|
+
*/
|
|
55
|
+
public static random() {
|
|
56
|
+
return new EthAddress(randomBytes(20));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Determines if the given string represents a valid Ethereum address.
|
|
61
|
+
* A valid address should meet the following criteria:
|
|
62
|
+
* 1. Contains exactly 40 hex characters (excluding an optional '0x' prefix).
|
|
63
|
+
* 2. Is either all lowercase, all uppercase, or has a valid checksum based on EIP-55.
|
|
64
|
+
*
|
|
65
|
+
* @param address - The string to be checked for validity as an Ethereum address.
|
|
66
|
+
* @returns True if the input string represents a valid Ethereum address, false otherwise.
|
|
67
|
+
*/
|
|
68
|
+
public static isAddress(address: string) {
|
|
69
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
70
|
+
// Does not have the basic requirements of an address.
|
|
71
|
+
return false;
|
|
72
|
+
} else if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
|
|
73
|
+
// It's ALL lowercase or ALL upppercase.
|
|
74
|
+
return true;
|
|
75
|
+
} else {
|
|
76
|
+
return EthAddress.checkAddressChecksum(address);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the EthAddress instance represents a zero address.
|
|
82
|
+
* A zero address consists of 20 bytes filled with zeros and is considered an invalid address.
|
|
83
|
+
*
|
|
84
|
+
* @returns A boolean indicating whether the EthAddress instance is a zero address or not.
|
|
85
|
+
*/
|
|
86
|
+
public isZero() {
|
|
87
|
+
return this.equals(EthAddress.ZERO);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Checks if the given Ethereum address has a valid checksum.
|
|
92
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 40 hex characters.
|
|
93
|
+
* Returns true if the address has a valid checksum, false otherwise.
|
|
94
|
+
*
|
|
95
|
+
* @param address - The hex-encoded string representing the Ethereum address.
|
|
96
|
+
* @returns A boolean value indicating whether the address has a valid checksum.
|
|
97
|
+
*/
|
|
98
|
+
public static checkAddressChecksum(address: string) {
|
|
99
|
+
address = address.replace(/^0x/i, '');
|
|
100
|
+
const addressHash = keccak256String(address.toLowerCase());
|
|
101
|
+
|
|
102
|
+
for (let i = 0; i < 40; i++) {
|
|
103
|
+
// The nth letter should be uppercase if the nth digit of casemap is 1.
|
|
104
|
+
if (
|
|
105
|
+
(parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) ||
|
|
106
|
+
(parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])
|
|
107
|
+
) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Converts an Ethereum address to its checksum format.
|
|
116
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 40 hex characters.
|
|
117
|
+
* The checksum format is created by capitalizing certain characters in the hex string
|
|
118
|
+
* based on the hash of the lowercase address.
|
|
119
|
+
* Throws an error if the input address is invalid.
|
|
120
|
+
*
|
|
121
|
+
* @param address - The Ethereum address as a hex-encoded string.
|
|
122
|
+
* @returns The Ethereum address in its checksum format.
|
|
123
|
+
*/
|
|
124
|
+
public static toChecksumAddress(address: string) {
|
|
125
|
+
if (!EthAddress.isAddress(address)) {
|
|
126
|
+
throw new Error('Invalid address string.');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
address = address.toLowerCase().replace(/^0x/i, '');
|
|
130
|
+
const addressHash = keccak256String(address);
|
|
131
|
+
let checksumAddress = '0x';
|
|
132
|
+
|
|
133
|
+
for (let i = 0; i < address.length; i++) {
|
|
134
|
+
// If ith character is 9 to f then make it uppercase.
|
|
135
|
+
if (parseInt(addressHash[i], 16) > 7) {
|
|
136
|
+
checksumAddress += address[i].toUpperCase();
|
|
137
|
+
} else {
|
|
138
|
+
checksumAddress += address[i];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return checksumAddress;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Checks whether the given EthAddress instance is equal to the current instance.
|
|
146
|
+
* Equality is determined by comparing the underlying byte buffers of both instances.
|
|
147
|
+
*
|
|
148
|
+
* @param rhs - The EthAddress instance to compare with the current instance.
|
|
149
|
+
* @returns A boolean value indicating whether the two instances are equal (true) or not (false).
|
|
150
|
+
*/
|
|
151
|
+
public equals(rhs: EthAddress) {
|
|
152
|
+
return this.buffer.equals(rhs.toBuffer());
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Converts the Ethereum address to a hex-encoded string.
|
|
157
|
+
* The resulting string is prefixed with '0x' and has exactly 40 hex characters.
|
|
158
|
+
* This method can be used to represent the EthAddress instance in the widely used hexadecimal format.
|
|
159
|
+
*
|
|
160
|
+
* @returns A hex-encoded string representation of the Ethereum address.
|
|
161
|
+
*/
|
|
162
|
+
public toString() {
|
|
163
|
+
return `0x${this.buffer.toString('hex')}` as `0x${string}`;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Returns the Ethereum address as a checksummed string.
|
|
168
|
+
* The output string will have characters in the correct upper or lowercase form, according to EIP-55.
|
|
169
|
+
* This provides a way to verify if an address is typed correctly, by checking the character casing.
|
|
170
|
+
*
|
|
171
|
+
* @returns A checksummed Ethereum address string.
|
|
172
|
+
*/
|
|
173
|
+
public toChecksumString() {
|
|
174
|
+
return EthAddress.toChecksumAddress(this.buffer.toString('hex'));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Returns the internal Buffer representation of the Ethereum address.
|
|
179
|
+
* This method is useful when working with raw binary data or when
|
|
180
|
+
* integrating with other modules that require a Buffer as input.
|
|
181
|
+
*
|
|
182
|
+
* @returns A Buffer instance containing the 20-byte Ethereum address.
|
|
183
|
+
*/
|
|
184
|
+
public toBuffer() {
|
|
185
|
+
return this.buffer;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Returns a 32-byte buffer representation of the Ethereum address, with the original 20-byte address
|
|
190
|
+
* occupying the last 20 bytes and the first 12 bytes being zero-filled.
|
|
191
|
+
* This format is commonly used in smart contracts when handling addresses as 32-byte values.
|
|
192
|
+
*
|
|
193
|
+
* @returns A 32-byte Buffer containing the padded Ethereum address.
|
|
194
|
+
*/
|
|
195
|
+
public toBuffer32() {
|
|
196
|
+
const buffer = Buffer.alloc(32);
|
|
197
|
+
this.buffer.copy(buffer, 12);
|
|
198
|
+
return buffer;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Returns a new field with the same contents as this EthAddress.
|
|
203
|
+
*
|
|
204
|
+
* @returns An Fr instance.
|
|
205
|
+
*/
|
|
206
|
+
public toField() {
|
|
207
|
+
return Fr.fromBuffer(this.toBuffer32());
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Converts a field to a eth address.
|
|
212
|
+
* @param fr - The field to convert.
|
|
213
|
+
* @returns The eth address.
|
|
214
|
+
*/
|
|
215
|
+
static fromField(fr: Fr): EthAddress {
|
|
216
|
+
return new EthAddress(fr.toBuffer().slice(-EthAddress.SIZE_IN_BYTES));
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Deserializes from a buffer or reader, corresponding to a write in cpp.
|
|
221
|
+
* @param buffer - Buffer to read from.
|
|
222
|
+
* @returns The EthAdress.
|
|
223
|
+
*/
|
|
224
|
+
static fromBuffer(buffer: Buffer | BufferReader): EthAddress {
|
|
225
|
+
const reader = BufferReader.asReader(buffer);
|
|
226
|
+
return new EthAddress(reader.readBuffer());
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Friendly representation for debugging purposes.
|
|
231
|
+
* @returns A hex string representing the address.
|
|
232
|
+
*/
|
|
233
|
+
toFriendlyJSON() {
|
|
234
|
+
return this.toString();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { toBufferBE } from '../bigint-buffer/index.js';
|
|
2
|
+
import { Coordinate } from './coordinate.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
|
|
5
|
+
const MAX_256_VALUE = 2n ** 256n - 1n;
|
|
6
|
+
|
|
7
|
+
describe('coordinate', () => {
|
|
8
|
+
it('stores 256 bits in fields', () => {
|
|
9
|
+
const max256Value = toBufferBE(MAX_256_VALUE, 32);
|
|
10
|
+
const coordinate = Coordinate.fromBuffer(max256Value);
|
|
11
|
+
// this returns a buffer containing the bit pattern split across 2 fields
|
|
12
|
+
expect(coordinate.toFieldsBuffer()).toEqual(
|
|
13
|
+
Buffer.concat([Buffer.alloc(1, 0), Buffer.alloc(31, 0xff), Buffer.alloc(31, 0), Buffer.alloc(1, 0xff)]),
|
|
14
|
+
);
|
|
15
|
+
// this returns the value in a single 32 byte buffer
|
|
16
|
+
expect(coordinate.toBuffer()).toEqual(max256Value);
|
|
17
|
+
// this returns the value as a big int
|
|
18
|
+
expect(coordinate.toBigInt()).toBe(MAX_256_VALUE);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('can be constructed from a field', () => {
|
|
22
|
+
const field = Fr.random();
|
|
23
|
+
const coordinate = Coordinate.fromField(field);
|
|
24
|
+
expect(coordinate.toBuffer()).toEqual(field.toBuffer());
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { toBigIntBE } from '../bigint-buffer/index.js';
|
|
2
|
+
import { Tuple } from '../serialize/types.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Class to wrap a single point coordinate.
|
|
7
|
+
* This class handles the complexities of representing point coordinates as 32 byte buffers as well as fields.
|
|
8
|
+
* The coordinate value is split across 2 fields to ensure that the max size of a field is not breached.
|
|
9
|
+
* This is achieved by placing the most significant byte of the lower field into the least significant byte of the higher field.
|
|
10
|
+
* Calls to 'toBuffer' or 'toBigInt' undo this change and simply return the original 32 byte value.
|
|
11
|
+
* Calls to 'toFieldsBuffer' will return a 64 bytes buffer containing the serialised fields.
|
|
12
|
+
*/
|
|
13
|
+
export class Coordinate {
|
|
14
|
+
static ZERO = new Coordinate([Fr.ZERO, Fr.ZERO]);
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
/**
|
|
18
|
+
* The fields of the coordinate value. Least significant limb at index 0.
|
|
19
|
+
*/
|
|
20
|
+
public fields: Tuple<Fr, 2>,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Converts the coordinate data into a tuple of fields
|
|
25
|
+
* @returns A tuple of the coordinate fields
|
|
26
|
+
*/
|
|
27
|
+
toFields(): Tuple<Fr, 2> {
|
|
28
|
+
return this.fields;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Generates a random coordinate value
|
|
33
|
+
* @returns The random coordinate
|
|
34
|
+
*/
|
|
35
|
+
static random(): Coordinate {
|
|
36
|
+
return this.fromField(Fr.random());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Serialises the oblect to buffer of 2 fields.
|
|
41
|
+
* @returns A buffer serialisation of the object.
|
|
42
|
+
*/
|
|
43
|
+
toFieldsBuffer(): Buffer {
|
|
44
|
+
return Buffer.concat([this.fields[0].toBuffer(), this.fields[1].toBuffer()]);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Serialises the coordinate to a single 32 byte buffer.
|
|
49
|
+
* @returns A buffer serialisation of the object.
|
|
50
|
+
*/
|
|
51
|
+
toBuffer(): Buffer {
|
|
52
|
+
const buf0 = this.fields[0].toBuffer();
|
|
53
|
+
const buf1 = this.fields[1].toBuffer();
|
|
54
|
+
buf0[0] = buf1[31];
|
|
55
|
+
return buf0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns true if this coordinate is equal to the one provided
|
|
60
|
+
* @param other - The coordinate against which to compare
|
|
61
|
+
* @returns True if the coordinates are the same, false otherwise
|
|
62
|
+
*/
|
|
63
|
+
equals(other: Coordinate): boolean {
|
|
64
|
+
return this.toBigInt() === other.toBigInt();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns the coordinate's value as a bigint
|
|
69
|
+
* @returns The coordinate value as a bigint
|
|
70
|
+
*/
|
|
71
|
+
toBigInt(): bigint {
|
|
72
|
+
return toBigIntBE(this.toBuffer());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Creates a coordinate object from a 32 byte coordinate value
|
|
77
|
+
* @param coordinate - A buffer containing the 32 byte coordinate value
|
|
78
|
+
* @returns The new coordinate object
|
|
79
|
+
*/
|
|
80
|
+
static fromBuffer(coordinate: Buffer) {
|
|
81
|
+
if (coordinate.length != 32) {
|
|
82
|
+
throw new Error(`Invalid size of coordinate buffer`);
|
|
83
|
+
}
|
|
84
|
+
const buf0 = Buffer.alloc(32);
|
|
85
|
+
coordinate.copy(buf0, 0, 0, 32);
|
|
86
|
+
const buf1 = Buffer.alloc(32);
|
|
87
|
+
buf1[31] = buf0[0];
|
|
88
|
+
buf0[0] = 0;
|
|
89
|
+
return new Coordinate([Fr.fromBuffer(buf0), Fr.fromBuffer(buf1)]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Creates a coordinate object from a field
|
|
94
|
+
* @param coordinate - The field containing the coordinate
|
|
95
|
+
* @returns The new coordinate object
|
|
96
|
+
*/
|
|
97
|
+
static fromField(coordinate: Fr) {
|
|
98
|
+
const buf0 = coordinate.toBuffer();
|
|
99
|
+
const buf1 = Buffer.alloc(32);
|
|
100
|
+
buf1[31] = buf0[0];
|
|
101
|
+
buf0[0] = 0;
|
|
102
|
+
return new Coordinate([Fr.fromBuffer(buf0), Fr.fromBuffer(buf1)]);
|
|
103
|
+
}
|
|
104
|
+
}
|