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.
109 lines (108 loc) • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CTx = void 0;
const blsct_1 = require("./blsct");
const ctxId_1 = require("./ctxId");
const ctxIn_1 = require("./ctxIn");
const ctxOut_1 = require("./ctxOut");
const managedObj_1 = require("./managedObj");
class CTx extends managedObj_1.ManagedObj {
constructor(obj) {
super(obj);
}
static generate(srcTxIns, srcTxOuts) {
const txIns = [];
for (const srcTxIn of srcTxIns) {
txIns.push(srcTxIn.clone());
}
const txOuts = [];
for (const srcTxOut of srcTxOuts) {
txOuts.push(srcTxOut.clone());
}
const freeTxInsOuts = () => {
for (const txIn of txIns) {
(0, blsct_1.freeObj)(txIn);
}
for (const txOut of txOuts) {
(0, blsct_1.freeObj)(txOut);
}
};
// create vector and add txIns to it
const txInVec = (0, blsct_1.createTxInVec)();
for (const txIn of txIns) {
(0, blsct_1.addTxInToVec)(txInVec, txIn.value());
}
// create vector and add txOuts to it
const txOutVec = (0, blsct_1.createTxOutVec)();
for (const txOut of txOuts) {
(0, blsct_1.addTxOutToVec)(txOutVec, txOut.value());
}
const rv = (0, blsct_1.buildCTx)(txInVec, txOutVec);
// free the temporary vectors
(0, blsct_1.freeObj)(txInVec);
(0, blsct_1.freeObj)(txOutVec);
if (rv.result === blsct_1.BLSCT_IN_AMOUNT_ERROR) {
(0, blsct_1.freeObj)(rv);
freeTxInsOuts();
throw new Error(`Failed to build transaction. txIns[${rv.in_amount_err_index}] has an invalid amount`);
}
if (rv.result === blsct_1.BLSCT_OUT_AMOUNT_ERROR) {
(0, blsct_1.freeObj)(rv);
freeTxInsOuts();
throw new Error(`Failed to build transaciton. tx_outs[${rv.out_amount_err_index}] has an invalid amount`);
}
if (rv.result !== 0) {
freeTxInsOuts();
(0, blsct_1.freeObj)(rv);
throw new Error(`building tx failed. Error code = ${rv.result}`);
}
const obj = rv.ser_ctx; // rv.ser_ctx is a byte array
const objSize = rv.ser_ctx_size; // rv.ser_ctx_size is the byte array size
(0, blsct_1.freeObj)(rv);
return CTx.fromObjAndSize(obj, objSize);
}
value() {
return (0, blsct_1.castToUint8_tPtr)(this.obj);
}
getCTxId() {
const txIdHex = (0, blsct_1.getCTxId)(this.value(), this.size());
return ctxId_1.CTxId.deserialize(txIdHex);
}
getCTxIns() {
const ctxIns = (0, blsct_1.getCTxIns)(this.value(), this.size());
const numCtxIns = (0, blsct_1.getCTxInCount)(ctxIns);
const xs = [];
for (let i = 0; i < numCtxIns; ++i) {
const rv = (0, blsct_1.getCTxIn)(ctxIns, i);
const x = ctxIn_1.CTxIn.fromObjAndSize(rv.value, rv.value_size);
xs.push(x);
}
(0, blsct_1.freeObj)(ctxIns);
return xs;
}
getCTxOuts() {
const ctxOuts = (0, blsct_1.getCTxOuts)(this.value(), this.size());
const numCtxOuts = (0, blsct_1.getCTxOutCount)(ctxOuts);
const xs = [];
for (let i = 0; i < numCtxOuts; ++i) {
const rv = (0, blsct_1.getCTxOut)(ctxOuts, i);
const x = ctxOut_1.CTxOut.fromObjAndSize(rv.value, rv.value_size);
xs.push(x);
}
(0, blsct_1.freeObj)(ctxOuts);
return xs;
}
serialize() {
const buf = (0, blsct_1.castToUint8_tPtr)(this.value());
return (0, blsct_1.toHex)(buf, this.size());
}
static deserialize(hex) {
if (hex.length % 2 !== 0) {
hex = `0${hex}`;
}
const objSize = hex.length / 2;
const obj = (0, blsct_1.hexToMallocedBuf)(hex);
return CTx.fromObjAndSize(obj, objSize);
}
}
exports.CTx = CTx;