airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
99 lines • 4.96 kB
JavaScript
;
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 __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var supported_1 = require("../../../node/supported");
var SCALEDecoder_1 = require("../../scale/SCALEDecoder");
var MetadataDecorator_1 = require("../decorator/MetadataDecorator");
var MetadataVersioned_1 = require("../MetadataVersioned");
var MetadataV12Module_1 = require("./module/MetadataV12Module");
var MetadataV12 = /** @class */ (function (_super) {
__extends(MetadataV12, _super);
function MetadataV12(magicNumber, version, modules) {
var _this = _super.call(this) || this;
_this.magicNumber = magicNumber;
_this.version = version;
_this.modules = modules;
_this.scaleFields = [_this.magicNumber, _this.version];
return _this;
}
MetadataV12.decode = function (network, raw) {
var decoder = new SCALEDecoder_1.SCALEDecoder(network, raw);
var magicNumber = decoder.decodeNextInt(32); // 32 bits
var version = decoder.decodeNextInt(8); // 8 bits
var modules = decoder.decodeNextArray(MetadataV12Module_1.MetadataV12Module.decode);
return new MetadataV12(magicNumber.decoded, version.decoded, modules.decoded);
};
MetadataV12.prototype.decorate = function () {
var _a, _b, _c;
var storageEntries = [];
var calls = [];
var constants = [];
var callModuleIndex = 0;
for (var _i = 0, _d = this.modules.elements; _i < _d.length; _i++) {
var module_1 = _d[_i];
var moduleName = module_1.name.value;
var storagePrefix = (_b = (_a = module_1.storage.value) === null || _a === void 0 ? void 0 : _a.prefix) === null || _b === void 0 ? void 0 : _b.value;
if (storagePrefix && Object.keys(supported_1.supportedStorageEntries).includes(storagePrefix)) {
var decoratedEntries = this.createDecoratedStorageEntries(module_1.storage.value);
if (decoratedEntries) {
storageEntries.push(decoratedEntries);
}
}
if (Object.keys(supported_1.supportedCalls).includes(moduleName)) {
var decoratedCalls = this.createDecoratedCalls(moduleName, callModuleIndex, ((_c = module_1.calls.value) === null || _c === void 0 ? void 0 : _c.elements) || []);
calls.push(decoratedCalls);
}
if (Object.keys(supported_1.supportedConstants).includes(moduleName)) {
var decoratedConstants = this.createDecoratedConstants(moduleName, module_1.constants.elements);
constants.push(decoratedConstants);
}
if (module_1.calls.value !== undefined) {
callModuleIndex += 1;
}
}
return new MetadataDecorator_1.MetadataDecorator(storageEntries.reduce(function (flatten, next) { return flatten.concat(next); }, []), calls.reduce(function (flatten, next) { return flatten.concat(next); }, []), constants.reduce(function (flatten, next) { return flatten.concat(next); }, []));
};
MetadataV12.prototype.createDecoratedStorageEntries = function (storage) {
if (storage) {
return storage.storageEntries.elements
.filter(function (entry) { return supported_1.supportedStorageEntries[storage.prefix.value].includes(entry.name.value); })
.map(function (entry) { return entry.type.decorate(storage.prefix.value, entry.name.value); });
}
return undefined;
};
MetadataV12.prototype.createDecoratedCalls = function (moduleName, moduleIndex, calls) {
return calls.map(function (call, index) {
return {
moduleName: moduleName,
name: call.name.value,
moduleIndex: moduleIndex,
callIndex: index
};
});
};
MetadataV12.prototype.createDecoratedConstants = function (moduleName, constants) {
return constants.map(function (constant) {
return {
moduleName: moduleName,
name: constant.name.value,
value: constant.value.bytes,
type: constant.type.value
};
});
};
return MetadataV12;
}(MetadataVersioned_1.MetadataVersioned));
exports.MetadataV12 = MetadataV12;
//# sourceMappingURL=MetadataV12.js.map