選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bytes.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BytesLookup = exports.Bytes = void 0;
  4. const buffer_1 = require("buffer/");
  5. /*
  6. * @brief: Bytes, name, and ordinal representing one type, ledger_type, transaction type, or result
  7. */
  8. class Bytes {
  9. constructor(name, ordinal, ordinalWidth) {
  10. this.name = name;
  11. this.ordinal = ordinal;
  12. this.ordinalWidth = ordinalWidth;
  13. this.bytes = buffer_1.Buffer.alloc(ordinalWidth);
  14. for (let i = 0; i < ordinalWidth; i++) {
  15. this.bytes[ordinalWidth - i - 1] = (ordinal >>> (i * 8)) & 0xff;
  16. }
  17. }
  18. toJSON() {
  19. return this.name;
  20. }
  21. toBytesSink(sink) {
  22. sink.put(this.bytes);
  23. }
  24. toBytes() {
  25. return this.bytes;
  26. }
  27. }
  28. exports.Bytes = Bytes;
  29. /*
  30. * @brief: Collection of Bytes objects, mapping bidirectionally
  31. */
  32. class BytesLookup {
  33. constructor(types, ordinalWidth) {
  34. this.ordinalWidth = ordinalWidth;
  35. Object.entries(types).forEach(([k, v]) => {
  36. this.add(k, v);
  37. });
  38. }
  39. /**
  40. * Add a new name value pair to the BytesLookup.
  41. *
  42. * @param name - A human readable name for the field.
  43. * @param value - The numeric value for the field.
  44. * @throws if the name or value already exist in the lookup because it's unclear how to decode.
  45. */
  46. add(name, value) {
  47. if (this[name]) {
  48. throw new SyntaxError(`Attempted to add a value with a duplicate name "${name}". This is not allowed because it is unclear how to decode.`);
  49. }
  50. if (this[value.toString()]) {
  51. throw new SyntaxError(`Attempted to add a duplicate value under a different name (Given name: "${name}" and previous name: "${this[value.toString()]}. This is not allowed because it is unclear how to decode.\nGiven value: ${value.toString()}`);
  52. }
  53. this[name] = new Bytes(name, value, this.ordinalWidth);
  54. this[value.toString()] = this[name];
  55. }
  56. from(value) {
  57. return value instanceof Bytes ? value : this[value];
  58. }
  59. fromParser(parser) {
  60. return this.from(parser.readUIntN(this.ordinalWidth).toString());
  61. }
  62. }
  63. exports.BytesLookup = BytesLookup;
  64. //# sourceMappingURL=bytes.js.map