▶️Initializing SDK

Core Options

The SDK can be initialized with the given CoreOptions.

import { Hex } from 'viem';

type CoreOptions = {
  chainId: number;
  account: ShieldedAccount;
  rpc: PublicClient | string;
  explorerApi: string;
  contracts?: CoreContracts;
  circuits?: SDKCircuits;
  services?: {
    eventFetcher: IEventFetcher;
    addressResolver: IAddressResolver;
    treeSource: ITreeSource;
    notesSource: INotesSource;
  }
}
  • chainId: The chain id of the instance of zkFi where transactions will be sent.

  • account: The Account of the user which will be used to sign and decrypt transactions.

  • rpc: RPC url orf the given chainId.

  • contracts: The various zkFi contract addresses on the given chain.

type CorContracts = {
  pool: Hex;
  paymaster: Hex;
}
  • circuits: Paths to ZK circuit artifacts.

  • snarkJs: The SDK requires snarkjs for the purpose for proving a transaction. Depending on where you are utilizing the SDK, you need to install snarkjs in your project before you start proving the transaction.

    • For node, install it as dependency: npm install snarkjs and include it in CoreOptions.

    • For the browser, simply include it as a script using <script> tag.

  • isTestnet : Boolean to specify if the chain is testnet.

  • services: The services needed for fetching external data needed for SDK to function. These services can be custom made e.g. for optimization, but default services are plugged in during SDK initialization.

Instantiation

You don't need to specify all the parameters in the options. Most parameters available are optional.

The only significant required parameter is the ShieldedAccount object corresponding to the shielded account of the user. For creating ShieldedAccount you need a Viewer , andISigner of the connected wallet.

import { ShieldedAccount } from '@zkfi-tech/account';

const account = new Account(signer, viewer);

Then SDK is simply initialized as:

const zkfi = new Core({
    chainId: <chain-id>,
    account,
    rpc: <rpc-url>
});

Usage

The SDK instance, zkfi above is now ready to be used for various functionalities like:

  • getBalance() Returns the balance of various assets that is in user's shielded account.

  • getHistory() Returns decoded transaction history of the user.

  • createTransaction(req: TransactionRequest, opts: TransactionOptions) Creates a Transaction given the request and options.

  • signTransaction(tx: Transaction) Signs the given transaction.

  • proveTransaction(tx: Transaction) Proves a given signed transaction by generating a ZK proof.

See the next section, for steps of crafting a transaction.

Last updated