UNPKG

blocklock-js

Version:

A library for encrypting and decrypting data for the future

99 lines (98 loc) 5.01 kB
import { Signer, Provider, BigNumberish } from "ethers"; import { Ciphertext, G2 } from "./crypto/ibe-bn254"; import { TypesLib } from "./generated/BlocklockSender"; import { NetworkConfig } from "./networks"; export declare class Blocklock { private networkConfig; private blocklockSender; private signer; constructor(signer: Signer | Provider, networkConfig: NetworkConfig); static createFromChainId(rpc: Signer | Provider, chainId: BigNumberish): Blocklock; /** * Request a blocklock decryption at block number blockHeight. * @param blockHeight time at which the decryption should key should be released * @param ciphertext encrypted message to store on chain * @param callbackGasLimit the maximum amount of gas the dcipher network should spend on the callback * @param gasMultiplier a multiplier to use on the gas price for the chain * @returns blocklock request id as a string */ requestBlocklock(blockHeight: bigint, ciphertext: TypesLib.CiphertextStruct, callbackGasLimit?: bigint, bufferPercent?: bigint): Promise<bigint>; /** * Calculates the request price for a blocklock request given the callbackGasLimit. * @param callbackGasLimit The callbackGasLimit to use when fulfilling the request with a decryption key. * @param bufferPercent Optional buffer percent to apply on top of the estimated request price. If not provided, the default from the network config will be used. * @returns The estimated request price and the transaction gas price used */ calculateRequestPriceNative(callbackGasLimit: bigint, bufferPercent?: bigint): Promise<[bigint, bigint]>; /** * Fetch the details of a blocklock request, decryption key / signature excluded. * This function should be called to fetch pending blocklock requests. * @param requestId blocklock request id * @returns details of the blocklock request, undefined if not found */ fetchBlocklockRequest(requestId: bigint): Promise<BlocklockRequest | undefined>; /** * Fetch all blocklock requests, decryption keys / signatures excluded. * @returns a map with the details of each blocklock request */ fetchAllBlocklockRequests(): Promise<Map<bigint, BlocklockRequest>>; /** * Fetch the status of a blocklock request, including the decryption key / signature if available. * This function should be called to fetch blocklock requests that have been fulfilled, or to check * whether it has been fulfilled or not. * @param requestId blocklock request id * @returns details of the blocklock request, undefined if not found */ fetchBlocklockStatus(requestId: bigint): Promise<BlocklockStatus>; /** * Encrypt a message that can be decrypted once a certain blockHeight is reached. * @param message plaintext to encrypt * @param blockHeight time at which the decryption key should be released * @param pk public key of the scheme * @returns encrypted message */ encrypt(message: Uint8Array, blockHeight: bigint, pk?: G2): Ciphertext; /** * Decrypt a ciphertext using a decryption key. * @param ciphertext the ciphertext to decrypt * @param key decryption key * @returns plaintext */ decrypt(ciphertext: Ciphertext, key: Uint8Array): Uint8Array; /** * Encrypt a message that can be decrypted once a certain blockHeight is reached. * @param message plaintext to encrypt * @param blockHeight time at which the decryption key should be released * @param pk public key of the scheme * @returns the identifier of the blocklock request, and the ciphertext */ encryptAndRegister(message: Uint8Array, blockHeight: bigint, pk?: G2): Promise<{ id: bigint; ciphertext: Ciphertext; }>; /** * Try to decrypt a ciphertext with a specific blocklock id. * @param requestId blocklock id of the ciphertext to decrypt * @returns the plaintext if the decryption key is available, undefined otherwise */ decryptWithId(requestId: bigint): Promise<Uint8Array>; static createFilecoinMainnet(rpc: Signer | Provider): Blocklock; static createFilecoinCalibnet(rpc: Signer | Provider): Blocklock; static createBaseSepolia(rpc: Signer | Provider): Blocklock; static createBaseMainnet(rpc: Signer | Provider): Blocklock; static createPolygonPos(rpc: Signer | Provider): Blocklock; static createAvalancheCChain(rpc: Signer | Provider): Blocklock; static createOptimismSepolia(rpc: Signer | Provider): Blocklock; static createArbitrumSepolia(rpc: Signer | Provider): Blocklock; static createArbitrumMainnet(rpc: Signer | Provider): Blocklock; static createSeiTestnet(rpc: Signer | Provider): Blocklock; } export type BlocklockRequest = { id: bigint; blockHeight: bigint; ciphertext: Ciphertext; }; export type BlocklockStatus = BlocklockRequest & { decryptionKey: Uint8Array; pending: boolean; };