Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# filecoin calibnet
FILECOIN_PRIVATE_KEY=somehexstringwithoutthe0xprefix
FILECOIN_RPC_URL=wss://wss.calibration.node.glif.io/apigw/lotus/rpc/v1
# dcipher furnace
FURNACE_PRIVATE_KEY=somehexstringwithoutthe0xprefix
FURNACE_RPC_URL=https://furnace.dcipher.network
# base sepolia
BASE_PRIVATE_KEY=somehexstringwithoutthe0xprefix
BASE_RPC_URL=https://sepolia.base.org
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ env:
FILECOIN_RPC_URL: ${{ secrets.FILECOIN_RPC_URL }}
FURNACE_PRIVATE_KEY: ${{ secrets.FURNACE_PRIVATE_KEY }}
FURNACE_RPC_URL: ${{ secrets.FURNACE_RPC_URL }}
BASE_PRIVATE_KEY: ${{ secrets.BASE_PRIVATE_KEY }}
BASE_RPC_URL: ${{ secrets.BASE_RPC_URL }}

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "randomness-js",
"version": "0.0.1-rc8",
"version": "0.0.1-rc9",
"description": "A library for consuming, verifying and using randomness from the dcipher network",
"source": "src/index.ts",
"main": "./dist/cjs/index.cjs",
Expand Down
22 changes: 17 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {RandomnessCallbackSuccessEvent, RandomnessSender} from "./generated/Rand
/* addresses of the deployed contracts */
export const FURNACE_TESTNET_CONTRACT_ADDRESS = "0x8192aF4ce49f473fCa7e3e5a8d819B0763Def048"
export const FILECOIN_CALIBNET_CONTRACT_ADDRESS = "0x9c789bc7F2B5c6619Be1572A39F2C3d6f33001dC"
export const BASE_SEPOLIA_CONTRACT_ADDRESS = "0x31e01BCA94b787D3B4a16C378Bd5D200686dEb99"

/* some cryptographic parameters that are also defined in the contracts, but we duplicate here for performance */
const RANDOMNESS_DST = "randomness:0.0.1:bn254"
Expand Down Expand Up @@ -51,16 +52,27 @@ export class Randomness {
return new Randomness(rpc, FURNACE_TESTNET_CONTRACT_ADDRESS)
}

static createBaseSepolia(rpc: Signer | Provider): Randomness {
return new Randomness(rpc, BASE_SEPOLIA_CONTRACT_ADDRESS)
}

static createFromChainId(rpc: Signer | Provider, chainId: BigNumberish): Randomness {
switch (chainId) {
switch (chainId.toString().toLowerCase()) {
case "314159":
case 314159n:
case 314159:
case "314159n":
case "0x4cb2f":
return Randomness.createFilecoinCalibnet(rpc)

case "64630":
case 64630n:
case 64630:
case "64630n":
case "0xfc76":
return Randomness.createFurnace(rpc)

case "84532":
case "84532n":
case "0x14a34":
return Randomness.createBaseSepolia(rpc)

default:
throw new Error("unsupported chainId :(")
}
Expand Down
25 changes: 22 additions & 3 deletions test/randomness.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import * as dotenv from "dotenv"
import {describe, it, expect, beforeAll} from "@jest/globals"
import {JsonRpcProvider, NonceManager, Wallet, WebSocketProvider} from "ethers"
import {FILECOIN_CALIBNET_CONTRACT_ADDRESS, FURNACE_TESTNET_CONTRACT_ADDRESS, Randomness} from "../src"
import {
BASE_SEPOLIA_CONTRACT_ADDRESS,
FILECOIN_CALIBNET_CONTRACT_ADDRESS,
FURNACE_TESTNET_CONTRACT_ADDRESS,
Randomness
} from "../src"

// filecoin calibnet might take forever
const TEST_TIMEOUT = 200_000
describe("randomness", () => {
beforeAll(() => {
dotenv.config()
})

it("can be requested from filecoin testnet and verified", async () => {
it("can be requested from filecoin testnet and verified", async () => {
const rpc = createProvider(process.env.FILECOIN_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.FILECOIN_PRIVATE_KEY || "", rpc))

Expand All @@ -37,6 +42,20 @@ describe("randomness", () => {
rpc.destroy()
}, TEST_TIMEOUT)

it("can be requested from a base sepolia and verified", async () => {
const rpc = createProvider(process.env.BASE_RPC_URL || "")
const wallet = new NonceManager(new Wallet(process.env.BASE_PRIVATE_KEY || "", rpc))

const randomness = new Randomness(wallet, BASE_SEPOLIA_CONTRACT_ADDRESS)
expect(randomness).not.toEqual(null)

const response = await randomness.requestRandomness(1, TEST_TIMEOUT)
console.log("randomness requested")
await randomness.verify(response)

rpc.destroy()
}, TEST_TIMEOUT)

})

function createProvider(url: string): JsonRpcProvider | WebSocketProvider {
Expand Down