@steemit/steem-js 0.8.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/README.md +202 -105
  2. package/dist/api/index.d.ts +128 -0
  3. package/dist/api/methods.d.ts +9 -0
  4. package/dist/api/rpc-auth.d.ts +43 -0
  5. package/dist/api/transports/base.d.ts +13 -0
  6. package/dist/api/transports/http.d.ts +9 -0
  7. package/dist/api/transports/index.d.ts +9 -0
  8. package/dist/api/transports/types.d.ts +35 -0
  9. package/dist/api/transports/ws.d.ts +18 -0
  10. package/dist/auth/ecc/index.d.ts +9 -0
  11. package/dist/auth/ecc/src/address.d.ts +13 -0
  12. package/dist/auth/ecc/src/aes.d.ts +16 -0
  13. package/dist/auth/ecc/src/brain_key.d.ts +1 -0
  14. package/dist/auth/ecc/src/ecdsa.d.ts +28 -0
  15. package/dist/auth/ecc/src/ecsignature.d.ts +19 -0
  16. package/dist/auth/ecc/src/enforce_types.d.ts +5 -0
  17. package/dist/auth/ecc/src/hash.d.ts +25 -0
  18. package/dist/auth/ecc/src/index.d.ts +9 -0
  19. package/dist/auth/ecc/src/key_private.d.ts +38 -0
  20. package/dist/auth/ecc/src/key_public.d.ts +41 -0
  21. package/dist/auth/ecc/src/key_utils.d.ts +9 -0
  22. package/dist/auth/ecc/src/signature.d.ts +18 -0
  23. package/dist/auth/ecc.d.ts +3 -0
  24. package/dist/auth/index.d.ts +48 -0
  25. package/dist/auth/key_classes.d.ts +3 -0
  26. package/dist/auth/serializer.d.ts +19 -0
  27. package/dist/broadcast/helpers.d.ts +11 -0
  28. package/dist/broadcast/index.d.ts +43 -0
  29. package/dist/broadcast/operations.d.ts +6 -0
  30. package/dist/config.d.ts +25 -0
  31. package/dist/crypto/index.d.ts +25 -0
  32. package/dist/formatter/index.d.ts +92 -0
  33. package/dist/index.cjs +25848 -0
  34. package/dist/index.cjs.map +1 -0
  35. package/dist/index.d.ts +30 -0
  36. package/dist/index.js +25812 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/index.umd.js +57513 -0
  39. package/dist/index.umd.js.map +1 -0
  40. package/dist/memo/index.d.ts +11 -0
  41. package/dist/operations/index.d.ts +44 -0
  42. package/dist/serializer/convert.d.ts +12 -0
  43. package/dist/serializer/index.d.ts +11 -0
  44. package/dist/serializer/number_utils.d.ts +8 -0
  45. package/dist/serializer/precision.d.ts +5 -0
  46. package/dist/serializer/types.d.ts +36 -0
  47. package/dist/types/index.d.ts +131 -0
  48. package/dist/types.d.ts +34 -0
  49. package/dist/utils/index.d.ts +8 -0
  50. package/dist/utils.d.ts +2 -0
  51. package/package.json +68 -87
  52. package/.dockerignore +0 -12
  53. package/.editorconfig +0 -20
  54. package/.eslintrc +0 -27
  55. package/LICENSE +0 -21
  56. package/circle.yml +0 -3
  57. package/config.json +0 -10
  58. package/dist/steem-tests.min.js +0 -4097
  59. package/dist/steem.min.js +0 -2089
  60. package/docker-webpack.config.js +0 -44
  61. package/lib/api/index.js +0 -315
  62. package/lib/api/methods.js +0 -428
  63. package/lib/api/rpc-auth.js +0 -135
  64. package/lib/api/transports/base.js +0 -31
  65. package/lib/api/transports/http.js +0 -157
  66. package/lib/api/transports/index.js +0 -13
  67. package/lib/api/transports/ws.js +0 -128
  68. package/lib/auth/ecc/index.js +0 -13
  69. package/lib/auth/ecc/src/address.js +0 -54
  70. package/lib/auth/ecc/src/aes.js +0 -148
  71. package/lib/auth/ecc/src/brain_key.js +0 -13
  72. package/lib/auth/ecc/src/ecdsa.js +0 -193
  73. package/lib/auth/ecc/src/ecsignature.js +0 -102
  74. package/lib/auth/ecc/src/enforce_types.js +0 -42
  75. package/lib/auth/ecc/src/hash.js +0 -58
  76. package/lib/auth/ecc/src/key_private.js +0 -159
  77. package/lib/auth/ecc/src/key_public.js +0 -143
  78. package/lib/auth/ecc/src/key_utils.js +0 -70
  79. package/lib/auth/ecc/src/signature.js +0 -135
  80. package/lib/auth/index.js +0 -113
  81. package/lib/auth/memo.js +0 -119
  82. package/lib/auth/serializer/index.js +0 -15
  83. package/lib/auth/serializer/src/ChainTypes.js +0 -87
  84. package/lib/auth/serializer/src/convert.js +0 -34
  85. package/lib/auth/serializer/src/error_with_cause.js +0 -27
  86. package/lib/auth/serializer/src/fast_parser.js +0 -57
  87. package/lib/auth/serializer/src/number_utils.js +0 -43
  88. package/lib/auth/serializer/src/object_id.js +0 -43
  89. package/lib/auth/serializer/src/operations.js +0 -695
  90. package/lib/auth/serializer/src/precision.js +0 -77
  91. package/lib/auth/serializer/src/serializer.js +0 -165
  92. package/lib/auth/serializer/src/template.js +0 -22
  93. package/lib/auth/serializer/src/types.js +0 -1085
  94. package/lib/auth/serializer/src/validation.js +0 -300
  95. package/lib/broadcast/helpers.js +0 -140
  96. package/lib/broadcast/index.js +0 -106
  97. package/lib/broadcast/operations.js +0 -255
  98. package/lib/browser.js +0 -25
  99. package/lib/config.js +0 -23
  100. package/lib/formatter.js +0 -163
  101. package/lib/index.js +0 -21
  102. package/lib/utils.js +0 -50
  103. package/node-18.dockerfile +0 -28
  104. package/test/Crypto.js +0 -100
  105. package/test/KeyFormats.js +0 -105
  106. package/test/all_types.js +0 -115
  107. package/test/api.test.js +0 -363
  108. package/test/broadcast.test.js +0 -231
  109. package/test/browser/BrowserTests.js +0 -55
  110. package/test/comment.test.js +0 -83
  111. package/test/hf20-accounts.test.js +0 -76
  112. package/test/hf21-sps.test.js +0 -78
  113. package/test/memo.test.js +0 -37
  114. package/test/number_utils.js +0 -28
  115. package/test/operations_test.js +0 -39
  116. package/test/promise-broadcast.test.js +0 -86
  117. package/test/reputation.test.js +0 -68
  118. package/test/smt.test.js +0 -347
  119. package/test/test-post.json +0 -13
  120. package/test/test.html +0 -13
  121. package/test/test_helper.js +0 -19
  122. package/test/types_test.js +0 -139
  123. package/test-github-workflow.bat +0 -19
  124. package/test-github-workflow.sh +0 -15
  125. package/webpack/makeConfig.js +0 -113
  126. package/webpack.config.js +0 -2
