UNPKG

@airgap/serializer

Version:

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

124 lines 5.98 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IACProtocol = void 0; var bs58check = __importStar(require("@airgap/coinlib-core/dependencies/src/bs58check-2.1.2/index")); var rlp = __importStar(require("@airgap/coinlib-core/dependencies/src/rlp-2.2.3/index")); var errors_1 = require("@airgap/coinlib-core/errors"); var chunked_payload_1 = require("./payloads/chunked-payload"); var full_payload_1 = require("./payloads/full-payload"); var serializer_1 = require("./serializer"); function sortByPage(a, b) { return a.currentPage - b.currentPage; } // IACProtocolMessage instead of IACProtocol? var IACProtocol = /** @class */ (function () { function IACProtocol(data) { this.version = 2; if (data instanceof full_payload_1.FullPayload) { this.payloadType = serializer_1.IACPayloadType.FULL; this.payload = data; } else if (data instanceof chunked_payload_1.ChunkedPayload) { this.payloadType = serializer_1.IACPayloadType.CHUNKED; this.payload = data; } else { throw new errors_1.SerializerError(errors_1.SerializerErrorType.PAYLOAD_TYPE_UNKNOWN, "Is neither \"Full\" nor \"Chunked\"."); } } IACProtocol.prototype.decoded = function () { return [this.version, this.payloadType, this.payload]; }; IACProtocol.prototype.encoded = function (serializer) { if (serializer === void 0) { serializer = serializer_1.Serializer.getInstance(); } return bs58check.encode(rlp.encode([this.version.toString(), this.payloadType.toString(), this.payload.asArray(serializer)])); }; IACProtocol.fromDecoded = function (data, singleChunkSize, multiChunkSize, serializer) { if (singleChunkSize === void 0) { singleChunkSize = 0; } if (multiChunkSize === void 0) { multiChunkSize = 0; } if (serializer === void 0) { serializer = serializer_1.Serializer.getInstance(); } var payload = full_payload_1.FullPayload.fromDecoded(data); var rawPayload = payload.asBuffer(serializer); if (singleChunkSize > 0 && rawPayload.length > singleChunkSize) { var chunks_1 = []; var nodeBuffer = rawPayload; var bufferLength = rawPayload.length; var i = 0; while (i < bufferLength) { chunks_1.push(nodeBuffer.slice(i, (i += multiChunkSize))); } return chunks_1.map(function (chunk, index) { return new IACProtocol(chunked_payload_1.ChunkedPayload.fromDecoded({ currentPage: index, total: chunks_1.length, payload: chunk })); }); } else { return [new IACProtocol(payload)]; } }; IACProtocol.fromEncoded = function (data, serializer) { if (serializer === void 0) { serializer = serializer_1.Serializer.getInstance(); } var chunked = []; var finalPayload; // make sure that all are the same type var globalType; data.forEach(function (entry) { var decoded = rlp.decode(bs58check.decode(entry)); // const version: string = decoded[0].toString() var type = parseInt(decoded[1].toString(), 10); globalType = globalType !== null && globalType !== void 0 ? globalType : type; if (globalType !== type) { throw new errors_1.SerializerError(errors_1.SerializerErrorType.PAYLOAD_TYPE_MISMATCH, 'All types within a group must either be "full" or "chunked".'); } if (type === serializer_1.IACPayloadType.FULL) { var payload = decoded[2]; finalPayload = full_payload_1.FullPayload.fromEncoded(payload, serializer); } else if (type === serializer_1.IACPayloadType.CHUNKED) { var payload = decoded[2]; chunked.push(chunked_payload_1.ChunkedPayload.fromEncoded(payload)); } else { throw new errors_1.SerializerError(errors_1.SerializerErrorType.PAYLOAD_TYPE_NOT_SUPPORTED, "Type \"".concat(type, "\" is unknown.")); } }); if (!finalPayload) { var sortedChunks = chunked.sort(sortByPage); var arr = sortedChunks.map(function (chunk) { return chunk.buffer; }); var result = { availablePages: sortedChunks.map(function (a) { return a.currentPage; }), totalPages: sortedChunks[0].total }; if (result.availablePages.length < result.totalPages) { throw result; } finalPayload = full_payload_1.FullPayload.fromEncoded(rlp.decode(Buffer.concat(arr)), serializer); } return [new IACProtocol(finalPayload)]; }; return IACProtocol; }()); exports.IACProtocol = IACProtocol; //# sourceMappingURL=inter-app-communication-protocol.js.map