我有一份具有以下功能的智能合同:
function testHash(
address _sender,
uint8 _method,
uint256 _number,
uint256 _amount)
public
returns (bytes32)
{
return keccak256(abi.encodePacked(_sender, _number, _method, _amount));
}当给定以下值时,0xa1d9e8788414eA9827f9639c4bd81bA8f3A29758, 0, 0, 0将生成以下哈希0x65a0fc07391cb16f60c589836a72bdeaf2ab8bcb16c819126abba4408e069bcb
我有下面的高丽代码
func GenerateSignedPaymentMessage(ethAddress common.Address, paymentMethod uint8, paymentNumber, chargeAmountInWei *big.Int) []byte {
//keccak256(abi.encodePacked(msg.sender, _paymentNumber, _paymentMethod, _chargeAmountInWei))
addressBytes := ethAddress.Bytes()
// we need to cast to []byte in order to generate a keccak hash
methodBytes := make([]byte, 1)
methodBytes[0] = byte(paymentMethod)
numberBytes := paymentNumber.Bytes()
amountBytes := chargeAmountInWei.Bytes()
// generate the hash
hash := crypto.Keccak256(addressBytes, numberBytes, methodBytes, amountBytes)
return hash}
当我这样打电话的时候
address := common.HexToAddress("0xa1d9e8788414eA9827f9639c4bd81bA8f3A29758")
method := uint8(0)
number := big.NewInt(0)
amount := big.NewInt(0)
hash := GenerateSignedPaymentMessage(address, method, number, amount)
fmt.Println(hash.String())它提供了下面的输出0x7fc96d5f44e3e792633fcc981adf2bd0f644f0c167132eca5659b8a72d95bf07,它与solidity代码提供的散列不匹配。我不知道在这里该怎么做,所以任何建议都会很好!
编辑:
正如smarx所指出的,我尝试了以下功能
func TestHash(t *testing.T) {
address := common.HexToAddress("0xa1d9e8788414eA9827f9639c4bd81bA8f3A29758")
bn := make([]byte, 32)
ba := make([]byte, 32)
bm := make([]byte, 32)
bn[31] = 0
ba[31] = 0
bm[31] = 0
hash := crypto.Keccak256Hash(address.Bytes(), bn, ba, bm)
fmt.Println(hash.String())
},它产生以下哈希0xfc60222910c18ebcfd5610479223663effa903325f3db1db39a4d5fa37ba33a2
发布于 2018-07-11 05:46:38
这绝对是smarx所指出的!我使用了https://github.com/miguelmota/go-solidity-sha3库,它可以工作!
hash := payment.SoliditySHA3(
payment.Address("0xa1d9e8788414eA9827f9639c4bd81bA8f3A29758"),
payment.Uint8(big.NewInt(0)),
payment.Uint256(big.NewInt(0)),
payment.Uint256(big.NewInt(0)),
)
fmt.Println(hex.EncodeToString(hash))https://ethereum.stackexchange.com/questions/53121
复制相似问题