@@ -1,1085 +0,0 @@
1
- "use strict";
2
-
3
- // Low-level types that make up operations
4
-
5
- const v = require('./validation');
6
- const ObjectId = require('./object_id');
7
- const fp = require('./fast_parser');
8
- const chain_types = require('./ChainTypes');
9
- //const BigInt = require('BigInt')
10
-
11
- const ecc = require("../../ecc");
12
- const PublicKey = ecc.PublicKey;
13
- const Address = ecc.Address;
14
- const ecc_config = ecc.ecc_config;
15
- const number_utils = require("./number_utils");
16
- const fromImpliedDecimal = number_utils.fromImpliedDecimal;
17
- const Config = require("../../../config.js");
18
- const Types = {};
19
- module.exports = Types;
20
- const HEX_DUMP = process.env.npm_config__graphene_serializer_hex_dump;
21
-
22
- // Highly optimized implementation of Damm algorithm
23
- // https://en.wikipedia.org/wiki/Damm_algorithm
24
- function damm_checksum_8digit(value) {
25
- if (value >= 100000000) throw new Error("Expected value less than 100000000, instead got " + value);
26
- const t = [0, 30, 10, 70, 50, 90, 80, 60, 40, 20, 70, 0, 90, 20, 10, 50, 40, 80, 60, 30, 40, 20, 0, 60, 80, 70, 10, 30, 50, 90, 10, 70, 50, 0, 90, 80, 30, 40, 20, 60, 60, 10, 20, 30, 0, 40, 50, 90, 70, 80, 30, 60, 70, 40, 20, 0, 90, 50, 80, 10, 50, 80, 60, 90, 70, 20, 0, 10, 30, 40, 80, 90, 40, 50, 30, 60, 20, 0, 10, 70, 90, 40, 30, 80, 60, 10, 70, 20, 0, 50, 20, 50, 80, 10, 40, 30, 60, 70, 90, 0];
27
- let q0 = value / 10;
28
- let d0 = value % 10;
29
- let q1 = q0 / 10;
30
- let d1 = q0 % 10;
31
- let q2 = q1 / 10;
32
- let d2 = q1 % 10;
33
- let q3 = q2 / 10;
34
- let d3 = q2 % 10;
35
- let q4 = q3 / 10;
36
- let d4 = q3 % 10;
37
- let q5 = q4 / 10;
38
- let d5 = q4 % 10;
39
- let d6 = q5 % 10;
40
- let d7 = q5 / 10;
41
- let x = t[d7];
42
- x = t[x + d6];
43
- x = t[x + d5];
44
- x = t[x + d4];
45
- x = t[x + d3];
46
- x = t[x + d2];
47
- x = t[x + d1];
48
- x = t[x + d0];
49
- return x / 10;
50
- }
51
-
52
- /**
53
- * Asset symbols contain the following information
54
- *
55
- * 4 bit PRECISION
56
- * 4 bit RESERVED
57
- * CHAR[6] up to 6 upper case alpha numeric ascii characters,
58
- * char = \0 null terminated
59
- *
60
- * It is treated as a uint64_t for all internal operations, but
61
- * is easily converted to something that can be displayed.
62
- *
63
- * Legacy serialization of assets
64
- * 0000pppp aaaaaaaa bbbbbbbb cccccccc dddddddd eeeeeeee ffffffff 00000000
65
- * Symbol = abcdef
66
- *
67
- * NAI serialization of assets
68
- * aaa1pppp bbbbbbbb cccccccc dddddddd
69
- * NAI = (MSB to LSB) dddddddd cccccccc bbbbbbbb aaa
70
- *
71
- * NAI internal storage of legacy assets
72
- */
73
- Types.asset = {
74
- fromByteBuffer(b) {
75
- let amount = b.readInt64();
76
- let precision = b.readUint8();
77
- let amount_string = "";
78
- let symbol = "";
79
- if (precision >= 16) {
80
- // NAI Case
81
- let b_copy = b.copy(b.offset - 1, b.offset + 3);
82
- let nai = new Buffer.from(b_copy.toBinary(), "binary").readInt32();
83
- nai = nai / 32;
84
- symbol = "@@" + nai.toString().padStart(8, '0') + damm_checksum_8digit(nai).to_String();
85
- precision = precision % 16;
86
- b.skip(3);
87
- amount_string = fromImpliedDecimal(amount, precision);
88
- } else {
89
- // Legacy Case
90
- let b_copy = b.copy(b.offset, b.offset + 7);
91
- symbol = new Buffer.from(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "");
92
- b.skip(7);
93
- // "1.000 STEEM" always written with full precision
94
- amount_string = fromImpliedDecimal(amount, precision);
95
- }
96
- return amount_string + " " + symbol;
97
- },
98
- appendByteBuffer(b, object) {
99
- let amount = "";
100
- let symbol = "";
101
- let nai = 0;
102
- let precision = 0;
103
- if (object["nai"]) {
104
- symbol = object["nai"];
105
- nai = parseInt(symbol.slice(2));
106
- let checksum = nai % 10;
107
- nai = Math.floor(nai / 10);
108
- let expected_checksum = damm_checksum_8digit(nai);
109
- switch (object["nai"]) {
110
- case "@@000000021":
111
- precision = 3;
112
- symbol = Config.get("address_prefix") == "STM" ? "STEEM" : "TESTS";
113
- break;
114
- case "@@000000013":
115
- precision = 3;
116
- symbol = Config.get("address_prefix") == "STM" ? "SBD" : "TBD";
117
- break;
118
- case "@@000000037":
119
- precision = 6;
120
- symbol = "VESTS";
121
- break;
122
- }
123
- precision = parseInt(object["precision"]);
124
- b.writeInt64(v.to_long(parseInt(object["amount"])));
125
- } else {
126
- object = object.trim();
127
- if (!/^[0-9]+\.?[0-9]* [A-Za-z0-9@]+$/.test(object)) throw new Error("Expecting amount like '99.000 SYMBOL', instead got '" + object + "'");
128
- let res = object.split(" ");
129
- amount = res[0];
130
- symbol = res[1];
131
- if (symbol.startsWith("@@")) {
132
- // NAI Case
133
- nai = parseInt(symbol.slice(2));
134
- let checksum = nai % 10;
135
- nai = Math.floor(nai / 10);
136
- let expected_checksum = damm_checksum_8digit(nai);
137
- } else if (symbol.length > 6) throw new Error("Symbols are not longer than 6 characters " + symbol + "-" + symbol.length);
138
- b.writeInt64(v.to_long(amount.replace(".", "")));
139
- let dot = amount.indexOf("."); // 0.000
140
- precision = dot === -1 ? 0 : amount.length - dot - 1;
141
- }
142
- if (symbol.startsWith("@@")) {
143
- nai = (nai << 5) + 16 + precision;
144
- b.writeUint32(nai);
145
- } else {
146
- b.writeUint8(precision);
147
- b.append(symbol.toUpperCase(), 'binary');
148
- for (let i = 0; i < 7 - symbol.length; i++) b.writeUint8(0);
149
- }
150
- return;
151
- },
152
- fromObject(object) {
153
- return object;
154
- },
155
- toObject(object, debug = {}) {
156
- if (debug.use_default && object === undefined) {
157
- return "0.000 STEEM";
158
- }
159
- return object;
160
- }
161
- };
162
- Types.asset_symbol = {
163
- fromByteBuffer(b) {
164
- let precision = b.readUint8();
165
- let amount_string = "";
166
- let nai_string = "";
167
- if (precision >= 16) {
168
- // NAI Case
169
- let b_copy = b.copy(b.offset - 1, b.offset + 3);
170
- let nai = new Buffer.from(b_copy.toBinary(), "binary").readInt32();
171
- nai = nai / 32;
172
- nai_string = "@@" + nai.toString().padStart(8, '0') + damm_checksum_8digit(nai).to_String();
173
- precision = precision % 16;
174
- b.skip(3);
175
- } else {
176
- // Legacy Case
177
- let b_copy = b.copy(b.offset, b.offset + 7);
178
- let symbol = new Buffer.from(b_copy.toBinary(), "binary").toString().replace(/\x00/g, "");
179
- if (symbol == "STEEM" || symbol == "TESTS") nai_string = "@@000000021";else if (symbol == "SBD" || symbol == "TBD") nai_string = "@@000000013";else if (symbol == "VESTS") nai_string = "@@000000037";else throw new Error("Expecting non-smt core asset symbol, instead got '" + symbol + "'");
180
- b.skip(7);
181
- }
182
- return {
183
- "nai": nai_string,
184
- "precision": precision
185
- };
186
- },
187
- appendByteBuffer(b, object) {
188
- let nai = 0;
189
- if (!object["nai"].startsWith("@@")) throw new Error("Asset Symbols NAIs must be prefixed with '@@'. Was " + object["nai"]);
190
- nai = parseInt(object["nai"].slice(2));
191
- let checksum = nai % 10;
192
- nai = Math.floor(nai / 10);
193
- let expected_checksum = damm_checksum_8digit(nai);
194
- let precision = 0;
195
- let symbol = "";
196
- switch (object["nai"]) {
197
- case "@@000000021":
198
- precision = 3;
199
- symbol = Config.get("address_prefix") == "STM" ? "STEEM" : "TESTS";
200
- break;
201
- case "@@000000013":
202
- precision = 3;
203
- symbol = Config.get("address_prefix") == "STM" ? "SBD" : "TBD";
204
- break;
205
- case "@@000000037":
206
- precision = 6;
207
- symbol = "VESTS";
208
- break;
209
- }
210
- if (precision > 0) {
211
- //Core Symbol Case
212
- b.writeUint8(precision);
213
- b.append(symbol, 'binary');
214
- for (let i = 0; i < 7 - symbol.length; i++) b.writeUint8(0);
215
- } else {
216
- nai = (nai << 5) + 16 + object["precision"];
217
- b.writeUint32(nai);
218
- }
219
- return;
220
- },
221
- fromObject(object) {
222
- return object;
223
- },
224
- toObject(object, debug = {}) {
225
- if (debug.use_default && object === undefined) {
226
- return "STEEM";
227
- }
228
- return object;
229
- }
230
- };
231
- Types.uint8 = {
232
- fromByteBuffer(b) {
233
- return b.readUint8();
234
- },
235
- appendByteBuffer(b, object) {
236
- v.require_range(0, 0xFF, object, `uint8 ${object}`);
237
- b.writeUint8(object);
238
- return;
239
- },
240
- fromObject(object) {
241
- v.require_range(0, 0xFF, object, `uint8 ${object}`);
242
- return object;
243
- },
244
- toObject(object, debug = {}) {
245
- if (debug.use_default && object === undefined) {
246
- return 0;
247
- }
248
- v.require_range(0, 0xFF, object, `uint8 ${object}`);
249
- return parseInt(object);
250
- }
251
- };
252
- Types.uint16 = {
253
- fromByteBuffer(b) {
254
- return b.readUint16();
255
- },
256
- appendByteBuffer(b, object) {
257
- v.require_range(0, 0xFFFF, object, `uint16 ${object}`);
258
- b.writeUint16(object);
259
- return;
260
- },
261
- fromObject(object) {
262
- v.require_range(0, 0xFFFF, object, `uint16 ${object}`);
263
- return object;
264
- },
265
- toObject(object, debug = {}) {
266
- if (debug.use_default && object === undefined) {
267
- return 0;
268
- }
269
- v.require_range(0, 0xFFFF, object, `uint16 ${object}`);
270
- return parseInt(object);
271
- }
272
- };
273
- Types.uint32 = {
274
- fromByteBuffer(b) {
275
- return b.readUint32();
276
- },
277
- appendByteBuffer(b, object) {
278
- v.require_range(0, 0xFFFFFFFF, object, `uint32 ${object}`);
279
- b.writeUint32(object);
280
- return;
281
- },
282
- fromObject(object) {
283
- v.require_range(0, 0xFFFFFFFF, object, `uint32 ${object}`);
284
- return object;
285
- },
286
- toObject(object, debug = {}) {
287
- if (debug.use_default && object === undefined) {
288
- return 0;
289
- }
290
- v.require_range(0, 0xFFFFFFFF, object, `uint32 ${object}`);
291
- return parseInt(object);
292
- }
293
- };
294
- var MIN_SIGNED_32 = -1 * Math.pow(2, 31);
295
- var MAX_SIGNED_32 = Math.pow(2, 31) - 1;
296
- Types.varint32 = {
297
- fromByteBuffer(b) {
298
- return b.readVarint32();
299
- },
300
- appendByteBuffer(b, object) {
301
- v.require_range(MIN_SIGNED_32, MAX_SIGNED_32, object, `uint32 ${object}`);
302
- b.writeVarint32(object);
303
- return;
304
- },
305
- fromObject(object) {
306
- v.require_range(MIN_SIGNED_32, MAX_SIGNED_32, object, `uint32 ${object}`);
307
- return object;
308
- },
309
- toObject(object, debug = {}) {
310
- if (debug.use_default && object === undefined) {
311
- return 0;
312
- }
313
- v.require_range(MIN_SIGNED_32, MAX_SIGNED_32, object, `uint32 ${object}`);
314
- return parseInt(object);
315
- }
316
- };
317
- Types.int16 = {
318
- fromByteBuffer(b) {
319
- return b.readInt16();
320
- },
321
- appendByteBuffer(b, object) {
322
- b.writeInt16(object);
323
- return;
324
- },
325
- fromObject(object) {
326
- return object;
327
- },
328
- toObject(object, debug = {}) {
329
- if (debug.use_default && object === undefined) {
330
- return 0;
331
- }
332
- return parseInt(object);
333
- }
334
- };
335
- Types.int64 = {
336
- fromByteBuffer(b) {
337
- return b.readInt64();
338
- },
339
- appendByteBuffer(b, object) {
340
- v.required(object);
341
- b.writeInt64(v.to_long(object));
342
- return;
343
- },
344
- fromObject(object) {
345
- v.required(object);
346
- return v.to_long(object);
347
- },
348
- toObject(object, debug = {}) {
349
- if (debug.use_default && object === undefined) {
350
- return "0";
351
- }
352
- v.required(object);
353
- return v.to_long(object).toString();
354
- }
355
- };
356
- Types.uint64 = {
357
- fromByteBuffer(b) {
358
- return b.readUint64();
359
- },
360
- appendByteBuffer(b, object) {
361
- b.writeUint64(v.to_long(v.unsigned(object)));
362
- return;
363
- },
364
- fromObject(object) {
365
- return v.to_long(v.unsigned(object));
366
- },
367
- toObject(object, debug = {}) {
368
- if (debug.use_default && object === undefined) {
369
- return "0";
370
- }
371
- return v.to_long(object).toString();
372
- }
373
- };
374
- Types.uint128 = {
375
- fromByteBuffer(b) {
376
- b.readBigInt64();
377
- return b.readBigInt64();
378
- },
379
- appendByteBuffer(b, object) {
380
- b.writeUint64(v.to_long(v.unsigned(0)));
381
- b.writeUint64(v.to_long(v.unsigned(object)));
382
- return;
383
- },
384
- fromObject(object) {
385
- return v.to_long(v.unsigned(object));
386
- },
387
- toObject(object, debug = {}) {
388
- if (debug.use_default && object === undefined) {
389
- return "0";
390
- }
391
- return v.to_long(object).toString();
392
- }
393
- };
394
- Types.string = {
395
- fromByteBuffer(b) {
396
- return new Buffer.from(b.readVString(), 'utf8');
397
- },
398
- appendByteBuffer(b, object) {
399
- v.required(object);
400
- b.writeVString(object.toString());
401
- return;
402
- },
403
- fromObject(object) {
404
- v.required(object);
405
- return new Buffer.from(object, 'utf8');
406
- },
407
- toObject(object, debug = {}) {
408
- if (debug.use_default && object === undefined) {
409
- return "";
410
- }
411
- return object.toString('utf8');
412
- }
413
- };
414
- Types.string_binary = {
415
- fromByteBuffer(b) {
416
- var b_copy;
417
- var len = b.readVarint32();
418
- b_copy = b.copy(b.offset, b.offset + len), b.skip(len);
419
- return new Buffer.from(b_copy.toBinary(), 'binary');
420
- },
421
- appendByteBuffer(b, object) {
422
- b.writeVarint32(object.length);
423
- b.append(object.toString('binary'), 'binary');
424
- return;
425
- },
426
- fromObject(object) {
427
- v.required(object);
428
- return new Buffer.from(object);
429
- },
430
- toObject(object, debug = {}) {
431
- if (debug.use_default && object === undefined) {
432
- return "";
433
- }
434
- return object.toString();
435
- }
436
- };
437
- Types.bytes = function (size) {
438
- return {
439
- fromByteBuffer(b) {
440
- if (size === undefined) {
441
- var b_copy;
442
- var len = b.readVarint32();
443
- b_copy = b.copy(b.offset, b.offset + len), b.skip(len);
444
- return new Buffer.from(b_copy.toBinary(), 'binary');
445
- } else {
446
- b_copy = b.copy(b.offset, b.offset + size), b.skip(size);
447
- return new Buffer.from(b_copy.toBinary(), 'binary');
448
- }
449
- },
450
- appendByteBuffer(b, object) {
451
- v.required(object);
452
- if (typeof object === "string") object = new Buffer.from(object, "hex");
453
- if (size === undefined) {
454
- b.writeVarint32(object.length);
455
- }
456
- b.append(object.toString('binary'), 'binary');
457
- return;
458
- },
459
- fromObject(object) {
460
- v.required(object);
461
- if (Buffer.isBuffer(object)) return object;
462
- return new Buffer.from(object, 'hex');
463
- },
464
- toObject(object, debug = {}) {
465
- if (debug.use_default && object === undefined) {
466
- var zeros = function (num) {
467
- return new Array(num).join("00");
468
- };
469
- return zeros(size);
470
- }
471
- v.required(object);
472
- return object.toString('hex');
473
- }
474
- };
475
- };
476
- Types.bool = {
477
- fromByteBuffer(b) {
478
- return b.readUint8() === 1;
479
- },
480
- appendByteBuffer(b, object) {
481
- // supports boolean or integer
482
- b.writeUint8(JSON.parse(object) ? 1 : 0);
483
- return;
484
- },
485
- fromObject(object) {
486
- return JSON.parse(object) ? true : false;
487
- },
488
- toObject(object, debug = {}) {
489
- if (debug.use_default && object === undefined) {
490
- return false;
491
- }
492
- return JSON.parse(object) ? true : false;
493
- }
494
- };
495
- Types.void = {
496
- fromByteBuffer(b) {
497
- throw new Error("(void) undefined type");
498
- },
499
- appendByteBuffer(b, object) {
500
- throw new Error("(void) undefined type");
501
- },
502
- fromObject(object) {
503
- throw new Error("(void) undefined type");
504
- },
505
- toObject(object, debug = {}) {
506
- if (debug.use_default && object === undefined) {
507
- return undefined;
508
- }
509
- throw new Error("(void) undefined type");
510
- }
511
- };
512
- Types.array = function (st_operation) {
513
- return {
514
- fromByteBuffer(b) {
515
- var size = b.readVarint32();
516
- if (HEX_DUMP) {
517
- console.log("varint32 size = " + size.toString(16));
518
- }
519
- var result = [];
520
- for (var i = 0; 0 < size ? i < size : i > size; 0 < size ? i++ : i++) {
521
- result.push(st_operation.fromByteBuffer(b));
522
- }
523
- return sortOperation(result, st_operation);
524
- },
525
- appendByteBuffer(b, object) {
526
- v.required(object);
527
- object = sortOperation(object, st_operation);
528
- b.writeVarint32(object.length);
529
- for (var i = 0, o; i < object.length; i++) {
530
- o = object[i];
531
- st_operation.appendByteBuffer(b, o);
532
- }
533
- },
534
- fromObject(object) {
535
- v.required(object);
536
- object = sortOperation(object, st_operation);
537
- var result = [];
538
- for (var i = 0, o; i < object.length; i++) {
539
- o = object[i];
540
- result.push(st_operation.fromObject(o));
541
- }
542
- return result;
543
- },
544
- toObject(object, debug = {}) {
545
- if (debug.use_default && object === undefined) {
546
- return [st_operation.toObject(object, debug)];
547
- }
548
- v.required(object);
549
- object = sortOperation(object, st_operation);
550
- var result = [];
551
- for (var i = 0, o; i < object.length; i++) {
552
- o = object[i];
553
- result.push(st_operation.toObject(o, debug));
554
- }
555
- return result;
556
- }
557
- };
558
- };
559
- Types.time_point_sec = {
560
- fromByteBuffer(b) {
561
- return b.readUint32();
562
- },
563
- appendByteBuffer(b, object) {
564
- if (typeof object !== "number") object = Types.time_point_sec.fromObject(object);
565
- b.writeUint32(object);
566
- return;
567
- },
568
- fromObject(object) {
569
- v.required(object);
570
- if (typeof object === "number") return object;
571
- if (object.getTime) return Math.floor(object.getTime() / 1000);
572
- if (typeof object !== "string") throw new Error("Unknown date type: " + object);
573
- if (typeof object === "string" && !/Z$/.test(object)) object = object + "Z";
574
- return Math.floor(new Date(object).getTime() / 1000);
575
- },
576
- toObject(object, debug = {}) {
577
- if (debug.use_default && object === undefined) return new Date(0).toISOString().split('.')[0];
578
- v.required(object);
579
- if (typeof object === "string") return object;
580
- if (object.getTime) return object.toISOString().split('.')[0];
581
- var int = parseInt(object);
582
- v.require_range(0, 0xFFFFFFFF, int, `uint32 ${object}`);
583
- return new Date(int * 1000).toISOString().split('.')[0];
584
- }
585
- };
586
- Types.set = function (st_operation) {
587
- return {
588
- validate(array) {
589
- var dup_map = {};
590
- for (var i = 0, o; i < array.length; i++) {
591
- o = array[i];
592
- var ref;
593
- if (ref = typeof o, ['string', 'number'].indexOf(ref) >= 0) {
594
- if (dup_map[o] !== undefined) {
595
- throw new Error("duplicate (set)");
596
- }
597
- dup_map[o] = true;
598
- }
599
- }
600
- return sortOperation(array, st_operation);
601
- },
602
- fromByteBuffer(b) {
603
- var size = b.readVarint32();
604
- if (HEX_DUMP) {
605
- console.log("varint32 size = " + size.toString(16));
606
- }
607
- return this.validate((() => {
608
- var result = [];
609
- for (var i = 0; 0 < size ? i < size : i > size; 0 < size ? i++ : i++) {
610
- result.push(st_operation.fromByteBuffer(b));
611
- }
612
- return result;
613
- })());
614
- },
615
- appendByteBuffer(b, object) {
616
- if (!object) {
617
- object = [];
618
- }
619
- b.writeVarint32(object.length);
620
- var iterable = this.validate(object);
621
- for (var i = 0, o; i < iterable.length; i++) {
622
- o = iterable[i];
623
- st_operation.appendByteBuffer(b, o);
624
- }
625
- return;
626
- },
627
- fromObject(object) {
628
- if (!object) {
629
- object = [];
630
- }
631
- return this.validate((() => {
632
- var result = [];
633
- for (var i = 0, o; i < object.length; i++) {
634
- o = object[i];
635
- result.push(st_operation.fromObject(o));
636
- }
637
- return result;
638
- })());
639
- },
640
- toObject(object, debug = {}) {
641
- if (debug.use_default && object === undefined) {
642
- return [st_operation.toObject(object, debug)];
643
- }
644
- if (!object) {
645
- object = [];
646
- }
647
- return this.validate((() => {
648
- var result = [];
649
- for (var i = 0, o; i < object.length; i++) {
650
- o = object[i];
651
- result.push(st_operation.toObject(o, debug));
652
- }
653
- return result;
654
- })());
655
- }
656
- };
657
- };
658
-
659
- // global_parameters_update_operation current_fees
660
- Types.fixed_array = function (count, st_operation) {
661
- return {
662
- fromByteBuffer: function (b) {
663
- var i, j, ref, results;
664
- results = [];
665
- for (i = j = 0, ref = count; j < ref; i = j += 1) {
666
- results.push(st_operation.fromByteBuffer(b));
667
- }
668
- return sortOperation(results, st_operation);
669
- },
670
- appendByteBuffer: function (b, object) {
671
- var i, j, ref;
672
- if (count !== 0) {
673
- v.required(object);
674
- object = sortOperation(object, st_operation);
675
- }
676
- for (i = j = 0, ref = count; j < ref; i = j += 1) {
677
- st_operation.appendByteBuffer(b, object[i]);
678
- }
679
- },
680
- fromObject: function (object) {
681
- var i, j, ref, results;
682
- if (count !== 0) {
683
- v.required(object);
684
- }
685
- results = [];
686
- for (i = j = 0, ref = count; j < ref; i = j += 1) {
687
- results.push(st_operation.fromObject(object[i]));
688
- }
689
- return results;
690
- },
691
- toObject: function (object, debug) {
692
- var i, j, k, ref, ref1, results, results1;
693
- if (debug == null) {
694
- debug = {};
695
- }
696
- if (debug.use_default && object === void 0) {
697
- results = [];
698
- for (i = j = 0, ref = count; j < ref; i = j += 1) {
699
- results.push(st_operation.toObject(void 0, debug));
700
- }
701
- return results;
702
- }
703
- if (count !== 0) {
704
- v.required(object);
705
- }
706
- results1 = [];
707
- for (i = k = 0, ref1 = count; k < ref1; i = k += 1) {
708
- results1.push(st_operation.toObject(object[i], debug));
709
- }
710
- return results1;
711
- }
712
- };
713
- };
714
-
715
- /* Supports instance numbers (11) or object types (1.2.11). Object type
716
- validation is enforced when an object type is used. */
717
- var id_type = function (reserved_spaces, object_type) {
718
- v.required(reserved_spaces, "reserved_spaces");
719
- v.required(object_type, "object_type");
720
- return {
721
- fromByteBuffer(b) {
722
- return b.readVarint32();
723
- },
724
- appendByteBuffer(b, object) {
725
- v.required(object);
726
- if (object.resolve !== undefined) {
727
- object = object.resolve;
728
- }
729
- // convert 1.2.n into just n
730
- if (/^[0-9]+\.[0-9]+\.[0-9]+$/.test(object)) {
731
- object = v.get_instance(reserved_spaces, object_type, object);
732
- }
733
- b.writeVarint32(v.to_number(object));
734
- return;
735
- },
736
- fromObject(object) {
737
- v.required(object);
738
- if (object.resolve !== undefined) {
739
- object = object.resolve;
740
- }
741
- if (v.is_digits(object)) {
742
- return v.to_number(object);
743
- }
744
- return v.get_instance(reserved_spaces, object_type, object);
745
- },
746
- toObject(object, debug = {}) {
747
- var object_type_id = chain_types.object_type[object_type];
748
- if (debug.use_default && object === undefined) {
749
- return `${reserved_spaces}.${object_type_id}.0`;
750
- }
751
- v.required(object);
752
- if (object.resolve !== undefined) {
753
- object = object.resolve;
754
- }
755
- if (/^[0-9]+\.[0-9]+\.[0-9]+$/.test(object)) {
756
- object = v.get_instance(reserved_spaces, object_type, object);
757
- }
758
- return `${reserved_spaces}.${object_type_id}.` + object;
759
- }
760
- };
761
- };
762
- Types.protocol_id_type = function (name) {
763
- v.required(name, "name");
764
- return id_type(chain_types.reserved_spaces.protocol_ids, name);
765
- };
766
- Types.object_id_type = {
767
- fromByteBuffer(b) {
768
- return ObjectId.fromByteBuffer(b);
769
- },
770
- appendByteBuffer(b, object) {
771
- v.required(object);
772
- if (object.resolve !== undefined) {
773
- object = object.resolve;
774
- }
775
- object = ObjectId.fromString(object);
776
- object.appendByteBuffer(b);
777
- return;
778
- },
779
- fromObject(object) {
780
- v.required(object);
781
- if (object.resolve !== undefined) {
782
- object = object.resolve;
783
- }
784
- return ObjectId.fromString(object);
785
- },
786
- toObject(object, debug = {}) {
787
- if (debug.use_default && object === undefined) {
788
- return "0.0.0";
789
- }
790
- v.required(object);
791
- if (object.resolve !== undefined) {
792
- object = object.resolve;
793
- }
794
- object = ObjectId.fromString(object);
795
- return object.toString();
796
- }
797
- };
798
- Types.vote_id = {
799
- TYPE: 0x000000FF,
800
- ID: 0xFFFFFF00,
801
- fromByteBuffer(b) {
802
- var value = b.readUint32();
803
- return {
804
- type: value & this.TYPE,
805
- id: value & this.ID
806
- };
807
- },
808
- appendByteBuffer(b, object) {
809
- v.required(object);
810
- if (object === "string") object = Types.vote_id.fromObject(object);
811
- var value = object.id << 8 | object.type;
812
- b.writeUint32(value);
813
- return;
814
- },
815
- fromObject(object) {
816
- v.required(object, "(type vote_id)");
817
- if (typeof object === "object") {
818
- v.required(object.type, "type");
819
- v.required(object.id, "id");
820
- return object;
821
- }
822
- v.require_test(/^[0-9]+:[0-9]+$/, object, `vote_id format ${object}`);
823
- var [type, id] = object.split(':');
824
- v.require_range(0, 0xff, type, `vote type ${object}`);
825
- v.require_range(0, 0xffffff, id, `vote id ${object}`);
826
- return {
827
- type,
828
- id
829
- };
830
- },
831
- toObject(object, debug = {}) {
832
- if (debug.use_default && object === undefined) {
833
- return "0:0";
834
- }
835
- v.required(object);
836
- if (typeof object === "string") object = Types.vote_id.fromObject(object);
837
- return object.type + ":" + object.id;
838
- },
839
- compare(a, b) {
840
- if (typeof a !== "object") a = Types.vote_id.fromObject(a);
841
- if (typeof b !== "object") b = Types.vote_id.fromObject(b);
842
- return parseInt(a.id) - parseInt(b.id);
843
- }
844
- };
845
- Types.optional = function (st_operation) {
846
- v.required(st_operation, "st_operation");
847
- return {
848
- fromByteBuffer(b) {
849
- if (!(b.readUint8() === 1)) {
850
- return undefined;
851
- }
852
- return st_operation.fromByteBuffer(b);
853
- },
854
- appendByteBuffer(b, object) {
855
- if (object !== null && object !== undefined) {
856
- b.writeUint8(1);
857
- st_operation.appendByteBuffer(b, object);
858
- } else {
859
- b.writeUint8(0);
860
- }
861
- return;
862
- },
863
- fromObject(object) {
864
- if (object === undefined) {
865
- return undefined;
866
- }
867
- return st_operation.fromObject(object);
868
- },
869
- toObject(object, debug = {}) {
870
- // toObject is only null save if use_default is true
871
- var result_object = (() => {
872
- if (!debug.use_default && object === undefined) {
873
- return undefined;
874
- } else {
875
- return st_operation.toObject(object, debug);
876
- }
877
- })();
878
- if (debug.annotate) {
879
- if (typeof result_object === "object") {
880
- result_object.__optional = "parent is optional";
881
- } else {
882
- result_object = {
883
- __optional: result_object
884
- };
885
- }
886
- }
887
- return result_object;
888
- }
889
- };
890
- };
891
- Types.static_variant = function (_st_operations) {
892
- return {
893
- nosort: true,
894
- st_operations: _st_operations,
895
- opTypeId(value) {
896
- let pos = 0,
897
- type_id;
898
- if (typeof value === "number") type_id = value;else {
899
- for (let op of this.st_operations) {
900
- if (op.operation_name === value) {
901
- type_id = pos;
902
- break;
903
- }
904
- pos++;
905
- }
906
- }
907
- return type_id;
908
- },
909
- fromByteBuffer(b) {
910
- var type_id = b.readVarint32();
911
- var st_operation = this.st_operations[type_id];
912
- if (HEX_DUMP) {
913
- console.error(`static_variant id 0x${type_id.toString(16)} (${type_id})`);
914
- }
915
- v.required(st_operation, `operation ${type_id}`);
916
- return [type_id, st_operation.fromByteBuffer(b)];
917
- },
918
- appendByteBuffer(b, object) {
919
- v.required(object);
920
- var type_id = this.opTypeId(object[0]);
921
- var st_operation = this.st_operations[type_id];
922
- v.required(st_operation, `operation ${type_id}`);
923
- b.writeVarint32(type_id);
924
- st_operation.appendByteBuffer(b, object[1]);
925
- return;
926
- },
927
- fromObject(object) {
928
- v.required(object);
929
- let type_id = this.opTypeId(object[0]);
930
- var st_operation = this.st_operations[type_id];
931
- v.required(st_operation, `operation ${type_id}`);
932
- return [type_id, st_operation.fromObject(object[1])];
933
- },
934
- toObject(object, debug = {}) {
935
- if (debug.use_default && object === undefined) {
936
- return [this.st_operations[0].operation_name, this.st_operations[0].toObject(undefined, debug)];
937
- }
938
- v.required(object);
939
- let type_id = this.opTypeId(object[0]);
940
- var st_operation = this.st_operations[type_id];
941
- v.required(st_operation, `operation ${type_id}`);
942
- return [st_operation.operation_name, st_operation.toObject(object[1], debug)];
943
- },
944
- compare(a, b) {
945
- return strCmp(this.opTypeId(a[0]), this.opTypeId(b[0]));
946
- }
947
- };
948
- };
949
- Types.map = function (key_st_operation, value_st_operation) {
950
- return {
951
- validate(array) {
952
- if (!Array.isArray(array)) {
953
- throw new Error("expecting array");
954
- }
955
- var dup_map = {};
956
- for (var i = 0, o; i < array.length; i++) {
957
- o = array[i];
958
- var ref;
959
- if (!(o.length === 2)) {
960
- throw new Error("expecting two elements");
961
- }
962
- if (ref = typeof o[0], ['number', 'string'].indexOf(ref) >= 0) {
963
- if (dup_map[o[0]] !== undefined) {
964
- throw new Error("duplicate (map)");
965
- }
966
- dup_map[o[0]] = true;
967
- }
968
- }
969
- return sortOperation(array, key_st_operation);
970
- },
971
- fromByteBuffer(b) {
972
- var result = [];
973
- var end = b.readVarint32();
974
- for (var i = 0; 0 < end ? i < end : i > end; 0 < end ? i++ : i++) {
975
- result.push([key_st_operation.fromByteBuffer(b), value_st_operation.fromByteBuffer(b)]);
976
- }
977
- return this.validate(result);
978
- },
979
- appendByteBuffer(b, object) {
980
- this.validate(object);
981
- b.writeVarint32(object.length);
982
- for (var i = 0, o; i < object.length; i++) {
983
- o = object[i];
984
- key_st_operation.appendByteBuffer(b, o[0]);
985
- value_st_operation.appendByteBuffer(b, o[1]);
986
- }
987
- return;
988
- },
989
- fromObject(object) {
990
- v.required(object);
991
- var result = [];
992
- for (var i = 0, o; i < object.length; i++) {
993
- o = object[i];
994
- result.push([key_st_operation.fromObject(o[0]), value_st_operation.fromObject(o[1])]);
995
- }
996
- return this.validate(result);
997
- },
998
- toObject(object, debug = {}) {
999
- if (debug.use_default && object === undefined) {
1000
- return [[key_st_operation.toObject(undefined, debug), value_st_operation.toObject(undefined, debug)]];
1001
- }
1002
- v.required(object);
1003
- object = this.validate(object);
1004
- var result = [];
1005
- for (var i = 0, o; i < object.length; i++) {
1006
- o = object[i];
1007
- result.push([key_st_operation.toObject(o[0], debug), value_st_operation.toObject(o[1], debug)]);
1008
- }
1009
- return result;
1010
- }
1011
- };
1012
- };
1013
- Types.public_key = {
1014
- toPublic(object) {
1015
- if (object.resolve !== undefined) {
1016
- object = object.resolve;
1017
- }
1018
- return object == null ? object : object.Q ? object : PublicKey.fromStringOrThrow(object);
1019
- },
1020
- fromByteBuffer(b) {
1021
- return fp.public_key(b);
1022
- },
1023
- appendByteBuffer(b, object) {
1024
- v.required(object);
1025
- fp.public_key(b, Types.public_key.toPublic(object));
1026
- return;
1027
- },
1028
- fromObject(object) {
1029
- v.required(object);
1030
- if (object.Q) {
1031
- return object;
1032
- }
1033
- return Types.public_key.toPublic(object);
1034
- },
1035
- toObject(object, debug = {}) {
1036
- if (debug.use_default && object === undefined) {
1037
- return ecc_config.get('address_prefix') + "859gxfnXyUriMgUeThh1fWv3oqcpLFyHa3TfFYC4PK2HqhToVM";
1038
- }
1039
- v.required(object);
1040
- return object.toString();
1041
- },
1042
- compare(a, b) {
1043
- // sort ascending
1044
- return 1 * strCmp(a.toString(), b.toString());
1045
- }
1046
- };
1047
- Types.address = {
1048
- _to_address(object) {
1049
- v.required(object);
1050
- if (object.addy) {
1051
- return object;
1052
- }
1053
- return Address.fromString(object);
1054
- },
1055
- fromByteBuffer(b) {
1056
- return new Address(fp.ripemd160(b));
1057
- },
1058
- appendByteBuffer(b, object) {
1059
- fp.ripemd160(b, Types.address._to_address(object).toBuffer());
1060
- return;
1061
- },
1062
- fromObject(object) {
1063
- return Types.address._to_address(object);
1064
- },
1065
- toObject(object, debug = {}) {
1066
- if (debug.use_default && object === undefined) {
1067
- return ecc_config.get('address_prefix') + "664KmHxSuQyDsfwo4WEJvWpzg1QKdg67S";
1068
- }
1069
- return Types.address._to_address(object).toString();
1070
- },
1071
- compare(a, b) {
1072
- // sort decending
1073
- return -1 * strCmp(a.toString(), b.toString());
1074
- }
1075
- };
1076
- let strCmp = (a, b) => a > b ? 1 : a < b ? -1 : 0;
1077
- let firstEl = el => Array.isArray(el) ? el[0] : el;
1078
- let sortOperation = (array, st_operation) => {
1079
- // console.log('operation.nosort', st_operation.nosort)
1080
- return st_operation.nosort ? array : st_operation.compare ? array.sort((a, b) => st_operation.compare(firstEl(a), firstEl(b))) :
1081
- // custom compare operation
1082
- array.sort((a, b) => typeof firstEl(a) === "number" && typeof firstEl(b) === "number" ? firstEl(a) - firstEl(b) :
1083
- // A binary string compare does not work. Performanance is very good so HEX is used.. localeCompare is another option.
1084
- Buffer.isBuffer(firstEl(a)) && Buffer.isBuffer(firstEl(b)) ? strCmp(firstEl(a).toString("hex"), firstEl(b).toString("hex")) : strCmp(firstEl(a).toString(), firstEl(b).toString()));
1085
- };