Using of CREDITS API in C # (demo)
References
https://www.youtube.com/watch?v=lSZQFMq7cHE
https://github.com/Xela101/CreditsAPIDemo
How to connect to a node to call the CREDITS API
To call the CREDITS API you have to connect to the Node’s Thrift API via port 9090. This port must be added to the firewall exceptions if you intend to connect to the Node’s Thrift API from the network.
Also, a prerequisite for the correct use of the 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 generating code you have to do the following:
Download CREDITS API files: https://github.com/CREDITSCOM/thrift-interface-definitions
Download Apache Thrift itself: https://thrift.apache.org
After creating the project you have to execute
The code generation command: thrift --gen csharp api.thrift
Files will be generated in gen-csharp directory, copy and paste files into your project.
Apache Thrift, (Install from Nuget - ApacheThrift)
A C# package called Simplebase to handle the encoding/decoding (https://github.com/ssg/SimpleBase), (Install from Nuget - SimpleBase)
Chaos.NaCI to handle the elliptic-curve digital signature algorithm Ed25519 (https://github.com/dlech/Chaos.NaCl), (Install from Nuget - dlech.Chaos.NaCl)
In this demo, wallet balance calls are implemented:
#region GetBalance
//Get the balance of our wallet.
var balance = client.BalanceGet(publicKeyBytes, 0);
Console.WriteLine($"[{publicKey}] Balance: {balance.Amount} CS");
Console.WriteLine($"[{publicKey}] Balance: {ConvUtils.FormatAmount(balance.Amount)} CS");
#endregion
create and execute a transaction:
#region CreateTransaction
//Create a transaction
var transaction = new Transaction();
transaction.Id = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
transaction.Source = publicKeyBytes;
transaction.Target = targetPublicKeyBytes;
transaction.Amount = new Amount(5, 0);
transaction.Balance = balance.Amount;
transaction.Fee = new Amount(0, 0);
transaction.Currency = 1;
//Create the signature of the transaction by writing the values into a memory stream and then writes the contents out into a bytearray.
byte[] bytes;
using (var memoryStream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(memoryStream))
{
writer.Write(transaction.Id);
writer.Write(transaction.Source);
writer.Write(transaction.Target);
writer.Write(transaction.Amount.Integral);
writer.Write(transaction.Amount.Fraction);
writer.Write(transaction.Fee.Integral);
writer.Write(transaction.Fee.Fraction);
writer.Write(transaction.Currency);
writer.Write(0);
}
bytes = memoryStream.ToArray();
}
var lastHash = SimpleBase.Base58.Bitcoin.Encode(client.GetLastHash());
//Sign the bytearray with the privateKey
var signature = Ed25519.Sign(bytes, privateKeyBytes);
//Verify the signature is correct.
var verifyResult = Ed25519.Verify(signature, bytes, SimpleBase.Base58.Bitcoin.Decode(publicKey).ToArray());
if (!verifyResult) throw new Exception("Signature could not be verified");
transaction.Signature = signature;
var transactionResult = client.TransactionFlow(transaction);
Console.WriteLine(transactionResult);
#endregion
Check the performance. Call the GetBalance Method.
As the result of the program we see the requested balance:
