UNPKG

navio-blsct

Version:

TypeScript bindings for the `libblsct` library used by the [Navio](https://nav.io/) blockchain to construct confidential transactions based on the BLS12-381 curve.

79 lines (78 loc) 2.18 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ManagedObj = void 0; const blsct_1 = require("./blsct"); const util_1 = __importDefault(require("util")); const finalizer = new FinalizationRegistry((fi) => { if (fi.obj && !fi.isBorrow) { (0, blsct_1.freeObj)(fi.obj); fi.obj = undefined; } }); class ManagedObj { obj; objSize; fi; constructor(obj) { this.fi = { obj, cls: this.constructor.name, isBorrow: false, }; if (obj !== undefined) { finalizer.register(this, this.fi, this); } this.obj = obj; this.objSize = 0; } size() { return this.objSize; } move() { if (this.obj === undefined) { throw new Error('Obj has already been moved'); } const obj = this.obj; this.obj = undefined; // both fi.obj and unregister can suppress freeing memory // using both just in case either of them fails this.fi.obj = undefined; finalizer.unregister(this); return obj; } static fromObj(obj) { return new this(obj); } static fromObjAndSize(obj, objSize) { const x = new this(obj); x.objSize = objSize; return x; } toString() { return `${this.constructor.name}(${this.serialize()})`; } [util_1.default.inspect.custom]() { return this.toString(); } serialize() { return 'NOT IMPLEMENTED'; } static _deserialize(hex, deserializer) { if (hex.length % 2 !== 0) { hex = `0${hex}`; } const rv = deserializer(hex); if (rv.result !== 0) { (0, blsct_1.freeObj)(rv); throw new Error(`Deserialization failed. Error code = ${rv.result}`); } const x = new this(rv.value); x.objSize = rv.value_size; (0, blsct_1.freeObj)(rv); return x; } } exports.ManagedObj = ManagedObj;