UNPKG

@airgap/serializer

Version:

The @airgap/serializer provides serializers used in AirGap applications.

116 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Message = exports.isMessageDefinitionArray = void 0; var errors_1 = require("@airgap/coinlib-core/errors"); var serializer_1 = require("./serializer"); var generateId_1 = require("./utils/generateId"); var json_to_rlp_1 = require("./utils/json-to-rlp"); function isMessageDefinitionArray(value) { return (Array.isArray(value) && value.length === 5 && typeof value[0] === 'number' && typeof value[1] === 'number' && typeof value[2] === 'string' && typeof value[3] === 'number' && Array.isArray(value[4])); } exports.isMessageDefinitionArray = isMessageDefinitionArray; var Message = /** @class */ (function () { function Message(type, protocol, payload, id, version) { if (id === void 0) { id = (0, generateId_1.generateId)(generateId_1.ID_LENGTH); } if (version === void 0) { version = 1; } this.id = id; this.type = type; this.protocol = protocol; this.payload = payload; this.version = version; } Message.prototype.asJson = function () { return { type: this.type, protocol: this.protocol, id: this.id, payload: this.payload }; }; Message.prototype.asArray = function (serializer) { if (serializer === void 0) { serializer = serializer_1.SerializerV3.getInstance(); } var schemaInfos = serializer.getSchemas(this.type, this.protocol); var lastError; for (var _i = 0, schemaInfos_1 = schemaInfos; _i < schemaInfos_1.length; _i++) { var schemaInfo = schemaInfos_1[_i]; try { var schema = (0, json_to_rlp_1.unwrapSchema)(schemaInfo.schema); var array = (0, json_to_rlp_1.jsonToArray)('root', schema, this.payload); return [this.version, this.type, this.protocol, this.id, array]; } catch (e) { lastError = e; } } if (lastError) { throw lastError; } throw new Error('NO SCHEMA FOUND'); }; Message.fromDecoded = function (object) { return new Message(object.type, object.protocol, object.payload, object.id); }; Message.fromEncoded = function (buf, serializer) { if (serializer === void 0) { serializer = serializer_1.SerializerV3.getInstance(); } var version = this.validateVersion(buf[0]); var protocol = this.validateProtocol(buf[2], Array.from(serializer.getSupportedProtocols())); var type = this.validateType(buf[1], protocol, serializer); var id = this.validateId(buf[3]); var encodedPayload = this.validatePayload(buf[4]); var schemaInfos = serializer.getSchemas(type, protocol); for (var _i = 0, schemaInfos_2 = schemaInfos; _i < schemaInfos_2.length; _i++) { var schemaInfo = schemaInfos_2[_i]; try { var schema = (0, json_to_rlp_1.unwrapSchema)(schemaInfo.schema); var schemaTransformer = schemaInfo.transformer; var json = (0, json_to_rlp_1.rlpArrayToJson)(schema, encodedPayload); var payload = schemaTransformer ? schemaTransformer(json) : json; return new Message(type, protocol, payload, id, version); } catch (e) { } } throw new Error('NO SCHEMA MATCHED'); }; Message.validateVersion = function (version) { return this.validateProperty('Version', version, function (val) { return val === 0 || val === 1; }); }; Message.validateType = function (value, protocol, serializer) { if (serializer === void 0) { serializer = serializer_1.SerializerV3.getInstance(); } return this.validateProperty('Type', value, function (val) { try { serializer.getSchemas(val, protocol); return true; } catch (error) { return false; } }); }; Message.validateProtocol = function (protocol, supportedProtocols) { return this.validateProperty('Protocol', protocol, function (val) { return val.length === 0 || supportedProtocols.some(function (value) { return val === value || val.split('-')[0] === value; }); }); }; Message.validateId = function (id) { return this.validateProperty('Id', id, function (val) { return val.toString().length <= generateId_1.ID_LENGTH; }); }; Message.validatePayload = function (payload) { return this.validateProperty('Payload', payload, function () { return true; }); }; Message.validateProperty = function (property, value, validate) { if (typeof value === 'undefined') { throw new errors_1.SerializerError(errors_1.SerializerErrorType.PROPERTY_IS_EMPTY, "".concat(property, " is empty")); } if (validate(value)) { return value; // TODO: Use type guard? } throw new errors_1.SerializerError(errors_1.SerializerErrorType.PROPERTY_IS_EMPTY, "".concat(property, " is invalid: \"").concat(value, "\"")); }; return Message; }()); exports.Message = Message; //# sourceMappingURL=message.js.map