airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
96 lines • 4.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var bs58check = require("../dependencies/src/bs58check-2.1.2/index");
var rlp = require("../dependencies/src/rlp-2.2.3/index");
var errors_1 = require("../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 () {
return bs58check.encode(rlp.encode([this.version.toString(), this.payloadType.toString(), this.payload.asArray()]));
};
IACProtocol.fromDecoded = function (data, chunkSize) {
if (chunkSize === void 0) { chunkSize = 0; }
var payload = full_payload_1.FullPayload.fromDecoded(data);
var rawPayload = payload.asBuffer();
if (chunkSize > 0 && rawPayload.length > chunkSize) {
var chunks_1 = [];
var nodeBuffer = rawPayload;
var bufferLength = rawPayload.length;
var i = 0;
while (i < bufferLength) {
chunks_1.push(nodeBuffer.slice(i, (i += chunkSize)));
}
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) {
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);
}
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 \"" + 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)));
}
return [new IACProtocol(finalPayload)];
};
return IACProtocol;
}());
exports.IACProtocol = IACProtocol;
//# sourceMappingURL=inter-app-communication-protocol.js.map