Skip to content

Commit

Permalink
feat(uics20 memo hook): add signer check
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Mar 8, 2024
1 parent d7921ad commit 34fa6fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
19 changes: 11 additions & 8 deletions x/uibc/uics20/memo_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func (mh MemoHandler) execute(ctx *sdk.Context) error {

// error messages used in validateMemoMsg
var (
errNoSubCoins = errors.New("message must use only coins sent from the transfer")
errMsg0Type = errors.New("only MsgSupply, MsgSupplyCollateral and MsgLiquidate are supported as messages[0]")
errWrongSigner = errors.New("msg signer doesn't match the ICS20 receiver")
errNoSubCoins = errors.New("message must use only coins sent from the transfer")
errMsg0Type = errors.New("only MsgSupply, MsgSupplyCollateral and MsgLiquidate are supported as messages[0]")
// errMsg1Type = errors.New("only MsgBorrow is supported as messages[1]")
)

Expand All @@ -122,17 +123,25 @@ var (
// - [MsgLiquidate]
// Signer of each message (account under charged with coins), must be the receiver of the ICS20
// transfer.
// Must be called after onRecvPacketPrepare.
func (mh MemoHandler) validateMemoMsg() error {
msgLen := len(mh.msgs)
if msgLen == 0 {
return nil
}

// In this release we only support 1msg, and only messages that don't create or change
// a borrow position
if msgLen > 1 {
return errors.New("ics20 memo with more than 1 message is not supported")
}

for _, msg := range mh.msgs {
if signers := msg.GetSigners(); len(signers) != 1 || !signers[0].Equals(mh.receiver) {
return errWrongSigner
}
}

var (
asset *sdk.Coin
// collateral sdk.Coin
Expand All @@ -154,12 +163,6 @@ func (mh MemoHandler) validateMemoMsg() error {
/**
TODO: handlers v2
for _, msg := range msgs {
if signers := msg.GetSigners(); len(signers) != 1 || !signers[0].Equals(receiver) {
return errors.New(
"msg signer doesn't match the receiver, expected signer: " + receiver.String())
}
}
if msgLen == 1 {
// early return - we don't need to do more checks
Expand Down
8 changes: 3 additions & 5 deletions x/uibc/uics20/memo_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ func TestValidateMemoMsg(t *testing.T) {
errManyMsgs := "memo with more than 1 message is not supported"
errNoSubCoins := errNoSubCoins.Error()
errMsg0type := errMsg0Type.Error()
// errWrongSigner := "signer doesn't match the receiver"
errWrongSigner := errWrongSigner.Error()

mh := MemoHandler{leverage: mocks.NewLvgNoopMsgSrv()}
tcs := []struct {
msgs []sdk.Msg
errstr string
}{
/** we don't check signers in handlers v1
{[]sdk.Msg{ltypes.NewMsgSupply(accs.Bob, asset)}, errWrongSigner},
{[]sdk.Msg{ltypes.NewMsgSupplyCollateral(accs.Bob, asset)}, errWrongSigner},
{[]sdk.Msg{goodMsgSupplyColl,
ltypes.NewMsgBorrow(accs.Bob, asset)}, errWrongSigner},
*/
// {[]sdk.Msg{goodMsgSupplyColl,
// ltypes.NewMsgBorrow(accs.Bob, asset)}, errWrongSigner},

// good messages[0]
{[]sdk.Msg{goodMsgSupply}, ""},
Expand Down

0 comments on commit 34fa6fd

Please sign in to comment.