Giter Club home page Giter Club logo

postchain-unity's Introduction

Postchain Client Unity

Compatible with Postchain 3.3.3 / Rell 0.10.5

This Unity package offers an API for the Chromia Blockchain. It includes normal transaction serialization, an API for Chromia's FT3 library as well as their SSO.

Currently tested with Standalone (Windows and Linux) as well as WebGL builds.

Examples

Postchain

private void Start()
{
    // Create BlockchainClient
    GameObject goConnection = new GameObject();
    BlockchainClient connection = goConnection.AddComponent<BlockchainClient>();

    connection.Setup(
        "5759EB34C39B4D34744EC324DFEFAC61526DCEB37FB05D22EB7C95A184380205",
        "http://localhost:7740"
    );

    // Create keypair
    var keyPair = PostchainUtil.MakeKeyPair();

    // Create and execute Operation
    var request = connection.NewTransaction(new byte[][] { keyPair["pubKey"] }, ErrorHandler);
    request.AddOperation("test_operation_create_user", "Bob");

    request.Sign(keyPair["privKey"], keyPair["pubKey"]);
    StartCoroutine(request.PostAndWait(() => Debug.Log("Successfully registered Bob")));

    // Query
    StartCoroutine(connection.Query<string>("find_user", new (string, object)[] { ("username", "Bob") },
        (string found) => Debug.Log("Found: " + found), ErrorHandler));
}

#region Helper
private void ErrorHandler(string error)
{
    throw new System.Exception(error);
}
#endregion

FT3

public Blockchain Blockchain;

private void Start()
{
    StartCoroutine(ExampleFlow());
}

private IEnumerator ExampleFlow()
{
    // Connect to Blockchain
    Postchain postchain = new Postchain("http://localhost:7740");
    yield return postchain.Blockchain("5759EB34C39B4D34744EC324DFEFAC61526DCEB37FB05D22EB7C95A184380205",
         SetBlockchain, ErrorHandler);

    // Register Ft3 Account
    KeyPair keyPair = new KeyPair();
    SingleSignatureAuthDescriptor singleSigAuthDescriptor = new SingleSignatureAuthDescriptor(
        keyPair.PubKey,
        new FlagsType[] { FlagsType.Account, FlagsType.Transfer }
    );

    // Create user who holds the keypair and the authdescriptor
    User user = new User(keyPair, singleSigAuthDescriptor);
    Account account = null;
    yield return this.Blockchain.RegisterAccount(user.AuthDescriptor, user,
        (Account _account) => account = _account, ErrorHandler);

    // Register Asset
    Asset asset = null;
    yield return Asset.Register("TestAsset", "5759EB34C39B4D34744EC324DFEFAC61526DCEB37FB05D22EB7C95A184380205",
        Blockchain, (Asset _asset) => asset = _asset, ErrorHandler);

    // Give Balance
    yield return AssetBalance.GiveBalance(account.Id, asset.Id, 1000, Blockchain, () => { }, ErrorHandler);

    // Sync Account and print asset amount
    yield return account.Sync(() => Debug.Log(account.Assets[0].Amount), ErrorHandler);

    // Burn
    yield return account.BurnTokens(asset.Id, 500, () => Debug.Log("Successfully burned"), ErrorHandler);

    // Self Transfer
    yield return account.Transfer(account.Id, asset.Id, 250, () => Debug.Log("Selftransfer completed"), ErrorHandler);

    // Custom Operation
    yield return Blockchain.TransactionBuilder()
        .Add(Operation.Op("test_operation_create_user", "Bob"))
        .Build(user.AuthDescriptor.Signers.ToArray(), ErrorHandler)
        .Sign(user.KeyPair)
        .Post();

    // Custom Query
    yield return Blockchain.Query<string>("find_user", new (string, object)[] { ("username", "Bob") },
        (string found) => Debug.Log("Found: " + found), ErrorHandler);
}


#region Helper
private void SetBlockchain(Blockchain blockchain)
{
    Blockchain = blockchain;
}

private void ErrorHandler(string error)
{
    throw new System.Exception(error);
}
#endregion

SSO Standalone

private const string CUSTOM_SCHEME = "sso";

private Blockchain _blockchain;
private SSO _sso;

private void Awake()
{
    ProtocolHandler.HandleTempTx(CUSTOM_SCHEME);
    SSO.VaultUrl = "https://vault-testnet.chromia.com";
}

private void Start()
{
    StartCoroutine(ExampleFlow());
}

private IEnumerator ExampleFlow()
{
    Postchain postchain = new Postchain("http://localhost:7740");
    yield return postchain.Blockchain("5759EB34C39B4D34744EC324DFEFAC61526DCEB37FB05D22EB7C95A184380205",
        SetBlockchain, ErrorHandler);

    _sso = new SSO(this._blockchain);


    List<(Account, User)> found = null;
    yield return _sso.AutoLogin((List<(Account, User)> aus) => found = aus, ErrorHandler);

    if (found.Count == 0)
    {
        // Trigger login
        yield return SSOS();
    }
}

private IEnumerator SSOS()
{
    ProtocolHandler.Register(CUSTOM_SCHEME);
    // Use registered scheme here
    _sso.InitiateLogin("sso://success", "sso://cancel");

    while (_sso.Store.TmpTx == null)
    {
        yield return new WaitForSeconds(3);
        _sso.Store.Load();
    }
    string payload = _sso.Store.TmpTx;

    try
    {
        payload = payload.Split("?"[0])[1];
        payload = payload.Split("="[0])[1];
    }
    catch (System.Exception)
    {
        throw;
    }

    yield return _sso.FinalizeLogin(payload,
        ((Account, User) au) => Debug.Log("Successfully registered via sso"), ErrorHandler);
}

#region Helper
private void SetBlockchain(Blockchain blockchain)
{
    this._blockchain = blockchain;
}

private void ErrorHandler(string error)
{
    throw new Exception(error);
}
#endregion

See Samples/FT3/SSO* for for further demo scenes and webgl.

Test

Run the rell code defined in Samples/FT3/rell and use Unity's internal test suite.

Support

Use the Chromia Dev Telegram Chat

postchain-unity's People

Contributors

cherden avatar simse92 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.