package tron import ( "encoding/hex" "fmt" "math/big" "github.com/wangjia/pangolin/pay/internal/wallet" "golang.org/x/crypto/sha3" ) // TransferSelector is the TRC20 transfer(address,uint256) function selector // string that TronGrid's triggersmartcontract expects. const TransferSelector = "transfer(address,uint256)" // ABIEncodeTransferParams builds the 64-byte ABI parameter for // transfer(address,uint256): the recipient (20-byte body, left-padded to 32) and // the amount (uint256, left-padded to 32). Returns hex (no 0x, no 4-byte // selector — TronGrid derives the selector from TransferSelector). func ABIEncodeTransferParams(toAddr string, amount int64) (string, error) { if amount <= 0 { return "", fmt.Errorf("tron: transfer amount must be positive") } payload, err := wallet.DecodeTronAddress(toAddr) if err != nil { return "", err } out := make([]byte, 64) copy(out[12:32], payload[1:]) // 20-byte body, right-aligned in first word new(big.Int).SetInt64(amount).FillBytes(out[32:64]) return hex.EncodeToString(out), nil } // keccakAddressBody derives the 20-byte address body from a 65-byte uncompressed // secp256k1 public key: keccak256(X||Y)[12:]. func keccakAddressBody(uncompressed []byte) []byte { h := sha3.NewLegacyKeccak256() h.Write(uncompressed[1:]) sum := h.Sum(nil) return sum[12:] }