Published 4 July 2019

Examples: C#: Send transaction

Project:

https://github.com/CREDITSCOM/examples.git

Branch:

https://github.com/CREDITSCOM/examples/tree/master/API/CreditsCSharpDemo

 

In order to receive or transfer coins to the specified address, it is necessary to transfer the public key of the wallet, the private key of the wallet and the public address of the transfer destination to the client class

 

takes the whole and fractional part of the number of coins to transfer and the amount of reward, by default, this amount for the transfer is 0.9 CS

public TransactionFlowResult TransferCoins(int integral, long fraction, double fee)

{

  return api.TransactionFlow(CreateTransaction(integral, fraction, fee));

}

a method of creating transactions, a signature and its signing

private Transaction CreateTransaction(int integral, long fraction, double fee)

{

   var transaction = new Transaction();

   transaction.Id = api.WalletTransactionsCountGet(keys.PublicKeyBytes).LastTransactionInnerId + 1;

   transaction.Source = keys.PublicKeyBytes;

   transaction.Target = keys.PublicKeyBytes;

   transaction.Amount = new Amount(integral, fraction);

   transaction.Fee = new AmountCommission(Fee(fee));

   transaction.Currency = 1;

   var bytes = new byte[86];

   Array.Copy(BitConverter.GetBytes(transaction.Id), 0, bytes, 0, 6);

   Array.Copy(transaction.Source, 0, bytes, 6, 32);

   Array.Copy(transaction.Target, 0, bytes, 38, 32);

   Array.Copy(BitConverter.GetBytes(transaction.Amount.Integral), 0, bytes, 70, 4);

   Array.Copy(BitConverter.GetBytes(transaction.Amount.Fraction), 0, bytes, 74, 8);

   Array.Copy(BitConverter.GetBytes(transaction.Fee.Commission), 0, bytes, 82, 2);

   bytes[84] = 1;

   bytes[85] = 0;

   var signature = Ed25519.Sign(bytes, keys.PrivateKeyBytes);

   var verifyResult = Ed25519.Verify(signature, bytes, keys.PublicKeyBytes);

   if (!verifyResult) throw new Exception("Signature could not be verified");

   transaction.Signature = signature;

   return transaction;

}

Call example

static void Main(string[] args)

{

   Console.WriteLine("Credits API Simple Demo");

   if(args.Length != 5)

   {

       Console.WriteLine("Usage: CreditsCSAPIDemo NodeIpAddress NodePort YourPublicKey YourPrivateKey TargetPublicKey");

       return;

   }

   using (var client = new Client(args[0], Convert.ToInt32(args[1]), args[2], args[3], args[4]))

   {

       Console.WriteLine("Result of the transfer coins:");

       Console.WriteLine(client.TransferCoins(1, 0, 0.9));

   }

   Console.WriteLine("Press [Enter] to exit...");

   Console.ReadLine();

}

Votes 0, average rating 0

Contents