@mysten/enoki
v2.0.1
Published
zkLogin authentication and sponsored transactions
Readme
@mysten/enoki
zkLogin authentication and sponsored transactions. Enable users to sign in with Google, Twitch, Facebook, and other OAuth providers, and execute transactions without gas fees.
Features
- zkLogin: Authenticate users with OAuth providers (Google, Twitch, Facebook)
- Sponsored Transactions: Execute transactions without requiring users to hold gas
- Wallet Integration: Register Enoki wallets with dApp Kit
- Subname Management: Create and manage SuiNS subnames
Installation
npm install @mysten/enokiQuick Start
Register Enoki wallets
import { registerEnokiWallets } from '@mysten/enoki';
registerEnokiWallets({
apiKey: 'your-enoki-api-key',
providers: ['google', 'twitch', 'facebook'],
});Use with React and dApp Kit
import { registerEnokiWallets } from '@mysten/enoki';
import { useEnokiFlow } from '@mysten/enoki/react';
// In your app setup
registerEnokiWallets({
apiKey: 'your-enoki-api-key',
providers: ['google'],
});
// In your component
function LoginButton() {
const { login } = useEnokiFlow();
return <button onClick={() => login('google')}>Sign in with Google</button>;
}Sponsor transactions
Add the enoki() extension to a Sui client on your backend, with a private API key that has
sponsored transactions enabled. Enoki pays gas from its sponsor's address balance, so the sender
doesn't need SUI.
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! }));
// Builds with Enoki as gas owner, signs, and has Enoki sponsor and execute it
const result = await client.enoki.sponsorAndExecuteTransaction({ transaction, signer });To sign another way, such as with a wallet, or to check the transaction first, prepare it and send the signed bytes:
transaction.setSenderIfNotSet(signer.toSuiAddress());
await client.enoki.prepareSponsoredTransaction({ transaction });
const bytes = await transaction.build({ client });
const { signature } = await signer.signTransaction(bytes);
await client.enoki.sponsorAndExecuteTransaction({ transaction: bytes, signature });If isStaleTransactionError(error) is true, the sponsor address or reference gas price changed
since the transaction was built. Create a new Transaction, sign it and try again; one that was
already built keeps its gas owner and gas price.
In a browser, prepare the transaction with an EnokiClient that uses your public API key, have the
wallet sign it, and send the bytes and signature to your backend:
import { EnokiClient } from '@mysten/enoki';
const enoki = new EnokiClient({ apiKey: 'your-public-api-key' });
await enoki.prepareSponsoredTransaction({ transaction, network: 'mainnet' });
const { bytes, signature } = await dAppKit.signTransaction({ transaction });
// POST { bytes, signature } to your backend, which calls client.enoki.sponsorAndExecuteTransactionThe sponsor address is cached for up to a minute, so after Enoki rotates it a retry may fail until the cache refreshes.
Low-level client usage
Use the client-only entry point to call the Enoki API without loading wallet helpers or local
zkLogin cryptography. This is useful in environments that do not tree-shake unused exports, such as
React Native. The existing exports from @mysten/enoki remain available.
import { EnokiClient } from '@mysten/enoki/client';
const client = new EnokiClient({
apiKey: 'your-enoki-api-key',
});
// Create a sponsored transaction
const sponsored = await client.createSponsoredTransaction({
network: 'mainnet',
sender: '0x...',
transactionKindBytes: '...',
});
// Execute it
await client.executeSponsoredTransaction({
digest: sponsored.digest,
signature: '...',
});Documentation
See the Enoki documentation for detailed setup and usage.
