|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { Core } from "@evolution-sdk/evolution" |
| 5 | + |
| 6 | +export function TransactionDecoder() { |
| 7 | + const [txHex, setTxHex] = useState("") |
| 8 | + const [decodedJson, setDecodedJson] = useState<string>("") |
| 9 | + const [error, setError] = useState<string | null>(null) |
| 10 | + |
| 11 | + const decodeTx = async () => { |
| 12 | + setError(null) |
| 13 | + setDecodedJson("") |
| 14 | + |
| 15 | + try { |
| 16 | + const cleanHex = txHex.trim().replace(/\s+/g, '') |
| 17 | + |
| 18 | + if (!cleanHex) { |
| 19 | + setError("Please enter a transaction CBOR hex string") |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + const tx = Core.Transaction.fromCBORHex(cleanHex) |
| 24 | + const json = JSON.stringify(tx.toJSON(), (key, value) => |
| 25 | + typeof value === 'bigint' ? value.toString() : value |
| 26 | + , 2) |
| 27 | + setDecodedJson(json) |
| 28 | + } catch (err) { |
| 29 | + console.error('Decode error:', err) |
| 30 | + setError(err instanceof Error ? err.message : "Failed to decode transaction") |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <div className="space-y-6"> |
| 36 | + <div className="space-y-4"> |
| 37 | + <button |
| 38 | + onClick={decodeTx} |
| 39 | + className="px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors" |
| 40 | + > |
| 41 | + Decode Transaction |
| 42 | + </button> |
| 43 | + |
| 44 | + <div className="space-y-2"> |
| 45 | + <label htmlFor="tx-hex" className="block text-sm font-medium"> |
| 46 | + Transaction CBOR Hex |
| 47 | + </label> |
| 48 | + <textarea |
| 49 | + id="tx-hex" |
| 50 | + value={txHex} |
| 51 | + onChange={(e) => setTxHex(e.target.value)} |
| 52 | + placeholder="Paste transaction CBOR hex here..." |
| 53 | + className="w-full h-32 px-3 py-2 bg-background border border-border rounded-md font-mono text-sm resize-y focus:outline-none focus:ring-2 focus:ring-ring" |
| 54 | + /> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + |
| 58 | + {error && ( |
| 59 | + <div className="p-4 bg-destructive/10 border border-destructive/20 rounded-md"> |
| 60 | + <p className="text-destructive font-medium">Error decoding transaction:</p> |
| 61 | + <p className="text-destructive/80 text-sm mt-1">{error}</p> |
| 62 | + </div> |
| 63 | + )} |
| 64 | + |
| 65 | + {decodedJson && ( |
| 66 | + <div className="space-y-2"> |
| 67 | + <label className="block text-sm font-medium">Decoded Transaction (JSON)</label> |
| 68 | + <pre className="w-full p-4 bg-muted border border-border rounded-md overflow-auto max-h-[600px] text-sm"> |
| 69 | + <code>{decodedJson}</code> |
| 70 | + </pre> |
| 71 | + </div> |
| 72 | + )} |
| 73 | + </div> |
| 74 | + ) |
| 75 | +} |
0 commit comments