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.
47 lines (46 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CTxId = void 0;
const blsct_1 = require("./blsct");
const managedObj_1 = require("./managedObj");
/** Represents the transaction ID of a CMutableTransaction
*
* Examples:
* ```ts
* const { CTxId, CTX_ID_SIZE } = require('navio-blsct')
* const { randomBytes } = require('crypto')
* const cTxIdHex = randomBytes(CTX_ID_SIZE).toString('hex')
* const cTxId = CTxId.deserialize(cTxIdHex)
* const ser = cTxId.serialize()
* const deser = CTxId.deserialize(ser)
* ser === deser.serialize() // true
* ```
*/
class CTxId extends managedObj_1.ManagedObj {
constructor(obj) {
super(obj);
}
value() {
return (0, blsct_1.castToUint8_tPtr)(this.obj);
}
serialize() {
const buf = this.value();
return (0, blsct_1.toHex)(buf, blsct_1.CTX_ID_SIZE);
}
/** Deserializes a `CTxId` from its hexadecimal representation.
*
* * @param hex - The hexadecimal string to deserialize.
* * @returns A new `CTxId` instance.
*/
static deserialize(hex) {
if (hex.length % 2 !== 0) {
hex = `0${hex}`;
}
if (hex.length !== blsct_1.CTX_ID_SIZE * 2) {
throw new Error(`Invalir TxId hex length. Expected ${blsct_1.CTX_ID_SIZE * 2}, but got ${hex.length}.`);
}
const obj = (0, blsct_1.hexToMallocedBuf)(hex);
return new CTxId(obj);
}
}
exports.CTxId = CTxId;