-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathokp4.go
84 lines (67 loc) · 1.68 KB
/
okp4.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package okp4lib
import (
"github.com/Megavolv/okp4lib/pkg/cosmos"
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
)
var KeysCdc *codec.LegacyAmino
func init() {
KeysCdc = codec.NewLegacyAmino()
cryptocodec.RegisterCrypto(KeysCdc)
KeysCdc.Seal()
config := sdk.GetConfig()
config.SetBech32PrefixForAccount("okp4", "okp4pub")
config.SetBech32PrefixForValidator("okp4valoper", "okp4valoperpub")
config.SetBech32PrefixForConsensusNode("okp4valcons", "okp4valconspub")
config.SetCoinType(118)
config.Seal()
}
type Okp4 struct {
}
func NewOkp4() *Okp4 {
return &Okp4{}
}
func (o *Okp4) GetEntropySeed() ([]byte, error) {
return bip39.NewEntropy(256)
}
func (o *Okp4) NewMnemonic(entropy []byte) (string, error) {
return bip39.NewMnemonic(entropy)
}
func (o *Okp4) CreateJsonedKey(name string) (string, error) {
key, err := o.CreateKey(name)
if err != nil {
return "", err
}
return key.ToJson()
}
func (o *Okp4) CreateKey(name string) (Key, error) {
entropy, err := o.GetEntropySeed()
if err != nil {
return Key{}, err
}
mnemonic, err := o.NewMnemonic(entropy)
if err != nil {
return Key{}, err
}
return o.createKey(name, mnemonic)
}
func (o *Okp4) createKey(name, mnemonic string) (Key, error) {
pk, err := cosmos.ParseMnemonic(mnemonic)
if err != nil {
return Key{}, err
}
pubKey := pk.PubKey()
addr := sdk.AccAddress(pubKey.Address())
return Key{
keyring.KeyOutput{
Name: name,
Type: "local",
Address: addr.String(),
PubKey: pubKey.String(),
Mnemonic: mnemonic,
},
}, nil
}