Skip to content

Commit 9ee4cf0

Browse files
CluEleSsUKnajienka
andauthored
refactored chain configuration, added new chains, refactored gas parameters (#11)
* added support for new networks * refactored a bunch of configuration * refactored gas params * updated pinned public keys --------- Co-authored-by: Nemitari Ajienka <nemitariajienka@gmail.com>
1 parent a55ed80 commit 9ee4cf0

17 files changed

Lines changed: 632 additions & 614 deletions

.env.example

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,27 @@ FILECOIN_MAINNET_RPC_URL=wss://wss.node.glif.io/apigw/lotus/rpc/v1
1111
# dcipher furnace
1212
FURNACE_PRIVATE_KEY=somehexstringwithoutthe0xprefix
1313
FURNACE_RPC_URL=https://api.furnace.dcipher.network
14+
1415
# base sepolia
1516
BASE_PRIVATE_KEY=somehexstringwithoutthe0xprefix
1617
BASE_RPC_URL=https://sepolia.base.org
18+
1719
# polygon PoS
1820
POLYGON_PRIVATE_KEY=somehexstringwithoutthe0xprefix
1921
POLYGON_RPC_URL=wss://cantuseoursbecauseitsprivate.quiknode.pro
22+
23+
# avalanche c-chain
24+
AVALANCHE_C_CHAIN_PRIVATE_KEY=somehexstringwithoutthe0xprefix
25+
AVALANCHE_C_CHAIN_RPC_URL=https://avalanche-c-chain-rpc.publicnode.com
26+
27+
# optimism sepolia
28+
OPTIMISM_SEPOLIA_PRIVATE_KEY=somehexstringwithoutthe0xprefix
29+
OPTIMISM_SEPOLIA_RPC_URL=https://sepolia.optimism.io
30+
31+
# arbitrum sepolia
32+
ARBITRUM_SEPOLIA_PRIVATE_KEY=somehexstringwithoutthe0xprefix
33+
ARBITRUM_SEPOLIA_RPC_URL=https://api.zan.top/arb-sepolia
34+
35+
# Sei testnet
36+
SEI_TESTNET_PRIVATE_KEY=somehexstringwithoutthe0xprefix
37+
SEI_TESTNET_RPC_URL=wss://sei-testnet.drpc.org

.github/workflows/build.yml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ on:
66

77
env:
88
BLS_KEY: ${{ secrets.BLS_KEY }}
9-
FILECOIN_PRIVATE_KEY: ${{ secrets.FILECOIN_PRIVATE_KEY }}
10-
FILECOIN_RPC_URL: ${{ secrets.FILECOIN_RPC_URL }}
9+
ARBITRUM_SEPOLIA_PRIVATE_KEY: ${{ secrets.ARBITRUM_SEPOLIA_PRIVATE_KEY }}
10+
ARBITRUM_SEPOLIA_RPC_URL: ${{ secrets.ARBITRUM_SEPOLIA_RPC_URL }}
11+
AVALANCHE_C_CHAIN_PRIVATE_KEY: ${{ secrets.AVALANCHE_C_CHAIN_PRIVATE_KEY }}
12+
AVALANCHE_C_CHAIN_RPC_URL: ${{ secrets.AVALANCHE_C_CHAIN_RPC_URL }}
13+
BASE_PRIVATE_KEY: ${{ secrets.BASE_PRIVATE_KEY }}
14+
BASE_RPC_URL: ${{ secrets.BASE_RPC_URL }}
1115
FILECOIN_MAINNET_PRIVATE_KEY: ${{ secrets.FILECOIN_MAINNET_PRIVATE_KEY }}
1216
FILECOIN_MAINNET_RPC_URL: ${{ secrets.FILECOIN_MAINNET_RPC_URL }}
17+
FILECOIN_PRIVATE_KEY: ${{ secrets.FILECOIN_PRIVATE_KEY }}
18+
FILECOIN_RPC_URL: ${{ secrets.FILECOIN_RPC_URL }}
1319
FURNACE_PRIVATE_KEY: ${{ secrets.FURNACE_PRIVATE_KEY }}
1420
FURNACE_RPC_URL: ${{ secrets.FURNACE_RPC_URL }}
15-
BASE_PRIVATE_KEY: ${{ secrets.BASE_PRIVATE_KEY }}
16-
BASE_RPC_URL: ${{ secrets.BASE_RPC_URL }}
1721
POLYGON_PRIVATE_KEY: ${{ secrets.POLYGON_PRIVATE_KEY }}
1822
POLYGON_RPC_URL: ${{ secrets.POLYGON_RPC_URL }}
23+
OPTIMISM_SEPOLIA_PRIVATE_KEY: ${{ secrets.OPTIMISM_SEPOLIA_PRIVATE_KEY }}
24+
OPTIMISM_SEPOLIA_RPC_URL: ${{ secrets.OPTIMISM_SEPOLIA_RPC_URL }}
25+
SEI_TESTNET_PRIVATE_KEY: ${{ secrets.SEI_TESTNET_PRIVATE_KEY }}
26+
SEI_TESTNET_RPC_URL: ${{ secrets.SEI_TESTNET_RPC_URL }}
1927

2028
jobs:
2129
build:

README.md

Lines changed: 99 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
## blocklock-js
22

3-
blocklock-js is a TypeScript library designed to simplify the process of generating encrypted data off-chain for the dcrypt network. It enables developers to securely encrypt data tied to a user-specified future chain height. The encrypted data can then be used to create on-chain timelock encryption requests in smart contracts. Once the specified chain height is mined, the user’s smart contract will receive the decryption keys automatically.
4-
5-
6-
### Key Capabilities
7-
8-
Using this library, developers can:
9-
10-
* Encode and encrypt various Solidity-compatible data types.
11-
* Encrypt the encoded data off-chain using a public key, which can then be integrated into smart contracts for timelock encryption requests.
3+
`blocklock-js` is a TypeScript library that simplifies generating encrypted data off-chain for use with the dcrypt network. It allows developers to securely encrypt data tied to a user-defined condition, e.g., a future block height. This encrypted payload can then be referenced in on-chain timelock encryption requests via smart contracts. Once the specified block is mined, the decryption key is automatically delivered to the smart contract via a callback, enabling conditional data access on-chain.
124

5+
The library also enables developers to track the status of a conditional encryption request.
136

147
### On-Chain Integration
158

16-
Solidity interfaces and associated documentation for them can be found in the [blocklock-solidity](https://github.com/randa-mu/blocklock-solidity.git) repository.
9+
Solidity interfaces and associated documentation can be found in the [blocklock-solidity](https://github.com/randa-mu/blocklock-solidity.git) repository.
1710

1811
#### Smart Contract Addresses
1912

@@ -27,8 +20,13 @@ A lightweight proxy contract that enables upgradeability for the `BlocklockSende
2720
| Filecoin Calibration Testnet | [0xF00aB3B64c81b6Ce51f8220EB2bFaa2D469cf702](https://calibration.filfox.info/en/address/0xF00aB3B64c81b6Ce51f8220EB2bFaa2D469cf702) |
2821
| Base Sepolia | [0x82Fed730CbdeC5A2D8724F2e3b316a70A565e27e](https://sepolia.basescan.org/address/0x82Fed730CbdeC5A2D8724F2e3b316a70A565e27e) |
2922
| Polygon PoS | [0x82Fed730CbdeC5A2D8724F2e3b316a70A565e27e](https://polygonscan.com/address/0x82Fed730CbdeC5A2D8724F2e3b316a70A565e27e) |
23+
| Optimism Sepolia | [0xd22302849a87d5B00f13e504581BC086300DA080](https://sepolia-optimism.etherscan.io/address/0xd22302849a87d5B00f13e504581BC086300DA080) |
24+
| Arbitrum Sepolia | [0xd22302849a87d5B00f13e504581BC086300DA080](https://sepolia.arbiscan.io/address/0xd22302849a87d5B00f13e504581BC086300DA080) |
25+
| Avalanche (C-Chain) Testnet | [0xd22302849a87d5B00f13e504581BC086300DA080](https://testnet.snowtrace.io/address/0xd22302849a87d5B00f13e504581BC086300DA080) |
26+
| Sei Testnet | [0xd22302849a87d5B00f13e504581BC086300DA080](https://seitrace.com/address/0xd22302849a87d5B00f13e504581BC086300DA080?chain=atlantic-2) |
27+
3028

31-
**Others**
29+
**Other contract addresses**
3230
You should only need the `BlocklockSender` proxy above, but a full list of contract addresses can be found in the [solidity repo's README](https://github.com/randa-mu/blocklock-solidity).
3331

3432

@@ -41,53 +39,68 @@ npm install blocklock-js
4139
```
4240

4341

44-
4542
### Usage Example
4643

4744
#### Prerequisites
4845

4946
* [ethers](https://www.npmjs.com/package/ethers) for wallet setup and message encoding.
47+
* Node.js v22+
48+
49+
#### Setup
5050

51+
1. Create a `.env` file or set the following environment variables, e.g., for Filecoin mainnet:
5152

52-
Here’s how to use BlocklockJS to encrypt data and create an on-chain timelock encryption request.
53+
```bash
54+
RPC_URL=https://your-rpc-url
55+
PRIVATE_KEY=your_private_key
56+
```
5357

54-
#### Example: Encrypting a uint256 (4 ETH) for Decryption 2 Blocks Later
58+
2. Install dependencies:
59+
60+
```bash
61+
npm install
62+
```
63+
64+
65+
#### Usage example
66+
67+
This example demonstrates encrypting a `uint256` value and using the Ciphertext and condition bytes in a user smart contract that implements the `createTimelockRequestWithDirectFunding` function to create a timelock encryption request on-chain. An example use case is a sealed-bid auction where bid amounts are encrypted and only decrypted at the auction ending block number.
5568

56-
This example demonstrates encrypting a uint256 value and sending it to a user smart contract that implements the createTimelockRequest function. In a different use case, e.g., sealed bid auction, this could be refactored into a `sealedBid` function.
5769
The example user smart contract source code can be found [here](https://github.com/randa-mu/blocklock-solidity/blob/main/src/mocks/MockBlocklockReceiver.sol).
5870

59-
```js
60-
import { ethers, getBytes } from "ethers";
61-
import { Blocklock, SolidityEncoder, encodeCiphertextToSolidity, encodeCondition } from "blocklock-js";
62-
import { MockBlocklockReceiver__factory } from "../types"; // Users' solidity contract TypeScript binding
71+
This script shows how to encrypt data with a future block as a condition
6372

64-
async function main() {
65-
// User wallet
66-
const wallet = new ethers.Wallet("your-private-key", ethers.provider);
67-
// User contract
68-
const mockBlocklockReceiver = MockBlocklockReceiver__factory.connect("user blocklcok receiver contract address", wallet);
6973

70-
// Ensure plainTextValue is initially 0
71-
console.log("Initial plainTextValue:", (await mockBlocklockReceiver.plainTextValue()).toString());
74+
```ts
75+
import { createProvider, Blocklock, encodeCiphertextToSolidity, encodeCondition } from "blocklock-js"
76+
import { Wallet, Provider, NonceManager, ethers, getBytes } from "ethers"
77+
import { MockBlocklockReceiver__factory } from "../types"; // Users' solidity contract TypeScript binding
7278

73-
// Set block height (current block + 2)
74-
const blockHeight = BigInt(await ethers.provider.getBlockNumber() + 2);
79+
async function main() {
80+
const rpc = createProvider(process.env.RPC_URL || "")
81+
const wallet = new NonceManager(new Wallet(process.env.PRIVATE_KEY || "", rpc))
82+
// Create a Blocklock instance for the Filecoin mainnet
83+
const blocklock = Blocklock.createFilecoinMainnet(wallet)
7584

7685
// Value to encrypt (4 ETH as uint256)
7786
const msg = ethers.utils.parseEther("4");
87+
const plaintext = Buffer.from(msg)
88+
const currentBlock = await rpc.getBlockNumber()
89+
const targetBlock = BigInt(currentBlock + 5)
7890

79-
// Encode the uint256 value
80-
const encoder = new SolidityEncoder();
81-
const msgBytes = encoder.encodeUint256(msg);
82-
const encodedMessage = getBytes(msgBytes);
91+
console.log(`Encrypting for block ${targetBlock} (current: ${currentBlock})`)
92+
const ciphertext = await blocklock.encrypt(plaintext, targetBlock)
8393

84-
// Encrypt the encoded message
85-
const blocklockjs = new Blocklock(wallet, "blocklockSender contract address");
86-
const ciphertext = blocklockjs.encrypt(encodedMessage, blockHeight);
94+
// User contract
95+
const mockBlocklockReceiver = MockBlocklockReceiver__factory.connect("user blocklcok receiver contract address", wallet);
8796

8897
// Generate the timelock encryption condition bytes string
89-
const conditionBytes = encodeCondition(blockHeight);
90-
const callbackGasLimit = 400_00;
98+
const conditionBytes = encodeCondition(targetBlock);
99+
// Amount of gas users callback function is expected to consume, i.e., the function that will be called with the decryption key
100+
const callbackGasLimit = 500_000;
101+
102+
// Compute the request price to pay for the direct funding request
103+
const requestPrice = await blocklock.calculateRequestPriceNative(callbackGasLimit);
91104

92105
// Call `createTimelockRequestWithDirectFunding` on the user's contract
93106
// for a direct or ad hoc funding request with the following parameters:
@@ -96,7 +109,11 @@ async function main() {
96109
// TypesLib.Ciphertext calldata encryptedData
97110
const tx = await mockBlocklockReceiver
98111
.connect(wallet)
99-
.createTimelockRequestWithDirectFunding(callbackGasLimit, conditionBytes, encodeCiphertextToSolidity(ciphertext));
112+
.createTimelockRequestWithDirectFunding(callbackGasLimit, conditionBytes, encodeCiphertextToSolidity(ciphertext),
113+
{value: requestPrice});
114+
// Note: for subscription-based funding,
115+
// use createTimelockRequestWithSubscription instead.
116+
100117
const receipt = await tx.wait(1);
101118

102119
if (!receipt) {
@@ -114,30 +131,58 @@ main().catch((error) => {
114131
#### How It Works
115132
1. Encoding and Encryption:
116133

117-
* Use the SolidityEncoder to encode Solidity-compatible data types.
118-
* Encrypt the encoded message and specify the decryption chain height.
119-
* Generate the condition bytes string
134+
* Use blocklock-js to:
135+
136+
* Create a provider and signer using Wallet and NonceManager.
137+
138+
* Initialize a Blocklock instance for your target network (e.g., Filecoin mainnet).
139+
140+
* Prepare the message to encrypt (e.g., 4 ETH encoded as a uint256).
141+
142+
* Select a future block height as the decryption condition.
143+
144+
```ts
145+
const msg = ethers.utils.parseEther("4");
146+
const plaintext = Buffer.from(msg);
147+
const currentBlock = await rpc.getBlockNumber();
148+
const targetBlock = BigInt(currentBlock + 5);
149+
150+
const ciphertext = await blocklock.encrypt(plaintext, targetBlock);
151+
const conditionBytes = encodeCondition(targetBlock);
152+
```
153+
154+
2. On-Chain Interaction:
155+
156+
* Use your own smart contract (e.g., `MockBlocklockReceiver`) to create a timelock encryption request.
157+
* Call `createTimelockRequestWithDirectFunding`, which:
158+
159+
* Stores the encrypted data (Ciphertext) and decryption condition on-chain via the `BlocklockSender` contract.
120160

121-
2. On-Chain Interaction:
161+
* Funds the request by paying the `requestPrice` via the transaction.
122162

123-
* Call the appropriate function in the user contract with the encrypted data and the chain height used during off-chain encryption. In this example, the `createTimelockRequestWithDirectFunding` function is called, which calls the [BlocklockSender](https://github.com/randa-mu/blocklock-solidity/blob/main/src/blocklock/BlocklockSender.sol) contract to create an on-chain timelock request with the encrypted data and condition (represented as bytes to support different condition types) for decryption, using the direct funding method. The `BlocklockSender` contract then stores the encrypted data, and generates a unique request ID. The `BlocklockSender` contract also supports a subscription funding method. To make a request via that is paid for via a funded subscription account, the `createTimelockRequestWithSubscription` function in the [example](https://github.com/randa-mu/blocklock-solidity/blob/main/src/mocks/MockBlocklockReceiver.sol) smart contract code can be called.
163+
* Generates a unique request ID.Call the appropriate function in the user contract with the encrypted data and the chain height used during off-chain encryption.
124164

125-
3. Decryption:
165+
```ts
166+
const callbackGasLimit = 500_000;
167+
168+
const requestPrice = await blocklock.calculateRequestPriceNative(callbackGasLimit);
126169

127-
* After the specified chain height, the on-chain timelock contract triggers a callback to the user's contract, providing the decryption key. The user's contract can then call the `decrypt` function in the `BlocklockSender` contract to perform on-chain decryption using the provided decryption key.
170+
await mockBlocklockReceiver.createTimelockRequestWithDirectFunding(
171+
callbackGasLimit,
172+
conditionBytes,
173+
encodeCiphertextToSolidity(ciphertext),
174+
{value: requestPrice}
175+
);
176+
```
128177

178+
3. Automatic Decryption and Callback:
179+
* Once the specified condition is met (e.g., target block is mined):
129180

130-
#### Supported Data Types
131-
The library supports encoding and encryption of the following Solidity-compatible data types:
181+
* The `BlocklockSender` contract receives the decryption key from the dcipher Threshold Network.
132182

133-
* uint256
134-
* int256
135-
* address
136-
* string
137-
* bytes
138-
* bytes32
183+
* It automatically triggers a callback to the users contract with the key.
139184

140-
Use the `SolidityEncoder` to encode any of these types before encryption.
185+
* The users contract can optionally call decrypt to recover the original message on-chain.
141186

142187

143188
### Common Errors

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "blocklock-js",
3-
"version": "0.0.8-rc4",
3+
"version": "0.0.8-rc5",
44
"description": "A library for encrypting and decrypting data for the future",
55
"source": "src/index.ts",
66
"main": "./dist/cjs/index.cjs",
@@ -32,7 +32,7 @@
3232
"clean": "rm -rf dist",
3333
"lint": "eslint src",
3434
"lint:fix": "eslint --fix",
35-
"test": "jest ./test/*.test.ts"
35+
"test": "jest ./test/*.test.ts --forceExit"
3636
},
3737
"keywords": [
3838
"conditional",

0 commit comments

Comments
 (0)