Skip to content

Commit

Permalink
update: get signer with codec
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Mar 5, 2024
1 parent 6d21265 commit 3a70700
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
29 changes: 12 additions & 17 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"reflect"

signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
"google.golang.org/protobuf/encoding/protojson"

rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/crypto"
Expand All @@ -30,7 +28,6 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crgerrs "github.com/cosmos/rosetta/lib/errors"
crgtypes "github.com/cosmos/rosetta/lib/types"
protov2 "google.golang.org/protobuf/proto"
)

// Converter is a utility that can be used to convert
Expand Down Expand Up @@ -63,11 +60,11 @@ type ToRosettaConverter interface {
// Amounts converts sdk.Coins to rosetta.Amounts
Amounts(ownedCoins []sdk.Coin, availableCoins sdk.Coins) []*rosettatypes.Amount
// Ops converts an sdk.Msg to rosetta operations
Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error)
Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error)
// OpsAndSigners takes raw transaction bytes and returns rosetta operations and the expected signers
OpsAndSigners(txBytes []byte) (ops []*rosettatypes.Operation, signers []*rosettatypes.AccountIdentifier, err error)
// Meta converts an sdk.Msg to rosetta metadata
Meta(msg protov2.Message) (meta map[string]interface{}, err error)
Meta(msg sdk.Msg) (meta map[string]interface{}, err error)
// SignerData returns account signing data from a queried any account
SignerData(anyAccount *codectypes.Any) (*SignerData, error)
// SigningComponents returns rosetta's components required to build a signable transaction
Expand Down Expand Up @@ -164,12 +161,11 @@ func (c converter) UnsignedTx(ops []*rosettatypes.Operation) (tx authsigning.Tx,
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
}

legacyMsg, ok := msg.(sdk.LegacyMsg)
if !ok {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, "Failed asserting LegacyMsg type")
signers, _, err := c.cdc.GetMsgV1Signers(msg)
if err != nil {
return nil, err
}

signers := legacyMsg.GetSigners()
// check if there are enough signers
if len(signers) == 0 {
return nil, crgerrs.WrapError(crgerrs.ErrBadArgument, fmt.Sprintf("operation at index %d got no signers", op.OperationIdentifier.Index))
Expand Down Expand Up @@ -222,8 +218,8 @@ func (c converter) Msg(meta map[string]interface{}, msg sdk.Msg) error {
return c.cdc.UnmarshalJSON(metaBytes, msg)
}

func (c converter) Meta(msg protov2.Message) (meta map[string]interface{}, err error) {
b, err := protojson.Marshal(msg)
func (c converter) Meta(msg sdk.Msg) (meta map[string]interface{}, err error) {
b, err := c.cdc.MarshalJSON(msg)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrCodec, err.Error())
}
Expand All @@ -239,13 +235,13 @@ func (c converter) Meta(msg protov2.Message) (meta map[string]interface{}, err e
// Ops will create an operation for each msg signer
// with the message proto name as type, and the raw fields
// as metadata
func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Operation, error) {
func (c converter) Ops(status string, msg sdk.Msg) ([]*rosettatypes.Operation, error) {
meta, err := c.Meta(msg)
if err != nil {
return nil, crgerrs.WrapError(crgerrs.ErrConverter, fmt.Sprintf("while getting meta from message %s", err.Error()))
}

signers, err := c.cdc.GetMsgV2Signers(msg)
signers, _, err := c.cdc.GetMsgV1Signers(msg)
if err != nil {
return nil, err
}
Expand All @@ -254,7 +250,7 @@ func (c converter) Ops(status string, msg protov2.Message) ([]*rosettatypes.Oper
for i, signer := range signers {
addr, _ := c.cdc.InterfaceRegistry().SigningContext().AddressCodec().BytesToString(signer)
op := &rosettatypes.Operation{
Type: "/" + string(msg.ProtoReflect().Descriptor().FullName()),
Type: sdk.MsgTypeURL(msg),
Status: &status,
Account: &rosettatypes.AccountIdentifier{Address: addr},
Metadata: meta,
Expand Down Expand Up @@ -290,9 +286,8 @@ func (c converter) Tx(rawTx cmttypes.Tx, txResult *abci.ExecTxResult) (*rosettat
}
}
// get operations from msgs
//msgs := tx.GetMsgs()
msgs, err := tx.GetMsgsV2()
// todo
msgs := tx.GetMsgs()

var rawTxOps []*rosettatypes.Operation

for _, msg := range msgs {
Expand Down
10 changes: 4 additions & 6 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/cosmos/rosetta"
crgerrs "github.com/cosmos/rosetta/lib/errors"

bankv1 "cosmossdk.io/api/cosmos/bank/v1beta1"
v1beta1 "cosmossdk.io/api/cosmos/base/v1beta1"
rosettatypes "github.com/coinbase/rosetta-sdk-go/types"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -62,16 +60,16 @@ func (s *ConverterTestSuite) TestFromRosettaOpsToTxSuccess() {
addr1 := sdk.AccAddress("address1").String()
addr2 := sdk.AccAddress("address2").String()

msg1 := &bankv1.MsgSend{
msg1 := &bank.MsgSend{
FromAddress: addr1,
ToAddress: addr2,
Amount: []*v1beta1.Coin{{Amount: "100", Denom: "test"}},
Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 10)),
}

msg2 := &bankv1.MsgSend{
msg2 := &bank.MsgSend{
FromAddress: addr2,
ToAddress: addr1,
Amount: []*v1beta1.Coin{{Amount: "10", Denom: "utxo"}},
Amount: sdk.NewCoins(sdk.NewInt64Coin("utxo", 10)),
}

ops, err := s.c.ToRosetta().Ops("", msg1)
Expand Down

0 comments on commit 3a70700

Please sign in to comment.