diff --git a/client/server/distribution.go b/client/server/distribution.go index 66c87816..7e0971a3 100644 --- a/client/server/distribution.go +++ b/client/server/distribution.go @@ -12,21 +12,22 @@ import ( "github.com/gorilla/mux" "github.com/piplabs/story/client/server/utils" + "github.com/piplabs/story/lib/k1util" ) 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. @@ -46,13 +47,18 @@ 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) { + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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 @@ -63,13 +69,18 @@ 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) { + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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 { @@ -81,13 +92,18 @@ 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) { + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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 { @@ -140,13 +156,18 @@ 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) { + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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{ @@ -167,13 +188,18 @@ func (s *Server) GetValidatorSlashesByValidatorAddress(req *getValidatorSlashesB // GetDistributionValidatorsByDelegatorAddress queries the validators of a delegator. func (s *Server) GetDistributionValidatorsByDelegatorAddress(r *http.Request) (resp any, err error) { + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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 { @@ -185,13 +211,18 @@ 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) { + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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 { @@ -203,15 +234,24 @@ 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) { + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) if err != nil { return nil, err } - muxVars := mux.Vars(r) 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 { @@ -223,13 +263,18 @@ 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) { + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(mux.Vars(r)["delegator_pub_key"]) + if err != nil { + return nil, err + } + queryContext, err := s.createQueryContextByHeader(r) 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..29a6915e 100644 --- a/client/server/staking.go +++ b/client/server/staking.go @@ -11,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() { @@ -19,19 +20,21 @@ 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}", 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)) + 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 +132,13 @@ func (s *Server) GetValidatorByValidatorAddress(r *http.Request) (resp any, err return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress(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 +159,13 @@ func (s *Server) GetValidatorDelegationsByValidatorAddress(req *getValidatorDele return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress(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 +189,19 @@ func (s *Server) GetDelegationByValidatorAddressDelegatorAddress(r *http.Request return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +217,13 @@ func (s *Server) GetValidatorUnbondingDelegations(req *getValidatorUnbondingDele return nil, err } + valAddr, err := k1util.CmpPubKeyToValidatorAddress(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 +246,19 @@ func (s *Server) GetDelegatorUnbondingDelegation(r *http.Request) (resp any, err return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +274,13 @@ func (s *Server) GetDelegationsByDelegatorAddress(req *getDelegationsByDelegator return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +304,13 @@ func (s *Server) GetRedelegationsByDelegatorAddress(req *getRedelegationsByDeleg return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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{ @@ -283,6 +329,41 @@ 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) { + 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(queryContext, delAddr) + if err != nil { + return nil, err + } + + delRewardEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorRewardAddress.Get(queryContext, delAddr) + if err != nil { + return nil, err + } + + delOperatorEvmAddr, err := s.store.GetEvmStakingKeeper().DelegatorOperatorAddress.Get(queryContext, 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) @@ -290,8 +371,13 @@ func (s *Server) GetUnbondingDelegationsByDelegatorAddress(req *getUnbondingDele return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +401,13 @@ func (s *Server) GetValidatorsByDelegatorAddress(req *getValidatorsByDelegatorAd return nil, err } + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +437,19 @@ func (s *Server) GetValidatorsByDelegatorAddressValidatorAddress(r *http.Request return nil, err } - muxVars := mux.Vars(r) + valAddr, err := k1util.CmpPubKeyToValidatorAddress(mux.Vars(r)["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddr, err := k1util.CmpPubKeyToDelegatorAddress(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 +471,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(muxVars["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress(muxVars["delegator_pub_key"]) if err != nil { return nil, err } - delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(muxVars["delegator_addr"]) + valAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(valAddrStr) + if err != nil { + return nil, err + } + + delAddr, err := s.store.GetAccountKeeper().AddressCodec().StringToBytes(delAddrStr) if err != nil { return nil, err } @@ -392,12 +502,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(muxVars["validator_pub_key"]) + if err != nil { + return nil, err + } + + delAddrStr, err := k1util.CmpPubKeyToDelegatorAddress(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..e040a476 100644 --- a/lib/k1util/k1util.go +++ b/lib/k1util/k1util.go @@ -3,12 +3,15 @@ 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" @@ -178,3 +181,35 @@ func CosmosPubkeyToEVMAddress(pubkeyCmp []byte) (addr common.Address, err error) return addr, nil } + +func CmpPubKeyToDelegatorAddress(cmpPubKeyHex string) (string, error) { + pubKey, err := decodePubKeyFromHex(cmpPubKeyHex) + if err != nil { + return "", errors.Wrap(err, "invalid compressed public key") + } + + return cosmostypes.AccAddress(pubKey.Address().Bytes()).String(), nil +} + +func CmpPubKeyToValidatorAddress(cmpPubKeyHex string) (string, error) { + 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 nil, errors.New("invalid compressed public key", "length", len(cmpPubKey)) + } + + return &cosmosk1.PubKey{Key: cmpPubKey}, nil +}