You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binary.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. /* eslint-disable func-style */
  3. Object.defineProperty(exports, "__esModule", { value: true });
  4. exports.transactionID = exports.sha512Half = exports.binaryToJSON = exports.signingClaimData = exports.signingData = exports.multiSigningData = exports.readJSON = exports.serializeObject = exports.makeParser = exports.BytesList = exports.BinarySerializer = exports.BinaryParser = void 0;
  5. const types_1 = require("./types");
  6. const binary_parser_1 = require("./serdes/binary-parser");
  7. Object.defineProperty(exports, "BinaryParser", { enumerable: true, get: function () { return binary_parser_1.BinaryParser; } });
  8. const hash_prefixes_1 = require("./hash-prefixes");
  9. const binary_serializer_1 = require("./serdes/binary-serializer");
  10. Object.defineProperty(exports, "BinarySerializer", { enumerable: true, get: function () { return binary_serializer_1.BinarySerializer; } });
  11. Object.defineProperty(exports, "BytesList", { enumerable: true, get: function () { return binary_serializer_1.BytesList; } });
  12. const hashes_1 = require("./hashes");
  13. Object.defineProperty(exports, "sha512Half", { enumerable: true, get: function () { return hashes_1.sha512Half; } });
  14. Object.defineProperty(exports, "transactionID", { enumerable: true, get: function () { return hashes_1.transactionID; } });
  15. const bigInt = require("big-integer");
  16. /**
  17. * Construct a BinaryParser
  18. *
  19. * @param bytes hex-string to construct BinaryParser from
  20. * @returns A BinaryParser
  21. */
  22. const makeParser = (bytes) => new binary_parser_1.BinaryParser(bytes);
  23. exports.makeParser = makeParser;
  24. /**
  25. * Parse BinaryParser into JSON
  26. *
  27. * @param parser BinaryParser object
  28. * @returns JSON for the bytes in the BinaryParser
  29. */
  30. const readJSON = (parser) => parser.readType(types_1.coreTypes.STObject).toJSON();
  31. exports.readJSON = readJSON;
  32. /**
  33. * Parse a hex-string into its JSON interpretation
  34. *
  35. * @param bytes hex-string to parse into JSON
  36. * @returns JSON
  37. */
  38. const binaryToJSON = (bytes) => readJSON(makeParser(bytes));
  39. exports.binaryToJSON = binaryToJSON;
  40. /**
  41. * Function to serialize JSON object representing a transaction
  42. *
  43. * @param object JSON object to serialize
  44. * @param opts options for serializing, including optional prefix, suffix, and signingFieldOnly
  45. * @returns A Buffer containing the serialized object
  46. */
  47. function serializeObject(object, opts = {}) {
  48. const { prefix, suffix, signingFieldsOnly = false } = opts;
  49. const bytesList = new binary_serializer_1.BytesList();
  50. if (prefix) {
  51. bytesList.put(prefix);
  52. }
  53. const filter = signingFieldsOnly
  54. ? (f) => f.isSigningField
  55. : undefined;
  56. types_1.coreTypes.STObject.from(object, filter).toBytesSink(bytesList);
  57. if (suffix) {
  58. bytesList.put(suffix);
  59. }
  60. return bytesList.toBytes();
  61. }
  62. exports.serializeObject = serializeObject;
  63. /**
  64. * Serialize an object for signing
  65. *
  66. * @param transaction Transaction to serialize
  67. * @param prefix Prefix bytes to put before the serialized object
  68. * @returns A Buffer with the serialized object
  69. */
  70. function signingData(transaction, prefix = hash_prefixes_1.HashPrefix.transactionSig) {
  71. return serializeObject(transaction, { prefix, signingFieldsOnly: true });
  72. }
  73. exports.signingData = signingData;
  74. /**
  75. * Serialize a signingClaim
  76. *
  77. * @param claim A claim object to serialize
  78. * @returns the serialized object with appropriate prefix
  79. */
  80. function signingClaimData(claim) {
  81. const num = bigInt(String(claim.amount));
  82. const prefix = hash_prefixes_1.HashPrefix.paymentChannelClaim;
  83. const channel = types_1.coreTypes.Hash256.from(claim.channel).toBytes();
  84. const amount = types_1.coreTypes.UInt64.from(num).toBytes();
  85. const bytesList = new binary_serializer_1.BytesList();
  86. bytesList.put(prefix);
  87. bytesList.put(channel);
  88. bytesList.put(amount);
  89. return bytesList.toBytes();
  90. }
  91. exports.signingClaimData = signingClaimData;
  92. /**
  93. * Serialize a transaction object for multiSigning
  94. *
  95. * @param transaction transaction to serialize
  96. * @param signingAccount Account to sign the transaction with
  97. * @returns serialized transaction with appropriate prefix and suffix
  98. */
  99. function multiSigningData(transaction, signingAccount) {
  100. const prefix = hash_prefixes_1.HashPrefix.transactionMultiSig;
  101. const suffix = types_1.coreTypes.AccountID.from(signingAccount).toBytes();
  102. return serializeObject(transaction, {
  103. prefix,
  104. suffix,
  105. signingFieldsOnly: true,
  106. });
  107. }
  108. exports.multiSigningData = multiSigningData;
  109. //# sourceMappingURL=binary.js.map