UNPKG

@gamechanger-finance/unimatrix

Version:

Unimatrix Sync is a decentralized, privacy preserving, transaction witness sharing and pairing solution for multisignatures or deferred signatures. It was originally created for GameChanger Wallet to improve it's multisignature user experience and boost t

661 lines (660 loc) 34.8 kB
"use strict"; /** * A set of helpers to standardize a basic data exchange protocol * to share **Cardano** transactions and signatures using **Unimatrix** * @module unimatrix-sync-cardano * * @remarks * * A module for **Cardano** built on top of the **Unimatrix** library, with custom validators, encryption functions and data types fully compatible with [cardano-serialization-lib](https://github.com/Emurgo/cardano-serialization-lib). * * Main use case are multi-signature or deferred-signature scenarios involving wallets, dapps and services on the **Cardano Blockchain** * * **Validators and their matching data types**: * - **Items** * - `cardano.vkWitnessHex` : transaction key witness (signature) in hexadecimal encoding * - `cardano.txHex`: transaction CBOR structure in hexadecimal encoding * - **Announcements** * - `cardano.TxHashHexList` : list of transaction hash strings */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.setTxHashes = exports.getTxHashes = exports.onTxHashes = exports.genTxHashesKey = exports.setTxHex = exports.getTxHex = exports.onTxHex = exports.genTxHexKey = exports.setVkWitnessHex = exports.getVkWitnessHex = exports.onVkWitnessHex = exports.genVkWitnessHexKey = exports.cardanoValidatorsFactory = exports.verifyVkWitnessHex = exports.decryptDataFactory = exports.encryptDataFactory = exports.genUnimatrixIdFromTxHashes = exports.randomBytes = exports.NONCE_SIZE = exports.SALT_SIZE = void 0; var unimatrix_1 = require("../unimatrix"); var common_1 = require("../common"); var sha512_1 = __importDefault(require("crypto-js/sha512")); var crypto_1 = __importDefault(require("crypto")); //export const defaultTimeoutMs =1000*5;//5 seconds /** * Salt size for `encryptDataFactory` */ exports.SALT_SIZE = 32; /** * Nonce size for `encryptDataFactory` */ exports.NONCE_SIZE = 12; function randomBytes(size) { var _a, _b; if (typeof (crypto_1.default === null || crypto_1.default === void 0 ? void 0 : crypto_1.default.getRandomValues) !== 'function') { if (typeof ((_a = window === null || window === void 0 ? void 0 : window.crypto) === null || _a === void 0 ? void 0 : _a.getRandomValues) === 'function') return Buffer.from(window.crypto.getRandomValues(new Uint8Array(size))); if (typeof ((_b = global === null || global === void 0 ? void 0 : global.crypto) === null || _b === void 0 ? void 0 : _b.getRandomValues) === 'function') return Buffer.from(global.crypto.getRandomValues(new Uint8Array(size))); } return Buffer.from(crypto_1.default.getRandomValues(new Uint8Array(size))); } exports.randomBytes = randomBytes; /** * Helper function that generates a hash string out of a list of transaction hash strings. * * Some clients like **GameChanger Wallet** uses it for establishing private channels. * The generated strings are used as private unique channel IDs based on built transactions * that are not yet signed nor submitted to the blockchain, meaning that these IDs are only known by * this transaction builder user and whomever this user shares them with. * * @param txHashes */ var genUnimatrixIdFromTxHashes = function (txHashes) { var data = txHashes .filter(function (x) { return !!(x || "").trim(); }) .sort() .join('-'); return (0, sha512_1.default)(data).toString(); }; exports.genUnimatrixIdFromTxHashes = genUnimatrixIdFromTxHashes; /** * Function that generates an `UnimatrixEncryptFn` using **cardano-serialization-lib**'s `encrypt_with_password()` underneath * @param CSL */ var encryptDataFactory = function (CSL) { return function (args) { try { if (!CSL) throw new Error("Missing CSL"); if (args.path.length <= 0 || !args.id || !args.validator) throw new Error("Missing parts"); if (args.path.some(function (x) { return !x.trim(); })) throw new Error("Path items cannot be empty"); var _CardanoWasm = CSL; var json = (0, common_1.JSONStringify)(args.store.file); var idHash = (0, sha512_1.default)(args.id).toString(); var publicDataHex = Buffer.from((0, common_1.JSONStringify)({ updatedAt: args.store.updatedAt })).toString('hex'); var password = "".concat(idHash, ":").concat(publicDataHex, ":").concat(args.validator, ":").concat(args.path.join('/')); var salt = randomBytes(exports.SALT_SIZE); var nonce = randomBytes(exports.NONCE_SIZE); var cypherHex = _CardanoWasm.encrypt_with_password(Buffer.from(password).toString('hex'), Buffer.from(salt).toString('hex'), Buffer.from(nonce).toString('hex'), Buffer.from(json).toString('hex')); var file = "".concat(publicDataHex, ":").concat(cypherHex); return file; } catch (err) { throw new Error("Unimatrix encryption error: ".concat(err)); } }; }; exports.encryptDataFactory = encryptDataFactory; /** * Function that generates an `UnimatrixDecryptFn` using **cardano-serialization-lib**'s `decrypt_with_password()` underneath * @param CSL */ var decryptDataFactory = function (CSL) { return function (args) { try { if (!CSL) throw new Error("Missing CSL"); if (args.path.length <= 0 || !args.id || !args.validator) throw new Error("Missing parts"); if (args.path.some(function (x) { return !x.trim(); })) throw new Error("Path items cannot be empty"); if (typeof args.store !== "string") throw new Error("Invalid storage"); var _a = (args.store || "").split(':'), publicDataHex = _a[0], cypherHex = _a[1]; if (!cypherHex) throw new Error("Missing encrypted data"); if (!publicDataHex) throw new Error("Missing public data"); var idHash = (0, sha512_1.default)(args.id).toString(); var password = "".concat(idHash, ":").concat(publicDataHex, ":").concat(args.validator, ":").concat(args.path.join('/')); var jsonHex = CSL.decrypt_with_password(Buffer.from(password).toString('hex'), cypherHex); var file = JSON.parse(Buffer.from(jsonHex, "hex").toString()); var updatedAt = (JSON.parse(Buffer.from(publicDataHex, "hex").toString()) || {}).updatedAt; return { file: file, updatedAt: updatedAt }; } catch (err) { throw new Error("Unimatrix decryption error: ".concat(err)); } }; }; exports.decryptDataFactory = decryptDataFactory; /** * Verifies a transaction witness with hexadecimal arguments provided. * @param args.CSL pass cardano serialization lib * @param args.txHash transaction hash hex * @param args.vkWitnessHex witness hex * @param args.vkHash (optional) public key hash hex. when provided will check vkHash against pub key */ var verifyVkWitnessHex = function (args) { if (common_1.logger) common_1.logger === null || common_1.logger === void 0 ? void 0 : common_1.logger.log("verifyVkWitnessHex():", { args: args }); try { var _CardanoWasm = args.CSL; if (!args.CSL) return "missing CSL"; if (!args.txHash) return "missing transaction hash"; if (typeof args.txHash !== "string") return "invalid transaction hash"; if (!args.vkWitnessHex) return "missing transaction witness"; if (typeof args.vkWitnessHex !== "string") return "invalid transaction witness"; var txHashObj = _CardanoWasm.TransactionHash.from_hex(args.txHash); var vkWitnessObj = _CardanoWasm.Vkeywitness.from_hex(args.vkWitnessHex); var pubKeyObj = vkWitnessObj.vkey().public_key(); var keyHashObj = pubKeyObj.hash(); var keyHashHex = keyHashObj.to_hex(); if (args.vkHash && args.vkHash !== keyHashHex) return "unexpected witness ".concat(keyHashHex); var signatureObj = vkWitnessObj.signature(); if (pubKeyObj.verify(txHashObj.to_bytes(), signatureObj) != true) return "invalid witness"; return true; } catch (err) { if (common_1.logger) common_1.logger.warn("verifyVkWitnessHex(): unexpected error. ".concat(err), { txHash: args === null || args === void 0 ? void 0 : args.txHash, vkHash: args === null || args === void 0 ? void 0 : args.vkHash, vkWitnessHex: args === null || args === void 0 ? void 0 : args.vkWitnessHex, }); } return "invalid data provided"; }; exports.verifyVkWitnessHex = verifyVkWitnessHex; /** * Function that generates the map of validators for **Cardano** (`UnimatrixValidatorMap`), using **cardano-serialization-lib**'s classes underneath. * * @param CSL */ var cardanoValidatorsFactory = function (CSL) { return ({ 'cardano.TxHashHexList': function (args) { var _a, _b, _c; if (((_a = args === null || args === void 0 ? void 0 : args.store.file) === null || _a === void 0 ? void 0 : _a.error) && typeof ((_b = args === null || args === void 0 ? void 0 : args.store.file) === null || _b === void 0 ? void 0 : _b.error) !== "string") return "Invalid error type"; var txHashes = ((_c = args === null || args === void 0 ? void 0 : args.store.file) === null || _c === void 0 ? void 0 : _c.data) || []; var areValid = txHashes.every(function (txHash) { try { if (CSL.TransactionHash.from_hex(txHash).to_hex() === txHash) return true; } catch (err) { if (common_1.logger) common_1.logger.warn("[UNIMATRIX] Validator '".concat(args === null || args === void 0 ? void 0 : args.validator, "' Error: ").concat(err), args); } return false; }); if (areValid !== true) return "Invalid transaction hash"; return true; }, 'cardano.vkWitnessHex': function (args) { var _a, _b, _c; if (((_a = args === null || args === void 0 ? void 0 : args.store.file) === null || _a === void 0 ? void 0 : _a.error) && typeof ((_b = args === null || args === void 0 ? void 0 : args.store.file) === null || _b === void 0 ? void 0 : _b.error) !== "string") return "Invalid error type"; try { var _d = args.path || [], dltTag = _d[0], networkTag = _d[1], txHash = _d[2], vkHash = _d[3]; return (0, exports.verifyVkWitnessHex)({ CSL: CSL, txHash: txHash, vkHash: vkHash, vkWitnessHex: (_c = args === null || args === void 0 ? void 0 : args.store.file) === null || _c === void 0 ? void 0 : _c.data }); } catch (err) { if (common_1.logger) common_1.logger.warn("[UNIMATRIX] Validator '".concat(args === null || args === void 0 ? void 0 : args.validator, "' Error: ").concat(err), { args: args }); } return "Invalid transaction witness"; }, 'cardano.txHex': function (args) { var _a; var _b, _c, _d, _e; if (((_b = args === null || args === void 0 ? void 0 : args.store.file) === null || _b === void 0 ? void 0 : _b.error) && typeof ((_c = args === null || args === void 0 ? void 0 : args.store.file) === null || _c === void 0 ? void 0 : _c.error) !== "string") return "Invalid error type"; try { var txHex = ((_d = args === null || args === void 0 ? void 0 : args.store.file) === null || _d === void 0 ? void 0 : _d.data) || ""; var _f = args.path || [], dltTag = _f[0], networkTag = _f[1], txHash = _f[2]; var txObj = CSL.FixedTransaction.from_hex(txHex); //.from_bytes(fromHex(txHex)) var txBodyObj = txObj.body(); //args.CSL.TransactionBody.from_bytes(fromHex(txHex)) var networkId = (_e = txBodyObj.network_id()) === null || _e === void 0 ? void 0 : _e.kind(); if (networkId !== undefined) { var networkTags = (_a = {}, _a[CSL.NetworkInfo.mainnet().network_id()] = 'mainnet', _a[CSL.NetworkInfo.testnet_preprod().network_id()] = 'preprod', _a); var realNetworkTag = networkTags[networkId]; if (realNetworkTag !== networkTag) return "This is a '".concat(realNetworkTag || "unknown network", "' transaction. Wrong network"); } var txHashObj = txObj.transaction_hash(); //CSL.hash_transaction(txBodyObj); var realTxHash = txHashObj.to_hex(); if (realTxHash !== txHash) return "Transaction hash mismatch"; return true; } catch (err) { if (common_1.logger) common_1.logger.warn("[UNIMATRIX] Validator '".concat(args === null || args === void 0 ? void 0 : args.validator, "' Error: ").concat(err), args); } return "Invalid transaction"; }, }); }; exports.cardanoValidatorsFactory = cardanoValidatorsFactory; ///////////////////////////////////////////// //////////// vkWitnessHex //////////////// ///////////////////////////////////////////// /** * Helper that generates the key for the GunDB key-value structure used for storing a transaction key witness * * @param args */ var genVkWitnessHexKey = function (args) { return (0, unimatrix_1.genDataKey)({ id: args.id, validator: 'cardano.vkWitnessHex', path: [args.dltTag, args.networkTag, args.txHash, args.vkHash], }); }; exports.genVkWitnessHexKey = genVkWitnessHexKey; /** * Listener promise that calls the `cb` callback every time it receives a specific incoming transaction key witness (in hexadecimal encoding) on a specific channel * * @param args */ var onVkWitnessHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.onData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, //specific id: args.id, validator: 'cardano.vkWitnessHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash, args.vkHash], encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), on: function (_a) { var _b, _c, _d; var store = _a.store, validationError = _a.validationError, timeoutError = _a.timeoutError, userError = _a.userError, node = _a.node, stop = _a.stop; var vkWitnessHex = (!!((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) && typeof ((_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data) === "string") ? (_d = store === null || store === void 0 ? void 0 : store.file) === null || _d === void 0 ? void 0 : _d.data : undefined; return args.cb({ vkWitnessHex: vkWitnessHex, store: store, validationError: validationError, timeoutError: timeoutError, userError: userError, node: node, stop: stop }); }, })]; }); }); }; exports.onVkWitnessHex = onVkWitnessHex; /** * Getter promise that returns a specific transaction key witness (in hexadecimal encoding) on a specific channel * * @param args */ var getVkWitnessHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.getData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, throwValidationErrors: !false, throwUserErrors: !false, throwTimeoutErrors: !false, //specific id: args.id, validator: 'cardano.vkWitnessHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash, args.vkHash], encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c; var vkWitnessHex = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && typeof ((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) === "string") ? (_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data : undefined; return { store: store, vkWitnessHex: vkWitnessHex, }; })]; }); }); }; exports.getVkWitnessHex = getVkWitnessHex; /** * Setter promise that shares a specific transaction key witness (in hexadecimal encoding) on a specific channel * * @param args */ var setVkWitnessHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.setData)({ //basic db: args.db, checkByFetching: false, //specific id: args.id, validator: 'cardano.vkWitnessHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash, args.vkHash], store: { file: !!args.error ? { data: undefined, error: args.error } : { data: args.vkWitnessHex, error: undefined }, updatedAt: Date.now(), }, encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c; var vkWitnessHex = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && typeof ((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) === "string") ? (_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data : undefined; return { store: store, vkWitnessHex: vkWitnessHex, }; })]; }); }); }; exports.setVkWitnessHex = setVkWitnessHex; //////////// txHex //////////////// /** * Helper that generates the key for the GunDB key-value structure used for storing a transaction CBOR structure * * @param args */ var genTxHexKey = function (args) { return (0, unimatrix_1.genDataKey)({ id: args.id, validator: 'cardano.txHex', path: [args.dltTag, args.networkTag, args.txHash], }); }; exports.genTxHexKey = genTxHexKey; /** * Listener promise that calls the `cb` callback every time it receives a specific incoming transaction CBOR structure (in hexadecimal encoding) on a specific channel * * @param args */ var onTxHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.onData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, //specific id: args.id, validator: 'cardano.txHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash], encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), on: function (_a) { var _b, _c, _d; var store = _a.store, validationError = _a.validationError, timeoutError = _a.timeoutError, userError = _a.userError, node = _a.node, stop = _a.stop; var txHex = (!!((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) && typeof ((_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data) === "string") ? (_d = store === null || store === void 0 ? void 0 : store.file) === null || _d === void 0 ? void 0 : _d.data : undefined; return args.cb({ txHex: txHex, store: store, validationError: validationError, timeoutError: timeoutError, userError: userError, node: node, stop: stop }); }, })]; }); }); }; exports.onTxHex = onTxHex; /** * Getter promise that returns a specific transaction CBOR structure (in hexadecimal encoding) on a specific channel * * @param args */ var getTxHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.getData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, throwValidationErrors: !false, throwUserErrors: !false, throwTimeoutErrors: !false, //specific id: args.id, validator: 'cardano.txHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash], encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c; var txHex = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && typeof ((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) === "string") ? (_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data : undefined; return { store: store, txHex: txHex, }; })]; }); }); }; exports.getTxHex = getTxHex; /** * Setter promise that shares a specific transaction CBOR structure (in hexadecimal encoding) on a specific channel * * @param args */ var setTxHex = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.setData)({ //basic db: args.db, checkByFetching: false, //specific id: args.id, validator: 'cardano.txHex', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: [args.dltTag, args.networkTag, args.txHash], store: { file: !!args.error ? { data: undefined, error: args.error } : { data: args.txHex, error: undefined }, updatedAt: Date.now(), }, encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c; var txHex = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && typeof ((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) === "string") ? (_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data : undefined; return { store: store, txHex: txHex, }; })]; }); }); }; exports.setTxHex = setTxHex; //////////// TxHashHexList //////////////// /** * Helper that generates the key for the GunDB key-value structure used for storing a list of transaction hashes * * @param args */ var genTxHashesKey = function (args) { return (0, unimatrix_1.genDataKey)({ id: args.id, validator: 'cardano.TxHashHexList', path: __spreadArray([args.dltTag, args.networkTag], (args === null || args === void 0 ? void 0 : args.subPath) || [], true), }); }; exports.genTxHashesKey = genTxHashesKey; /** * Listener promise that calls the `cb` callback every time it receives any incoming announced list of transaction hashes on a specific channel * * @param args */ var onTxHashes = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.onData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, //specific id: args.id, validator: 'cardano.TxHashHexList', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: __spreadArray([args.dltTag, args.networkTag], args.subPath || [], true), encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), on: function (_a) { var _b, _c, _d, _e; var store = _a.store, validationError = _a.validationError, timeoutError = _a.timeoutError, userError = _a.userError, node = _a.node, stop = _a.stop; var txHashes = (!!((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) && Array.isArray((_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data) && ((_d = store === null || store === void 0 ? void 0 : store.file) === null || _d === void 0 ? void 0 : _d.data).every(function (x) { return (!!x && typeof x === "string"); })) ? (_e = store === null || store === void 0 ? void 0 : store.file) === null || _e === void 0 ? void 0 : _e.data : undefined; return args.cb({ txHashes: txHashes, store: store, validationError: validationError, timeoutError: timeoutError, userError: userError, node: node, stop: stop }); }, })]; }); }); }; exports.onTxHashes = onTxHashes; /** * Getter promise that returns any announced list of transaction hashes on a specific channel * * @param args */ var getTxHashes = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.getData)({ //basic db: args.db, timeout: args === null || args === void 0 ? void 0 : args.timeout, throwValidationErrors: !false, throwUserErrors: !false, throwTimeoutErrors: !false, //specific id: args.id, validator: 'cardano.TxHashHexList', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: __spreadArray([args.dltTag, args.networkTag], args.subPath || [], true), encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c, _d; var txHashes = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && Array.isArray((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) && ((_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data).every(function (x) { return (!!x && typeof x === "string"); })) ? (_d = store === null || store === void 0 ? void 0 : store.file) === null || _d === void 0 ? void 0 : _d.data : undefined; return { store: store, txHashes: txHashes, }; })]; }); }); }; exports.getTxHashes = getTxHashes; /** * Setter promise that shares the announcement of a list of transaction hashes on a specific channel * * @param args */ var setTxHashes = function (args) { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, (0, unimatrix_1.setData)({ //basic db: args.db, checkByFetching: false, //specific id: args.id, validator: 'cardano.TxHashHexList', validators: (0, exports.cardanoValidatorsFactory)(args.CSL), path: __spreadArray([args.dltTag, args.networkTag], args.subPath || [], true), store: { file: !!args.error ? { data: undefined, error: args.error } : { data: args.txHashes, error: undefined }, updatedAt: Date.now(), }, encryptData: (0, exports.encryptDataFactory)(args.CSL), decryptData: (0, exports.decryptDataFactory)(args.CSL), }) .then(function (store) { var _a, _b, _c, _d; var txHashes = (!!((_a = store === null || store === void 0 ? void 0 : store.file) === null || _a === void 0 ? void 0 : _a.data) && Array.isArray((_b = store === null || store === void 0 ? void 0 : store.file) === null || _b === void 0 ? void 0 : _b.data) && ((_c = store === null || store === void 0 ? void 0 : store.file) === null || _c === void 0 ? void 0 : _c.data).every(function (x) { return (!!x && typeof x === "string"); })) ? (_d = store === null || store === void 0 ? void 0 : store.file) === null || _d === void 0 ? void 0 : _d.data : undefined; return { store: store, txHashes: txHashes, }; })]; }); }); }; exports.setTxHashes = setTxHashes;