// Plans how to move a SanarioMultisig from its current owners / threshold to a // new M-of-N configuration, as a sequence of owner-management calls that the // vault executes on itself (each one needs the quorum in force at that time). // // Pure (no viem / browser imports) so it can be unit-tested against the // contract. Addresses are compared case-insensitively. export type SignerOp = | { fn: 'replaceOwner'; args: [oldOwner: string, newOwner: string] } | { fn: 'addOwner'; args: [owner: string, threshold: number] } | { fn: 'removeOwner'; args: [owner: string, threshold: number] } | { fn: 'changeThreshold'; args: [threshold: number] }; export const MAX_OWNERS = 50; const ADDR = /^0x[0-9a-fA-F]{40}$/; const ZERO = '0x0000000000000000000000000000000000000000'; export function planSignerChange(current: string[], currentThreshold: number, target: string[], newThreshold: number): SignerOp[] { const lc = (a: string) => a.toLowerCase(); const seen = new Set(); for (const a of target) { if (!ADDR.test(a)) throw new Error(`${a || '(empty)'} is not a valid address.`); if (lc(a) === ZERO) throw new Error('The zero address cannot be an owner.'); if (seen.has(lc(a))) throw new Error(`${a} is listed twice.`); seen.add(lc(a)); } if (target.length < 1) throw new Error('Keep at least one owner.'); if (target.length > MAX_OWNERS) throw new Error(`At most ${MAX_OWNERS} owners.`); if (!Number.isInteger(newThreshold) || newThreshold < 1 || newThreshold > target.length) { throw new Error(`Threshold must be between 1 and ${target.length}.`); } const cur = new Set(current.map(lc)); const adds = target.filter((a) => !cur.has(lc(a))); const removes = current.filter((a) => !seen.has(lc(a))); const ops: SignerOp[] = []; let count = current.length; let t = currentThreshold; // 1. swap removed owners for new ones one-for-one (count and quorum unchanged) const pairs = Math.min(adds.length, removes.length); for (let i = 0; i < pairs; i++) ops.push({ fn: 'replaceOwner', args: [removes[i], adds[i]] }); // 2. extra additions keep the current quorum for (const a of adds.slice(pairs)) { count++; ops.push({ fn: 'addOwner', args: [a, t] }); } // 3. extra removals lower the quorum only if it would exceed the owner count for (const r of removes.slice(pairs)) { count--; t = Math.min(t, count); ops.push({ fn: 'removeOwner', args: [r, t] }); } // 4. the final threshold rides on the last add/remove, or gets its own call const last = ops[ops.length - 1]; if (last && (last.fn === 'addOwner' || last.fn === 'removeOwner')) { last.args[1] = newThreshold; } else if (newThreshold !== t) { ops.push({ fn: 'changeThreshold', args: [newThreshold] }); } return ops; } /** Owner set and threshold after applying the plan (for previews and tests). */ export function applyPlan(current: string[], threshold: number, ops: SignerOp[]): { owners: string[]; threshold: number } { let owners = [...current]; const eq = (a: string) => (b: string) => a.toLowerCase() === b.toLowerCase(); for (const op of ops) { if (op.fn === 'replaceOwner') owners = owners.map((o) => (eq(op.args[0])(o) ? op.args[1] : o)); else if (op.fn === 'addOwner') { owners.push(op.args[0]); threshold = op.args[1]; } else if (op.fn === 'removeOwner') { owners = owners.filter((o) => !eq(op.args[0])(o)); threshold = op.args[1]; } else threshold = op.args[0]; } return { owners, threshold }; } const short = (a: string) => `${a.slice(0, 6)}…${a.slice(-4)}`; export function describeOp(op: SignerOp, name: (a: string) => string = short): string { switch (op.fn) { case 'replaceOwner': return `Replace ${name(op.args[0])} with ${name(op.args[1])}`; case 'addOwner': return `Add ${name(op.args[0])} (threshold ${op.args[1]})`; case 'removeOwner': return `Remove ${name(op.args[0])} (threshold ${op.args[1]})`; case 'changeThreshold': return `Change threshold to ${op.args[0]}`; } }