@funded-labs/plug-controller
Version:
Internet Computer Plug wallet's controller
27 lines (26 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.recursiveFindPrincipals = exports.replacePrincipalsForICNS = exports.getMappingValue = void 0;
const constants_1 = require("../constants");
const getMappingValue = (pid, mappings) => ({ principal: pid, icns: mappings[pid] });
exports.getMappingValue = getMappingValue;
const replacePrincipalsForICNS = (tx, mappings) => {
const parsedTx = Object.assign({}, tx);
const { from: detailFrom, to } = (parsedTx === null || parsedTx === void 0 ? void 0 : parsedTx.details) || {};
const from = detailFrom || (parsedTx === null || parsedTx === void 0 ? void 0 : parsedTx.caller);
parsedTx.details = Object.assign(Object.assign({}, parsedTx.details), { from: (0, exports.getMappingValue)(from, mappings), to: (0, exports.getMappingValue)(to, mappings) });
return parsedTx;
};
exports.replacePrincipalsForICNS = replacePrincipalsForICNS;
const recursiveFindPrincipals = (transactions) => {
return transactions.reduce((acc, tx) => {
const copy = [...acc];
const { from, to } = tx.details || {};
if (constants_1.PRINCIPAL_REGEX.test(from))
copy.push(from);
if (constants_1.PRINCIPAL_REGEX.test(to))
copy.push(to);
return [...new Set(copy)];
}, []);
};
exports.recursiveFindPrincipals = recursiveFindPrincipals;