Enoki SDK Examples
The following examples highlight some common workflows for onboarding users using Enoki.
Use Enoki for client-side zkLogin
Enoki offers public
API keys so you can integrate client-side authentication with zkLogin. To do
so:
-
Log in to the Enoki Portal and select the desired app from your team's dashboard.
-
Create a
public
API key enabled for zkLogin and the network you want to target. -
Use the dashboard to add an auth provider and configure the Client ID value it provides.
-
Handle the sign in from your client-side application. The following example handles zkLogin with a Gmail account:
-
Use an auth callback page to handle redirect after a successful log in:
Sponsored transactions using client-side signature
To sponsor and execute transactions in your backend service using client-side signatures:
-
Log in to the Enoki Portal and select the desired app from your team's dashboard.
-
Create a
private
API key enabled for Sponsored Transactions and the networks you want to target. -
Create an Enoki client in your backend service using the key you create in the previous step.
Keep your secret key in an
env
variable and take care not to expose it publicly. -
Create a new
Transaction
, include all the transactions and convert it totransactionBytes (Uint8Array)
. The following example sets the kiosk owner and transfers thekioskOwnerCap
to the recipient. -
Make a call to the
enokiClient
to sponsor the transaction. The response returned will be the bytes and digest of thetransaction
, which is later used to execute the transaction:Set the sender of the transaction, include the move call you are going to invoke in the
allowedMoveCallTargets
field and the recipient (if any) in theallowedAddresses
field. You can also configure these values in the Enoki Portal project dashboard. -
Execute the sponsored transaction. You need both the user’s signature and the digest to execute the transaction. The previous call returns the digest but you must collect the signature from the user. Assuming the user session is on the client side, you need to return the transaction bytes to the client and collect the signature as shown:
The code snippet gets the
bytes
value from thecreateSponsoredTransaction()
response, which it uses to collect the user signature. The code then needs to return that signature to the backend service, along with the digest, to execute the transaction:
Backend sponsor, execute, and sign transactions
When sponsoring, executing, and signing transactions from a backend, you don't need to transfer the transaction bytes to the client to get the signature. Instead, you can use the initial steps from the previous example to get the API key and create the Enoki client, then execute the following code from the backend:
The backend service executes this code without the need to transfer from server side to client side, because the backend stores the signer.
Migrating a dApp to Mainnet
During dApp development, you typically implement the Enoki workflows in these examples on one of the Sui development networks, Testnet or Devnet. Before migrating an Enoki-enhanced dApp from development networks to Mainnet, check that:
- You set the network parameter that might be passed to the
enokiClient.createAuthorizationURL
or to theenokiClient.createSponsoredTransaction
functions is set tomainnet
. - In the Enoki Portal, make sure you have Mainnet enabled in the networks list for the API keys that are used for the sponsorship.