Skip to content

worldwide-asset-exchange/cloudwallet-unity-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloud Wallet Unity SDK (Plugin) and Demo Documentation

Introduction

The CloudWallet plugin for Unity enables dApp developers to seamlessly establish the connection between the dapp with the Cloud Wallet. This plugin provides essential features such as wallet activation, wallet deactivation, and transaction signing.

This repository has the Unity Plugin and a Demo that shows how to implement it.

New: The plugin now also supports direct wallet connection and direct transaction signing, enabling faster and more streamlined user experiences for certain scenarios.

Project Structure

Folder Description
Assets/CloudWalletDemo Contains the dapp that uses the SDK as an example of the implementation
Assets/CloudWalletPlugin Houses the SDK itself

Cloud Wallet Demo

Please check the Cloud Wallet Demo documentation for further information about the Demo.

Cloud Wallet Plugin

Please check the Cloud Wallet Plugin documentation for further information about the Plugin.

Summary

Dependencies

The Cloud Wallet Plugin requires the following dependencies:

The internal dependencies required for the Cloud Wallet plugin to function properly.

Dependency Description
GraphQL.Client.Abstractions.dll Provides abstractions for handling GraphQL queries, mutations, and subscriptions.
GraphQL.Client.Abstractions.Websocket.dll Adds WebSocket support to the GraphQL client for handling subscriptions.
GraphQL.Client.Serializer.Newtonsoft.dll Integrates Newtonsoft JSON as the serializer for the GraphQL client.
GraphQL.Client.dll Core library for making GraphQL requests, including queries and mutations.
GraphQL.Primitives.dll Contains primitive types used in GraphQL to support the client's operations.
Microsoft.Bcl.AsyncInterfaces.dll Provides asynchronous interfaces like IAsyncEnumerable<T> for .NET Framework compatibility.
Newtonsoft.Json.dll Widely-used JSON library for serialization and deserialization in .NET applications.
System.Reactive.dll Enables reactive programming by composing asynchronous and event-based programs using observable sequences.
System.Reactive.* Additional libraries in the Reactive Extensions suite, supporting various reactive programming patterns.

⚠️ Warning: Make sure to have these dependencies installed; the plugin will not work without them.

Initialization

The plugin implements the Singleton pattern to ensure that only one instance of the CloudWalletPlugin class exists throughout the application. This is important to manage the wallet's state and avoid multiple initializations consistently.

public sealed class DemoScript : MonoBehaviour
{
    public CloudWalletConnector cloudWalletConnector;
    private void Start()
    {
        cloudWalletPluginConnector = CloudWalletPluginConnector.Initialize(
            dappName: "DappUnityTestName",
            origin: "dappunitytestname.com",
            icon: "https://picsum.photos/200",
            description: "dapp integration using the Unity Cloud Wallet Plugin",
            schema: "dappschema",
            sutGenerationUrl: "your-backend-api-url/single-use-token/generate",
            sdkClientId: "your-sdk-client-id"
        );
    }
}

Activate Wallet

This process activates the user's wallet by connecting it to the dApp. The behavior varies depending on the platform:

  • Mobile Platforms (Android, iOS): On mobile platforms, the activation function sends a request to the Cloud Wallet app installed on the same device to connect the wallet. If the app is not installed, the user is redirected to the appropriate app store (Google Play Store for Android, Apple App Store for iOS).

  • Non-Mobile Platforms: For non-mobile platforms, a modal is opened, allowing the user to scan a QR code with the Cloud Wallet app on their mobile device. The activation is confirmed through the app.

// DemoScript : MonoBehaviour Class
public async void ConnectWallet()
{
   try
   {
        string randomNonce = Guid.NewGuid().ToString();
        await cloudWalletPluginConnector.Activate(randomNonce);
   }
   catch (System.Exception e)
   {
       ShowErrors(e.Message);
   }
}

Important: the Random Nonce is optional and is only necessary in case you need to get the proof of login for any backend authentication.

Deactivate Wallet

Deactivates the user's wallet, disconnecting it from the dApp. This function terminates the wallet session and clears the user login information.

// DemoScript : MonoBehaviour Class
public async void DisconnectWallet()
{
    try
    {
        await cloudWalletConnector.Deactivate();
        UpdateUI();
    }
    catch (System.Exception e)
    {
        ShowErrors(e.Message);
    }
}

