您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

field.js 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FieldLookup = void 0;
  4. const bytes_1 = require("./bytes");
  5. const serialized_type_1 = require("../types/serialized-type");
  6. const constants_1 = require("./constants");
  7. const buffer_1 = require("buffer/");
  8. /*
  9. * @brief: Serialize a field based on type_code and Field.nth
  10. */
  11. function fieldHeader(type, nth) {
  12. const header = [];
  13. if (type < 16) {
  14. if (nth < 16) {
  15. header.push((type << 4) | nth);
  16. }
  17. else {
  18. header.push(type << 4, nth);
  19. }
  20. }
  21. else if (nth < 16) {
  22. header.push(nth, type);
  23. }
  24. else {
  25. header.push(0, type, nth);
  26. }
  27. return buffer_1.Buffer.from(header);
  28. }
  29. function buildField([name, info], typeOrdinal) {
  30. const field = fieldHeader(typeOrdinal, info.nth);
  31. return {
  32. name: name,
  33. nth: info.nth,
  34. isVariableLengthEncoded: info.isVLEncoded,
  35. isSerialized: info.isSerialized,
  36. isSigningField: info.isSigningField,
  37. ordinal: (typeOrdinal << 16) | info.nth,
  38. type: new bytes_1.Bytes(info.type, typeOrdinal, constants_1.TYPE_WIDTH),
  39. header: field,
  40. associatedType: serialized_type_1.SerializedType, // For later assignment in ./types/index.js or Definitions.updateAll(...)
  41. };
  42. }
  43. /*
  44. * @brief: The collection of all fields as defined in definitions.json
  45. */
  46. class FieldLookup {
  47. constructor(fields, types) {
  48. fields.forEach(([name, field_info]) => {
  49. const typeOrdinal = types[field_info.type];
  50. this[name] = buildField([name, field_info], typeOrdinal);
  51. this[this[name].ordinal.toString()] = this[name];
  52. });
  53. }
  54. fromString(value) {
  55. return this[value];
  56. }
  57. }
  58. exports.FieldLookup = FieldLookup;
  59. //# sourceMappingURL=field.js.map