Skip to content

Commit

Permalink
use onion sequence in signature verification and onion sing
Browse files Browse the repository at this point in the history
  • Loading branch information
antstalepresh committed Jun 11, 2024
1 parent af7c748 commit 9d31ece
Show file tree
Hide file tree
Showing 14 changed files with 1,226 additions and 21 deletions.
3 changes: 1 addition & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func New(
)

// Configure the hooks keeper
hooksKeeper := onionkeeper.NewKeeper(
app.OnionKeeper = onionkeeper.NewKeeper(
keys[oniontypes.StoreKey],
app.GetSubspace(oniontypes.ModuleName),
app.IBCKeeper.ChannelKeeper,
Expand All @@ -564,7 +564,6 @@ func New(
app.AccountKeeper,
txConfig.SignModeHandler(),
)
app.OnionKeeper = hooksKeeper

app.WireICS20PreWasmKeeper(appCodec, bApp, app.OnionKeeper)

Expand Down
10 changes: 9 additions & 1 deletion proto/kujira/onion/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ import "kujira/onion/params.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

message GenesisState { Params params = 1 [ (gogoproto.nullable) = false ]; }
message GenesisState {
Params params = 1 [ (gogoproto.nullable) = false ];
repeated OnionSequence sequences = 2 [ (gogoproto.nullable) = false ];
}

message OnionSequence {
string address = 1;
uint64 sequence = 2;
}
20 changes: 20 additions & 0 deletions proto/kujira/onion/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
syntax = "proto3";
package kujira.onion;

import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "kujira/onion/genesis.proto";

option go_package = "github.com/Team-Kujira/core/x/onion/types";

// Query defines the gRPC querier service.
service Query {
rpc Sequence(QuerySequenceRequest) returns (QuerySequenceResponse) {
option (google.api.http).get = "/kujira/onion/sequence/{address}";
}
}

message QuerySequenceRequest { string address = 1; }
message QuerySequenceResponse {
OnionSequence seq = 1 [ (gogoproto.nullable) = false ];
}
30 changes: 30 additions & 0 deletions x/onion/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cli

import (
"context"
"fmt"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -39,6 +41,7 @@ func GetQueryCmd() *cobra.Command {

cmd.AddCommand(
GetCmdWasmSender(),
GetCmdQuerySequence(),
)
return cmd
}
Expand Down Expand Up @@ -75,3 +78,30 @@ $ %s query ibc-hooks wasm-hooks-sender channel-42 juno12smx2wdlyttvyzvzg54y2vnqw

return cmd
}

// GetCmdQuerySequence implements the query sequence command.
func GetCmdQuerySequence() *cobra.Command {
cmd := &cobra.Command{
Use: "sequence [address]",
Args: cobra.ExactArgs(1),
Short: "Query the onion sequence of an address",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.Sequence(context.Background(), &types.QuerySequenceRequest{
Address: args[0],
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
11 changes: 11 additions & 0 deletions x/onion/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"context"
"encoding/base64"
"errors"
"fmt"
Expand Down Expand Up @@ -85,6 +86,16 @@ func WriteBase64Tx(clientCtx client.Context, flagSet *pflag.FlagSet, msgs ...sdk
return err
}

queryClient := types.NewQueryClient(clientCtx)
newSeq := uint64(0)
res, err := queryClient.Sequence(context.Background(), &types.QuerySequenceRequest{
Address: clientCtx.GetFromAddress().String(),
})
if err == nil {
newSeq = res.Seq.Sequence
}
txf = txf.WithSequence(newSeq)

if txf.SimulateAndExecute() || clientCtx.Simulate {
if clientCtx.Offline {
return errors.New("cannot estimate gas in offline mode")
Expand Down
27 changes: 20 additions & 7 deletions x/onion/keeper/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,16 @@ func (k Keeper) ExecuteAnte(ctx sdk.Context, tx sdk.Tx) error {
return errorsmod.Wrap(sdkerrors.ErrInvalidPubKey, "pubkey on account is not set")
}

if sig.Sequence != acc.GetSequence() {
onionSeq := uint64(0)
seq, err := k.GetSequence(ctx, acc.GetAddress().String())
if err == nil {
onionSeq = seq.Sequence
}

if sig.Sequence != onionSeq {
return errorsmod.Wrapf(
sdkerrors.ErrWrongSequence,
"account sequence mismatch, expected %d, got %d", acc.GetSequence(), sig.Sequence,
"onion sequence mismatch, expected %d, got %d", onionSeq, sig.Sequence,
)
}

Expand All @@ -152,7 +158,7 @@ func (k Keeper) ExecuteAnte(ctx sdk.Context, tx sdk.Tx) error {
Address: acc.GetAddress().String(),
ChainID: chainID,
AccountNumber: accNum,
Sequence: acc.GetSequence(),
Sequence: onionSeq,
PubKey: pubKey,
}

Expand All @@ -170,12 +176,19 @@ func (k Keeper) ExecuteAnte(ctx sdk.Context, tx sdk.Tx) error {

// IncrementSequenceDecorator
for _, addr := range sigTx.GetSigners() {
acc := k.accountKeeper.GetAccount(ctx, addr)
if err := acc.SetSequence(acc.GetSequence() + 1); err != nil {
panic(err)
seq, err := k.GetSequence(ctx, addr.String())
if err != nil {
seq = types.OnionSequence{
Address: addr.String(),
Sequence: 0,
}
}

k.accountKeeper.SetAccount(ctx, acc)
seq.Sequence++
err = k.SetSequence(ctx, seq)
if err != nil {
return err
}
}

return nil
Expand Down
6 changes: 5 additions & 1 deletion x/onion/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,15 @@ func (k Keeper) SetParam(ctx sdk.Context, key []byte, value interface{}) {

func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
k.SetParams(ctx, genState.Params)
for _, seq := range genState.Sequences {
k.SetSequence(ctx, seq)
}
}

func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{
Params: k.GetParams(ctx),
Params: k.GetParams(ctx),
Sequences: k.GetAllSequences(ctx),
}
}

Expand Down
20 changes: 20 additions & 0 deletions x/onion/keeper/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/Team-Kujira/core/x/onion/types"
)

var _ types.QueryServer = Keeper{}

func (k Keeper) Sequence(c context.Context, req *types.QuerySequenceRequest) (*types.QuerySequenceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
seq, err := k.GetSequence(ctx, req.Address)
if err != nil {
return nil, err
}
return &types.QuerySequenceResponse{Seq: seq}, nil
}
59 changes: 59 additions & 0 deletions x/onion/keeper/sequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package keeper

import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/gogoproto/proto"

"github.com/Team-Kujira/core/x/onion/types"
)

// GetAuthorityMetadata returns the authority metadata for a specific denom
func (k Keeper) GetSequence(ctx sdk.Context, address string) (types.OnionSequence, error) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, []byte(types.OnionSequencePrefix))
bz := prefixStore.Get([]byte(address))
if bz == nil {
return types.OnionSequence{
Address: address,
Sequence: 0,
}, nil
}
sequence := types.OnionSequence{}
err := proto.Unmarshal(bz, &sequence)
if err != nil {
return types.OnionSequence{}, err
}
return sequence, nil
}

// SetAuthorityMetadata stores authority metadata for a specific denom
func (k Keeper) SetSequence(ctx sdk.Context, sequence types.OnionSequence) error {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, []byte(types.OnionSequencePrefix))

bz, err := proto.Marshal(&sequence)
if err != nil {
return err
}

prefixStore.Set([]byte(sequence.Address), bz)
return nil
}

func (k Keeper) GetAllSequences(ctx sdk.Context) []types.OnionSequence {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()

sequences := []types.OnionSequence{}
for ; iterator.Valid(); iterator.Next() {
sequence := types.OnionSequence{}
err := proto.Unmarshal(iterator.Value(), &sequence)
if err != nil {
panic(err)
}
sequences = append(sequences, sequence)
}
return sequences
}
1 change: 1 addition & 0 deletions x/onion/sdkmodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (AppModule) QuerierRoute() string {
// module-specific gRPC queries.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
}

// InitGenesis performs genesis initialization for the ibc-hooks module. It returns
Expand Down
Loading

0 comments on commit 9d31ece

Please sign in to comment.