Published 11 February 2019

Using of Credits API in C # (demo).

References

References

https://www.youtube.com/watch?v=lSZQFMq7cHE

https://github.com/Xela101/CreditsAPIDemo

How to connect to a node to call the Credits API

How to connect to a node to call the Credits API

To call CREDITS API, you need to connect to Node’s Thrift API via port 9090. This port must be added to the firewall exceptions if you intend to connect to Node’s Thrift API from the network.

A necessary condition for the correct use of Node’s Thrift API is that the node must be fully synchronized.

The three main components you need to use with the API are:

  • Apache Thrift library (https://thrift.apache.org)  
  • ABase58 encoding/decoding library for encoding and decoding pub and priv keys
  • Elliptic-curve digital signature library

Before the code generation you need to:

After the project creation it is necessary to execute:

  1. The code generation command: thrift --gen csharp api.thrift
  2. Files will be generated in the gen-csharp directory, you need to copy and paste files into your project.
  3. Apache Thrift, (Install from Nuget - ApacheThrift, version 0.9.3)
  4. C# package called Simplebase to handle the encoding/decoding (https://github.com/ssg/SimpleBase), (Install from Nuget - SimpleBase)
  5. Chaos.NaCI to handle the elliptic-curve digital signature algorithm Ed25519 (https://github.com/dlech/Chaos.NaCl), (Install from Nuget - dlech.Chaos.NaCl)

Calls of the wallet balance are implemented in this demo:

  static void Main(string[] args)

       {

           var publicKey = "5B3YXqDTcWQFGAqEJQJP3Bg1ZK8FFtHtgCiFLT5VAxpe";

           var publicKeyBytes = SimpleBase.Base58.Bitcoin.Decode(publicKey).ToArray();

           try

           {

               using (var transport = new TSocket("127.0.0.1", 9090))

               {

                   using (var protocol = new TBinaryProtocol(transport))

                   {

                       using (var client = new API.Client(protocol))

                       {

                           transport.Open();

                           var balance = client.WalletBalanceGet(publicKeyBytes);

                           Console.WriteLine($"[{publicKey}] Balance Integral: {balance.Balance.Integral}");

                           Console.WriteLine($"[{publicKey}] Balance Fraction: {balance.Balance.Fraction}");

                       }

                   }

               }

           }

           catch (Exception exc)

           {

               Console.WriteLine(exc.Message);

           }

           Console.ReadKey();

       }

Checking the efficiency. Calling the WalletBalanceGet method

The result of the work will be the requested balance:

Link to GitHub

Votes 0, average rating 0

Contents