Skip to main content

6. Set up a wallet

💡Info
The complete code for this step can be found in the branch named step_06.

Players must first set up a wallet to mint or craft in-game items. This process involves initialising a provider to communicate with Immutable zkEVM.

In LevelCompleteScreen.cs, take the player to the wallet creation screen after a successful Passport login.

Assets/Shared/Scripts/UI/LevelCompleteScreen.cs

private async void OnContinueWithPassportButtonClicked()
{
// omitted

// Successfully logged in
// Save a persistent flag in the game that the player is logged in
SaveManager.Instance.IsLoggedIn = true;
// Show 'Next' button
ShowNextButton(true);
ShowLoading(false);
// Take the player to the Setup Wallet screen
m_SetupWalletEvent.Raise();

// omitted
}

Initialise the provider and wallet

Use the ConnectEvm() function to instantiate Passport's zkEVM provider, and then call ZkEvmRequestAccounts() to initialise the player's wallet.

⚠️Warning
ZkEvmRequestAccounts is a resource-intensive function. To ensure optimal gaming experience and performance, it is recommended to call this function separately or asynchronously from the login process.

Assets/Shared/Scripts/UI/SetupWalletScreen.cs

using Immutable.Passport;

private async void SetupWallet()
{
try
{
m_Title.text = "Setting up your wallet...";
ShowLoading(true);
ShowError(false);
ShowNextButton(false);

// Set up provider
await Passport.Instance.ConnectEvm();
// Set up wallet (includes creating a wallet for new players)
await Passport.Instance.ZkEvmRequestAccounts();

m_Title.text = "Your wallet has been successfully set up!";
ShowLoading(false);
ShowError(false);
ShowNextButton(true);
}
catch (Exception ex)
{
// Failed to set up wallet, let the player try again
Debug.Log($"Failed to set up wallet: {ex.Message}");
ShowLoading(false);
ShowError(true);
ShowNextButton(false);
}
}

Set up wallet on re-login

Also, set up the player’s wallet when they log back into Passport on the main menu screen.

Assets/Shared/Scripts/UI/MainMenu.cs

async void OnEnable()
{
// omitted

// Check if the player is supposed to be logged in and if there are credentials saved
if (SaveManager.Instance.IsLoggedIn && await Passport.Instance.HasCredentialsSaved())
{
// Try to log in using saved credentials
bool success = await Passport.Instance.Login(useCachedSession: true);
// Update the login flag
SaveManager.Instance.IsLoggedIn = success;
// Set up wallet if successful
if (success)
{
await Passport.Instance.ConnectEvm();
await Passport.Instance.ZkEvmRequestAccounts();
}
} else {
// No saved credentials to re-login the player, reset the login flag
SaveManager.Instance.IsLoggedIn = false;
}

// omitted
}