golos-lib-js
Version:
Golos-js the JavaScript library with API for GOLOS blockchain
162 lines (161 loc) • 5.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.aes_decrypt = exports._Price = exports._AssetEditor = exports._Asset = exports.Price = exports.NativeLibContext = exports.AssetEditor = exports.Asset = void 0;
exports.assertNativeLib = assertNativeLib;
exports.importNativeLib = importNativeLib;
exports.importNativeLibCtx = importNativeLibCtx;
exports.isNativeLibLoaded = isNativeLibLoaded;
exports.unloadNativeLib = unloadNativeLib;
var _code = _interopRequireDefault(require("./code"));
var _golos_lib = _interopRequireDefault(require("./golos_lib"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const {
_Asset,
_AssetEditor,
_Price,
aes256_decrypt
} = _golos_lib.default;
exports._Price = _Price;
exports._AssetEditor = _AssetEditor;
exports._Asset = _Asset;
class NativeLibContext {}
exports.NativeLibContext = NativeLibContext;
const fromHexString = str => new Uint8Array(str.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));
const fromBase64String = str => {
if (typeof atob !== 'undefined') {
return Uint8Array.from(atob(str), c => c.charCodeAt(0));
} else {
return Buffer.from(str, 'base64');
}
};
async function importNativeLibCtx() {
if (!_code.default || !_golos_lib.default || !_Asset) {
const what = !_code.default ? 'wasm code' : 'init()';
throw new Error(`Native golos-lib core is not included into this golos-lib-js library. There is no ${what}.`);
}
await (0, _golos_lib.default)(fromBase64String(_code.default));
return new NativeLibContext();
}
let globalNativeCtx = null;
async function importNativeLib() {
if (globalNativeCtx) {
return globalNativeCtx;
}
globalNativeCtx = await importNativeLibCtx();
return globalNativeCtx;
}
function unloadNativeLib() {
globalNativeCtx = null;
}
function isNativeLibLoaded() {
return !!globalNativeCtx;
}
function assertNativeLib(forWhat) {
let version = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0.9.0';
if (!isNativeLibLoaded()) {
throw new Error(`Starting from ${version}, golos-lib-js requires "await golos.importNativeLib()" before calling ${forWhat}`);
}
}
function wrapNative(orig) {
return function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
const hasContext = args[0] instanceof NativeLibContext;
if (!hasContext && !isNativeLibLoaded()) {
return importNativeLib().then(() => {
return orig(...args);
});
}
if (hasContext) {
args = args.slice(1, args.length);
}
return orig(...args);
};
}
let Asset = exports.Asset = wrapNative((amount, precision, symbol) => {
let a = null;
if (precision !== undefined && symbol) {
a = _Asset.new(amount, precision, symbol);
} else {
const str = amount;
a = _Asset.fromString(str);
}
const wrapBinOp = function (op) {
return function (asset2) {
if (!(asset2 instanceof _Asset)) return this[`_${op}_num`](asset2);
return this[`_${op}`](asset2);
};
};
_Asset.prototype.plus = wrapBinOp('plus');
_Asset.prototype.minus = wrapBinOp('minus');
_Asset.prototype.mul = function (price, remainderAsset) {
if (price instanceof _Price) {
if (!remainderAsset) {
remainderAsset = this.clone();
}
return this._mul_price(price, remainderAsset);
}
const asset2 = price;
if (!(asset2 instanceof _Asset)) return this._mul_num(asset2);
return this._mul(asset2);
};
_Asset.prototype.div = wrapBinOp('div');
_Asset.prototype.mod = wrapBinOp('mod');
_Asset.prototype.eq = wrapBinOp('eq');
_Asset.prototype.ne = wrapBinOp('ne');
_Asset.prototype.lt = wrapBinOp('lt');
_Asset.prototype.gt = wrapBinOp('gt');
_Asset.prototype.lte = wrapBinOp('lte');
_Asset.prototype.gte = wrapBinOp('gte');
_Asset.prototype.min = wrapBinOp('min');
_Asset.prototype.max = wrapBinOp('max');
_Asset.prototype.toJSON = function () {
return this.toString();
};
return a;
});
let AssetEditor = exports.AssetEditor = wrapNative((amount, precision, symbol) => {
let ae = null;
if (amount instanceof _Asset) {
ae = _AssetEditor.fromAsset(amount);
} else if (precision !== undefined && symbol) {
ae = _AssetEditor.new(amount, precision, symbol);
} else {
const str = amount;
ae = _AssetEditor.fromString(str);
}
_AssetEditor.prototype.toJSON = function () {
return this.asset.toString();
};
_AssetEditor.prototype.toString = function () {
return JSON.stringify({
asset: this.asset,
amount_str: this.amountStr
});
};
return ae;
});
let Price = exports.Price = wrapNative((base, quote) => {
let pr = null;
if (!quote) {
pr = _Price.new(Asset(base.base), Asset(base.quote));
} else {
pr = _Price.new(base, quote);
}
_Price.prototype.toJSON = function () {
return {
base: this.base.toJSON(),
quote: this.quote.toJSON()
};
};
_Price.prototype.toString = function () {
return this.toJSON().toString();
};
return pr;
});
let aes_decrypt = exports.aes_decrypt = wrapNative((key, iv, data) => {
return aes256_decrypt(key, iv, data);
});