-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeys.go
83 lines (74 loc) · 2.26 KB
/
keys.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
package gosdk
import (
"github.com/NibiruChain/nibiru/app"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdktestutil "github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
)
var EncodingConfig = app.MakeEncodingConfig()
// NewKeyring: Creates an empty, in-memory keyring
func NewKeyring() keyring.Keyring {
var cdc codec.Codec = EncodingConfig.Marshaler
return keyring.NewInMemory(cdc)
}
// TODO: Is this needed?
// import (
// "bufio"
// "os"
// "path/filepath"
// )
// func NewKeyringLocal(nodeDir string) (keyring.Keyring) {
// clientDir := filepath.Join(nodeDir, "keyring")
// var cdc codec.Codec = EncodingConfig.Marshaler
// buf := bufio.NewReader(os.Stdin)
// return keyring.New(
// sdk.KeyringServiceName(),
// keyring.BackendTest,
// clientDir,
// buf,
// cdc,
// )
// }
func PrivKeyFromMnemonic(
kring keyring.Keyring, mnemonic string, keyName string,
) (cryptotypes.PrivKey, sdk.AccAddress, error) {
algo := hd.Secp256k1
overwrite := true
addr, secret, err := sdktestutil.GenerateSaveCoinKey(
kring, keyName, mnemonic, overwrite, algo,
)
if err != nil {
return &secp256k1.PrivKey{}, sdk.AccAddress{}, err
}
privKey := secp256k1.GenPrivKeyFromSecret([]byte(secret))
return privKey, addr, err
}
func CreateSigner(
mnemonic string,
kring keyring.Keyring,
keyName string,
) (kringRecord *keyring.Record, privKey cryptotypes.PrivKey, err error) {
privKey, _, err = PrivKeyFromMnemonic(kring, mnemonic, keyName)
if err != nil {
return kringRecord, privKey, err
}
kringRecord, err = CreateSignerFromPrivKey(privKey, keyName)
return kringRecord, privKey, err
}
func CreateSignerFromPrivKey(
privKey cryptotypes.PrivKey, keyName string,
) (*keyring.Record, error) {
return keyring.NewLocalRecord(keyName, privKey, privKey.PubKey())
}
func AddSignerToKeyring(
kring keyring.Keyring, privKey cryptotypes.PrivKey, keyName string,
) error {
passphrase := "password"
armor := crypto.EncryptArmorPrivKey(privKey, passphrase, privKey.Type())
return kring.ImportPrivKey(keyName, armor, passphrase)
}