Sign Transaction

This function constructs the transaction, sends it to the Cloud Wallet app for signing, and waits for the response.

// DemoScript : MonoBehaviour Class
public async void SignTransaction()
{
    try
    {
        loadingIndicator.SetActive(true);
        object[] actions = new[]
        {
            new
            {
                account = "eosio.token",
                name = "transfer",
                authorization = new[]
                {
                    new
                    {
                        actor = cloudWalletPluginConnector.WalletAccount,
                        permission = "active"
                    }
                },
                data = new
                {
                    from = cloudWalletPluginConnector.WalletAccount,
                    to = "cc.wam",
                    quantity = "1.00000001 WAX",
                    memo = "WAX Transfer From Demo MCW Plugin"
                }
            }
        };
        /**
         * NamedParams is a class that contains the named parameters for the transaction.
         * It is used to pass the named parameters to the SignTransaction method.
         * The named parameters are optional and can be null, in that case, it will be initialized with default values.
         */
        NamedParams namedParams = new(
            BlocksBehind: 3,
            ExpireSeconds: 30000
        );
        Subscribe2ChannelDataMessageContent response = await cloudWalletPluginConnector.SignTransaction(actions, namedParams);
        loadingIndicator.SetActive(false);
    }
    catch (Exception e)
    {
        Debug.LogError(e.Message);
        loadingIndicator.SetActive(false);
        ShowErrors(e.Message);
        UpdateUI();
    }
}

Properties Available

Property Name Description
Instance Returns the Cloud Wallet Plugin Connector instance if exists.
IsLoggedIn Returns true if the user is logged in, and false if not.
IsUserTemp Returns true if the logged-in user is a temporary user, and null if no user is logged in.
ProofReferer Returns the proof referer of the logged-in user, and null if no user is logged in or proof is not available.
ProofSignature Returns the proof signature of the logged-in user, and null if no user is logged in or proof is not available.
UserAccount Returns the account of the logged-in user, and null if no user is logged in.
UserAvatarUrl Returns the avatar URL of the logged-in user, and null if no user is logged in.
UserCreateDate Returns the creation date of the logged-in user, and null if no user is logged in.
UserKeys Returns a list of keys of the logged-in user, and null if no user is logged in.
UserTrustScore Returns the trust score of the logged-in user, and null if no user is logged in.

Direct Connect

This function provides a direct connection to the Cloud Wallet without requiring the full activation flow. It's useful for scenarios where you want to quickly connect to a wallet without the full authentication process.

// DemoScript : MonoBehaviour Class
public async void DirectConnect()
{
    try
    {
        string account = await cloudWalletPluginConnector.DirectConnect();
        if (!string.IsNullOrEmpty(account))
        {
            // Handle successful connection
            ShowSuccessMessage($"Connected to account: {account}");
        }
    }
    catch (Exception e)
    {
        ShowErrors(e.Message);
    }
}

When to use: Use DirectConnect when you want to quickly establish a session with the wallet, for example, in trusted environments or for streamlined user flows where full authentication is not required.

Direct Transact

This function allows for direct transaction signing without going through the full activation flow. It's particularly useful when you want to perform transactions quickly after a direct connection. The function takes the same parameters as SignTransaction but uses a more streamlined process.

// DemoScript : MonoBehaviour Class
public async void DirectTransact()
{
    try
    {
        object[] actions = new[]
        {
            new
            {
                account = "eosio.token",
                name = "transfer",
                authorization = new[]
                {
                    new
                    {
                        actor = cloudWalletPluginConnector.UserAccount,
                        permission = "active"
                    }
                },
                data = new
                {
                    from = cloudWalletPluginConnector.UserAccount,
                    to = "recipient.account",
                    quantity = "1.00000001 WAX",
                    memo = "Direct transaction from Cloud Wallet"
                }
            }
        };

        NamedParams namedParams = new(
            BlocksBehind: 3,
            ExpireSeconds: 30000
        );

        TransactionApprovedResult response = await cloudWalletPluginConnector.DirectTransact(
            actions,
            namedParams
        );
    }
    catch (Exception e)
    {
        ShowErrors(e.Message);
    }
}

When to use: Use DirectTransact after a successful DirectConnect to quickly sign and send transactions without requiring the user to go through the full wallet activation process.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •