|
| 1 | +## @randamu/blocklock-agent |
| 2 | + |
| 3 | +The `blocklock-agent` is a blockchain-based timelock decryption and signature agent leveraging BLS signatures and blocklock mechanisms on the Ethereum-compatible Filecoin network. This agent listens for decryption requests and processes them based on predefined conditions. |
| 4 | + |
| 5 | +### Features |
| 6 | + |
| 7 | +- **Event Listener**: Listens for `DecryptionRequested` events from the `DecryptionSender` smart contract. |
| 8 | +- **BLS Cryptography**: Utilizes BLS signatures for secure and efficient cryptographic operations. |
| 9 | +- **IBE Decryption**: Implements identity-based encryption (IBE) to handle requests securely. |
| 10 | +- **Health Check**: Provides an HTTP health-check endpoint. |
| 11 | +- **Event Replay**: Replays past unfulfilled decryption requests upon startup. |
| 12 | + |
| 13 | +### Requirements |
| 14 | + |
| 15 | +- Node.js version 16+. |
| 16 | +- An Ethereum-compatible blockchain node (e.g., Filecoin calibration network). |
| 17 | +- Access to the deployed `DecryptionSender` contract address. |
| 18 | + |
| 19 | +### Installation |
| 20 | + |
| 21 | +1. Clone the repository: |
| 22 | + ```bash |
| 23 | + git clone <repository-url> |
| 24 | + cd blocklock-agent |
| 25 | + ``` |
| 26 | + |
| 27 | +2. Install dependencies: |
| 28 | + ```bash |
| 29 | + npm install |
| 30 | + ``` |
| 31 | + |
| 32 | +### Configuration |
| 33 | + |
| 34 | +The agent can be configured using command-line arguments, environment variables, or a combination of both. Below are the available options: |
| 35 | + |
| 36 | +| Option | Default Value | Environment Variable | Description | |
| 37 | +|--------------------------|-----------------------------------------------|------------------------------------------------|--------------------------------------------------------------------------------------------------------------------| |
| 38 | +| `--port` | `8080` | `BLOCKLOCK_PORT` | The port to host the health-check HTTP server. | |
| 39 | +| `--rpc-url` | `https://api.calibration.node.glif.io/rpc/v1` | `BLOCKLOCK_RPC_URL` | Blockchain RPC URL. | |
| 40 | +| `--private-key` | `<Default Private Key>` | `BLOCKLOCK_PRIVATE_KEY` | Private key for transaction signing. | |
| 41 | +| `--bls-key` | `<Default BLS Key>` | `BLOCKLOCK_BLS_PRIVATE_KEY` | BLS private key for signing. | |
| 42 | +| `--decryptionSenderAddr` | `<Deployed Contract Address>` | `BLOCKLOCK_DECRYPTION_SENDER_CONTRACT_ADDRESS` | Address of the deployed `DecryptionSender` contract. | |
| 43 | +| `--state-path` | `./state.json` | `BLOCKLOCK_STATE_PATH` | A JSON file containing the `chainHeight` at which to start processing events. Empty will start from current block. | |
| 44 | +| `--polling-interval` | 1000 | `BLOCKLOCK_POLLING_INTERVAL` | How frequently in milliseconds to call the RPC for the latest events/state. | |
| 45 | +| `--log-level` | info | `BLOCKLOCK_LOG_LEVEL` | The logging level for structured JSON logging. Can be "info", "debug", "error", or "trace". | |
| 46 | + |
| 47 | +### Usage |
| 48 | + |
| 49 | +To start the `blocklock-agent`, run the following command: |
| 50 | + |
| 51 | +```bash |
| 52 | +npm run start [options] |
| 53 | +``` |
| 54 | + |
| 55 | +For example, to specify a custom RPC URL and state file, run: |
| 56 | + |
| 57 | +```bash |
| 58 | +npm run start --rpc-url http://localhost:8545 --state-file /home/cooluser/state.json |
| 59 | +``` |
| 60 | + |
| 61 | +The agent will store its state periodically to the provided state file. If a state file doesn't exist, it will start from the current chain height. |
| 62 | +If you wish to bootstrap it with a custom state file, an example format is the following: |
| 63 | +```{ "chainHeight": 123456 }``` |
| 64 | +
|
| 65 | +### How It Works |
| 66 | +
|
| 67 | +1. **Initialization**: |
| 68 | + - Connects to the specified RPC URL. |
| 69 | + - Configures the agent with the provided private and BLS keys. |
| 70 | + - Loads the state file to determine which chain height to start from, |
| 71 | + - Retrieves past `DecryptionRequested` events from the `chainHeight` specified in the state file |
| 72 | + - Listens for future `DecryptionRequested` events |
| 73 | +
|
| 74 | +2. **Event Processing**: |
| 75 | + - For each `DecryptionRequested` event: |
| 76 | + - Verifies the scheme ID (`BN254-BLS-BLOCKLOCK`). |
| 77 | + - Computes the BLS signature for the request condition. |
| 78 | + - Processes the ciphertext and fulfills the request via the `DecryptionSender` contract. |
| 79 | +
|
| 80 | +3. **Health Check**: |
| 81 | + - Hosts a lightweight HTTP server on the configured port to indicate the agent's health. |
| 82 | + |
| 83 | +4. **Keep Alive**: |
| 84 | + - The agent remains active, continuously listening for new events and processing them in real-time. |
| 85 | + |
| 86 | +### Development |
| 87 | + |
| 88 | +#### Scripts |
| 89 | + |
| 90 | +- Build the project: |
| 91 | + ```bash |
| 92 | + npm run build |
| 93 | + ``` |
| 94 | + |
| 95 | +### Directory Structure |
| 96 | + |
| 97 | +- **`crypto/`**: Contains cryptographic implementations (BLS and IBE). |
| 98 | +- **`generated/`**: Auto-generated smart contract TypeScript bindings / factories and type definitions. |
| 99 | +- **`provider.ts`**: Utilities for blockchain provider creation and retry logic. |
| 100 | +- **`index.ts`**: Entry point of the application. |
| 101 | + |
| 102 | +### Testing |
| 103 | + |
| 104 | +To test the agent, you can simulate `DecryptionRequested` events on a local blockchain, e.g., [Anvil](https://book.getfoundry.sh/reference/anvil/). |
| 105 | +Note that the required smart contracts will need to be deployed on the local chain, i.e., DecryptionSender, BlocklockSender and the user contract from where the on-chain timelock encryption / decryption requests will be made. |
| 106 | +See the [blocklock-solidity repository](github.com/randa-mu/blocklock-solidity.git) for more. |
| 107 | + |
| 108 | +### Troubleshooting |
| 109 | + |
| 110 | +- **Error: `missing revert data`**: |
| 111 | + Ensure the contract address, private key, and RPC URL are correctly configured. Ensure that the Ciphertext is correctly generated. There's an example [here](github.com/randa-mu/blocklock-solidity/blob/main/scripts/chain-interaction.ts) |
| 112 | + |
| 113 | +- **Unhandled Exception**: |
| 114 | + Check the blockchain node for connectivity issues or misconfigured start block. |
| 115 | +
|
| 116 | +### Contributing |
| 117 | +
|
| 118 | +Contributions are welcome! Please submit issues or pull requests to improve the project. |
| 119 | +
|
| 120 | +### License |
| 121 | +
|
| 122 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
0 commit comments