-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration.test.ts
More file actions
139 lines (120 loc) · 6.77 KB
/
integration.test.ts
File metadata and controls
139 lines (120 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {describe, it, expect, beforeAll} from "@jest/globals"
import {equalBytes} from "@noble/curves/abstract/utils"
import dotenv from "dotenv"
import {JsonRpcProvider, NonceManager, Provider, Wallet, WebSocketProvider} from "ethers"
import {Blocklock} from "../src"
const TIMEOUT = 60_000
const FILECOIN_TIMEOUT = 300_000
describe("Blocklock integration tests with supported networks", () => {
beforeAll(() => {
dotenv.config()
})
it("should return non-zero request price to cover BLS operations when callbackGasLimit and bufferPercent are both zero", async () => {
const rpc = createProvider(process.env.FILECOIN_MAINNET_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_MAINNET_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createFilecoinMainnet(wallet)
const callbackGasLimit = 0n;
const bufferPercent = 0n;
const [estimatedRequestPrice, ] = await blocklock.calculateRequestPriceNative(callbackGasLimit, bufferPercent);
expect(estimatedRequestPrice).toBeGreaterThan(0n);
}, FILECOIN_TIMEOUT)
it("should return non-zero request price to cover BLS operations when callbackGasLimit is zero", async () => {
const rpc = createProvider(process.env.FILECOIN_MAINNET_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_MAINNET_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createFilecoinMainnet(wallet)
const callbackGasLimit = 0n;
const bufferPercent = 100n;
const [estimatedRequestPrice, ] = await blocklock.calculateRequestPriceNative(callbackGasLimit, bufferPercent);
expect(estimatedRequestPrice).toBeGreaterThan(0n);
}, FILECOIN_TIMEOUT)
it("should return non-zero request price to cover BLS operations when callbackGasLimit is zero and bufferPercent is not specified", async () => {
const rpc = createProvider(process.env.FILECOIN_MAINNET_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_MAINNET_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createFilecoinMainnet(wallet)
const callbackGasLimit = 0n;
const [estimatedRequestPrice, ] = await blocklock.calculateRequestPriceNative(callbackGasLimit);
expect(estimatedRequestPrice).toBeGreaterThan(0n);
}, FILECOIN_TIMEOUT)
it("should encrypt and decrypt for polygon pos", async () => {
const rpc = createProvider(process.env.POLYGON_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.POLYGON_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createPolygonPos(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
it("should encrypt and decrypt for base sepolia", async () => {
const rpc = createProvider(process.env.BASE_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.BASE_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createBaseSepolia(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
it.skip("should encrypt and decrypt for base mainnet", async () => {
const rpc = createProvider(process.env.BASE_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.BASE_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createBaseMainnet(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
it.skip("should encrypt and decrypt for filecoin mainnet", async () => {
const rpc = createProvider(process.env.FILECOIN_MAINNET_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_MAINNET_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createFilecoinMainnet(wallet)
await runEncryptionTest(rpc, blocklock)
}, FILECOIN_TIMEOUT)
// filecoin calibnet is very slow
// the test can take up to 260s
it.skip("should encrypt and decrypt for filecoin calibnet", async () => {
const rpc = createProvider(process.env.FILECOIN_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createFilecoinCalibnet(wallet)
await runEncryptionTest(rpc, blocklock)
}, FILECOIN_TIMEOUT)
// skipped because costs money
it.skip("should encrypt and decrypt for avalanche c chain", async () => {
const rpc = createProvider(process.env.AVALANCHE_C_CHAIN_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.AVALANCHE_C_CHAIN_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createAvalancheCChain(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
it("should encrypt and decrypt for optimism sepolia", async () => {
const rpc = createProvider(process.env.OPTIMISM_SEPOLIA_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.OPTIMISM_SEPOLIA_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createOptimismSepolia(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
it("should encrypt and decrypt for arbitrum sepolia", async () => {
const rpc = createProvider(process.env.ARBITRUM_SEPOLIA_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.ARBITRUM_SEPOLIA_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createArbitrumSepolia(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
// skipping temporarily because there's some weirdness with their RPC
it.skip("should encrypt and decrypt for sei testnet", async () => {
const rpc = createProvider(process.env.SEI_TESTNET_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.SEI_TESTNET_PRIVATE_KEY || "", rpc))
const blocklock = Blocklock.createSeiTestnet(wallet)
await runEncryptionTest(rpc, blocklock)
}, TIMEOUT)
})
async function runEncryptionTest(rpc: Provider, blocklock: Blocklock) {
const plaintext = Buffer.from("hello world!")
const currentBlock = await rpc.getBlockNumber()
const targetBlock = BigInt(currentBlock + 5)
const {id} = await blocklock.encryptAndRegister(plaintext, targetBlock)
let result = new Uint8Array(0)
while (result.length === 0) {
await sleep(1000)
result = await blocklock.decryptWithId(id)
}
expect(equalBytes(result, plaintext)).toBeTruthy()
}
function createProvider(url: string): JsonRpcProvider | WebSocketProvider {
if (url.startsWith("http")) {
return new JsonRpcProvider(url, undefined, {pollingInterval: 1000})
}
if (url.startsWith("ws")) {
return new WebSocketProvider(url)
}
throw new Error(`provider cannot be created for the protocol in ${url}`)
}
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms))
}