-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
64 lines (53 loc) · 1.48 KB
/
key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package config
import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
"github.com/ethereum/go-ethereum/common/hexutil"
"gitlab.com/distributed_lab/figure"
"gitlab.com/distributed_lab/kit/comfig"
"gitlab.com/distributed_lab/kit/kv"
"gitlab.com/distributed_lab/logan/v3/errors"
)
const AccountPrefix = "rarimo"
type KeyConf struct {
Sender cryptotypes.PrivKey
SenderAddress string
ChainId string
CoinName string
}
type Keyer interface {
Key() *KeyConf
}
type keyer struct {
getter kv.Getter
once comfig.Once
}
func NewKeyer(getter kv.Getter) Keyer {
return &keyer{
getter: getter,
}
}
func (k *keyer) Key() *KeyConf {
return k.once.Do(func() interface{} {
var config struct {
SenderPrvKeyHex string `fig:"sender_prv_hex"`
ChainId string `fig:"chain_id"`
CoinName string `fig:"coin_name"`
}
if err := figure.Out(&config).From(kv.MustGetStringMap(k.getter, "key")).Please(); err != nil {
panic(err)
}
sender := &secp256k1.PrivKey{Key: hexutil.MustDecode(config.SenderPrvKeyHex)}
address, err := bech32.ConvertAndEncode(AccountPrefix, sender.PubKey().Address().Bytes())
if err != nil {
panic(errors.Wrap(err, "failed to convert and encode sender address"))
}
return &KeyConf{
Sender: sender,
SenderAddress: address,
ChainId: config.ChainId,
CoinName: config.CoinName,
}
}).(*KeyConf)
}