@@ -6,14 +6,16 @@ import (
6
6
"fmt"
7
7
"os"
8
8
9
+ addresscodec "cosmossdk.io/core/address"
9
10
dkeyring "github.com/99designs/keyring"
11
+
10
12
"github.com/cosmos/cosmos-sdk/codec"
13
+ "github.com/cosmos/cosmos-sdk/codec/address"
11
14
"github.com/cosmos/cosmos-sdk/codec/types"
12
15
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
13
16
"github.com/cosmos/cosmos-sdk/crypto/hd"
14
17
"github.com/cosmos/cosmos-sdk/crypto/keyring"
15
18
sdktypes "github.com/cosmos/cosmos-sdk/types"
16
- "github.com/cosmos/cosmos-sdk/types/bech32"
17
19
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
18
20
"github.com/cosmos/go-bip39"
19
21
@@ -22,7 +24,7 @@ import (
22
24
23
25
const (
24
26
// KeyringServiceName used for the name of keyring in OS backend.
25
- KeyringServiceName = "starport "
27
+ KeyringServiceName = "ignite "
26
28
27
29
// DefaultAccount is the name of the default account.
28
30
DefaultAccount = "default"
@@ -34,6 +36,7 @@ var KeyringHome = os.ExpandEnv("$HOME/.ignite/accounts")
34
36
var ErrAccountExists = errors .New ("account already exists" )
35
37
36
38
const (
39
+ CoinTypeCosmos = sdktypes .CoinType
37
40
AccountPrefixCosmos = "cosmos"
38
41
)
39
42
@@ -58,6 +61,8 @@ type Registry struct {
58
61
homePath string
59
62
keyringServiceName string
60
63
keyringBackend KeyringBackend
64
+ addressCodec addresscodec.Codec
65
+ coinType uint32
61
66
62
67
Keyring keyring.Keyring
63
68
}
@@ -83,12 +88,26 @@ func WithKeyringBackend(backend KeyringBackend) Option {
83
88
}
84
89
}
85
90
91
+ func WithBech32Prefix (prefix string ) Option {
92
+ return func (c * Registry ) {
93
+ c .addressCodec = address .NewBech32Codec (prefix )
94
+ }
95
+ }
96
+
97
+ func WithCoinType (coinType uint32 ) Option {
98
+ return func (c * Registry ) {
99
+ c .coinType = coinType
100
+ }
101
+ }
102
+
86
103
// New creates a new registry to manage accounts.
87
104
func New (options ... Option ) (Registry , error ) {
88
105
r := Registry {
89
106
keyringServiceName : sdktypes .KeyringServiceName (),
90
107
keyringBackend : KeyringTest ,
91
108
homePath : KeyringHome ,
109
+ addressCodec : address .NewBech32Codec (AccountPrefixCosmos ),
110
+ coinType : CoinTypeCosmos ,
92
111
}
93
112
94
113
for _ , apply := range options {
@@ -145,7 +164,13 @@ func (a Account) Address(accPrefix string) (string, error) {
145
164
return "" , err
146
165
}
147
166
148
- return toBech32 (accPrefix , pk .Address ())
167
+ addressCodec := address .NewBech32Codec (accPrefix )
168
+ addr , err := addressCodec .BytesToString (pk .Address ())
169
+ if err != nil {
170
+ return "" , err
171
+ }
172
+
173
+ return addr , nil
149
174
}
150
175
151
176
// PubKey returns a public key for account.
@@ -158,14 +183,6 @@ func (a Account) PubKey() (string, error) {
158
183
return pk .String (), nil
159
184
}
160
185
161
- func toBech32 (prefix string , addr []byte ) (string , error ) {
162
- bech32Addr , err := bech32 .ConvertAndEncode (prefix , addr )
163
- if err != nil {
164
- return "" , err
165
- }
166
- return bech32Addr , nil
167
- }
168
-
169
186
// EnsureDefaultAccount ensures that default account exists.
170
187
func (r Registry ) EnsureDefaultAccount () error {
171
188
_ , err := r .GetByName (DefaultAccount )
@@ -288,11 +305,11 @@ func (r Registry) GetByName(name string) (Account, error) {
288
305
289
306
// GetByAddress returns an account by its address.
290
307
func (r Registry ) GetByAddress (address string ) (Account , error ) {
291
- sdkAddr , err := sdktypes . AccAddressFromBech32 (address )
308
+ sdkAddr , err := r . addressCodec . StringToBytes (address )
292
309
if err != nil {
293
310
return Account {}, err
294
311
}
295
- record , err := r .Keyring .KeyByAddress (sdkAddr )
312
+ record , err := r .Keyring .KeyByAddress (sdktypes . AccAddress ( sdkAddr ) )
296
313
if errors .Is (err , dkeyring .ErrKeyNotFound ) || errors .Is (err , sdkerrors .ErrKeyNotFound ) {
297
314
return Account {}, & AccountDoesNotExistError {address }
298
315
}
@@ -334,7 +351,7 @@ func (r Registry) DeleteByName(name string) error {
334
351
}
335
352
336
353
func (r Registry ) hdPath () string {
337
- return hd .CreateHDPath (sdktypes . GetConfig (). GetCoinType () , 0 , 0 ).String ()
354
+ return hd .CreateHDPath (r . coinType , 0 , 0 ).String ()
338
355
}
339
356
340
357
func (r Registry ) algo () (keyring.SignatureAlgo , error ) {
0 commit comments