UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2/* eslint-disable func-style */
3Object.defineProperty(exports, "__esModule", { value: true });
4exports.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;
5const utils_1 = require("@xrplf/isomorphic/utils");
6const types_1 = require("./types");
7const binary_parser_1 = require("./serdes/binary-parser");
8Object.defineProperty(exports, "BinaryParser", { enumerable: true, get: function () { return binary_parser_1.BinaryParser; } });
9const hash_prefixes_1 = require("./hash-prefixes");
10const binary_serializer_1 = require("./serdes/binary-serializer");
11Object.defineProperty(exports, "BinarySerializer", { enumerable: true, get: function () { return binary_serializer_1.BinarySerializer; } });
12Object.defineProperty(exports, "BytesList", { enumerable: true, get: function () { return binary_serializer_1.BytesList; } });
13const hashes_1 = require("./hashes");
14Object.defineProperty(exports, "sha512Half", { enumerable: true, get: function () { return hashes_1.sha512Half; } });
15Object.defineProperty(exports, "transactionID", { enumerable: true, get: function () { return hashes_1.transactionID; } });
16const enums_1 = require("./enums");
17/**
18 * Construct a BinaryParser
19 *
20 * @param bytes hex-string or Uint8Array to construct BinaryParser from
21 * @param definitions rippled definitions used to parse the values of transaction types and such.
22 * Can be customized for sidechains and amendments.
23 * @returns BinaryParser
24 */
25const makeParser = (bytes, definitions) => new binary_parser_1.BinaryParser(bytes instanceof Uint8Array ? (0, utils_1.bytesToHex)(bytes) : bytes, definitions);
26exports.makeParser = makeParser;
27/**
28 * Parse BinaryParser into JSON
29 *
30 * @param parser BinaryParser object
31 * @param definitions rippled definitions used to parse the values of transaction types and such.
32 * Can be customized for sidechains and amendments.
33 * @returns JSON for the bytes in the BinaryParser
34 */
35const readJSON = (parser, definitions = enums_1.DEFAULT_DEFINITIONS) => parser.readType(types_1.coreTypes.STObject).toJSON(definitions);
36exports.readJSON = readJSON;
37/**
38 * Parse a hex-string into its JSON interpretation
39 *
40 * @param bytes hex-string to parse into JSON
41 * @param definitions rippled definitions used to parse the values of transaction types and such.
42 * Can be customized for sidechains and amendments.
43 * @returns JSON
44 */
45const binaryToJSON = (bytes, definitions) => readJSON(makeParser(bytes, definitions), definitions);
46exports.binaryToJSON = binaryToJSON;
47/**
48 * Function to serialize JSON object representing a transaction
49 *
50 * @param object JSON object to serialize
51 * @param opts options for serializing, including optional prefix, suffix, signingFieldOnly, and definitions
52 * @returns A Uint8Array containing the serialized object
53 */
54function serializeObject(object, opts = {}) {
55 const { prefix, suffix, signingFieldsOnly = false, definitions } = opts;
56 const bytesList = new binary_serializer_1.BytesList();
57 if (prefix) {
58 bytesList.put(prefix);
59 }
60 const filter = signingFieldsOnly
61 ? (f) => f.isSigningField
62 : undefined;
63 types_1.coreTypes.STObject
64 .from(object, filter, definitions)
65 .toBytesSink(bytesList);
66 if (suffix) {
67 bytesList.put(suffix);
68 }
69 return bytesList.toBytes();
70}
71exports.serializeObject = serializeObject;
72/**
73 * Serialize an object for signing
74 *
75 * @param transaction Transaction to serialize
76 * @param prefix Prefix bytes to put before the serialized object
77 * @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
78 * @returns A Uint8Array with the serialized object
79 */
80function signingData(transaction, prefix = hash_prefixes_1.HashPrefix.transactionSig, opts = {}) {
81 return serializeObject(transaction, {
82 prefix,
83 signingFieldsOnly: true,
84 definitions: opts.definitions,
85 });
86}
87exports.signingData = signingData;
88/**
89 * Serialize a signingClaim
90 *
91 * @param claim A claim object to serialize
92 * @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
93 * @returns the serialized object with appropriate prefix
94 */
95function signingClaimData(claim) {
96 const num = BigInt(String(claim.amount));
97 const prefix = hash_prefixes_1.HashPrefix.paymentChannelClaim;
98 const channel = types_1.coreTypes.Hash256.from(claim.channel).toBytes();
99 const amount = types_1.coreTypes.UInt64.from(num).toBytes();
100 const bytesList = new binary_serializer_1.BytesList();
101 bytesList.put(prefix);
102 bytesList.put(channel);
103 bytesList.put(amount);
104 return bytesList.toBytes();
105}
106exports.signingClaimData = signingClaimData;
107/**
108 * Serialize a transaction object for multiSigning
109 *
110 * @param transaction transaction to serialize
111 * @param signingAccount Account to sign the transaction with
112 * @param opts.definitions Custom rippled types to use instead of the default. Used for sidechains and amendments.
113 * @returns serialized transaction with appropriate prefix and suffix
114 */
115function multiSigningData(transaction, signingAccount, opts = {
116 definitions: enums_1.DEFAULT_DEFINITIONS,
117}) {
118 const prefix = hash_prefixes_1.HashPrefix.transactionMultiSig;
119 const suffix = types_1.coreTypes.AccountID.from(signingAccount).toBytes();
120 return serializeObject(transaction, {
121 prefix,
122 suffix,
123 signingFieldsOnly: true,
124 definitions: opts.definitions,
125 });
126}
127exports.multiSigningData = multiSigningData;
128//# sourceMappingURL=binary.js.map
\No newline at end of file