|
| 1 | +import { Aptos, AptosConfig, Network } from '@aptos-labs/ts-sdk' |
| 2 | +import { ethers } from 'ethers' |
| 3 | + |
| 4 | +/** |
| 5 | + * Script to analyze the decoded values and understand the decoding issue |
| 6 | + */ |
| 7 | +async function main() { |
| 8 | + const config = new AptosConfig({ network: Network.TESTNET }) |
| 9 | + const aptos = new Aptos(config) |
| 10 | + const oappAddress = '<your-oapp-address>' |
| 11 | + |
| 12 | + console.log('Analyzing decoded values...') |
| 13 | + console.log('---') |
| 14 | + |
| 15 | + // Get all decoded values |
| 16 | + const [counterResult, decodedCounterResult, address1Result, address2Result, numberResult] = await Promise.all([ |
| 17 | + aptos.view({ |
| 18 | + payload: { |
| 19 | + function: `${oappAddress}::oapp::get_counter`, |
| 20 | + typeArguments: [], |
| 21 | + }, |
| 22 | + }), |
| 23 | + aptos.view({ |
| 24 | + payload: { |
| 25 | + function: `${oappAddress}::oapp::get_decoded_counter`, |
| 26 | + typeArguments: [], |
| 27 | + }, |
| 28 | + }), |
| 29 | + aptos.view({ |
| 30 | + payload: { |
| 31 | + function: `${oappAddress}::oapp::get_decoded_address1`, |
| 32 | + typeArguments: [], |
| 33 | + }, |
| 34 | + }), |
| 35 | + aptos.view({ |
| 36 | + payload: { |
| 37 | + function: `${oappAddress}::oapp::get_decoded_address2`, |
| 38 | + typeArguments: [], |
| 39 | + }, |
| 40 | + }), |
| 41 | + aptos.view({ |
| 42 | + payload: { |
| 43 | + function: `${oappAddress}::oapp::get_decoded_number`, |
| 44 | + typeArguments: [], |
| 45 | + }, |
| 46 | + }), |
| 47 | + ]) |
| 48 | + |
| 49 | + console.log('Current decoded values:') |
| 50 | + console.log('- Address 1:', address1Result[0]) |
| 51 | + console.log('- Address 2:', address2Result[0]) |
| 52 | + console.log('- Number:', numberResult[0]) |
| 53 | + console.log('---') |
| 54 | + |
| 55 | + // What we expected to send |
| 56 | + console.log('Expected values:') |
| 57 | + console.log('- Address 1: 0x1234567890123456789012345678901234567890') |
| 58 | + console.log('- Address 2: 0x9876543210987654321098765432109876543210') |
| 59 | + console.log('- Number: 123456789012345678901234567890') |
| 60 | + console.log('---') |
| 61 | + |
| 62 | + // Analyze the decoded values |
| 63 | + console.log('Analysis:') |
| 64 | + |
| 65 | + // Address 1 is 0x20 = 32 in decimal |
| 66 | + console.log('\nAddress 1 (0x20 = 32):') |
| 67 | + console.log('- This is suspiciously the standard ABI offset value') |
| 68 | + console.log('- In ABI encoding, dynamic data starts with a 32-byte offset pointer') |
| 69 | + |
| 70 | + // Address 2 is 0xc2 = 194 in decimal |
| 71 | + console.log('\nAddress 2 (0xc2 = 194):') |
| 72 | + console.log('- 194 bytes = 0xc2 in hex') |
| 73 | + console.log('- This might be the length of the hex string data') |
| 74 | + |
| 75 | + // Let's check what our actual message would look like |
| 76 | + const address1 = '0x1234567890123456789012345678901234567890' |
| 77 | + const address2 = '0x9876543210987654321098765432109876543210' |
| 78 | + const number = ethers.BigNumber.from('123456789012345678901234567890') |
| 79 | + |
| 80 | + // Our packed message |
| 81 | + const packedMessage = ethers.utils.solidityPack( |
| 82 | + ['bytes32', 'bytes32', 'uint256'], |
| 83 | + [ethers.utils.hexZeroPad(address1, 32), ethers.utils.hexZeroPad(address2, 32), number] |
| 84 | + ) |
| 85 | + |
| 86 | + console.log('\nOur packed message:') |
| 87 | + console.log('- Length:', packedMessage.length - 2, 'characters (excluding 0x)') |
| 88 | + console.log('- As bytes:', (packedMessage.length - 2) / 2, 'bytes') |
| 89 | + console.log('- Hex:', packedMessage) |
| 90 | + |
| 91 | + // ABI encoded version |
| 92 | + const abiEncoded = ethers.utils.defaultAbiCoder.encode(['string'], [packedMessage]) |
| 93 | + console.log('\nABI-encoded version:') |
| 94 | + console.log('- Total length:', abiEncoded.length - 2, 'characters') |
| 95 | + console.log('- As bytes:', (abiEncoded.length - 2) / 2, 'bytes') |
| 96 | + console.log('- First 64 chars:', '0x' + abiEncoded.substring(2, 66)) |
| 97 | + console.log('- Offset (first 32 bytes):', '0x' + abiEncoded.substring(2, 66)) |
| 98 | + console.log('- String length (next 32 bytes):', '0x' + abiEncoded.substring(66, 130)) |
| 99 | + |
| 100 | + // Decode the values |
| 101 | + const offset = parseInt(abiEncoded.substring(2, 66), 16) |
| 102 | + const strLength = parseInt(abiEncoded.substring(66, 130), 16) |
| 103 | + console.log('- Offset value:', offset, '(0x' + offset.toString(16) + ')') |
| 104 | + console.log('- String length value:', strLength, '(0x' + strLength.toString(16) + ')') |
| 105 | + |
| 106 | + console.log('\nConclusion:') |
| 107 | + console.log('The Move contract is reading the ABI encoding metadata instead of the actual data!') |
| 108 | + console.log("- It's reading byte 0 as address1 (getting the offset = 0x20 = 32)") |
| 109 | + console.log("- It's reading byte 32 as address2 (getting part of the length field = 0xc2 = 194)") |
| 110 | + console.log('- The large number is likely from reading subsequent ABI encoding bytes') |
| 111 | + console.log('\nThe contract needs to:') |
| 112 | + console.log('1. Skip the ABI encoding wrapper (first 64+ bytes)') |
| 113 | + console.log('2. Parse the hex string inside') |
| 114 | + console.log('3. Convert the hex string to bytes') |
| 115 | + console.log('4. Then decode the addresses and number from those bytes') |
| 116 | +} |
| 117 | + |
| 118 | +main().catch(console.error) |
0 commit comments