Skip to content

Commit 02576cd

Browse files
committed
feat: expose governance authority address via precompile query
1 parent 4ab7ba4 commit 02576cd

10 files changed

Lines changed: 245 additions & 4 deletions

File tree

contracts/solidity/precompiles/gov/IGov.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,15 @@ interface IGov {
274274
/// @dev getConstitution returns the current constitution.
275275
/// @return constitution The current constitution
276276
function getConstitution() external view returns (string memory constitution);
277+
278+
/// @dev getAuthority returns the governance module account address.
279+
/// This address must be used as the `authority` field in parameter-change
280+
/// and software-upgrade proposal messages.
281+
/// @return authorityBech32 The governance module account address in Bech32 format
282+
/// @return authorityHex The governance module account address as an EVM address
283+
function getAuthority()
284+
external
285+
view
286+
returns (string memory authorityBech32, address authorityHex);
277287
}
278288

contracts/solidity/precompiles/testutil/contracts/GovCaller.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ contract GovCaller {
130130
return gov.GOV_CONTRACT.getParams();
131131
}
132132

133+
function getAuthority()
134+
external
135+
view
136+
returns (string memory authorityBech32, address authorityHex)
137+
{
138+
return gov.GOV_CONTRACT.getAuthority();
139+
}
140+
133141
function testDepositWithTransfer(
134142
uint64 _proposalId,
135143
types.Coin[] calldata _deposit,

precompiles/gov/IGov.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,5 +274,15 @@ interface IGov {
274274
/// @dev getConstitution returns the current constitution.
275275
/// @return constitution The current constitution
276276
function getConstitution() external view returns (string memory constitution);
277+
278+
/// @dev getAuthority returns the governance module account address.
279+
/// This address must be used as the `authority` field in parameter-change
280+
/// and software-upgrade proposal messages.
281+
/// @return authorityBech32 The governance module account address in Bech32 format
282+
/// @return authorityHex The governance module account address as an EVM address
283+
function getAuthority()
284+
external
285+
view
286+
returns (string memory authorityBech32, address authorityHex);
277287
}
278288

precompiles/gov/abi.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,5 +1103,23 @@
11031103
],
11041104
"stateMutability": "nonpayable",
11051105
"type": "function"
1106+
},
1107+
{
1108+
"inputs": [],
1109+
"name": "getAuthority",
1110+
"outputs": [
1111+
{
1112+
"internalType": "string",
1113+
"name": "authorityBech32",
1114+
"type": "string"
1115+
},
1116+
{
1117+
"internalType": "address",
1118+
"name": "authorityHex",
1119+
"type": "address"
1120+
}
1121+
],
1122+
"stateMutability": "view",
1123+
"type": "function"
11061124
}
11071125
]

precompiles/gov/gov.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ func (p Precompile) Execute(ctx sdk.Context, stateDB vm.StateDB, contract *vm.Co
141141
bz, err = p.GetParams(ctx, method, contract, args)
142142
case GetConstitutionMethod:
143143
bz, err = p.GetConstitution(ctx, method, contract, args)
144+
case GetAuthorityMethod:
145+
bz, err = p.GetAuthority(ctx, method, contract, args)
144146
default:
145147
return nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name)
146148
}

precompiles/gov/query.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package gov
22

33
import (
4+
"fmt"
5+
46
"github.com/ethereum/go-ethereum/accounts/abi"
7+
"github.com/ethereum/go-ethereum/common"
58
"github.com/ethereum/go-ethereum/core/vm"
69

10+
cmn "github.com/cosmos/evm/precompiles/common"
11+
712
sdk "github.com/cosmos/cosmos-sdk/types"
13+
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
14+
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
815
)
916

1017
const (
@@ -26,6 +33,8 @@ const (
2633
GetParamsMethod = "getParams"
2734
// GetConstitutionMethod defines the method name for the get constitution precompile request.
2835
GetConstitutionMethod = "getConstitution"
36+
// GetAuthorityMethod defines the method name for the get authority precompile request.
37+
GetAuthorityMethod = "getAuthority"
2938
)
3039

3140
// GetVotes implements the query logic for getting votes for a proposal.
@@ -233,3 +242,31 @@ func (p *Precompile) GetConstitution(
233242

234243
return method.Outputs.Pack(res.Constitution)
235244
}
245+
246+
// GetAuthority returns the governance module account address in both
247+
// Bech32 and EVM hex formats. This address must be used as the `authority`
248+
// field in parameter-change and software-upgrade proposal messages.
249+
func (p *Precompile) GetAuthority(
250+
ctx sdk.Context,
251+
method *abi.Method,
252+
_ *vm.Contract,
253+
args []interface{},
254+
) ([]byte, error) {
255+
if len(args) != 0 {
256+
return nil, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 0, len(args))
257+
}
258+
259+
// Derive the governance module account address.
260+
moduleAddr := authtypes.NewModuleAddress(govtypes.ModuleName)
261+
262+
// Convert to Bech32 string using the chain's account prefix.
263+
authorityBech32, err := p.addrCdc.BytesToString(moduleAddr.Bytes())
264+
if err != nil {
265+
return nil, fmt.Errorf("failed to encode authority address: %w", err)
266+
}
267+
268+
// Convert to EVM hex address.
269+
authorityHex := common.BytesToAddress(moduleAddr.Bytes())
270+
271+
return method.Outputs.Pack(authorityBech32, authorityHex)
272+
}

0 commit comments

Comments
 (0)