UNPKG

@swtc/serializer

Version:
133 lines 6.12 kB
"use strict"; /** * Type definitions for binary format and * Utils to handle format conversions. * * This file should not be included directly. Instead, find the format you're * trying to parse or serialize in binformat.js and pass that to * SerializedObject.parse() or SerializedObject.serialize(). */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Factory = Factory; const extend_1 = __importDefault(require("extend")); const common_1 = require("@swtc/common"); const wallet_1 = require("@swtc/wallet"); const TumAmount_1 = require("./TumAmount"); const STAccount_1 = __importDefault(require("./types/STAccount")); const STAmount_1 = __importDefault(require("./types/STAmount")); const STArray_1 = __importDefault(require("./types/STArray")); const STCurrency_1 = __importDefault(require("./types/STCurrency")); const STHash128_1 = __importDefault(require("./types/STHash128")); const STHash160_1 = __importDefault(require("./types/STHash160")); const STHash256_1 = __importDefault(require("./types/STHash256")); const STInt16_1 = __importDefault(require("./types/STInt16")); const STInt32_1 = __importDefault(require("./types/STInt32")); const STInt64_1 = __importDefault(require("./types/STInt64")); const STInt8_1 = __importDefault(require("./types/STInt8")); const STMemo_1 = __importDefault(require("./types/STMemo")); const STObject_1 = __importDefault(require("./types/STObject")); const STPathSet_1 = __importDefault(require("./types/STPathSet")); const STVector256_1 = __importDefault(require("./types/STVector256")); const STVL_1 = __importDefault(require("./types/STVL")); const Utils_1 = require("./Utils"); function Factory(Wallet = (0, wallet_1.Factory)("jingtum")) { const KeyPair = Wallet.KeyPair; const Amount = (0, TumAmount_1.Factory)(Wallet); const DataCheck = Amount.DataCheck; const Methods = { Int8: STInt8_1.default, Int16: STInt16_1.default, Int32: STInt32_1.default, Int64: STInt64_1.default, Hash128: STHash128_1.default, Hash160: (0, extend_1.default)(STHash160_1.default, { KeyPair }), Hash256: STHash256_1.default, STCurrency: (0, extend_1.default)(STCurrency_1.default, { dataCheck: DataCheck }), Amount: (0, extend_1.default)(STAmount_1.default, { Amount, KeyPair, STCurrency: STCurrency_1.default }), VL: STVL_1.default, Account: (0, extend_1.default)(STAccount_1.default, { KeyPair }), PathSet: (0, extend_1.default)(STPathSet_1.default, { KeyPair, STCurrency: STCurrency_1.default, STHash160: STHash160_1.default, STInt8: STInt8_1.default }), Vector256: (0, extend_1.default)(STVector256_1.default, { STHash256: STHash256_1.default }), STMemo: (0, extend_1.default)(STMemo_1.default, { customSerialize: serialize, customParse: parse, STInt8: STInt8_1.default }), Object: (0, extend_1.default)(true, STObject_1.default, { customSerialize: serialize, customParse: parse, STInt8: STInt8_1.default }), Array: (0, extend_1.default)(true, STArray_1.default, { customSerialize: serialize, customParse: parse, STInt8: STInt8_1.default }), serialize, parse }; function serialize(so, field_name, value) { // so: a byte-stream to serialize into. // field_name: a string for the field name ('LedgerEntryType' etc.) // value: the value of that field. const field_coordinates = common_1.INVERSE_FIELDS_MAP[field_name]; const type_bits = field_coordinates[0]; const field_bits = field_coordinates[1]; const tag_byte = (type_bits < 16 ? type_bits << 4 : 0) | (field_bits < 16 ? field_bits : 0); if (typeof value === "string") { if (field_name === "LedgerEntryType") { value = (0, Utils_1.get_ledger_entry_type)(value); } else if (field_name === "TransactionResult") { value = (0, Utils_1.get_transaction_type)(value); // binformat.ter[value]; } } STInt8_1.default.serialize(so, tag_byte); if (type_bits >= 16) { STInt8_1.default.serialize(so, type_bits); } if (field_bits >= 16) { STInt8_1.default.serialize(so, field_bits); } // Get the serializer class (ST...) let serialized_object_type; if (field_name === "Memo" && typeof value === "object") { // for Memo we override the default behavior with our STMemo serializer serialized_object_type = Methods.STMemo; } else { // for a field based on the type bits. serialized_object_type = Methods[common_1.TYPES_MAP[type_bits]]; } try { serialized_object_type.serialize(so, value); } catch (e) { if (e instanceof Error) { e.message += " (" + field_name + ")"; } throw e; } } function parse(so) { const tag_byte = so.read(1)[0]; let type_bits = tag_byte >> 4; if (type_bits === 0) { type_bits = so.read(1)[0]; } const field_bits = tag_byte & 0x0f; const field_name = field_bits === 0 ? common_1.FIELDS_MAP[type_bits][so.read(1)[0]] : common_1.FIELDS_MAP[type_bits][field_bits]; (0, common_1.funcAssert)(field_name, "Unknown field - header byte is 0x" + tag_byte.toString(16)); // Get the parser class (ST...) for a field based on the type bits. const type = field_name === "Memo" ? Methods.STMemo : Methods[common_1.TYPES_MAP[type_bits]]; (0, common_1.funcAssert)(type, "Unknown type - header byte is 0x" + tag_byte.toString(16)); return [field_name, type.parse(so)]; // key, value } return Methods; } //# sourceMappingURL=TypesUtils.js.map