Skip to content

Commit

Permalink
passed auth_test
Browse files Browse the repository at this point in the history
  • Loading branch information
freeelancer committed Mar 1, 2024
1 parent 4f0114f commit 8e08a83
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
12 changes: 8 additions & 4 deletions x/smartaccount/ante/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ func (sad SmartAccountAuthDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
// Signer here is the account that the state transition is affecting
// e.g. Account that is transferring some Coins
signers := sigTx.GetSigners()
account := signers[0].String()
account := signers[0]
accountStr := account.String()

// check if the tx is from a smart account
setting, err := sad.smartAccountKeeper.GetSetting(ctx, account)
setting, err := sad.smartAccountKeeper.GetSetting(ctx, accountStr)
if sdkerrors.ErrKeyNotFound.Is(err) {
// run through the default handlers for signature verification
newCtx, err := sad.defaultVerifySigDecorator(ctx, tx, simulate)
Expand Down Expand Up @@ -106,7 +107,7 @@ func (sad SmartAccountAuthDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
return ctx, err
}

acc, err := authante.GetSignerAcc(ctx, sad.accountKeeper, senderAddr)
acc, err := authante.GetSignerAcc(ctx, sad.accountKeeper, account)
if err != nil {
return ctx, err
}
Expand Down Expand Up @@ -139,7 +140,7 @@ func (sad SmartAccountAuthDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simu
for _, auth := range setting.Authorization {
authMsg := types.Authorization{
Senders: []string{senderAddr.String()},
Account: account,
Account: accountStr,
// TODO: add in future when needed
Signatures: signaturesBs,
SignedBytes: signedBytes,
Expand Down Expand Up @@ -227,6 +228,9 @@ func GetSignBytesArr(pubKey cryptotypes.PubKey, signerData authsigning.SignerDat
if err != nil {
return nil, err
}
// TODO: should this be removed?
// this works right now because its secp256k1
// if verification is done only in wasm, then this probably would not work
// if !pubKey.VerifySignature(signBytes, data.Signature) {
// return nil, fmt.Errorf("unable to verify single signer signature")
// }
Expand Down
45 changes: 39 additions & 6 deletions x/smartaccount/ante/tests/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
Expand All @@ -33,6 +34,7 @@ func (s *AnteTestSuite) Setup() {
s.SmartAccountTestSuite.SetupTests()
s.WasmKeeper = wasmkeeper.NewDefaultPermissionKeeper(s.App.Keepers.WasmKeeper)
s.Decorator = ante.NewSmartAccountAuthDecorator(s.SmartAccountKeeper, s.WasmKeeper, s.App.Keepers.AccountKeeper, nil, s.EncodingConfig.TxConfig.SignModeHandler())
s.Ctx = s.Ctx.WithChainID("test")
}

func (s *AnteTestSuite) TestAuthAnteHandler() {
Expand Down Expand Up @@ -73,7 +75,17 @@ func (s *AnteTestSuite) TestAuthAnteHandler() {
})
require.NoError(s.T(), err)

txBuilder := s.BuildDefaultMsgTx(0, &types.MsgSend{
// signing with testAcc1 pk which should error
txBuilder := s.BuildDefaultMsgTx(1, &types.MsgSend{
FromAddress: acc.String(),
ToAddress: acc.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 1)),
})
_, err = s.Decorator.AnteHandle(s.Ctx, txBuilder.GetTx(), false, sdk.ChainAnteDecorators(sdk.Terminator{}))
require.Error(s.T(), err)

// signing with testAcc0 pk which should pass
txBuilder = s.BuildDefaultMsgTx(0, &types.MsgSend{
FromAddress: acc.String(),
ToAddress: acc.String(),
Amount: sdk.NewCoins(sdk.NewInt64Coin("uluna", 1)),
Expand All @@ -84,25 +96,46 @@ func (s *AnteTestSuite) TestAuthAnteHandler() {

func (s *AnteTestSuite) BuildDefaultMsgTx(accountIndex int, msgs ...sdk.Msg) client.TxBuilder {
pk := s.TestAccPrivs[accountIndex]
sender := s.TestAccs[accountIndex]
acc := s.App.Keepers.AccountKeeper.GetAccount(s.Ctx, msgs[0].GetSigners()[0])
txBuilder := s.EncodingConfig.TxConfig.NewTxBuilder()
err := txBuilder.SetMsgs(
msgs...,
)
require.NoError(s.T(), err)

signer := authsigning.SignerData{
Address: sender.String(),
ChainID: "test",
AccountNumber: 0,
Sequence: 0,
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
PubKey: pk.PubKey(),
}
sig, err := tx.SignWithPrivKey(

emptySig := signing.SignatureV2{
PubKey: signer.PubKey,
Data: &signing.SingleSignatureData{
SignMode: s.EncodingConfig.TxConfig.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: signer.Sequence,
}

err = txBuilder.SetSignatures(emptySig)
require.NoError(s.T(), err)

sigV2, err := tx.SignWithPrivKey(
s.EncodingConfig.TxConfig.SignModeHandler().DefaultMode(),
signer,
txBuilder,
pk,
s.EncodingConfig.TxConfig,
0,
acc.GetSequence(),
)
require.NoError(s.T(), err)
txBuilder.SetSignatures(sig)

err = txBuilder.SetSignatures(sigV2)
require.NoError(s.T(), err)

return txBuilder
}

0 comments on commit 8e08a83

Please sign in to comment.