|
| 1 | +export type SignatureContextDetail = { |
| 2 | + label: string; |
| 3 | + value: string; |
| 4 | +}; |
| 5 | + |
| 6 | +export type SignatureContext = { |
| 7 | + summary: string; |
| 8 | + details?: SignatureContextDetail[]; |
| 9 | +}; |
| 10 | + |
| 11 | +type TxOutputAmount = { |
| 12 | + unit?: unknown; |
| 13 | + quantity?: unknown; |
| 14 | +}; |
| 15 | + |
| 16 | +type TxOutput = { |
| 17 | + address?: unknown; |
| 18 | + amount?: unknown; |
| 19 | +}; |
| 20 | + |
| 21 | +type ProxyVote = { |
| 22 | + proposalId?: unknown; |
| 23 | + voteKind?: unknown; |
| 24 | +}; |
| 25 | + |
| 26 | +function asRecord(value: unknown): Record<string, unknown> | null { |
| 27 | + return typeof value === "object" && value !== null |
| 28 | + ? (value as Record<string, unknown>) |
| 29 | + : null; |
| 30 | +} |
| 31 | + |
| 32 | +function parseJsonRecord(value: unknown): Record<string, unknown> | null { |
| 33 | + if (typeof value === "string") { |
| 34 | + try { |
| 35 | + return asRecord(JSON.parse(value)); |
| 36 | + } catch { |
| 37 | + return null; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return asRecord(value); |
| 42 | +} |
| 43 | + |
| 44 | +export function maskAddress(address: string): string { |
| 45 | + const trimmed = address.trim(); |
| 46 | + if (trimmed.length <= 12) return trimmed; |
| 47 | + return `${trimmed.slice(0, 8)}...${trimmed.slice(-4)}`; |
| 48 | +} |
| 49 | + |
| 50 | +function formatQuantity(unit: string, quantity: string): string { |
| 51 | + if (unit === "lovelace") { |
| 52 | + try { |
| 53 | + const lovelace = BigInt(quantity); |
| 54 | + const whole = lovelace / 1_000_000n; |
| 55 | + const fraction = lovelace % 1_000_000n; |
| 56 | + const fractionText = fraction.toString().padStart(6, "0").replace(/0+$/, ""); |
| 57 | + return `${fractionText ? `${whole.toString()}.${fractionText}` : whole.toString()} ADA`; |
| 58 | + } catch { |
| 59 | + return `${quantity} lovelace`; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return `${quantity} ${maskAssetUnit(unit)}`; |
| 64 | +} |
| 65 | + |
| 66 | +function maskAssetUnit(unit: string): string { |
| 67 | + return unit.length > 18 ? `${unit.slice(0, 10)}...${unit.slice(-6)}` : unit; |
| 68 | +} |
| 69 | + |
| 70 | +function firstAmount(amount: unknown): { unit: string; quantity: string } | null { |
| 71 | + if (!Array.isArray(amount)) return null; |
| 72 | + |
| 73 | + for (const item of amount as TxOutputAmount[]) { |
| 74 | + if ( |
| 75 | + typeof item?.unit === "string" && |
| 76 | + typeof item.quantity === "string" && |
| 77 | + item.quantity.trim().length > 0 |
| 78 | + ) { |
| 79 | + return { unit: item.unit, quantity: item.quantity }; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return null; |
| 84 | +} |
| 85 | + |
| 86 | +function summarizeOutputs(outputs: unknown): SignatureContext | null { |
| 87 | + if (!Array.isArray(outputs) || outputs.length === 0) return null; |
| 88 | + |
| 89 | + const output = (outputs as TxOutput[]).find( |
| 90 | + (candidate) => |
| 91 | + typeof candidate?.address === "string" && firstAmount(candidate.amount), |
| 92 | + ); |
| 93 | + if (!output || typeof output.address !== "string") return null; |
| 94 | + |
| 95 | + const amount = firstAmount(output.amount); |
| 96 | + if (!amount) return null; |
| 97 | + |
| 98 | + const outputCount = outputs.length; |
| 99 | + return { |
| 100 | + summary: `Send ${formatQuantity(amount.unit, amount.quantity)} to ${maskAddress(output.address)}`, |
| 101 | + details: |
| 102 | + outputCount > 1 |
| 103 | + ? [{ label: "Outputs", value: `${outputCount} total outputs` }] |
| 104 | + : undefined, |
| 105 | + }; |
| 106 | +} |
| 107 | + |
| 108 | +function summarizeProxyBot(proxyBot: Record<string, unknown>): SignatureContext | null { |
| 109 | + const kind = proxyBot.kind; |
| 110 | + |
| 111 | + if (kind === "proxyVote" && Array.isArray(proxyBot.votes)) { |
| 112 | + const vote = (proxyBot.votes as ProxyVote[]).find( |
| 113 | + (candidate) => |
| 114 | + typeof candidate?.proposalId === "string" || |
| 115 | + typeof candidate?.voteKind === "string", |
| 116 | + ); |
| 117 | + const voteCount = proxyBot.votes.length; |
| 118 | + const choice = typeof vote?.voteKind === "string" ? vote.voteKind : "vote"; |
| 119 | + const proposal = |
| 120 | + typeof vote?.proposalId === "string" ? maskAddress(vote.proposalId) : null; |
| 121 | + |
| 122 | + return { |
| 123 | + summary: `Governance ${choice} vote${proposal ? ` on ${proposal}` : ""}`, |
| 124 | + details: |
| 125 | + voteCount > 1 ? [{ label: "Votes", value: `${voteCount} proposals` }] : undefined, |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + if (kind === "proxyDRepCertificate") { |
| 130 | + const action = |
| 131 | + typeof proxyBot.action === "string" && proxyBot.action.trim() |
| 132 | + ? proxyBot.action.trim() |
| 133 | + : "update"; |
| 134 | + const dRepId = |
| 135 | + typeof proxyBot.dRepId === "string" && proxyBot.dRepId.trim() |
| 136 | + ? maskAddress(proxyBot.dRepId) |
| 137 | + : null; |
| 138 | + |
| 139 | + return { |
| 140 | + summary: `Governance DRep ${action}${dRepId ? ` for ${dRepId}` : ""}`, |
| 141 | + }; |
| 142 | + } |
| 143 | + |
| 144 | + return null; |
| 145 | +} |
| 146 | + |
| 147 | +export function summarizeTransactionSignatureContext( |
| 148 | + txJson: unknown, |
| 149 | + description?: string | null, |
| 150 | +): SignatureContext | null { |
| 151 | + const parsed = parseJsonRecord(txJson); |
| 152 | + if (parsed) { |
| 153 | + const proxyBot = asRecord(parsed.proxyBot); |
| 154 | + if (proxyBot) { |
| 155 | + const proxySummary = summarizeProxyBot(proxyBot); |
| 156 | + if (proxySummary) return proxySummary; |
| 157 | + } |
| 158 | + |
| 159 | + const outputSummary = summarizeOutputs(parsed.outputs); |
| 160 | + if (outputSummary) return outputSummary; |
| 161 | + } |
| 162 | + |
| 163 | + return summarizeDescriptionSignatureContext(description); |
| 164 | +} |
| 165 | + |
| 166 | +export function summarizeSignableSignatureContext(args: { |
| 167 | + method?: string | null; |
| 168 | + description?: string | null; |
| 169 | +}): SignatureContext | null { |
| 170 | + const method = args.method?.trim(); |
| 171 | + const description = args.description?.trim(); |
| 172 | + |
| 173 | + if (method === "ekklesia-vote") { |
| 174 | + return { summary: "Governance vote package" }; |
| 175 | + } |
| 176 | + |
| 177 | + if (method) { |
| 178 | + return { summary: `Sign ${method}` }; |
| 179 | + } |
| 180 | + |
| 181 | + if (description?.toLowerCase().includes("governance")) { |
| 182 | + return { summary: "Governance action" }; |
| 183 | + } |
| 184 | + |
| 185 | + return null; |
| 186 | +} |
| 187 | + |
| 188 | +export function summarizeDescriptionSignatureContext( |
| 189 | + description?: string | null, |
| 190 | +): SignatureContext | null { |
| 191 | + const trimmed = description?.trim(); |
| 192 | + if (!trimmed) return null; |
| 193 | + |
| 194 | + if (!/(governance|vote|drep|delegate|stake|certificate)/i.test(trimmed)) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + |
| 198 | + return { |
| 199 | + summary: `Governance action: ${trimmed.length > 90 ? `${trimmed.slice(0, 87)}...` : trimmed}`, |
| 200 | + }; |
| 201 | +} |
0 commit comments