How can I get the same return value as solidity `abi.encodePacked` in Golang
finally I managed to do it. :)
package main
import (
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/accounts/abi"
"log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
)
func main() {
uint256Ty, _ := abi.NewType("uint256")
bytes32Ty, _ := abi.NewType("bytes32")
addressTy, _ := abi.NewType("address")
arguments := abi.Arguments{
{
Type: addressTy,
},
{
Type: bytes32Ty,
},
{
Type: uint256Ty,
},
}
bytes, _ := arguments.Pack(
common.HexToAddress("0x0000000000000000000000000000000000000000"),
[32]byte{'I','D','1'},
big.NewInt(42),
)
var buf []byte
hash := sha3.NewKeccak256()
hash.Write(bytes)
buf = hash.Sum(buf)
log.Println(hexutil.Encode(buf))
// output:
// 0x1f214438d7c061ad56f98540db9a082d372df1ba9a3c96367f0103aa16c2fe9a
}