|
| 1 | +import { TransactionBuilder } from "@stellar/stellar-sdk"; |
| 2 | +import { Icon, Text } from "@stellar/design-system"; |
| 3 | + |
| 4 | +import { Box } from "@/components/layout/Box"; |
| 5 | + |
| 6 | +import { useStore } from "@/store/useStore"; |
| 7 | + |
| 8 | +import { getTxData } from "@/helpers/getTxData"; |
| 9 | +import { shortenStellarAddress } from "@/helpers/shortenStellarAddress"; |
| 10 | +import { |
| 11 | + findKeyBySignatureHint, |
| 12 | + verifySignature, |
| 13 | +} from "@/helpers/signatureHint"; |
| 14 | +import * as StellarXdr from "@/helpers/StellarXdr"; |
| 15 | + |
| 16 | +import { useIsXdrInit } from "@/hooks/useIsXdrInit"; |
| 17 | + |
| 18 | +import { RpcTxJsonResponse } from "@/types/types"; |
| 19 | + |
| 20 | +export const Signatures = ({ |
| 21 | + txDetails, |
| 22 | +}: { |
| 23 | + txDetails: RpcTxJsonResponse | null; |
| 24 | +}) => { |
| 25 | + const { transaction, feeBumpTx, signatures, txHash } = getTxData(txDetails); |
| 26 | + const isXdrInit = useIsXdrInit(); |
| 27 | + const { network } = useStore(); |
| 28 | + |
| 29 | + if (!signatures || signatures.length === 0 || !txHash) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + const feeBumpInnerTxXdr = |
| 34 | + feeBumpTx?.tx?.inner_tx && |
| 35 | + isXdrInit && |
| 36 | + StellarXdr.encode( |
| 37 | + "TransactionEnvelope", |
| 38 | + JSON.stringify(feeBumpTx.tx.inner_tx), |
| 39 | + ); |
| 40 | + |
| 41 | + const feeBumpInnerTxHash = feeBumpInnerTxXdr |
| 42 | + ? TransactionBuilder.fromXDR(feeBumpInnerTxXdr, network.passphrase) |
| 43 | + .hash() |
| 44 | + .toString("hex") |
| 45 | + : undefined; |
| 46 | + |
| 47 | + const possibleSigners = getPossibleSigners( |
| 48 | + signatures, |
| 49 | + transaction, |
| 50 | + feeBumpTx, |
| 51 | + ); |
| 52 | + |
| 53 | + const renderTableBody = () => { |
| 54 | + return signatures.map( |
| 55 | + ({ hint, signature }: { hint: string; signature: string }) => { |
| 56 | + const rowKey = `table-row-${hint}`; |
| 57 | + |
| 58 | + const signerPubKey = findKeyBySignatureHint(hint, possibleSigners); |
| 59 | + |
| 60 | + const isVerified = verifySignature( |
| 61 | + { hint, signature }, |
| 62 | + signerPubKey, |
| 63 | + feeBumpInnerTxHash ? feeBumpInnerTxHash : txHash, |
| 64 | + ); |
| 65 | + |
| 66 | + return ( |
| 67 | + <tr role="row" key={rowKey}> |
| 68 | + <td> |
| 69 | + <SignatureCell> |
| 70 | + {signerPubKey ? renderSigner(isVerified, signerPubKey) : "-"} |
| 71 | + </SignatureCell> |
| 72 | + </td> |
| 73 | + <td> |
| 74 | + <SignatureCell isSignature={true}> |
| 75 | + <code>{signature}</code> |
| 76 | + </SignatureCell> |
| 77 | + </td> |
| 78 | + <td> |
| 79 | + <SignatureCell>{hint}</SignatureCell> |
| 80 | + </td> |
| 81 | + </tr> |
| 82 | + ); |
| 83 | + }, |
| 84 | + ); |
| 85 | + }; |
| 86 | + |
| 87 | + return ( |
| 88 | + <Box gap="md" addlClassName="Signatures"> |
| 89 | + <div className="Signatures__gridTableContainer"> |
| 90 | + <table> |
| 91 | + <thead> |
| 92 | + <tr> |
| 93 | + <th> |
| 94 | + <SignatureCell isHeader={true}>Signer</SignatureCell> |
| 95 | + </th> |
| 96 | + <th> |
| 97 | + <SignatureCell isHeader={true}>Signature</SignatureCell> |
| 98 | + </th> |
| 99 | + <th> |
| 100 | + <SignatureCell isHeader={true}>Hint</SignatureCell> |
| 101 | + </th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody>{renderTableBody()}</tbody> |
| 105 | + </table> |
| 106 | + </div> |
| 107 | + </Box> |
| 108 | + ); |
| 109 | +}; |
| 110 | + |
| 111 | +const SignatureCell = ({ |
| 112 | + children, |
| 113 | + isHeader, |
| 114 | + isSignature, |
| 115 | +}: { |
| 116 | + children: React.ReactNode; |
| 117 | + isHeader?: boolean; |
| 118 | + isSignature?: boolean; |
| 119 | +}) => { |
| 120 | + return ( |
| 121 | + <Text |
| 122 | + size="sm" |
| 123 | + as="div" |
| 124 | + weight="medium" |
| 125 | + addlClassName="Signatures__cell" |
| 126 | + {...(isHeader ? { "data-is-header": true } : {})} |
| 127 | + {...(isSignature ? { "data-is-signature": true } : {})} |
| 128 | + > |
| 129 | + {children} |
| 130 | + </Text> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +const renderSigner = (isVerified: boolean, signer: string) => { |
| 135 | + return isVerified ? ( |
| 136 | + <Box |
| 137 | + gap="xs" |
| 138 | + direction="row" |
| 139 | + align="center" |
| 140 | + addlClassName="success-message" |
| 141 | + > |
| 142 | + <Icon.CheckCircle /> |
| 143 | + <span>{shortenStellarAddress(signer)}</span> |
| 144 | + </Box> |
| 145 | + ) : ( |
| 146 | + <Box gap="xs" direction="row" align="center" addlClassName="error-message"> |
| 147 | + <Icon.XCircle /> |
| 148 | + <span>{shortenStellarAddress(signer)}</span> |
| 149 | + </Box> |
| 150 | + ); |
| 151 | +}; |
| 152 | + |
| 153 | +// Helper function to get possible signers |
| 154 | +const getPossibleSigners = ( |
| 155 | + signatures: any[] | undefined, |
| 156 | + transaction: any, |
| 157 | + feeBumpTx: any, |
| 158 | +) => { |
| 159 | + if (!signatures || signatures.length === 0 || !transaction) { |
| 160 | + return []; |
| 161 | + } |
| 162 | + |
| 163 | + const allPossibleSigners: string[] = []; |
| 164 | + |
| 165 | + const addSigner = (key: string | undefined) => { |
| 166 | + if (key && !allPossibleSigners.includes(key)) { |
| 167 | + allPossibleSigners.push(key); |
| 168 | + } |
| 169 | + }; |
| 170 | + |
| 171 | + // #1: Single signature case - only source account |
| 172 | + if ( |
| 173 | + signatures.length === 1 && |
| 174 | + findKeyBySignatureHint(signatures[0].hint, [transaction?.source_account]) |
| 175 | + ) { |
| 176 | + addSigner(transaction?.source_account); |
| 177 | + } |
| 178 | + |
| 179 | + // #2: Fee bump transaction |
| 180 | + if (feeBumpTx?.tx?.fee_source) { |
| 181 | + addSigner(feeBumpTx?.tx?.fee_source); |
| 182 | + } else { |
| 183 | + // #3: Regular transaction - source account + operation sources |
| 184 | + addSigner(transaction.source_account); |
| 185 | + |
| 186 | + for (const operation of transaction.operations) { |
| 187 | + addSigner(operation.source || transaction.source_account); |
| 188 | + } |
| 189 | + |
| 190 | + // Soroban Transaction doesn't use extra signers |
| 191 | + } |
| 192 | + return allPossibleSigners; |
| 193 | +}; |
0 commit comments