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.
129 lines (128 loc) • 4.17 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ManagedObj = void 0;
const blsct_1 = require("./blsct");
const util = __importStar(require("util"));
const finalizer = new FinalizationRegistry((fi) => {
if (fi.obj) {
if (fi.deleteMethod) {
fi.deleteMethod();
}
else {
(0, blsct_1.freeObj)(fi.obj);
}
fi.obj = undefined;
}
});
class ManagedObj {
obj;
objSize;
fi;
/** Constructs a new instance of the class using the C++ object.
* @param obj - The C++ object to use for the new instance.
*/
constructor(obj, deleteMethod) {
if (obj === undefined || obj === null) {
throw new Error("Undefined/null object passed to ManagedObj");
}
this.obj = obj;
this.objSize = 0;
this.fi = {
obj,
cls: this.constructor.name,
deleteMethod,
};
finalizer.register(this, this.fi, this);
}
/** Returns the size of the underlying C++ object.
* @returns The size of the underlying C++ object in bytes.
*/
size() {
return this.objSize;
}
/** Constucts a new instance using the provided object.
*
* @param obj - The object to use for the new instance.
* @return A new instance of the class.
*/
static fromObj(obj) {
return new this(obj);
}
/** Constructs a new instance using the provided object and size.
*
* @param obj - The object to use for the new instance.
* @param objSize - The size of the object.
* @return A new instance of the class.
*/
static fromObjAndSize(obj, objSize) {
const x = new this(obj);
x.objSize = objSize;
return x;
}
/** Returns a string representation of the instance.
* @returns A string representation of the instance.
*/
toString() {
return `${this.constructor.name}(${this.serialize()})`;
}
/** @hidden */
[util.inspect.custom]() {
return this.toString();
}
/** Serializes the instance to a hexadecimal string.
* @returns A hexadecimal string representation of the instance.
*/
serialize() {
return 'NOT IMPLEMENTED';
}
/** @hidden */
static _deserialize(hex, deserializer) {
if (hex.length % 2 !== 0) {
hex = `0${hex}`;
}
const rv = deserializer(hex);
if (rv.result !== 0) {
const msg = `Deserialization failed. Error code = ${rv.result}`;
(0, blsct_1.freeObj)(rv);
throw new Error(msg);
}
const x = new this(rv.value);
x.objSize = rv.value_size;
(0, blsct_1.freeObj)(rv);
return x;
}
}
exports.ManagedObj = ManagedObj;