index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.TransactionType = exports.TransactionResult = exports.LedgerEntryType = exports.Type = exports.Field = exports.Bytes = exports.TRANSACTION_TYPES = void 0;
  4. var enums = require("./definitions.json");
  5. var serialized_type_1 = require("../types/serialized-type");
  6. var buffer_1 = require("buffer/");
  7. /*
  8. * @brief: All valid transaction types
  9. */
  10. exports.TRANSACTION_TYPES = Object.entries(enums.TRANSACTION_TYPES)
  11. .filter(function (_a) {
  12. var _key = _a[0], value = _a[1];
  13. return value >= 0;
  14. })
  15. .map(function (_a) {
  16. var key = _a[0], _value = _a[1];
  17. return key;
  18. });
  19. var TYPE_WIDTH = 2;
  20. var LEDGER_ENTRY_WIDTH = 2;
  21. var TRANSACTION_TYPE_WIDTH = 2;
  22. var TRANSACTION_RESULT_WIDTH = 1;
  23. /*
  24. * @brief: Serialize a field based on type_code and Field.nth
  25. */
  26. function fieldHeader(type, nth) {
  27. var header = [];
  28. if (type < 16) {
  29. if (nth < 16) {
  30. header.push((type << 4) | nth);
  31. }
  32. else {
  33. header.push(type << 4, nth);
  34. }
  35. }
  36. else if (nth < 16) {
  37. header.push(nth, type);
  38. }
  39. else {
  40. header.push(0, type, nth);
  41. }
  42. return buffer_1.Buffer.from(header);
  43. }
  44. /*
  45. * @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
  46. */
  47. var Bytes = /** @class */ (function () {
  48. function Bytes(name, ordinal, ordinalWidth) {
  49. this.name = name;
  50. this.ordinal = ordinal;
  51. this.ordinalWidth = ordinalWidth;
  52. this.bytes = buffer_1.Buffer.alloc(ordinalWidth);
  53. for (var i = 0; i < ordinalWidth; i++) {
  54. this.bytes[ordinalWidth - i - 1] = (ordinal >>> (i * 8)) & 0xff;
  55. }
  56. }
  57. Bytes.prototype.toJSON = function () {
  58. return this.name;
  59. };
  60. Bytes.prototype.toBytesSink = function (sink) {
  61. sink.put(this.bytes);
  62. };
  63. Bytes.prototype.toBytes = function () {
  64. return this.bytes;
  65. };
  66. return Bytes;
  67. }());
  68. exports.Bytes = Bytes;
  69. /*
  70. * @brief: Collection of Bytes objects, mapping bidirectionally
  71. */
  72. var BytesLookup = /** @class */ (function () {
  73. function BytesLookup(types, ordinalWidth) {
  74. var _this = this;
  75. this.ordinalWidth = ordinalWidth;
  76. Object.entries(types).forEach(function (_a) {
  77. var k = _a[0], v = _a[1];
  78. _this[k] = new Bytes(k, v, ordinalWidth);
  79. _this[v.toString()] = _this[k];
  80. });
  81. }
  82. BytesLookup.prototype.from = function (value) {
  83. return value instanceof Bytes ? value : this[value];
  84. };
  85. BytesLookup.prototype.fromParser = function (parser) {
  86. return this.from(parser.readUIntN(this.ordinalWidth).toString());
  87. };
  88. return BytesLookup;
  89. }());
  90. function buildField(_a) {
  91. var name = _a[0], info = _a[1];
  92. var typeOrdinal = enums.TYPES[info.type];
  93. var field = fieldHeader(typeOrdinal, info.nth);
  94. return {
  95. name: name,
  96. nth: info.nth,
  97. isVariableLengthEncoded: info.isVLEncoded,
  98. isSerialized: info.isSerialized,
  99. isSigningField: info.isSigningField,
  100. ordinal: (typeOrdinal << 16) | info.nth,
  101. type: new Bytes(info.type, typeOrdinal, TYPE_WIDTH),
  102. header: field,
  103. associatedType: serialized_type_1.SerializedType, // For later assignment in ./types/index.js
  104. };
  105. }
  106. /*
  107. * @brief: The collection of all fields as defined in definitions.json
  108. */
  109. var FieldLookup = /** @class */ (function () {
  110. function FieldLookup(fields) {
  111. var _this = this;
  112. fields.forEach(function (_a) {
  113. var k = _a[0], v = _a[1];
  114. _this[k] = buildField([k, v]);
  115. _this[_this[k].ordinal.toString()] = _this[k];
  116. });
  117. }
  118. FieldLookup.prototype.fromString = function (value) {
  119. return this[value];
  120. };
  121. return FieldLookup;
  122. }());
  123. var Type = new BytesLookup(enums.TYPES, TYPE_WIDTH);
  124. exports.Type = Type;
  125. var LedgerEntryType = new BytesLookup(enums.LEDGER_ENTRY_TYPES, LEDGER_ENTRY_WIDTH);
  126. exports.LedgerEntryType = LedgerEntryType;
  127. var TransactionType = new BytesLookup(enums.TRANSACTION_TYPES, TRANSACTION_TYPE_WIDTH);
  128. exports.TransactionType = TransactionType;
  129. var TransactionResult = new BytesLookup(enums.TRANSACTION_RESULTS, TRANSACTION_RESULT_WIDTH);
  130. exports.TransactionResult = TransactionResult;
  131. var Field = new FieldLookup(enums.FIELDS);
  132. exports.Field = Field;
  133. //# sourceMappingURL=index.js.map