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

binary-serializer.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.BinarySerializer = exports.BytesList = void 0;
  27. const assert = __importStar(require("assert"));
  28. const buffer_1 = require("buffer/");
  29. /**
  30. * Bytes list is a collection of buffer objects
  31. */
  32. class BytesList {
  33. constructor() {
  34. this.bytesArray = [];
  35. }
  36. /**
  37. * Get the total number of bytes in the BytesList
  38. *
  39. * @return the number of bytes
  40. */
  41. getLength() {
  42. return buffer_1.Buffer.concat(this.bytesArray).byteLength;
  43. }
  44. /**
  45. * Put bytes in the BytesList
  46. *
  47. * @param bytesArg A Buffer
  48. * @return this BytesList
  49. */
  50. put(bytesArg) {
  51. const bytes = buffer_1.Buffer.from(bytesArg); // Temporary, to catch instances of Uint8Array being passed in
  52. this.bytesArray.push(bytes);
  53. return this;
  54. }
  55. /**
  56. * Write this BytesList to the back of another bytes list
  57. *
  58. * @param list The BytesList to write to
  59. */
  60. toBytesSink(list) {
  61. list.put(this.toBytes());
  62. }
  63. toBytes() {
  64. return buffer_1.Buffer.concat(this.bytesArray);
  65. }
  66. toHex() {
  67. return this.toBytes().toString('hex').toUpperCase();
  68. }
  69. }
  70. exports.BytesList = BytesList;
  71. /**
  72. * BinarySerializer is used to write fields and values to buffers
  73. */
  74. class BinarySerializer {
  75. constructor(sink) {
  76. this.sink = new BytesList();
  77. this.sink = sink;
  78. }
  79. /**
  80. * Write a value to this BinarySerializer
  81. *
  82. * @param value a SerializedType value
  83. */
  84. write(value) {
  85. value.toBytesSink(this.sink);
  86. }
  87. /**
  88. * Write bytes to this BinarySerializer
  89. *
  90. * @param bytes the bytes to write
  91. */
  92. put(bytes) {
  93. this.sink.put(bytes);
  94. }
  95. /**
  96. * Write a value of a given type to this BinarySerializer
  97. *
  98. * @param type the type to write
  99. * @param value a value of that type
  100. */
  101. writeType(type, value) {
  102. this.write(type.from(value));
  103. }
  104. /**
  105. * Write BytesList to this BinarySerializer
  106. *
  107. * @param bl BytesList to write to BinarySerializer
  108. */
  109. writeBytesList(bl) {
  110. bl.toBytesSink(this.sink);
  111. }
  112. /**
  113. * Calculate the header of Variable Length encoded bytes
  114. *
  115. * @param length the length of the bytes
  116. */
  117. encodeVariableLength(length) {
  118. const lenBytes = buffer_1.Buffer.alloc(3);
  119. if (length <= 192) {
  120. lenBytes[0] = length;
  121. return lenBytes.slice(0, 1);
  122. }
  123. else if (length <= 12480) {
  124. length -= 193;
  125. lenBytes[0] = 193 + (length >>> 8);
  126. lenBytes[1] = length & 0xff;
  127. return lenBytes.slice(0, 2);
  128. }
  129. else if (length <= 918744) {
  130. length -= 12481;
  131. lenBytes[0] = 241 + (length >>> 16);
  132. lenBytes[1] = (length >> 8) & 0xff;
  133. lenBytes[2] = length & 0xff;
  134. return lenBytes.slice(0, 3);
  135. }
  136. throw new Error('Overflow error');
  137. }
  138. /**
  139. * Write field and value to BinarySerializer
  140. *
  141. * @param field field to write to BinarySerializer
  142. * @param value value to write to BinarySerializer
  143. */
  144. writeFieldAndValue(field, value, isUnlModifyWorkaround = false) {
  145. const associatedValue = field.associatedType.from(value);
  146. assert.ok(associatedValue.toBytesSink !== undefined);
  147. assert.ok(field.name !== undefined);
  148. this.sink.put(field.header);
  149. if (field.isVariableLengthEncoded) {
  150. this.writeLengthEncoded(associatedValue, isUnlModifyWorkaround);
  151. }
  152. else {
  153. associatedValue.toBytesSink(this.sink);
  154. }
  155. }
  156. /**
  157. * Write a variable length encoded value to the BinarySerializer
  158. *
  159. * @param value length encoded value to write to BytesList
  160. */
  161. writeLengthEncoded(value, isUnlModifyWorkaround = false) {
  162. const bytes = new BytesList();
  163. if (!isUnlModifyWorkaround) {
  164. // this part doesn't happen for the Account field in a UNLModify transaction
  165. value.toBytesSink(bytes);
  166. }
  167. this.put(this.encodeVariableLength(bytes.getLength()));
  168. this.writeBytesList(bytes);
  169. }
  170. }
  171. exports.BinarySerializer = BinarySerializer;
  172. //# sourceMappingURL=binary-serializer.js.map