Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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