Skip to content

Commit

Permalink
chore: add payload helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnletey committed May 28, 2024
1 parent b6f1a33 commit d89e4fc
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
29 changes: 29 additions & 0 deletions x/aura/keeper/msg_server_bridge_destination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper_test

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/noble-assets/aura/x/aura/types/bridge"
"github.com/stretchr/testify/require"
)

func TestBridgePayloadDecoding(t *testing.T) {
// https://etherscan.io/tx/0xf2372565408ed06136c6094007bafaa40023dc4668fe3bb8fef52e9aaea0bdd2

amount, _ := sdk.NewIntFromString("12300000000000000")
expected := bridge.Payload{
Version: "1.0",
Chain: 5000,
Sender: common.HexToAddress("0x26621f75cECaD3501202961E81f74B648F9DCe80"),
Amount: amount,
Nonce: 0,
}

var payload bridge.Payload
err := payload.Parse("0x312e300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000138800000000000000000000000026621f75cecad3501202961e81f74b648f9dce80000000000000000000000000000000000000000000000000002bb2c8eabcc0000000000000000000000000000000000000000000000000000000000000000000")

require.NoError(t, err)
require.EqualValues(t, expected, payload)
}
29 changes: 29 additions & 0 deletions x/aura/keeper/msg_server_bridge_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keeper_test

import (
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/noble-assets/aura/x/aura/types/bridge"
"github.com/stretchr/testify/require"
)

func TestBridgePayloadEncoding(t *testing.T) {
// https://etherscan.io/tx/0x781e74c855ba859fd7c963f75227c57daa2ae1dacd28c5fe1a1e5658a7fd8c32

amount, _ := sdk.NewIntFromString("2968688625494144582144")
payload := bridge.Payload{
Version: "1.0",
Chain: 1,
Sender: common.HexToAddress("0x90e0d37f59B4d3202880d2FB17f3e50b7056f762"),
Amount: amount,
Nonce: 42,
}

bz, err := payload.Bytes()

require.NoError(t, err)
require.Equal(t, "312E300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000090E0D37F59B4D3202880D2FB17F3E50B7056F7620000000000000000000000000000000000000000000000A0EED4B019B9240200000000000000000000000000000000000000000000000000000000000000002A", strings.ToUpper(common.Bytes2Hex(bz)))
}
88 changes: 88 additions & 0 deletions x/aura/types/bridge/payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package bridge

import (
"errors"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
)

var ABI abi.Arguments

type Payload struct {
Version string
Chain uint64
// NOTE: Current EVM implementation will need to be adjusted to accept a
// recipient address, which will be included here as bytes32.
Sender common.Address
Amount sdk.Int
Nonce uint64
}

func (payload *Payload) Parse(input string) error {
bz := common.FromHex(input)
raw, err := ABI.Unpack(bz)
if err != nil {
return err
}

version, ok := raw[0].([32]uint8)
if !ok {
return errors.New("invalid version")
}
payload.Version = string(common.TrimRightZeroes(version[:]))

chain, ok := raw[1].(*big.Int)
if !ok {
return errors.New("invalid chain")
}
payload.Chain = chain.Uint64()

sender, ok := raw[2].(common.Address)
if !ok {
return errors.New("invalid sender")
}
payload.Sender = sender

amount, ok := raw[3].(*big.Int)
if !ok {
return errors.New("invalid amount")
}
payload.Amount = sdk.NewIntFromBigInt(amount)

nonce, ok := raw[4].(*big.Int)
if !ok {
return errors.New("invalid nonce")
}
payload.Nonce = nonce.Uint64()

return nil
}

func (payload *Payload) Bytes() ([]byte, error) {
return ABI.Pack(
[32]uint8(common.RightPadBytes([]byte(payload.Version), 32)),
big.NewInt(int64(payload.Chain)),
payload.Sender,
payload.Amount.BigInt(),
big.NewInt(int64(payload.Nonce)),
)
}

//

func init() {
bytes32Type, _ := abi.NewType("bytes32", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
addressType, _ := abi.NewType("address", "", nil)

ABI = abi.Arguments{
{Type: bytes32Type},
{Type: uint256Type},
{Type: addressType},
{Type: uint256Type},
{Type: uint256Type},
}
}

0 comments on commit d89e4fc

Please sign in to comment.