airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
88 lines • 4.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var errors_1 = require("../errors");
var ProtocolSymbols_1 = require("../utils/ProtocolSymbols");
var serializer_1 = require("./serializer");
var generateId_1 = require("./utils/generateId");
var json_to_rlp_1 = require("./utils/json-to-rlp");
var ID_LENGTH = 10;
var Message = /** @class */ (function () {
function Message(type, protocol, payload, id, version) {
if (id === void 0) { id = generateId_1.generateId(ID_LENGTH); }
if (version === void 0) { version = '1'; }
this.id = id;
this.type = type;
this.protocol = protocol;
this.payload = payload;
this.version = version;
var schemaInfo = serializer_1.Serializer.getSchema(this.type, this.protocol);
this.schema = json_to_rlp_1.unwrapSchema(schemaInfo.schema);
}
Message.prototype.asJson = function () {
return {
type: this.type,
protocol: this.protocol,
id: this.id,
payload: this.payload
};
};
Message.prototype.asArray = function () {
var array = json_to_rlp_1.jsonToArray('root', this.schema, this.payload);
return [this.version, this.type.toString(), this.protocol, array, this.id];
};
Message.fromDecoded = function (object) {
return new Message(object.type, object.protocol, object.payload, object.id);
};
Message.fromEncoded = function (buf) {
var version = this.parseVersion(buf[0]);
var type = this.parseType(buf[1]);
var protocol = this.parseProtocol(buf[2]);
// Backwards compatiblity for version 0, before we had an ID
var idBuf = version === '0' ? Buffer.from(generateId_1.generateId(ID_LENGTH)) : buf[4];
// End Backwards compatibility
var id = this.parseId(idBuf);
var encodedPayload = this.parsePayload(buf[3]);
var schemaInfo = serializer_1.Serializer.getSchema(type, protocol);
var schema = json_to_rlp_1.unwrapSchema(schemaInfo.schema);
var schemaTransformer = schemaInfo.transformer;
var json = json_to_rlp_1.rlpArrayToJson(schema, encodedPayload);
var payload = schemaTransformer ? schemaTransformer(json) : json;
return new Message(type, protocol, payload, id, version);
};
Message.parseVersion = function (buffer) {
return this.validateProperty('Version', buffer, function (buf) { return buf.toString(); }, function (val) { return val === '0' || val === '1'; });
};
Message.parseType = function (buffer) {
return this.validateProperty('Type', buffer, function (buf) { return parseInt(buf.toString(), 10); }, function (val) {
try {
serializer_1.Serializer.getSchema(val, ProtocolSymbols_1.MainProtocolSymbols.ETH); // TODO: Remove hardcoded protocol
return true;
}
catch (error) {
return false;
}
});
};
Message.parseProtocol = function (buffer) {
return this.validateProperty('Protocol', buffer, function (buf) { return buf.toString(); }, function (val) { return val.length === 0 || Object.values(ProtocolSymbols_1.MainProtocolSymbols).some(function (value) { return val.split('-')[0] === value; }); });
};
Message.parseId = function (buffer) {
return this.validateProperty('Id', buffer, function (buf) { return buf.toString(); }, function (val) { return val.length === ID_LENGTH; });
};
Message.parsePayload = function (buffer) {
return this.validateProperty('Payload', buffer, function (buf) { return buf; }, function () { return true; });
};
Message.validateProperty = function (property, buffer, parse, validate) {
if (typeof buffer === 'undefined') {
throw new errors_1.SerializerError(errors_1.SerializerErrorType.PROPERTY_IS_EMPTY, property + " is empty");
}
var parsed = parse(buffer);
if (validate(parsed)) {
return parsed; // TODO: Use type guard?
}
throw new errors_1.SerializerError(errors_1.SerializerErrorType.PROPERTY_IS_EMPTY, property + " is invalid: \"" + parsed + "\"");
};
return Message;
}());
exports.Message = Message;
//# sourceMappingURL=message.js.map