-
Notifications
You must be signed in to change notification settings - Fork 3
/
transaction.d
470 lines (417 loc) · 7.83 KB
/
transaction.d
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// Author: Ivan Kazmenko ([email protected])
module transaction;
import core.bitop;
import core.stdc.stdint;
import std.algorithm;
import std.conv;
import std.datetime;
import std.exception;
import std.format;
import std.json;
import std.net.curl;
import std.range;
import std.stdio;
import std.string;
import std.traits;
alias int8 = byte;
alias int16 = short;
alias int32 = int;
alias int64 = long;
alias uint8 = ubyte;
alias uint16 = ushort;
alias uint32 = uint;
alias uint64 = ulong;
alias float32 = float;
alias float64 = double;
uint64_t charToBase32 (char c)
{
if ('a' <= c && c <= 'z')
{
return (c - 'a') + 6;
}
if ('1' <= c && c <= '5')
{
return (c - '1') + 1;
}
if (c == '.')
{
return 0;
}
assert (false);
}
char base32ToChar (uint64_t v)
{
if (v == 0)
{
return '.';
}
if (1 <= v && v < 6)
{
return cast (char) (v - 1 + '1');
}
if (6 <= v && v < 32)
{
return cast (char) (v - 6 + 'a');
}
assert (false);
}
uint64_t stringToName (string input)
{
uint64_t name = 0;
foreach (i; 0..input.length)
{
name |= (charToBase32 (input[i]) & 0x1f) << (64 - 5 * (i + 1));
}
return name;
}
string nameToString (uint64_t input)
{
string res;
for (int pos = 64 - 5; input != 0; pos = max (pos - 5, 0))
{
auto cur = (input >> pos) & 0x1f;
res ~= base32ToChar (cur);
input ^= cur << pos;
}
return res;
}
struct VarUint32
{
uint value;
this (uint value_)
{
value = value_;
}
alias value this;
ubyte [] toBinary () const
{
ubyte [] res;
uint cur = value;
do
{
if (!res.empty)
{
res.back |= 128;
}
res ~= cur & 127;
cur >>= 7;
}
while (cur > 0);
return res;
}
}
ubyte [] toBinary (T) (T value)
{
ubyte [] res;
static if (is (Unqual !(T) == E [], E))
{
res ~= VarUint32 (value.length.to !(uint)).toBinary;
foreach (ref element; value)
{
res ~= element.toBinary;
}
}
else static if (is (T == struct))
{
static foreach (field; FieldNameTuple !(T))
{
res ~= mixin ("value." ~ field).toBinary;
}
}
else
{
res ~= * (cast (ubyte [T.sizeof] *) (&value));
}
return res;
}
struct Name
{
uint64_t value;
alias value this;
this (string name)
{
value = stringToName (name);
}
string toString () const
{
return nameToString (value);
}
}
struct CurrencySymbol
{
static immutable int maxNameLength = 7;
uint8_t point;
char [maxNameLength] name;
ubyte [] toBinary () const
{
ubyte [] res;
res ~= point.toBinary;
res ~= name.toBinary;
return res;
}
int opCmp () (const auto ref CurrencySymbol that) const
{
if (this.point != that.point)
{
return (this.point > that.point) -
(this.point < that.point);
}
return (this.name > that.name) - (this.name < that.name);
}
string prettyName () const
{
return name[].stripRight ('\0').text;
}
}
struct CurrencyAmount
{
uint64_t quantity;
CurrencySymbol symbol;
this (string s)
{
auto t = s.split (" ");
auto r = t[0].split (".");
symbol.point = 0;
if (r.length == 2)
{
symbol.point = to !(uint8_t) (r[1].length);
r[0] ~= r[1];
}
symbol.name[] = '\0';
enforce (t[1].length <= symbol.maxNameLength);
symbol.name[0..t[1].length] = t[1][];
quantity = to !(uint64_t) (r[0]);
}
CurrencyAmount opBinary (string op)
(const auto ref CurrencyAmount that) const
if (op == "+")
{
enforce (this.symbol == that.symbol);
CurrencyAmount res = this;
res.quantity = this.quantity + that.quantity;
return res;
}
ubyte [] toBinary () const
{
ubyte [] res;
res ~= quantity.toBinary;
res ~= symbol.toBinary;
return res;
}
}
class AuthLevel
{
Name actor;
Name permission;
this (string actor_, string permission_)
{
actor = actor_;
permission = permission_;
}
override string toString () const
{
return format (`{"actor": "%s", "permission": "%s"}`,
actor, permission);
}
ubyte [] toBinary () const
{
ubyte [] res;
res ~= actor.toBinary;
res ~= permission.toBinary;
return res;
}
}
class Action
{
Name account;
Name name;
AuthLevel [] auths;
ubyte [] hexData;
this (Args...) (string account_, string name_, string [] auths_,
Args args)
{
account = account_;
name = name_;
auths = auths_.chunks (2).map !(line =>
new AuthLevel (line[0], line[1])).array;
hexData = null;
static foreach (cur; args)
{
hexData ~= cur.toBinary;
}
}
override string toString () const
{
return format (` {
"account": "%s",
"name": "%s",
"authorization": [%(
%s,
%)
],
"data": "%(%02x%)"
}`, account, name, auths, hexData);
}
ubyte [] toBinary () const
{
ubyte [] res;
res ~= account.toBinary;
res ~= name.toBinary;
res ~= auths.toBinary;
res ~= hexData.toBinary;
return res;
}
}
class TransactionHeader
{
uint expiration;
ushort refBlockNum;
uint refBlockPrefix;
VarUint32 maxNetUsageWords;
ubyte maxCpuUsageMs;
VarUint32 delaySec;
this ()
{
expiration = cast (uint) ((Clock.currTime (UTC ()) +
1.hours - 2.minutes).toUnixTime !(int));
refBlockNum = ChainState.chainState.refBlockNum;
refBlockPrefix = ChainState.chainState.refBlockPrefix;
maxNetUsageWords = 0;
maxCpuUsageMs = 0;
delaySec = 0;
}
override string toString () const
{
return format (`
"expiration": "%s",
"ref_block_num": %s,
"ref_block_prefix": %s,
"max_net_usage_words": %s,
"max_cpu_usage_ms": %s,
"delay_sec": %s,`, SysTime.fromUnixTime (expiration.to !(long), UTC ())
.toISOExtString[0..$ - 1], refBlockNum, refBlockPrefix,
maxNetUsageWords.value, maxCpuUsageMs, delaySec.value);
}
ubyte [] toBinary () const
{
ubyte [] res;
res ~= expiration.toBinary;
res ~= refBlockNum.toBinary;
res ~= refBlockPrefix.toBinary;
res ~= maxNetUsageWords.toBinary;
res ~= maxCpuUsageMs.toBinary;
res ~= delaySec.toBinary;
return res;
}
}
class Transaction
{
TransactionHeader header;
Action [] contextFreeActions;
Action [] actions;
uint [] transactionExtensions;
this (Action [] actions_)
{
header = new TransactionHeader ();
contextFreeActions = null;
actions = actions_;
transactionExtensions = null;
}
override string toString () const
{
return format (`{%s
"context_free_actions": [%(
%s,
%)
],
"actions": [%(
%s,
%)
],
"transaction_extensions": [%(
%s,
%)
]
}`, header, contextFreeActions, actions, transactionExtensions);
}
ubyte [] toBinary () const
{
ubyte [] res;
res ~= header.toBinary;
res ~= contextFreeActions.toBinary;
res ~= actions.toBinary;
res ~= transactionExtensions.toBinary;
return res;
}
}
class EosUrl
{
static string defaultUrl = "https://eos.greymass.com";
string url;
this (string url_)
{
url = url_;
}
static EosUrl eosUrl_ = null;
@property static string eosUrl ()
{
if (eosUrl_ is null)
{
eosUrl_ = new EosUrl (defaultUrl);
}
return eosUrl_.url;
}
@property static void eosUrl (string url_)
{
eosUrl_ = new EosUrl (url_);
}
}
class ChainState
{
ushort refBlockNum;
uint refBlockPrefix;
this ()
{
auto cur = get (EosUrl.eosUrl ~ "/v1/chain/get_info")
.parseJSON;
auto temp = cur["last_irreversible_block_id"].str;
refBlockNum = temp[4..8].to !(ushort) (16);
refBlockPrefix = temp[16..24].to !(uint) (16).bswap;
}
this (string url)
{
EosUrl.eosUrl = url;
this ();
}
static ChainState chainState_ = null;
@property static ChainState chainState ()
{
if (chainState_ is null)
{
chainState_ = new ChainState ();
}
return chainState_;
}
@property static void chainState (ChainState that)
{
chainState_ = that;
}
}
void testTransaction ()
{
EosUrl.eosUrl = "https://jungle2.cryptolions.io";
auto a = new Action ("eosio.token", "transfer",
["useraccount1", "active"],
Name ("useraccount1"), Name ("useraccount2"),
CurrencyAmount ("0.0001 EOS") + CurrencyAmount ("0.0002 EOS"),
"test");
auto t = new Transaction ([a]);
auto s = t.toString;
auto b = t.toBinary;
// writeln (s);
// writefln ("%(%02x%)", b);
}
unittest
{
// testTransaction ();
}