immudb-node
Version:
Node.js SDK for immudb written in TypeScript
98 lines • 3.55 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.proofTx = exports.txFrom = exports.Tx = exports.digestTXe = void 0;
const htree_1 = __importDefault(require("./htree"));
const util_1 = require("./util");
class TXe {
constructor(hValue, vOff, valueLen, key) {
this.hValue = hValue;
this.vOff = vOff;
this.valueLen = valueLen;
this.key = key;
this.keyLen = key.length;
}
}
exports.digestTXe = ({ key, hValue }) => {
const b = new Uint8Array(key.length + hValue.length);
b.set(key);
b.set(hValue, key.length);
return util_1.hashUint8Array(b);
};
class Tx {
constructor(entries, id = 0, prevAlh, ts, blTxId, blRoot) {
this.entries = entries;
this.nEntries = entries.length;
this.htree = new htree_1.default(this.nEntries);
this.id = id;
this.prevAlh = prevAlh;
this.ts = ts;
this.blTxId = blTxId;
this.blRoot = blRoot;
this.buildHashTree();
this.innerHash = this.calcInnerHash();
this.alh = this.calcAlh();
}
buildHashTree() {
const digests = [];
for (let e of this.entries) {
digests.push(exports.digestTXe(e));
}
this.htree.buildWith(digests);
}
calcAlh() {
const encodedId = util_1.encodeInt64(this.id);
const bi = new Uint8Array(encodedId.length + this.prevAlh.length + this.innerHash.length);
bi.set(encodedId);
bi.set(this.prevAlh, encodedId.length);
bi.set(this.innerHash, encodedId.length + this.prevAlh.length);
return util_1.hashUint8Array(bi);
}
calcInnerHash() {
const encTs = util_1.encodeInt64(this.ts);
const encNEntries = util_1.encodeInt32(this.nEntries);
const encBlTxId = util_1.encodeInt64(this.blTxId);
const bj = new Uint8Array(encTs.length + encNEntries.length + this.htree.root.length + encBlTxId.length + this.blRoot.length);
bj.set(encTs);
bj.set(encNEntries, encTs.length);
bj.set(this.htree.root, encTs.length + encNEntries.length);
bj.set(encBlTxId, encTs.length + encNEntries.length + this.htree.root.length);
bj.set(this.blRoot, encTs.length + encNEntries.length + this.htree.root.length + encBlTxId.length);
return util_1.hashUint8Array(bj);
}
}
exports.Tx = Tx;
exports.txFrom = (sTx) => {
const sTxMetadata = sTx.getMetadata();
if (sTxMetadata === undefined) {
throw new Error('Corrupted txFrom parameters');
}
let entries = [];
for (let e of sTx.getEntriesList()) {
const i = new TXe(e.getHvalue_asU8(), e.getVoff(), e.getVlen(), e.getKey_asU8());
entries.push(i);
}
const sTxId = sTxMetadata.getId();
const sTxPrevalh = util_1.getAlh(sTxMetadata);
const sTxTs = sTxMetadata.getTs();
const sTxBlTxId = sTxMetadata.getBltxid();
const sTxBlRoot = sTxMetadata.getBlroot_asU8();
return new Tx(entries, sTxId, sTxPrevalh, sTxTs, sTxBlTxId, sTxBlRoot);
};
exports.proofTx = (tx, key) => {
let kindex;
for (let [k, v] of tx.entries.entries()) {
if (util_1.equalArray(v.key, key)) {
kindex = k;
}
}
if (kindex === undefined) {
console.error('Error finding kindex');
}
else {
return tx.htree.inclusionProof(kindex);
}
};
//# sourceMappingURL=tx.js.map