Skip to content

Commit 7d6ee8c

Browse files
psy2848048nowooj
authored andcommitted
feat: implemented more auth methods
1 parent 4392a9d commit 7d6ee8c

File tree

6 files changed

+310
-69
lines changed

6 files changed

+310
-69
lines changed

precompile/auth/IAuth.abi

+73-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,79 @@
1010
"name": "account",
1111
"outputs": [
1212
{
13-
"internalType": "bytes",
14-
"name": "addr",
15-
"type": "bytes"
13+
"internalType": "string",
14+
"name": "stringAddress",
15+
"type": "string"
16+
}
17+
],
18+
"stateMutability": "view",
19+
"type": "function"
20+
},
21+
{
22+
"inputs": [
23+
{
24+
"internalType": "address",
25+
"name": "evmAddress",
26+
"type": "address"
27+
}
28+
],
29+
"name": "addressBytesToString",
30+
"outputs": [
31+
{
32+
"internalType": "string",
33+
"name": "stringAddress",
34+
"type": "string"
35+
}
36+
],
37+
"stateMutability": "view",
38+
"type": "function"
39+
},
40+
{
41+
"inputs": [
42+
{
43+
"internalType": "string",
44+
"name": "stringAddress",
45+
"type": "string"
46+
}
47+
],
48+
"name": "addressStringToBytes",
49+
"outputs": [
50+
{
51+
"internalType": "address",
52+
"name": "byteAddress",
53+
"type": "address"
54+
}
55+
],
56+
"stateMutability": "view",
57+
"type": "function"
58+
},
59+
{
60+
"inputs": [],
61+
"name": "bech32Prefix",
62+
"outputs": [
63+
{
64+
"internalType": "string",
65+
"name": "prefix",
66+
"type": "string"
67+
}
68+
],
69+
"stateMutability": "view",
70+
"type": "function"
71+
},
72+
{
73+
"inputs": [
74+
{
75+
"internalType": "string",
76+
"name": "name",
77+
"type": "string"
78+
}
79+
],
80+
"name": "moduleAccountByName",
81+
"outputs": [
82+
{
83+
"internalType": "string",
84+
"name": "stringAddress",
85+
"type": "string"
1686
}
1787
],
1888
"stateMutability": "view",

precompile/auth/IAuth.sol

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,14 @@ IAuth constant AUTH_CONTRACT = IAuth(
88
);
99

1010
interface IAuth {
11-
function account(address evmAddress) external view returns (bytes calldata addr);
11+
// function accountAddressByID(uint accountId) external view returns (string calldata stringAddress);
12+
// function accounts(address evmAddress) external view returns (string[] calldata stringAddress);
13+
function account(address evmAddress) external view returns (string calldata stringAddress);
14+
// function params() external view returns (...);
15+
// function moduleAccounts() external view returns (string[] calldata stringAddresses);
16+
function moduleAccountByName(string calldata name) external view returns (string calldata stringAddress);
17+
function bech32Prefix() external view returns (string calldata prefix);
18+
function addressBytesToString(address evmAddress) external view returns (string calldata stringAddress);
19+
function addressStringToBytes(string calldata stringAddress) external view returns (address byteAddress);
20+
// function accountInfo(address evmAddress) external view returns (...);
1221
}

precompile/auth/auth.go

+54-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import (
66

77
"github.com/ethereum/go-ethereum/accounts/abi"
88
"github.com/ethereum/go-ethereum/common"
9+
ethcommon "github.com/ethereum/go-ethereum/common"
910
"github.com/ethereum/go-ethereum/core/vm"
1011

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

1314
"github.com/xpladev/ethermint/x/evm/statedb"
1415

1516
"github.com/xpladev/xpla/precompile/util"
17+
xplatypes "github.com/xpladev/xpla/types"
1618
)
1719

1820
var _ vm.PrecompiledContract = PrecompiledAuth{}
@@ -64,6 +66,14 @@ func (p PrecompiledAuth) Run(evm *vm.EVM, input []byte) ([]byte, error) {
6466
switch MethodAuth(abiMethod.Name) {
6567
case Account:
6668
return p.account(ctx, abiMethod, args)
69+
case ModuleAccountByName:
70+
return p.moduleAccountByName(ctx, abiMethod, args)
71+
case Bech32Prefix:
72+
return p.bech32Prefix(ctx, abiMethod, args)
73+
case AddressBytesToString:
74+
return p.addressBytesToString(ctx, abiMethod, args)
75+
case AddressStringToBytes:
76+
return p.addressStringToBytes(ctx, abiMethod, args)
6777
default:
6878
return nil, errors.New("method not found")
6979
}
@@ -81,9 +91,50 @@ func (p PrecompiledAuth) account(ctx sdk.Context, method *abi.Method, args []int
8191
account := p.ak.GetAccount(ctx, address)
8292
strAddress = account.GetAddress().String()
8393
} else {
84-
// should be address type
85-
strAddress = address.String()
94+
// cannot query
95+
strAddress = ""
8696
}
8797

88-
return method.Outputs.Pack([]byte(strAddress))
98+
return method.Outputs.Pack(strAddress)
99+
}
100+
101+
func (p PrecompiledAuth) moduleAccountByName(ctx sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) {
102+
moduleName, err := util.GetString(args[0])
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
account := p.ak.GetModuleAccount(ctx, moduleName)
108+
if account == nil {
109+
return method.Outputs.Pack("")
110+
} else {
111+
return method.Outputs.Pack(account.GetAddress().String())
112+
}
113+
}
114+
115+
func (p PrecompiledAuth) bech32Prefix(_ sdk.Context, method *abi.Method, _ []interface{}) ([]byte, error) {
116+
return method.Outputs.Pack(xplatypes.Bech32MainPrefix)
117+
}
118+
119+
func (p PrecompiledAuth) addressBytesToString(_ sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) {
120+
address, err := util.GetAccAddress(args[0])
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
return method.Outputs.Pack(address.String())
126+
}
127+
128+
func (p PrecompiledAuth) addressStringToBytes(_ sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) {
129+
stringAddress, err := util.GetString(args[0])
130+
if err != nil {
131+
return nil, err
132+
}
133+
134+
byteAddress, err := sdk.AccAddressFromBech32(stringAddress)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
return method.Outputs.Pack(ethcommon.BytesToAddress(byteAddress.Bytes()))
89140
}

precompile/auth/const.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ const (
88
type MethodAuth string
99

1010
const (
11-
Account MethodAuth = "account"
11+
Account MethodAuth = "account"
12+
ModuleAccountByName MethodAuth = "moduleAccountByName"
13+
Bech32Prefix MethodAuth = "bech32Prefix"
14+
AddressBytesToString MethodAuth = "addressBytesToString"
15+
AddressStringToBytes MethodAuth = "addressStringToBytes"
1216
)

precompile/auth/expected_keepers.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ import (
99
type AccountKeeper interface {
1010
GetAccount(ctx context.Context, addr sdk.AccAddress) (acc sdk.AccountI)
1111
HasAccount(ctx context.Context, addr sdk.AccAddress) bool
12+
GetModuleAccount(ctx context.Context, moduleName string) sdk.ModuleAccountI
1213
}

0 commit comments

Comments
 (0)