Skip to content

Commit

Permalink
Remove hard-coded record types (#132)
Browse files Browse the repository at this point in the history
- Resolves #107 and is initial work on #108
  - Refactors the `Record.Attributes` from Any into a byte string and removes the hard-coded Protobuf record types.
  - Fixes EIP-712 bytes decoding.
- Resolves #109
  - Rewords the graphql schema to be able to represent generic IPLD objects encoded as DAG-JSON.

Co-authored-by: Roy Crihfield <roy@manteia.ltd>
Co-authored-by: neeraj <neeraj.rtly@gmail.com>
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Reviewed-on: https://git.vdb.to/cerc-io/laconicd/pulls/132
Reviewed-by: Thomas E Lackey <telackey@noreply.git.vdb.to>
Co-authored-by: Nabarun <nabarun@deepstacksoft.com>
Co-committed-by: Nabarun <nabarun@deepstacksoft.com>
  • Loading branch information
4 people authored and Ashwin committed Jan 15, 2024
1 parent 9c240f1 commit 776799e
Show file tree
Hide file tree
Showing 32 changed files with 6,260 additions and 9,811 deletions.
149 changes: 100 additions & 49 deletions ethereum/eip712/eip712.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,30 @@ package eip712

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"math/big"
"reflect" // #nosec G702
"strings"
"time"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"golang.org/x/text/cases"
"golang.org/x/text/language"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"golang.org/x/text/cases"
"golang.org/x/text/language"

registry "github.com/cerc-io/laconicd/x/registry/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
)

const bytesStr = "bytes"

