|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | + |
| 6 | +interface DecodeArgsOptions { |
| 7 | + target: string; |
| 8 | + rpc_url?: string; |
| 9 | + default?: boolean; |
| 10 | + skip_resolving?: boolean; |
| 11 | + raw?: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +class DecodeArgs { |
| 15 | + public target: string; |
| 16 | + public rpc_url: string; |
| 17 | + public default: boolean; |
| 18 | + public skip_resolving: boolean; |
| 19 | + public raw: boolean; |
| 20 | + |
| 21 | + constructor( |
| 22 | + target: string, |
| 23 | + rpc_url: string = "", |
| 24 | + useDefault: boolean = false, |
| 25 | + skip_resolving: boolean = false, |
| 26 | + raw: boolean = false |
| 27 | + ) { |
| 28 | + this.target = target; |
| 29 | + this.rpc_url = rpc_url; |
| 30 | + this.default = useDefault; |
| 31 | + this.skip_resolving = skip_resolving; |
| 32 | + this.raw = raw; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class Decoder { |
| 37 | + private args: DecodeArgs; |
| 38 | + |
| 39 | + constructor(args: DecodeArgs) { |
| 40 | + this.args = args; |
| 41 | + } |
| 42 | + |
| 43 | + public decode(): any | null { |
| 44 | + try { |
| 45 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'decoder-')); |
| 46 | + |
| 47 | + const command = ['decode', this.args.target, ]; |
| 48 | + |
| 49 | + if (this.args.rpc_url) { |
| 50 | + command.push('--rpc-url', this.args.rpc_url); |
| 51 | + } |
| 52 | + if (this.args.default) { |
| 53 | + command.push('--default'); |
| 54 | + } |
| 55 | + if (this.args.skip_resolving) { |
| 56 | + command.push('--skip-resolving'); |
| 57 | + } |
| 58 | + if (this.args.raw) { |
| 59 | + command.push('--raw'); |
| 60 | + } |
| 61 | + |
| 62 | + // Execute heimdall command |
| 63 | + execSync(`heimdall ${command.join(' ')}`, { stdio: 'inherit' }); |
| 64 | + |
| 65 | + // Here you would read and parse the output from `tempDir` |
| 66 | + // For now, we return null since the original code doesn't show the parsing step. |
| 67 | + return null; |
| 68 | + } catch (e) { |
| 69 | + console.error("Error: ", e); |
| 70 | + return null; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function isHeimdallInstalled(): boolean { |
| 76 | + try { |
| 77 | + execSync('which heimdall', { stdio: 'pipe' }); |
| 78 | + return true; |
| 79 | + } catch { |
| 80 | + return false; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function main() { |
| 85 | + if (!isHeimdallInstalled()) { |
| 86 | + console.log("heimdall does not seem to be installed on your system."); |
| 87 | + console.log("please install heimdall before running this script."); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const args = new DecodeArgs( |
| 92 | + "0x000000000000000000000000008dfede2ef0e61578c3bba84a7ed4b9d25795c30000000000000000000000000000000000000001431e0fae6d7217caa00000000000000000000000000000000000000000000000000000000000000000002710fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc7c0000000000000000000000000000000000000000000000a6af776004abf4e612ad000000000000000000000000000000000000000000000000000000012a05f20000000000000000000000000000000000000000000000000000000000000111700000000000000000000000001c5f545f5b46f76e440fa02dabf88fdc0b10851a00000000000000000000000000000000000000000000000000000002540be400", |
| 93 | + "", |
| 94 | + false, |
| 95 | + false, |
| 96 | + true |
| 97 | + ); |
| 98 | + |
| 99 | + const decoded = new Decoder(args).decode(); |
| 100 | + console.log("Decoded Result:", decoded); |
| 101 | +} |
| 102 | + |
| 103 | +main(); |
0 commit comments