forked from pvl1175/CreditsCSharpDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
146 lines (128 loc) · 4.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using Chaos.NaCl;
using NodeApi;
using System;
using System.Diagnostics;
using System.IO;
using Thrift.Protocol;
using Thrift.Transport;
namespace CreditsCSAPIDemo
{
class Keys
{
public string PublicKey { get; set; }
public string PrivateKey { get; set; }
public byte[] PublicKeyBytes
{
get
{
return PublicKey != null ? SimpleBase.Base58.Bitcoin.Decode(PublicKey).ToArray() : null;
}
}
public byte[] PrivateKeyBytes
{
get
{
return PrivateKey != null ? SimpleBase.Base58.Bitcoin.Decode(PrivateKey).ToArray() : null;
}
}
}
class ClientEx
{
Keys sourceKeys;
Keys targetKeys;
API.Client client;
public ClientEx(API.Client client, Keys sk, Keys tk)
{
sourceKeys = sk;
targetKeys = tk;
this.client = client;
}
public TransactionFlowResult ExecuteTransaction()
{
return client.TransactionFlow(CreateTransaction());
}
private Transaction CreateTransaction()
{
var transaction = new Transaction();
transaction.Id = client.WalletTransactionsCountGet(sourceKeys.PublicKeyBytes).LastTransactionInnerId + 1;
transaction.Source = sourceKeys.PublicKeyBytes;
transaction.Target = targetKeys.PublicKeyBytes;
transaction.Amount = new Amount(1, 0);
transaction.Fee = new AmountCommission(Fee(0.9));
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, sourceKeys.PrivateKeyBytes);
var verifyResult = Ed25519.Verify(signature, bytes, sourceKeys.PublicKeyBytes);
if (!verifyResult) throw new Exception("Signature could not be verified");
transaction.Signature = signature;
return transaction;
}
private short Fee(Double value)
{
byte sign = (byte)(value < 0.0 ? 1 : 0); // sign
int exp; // exponent
long frac; // mantissa
value = Math.Abs(value);
double expf = value == 0.0 ? 0.0 : Math.Log10(value);
int expi = Convert.ToInt32(expf >= 0 ? expf + 0.5 : expf - 0.5);
value /= Math.Pow(10, expi);
if (value >= 1.0)
{
value *= 0.1;
++expi;
}
exp = expi + 18;
if (exp < 0 || exp > 28)
{
throw new Exception($"exponent value {exp} out of range [0, 28]");
}
frac = (long)Math.Round(value * 1024);
return (short)(sign * 32768 + exp * 1024 + frac);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Credits API Simple Demo");
if(args.Length != 4)
{
Console.WriteLine("Usage: CreditsCSAPIDemo NodeIpAddress YourPublicKey YourPrivateKey TargetPublicKey");
return;
}
var SourceKeys = new Keys
{
PublicKey = args[1],
PrivateKey = args[2]
};
var TargetKeys = new Keys
{
PublicKey = args[3],
};
using (var transport = new TSocket(args[0], 9090))
{
using (var protocol = new TBinaryProtocol(transport))
{
using (var client = new API.Client(protocol))
{
transport.Open();
var balance = client.WalletBalanceGet(SourceKeys.PublicKeyBytes);
Console.WriteLine($"[{SourceKeys.PublicKey}] Balance: {balance.Balance.ToString()}");
var clientEx = new ClientEx(client, SourceKeys, TargetKeys);
Console.WriteLine(clientEx.ExecuteTransaction());
}
}
}
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
}
}