219 lines
7.9 KiB
JavaScript
219 lines
7.9 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Evaluates configurable approval policies (N-of-M / auto-execute).
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.policyMatches = policyMatches;
|
|
exports.evaluate = evaluate;
|
|
exports.quorumSatisfied = quorumSatisfied;
|
|
exports.countQuorumProgress = countQuorumProgress;
|
|
exports.isQuorumComplete = isQuorumComplete;
|
|
exports.voterFillsOpenSlot = voterFillsOpenSlot;
|
|
exports.annotateVoterRolesForQuorum = annotateVoterRolesForQuorum;
|
|
const approvalPolicyRepository = __importStar(require("../repositories/approvalPolicyRepository"));
|
|
const FAIL_CLOSED = {
|
|
mode: 'require_approvals',
|
|
quorum: [{ role: 'super_admin', count: 1 }],
|
|
forbid_self_approve: true,
|
|
require_distinct_users: true,
|
|
};
|
|
function kindMatches(rule, actual) {
|
|
if (rule === 'any')
|
|
return true;
|
|
if (!actual || actual === 'any')
|
|
return true;
|
|
return rule === actual;
|
|
}
|
|
function amountInRange(amountRaw, minRaw, maxRaw) {
|
|
if (amountRaw == null || amountRaw === '') {
|
|
return minRaw == null && maxRaw == null;
|
|
}
|
|
let amount;
|
|
try {
|
|
amount = BigInt(amountRaw);
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
if (minRaw != null && minRaw !== '') {
|
|
try {
|
|
if (amount < BigInt(minRaw))
|
|
return false;
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
}
|
|
if (maxRaw != null && maxRaw !== '') {
|
|
try {
|
|
if (amount > BigInt(maxRaw))
|
|
return false;
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
function policyMatches(policy, input) {
|
|
if (!policy.enabled)
|
|
return false;
|
|
if (policy.operation_type && policy.operation_type !== input.operationType)
|
|
return false;
|
|
if (policy.wallet_type_id != null && policy.wallet_type_id !== input.walletTypeId)
|
|
return false;
|
|
if (policy.network && policy.network !== input.network)
|
|
return false;
|
|
if (!kindMatches(policy.source_wallet_kind, input.sourceKind))
|
|
return false;
|
|
if (!kindMatches(policy.dest_wallet_kind, input.destKind))
|
|
return false;
|
|
if (!amountInRange(input.amountRaw, policy.amount_min_raw, policy.amount_max_raw))
|
|
return false;
|
|
return true;
|
|
}
|
|
async function evaluate(input) {
|
|
await approvalPolicyRepository.ensureDefaultPoliciesSeeded();
|
|
const policies = await approvalPolicyRepository.listEnabledPolicies();
|
|
for (const policy of policies) {
|
|
if (policyMatches(policy, input)) {
|
|
return {
|
|
policy,
|
|
decision: policy.decision,
|
|
matchedBy: 'policy',
|
|
};
|
|
}
|
|
}
|
|
return {
|
|
policy: null,
|
|
decision: FAIL_CLOSED,
|
|
matchedBy: 'fail_closed_default',
|
|
};
|
|
}
|
|
/** True if approve votes satisfy every quorum slot. */
|
|
function quorumSatisfied(quorum, approveVotes, voterHasRole, voterHasPermission) {
|
|
if (!quorum.length)
|
|
return false;
|
|
const used = new Set();
|
|
for (const slot of quorum) {
|
|
let filled = 0;
|
|
for (const vote of approveVotes) {
|
|
if (used.has(vote.voter_id))
|
|
continue;
|
|
const roleOk = slot.role ? voterHasRole(vote.voter_id, slot.role) || vote.voter_roles.includes(slot.role) : false;
|
|
const permOk = slot.permission ? voterHasPermission(vote.voter_id, slot.permission) : false;
|
|
// Prefer roles recorded on the vote; async permission checked by caller via cache map
|
|
const ok = slot.role
|
|
? vote.voter_roles.includes(slot.role) || roleOk
|
|
: permOk;
|
|
if (!ok && slot.permission) {
|
|
// permission checked externally — caller passes precomputed eligibility
|
|
continue;
|
|
}
|
|
if (slot.role && !vote.voter_roles.includes(slot.role))
|
|
continue;
|
|
if (!slot.role && slot.permission) {
|
|
// handled below with permission set on vote metadata — skip if only permission and not in roles
|
|
continue;
|
|
}
|
|
filled += 1;
|
|
used.add(vote.voter_id);
|
|
if (filled >= slot.count)
|
|
break;
|
|
}
|
|
if (filled < slot.count)
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
/**
|
|
* Count how many approve votes fill each slot (role-based; permission slots need prefilter).
|
|
*/
|
|
function countQuorumProgress(quorum, approveVotes) {
|
|
const used = new Set();
|
|
const onlyApproves = approveVotes.filter((v) => v.decision === 'approve');
|
|
const slots = quorum.map((slot) => {
|
|
let filled = 0;
|
|
for (const vote of onlyApproves) {
|
|
if (used.has(vote.voter_id))
|
|
continue;
|
|
const matchesRole = slot.role ? vote.voter_roles.includes(slot.role) : false;
|
|
const matchesPerm = slot.permission && !slot.role
|
|
? vote.voter_roles.includes(`perm:${slot.permission}`) ||
|
|
vote.voter_roles.includes(slot.permission)
|
|
: false;
|
|
if (!matchesRole && !matchesPerm)
|
|
continue;
|
|
filled += 1;
|
|
used.add(vote.voter_id);
|
|
if (filled >= slot.count)
|
|
break;
|
|
}
|
|
return { slot, filled, needed: slot.count };
|
|
});
|
|
const required = slots.reduce((s, x) => s + x.needed, 0);
|
|
const approved = slots.reduce((s, x) => s + Math.min(x.filled, x.needed), 0);
|
|
return { required, approved, slots };
|
|
}
|
|
function isQuorumComplete(quorum, approveVotes) {
|
|
const progress = countQuorumProgress(quorum, approveVotes);
|
|
return progress.slots.every((s) => s.filled >= s.needed);
|
|
}
|
|
/** Whether voter can contribute to any unfilled quorum slot. */
|
|
function voterFillsOpenSlot(quorum, approveVotes, voterRoles, voterPermissions) {
|
|
const progress = countQuorumProgress(quorum, approveVotes);
|
|
for (const { slot, filled, needed } of progress.slots) {
|
|
if (filled >= needed)
|
|
continue;
|
|
if (slot.role && voterRoles.includes(slot.role))
|
|
return true;
|
|
if (slot.permission && voterPermissions.includes(slot.permission))
|
|
return true;
|
|
if (slot.permission && voterPermissions.includes(`admin.${slot.permission}`))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
function annotateVoterRolesForQuorum(roles, permissions, quorum) {
|
|
const out = [...roles];
|
|
for (const slot of quorum) {
|
|
if (slot.permission && permissions.includes(slot.permission)) {
|
|
out.push(`perm:${slot.permission}`);
|
|
}
|
|
}
|
|
return Array.from(new Set(out));
|
|
}
|
|
//# sourceMappingURL=approvalPolicyEngine.js.map
|