📤Sending Transaction

The flow from crafting transactions to sending transactions is simple using the SDK. The process is divided into four steps:

  1. Creating a Transaction by providing a simple TransactionRequest.

  2. Signing the created Transaction.

  3. Proving the signed Transaction to get the shielded transaction (ZTransaction).

  4. Sending the proved transaction.

Creating Transaction

As mentioned before, creating transactions in zkFi can be done by creating TransactionRequest objects. Below is a transaction request to send 100 of WETH (asset id 65537) to vitalik.eth and pay gas fees in WETH:

import { Transaction } from '@zkfi-tech/transaction';
import { TransactionType, TransactionRequest } from '@zkfi-tech/shared-types';

const txReq: TransactionRequest = { 
    type: TransactionType.TRANSFER,
    assetIds: 65537,
    values: [100],
    feeAssetId: 65537,
    to: 'vitalik.eth',
    payload: '0x'

const tx: Transaction = await zkfi.createTransaction(txReq);

Signing Transaction

To authorize the created Transaction i.e tx above it needs to be signed by the owner. This is as simple as:

const signedTx: Transaction = await zkfi.signTransaction(tx);

Proving Transaction

Now, we need to prove this signed transaction to include ZK proof in it & hide all the sensitive fields. This too, is as simple as:

const ztx: ZTransaction = await zkfi.proveTransaction(signedTx);

Sending Transaction

By default, the transaction is crafted by the SDK such that DEPOSIT transactions are meant to be sent via your public wallet (which is a must for DEPOSIT since you are sending tokens to contract) and other types are meant to be sent via a bundler, which is recommended for privacy. However, you can override this behavior to send transactions other than DEPOSIT via the public wallet itself - but it will damage privacy and should be prevented. The shielded transaction can be used as input to the zkFi on-chain Pool contract by calling its transact() method:

function transact(ZTransaction memory ztx) external;

But before passing the TypeScript object ZTransaction to transact you have to convert it to input suitable for the contract. Doing so is as simple as calling ztx.toSolidityInput().

ZTransaction ztx = zkfi.proveTransaction(signedTx);
const poolContract = ...
const inp = ztx.toSolidityInput();
await poolContract.transact(inp);

Last updated