binary.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. var types_1 = require("./types");
  6. var binary_parser_1 = require("./serdes/binary-parser");
  7. Object.defineProperty(exports, "BinaryParser", { enumerable: true, get: function () { return binary_parser_1.BinaryParser; } });
  8. var hash_prefixes_1 = require("./hash-prefixes");
  9. var 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. var 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. var 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. var makeParser = function (bytes) { return 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. var readJSON = function (parser) {
  31. return parser.readType(types_1.coreTypes.STObject).toJSON();
  32. };
  33. exports.readJSON = readJSON;
  34. /**
  35. * Parse a hex-string into its JSON interpretation
  36. *
  37. * @param bytes hex-string to parse into JSON
  38. * @returns JSON
  39. */
  40. var binaryToJSON = function (bytes) { return readJSON(makeParser(bytes)); };
  41. exports.binaryToJSON = binaryToJSON;
  42. /**
  43. * Function to serialize JSON object representing a transaction
  44. *
  45. * @param object JSON object to serialize
  46. * @param opts options for serializing, including optional prefix, suffix, and signingFieldOnly
  47. * @returns A Buffer containing the serialized object
  48. */
  49. function serializeObject(object, opts) {
  50. if (opts === void 0) { opts = {}; }
  51. var prefix = opts.prefix, suffix = opts.suffix, _a = opts.signingFieldsOnly, signingFieldsOnly = _a === void 0 ? false : _a;
  52. var bytesList = new binary_serializer_1.BytesList();
  53. if (prefix) {
  54. bytesList.put(prefix);
  55. }
  56. var filter = signingFieldsOnly
  57. ? function (f) { return f.isSigningField; }
  58. : undefined;
  59. types_1.coreTypes.STObject.from(object, filter).toBytesSink(bytesList);
  60. if (suffix) {
  61. bytesList.put(suffix);
  62. }
  63. return bytesList.toBytes();
  64. }
  65. exports.serializeObject = serializeObject;
  66. /**
  67. * Serialize an object for signing
  68. *
  69. * @param transaction Transaction to serialize
  70. * @param prefix Prefix bytes to put before the serialized object
  71. * @returns A Buffer with the serialized object
  72. */
  73. function signingData(transaction, prefix) {
  74. if (prefix === void 0) { prefix = hash_prefixes_1.HashPrefix.transactionSig; }
  75. return serializeObject(transaction, { prefix: prefix, signingFieldsOnly: true });
  76. }
  77. exports.signingData = signingData;
  78. /**
  79. * Serialize a signingClaim
  80. *
  81. * @param claim A claim object to serialize
  82. * @returns the serialized object with appropriate prefix
  83. */
  84. function signingClaimData(claim) {
  85. var num = bigInt(String(claim.amount));
  86. var prefix = hash_prefixes_1.HashPrefix.paymentChannelClaim;
  87. var channel = types_1.coreTypes.Hash256.from(claim.channel).toBytes();
  88. var amount = types_1.coreTypes.UInt64.from(num).toBytes();
  89. var bytesList = new binary_serializer_1.BytesList();
  90. bytesList.put(prefix);
  91. bytesList.put(channel);
  92. bytesList.put(amount);
  93. return bytesList.toBytes();
  94. }
  95. exports.signingClaimData = signingClaimData;
  96. /**
  97. * Serialize a transaction object for multiSigning
  98. *
  99. * @param transaction transaction to serialize
  100. * @param signingAccount Account to sign the transaction with
  101. * @returns serialized transaction with appropriate prefix and suffix
  102. */
  103. function multiSigningData(transaction, signingAccount) {
  104. var prefix = hash_prefixes_1.HashPrefix.transactionMultiSig;
  105. var suffix = types_1.coreTypes.AccountID.from(signingAccount).toBytes();
  106. return serializeObject(transaction, {
  107. prefix: prefix,
  108. suffix: suffix,
  109. signingFieldsOnly: true,
  110. });
  111. }
  112. exports.multiSigningData = multiSigningData;
  113. //# sourceMappingURL=binary.js.map