@aztec/foundation 0.7.2 → 0.7.4
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/.tsbuildinfo +1 -0
- package/dest/abi/abi.d.ts +237 -0
- package/dest/abi/abi.d.ts.map +1 -0
- package/dest/abi/abi.js +37 -0
- package/dest/abi/abi_coder.d.ts +8 -0
- package/dest/abi/abi_coder.d.ts.map +1 -0
- package/dest/abi/abi_coder.js +24 -0
- package/dest/abi/decoder.d.ts +57 -0
- package/dest/abi/decoder.d.ts.map +1 -0
- package/dest/abi/decoder.js +149 -0
- package/dest/abi/decoder.test.d.ts +2 -0
- package/dest/abi/decoder.test.d.ts.map +1 -0
- package/dest/abi/decoder.test.js +70 -0
- package/dest/abi/encoder.d.ts +10 -0
- package/dest/abi/encoder.d.ts.map +1 -0
- package/dest/abi/encoder.js +92 -0
- package/dest/abi/encoder.test.d.ts +2 -0
- package/dest/abi/encoder.test.d.ts.map +1 -0
- package/dest/abi/encoder.test.js +74 -0
- package/dest/abi/function_selector.d.ts +73 -0
- package/dest/abi/function_selector.d.ts.map +1 -0
- package/dest/abi/function_selector.js +104 -0
- package/dest/abi/index.d.ts +6 -0
- package/dest/abi/index.d.ts.map +1 -0
- package/dest/abi/index.js +6 -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 +114 -0
- package/dest/aztec-address/index.d.ts.map +1 -0
- package/dest/aztec-address/index.js +148 -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/committable/committable.d.ts +28 -0
- package/dest/committable/committable.d.ts.map +1 -0
- package/dest/committable/committable.js +41 -0
- package/dest/committable/committable.test.d.ts +2 -0
- package/dest/committable/committable.test.d.ts.map +1 -0
- package/dest/committable/committable.test.js +26 -0
- package/dest/committable/index.d.ts +2 -0
- package/dest/committable/index.d.ts.map +1 -0
- package/dest/committable/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 +95 -0
- package/dest/eth-address/index.d.ts +144 -0
- package/dest/eth-address/index.d.ts.map +1 -0
- package/dest/eth-address/index.js +228 -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 +177 -0
- package/dest/fields/fields.d.ts.map +1 -0
- package/dest/fields/fields.js +219 -0
- package/dest/fields/grumpkin_scalar.d.ts +96 -0
- package/dest/fields/grumpkin_scalar.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.js +125 -0
- package/dest/fields/grumpkin_scalar.test.d.ts +2 -0
- package/dest/fields/grumpkin_scalar.test.d.ts.map +1 -0
- package/dest/fields/grumpkin_scalar.test.js +45 -0
- package/dest/fields/index.d.ts +5 -0
- package/dest/fields/index.d.ts.map +1 -0
- package/dest/fields/index.js +5 -0
- package/dest/fields/point.d.ts +105 -0
- package/dest/fields/point.d.ts.map +1 -0
- package/dest/fields/point.js +125 -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 +144 -0
- package/dest/json-rpc/class_converter.d.ts.map +1 -0
- package/dest/json-rpc/class_converter.js +100 -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 +29 -0
- package/dest/json-rpc/client/json_rpc_client.d.ts.map +1 -0
- package/dest/json-rpc/client/json_rpc_client.js +104 -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 +28 -0
- package/dest/json-rpc/convert.d.ts.map +1 -0
- package/dest/json-rpc/convert.js +139 -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 +68 -0
- package/dest/json-rpc/fixtures/class_a.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_a.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_a.js +17 -0
- package/dest/json-rpc/fixtures/class_b.d.ts +11 -0
- package/dest/json-rpc/fixtures/class_b.d.ts.map +1 -0
- package/dest/json-rpc/fixtures/class_b.js +17 -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 +3 -0
- package/dest/json-rpc/index.d.ts.map +1 -0
- package/dest/json-rpc/index.js +3 -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 +36 -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 +133 -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 +11 -0
- package/dest/log/console.d.ts.map +1 -0
- package/dest/log/console.js +38 -0
- package/dest/log/debug.d.ts +43 -0
- package/dest/log/debug.d.ts.map +1 -0
- package/dest/log/debug.js +75 -0
- package/dest/log/index.d.ts +9 -0
- package/dest/log/index.d.ts.map +1 -0
- package/dest/log/index.js +5 -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/log/logger.d.ts +34 -0
- package/dest/log/logger.d.ts.map +1 -0
- package/dest/log/logger.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 +44 -0
- package/dest/retry/index.d.ts.map +1 -0
- package/dest/retry/index.js +89 -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/url/index.d.ts +9 -0
- package/dest/url/index.d.ts.map +1 -0
- package/dest/url/index.js +66 -0
- package/dest/wasm/empty_wasi_sdk.d.ts +130 -0
- package/dest/wasm/empty_wasi_sdk.d.ts.map +1 -0
- package/dest/wasm/empty_wasi_sdk.js +166 -0
- package/dest/wasm/index.d.ts +2 -0
- package/dest/wasm/index.d.ts.map +1 -0
- package/dest/wasm/index.js +2 -0
- package/dest/wasm/wasm_module.d.ts +135 -0
- package/dest/wasm/wasm_module.d.ts.map +1 -0
- package/dest/wasm/wasm_module.js +205 -0
- package/dest/wasm/wasm_module.test.d.ts +2 -0
- package/dest/wasm/wasm_module.test.d.ts.map +1 -0
- package/dest/wasm/wasm_module.test.js +24 -0
- package/dest/worker/browser/index.d.ts +3 -0
- package/dest/worker/browser/index.d.ts.map +1 -0
- package/dest/worker/browser/index.js +3 -0
- package/dest/worker/browser/start_web_module.d.ts +7 -0
- package/dest/worker/browser/start_web_module.d.ts.map +1 -0
- package/dest/worker/browser/start_web_module.js +22 -0
- package/dest/worker/browser/web_data_store.d.ts +23 -0
- package/dest/worker/browser/web_data_store.d.ts.map +1 -0
- package/dest/worker/browser/web_data_store.js +32 -0
- package/dest/worker/browser/web_worker.d.ts +10 -0
- package/dest/worker/browser/web_worker.d.ts.map +1 -0
- package/dest/worker/browser/web_worker.js +23 -0
- package/dest/worker/data_store.d.ts +20 -0
- package/dest/worker/data_store.d.ts.map +1 -0
- package/dest/worker/data_store.js +2 -0
- package/dest/worker/index.d.ts +3 -0
- package/dest/worker/index.d.ts.map +1 -0
- package/dest/worker/index.js +2 -0
- package/dest/worker/node/index.d.ts +3 -0
- package/dest/worker/node/index.d.ts.map +1 -0
- package/dest/worker/node/index.js +3 -0
- package/dest/worker/node/node_data_store.d.ts +12 -0
- package/dest/worker/node/node_data_store.d.ts.map +1 -0
- package/dest/worker/node/node_data_store.js +21 -0
- package/dest/worker/node/node_worker.d.ts +6 -0
- package/dest/worker/node/node_worker.d.ts.map +1 -0
- package/dest/worker/node/node_worker.js +20 -0
- package/dest/worker/node/start_node_module.d.ts +7 -0
- package/dest/worker/node/start_node_module.d.ts.map +1 -0
- package/dest/worker/node/start_node_module.js +26 -0
- package/dest/worker/wasm_worker.d.ts +9 -0
- package/dest/worker/wasm_worker.d.ts.map +1 -0
- package/dest/worker/wasm_worker.js +2 -0
- package/dest/worker/worker_pool.d.ts +40 -0
- package/dest/worker/worker_pool.d.ts.map +1 -0
- package/dest/worker/worker_pool.js +62 -0
- package/package.json +1 -1
- package/src/abi/abi.ts +8 -2
- package/src/abi/encoder.ts +5 -3
- package/Dockerfile +0 -15
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { keccak256String, randomBytes } from '../crypto/index.js';
|
|
2
|
+
import { Fr } from '../fields/index.js';
|
|
3
|
+
import { BufferReader } from '../serialize/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Represents an Ethereum address as a 20-byte buffer and provides various utility methods
|
|
6
|
+
* for converting between different representations, generating random addresses, validating
|
|
7
|
+
* checksums, and comparing addresses. EthAddress can be instantiated using a buffer or string,
|
|
8
|
+
* and can be serialized/deserialized from a buffer or BufferReader.
|
|
9
|
+
*/
|
|
10
|
+
export class EthAddress {
|
|
11
|
+
constructor(buffer) {
|
|
12
|
+
this.buffer = buffer;
|
|
13
|
+
if (buffer.length === 32) {
|
|
14
|
+
if (!buffer.slice(0, 12).equals(Buffer.alloc(12))) {
|
|
15
|
+
throw new Error(`Invalid address buffer: ${buffer.toString('hex')}`);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
this.buffer = buffer.slice(12);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
else if (buffer.length !== EthAddress.SIZE_IN_BYTES) {
|
|
22
|
+
throw new Error(`Expect buffer size to be ${EthAddress.SIZE_IN_BYTES}. Got ${buffer.length}.`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates an EthAddress instance from a valid Ethereum address string.
|
|
27
|
+
* The input 'address' can be either in checksum format or lowercase, and it can be prefixed with '0x'.
|
|
28
|
+
* Throws an error if the input is not a valid Ethereum address.
|
|
29
|
+
*
|
|
30
|
+
* @param address - The string representing the Ethereum address.
|
|
31
|
+
* @returns An EthAddress instance.
|
|
32
|
+
*/
|
|
33
|
+
static fromString(address) {
|
|
34
|
+
if (!EthAddress.isAddress(address)) {
|
|
35
|
+
throw new Error(`Invalid address string: ${address}`);
|
|
36
|
+
}
|
|
37
|
+
return new EthAddress(Buffer.from(address.replace(/^0x/i, ''), 'hex'));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create a random EthAddress instance with 20 random bytes.
|
|
41
|
+
* This method generates a new Ethereum address with a randomly generated set of 20 bytes.
|
|
42
|
+
* It is useful for generating test addresses or unique identifiers.
|
|
43
|
+
*
|
|
44
|
+
* @returns A randomly generated EthAddress instance.
|
|
45
|
+
*/
|
|
46
|
+
static random() {
|
|
47
|
+
return new EthAddress(randomBytes(20));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Determines if the given string represents a valid Ethereum address.
|
|
51
|
+
* A valid address should meet the following criteria:
|
|
52
|
+
* 1. Contains exactly 40 hex characters (excluding an optional '0x' prefix).
|
|
53
|
+
* 2. Is either all lowercase, all uppercase, or has a valid checksum based on EIP-55.
|
|
54
|
+
*
|
|
55
|
+
* @param address - The string to be checked for validity as an Ethereum address.
|
|
56
|
+
* @returns True if the input string represents a valid Ethereum address, false otherwise.
|
|
57
|
+
*/
|
|
58
|
+
static isAddress(address) {
|
|
59
|
+
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
|
60
|
+
// Does not have the basic requirements of an address.
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
else if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
|
|
64
|
+
// It's ALL lowercase or ALL upppercase.
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return EthAddress.checkAddressChecksum(address);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Checks if the EthAddress instance represents a zero address.
|
|
73
|
+
* A zero address consists of 20 bytes filled with zeros and is considered an invalid address.
|
|
74
|
+
*
|
|
75
|
+
* @returns A boolean indicating whether the EthAddress instance is a zero address or not.
|
|
76
|
+
*/
|
|
77
|
+
isZero() {
|
|
78
|
+
return this.equals(EthAddress.ZERO);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the given Ethereum address has a valid checksum.
|
|
82
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 40 hex characters.
|
|
83
|
+
* Returns true if the address has a valid checksum, false otherwise.
|
|
84
|
+
*
|
|
85
|
+
* @param address - The hex-encoded string representing the Ethereum address.
|
|
86
|
+
* @returns A boolean value indicating whether the address has a valid checksum.
|
|
87
|
+
*/
|
|
88
|
+
static checkAddressChecksum(address) {
|
|
89
|
+
address = address.replace(/^0x/i, '');
|
|
90
|
+
const addressHash = keccak256String(address.toLowerCase());
|
|
91
|
+
for (let i = 0; i < 40; i++) {
|
|
92
|
+
// The nth letter should be uppercase if the nth digit of casemap is 1.
|
|
93
|
+
if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) ||
|
|
94
|
+
(parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Converts an Ethereum address to its checksum format.
|
|
102
|
+
* The input 'address' should be prefixed with '0x' or not, and have exactly 40 hex characters.
|
|
103
|
+
* The checksum format is created by capitalizing certain characters in the hex string
|
|
104
|
+
* based on the hash of the lowercase address.
|
|
105
|
+
* Throws an error if the input address is invalid.
|
|
106
|
+
*
|
|
107
|
+
* @param address - The Ethereum address as a hex-encoded string.
|
|
108
|
+
* @returns The Ethereum address in its checksum format.
|
|
109
|
+
*/
|
|
110
|
+
static toChecksumAddress(address) {
|
|
111
|
+
if (!EthAddress.isAddress(address)) {
|
|
112
|
+
throw new Error('Invalid address string.');
|
|
113
|
+
}
|
|
114
|
+
address = address.toLowerCase().replace(/^0x/i, '');
|
|
115
|
+
const addressHash = keccak256String(address);
|
|
116
|
+
let checksumAddress = '0x';
|
|
117
|
+
for (let i = 0; i < address.length; i++) {
|
|
118
|
+
// If ith character is 9 to f then make it uppercase.
|
|
119
|
+
if (parseInt(addressHash[i], 16) > 7) {
|
|
120
|
+
checksumAddress += address[i].toUpperCase();
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
checksumAddress += address[i];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return checksumAddress;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Checks whether the given EthAddress instance is equal to the current instance.
|
|
130
|
+
* Equality is determined by comparing the underlying byte buffers of both instances.
|
|
131
|
+
*
|
|
132
|
+
* @param rhs - The EthAddress instance to compare with the current instance.
|
|
133
|
+
* @returns A boolean value indicating whether the two instances are equal (true) or not (false).
|
|
134
|
+
*/
|
|
135
|
+
equals(rhs) {
|
|
136
|
+
return this.buffer.equals(rhs.buffer);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Converts the Ethereum address to a hex-encoded string.
|
|
140
|
+
* The resulting string is prefixed with '0x' and has exactly 40 hex characters.
|
|
141
|
+
* This method can be used to represent the EthAddress instance in the widely used hexadecimal format.
|
|
142
|
+
*
|
|
143
|
+
* @returns A hex-encoded string representation of the Ethereum address.
|
|
144
|
+
*/
|
|
145
|
+
toString() {
|
|
146
|
+
return `0x${this.buffer.toString('hex')}`;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Returns the Ethereum address as a checksummed string.
|
|
150
|
+
* The output string will have characters in the correct upper or lowercase form, according to EIP-55.
|
|
151
|
+
* This provides a way to verify if an address is typed correctly, by checking the character casing.
|
|
152
|
+
*
|
|
153
|
+
* @returns A checksummed Ethereum address string.
|
|
154
|
+
*/
|
|
155
|
+
toChecksumString() {
|
|
156
|
+
return EthAddress.toChecksumAddress(this.buffer.toString('hex'));
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Alias for toBuffer32.
|
|
160
|
+
* @returns A 32-byte Buffer containing the padded Ethereum address.
|
|
161
|
+
*/
|
|
162
|
+
toBuffer() {
|
|
163
|
+
return this.toBuffer32();
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Returns the internal Buffer representation of the Ethereum address.
|
|
167
|
+
* This method is useful when working with raw binary data or when
|
|
168
|
+
* integrating with other modules that require a Buffer as input.
|
|
169
|
+
*
|
|
170
|
+
* @returns A Buffer instance containing the 20-byte Ethereum address.
|
|
171
|
+
*/
|
|
172
|
+
toBuffer20() {
|
|
173
|
+
return this.buffer;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Returns a 32-byte buffer representation of the Ethereum address, with the original 20-byte address
|
|
177
|
+
* occupying the last 20 bytes and the first 12 bytes being zero-filled.
|
|
178
|
+
* This format is commonly used in smart contracts when handling addresses as 32-byte values.
|
|
179
|
+
*
|
|
180
|
+
* @returns A 32-byte Buffer containing the padded Ethereum address.
|
|
181
|
+
*/
|
|
182
|
+
toBuffer32() {
|
|
183
|
+
const buffer = Buffer.alloc(32);
|
|
184
|
+
this.buffer.copy(buffer, 12);
|
|
185
|
+
return buffer;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Returns a new field with the same contents as this EthAddress.
|
|
189
|
+
*
|
|
190
|
+
* @returns An Fr instance.
|
|
191
|
+
*/
|
|
192
|
+
toField() {
|
|
193
|
+
return Fr.fromBuffer(this.toBuffer32());
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Converts a field to a eth address.
|
|
197
|
+
* @param fr - The field to convert.
|
|
198
|
+
* @returns The eth address.
|
|
199
|
+
*/
|
|
200
|
+
static fromField(fr) {
|
|
201
|
+
return new EthAddress(fr.toBuffer().slice(-EthAddress.SIZE_IN_BYTES));
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Deserializes from a buffer or reader, corresponding to a write in cpp.
|
|
205
|
+
* @param buffer - Buffer to read from.
|
|
206
|
+
* @returns The EthAdress.
|
|
207
|
+
*/
|
|
208
|
+
static fromBuffer(buffer) {
|
|
209
|
+
const reader = BufferReader.asReader(buffer);
|
|
210
|
+
return new EthAddress(reader.readBuffer());
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Friendly representation for debugging purposes.
|
|
214
|
+
* @returns A hex string representing the address.
|
|
215
|
+
*/
|
|
216
|
+
toFriendlyJSON() {
|
|
217
|
+
return this.toString();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* The size of an Ethereum address in bytes.
|
|
222
|
+
*/
|
|
223
|
+
EthAddress.SIZE_IN_BYTES = 20;
|
|
224
|
+
/**
|
|
225
|
+
* Represents a zero Ethereum address with 20 bytes filled with zeros.
|
|
226
|
+
*/
|
|
227
|
+
EthAddress.ZERO = new EthAddress(Buff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DQUFDO0lBQzdDLENBQUM7SUFFRDs7O09BR0c7SUFDSCxjQUFjO1FBQ1osT0FBTyxJQUFJLENBQUMsUUFBUSxFQUFFLENBQUM7SUFDekIsQ0FBQzs7QUF2T0Q7O0dBRUc7QUFDVyx3QkFBYSxHQUFHLEVBQUUsQ0FBQztBQUNqQzs7R0FFRztBQUNXLGVBQUksR0FBRyxJQUFJLFVBQVUsQ0FBQyxNQUFNLENBQUMsS0FBSyxDQUFDLFVBQVUsQ0FBQyxhQUFhLENBQUMsQ0FBQyxDQUFDIn0=
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Tuple } from '../serialize/types.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
/**
|
|
5
|
+
* Class to wrap a single point coordinate.
|
|
6
|
+
* This class handles the complexities of representing point coordinates as 32 byte buffers as well as fields.
|
|
7
|
+
* The coordinate value is split across 2 fields to ensure that the max size of a field is not breached.
|
|
8
|
+
* This is achieved by placing the most significant byte of the lower field into the least significant byte of the higher field.
|
|
9
|
+
* Calls to 'toBuffer' or 'toBigInt' undo this change and simply return the original 32 byte value.
|
|
10
|
+
* Calls to 'toFieldsBuffer' will return a 64 bytes buffer containing the serialised fields.
|
|
11
|
+
*/
|
|
12
|
+
export declare class Coordinate {
|
|
13
|
+
/**
|
|
14
|
+
* The fields of the coordinate value. Least significant limb at index 0.
|
|
15
|
+
*/
|
|
16
|
+
fields: Tuple<Fr, 2>;
|
|
17
|
+
static ZERO: Coordinate;
|
|
18
|
+
constructor(
|
|
19
|
+
/**
|
|
20
|
+
* The fields of the coordinate value. Least significant limb at index 0.
|
|
21
|
+
*/
|
|
22
|
+
fields: Tuple<Fr, 2>);
|
|
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
|
+
/**
|
|
29
|
+
* Generates a random coordinate value
|
|
30
|
+
* @returns The random coordinate
|
|
31
|
+
*/
|
|
32
|
+
static random(): Coordinate;
|
|
33
|
+
/**
|
|
34
|
+
* Serialises the oblect to buffer of 2 fields.
|
|
35
|
+
* @returns A buffer serialisation of the object.
|
|
36
|
+
*/
|
|
37
|
+
toFieldsBuffer(): Buffer;
|
|
38
|
+
/**
|
|
39
|
+
* Serialises the coordinate to a single 32 byte buffer.
|
|
40
|
+
* @returns A buffer serialisation of the object.
|
|
41
|
+
*/
|
|
42
|
+
toBuffer(): Buffer;
|
|
43
|
+
/**
|
|
44
|
+
* Returns true if this coordinate is equal to the one provided
|
|
45
|
+
* @param other - The coordinate against which to compare
|
|
46
|
+
* @returns True if the coordinates are the same, false otherwise
|
|
47
|
+
*/
|
|
48
|
+
equals(other: Coordinate): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the coordinate's value as a bigint
|
|
51
|
+
* @returns The coordinate value as a bigint
|
|
52
|
+
*/
|
|
53
|
+
toBigInt(): bigint;
|
|
54
|
+
/**
|
|
55
|
+
* Creates a coordinate object from a 32 byte coordinate value
|
|
56
|
+
* @param coordinate - A buffer containing the 32 byte coordinate value
|
|
57
|
+
* @returns The new coordinate object
|
|
58
|
+
*/
|
|
59
|
+
static fromBuffer(coordinate: Buffer): Coordinate;
|
|
60
|
+
/**
|
|
61
|
+
* Creates a coordinate object from a field
|
|
62
|
+
* @param coordinate - The field containing the coordinate
|
|
63
|
+
* @returns The new coordinate object
|
|
64
|
+
*/
|
|
65
|
+
static fromField(coordinate: Fr): Coordinate;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=coordinate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinate.d.ts","sourceRoot":"","sources":["../../src/fields/coordinate.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEjC;;;;;;;GAOG;AACH,qBAAa,UAAU;IAInB;;OAEG;IACI,MAAM,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;IAN7B,MAAM,CAAC,IAAI,aAAsC;;IAG/C;;OAEG;IACI,MAAM,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;IAG7B;;;OAGG;IACH,QAAQ,IAAI,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;IAIxB;;;OAGG;IACH,MAAM,CAAC,MAAM,IAAI,UAAU;IAI3B;;;OAGG;IACH,cAAc,IAAI,MAAM;IAIxB;;;OAGG;IACH,QAAQ,IAAI,MAAM;IAOlB;;;;OAIG;IACH,MAAM,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO;IAIlC;;;OAGG;IACH,QAAQ,IAAI,MAAM;IAIlB;;;;OAIG;IACH,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,MAAM;IAYpC;;;;OAIG;IACH,MAAM,CAAC,SAAS,CAAC,UAAU,EAAE,EAAE;CAOhC"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { toBigIntBE } from '../bigint-buffer/index.js';
|
|
2
|
+
import { Fr } from './fields.js';
|
|
3
|
+
/**
|
|
4
|
+
* Class to wrap a single point coordinate.
|
|
5
|
+
* This class handles the complexities of representing point coordinates as 32 byte buffers as well as fields.
|
|
6
|
+
* The coordinate value is split across 2 fields to ensure that the max size of a field is not breached.
|
|
7
|
+
* This is achieved by placing the most significant byte of the lower field into the least significant byte of the higher field.
|
|
8
|
+
* Calls to 'toBuffer' or 'toBigInt' undo this change and simply return the original 32 byte value.
|
|
9
|
+
* Calls to 'toFieldsBuffer' will return a 64 bytes buffer containing the serialised fields.
|
|
10
|
+
*/
|
|
11
|
+
export class Coordinate {
|
|
12
|
+
constructor(
|
|
13
|
+
/**
|
|
14
|
+
* The fields of the coordinate value. Least significant limb at index 0.
|
|
15
|
+
*/
|
|
16
|
+
fields) {
|
|
17
|
+
this.fields = fields;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Converts the coordinate data into a tuple of fields
|
|
21
|
+
* @returns A tuple of the coordinate fields
|
|
22
|
+
*/
|
|
23
|
+
toFields() {
|
|
24
|
+
return this.fields;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Generates a random coordinate value
|
|
28
|
+
* @returns The random coordinate
|
|
29
|
+
*/
|
|
30
|
+
static random() {
|
|
31
|
+
return this.fromField(Fr.random());
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Serialises the oblect to buffer of 2 fields.
|
|
35
|
+
* @returns A buffer serialisation of the object.
|
|
36
|
+
*/
|
|
37
|
+
toFieldsBuffer() {
|
|
38
|
+
return Buffer.concat([this.fields[0].toBuffer(), this.fields[1].toBuffer()]);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Serialises the coordinate to a single 32 byte buffer.
|
|
42
|
+
* @returns A buffer serialisation of the object.
|
|
43
|
+
*/
|
|
44
|
+
toBuffer() {
|
|
45
|
+
const buf0 = this.fields[0].toBuffer();
|
|
46
|
+
const buf1 = this.fields[1].toBuffer();
|
|
47
|
+
buf0[0] = buf1[31];
|
|
48
|
+
return buf0;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns true if this coordinate is equal to the one provided
|
|
52
|
+
* @param other - The coordinate against which to compare
|
|
53
|
+
* @returns True if the coordinates are the same, false otherwise
|
|
54
|
+
*/
|
|
55
|
+
equals(other) {
|
|
56
|
+
return this.toBigInt() === other.toBigInt();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns the coordinate's value as a bigint
|
|
60
|
+
* @returns The coordinate value as a bigint
|
|
61
|
+
*/
|
|
62
|
+
toBigInt() {
|
|
63
|
+
return toBigIntBE(this.toBuffer());
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates a coordinate object from a 32 byte coordinate value
|
|
67
|
+
* @param coordinate - A buffer containing the 32 byte coordinate value
|
|
68
|
+
* @returns The new coordinate object
|
|
69
|
+
*/
|
|
70
|
+
static fromBuffer(coordinate) {
|
|
71
|
+
if (coordinate.length != 32) {
|
|
72
|
+
throw new Error(`Invalid size of coordinate buffer`);
|
|
73
|
+
}
|
|
74
|
+
const buf0 = Buffer.alloc(32);
|
|
75
|
+
coordinate.copy(buf0, 0, 0, 32);
|
|
76
|
+
const buf1 = Buffer.alloc(32);
|
|
77
|
+
buf1[31] = buf0[0];
|
|
78
|
+
buf0[0] = 0;
|
|
79
|
+
return new Coordinate([Fr.fromBuffer(buf0), Fr.fromBuffer(buf1)]);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a coordinate object from a field
|
|
83
|
+
* @param coordinate - The field containing the coordinate
|
|
84
|
+
* @returns The new coordinate object
|
|
85
|
+
*/
|
|
86
|
+
static fromField(coordinate) {
|
|
87
|
+
const buf0 = coordinate.toBuffer();
|
|
88
|
+
const buf1 = Buffer.alloc(32);
|
|
89
|
+
buf1[31] = buf0[0];
|
|
90
|
+
buf0[0] = 0;
|
|
91
|
+
return new Coordinate([Fr.fromBuffer(buf0), Fr.fromBuffer(buf1)]);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
Coordinate.ZERO = new Coordinate([Fr.ZERO, Fr.ZERO]);
|
|
95
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29vcmRpbmF0ZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9maWVsZHMvY29vcmRpbmF0ZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sMkJBQTJCLENBQUM7QUFFdkQsT0FBTyxFQUFFLEVBQUUsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUVqQzs7Ozs7OztHQU9HO0FBQ0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UFDLEVBQUUsQ0FBQyxHQUFHLElBQUksQ0FBQyxDQUFDLENBQUMsQ0FBQztRQUNuQixJQUFJLENBQUMsQ0FBQyxDQUFDLEdBQUcsQ0FBQyxDQUFDO1FBQ1osT0FBTyxJQUFJLFVBQVUsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxVQUFVLENBQUMsSUFBSSxDQUFDLEVBQUUsRUFBRSxDQUFDLFVBQVUsQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLENBQUM7SUFDcEUsQ0FBQzs7QUF6Rk0sZUFBSSxHQUFHLElBQUksVUFBVSxDQUFDLENBQUMsRUFBRSxDQUFDLElBQUksRUFBRSxFQUFFLENBQUMsSUFBSSxDQUFDLENBQUMsQ0FBQyJ9
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coordinate.test.d.ts","sourceRoot":"","sources":["../../src/fields/coordinate.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { toBufferBE } from '../bigint-buffer/index.js';
|
|
2
|
+
import { Coordinate } from './coordinate.js';
|
|
3
|
+
import { Fr } from './fields.js';
|
|
4
|
+
const MAX_256_VALUE = 2n ** 256n - 1n;
|
|
5
|
+
describe('coordinate', () => {
|
|
6
|
+
it('stores 256 bits in fields', () => {
|
|
7
|
+
const max256Value = toBufferBE(MAX_256_VALUE, 32);
|
|
8
|
+
const coordinate = Coordinate.fromBuffer(max256Value);
|
|
9
|
+
// this returns a buffer containing the bit pattern split across 2 fields
|
|
10
|
+
expect(coordinate.toFieldsBuffer()).toEqual(Buffer.concat([Buffer.alloc(1, 0), Buffer.alloc(31, 0xff), Buffer.alloc(31, 0), Buffer.alloc(1, 0xff)]));
|
|
11
|
+
// this returns the value in a single 32 byte buffer
|
|
12
|
+
expect(coordinate.toBuffer()).toEqual(max256Value);
|
|
13
|
+
// this returns the value as a big int
|
|
14
|
+
expect(coordinate.toBigInt()).toBe(MAX_256_VALUE);
|
|
15
|
+
});
|
|
16
|
+
it('can be constructed from a field', () => {
|
|
17
|
+
const field = Fr.random();
|
|
18
|
+
const coordinate = Coordinate.fromField(field);
|
|
19
|
+
expect(coordinate.toBuffer()).toEqual(field.toBuffer());
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29vcmRpbmF0ZS50ZXN0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2ZpZWxkcy9jb29yZGluYXRlLnRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLDJCQUEyQixDQUFDO0FBQ3ZELE9BQU8sRUFBRSxVQUFVLEVBQUUsTUFBTSxpQkFBaUIsQ0FBQztBQUM3QyxPQUFPLEVBQUUsRUFBRSxFQUFFLE1BQU0sYUFBYSxDQUFDO0FBRWpDLE1BQU0sYUFBYSxHQUFHLEVBQUUsSUFBSSxJQUFJLEdBQUcsRUFBRSxDQUFDO0FBRXRDLFFBQVEsQ0FBQyxZQUFZLEVBQUUsR0FBRyxFQUFFO0lBQzFCLEVBQUUsQ0FBQywyQkFBMkIsRUFBRSxHQUFHLEVBQUU7UUFDbkMsTUFBTSxXQUFXLEdBQUcsVUFBVSxDQUFDLGFBQWEsRUFBRSxFQUFFLENBQUMsQ0FBQztRQUNsRCxNQUFNLFVBQVUsR0FBRyxVQUFVLENBQUMsVUFBVSxDQUFDLFdBQVcsQ0FBQyxDQUFDO1FBQ3RELHlFQUF5RTtRQUN6RSxNQUFNLENBQUMsVUFBVSxDQUFDLGNBQWMsRUFBRSxDQUFDLENBQUMsT0FBTyxDQUN6QyxNQUFNLENBQUMsTUFBTSxDQUFDLENBQUMsTUFBTSxDQUFDLEtBQUssQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLEVBQUUsTUFBTSxDQUFDLEtBQUssQ0FBQyxFQUFFLEVBQUUsSUFBSSxDQUFDLEVBQUUsTUFBTSxDQUFDLEtBQUssQ0FBQyxFQUFFLEVBQUUsQ0FBQyxDQUFDLEVBQUUsTUFBTSxDQUFDLEtBQUssQ0FBQyxDQUFDLEVBQUUsSUFBSSxDQUFDLENBQUMsQ0FBQyxDQUN4RyxDQUFDO1FBQ0Ysb0RBQW9EO1FBQ3BELE1BQU0sQ0FBQyxVQUFVLENBQUMsUUFBUSxFQUFFLENBQUMsQ0FBQyxPQUFPLENBQUMsV0FBVyxDQUFDLENBQUM7UUFDbkQsc0NBQXNDO1FBQ3RDLE1BQU0sQ0FBQyxVQUFVLENBQUMsUUFBUSxFQUFFLENBQUMsQ0FBQyxJQUFJLENBQUMsYUFBYSxDQUFDLENBQUM7SUFDcEQsQ0FBQyxDQUFDLENBQUM7SUFFSCxFQUFFLENBQUMsaUNBQWlDLEVBQUUsR0FBRyxFQUFFO1FBQ3pDLE1BQU0sS0FBSyxHQUFHLEVBQUUsQ0FBQyxNQUFNLEVBQUUsQ0FBQztRQUMxQixNQUFNLFVBQVUsR0FBRyxVQUFVLENBQUMsU0FBUyxDQUFDLEtBQUssQ0FBQyxDQUFDO1FBQy9DLE1BQU0sQ0FBQyxVQUFVLENBQUMsUUFBUSxFQUFFLENBQUMsQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDLFFBQVEsRUFBRSxDQUFDLENBQUM7SUFDMUQsQ0FBQyxDQUFDLENBQUM7QUFDTCxDQUFDLENBQUMsQ0FBQyJ9
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { AztecAddress } from '../aztec-address/index.js';
|
|
3
|
+
import { BufferReader } from '../serialize/buffer_reader.js';
|
|
4
|
+
/**
|
|
5
|
+
* Fr represents a field of integers modulo the prime number MODULUS.
|
|
6
|
+
* It provides utility functions to work with elements in this field, such as conversions between different representations and checks for equality and zero values. The elements can be serialized to and deserialized from byte buffers or strings.
|
|
7
|
+
* Some use cases include working with cryptographic operations and finite fields.
|
|
8
|
+
*/
|
|
9
|
+
export declare class Fr {
|
|
10
|
+
static ZERO: Fr;
|
|
11
|
+
static MODULUS: bigint;
|
|
12
|
+
static MAX_VALUE: bigint;
|
|
13
|
+
static SIZE_IN_BYTES: number;
|
|
14
|
+
/**
|
|
15
|
+
* The numeric value of the field element as a bigint.
|
|
16
|
+
*/
|
|
17
|
+
readonly value: bigint;
|
|
18
|
+
constructor(value: bigint | number | Fr | AztecAddress);
|
|
19
|
+
/**
|
|
20
|
+
* Generates a random Fr or Fq instance with a value modulo the respective class' MODULUS.
|
|
21
|
+
* This method uses randomBytes to generate a random 32-byte buffer, converts it to a bigint
|
|
22
|
+
* and takes the modulus of the result with the class' MODULUS constant.
|
|
23
|
+
*
|
|
24
|
+
* @returns A new Fr or Fq instance with a random value.
|
|
25
|
+
*/
|
|
26
|
+
static random(): Fr;
|
|
27
|
+
/**
|
|
28
|
+
* Returns a new zero-value field.
|
|
29
|
+
* @returns A new zero-value field.
|
|
30
|
+
*/
|
|
31
|
+
static zero(): Fr;
|
|
32
|
+
/**
|
|
33
|
+
* Create an instance of the corresponding class (Fr or Fq) from a Buffer or a BufferReader.
|
|
34
|
+
* Reads 'SIZE_IN_BYTES' bytes from the given Buffer or BufferReader and constructs an instance with the decoded value.
|
|
35
|
+
* If the input is a Buffer, it is internally converted to a BufferReader before reading.
|
|
36
|
+
* Throws an error if the input length is invalid or the decoded value is out of range.
|
|
37
|
+
*
|
|
38
|
+
* @param buffer - The Buffer or BufferReader containing the bytes representing the value.
|
|
39
|
+
* @returns An instance of the corresponding class (Fr or Fq) with the decoded value.
|
|
40
|
+
*/
|
|
41
|
+
static fromBuffer(buffer: Buffer | BufferReader): Fr;
|
|
42
|
+
/**
|
|
43
|
+
* Create a Fr instance from a hex-encoded string.
|
|
44
|
+
* The input 'address' can be either prefixed with '0x' or not, and should have exactly 64 hex characters.
|
|
45
|
+
* Throws an error if the input length is invalid or the address value is out of range.
|
|
46
|
+
*
|
|
47
|
+
* @param address - The hex-encoded string representing the field element.
|
|
48
|
+
* @returns A Fr instance.
|
|
49
|
+
*/
|
|
50
|
+
static fromString(address: string): Fr;
|
|
51
|
+
/**
|
|
52
|
+
* Converts the value of the instance to a buffer with a specified length.
|
|
53
|
+
* The method uses the provided value and size in bytes to create a buffer representation
|
|
54
|
+
* of the numeric value. This can be useful for serialization and communication purposes.
|
|
55
|
+
*
|
|
56
|
+
* @returns A buffer representing the instance's value.
|
|
57
|
+
*/
|
|
58
|
+
toBuffer(): Buffer;
|
|
59
|
+
/**
|
|
60
|
+
* Converts the value of the Fr or Fq class instance to a hexadecimal string.
|
|
61
|
+
* The resulting string is prefixed with '0x' and represents the bigint value
|
|
62
|
+
* in base 16.
|
|
63
|
+
*
|
|
64
|
+
* @param padTo32 - Whether to pad the string to 32 bytes.
|
|
65
|
+
* @returns A hex-encoded string representing the value of the class instance.
|
|
66
|
+
*/
|
|
67
|
+
toString(padTo32?: boolean): `0x${string}`;
|
|
68
|
+
/**
|
|
69
|
+
* Retrieves the underlying bigint.
|
|
70
|
+
* This method mostly exists to match user expectations, as value is already public.
|
|
71
|
+
* @returns The underlying bigint.
|
|
72
|
+
*/
|
|
73
|
+
toBigInt(): bigint;
|
|
74
|
+
/**
|
|
75
|
+
* Returns a shortened string representation of the Fr value, formatted with '0x' prefix and ellipsis in the middle.
|
|
76
|
+
* The resulting string has first 10 characters (including '0x') and last 4 characters of the full hexadecimal value.
|
|
77
|
+
*
|
|
78
|
+
* @returns A shorter, human-readable string representation of the Fr value.
|
|
79
|
+
*/
|
|
80
|
+
toShortString(): string;
|
|
81
|
+
/**
|
|
82
|
+
* Checks if the provided Fr instance is equal to the current instance.
|
|
83
|
+
* Two instances are considered equal if their 'value' properties are the same.
|
|
84
|
+
*
|
|
85
|
+
* @param rhs - The Fr instance to compare with the current instance.
|
|
86
|
+
* @returns A boolean indicating whether the two instances are equal.
|
|
87
|
+
*/
|
|
88
|
+
equals(rhs: Fr): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Check if the instance value is zero.
|
|
91
|
+
* The method returns true if the value of the instance is 0n (zero in BigInt representation),
|
|
92
|
+
* otherwise, it returns false.
|
|
93
|
+
*
|
|
94
|
+
* @returns A boolean indicating whether the instance value is zero or not.
|
|
95
|
+
*/
|
|
96
|
+
isZero(): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Converts the current value of the Fq or Fr instance to a friendly JSON representation.
|
|
99
|
+
* The output will be a hexadecimal string prefixed with '0x'.
|
|
100
|
+
*
|
|
101
|
+
* @returns A '0x' prefixed hexadecimal string representing the current value.
|
|
102
|
+
*/
|
|
103
|
+
toFriendlyJSON(): `0x${string}`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Fq represents a field element in a prime finite field with modulus defined by the constant MODULUS.
|
|
107
|
+
* It provides methods for creating, manipulating, and comparing field elements, as well as converting
|
|
108
|
+
* them to/from different data types like Buffers and hex strings. Field elements are used in various
|
|
109
|
+
* cryptographic protocols and operations, such as elliptic curve cryptography.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* const fqElem = new Fq(BigInt("123456789"));
|
|
113
|
+
* const randomFqElem = Fq.random();
|
|
114
|
+
* const fromBufferFqElem = Fq.fromBuffer(buffer);
|
|
115
|
+
*/
|
|
116
|
+
export declare class Fq {
|
|
117
|
+
/**
|
|
118
|
+
* The element's value as a bigint in the finite field.
|
|
119
|
+
*/
|
|
120
|
+
readonly value: bigint;
|
|
121
|
+
static MODULUS: bigint;
|
|
122
|
+
static MAX_VALUE: bigint;
|
|
123
|
+
static SIZE_IN_BYTES: number;
|
|
124
|
+
constructor(
|
|
125
|
+
/**
|
|
126
|
+
* The element's value as a bigint in the finite field.
|
|
127
|
+
*/
|
|
128
|
+
value: bigint);
|
|
129
|
+
/**
|
|
130
|
+
* Generates a random Fr or Fq instance with a value within the range of their respective modulus.
|
|
131
|
+
* The random value is generated from a byte array of length equal to SIZE_IN_BYTES, then truncated
|
|
132
|
+
* to the appropriate modulus before creating the new Fr or Fq instance.
|
|
133
|
+
*
|
|
134
|
+
* @returns A new Fr or Fq instance with a randomly generated value.
|
|
135
|
+
*/
|
|
136
|
+
static random(): Fq;
|
|
137
|
+
/**
|
|
138
|
+
* Create an instance of the calling class (Fr or Fq) from a given buffer or BufferReader.
|
|
139
|
+
* Reads SIZE_IN_BYTES from the provided buffer and converts it to a bigint, then creates a new instance
|
|
140
|
+
* with that value. Throws an error if the value is out of range for the calling class.
|
|
141
|
+
*
|
|
142
|
+
* @param buffer - The input buffer or BufferReader containing the bytes representing the value.
|
|
143
|
+
* @returns An instance of the calling class (Fr or Fq) initialized with the bigint value.
|
|
144
|
+
*/
|
|
145
|
+
static fromBuffer(buffer: Buffer | BufferReader): Fq;
|
|
146
|
+
/**
|
|
147
|
+
* Converts the bigint value of the instance to a Buffer representation.
|
|
148
|
+
* The output buffer has a fixed size, determined by the 'SIZE_IN_BYTES' constant.
|
|
149
|
+
*
|
|
150
|
+
* @returns A Buffer containing the byte representation of the instance's value.
|
|
151
|
+
*/
|
|
152
|
+
toBuffer(): Buffer;
|
|
153
|
+
/**
|
|
154
|
+
* Converts the Fq value to a hexadecimal string representation.
|
|
155
|
+
* The resulting string is prefixed with '0x' and contains the exact number of hex characters required
|
|
156
|
+
* to represent the numeric value of this instance.
|
|
157
|
+
*
|
|
158
|
+
* @returns A hexadecimal string representing the Fq value.
|
|
159
|
+
*/
|
|
160
|
+
toString(): `0x${string}`;
|
|
161
|
+
/**
|
|
162
|
+
* Check if the value of the current instance is zero.
|
|
163
|
+
* This function compares the internal 'value' property with 0n (BigInt representation of zero).
|
|
164
|
+
* Returns true if the value is zero, otherwise returns false.
|
|
165
|
+
*
|
|
166
|
+
* @returns A boolean indicating whether the value is zero or not.
|
|
167
|
+
*/
|
|
168
|
+
isZero(): boolean;
|
|
169
|
+
/**
|
|
170
|
+
* Converts the value of the Fr or Fq instance to a friendly JSON format.
|
|
171
|
+
* The output is a hexadecimal string representation of the value with '0x' prefix.
|
|
172
|
+
*
|
|
173
|
+
* @returns A string representing the value in the JSON format.
|
|
174
|
+
*/
|
|
175
|
+
toFriendlyJSON(): `0x${string}`;
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=fields.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../src/fields/fields.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAGzD,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAE7D;;;;GAIG;AACH,qBAAa,EAAE;IACb,MAAM,CAAC,IAAI,KAAc;IACzB,MAAM,CAAC,OAAO,SAAuE;IACrF,MAAM,CAAC,SAAS,SAAmB;IACnC,MAAM,CAAC,aAAa,SAAM;IAE1B;;OAEG;IACH,SAAgB,KAAK,EAAE,MAAM,CAAC;gBAElB,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,EAAE,GAAG,YAAY;IAQtD;;;;;;OAMG;IACH,MAAM,CAAC,MAAM;IAKb;;;OAGG;IACH,MAAM,CAAC,IAAI;IAIX;;;;;;;;OAQG;IACH,MAAM,CAAC,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,YAAY;IAK/C;;;;;;;OAOG;IACH,MAAM,CAAC,UAAU,CAAC,OAAO,EAAE,MAAM;IAIjC;;;;;;OAMG;IACH,QAAQ;IAIR;;;;;;;OAOG;IACH,QAAQ,CAAC,OAAO,UAAQ,GAAG,KAAK,MAAM,EAAE;IAIxC;;;;OAIG;IACI,QAAQ,IAAI,MAAM;IAIzB;;;;;OAKG;IACH,aAAa;IAKb;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,EAAE,EAAE;IAId;;;;;;OAMG;IACH,MAAM;IAIN;;;;;OAKG;IACH,cAAc;CAGf;AAED;;;;;;;;;;GAUG;AACH,qBAAa,EAAE;IAMX;;OA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