// WrapTxToTypedData is an ultimate method that wraps Amino-encoded Cosmos Tx JSON data
// into an EIP712-compatible TypedData request.
func WrapTxToTypedData(
Expand All @@ -34,25 +35,6 @@ func WrapTxToTypedData(
data []byte,
feeDelegation *FeeDelegationOptions,
) (apitypes.TypedData, error) {
txData := make(map[string]interface{})

if err := json.Unmarshal(data, &txData); err != nil {
return apitypes.TypedData{}, errorsmod.Wrap(errortypes.ErrJSONUnmarshal, "failed to JSON unmarshal data")
}

if txData["msgs"].([]interface{})[0].(map[string]interface{})["value"].(map[string]interface{})["payload"] != nil {
setRecordMsg := msg.(*registry.MsgSetRecord)
var attr []interface{}
for _, b := range setRecordMsg.Payload.Record.Attributes.Value {
attr = append(attr, fmt.Sprintf("%v", b))
}

txData["msgs"].([]interface{})[0].(map[string]interface{})["value"].(map[string]interface{})["payload"].(map[string]interface{})["record"].(map[string]interface{})["attributes"] = map[string]interface{}{ //nolint:lll
"type_url": setRecordMsg.Payload.Record.Attributes.TypeUrl,
"value": attr,
}
}

domain := apitypes.TypedDataDomain{
Name: "Cosmos Web3",
Version: "1.0.0",
Expand All @@ -66,22 +48,13 @@ func WrapTxToTypedData(
return apitypes.TypedData{}, err
}

if msgTypes["TypePayloadRecord"] != nil {
msgTypes["TypePayloadRecord"] = []apitypes.Type{
{Name: "id", Type: "string"},
{Name: "bond_id", Type: "string"},
{Name: "create_time", Type: "string"},
{Name: "expiry_time", Type: "string"},
{Name: "deleted", Type: "bool"},
{Name: "attributes", Type: "TypePayloadRecordAttributes"},
}
txData := make(map[string]interface{})
if err := json.Unmarshal(data, &txData); err != nil {
return apitypes.TypedData{}, errorsmod.Wrap(errortypes.ErrJSONUnmarshal, "failed to JSON unmarshal data")
}
if msgTypes["TypePayloadRecordAttributes"] != nil {
msgTypes["TypePayloadRecordAttributes"] = []apitypes.Type{
{Name: "type_url", Type: "string"},
{Name: "value", Type: "uint8[]"},
}
delete(msgTypes, "TypePayloadRecordAttributesValue")

if err := patchTxData(txData, msgTypes, "Tx"); err != nil {
return apitypes.TypedData{}, errorsmod.Wrap(errortypes.ErrJSONUnmarshal, "failed to patch JSON data")
}

if feeDelegation != nil {
Expand Down Expand Up @@ -320,10 +293,15 @@ func traverseFields(
ethTyp := typToEth(fieldType)
if len(ethTyp) > 0 {
// Support array of uint64
if isCollection && fieldType.Kind() != reflect.Slice && fieldType.Kind() != reflect.Array {
ethTyp += "[]"
if isCollection {
if fieldType.Kind() != reflect.Slice && fieldType.Kind() != reflect.Array {
ethTyp += "[]"
}
// convert uint8[] to bytes
if fieldType.Kind() == reflect.Uint8 {
ethTyp = bytesStr
}
}

if prefix == typeDefPrefix {
typeMap[rootType] = append(typeMap[rootType], apitypes.Type{
Name: fieldName,
Expand Down Expand Up @@ -466,14 +444,13 @@ func typToEth(typ reflect.Type) string {
return "uint32"
case reflect.Uint64:
return "uint64"
case reflect.Slice:
ethName := typToEth(typ.Elem())
if len(ethName) > 0 {
return ethName + "[]"
}
case reflect.Array:
case reflect.Slice | reflect.Array:
// Note: this case may never be reached due to previous handling in traverseFields
ethName := typToEth(typ.Elem())
if len(ethName) > 0 {
if ethName == "uint8" {
return bytesStr
}
return ethName + "[]"
}
case reflect.Ptr:
Expand Down Expand Up @@ -510,3 +487,77 @@ func doRecover(err *error) {
*err = fmt.Errorf("%v", r)
}
}

// Performs extra type conversions on JSON-decoded data accoding to the provided type definitions
// for compatibility with Geth's encoding
func patchTxData(data map[string]any, schema apitypes.Types, rootType string) error {
// Scan the data for any types that need to be converted.
// This is adapted from TypedData.EncodeData
for _, field := range schema[rootType] {
encType := field.Type
encValue := data[field.Name]

switch {
case encType[len(encType)-1:] == "]":
arrayValue, ok := encValue.([]interface{})
if !ok {
return dataMismatchError(encType, encValue)
}

parsedType := strings.Split(encType, "[")[0]
if schema[parsedType] != nil {
for _, item := range arrayValue {
mapValue, ok := item.(map[string]interface{})
if !ok {
return dataMismatchError(parsedType, item)
}

err := patchTxData(mapValue, schema, parsedType)
if err != nil {
return err
}
}
} else {
for i, item := range arrayValue {
converted, err := handleConversion(parsedType, item)
if err != nil {
return err
}
arrayValue[i] = converted
}
}
case schema[encType] != nil:
mapValue, ok := encValue.(map[string]interface{})
if !ok {
return dataMismatchError(encType, encValue)
}
err := patchTxData(mapValue, schema, encType)
if err != nil {
return err
}
default:
converted, err := handleConversion(encType, encValue)
if err != nil {
return err
}
data[field.Name] = converted
}
}
return nil
}

func handleConversion(encType string, encValue any) (any, error) {
if encType == bytesStr {
// Protobuf encodes byte strings in base64
if v, ok := encValue.(string); ok {
return base64.StdEncoding.DecodeString(v)
}
}
return encValue, nil
}

// dataMismatchError generates an error for a mismatch between
// the provided type and data
func dataMismatchError(encType string, encValue any) error {
return fmt.Errorf("provided data '%v' doesn't match type '%s'", encValue, encType)
}
69 changes: 57 additions & 12 deletions ethereum/eip712/eip712_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@ import (
"testing"

"cosmossdk.io/math"

"github.com/cerc-io/laconicd/ethereum/eip712"
registrytypes "github.com/cerc-io/laconicd/x/registry/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/ethereum/go-ethereum/crypto"

"github.com/cerc-io/laconicd/crypto/ethsecp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp/params"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cerc-io/laconicd/app"
"github.com/cerc-io/laconicd/encoding"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"

banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/suite"

"github.com/cerc-io/laconicd/app"
"github.com/cerc-io/laconicd/crypto/ethsecp256k1"
"github.com/cerc-io/laconicd/encoding"
"github.com/cerc-io/laconicd/ethereum/eip712"
)

// Unit tests for single-signer EIP-712 signature verification. Multi-signer verification tests are included
Expand Down Expand Up @@ -332,6 +329,54 @@ func (suite *EIP712TestSuite) TestEIP712SignatureVerification() {
sequence: 78,
expectSuccess: false,
},

// test laconic registry messages
{
title: "Succeeds - Standard MsgSetName",
fee: txtypes.Fee{
Amount: suite.makeCoins("aphoton", math.NewInt(2000)),
GasLimit: 100000,
},
memo: "",
msgs: []sdk.Msg{
registrytypes.NewMsgSetName(
"testcrn",
"testcid",
suite.createTestAddress(),
),
},
accountNumber: 25,
sequence: 78,
expectSuccess: true,
},
{
title: "Succeeds - Standard MsgSetRecord",
fee: txtypes.Fee{
Amount: suite.makeCoins("aphoton", math.NewInt(2000)),
GasLimit: 100000,
},
memo: "",
msgs: []sdk.Msg{
registrytypes.NewMsgSetRecord(
registrytypes.Payload{
Record: &registrytypes.Record{
Attributes: []byte("test attributes"),
},
Signatures: []registrytypes.Signature{
{
Sig: "fake sig",
PubKey: "fake pubkey",
},
},
},
"testbondid",
suite.createTestAddress(),
),
},
accountNumber: 25,
sequence: 78,
expectSuccess: true,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 776799e

Please sign in to comment.