From 9c51f71722ec5ce6538d80ff6fc475c6b0635744 Mon Sep 17 00:00:00 2001 From: haodi Date: Sat, 19 Oct 2024 13:44:01 +0800 Subject: [PATCH 1/8] api using pubkey instead of address --- client/server/distribution.go | 81 ++++++++++++++---- client/server/staking.go | 149 ++++++++++++++++++++++++++-------- lib/k1util/k1util.go | 21 +++++ 3 files changed, 200 insertions(+), 51 deletions(-) diff --git a/client/server/distribution.go b/client/server/distribution.go index 66c87816..46c7e348 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -3,6 +3,7 @@ package server import ( "errors" + "github.com/piplabs/story/lib/k1util" "net/http" sdk "github.com/cosmos/cosmos-sdk/types" @@ -17,16 +18,16 @@ import ( func (s *Server) initDistributionRoute() { s.httpMux.HandleFunc("/distribution/params", utils.SimpleWrap(s.aminoCodec, s.GetDistributionParams)) - s.httpMux.HandleFunc("/distribution/validators/{validator_address}", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorByValidatorAddress)) - s.httpMux.HandleFunc("/distribution/validators/{validator_address}/commission", utils.SimpleWrap(s.aminoCodec, s.GetValidatorCommissionByValidatorAddress)) - s.httpMux.HandleFunc("/distribution/validators/{validator_address}/outstanding_rewards", utils.SimpleWrap(s.aminoCodec, s.GetValidatorOutstandingRewardsByValidatorAddress)) - s.httpMux.HandleFunc("/distribution/validators/{validator_address}/slashes", utils.AutoWrap(s.aminoCodec, s.GetValidatorSlashesByValidatorAddress)) + s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorByValidatorAddress)) + s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/commission", utils.SimpleWrap(s.aminoCodec, s.GetValidatorCommissionByValidatorAddress)) + s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/outstanding_rewards", utils.SimpleWrap(s.aminoCodec, s.GetValidatorOutstandingRewardsByValidatorAddress)) + s.httpMux.HandleFunc("/distribution/validators/{validator_pub_key}/slashes", utils.AutoWrap(s.aminoCodec, s.GetValidatorSlashesByValidatorAddress)) s.httpMux.HandleFunc("/distribution/all_validators/outstanding_rewards", utils.AutoWrap(s.aminoCodec, s.GetAllValidatorOutstandingRewards)) - s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/validators", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorsByDelegatorAddress)) - s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/rewards", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddress)) - s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/rewards/{validator_address}", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddressValidatorAddress)) - s.httpMux.HandleFunc("/distribution/delegators/{delegator_address}/withdraw_address", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorWithdrawAddressByDelegatorAddress)) + s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/validators", utils.SimpleWrap(s.aminoCodec, s.GetDistributionValidatorsByDelegatorAddress)) + s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/rewards", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddress)) + s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/rewards/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorRewardsByDelegatorAddressValidatorAddress)) + s.httpMux.HandleFunc("/distribution/delegators/{delegator_pub_key}/withdraw_address", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorWithdrawAddressByDelegatorAddress)) } // GetDistributionParams queries params of the distribution module. @@ -51,8 +52,13 @@ func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (re return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorDistributionInfo(queryContext, &distributiontypes.QueryValidatorDistributionInfoRequest{ - ValidatorAddress: mux.Vars(r)["validator_address"], + ValidatorAddress: valAddr, }) if err != nil { return nil, err @@ -68,8 +74,13 @@ func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorCommission(queryContext, &distributiontypes.QueryValidatorCommissionRequest{ - ValidatorAddress: mux.Vars(r)["validator_address"], + ValidatorAddress: valAddr, }) if err != nil { @@ -86,8 +97,13 @@ func (s *Server) GetValidatorOutstandingRewardsByValidatorAddress(r *http.Reques return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorOutstandingRewards(queryContext, &distributiontypes.QueryValidatorOutstandingRewardsRequest{ - ValidatorAddress: mux.Vars(r)["validator_address"], + ValidatorAddress: valAddr, }) if err != nil { @@ -145,8 +161,13 @@ func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesB return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).ValidatorSlashes(queryContext, &distributiontypes.QueryValidatorSlashesRequest{ - ValidatorAddress: mux.Vars(r)["validator_address"], + ValidatorAddress: valAddr, StartingHeight: req.StartingHeight, EndingHeight: req.EndingHeight, Pagination: &query.PageRequest{ @@ -172,8 +193,13 @@ func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (r return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegatorValidators(queryContext, &distributiontypes.QueryDelegatorValidatorsRequest{ - DelegatorAddress: mux.Vars(r)["delegator_address"], + DelegatorAddress: delAddr, }) if err != nil { @@ -190,8 +216,13 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp an return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegationTotalRewards(queryContext, &distributiontypes.QueryDelegationTotalRewardsRequest{ - DelegatorAddress: mux.Vars(r)["delegator_address"], + DelegatorAddress: delAddr, }) if err != nil { @@ -208,10 +239,19 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.R return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegationRewards(queryContext, &distributiontypes.QueryDelegationRewardsRequest{ - DelegatorAddress: muxVars["delegator_address"], - ValidatorAddress: muxVars["validator_address"], + DelegatorAddress: delAddr, + ValidatorAddress: valAddr, }) if err != nil { @@ -228,8 +268,13 @@ func (s *Server) GetDelegatorWithdrawAddressByDelegatorAddress(r *http.Request) return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetDistrKeeper()).DelegatorWithdrawAddress(queryContext, &distributiontypes.QueryDelegatorWithdrawAddressRequest{ - DelegatorAddress: mux.Vars(r)["delegator_address"], + DelegatorAddress: delAddr, }) if err != nil { diff --git a/client/server/staking.go b/client/server/staking.go index 1cc8f690..010c3f6d 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -2,6 +2,7 @@ package server import ( + "github.com/piplabs/story/lib/k1util" "net/http" "strconv" @@ -19,19 +20,19 @@ func (s *Server) initStakingRoute() { s.httpMux.HandleFunc("/staking/historical_info/{height}", utils.SimpleWrap(s.aminoCodec, s.GetHistoricalInfoByHeight)) s.httpMux.HandleFunc("/staking/validators", utils.AutoWrap(s.aminoCodec, s.GetValidators)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}", utils.SimpleWrap(s.aminoCodec, s.GetValidatorByValidatorAddress)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/delegations", utils.AutoWrap(s.aminoCodec, s.GetValidatorDelegationsByValidatorAddress)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/delegations/{delegator_addr}", utils.SimpleWrap(s.aminoCodec, s.GetDelegationByValidatorAddressDelegatorAddress)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetValidatorUnbondingDelegations)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/delegations/{delegator_addr}/unbonding_delegation", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorUnbondingDelegation)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/delegators/{delegator_addr}/period_delegations", utils.SimpleWrap(s.aminoCodec, s.GetPeriodDelegationsByDelegatorAddress)) - s.httpMux.HandleFunc("/staking/validators/{validator_addr}/delegators/{delegator_addr}/period_delegations/{period_delegation_id}", utils.SimpleWrap(s.aminoCodec, s.GetPeriodDelegationByDelegatorAddressAndID)) - - s.httpMux.HandleFunc("/staking/delegations/{delegator_addr}", utils.AutoWrap(s.aminoCodec, s.GetDelegationsByDelegatorAddress)) - s.httpMux.HandleFunc("/staking/delegators/{delegator_addr}/redelegations", utils.AutoWrap(s.aminoCodec, s.GetRedelegationsByDelegatorAddress)) - s.httpMux.HandleFunc("/staking/delegators/{delegator_addr}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetUnbondingDelegationsByDelegatorAddress)) - s.httpMux.HandleFunc("/staking/delegators/{delegator_addr}/validators", utils.AutoWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddress)) - s.httpMux.HandleFunc("/staking/delegators/{delegator_addr}/validators/{validator_addr}", utils.SimpleWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddressValidatorAddress)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetValidatorByValidatorAddress)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegations", utils.AutoWrap(s.aminoCodec, s.GetValidatorDelegationsByValidatorAddress)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegations/{delegator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDelegationByValidatorAddressDelegatorAddress)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetValidatorUnbondingDelegations)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegations/{delegator_pub_key}/unbonding_delegation", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorUnbondingDelegation)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegators/{delegator_pub_key}/period_delegations", utils.SimpleWrap(s.aminoCodec, s.GetPeriodDelegationsByDelegatorAddress)) + s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegators/{delegator_pub_key}/period_delegations/{period_delegation_id}", utils.SimpleWrap(s.aminoCodec, s.GetPeriodDelegationByDelegatorAddressAndID)) + + s.httpMux.HandleFunc("/staking/delegations/{delegator_pub_key}", utils.AutoWrap(s.aminoCodec, s.GetDelegationsByDelegatorAddress)) + s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/redelegations", utils.AutoWrap(s.aminoCodec, s.GetRedelegationsByDelegatorAddress)) + s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetUnbondingDelegationsByDelegatorAddress)) + s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/validators", utils.AutoWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddress)) + s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/validators/{validator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddressValidatorAddress)) } // GetStakingParams queries the staking parameters. @@ -129,8 +130,13 @@ func (s *Server) GetValidatorByValidatorAddress(r *http.Request) (resp any, err return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).Validator(queryContext, &stakingtypes.QueryValidatorRequest{ - ValidatorAddr: mux.Vars(r)["validator_addr"], + ValidatorAddr: valAddr, }) if err != nil { @@ -151,8 +157,13 @@ func (s *Server) GetValidatorDelegationsByValidatorAddress(req *getValidatorDele return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).ValidatorDelegations(queryContext, &stakingtypes.QueryValidatorDelegationsRequest{ - ValidatorAddr: mux.Vars(r)["validator_addr"], + ValidatorAddr: valAddr, Pagination: &query.PageRequest{ Key: []byte(req.Pagination.Key), Offset: req.Pagination.Offset, @@ -176,10 +187,19 @@ func (s *Server) GetDelegationByValidatorAddressDelegatorAddress(r *http.Request return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).Delegation(queryContext, &stakingtypes.QueryDelegationRequest{ - ValidatorAddr: muxVars["validator_addr"], - DelegatorAddr: muxVars["delegator_addr"], + ValidatorAddr: valAddr, + DelegatorAddr: delAddr, }) if err != nil { return nil, err @@ -195,8 +215,13 @@ func (s *Server) GetValidatorUnbondingDelegations(req *getValidatorUnbondingDele return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).ValidatorUnbondingDelegations(queryContext, &stakingtypes.QueryValidatorUnbondingDelegationsRequest{ - ValidatorAddr: mux.Vars(r)["validator_addr"], + ValidatorAddr: valAddr, Pagination: &query.PageRequest{ Key: []byte(req.Pagination.Key), Offset: req.Pagination.Offset, @@ -219,10 +244,19 @@ func (s *Server) GetDelegatorUnbondingDelegation(r *http.Request) (resp any, err return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).UnbondingDelegation(queryContext, &stakingtypes.QueryUnbondingDelegationRequest{ - ValidatorAddr: muxVars["validator_addr"], - DelegatorAddr: muxVars["delegator_addr"], + ValidatorAddr: valAddr, + DelegatorAddr: delAddr, }) if err != nil { return nil, err @@ -238,8 +272,13 @@ func (s *Server) GetDelegationsByDelegatorAddress(req *getDelegationsByDelegator return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).DelegatorDelegations(queryContext, &stakingtypes.QueryDelegatorDelegationsRequest{ - DelegatorAddr: mux.Vars(r)["delegator_addr"], + DelegatorAddr: delAddr, Pagination: &query.PageRequest{ Key: []byte(req.Pagination.Key), Offset: req.Pagination.Offset, @@ -263,8 +302,13 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).Redelegations(queryContext, &stakingtypes.QueryRedelegationsRequest{ - DelegatorAddr: mux.Vars(r)["delegator_addr"], + DelegatorAddr: delAddr, SrcValidatorAddr: req.SrcValidatorAddr, DstValidatorAddr: req.DstValidatorAddr, Pagination: &query.PageRequest{ @@ -290,8 +334,13 @@ func (s *Server) GetUnbondingDelegationsByDelegatorAddress(req *getUnbondingDele return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).DelegatorUnbondingDelegations(queryContext, &stakingtypes.QueryDelegatorUnbondingDelegationsRequest{ - DelegatorAddr: mux.Vars(r)["delegator_addr"], + DelegatorAddr: delAddr, Pagination: &query.PageRequest{ Key: []byte(req.Pagination.Key), Offset: req.Pagination.Offset, @@ -315,8 +364,13 @@ func (s *Server) GetValidatorsByDelegatorAddress(req *getValidatorsByDelegatorAd return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).DelegatorValidators(queryContext, &stakingtypes.QueryDelegatorValidatorsRequest{ - DelegatorAddr: mux.Vars(r)["delegator_addr"], + DelegatorAddr: delAddr, Pagination: &query.PageRequest{ Key: []byte(req.Pagination.Key), Offset: req.Pagination.Offset, @@ -346,10 +400,19 @@ func (s *Server) GetValidatorsByDelegatorAddressValidatorAddress(r *http.Request return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + queryResp, err := keeper.NewQuerier(s.store.GetStakingKeeper()).DelegatorValidator(queryContext, &stakingtypes.QueryDelegatorValidatorRequest{ - DelegatorAddr: muxVars["delegator_addr"], - ValidatorAddr: muxVars["validator_addr"], + DelegatorAddr: delAddr, + ValidatorAddr: valAddr, }) if err != nil { @@ -371,12 +434,22 @@ func (s *Server) GetPeriodDelegationsByDelegatorAddress(r *http.Request) (resp a } muxVars := mux.Vars(r) - valAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(muxVars["validator_addr"]) + valAddrStr, err := k1util.CmpPubKeyToValidatorAddress([]byte(muxVars["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(muxVars["delegator_pub_key"])) + if err != nil { + return nil, err + } + + valAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(valAddrStr) if err != nil { return nil, err } - delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(muxVars["delegator_addr"]) + delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(delAddrStr) if err != nil { return nil, err } @@ -392,12 +465,22 @@ func (s *Server) GetPeriodDelegationByDelegatorAddressAndID(r *http.Request) (re } muxVars := mux.Vars(r) - valAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(muxVars["validator_addr"]) + valAddrStr, err := k1util.CmpPubKeyToValidatorAddress([]byte(muxVars["validator_pub_key"])) + if err != nil { + return nil, err + } + + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(muxVars["delegator_pub_key"])) + if err != nil { + return nil, err + } + + valAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(valAddrStr) if err != nil { return nil, err } - delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(muxVars["delegator_addr"]) + delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(delAddrStr) if err != nil { return nil, err } diff --git a/lib/k1util/k1util.go b/lib/k1util/k1util.go index bfb13629..29b06041 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -3,6 +3,8 @@ package k1util import ( stdecdsa "crypto/ecdsa" + "fmt" + cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/cometbft/cometbft/crypto" k1 "github.com/cometbft/cometbft/crypto/secp256k1" @@ -178,3 +180,22 @@ func CosmosPubkeyToEVMAddress(pubkeyCmp []byte) (addr common.Address, err error) return addr, nil } + +func CmpPubKeyToDelegatorAddress(cmpPubKey []byte) (string, error) { + if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { + return "", fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)) + } + + pubKey := &cosmosk1.PubKey{Key: cmpPubKey} + + return cosmostypes.AccAddress(pubKey.Address().Bytes()).String(), nil +} + +func CmpPubKeyToValidatorAddress(cmpPubKey []byte) (string, error) { + if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { + return "", fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)) + } + pubKey := &cosmosk1.PubKey{Key: cmpPubKey} + + return cosmostypes.ValAddress(pubKey.Address().Bytes()).String(), nil +} From 9caf4e79d75ab1de1870f42611df054d7311254e Mon Sep 17 00:00:00 2001 From: haodi Date: Sat, 19 Oct 2024 20:57:11 +0800 Subject: [PATCH 2/8] add get delegator info interface --- client/server/staking.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/client/server/staking.go b/client/server/staking.go index 010c3f6d..d75b04d2 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -29,6 +29,8 @@ func (s *Server) initStakingRoute() { s.httpMux.HandleFunc("/staking/validators/{validator_pub_key}/delegators/{delegator_pub_key}/period_delegations/{period_delegation_id}", utils.SimpleWrap(s.aminoCodec, s.GetPeriodDelegationByDelegatorAddressAndID)) s.httpMux.HandleFunc("/staking/delegations/{delegator_pub_key}", utils.AutoWrap(s.aminoCodec, s.GetDelegationsByDelegatorAddress)) + + s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}", utils.SimpleWrap(s.aminoCodec, s.GetDelegatorByDelegatorAddress)) s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/redelegations", utils.AutoWrap(s.aminoCodec, s.GetRedelegationsByDelegatorAddress)) s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/unbonding_delegations", utils.AutoWrap(s.aminoCodec, s.GetUnbondingDelegationsByDelegatorAddress)) s.httpMux.HandleFunc("/staking/delegators/{delegator_pub_key}/validators", utils.AutoWrap(s.aminoCodec, s.GetValidatorsByDelegatorAddress)) @@ -327,6 +329,36 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg return queryResp, nil } +// GetDelegatorByDelegatorAddress queries delegator info for given delegator address. +func (s *Server) GetDelegatorByDelegatorAddress(r *http.Request) (resp any, err error) { + delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + if err != nil { + return nil, err + } + + delWithdrawEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorWithdrawAddress.Get(r.Context(), delAddr) + if err != nil { + return nil, err + } + + delRewardEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorRewardAddress.Get(r.Context(), delAddr) + if err != nil { + return nil, err + } + + delOperatorEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorOperatorAddress.Get(r.Context(), delAddr) + if err != nil { + return nil, err + } + + return map[string]string{ + "delegator_pubkey": mux.Vars(r)["delegator_pub_key"], + "withdraw_address": delWithdrawEvmAddr, + "reward_address": delRewardEvmAddr, + "operator_address": delOperatorEvmAddr, + }, nil +} + // GetUnbondingDelegationsByDelegatorAddress queries all unbonding delegations of a given delegator address. func (s *Server) GetUnbondingDelegationsByDelegatorAddress(req *getUnbondingDelegationsByDelegatorAddressRequest, r *http.Request) (resp any, err error) { queryContext, err := s.createQueryContextByHeader(r) From ff283a8f8d0b4dab47afb7b566cf3ddb8dd418d5 Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 00:23:12 +0800 Subject: [PATCH 3/8] fixed --- client/server/distribution.go | 18 +++++++++--------- client/server/staking.go | 36 +++++++++++++++++------------------ lib/k1util/k1util.go | 22 +++++++++++++++++---- 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/client/server/distribution.go b/client/server/distribution.go index 46c7e348..cbd10975 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -52,7 +52,7 @@ func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (re return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -74,7 +74,7 @@ func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -97,7 +97,7 @@ func (s *Server) GetValidatorOutstandingRewardsByValidatorAddress(r *http.Reques return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -161,7 +161,7 @@ func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesB return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -193,7 +193,7 @@ func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (r return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -216,7 +216,7 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp an return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -239,12 +239,12 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.R return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -268,7 +268,7 @@ func (s *Server) GetDelegatorWithdrawAddressByDelegatorAddress(r *http.Request) return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } diff --git a/client/server/staking.go b/client/server/staking.go index d75b04d2..06a9307c 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -132,7 +132,7 @@ func (s *Server) GetValidatorByValidatorAddress(r *http.Request) (resp any, err return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -159,7 +159,7 @@ func (s *Server) GetValidatorDelegationsByValidatorAddress(req *getValidatorDele return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -189,12 +189,12 @@ func (s *Server) GetDelegationByValidatorAddressDelegatorAddress(r *http.Request return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -217,7 +217,7 @@ func (s *Server) GetValidatorUnbondingDelegations(req *getValidatorUnbondingDele return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } @@ -246,12 +246,12 @@ func (s *Server) GetDelegatorUnbondingDelegation(r *http.Request) (resp any, err return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -274,7 +274,7 @@ func (s *Server) GetDelegationsByDelegatorAddress(req *getDelegationsByDelegator return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -304,7 +304,7 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -331,7 +331,7 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg // GetDelegatorByDelegatorAddress queries delegator info for given delegator address. func (s *Server) GetDelegatorByDelegatorAddress(r *http.Request) (resp any, err error) { - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -366,7 +366,7 @@ func (s *Server) GetUnbondingDelegationsByDelegatorAddress(req *getUnbondingDele return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -396,7 +396,7 @@ func (s *Server) GetValidatorsByDelegatorAddress(req *getValidatorsByDelegatorAd return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -432,12 +432,12 @@ func (s *Server) GetValidatorsByDelegatorAddressValidatorAddress(r *http.Request return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress([]byte(mux.Vars(r)["validator_pub_key"])) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(mux.Vars(r)["delegator_pub_key"])) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } @@ -466,12 +466,12 @@ func (s *Server) GetPeriodDelegationsByDelegatorAddress(r *http.Request) (resp a } muxVars := mux.Vars(r) - valAddrStr, err := k1util.CmpPubKeyToValidatorAddress([]byte(muxVars["validator_pub_key"])) + valAddrStr, err := k1util.CmpPubKeyToValidatorAddress(muxVars["validator_pub_key"]) if err != nil { return nil, err } - delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(muxVars["delegator_pub_key"])) + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress(muxVars["delegator_pub_key"]) if err != nil { return nil, err } @@ -497,12 +497,12 @@ func (s *Server) GetPeriodDelegationByDelegatorAddressAndID(r *http.Request) (re } muxVars := mux.Vars(r) - valAddrStr, err := k1util.CmpPubKeyToValidatorAddress([]byte(muxVars["validator_pub_key"])) + valAddrStr, err := k1util.CmpPubKeyToValidatorAddress(muxVars["validator_pub_key"]) if err != nil { return nil, err } - delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress([]byte(muxVars["delegator_pub_key"])) + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress(muxVars["delegator_pub_key"]) if err != nil { return nil, err } diff --git a/lib/k1util/k1util.go b/lib/k1util/k1util.go index 29b06041..5bf21b4d 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -3,8 +3,10 @@ package k1util import ( stdecdsa "crypto/ecdsa" + "encoding/hex" "fmt" cosmostypes "github.com/cosmos/cosmos-sdk/types" + "strings" "github.com/cometbft/cometbft/crypto" k1 "github.com/cometbft/cometbft/crypto/secp256k1" @@ -181,9 +183,15 @@ func CosmosPubkeyToEVMAddress(pubkeyCmp []byte) (addr common.Address, err error) return addr, nil } -func CmpPubKeyToDelegatorAddress(cmpPubKey []byte) (string, error) { +func CmpPubKeyToDelegatorAddress(cmpPubKeyHex string) (string, error) { + cmpPubKeyHex = strings.Replace(cmpPubKeyHex, "0x", "", 1) + cmpPubKey, err := hex.DecodeString(cmpPubKeyHex) + if err != nil { + return "", errors.Wrap(err, "invalid compressed public key") + } + if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { - return "", fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)) + return "", errors.Wrap(fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)), "invalid compressed public key") } pubKey := &cosmosk1.PubKey{Key: cmpPubKey} @@ -191,9 +199,15 @@ func CmpPubKeyToDelegatorAddress(cmpPubKey []byte) (string, error) { return cosmostypes.AccAddress(pubKey.Address().Bytes()).String(), nil } -func CmpPubKeyToValidatorAddress(cmpPubKey []byte) (string, error) { +func CmpPubKeyToValidatorAddress(cmpPubKeyHex string) (string, error) { + cmpPubKeyHex = strings.Replace(cmpPubKeyHex, "0x", "", 1) + cmpPubKey, err := hex.DecodeString(cmpPubKeyHex) + if err != nil { + return "", errors.Wrap(err, "invalid compressed public key") + } + if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { - return "", fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)) + return "", errors.Wrap(fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)), "invalid compressed public key") } pubKey := &cosmosk1.PubKey{Key: cmpPubKey} From 9d55118a36de8df1112c337b079c6af37f1c791e Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 01:06:44 +0800 Subject: [PATCH 4/8] fixed context --- client/server/staking.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/client/server/staking.go b/client/server/staking.go index 06a9307c..f2104884 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -331,22 +331,27 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg // GetDelegatorByDelegatorAddress queries delegator info for given delegator address. func (s *Server) GetDelegatorByDelegatorAddress(r *http.Request) (resp any, err error) { + queryContext, err := s.createQueryContextByHeader(r) + if err != nil { + return nil, err + } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } - delWithdrawEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorWithdrawAddress.Get(r.Context(), delAddr) + delWithdrawEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorWithdrawAddress.Get(queryContext, delAddr) if err != nil { return nil, err } - delRewardEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorRewardAddress.Get(r.Context(), delAddr) + delRewardEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorRewardAddress.Get(queryContext, delAddr) if err != nil { return nil, err } - delOperatorEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorOperatorAddress.Get(r.Context(), delAddr) + delOperatorEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorOperatorAddress.Get(queryContext, delAddr) if err != nil { return nil, err } From 903acb7d50ef277abc8b71151c1da1b2aef07599 Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 10:54:27 +0800 Subject: [PATCH 5/8] fix lint --- lib/k1util/k1util.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/k1util/k1util.go b/lib/k1util/k1util.go index 5bf21b4d..bdcad9e8 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -184,32 +184,33 @@ func CosmosPubkeyToEVMAddress(pubkeyCmp []byte) (addr common.Address, err error) } func CmpPubKeyToDelegatorAddress(cmpPubKeyHex string) (string, error) { - cmpPubKeyHex = strings.Replace(cmpPubKeyHex, "0x", "", 1) - cmpPubKey, err := hex.DecodeString(cmpPubKeyHex) + pubKey, err := decodePubKeyFromHex(cmpPubKeyHex) if err != nil { return "", errors.Wrap(err, "invalid compressed public key") } - if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { - return "", errors.Wrap(fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)), "invalid compressed public key") - } - - pubKey := &cosmosk1.PubKey{Key: cmpPubKey} - return cosmostypes.AccAddress(pubKey.Address().Bytes()).String(), nil } func CmpPubKeyToValidatorAddress(cmpPubKeyHex string) (string, error) { - cmpPubKeyHex = strings.Replace(cmpPubKeyHex, "0x", "", 1) - cmpPubKey, err := hex.DecodeString(cmpPubKeyHex) + pubKey, err := decodePubKeyFromHex(cmpPubKeyHex) if err != nil { return "", errors.Wrap(err, "invalid compressed public key") } + return cosmostypes.ValAddress(pubKey.Address().Bytes()).String(), nil +} + +func decodePubKeyFromHex(pubKeyHex string) (*cosmosk1.PubKey, error) { + cmpPubKeyHex := strings.Replace(pubKeyHex, "0x", "", 1) + cmpPubKey, err := hex.DecodeString(cmpPubKeyHex) + if err != nil { + return nil, errors.Wrap(err, "invalid compressed public key") + } + if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { - return "", errors.Wrap(fmt.Errorf("invalid compressed public key length: %d", len(cmpPubKey)), "invalid compressed public key") + return nil, errors.New(fmt.Sprintf("invalid compressed public key length: %d", len(cmpPubKey))) } - pubKey := &cosmosk1.PubKey{Key: cmpPubKey} - return cosmostypes.ValAddress(pubKey.Address().Bytes()).String(), nil + return &cosmosk1.PubKey{Key: cmpPubKey}, nil } From ad0ac5800463a0450c16ed7513e75d742d2c1252 Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 11:14:06 +0800 Subject: [PATCH 6/8] fixed lint --- client/server/distribution.go | 3 ++- client/server/staking.go | 4 ++-- lib/k1util/k1util.go | 11 +++++------ 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/client/server/distribution.go b/client/server/distribution.go index cbd10975..aade2160 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -3,7 +3,6 @@ package server import ( "errors" - "github.com/piplabs/story/lib/k1util" "net/http" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,6 +12,8 @@ import ( "github.com/gorilla/mux" "github.com/piplabs/story/client/server/utils" + + "github.com/piplabs/story/lib/k1util" ) func (s *Server) initDistributionRoute() { diff --git a/client/server/staking.go b/client/server/staking.go index f2104884..c3ed49c7 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -154,12 +154,12 @@ func (s *Server) GetValidatorByValidatorAddress(r *http.Request) (resp any, err // GetValidatorDelegationsByValidatorAddress queries delegate info for given validator. func (s *Server) GetValidatorDelegationsByValidatorAddress(req *getValidatorDelegationsByValidatorAddressRequest, r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } diff --git a/lib/k1util/k1util.go b/lib/k1util/k1util.go index bdcad9e8..23d0fbf7 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -4,22 +4,21 @@ package k1util import ( stdecdsa "crypto/ecdsa" "encoding/hex" - "fmt" - cosmostypes "github.com/cosmos/cosmos-sdk/types" - "strings" - "github.com/cometbft/cometbft/crypto" k1 "github.com/cometbft/cometbft/crypto/secp256k1" cryptopb "github.com/cometbft/cometbft/proto/tendermint/crypto" cosmosk1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - cosmoscrypto "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" "github.com/ethereum/go-ethereum/common" ethcrypto "github.com/ethereum/go-ethereum/crypto" + "strings" "github.com/piplabs/story/lib/cast" "github.com/piplabs/story/lib/errors" + + cosmoscrypto "github.com/cosmos/cosmos-sdk/crypto/types" + cosmostypes "github.com/cosmos/cosmos-sdk/types" ) // privKeyLen is the length of a secp256k1 private key. @@ -209,7 +208,7 @@ func decodePubKeyFromHex(pubKeyHex string) (*cosmosk1.PubKey, error) { } if len(cmpPubKey) != secp256k1.PubKeyBytesLenCompressed { - return nil, errors.New(fmt.Sprintf("invalid compressed public key length: %d", len(cmpPubKey))) + return nil, errors.New("invalid compressed public key", "length", len(cmpPubKey)) } return &cosmosk1.PubKey{Key: cmpPubKey}, nil From 1e751a051c90f05b69d9b8375d08e2078efa7f47 Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 11:21:15 +0800 Subject: [PATCH 7/8] fixed lint --- client/server/distribution.go | 34 +++++++++++++++++----------------- client/server/staking.go | 4 ++-- lib/k1util/k1util.go | 8 ++++---- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/client/server/distribution.go b/client/server/distribution.go index aade2160..5abf5d8d 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -48,12 +48,12 @@ func (s *Server) GetDistributionParams(r *http.Request) (resp any, err error) { // GetDistributionValidatorByValidatorAddress queries validator commission and self-delegation rewards for validator. func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -70,12 +70,12 @@ func (s *Server) GetDistributionValidatorByValidatorAddress(r *http.Request) (re // GetValidatorCommissionByValidatorAddress queries accumulated commission for a validator. func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -93,12 +93,12 @@ func (s *Server) GetValidatorCommissionByValidatorAddress(r *http.Request) (resp // GetValidatorOutstandingRewardsByValidatorAddress queries rewards of a validator address. func (s *Server) GetValidatorOutstandingRewardsByValidatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -157,12 +157,12 @@ func (s *Server) GetAllValidatorOutstandingRewards(req *getAllValidatorOutstandi // GetValidatorSlashesByValidatorAddress queries slash events of a validator. func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesByValidatorAddressRequest, r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -189,12 +189,12 @@ func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesB // GetDistributionValidatorsByDelegatorAddress queries the validators of a delegator. func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -212,12 +212,12 @@ func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (r // GetDelegatorRewardsByDelegatorAddress queries the total rewards accrued by each validator. func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -235,17 +235,17 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddress(r *http.Request) (resp an // GetDelegatorRewardsByDelegatorAddressValidatorAddress queries the total rewards accrued by a delegation. func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } @@ -264,12 +264,12 @@ func (s *Server) GetDelegatorRewardsByDelegatorAddressValidatorAddress(r *http.R // GetDelegatorWithdrawAddressByDelegatorAddress queries withdraw address of a delegator. func (s *Server) GetDelegatorWithdrawAddressByDelegatorAddress(r *http.Request) (resp any, err error) { - queryContext, err := s.createQueryContextByHeader(r) + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) if err != nil { return nil, err } - delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } diff --git a/client/server/staking.go b/client/server/staking.go index c3ed49c7..f2104884 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -154,12 +154,12 @@ func (s *Server) GetValidatorByValidatorAddress(r *http.Request) (resp any, err // GetValidatorDelegationsByValidatorAddress queries delegate info for given validator. func (s *Server) GetValidatorDelegationsByValidatorAddress(req *getValidatorDelegationsByValidatorAddressRequest, r *http.Request) (resp any, err error) { - valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } - queryContext, err := s.createQueryContextByHeader(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) if err != nil { return nil, err } diff --git a/lib/k1util/k1util.go b/lib/k1util/k1util.go index 23d0fbf7..e040a476 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -4,21 +4,21 @@ package k1util import ( stdecdsa "crypto/ecdsa" "encoding/hex" + "strings" + "github.com/cometbft/cometbft/crypto" k1 "github.com/cometbft/cometbft/crypto/secp256k1" cryptopb "github.com/cometbft/cometbft/proto/tendermint/crypto" cosmosk1 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" + cosmoscrypto "github.com/cosmos/cosmos-sdk/crypto/types" + cosmostypes "github.com/cosmos/cosmos-sdk/types" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" "github.com/ethereum/go-ethereum/common" ethcrypto "github.com/ethereum/go-ethereum/crypto" - "strings" "github.com/piplabs/story/lib/cast" "github.com/piplabs/story/lib/errors" - - cosmoscrypto "github.com/cosmos/cosmos-sdk/crypto/types" - cosmostypes "github.com/cosmos/cosmos-sdk/types" ) // privKeyLen is the length of a secp256k1 private key. From 29f27267874fc8764431c9229d6518dc9164308e Mon Sep 17 00:00:00 2001 From: haodi Date: Sun, 20 Oct 2024 11:26:37 +0800 Subject: [PATCH 8/8] fixed lint --- client/server/distribution.go | 1 - client/server/staking.go | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/client/server/distribution.go b/client/server/distribution.go index 5abf5d8d..7e0971a3 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -12,7 +12,6 @@ import ( "github.com/gorilla/mux" "github.com/piplabs/story/client/server/utils" - "github.com/piplabs/story/lib/k1util" ) diff --git a/client/server/staking.go b/client/server/staking.go index f2104884..29a6915e 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -2,7 +2,6 @@ package server import ( - "github.com/piplabs/story/lib/k1util" "net/http" "strconv" @@ -12,6 +11,7 @@ import ( "github.com/gorilla/mux" "github.com/piplabs/story/client/server/utils" + "github.com/piplabs/story/lib/k1util" ) func (s *Server) initStakingRoute() {