keep-network / keep-common

Common libraries and tools used across Keep repositories
MIT License
5 stars 14 forks source link

Support bytes20 and bytes32 in Ethereum contract bindings #116

Closed pdyraga closed 1 year ago

pdyraga commented 1 year ago

There are quite a few functions in our Solidity contracts that accept bytes20 or bytes32 as a parameter. One of many examples are requestHeartbeat(bytes20 walletPubKeyHash, bytes calldata message) from the WalletCoordinator contract and getWallet(bytes32 walletID) from the WalletRegistry contract. Given there was no parsing function defined in our Ethereum contract parsing logic, all functions accepting bytes32 and bytes20 were skipped during the code generation:

WARNING: Unsupported param type for method getWallet:
  ABI Type: bytes32
  Go Type:  [32]byte
  the method won't be callable with 'ethereum' command

This changeset adds two parsing functions: one for bytes20 and another for bytes32 Solidity type. In theory, we could have other variations of bytesN but this is not the case for our codebase, and defining a generic function is more complicated given we do not deal with slices but fixed-size arrays. Even though the proposed solution does not work for all bytesN types, it is enough for now.

Testing

Testing this change is easy.

First, see how it works without this change. Go to your keep-network/keep-core repository copy and execute make all. You will see a bunch of warnings like this:

WARNING: Unsupported param type for method getWallet:
  ABI Type: bytes32
  Go Type:  [32]byte
  the method won't be callable with 'ethereum' command
WARNING: Unsupported param type for method isWalletRegistered:
  ABI Type: bytes32
  Go Type:  [32]byte
  the method won't be callable with 'ethereum' command
WARNING: Unsupported param type for method isWalletMember:
  ABI Type: bytes32
  Go Type:  [32]byte
  the method won't be callable with 'ethereum' command

Check out this branch locally, go to keep-network/keep-core repository, and add replace directive in go.mod to point to the checked-out code. In my case, it is github.com/keep-network/keep-common => /Users/piotr/git/keep-network/keep-common. Then run make all again.

All warnings should be gone and you should see the code generated for the previously skipped functions.

I did this testing locally and I was able to call some functions with the generated code. Example:

$ ./keep-client ethereum tbtc wallet-coordinator request-heartbeat \
0x3805eed0bb0792eff8815addedb36add2c7257e5 \
0xFFFFFFFFFFFFFFFF0000000000000000 \
--config configs/config.deployer.toml --submit