How can I get the same return value as solidity abi encodePacked in Golang

0 votes

How can i run abi.encodePacked in golang? In solidity i use keccak256(abi.encodePacked(a, b)) to calc the signature of the params.

here is my contract.

pragma solidity ^0.4.24;

import "openzeppelin-solidity/contracts/ECRecovery.sol";


contract MyContract {
    using ECRecovery for bytes32;
    address permittedSinger;

    function doSomething(
    bytes32 id, uint256 amount, bytes sig
    ) {
        bytes32 hash = getHash(msg.sender, id, amount);
        address msgSigner = hash.recover(sig);
        require(msgSigner == permittedSinger);
    }

    function getMsgSigner(bytes32 proveHash, bytes sig) public pure returns (address) {
        return proveHash.recover(sig);
    }

    function getHash(
    address receiver, bytes32 id, uint256 amount
    ) pure returns (bytes32) {
        return keccak256(abi.encodePacked(receiver, id, amount));
    }
}
Sep 26, 2018 in Blockchain by digger
• 26,740 points
3,755 views

1 answer to this question.

0 votes

This is what I used: 

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
}
answered Sep 26, 2018 by slayer
• 29,350 points

Related Questions In Blockchain

0 votes
0 answers
0 votes
0 answers

Solidity: How i can return data from hash value ?

I have a solidity contract with two ...READ MORE

Feb 14, 2020 in Blockchain by simone
• 120 points
1,065 views
+2 votes
2 answers

How can I traverse the blocks of transactions in hyperledger fabric?

For hyperledger fabric you can use query ...READ MORE

answered May 8, 2018 in Blockchain by Rachel
5,035 views
0 votes
1 answer

How can I get into the development of blockchain decentralized applications?

try Microsoft Blockchain as a service, they ...READ MORE

answered Jun 30, 2018 in Blockchain by Shashank
• 10,400 points
449 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,663 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
948 views
0 votes
1 answer

How to get return value from solidity contract?

Once you call a constant function, you ...READ MORE

answered Aug 16, 2018 in Blockchain by slayer
• 29,350 points
4,742 views
0 votes
1 answer

Can i use HTTP GET to call external service in Hyperledger Composer?

Right now, the Hyperledger Composer supports only ...READ MORE

answered Jul 16, 2018 in Blockchain by slayer
• 29,350 points
425 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP