Enoki Docs

Sponsored Transactions

Enoki provides sponsored transactions, which allow you to send transactions to zkLogin accounts without the need to on-ramp users to Sui.

Using Enoki you can:

  • Integrate sponsored transactions with less effort.
  • Safely sponsor transactions without introducing excessive costs or opportunities for misuse.
  • Sponsor transactions in development environments before introducing them into production.

Implementation

Sponsoring transactions requires a private API key, so sponsored transactions go through a backend service that calls the Enoki API. This keeps your private API key off the client.

The transaction is built with Enoki's sponsor address as the gas owner and signed by the sender. Your backend sends it to Enoki, which checks it against your app's sponsorship rules, signs as the sponsor and executes it. Enoki pays gas from the sponsor's address balance, so the sender doesn't need any SUI.

On your backend, add the enoki() extension to a Sui client with your private API key:

import { enoki } from '@mysten/enoki';
import { SuiGrpcClient } from '@mysten/sui/grpc';

const client = new SuiGrpcClient({
	network: 'mainnet',
	baseUrl: 'https://fullnode.mainnet.sui.io:443',
}).$extend(enoki({ apiKey: process.env.ENOKI_PRIVATE_KEY! }));

Users sign in your app

The user's wallet signs in the browser, and your backend sponsors.

  1. In the browser, prepare the transaction with an EnokiClient that uses your public API key. This sets Enoki's sponsor as the gas owner. Then have the wallet sign it and send the bytes and signature to your backend.

    import { EnokiClient } from '@mysten/enoki';
    import { Transaction } from '@mysten/sui/transactions';
    
    const enoki = new EnokiClient({ apiKey: ENOKI_PUBLIC_KEY });
    
    const tx = new Transaction();
    // ... add some commands...
    
    await enoki.prepareSponsoredTransaction({ transaction: tx, network: 'mainnet' });
    const { bytes, signature } = await dAppKit.signTransaction({ transaction: tx });
    
    await fetch('/api/sponsor', { method: 'POST', body: JSON.stringify({ bytes, signature }) });
  2. On your backend, check the request as your app requires, for example that the user is signed in, then have Enoki sponsor and execute it.

    const { bytes, signature } = await request.json();
    
    const result = await client.enoki.sponsorAndExecuteTransaction({
    	transaction: bytes,
    	signature,
    });

Enoki already runs the sponsor SDK's default checks, a gas budget limit and your allowlists. For checks of your own on the transaction, run sponsor SDK validators before Enoki sponsors it. Create the sponsor for each request, which is cheap; that also lets a request add to the app's allowlists, such as allowing the signed-in user's address:

import { createSponsor } from '@mysten-incubation/sponsor';

const sponsor = createSponsor({
	signer: await client.enoki.getSponsorSigner({ allowedAddresses: [userAddress] }),
	client,
	validate: [myValidator],
});

const result = await sponsor.signAndExecuteTransaction({
	transaction: bytes,
	userSignature: signature,
});

Request allowlists need the matching options on the API key. On this path, transactions Enoki declines come back as a Rejected result instead of a thrown error.

Your backend is the sender

When your backend sends transactions from its own account, for example an admin or bot key, one call builds, signs, sponsors and waits for the transaction:

const result = await client.enoki.sponsorAndExecuteTransaction({
	transaction: tx,
	signer: adminKeypair,
});

Notes

  • To spend the sender's own SUI, use tx.coin({ balance, useGasCoin: false }). The gas coin belongs to the sponsor, so transactions that use it are rejected.
  • Without the SDK, get the sponsor address from GET /v1/app (sponsorAddresses.mainnet or sponsorAddresses.testnet), call tx.setGasOwner(sponsorAddress) and tx.setGasPayment([]) before building, and POST the network, base64 transactionBytes and signature to /v1/transaction-blocks/sponsor/execute. The response includes the transaction digest.

Enoki only sponsors transactions that:

  • Call Move targets and use addresses allowed in the Enoki Developer Portal, or in the allowedMoveCallTargets and allowedAddresses body fields if the API key allows them (see Allow lists).
  • Use the current reference gas price and a gas budget of at most 1 SUI.
  • Expire no later than the next epoch. tx.build({ client }) sets this for you.
  • Are signed by the sender in the transaction bytes.

Allowlists in the request body are added to the ones configured in the portal, so don't let your users choose them.

Rejected transactions return 400 invalid_transaction. Two errors mean a new transaction should be built, signed and retried. A Transaction that was already built keeps its gas owner, gas price and expiration, so create a new one instead of reusing it. When users sign in your app, send them back to the browser to prepare and sign again. The sponsor address is cached for up to a minute, so right after Enoki rotates it a retry can fail until the cache refreshes. In the SDK, isStaleTransactionError(error) is true for both:

  • invalid_gas_owner: the gas owner is not the current sponsor address. The error's data.sponsorAddress has the current address.
  • invalid_gas_price: the gas price is not the current reference gas price, usually because the epoch changed. The error's data.referenceGasPrice has the current price.

Each transaction is sponsored once. Sending the same bytes again returns 409 already_sponsored and doesn't execute them again. If a response is lost, look up the digest on chain. If it isn't there, transaction-blocks/sponsor/:digest executes the transaction if it's still pending. If that returns 404 or 400 expired, wait until the original's expiration epoch has passed, look up the digest on chain again, and send a new transaction only if it still isn't there.

Let Enoki build the transaction

Alternatively, the client sends only the transaction kind, and Enoki builds the full transaction.

  1. Construct transactionBlockKindBytes with txb.build({ client, onlyTransactionKind: true }).

    import { useCurrentClient } from '@mysten/dapp-kit-react';
    import { Transaction } from '@mysten/sui/transactions';
    
    const client = useCurrentClient();
    const txb = new Transaction();
    
    // ... add some transactions...
    
    const transactionBlockKindBytes = await txb.build({ client, onlyTransactionKind: true });
  2. Pass the transactionBlockKindBytes value to your backend service. The backend calls transaction-blocks/sponsor with method POST and provides:

    • zklogin-jwt value as part of the header.
    • network and transactionBlockKindBytes values in the body.

    If successful, the response includes the bytes for the transaction and the transaction digest.

    The body can also include allowedAddresses and allowedMoveCallTargets, which add to the allowed addresses and Move call targets configured for the app in the Enoki Portal. See Allow lists.

  3. Sign the returned transaction bytes with your key pair.

  4. From your backend service, call transaction-blocks/sponsor/:digest with method POST, and provide the signature you create in the previous step in the body of the call. If successful, the response includes a sponsor-signed base64-encoded transaction.

Allow lists

Enoki only sponsors transactions that call allowed Move call targets and send objects to allowed addresses. Configure the app's allow lists on the Sponsored Transactions page of the Enoki Portal.

Sponsor requests can add to these lists with the allowedMoveCallTargets and allowedAddresses fields. Two options under Sponsored Transactions in each private API key's settings control this:

  • Request allowed addresses: requests made with the key can include allowedAddresses.
  • Request allowed Move call targets: requests made with the key can include allowedMoveCallTargets.

Both options are on by default, including for existing keys. With an option off, the app's list is the limit for that key, and a request that includes a non-empty list of that kind fails with 400 invalid_request.

With these options on, anyone with the private API key can get any Move call or address sponsored by including it in the request. Turn them off if your backend doesn't need to set allow lists per request.