airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
137 lines • 6.26 kB
JavaScript
;
// tslint:disable: max-classes-per-file
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Lazy_1 = require("../../../../data/Lazy");
var utils_1 = require("../utils");
var factories_1 = require("./factories");
var MichelsonType_1 = require("./MichelsonType");
var MichelsonAnnotationPrefix;
(function (MichelsonAnnotationPrefix) {
MichelsonAnnotationPrefix["TYPE"] = ":";
MichelsonAnnotationPrefix["VARIABLE"] = "@";
MichelsonAnnotationPrefix["FIELD"] = "%";
})(MichelsonAnnotationPrefix = exports.MichelsonAnnotationPrefix || (exports.MichelsonAnnotationPrefix = {}));
var MichelsonTypeMeta = /** @class */ (function () {
function MichelsonTypeMeta(type, parent, annots) {
if (annots === void 0) { annots = []; }
this.type = type;
this.parent = parent;
this.annots = annots;
}
MichelsonTypeMeta.fromMichelineNode = function (node) {
if (!utils_1.isMichelinePrimitiveApplication(node)) {
return undefined;
}
return this.fromMichelinePrimitiveApplication(node);
};
MichelsonTypeMeta.fromMichelinePrimitiveApplication = function (primitiveApplication, parent) {
var _a, _b, _c;
return this.from(primitiveApplication.prim, (_a = primitiveApplication.annots, (_a !== null && _a !== void 0 ? _a : [])), (_c = (_b = primitiveApplication.args) === null || _b === void 0 ? void 0 : _b.filter(utils_1.isMichelinePrimitiveApplication), (_c !== null && _c !== void 0 ? _c : [])), parent);
};
MichelsonTypeMeta.from = function (type, annots, args, parent) {
return args.length === 0
? new MichelsonTypeMeta(type, parent, annots)
: new MichelsonGenericTypeMeta(type, args, parent, annots);
};
MichelsonTypeMeta.prototype.createValue = function (value, configuration) {
if (configuration === void 0) { configuration = {}; }
var _a;
if (value instanceof MichelsonType_1.MichelsonType) {
return value;
}
var raw = this.getRawValue(value);
if (configuration.beforeNext) {
configuration.beforeNext(this, raw);
}
var michelsonValue = factories_1.michelsonTypeFactories[this.type](raw);
if (!(_a = configuration.lazyEval, (_a !== null && _a !== void 0 ? _a : true))) {
michelsonValue.eval();
}
if (configuration.onNext) {
configuration.onNext(this, raw, michelsonValue);
}
return michelsonValue;
};
MichelsonTypeMeta.prototype.getAnnotation = function () {
var prefixes = [];
for (var _i = 0; _i < arguments.length; _i++) {
prefixes[_i] = arguments[_i];
}
var _loop_1 = function (annot) {
var matchedPrefix = prefixes.find(function (prefix) { return annot.startsWith(prefix); });
if (matchedPrefix !== undefined) {
return { value: annot.slice(matchedPrefix.length) };
}
};
for (var _a = 0, _b = this.annots; _a < _b.length; _a++) {
var annot = _b[_a];
var state_1 = _loop_1(annot);
if (typeof state_1 === "object")
return state_1.value;
}
return undefined;
};
MichelsonTypeMeta.prototype.getRawValue = function (value) {
var argName = this.getAnnotation(MichelsonAnnotationPrefix.TYPE, MichelsonAnnotationPrefix.FIELD);
return value instanceof Object && argName && argName in value
? value[argName]
: value;
};
return MichelsonTypeMeta;
}());
exports.MichelsonTypeMeta = MichelsonTypeMeta;
var MichelsonGenericTypeMeta = /** @class */ (function (_super) {
__extends(MichelsonGenericTypeMeta, _super);
function MichelsonGenericTypeMeta(type, genericTypes, parent, annots) {
if (annots === void 0) { annots = []; }
var _this = _super.call(this, type, parent, annots) || this;
_this.generics = genericTypes.map(function (type) {
return new Lazy_1.Lazy(function () { return MichelsonTypeMeta.fromMichelinePrimitiveApplication(type, _this); });
});
return _this;
}
MichelsonGenericTypeMeta.prototype.createValue = function (value, configuration) {
var _a;
if (value instanceof MichelsonType_1.MichelsonType) {
return value;
}
var raw = this.getRawValue(value);
if (configuration.beforeNext) {
configuration.beforeNext(this, raw);
}
var genericFactories = this.generics.map(function (genericMeta) {
return function (genericValue) { return genericMeta.get().createValue(genericValue, configuration); };
});
var michelsonValue = factories_1.michelsonTypeFactories[this.type].apply(factories_1.michelsonTypeFactories, __spreadArrays([raw], genericFactories));
if (!(_a = configuration.lazyEval, (_a !== null && _a !== void 0 ? _a : true))) {
michelsonValue.eval();
}
if (configuration.onNext) {
configuration.onNext(this, raw, michelsonValue);
}
return michelsonValue;
};
return MichelsonGenericTypeMeta;
}(MichelsonTypeMeta));
exports.MichelsonGenericTypeMeta = MichelsonGenericTypeMeta;
//# sourceMappingURL=MichelsonTypeMeta.js.map