-
Notifications
You must be signed in to change notification settings - Fork 41
/
util.go
42 lines (34 loc) · 1.02 KB
/
util.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
package client
import (
"crypto/rand"
"github.com/cosmos/cosmos-sdk/crypto"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/pkg/errors"
log "github.com/xlab/suplog"
"github.com/InjectiveLabs/sdk-go/chain/crypto/hd"
)
// KeyringForPrivKey creates a temporary in-mem keyring for a PrivKey.
// Allows to init Context when the key has been provided in plaintext and parsed.
func KeyringForPrivKey(name string, privKey cryptotypes.PrivKey) (keyring.Keyring, error) {
kb := keyring.NewInMemory(hd.EthSecp256k1Option())
tmpPhrase := randPhrase(64)
armored := crypto.EncryptArmorPrivKey(privKey, tmpPhrase, privKey.Type())
err := kb.ImportPrivKey(name, armored, tmpPhrase)
if err != nil {
err = errors.Wrap(err, "failed to import privkey")
return nil, err
}
return kb, nil
}
func randPhrase(size int) string {
buf := make([]byte, size)
_, err := rand.Read(buf)
orPanic(err)
return string(buf)
}
func orPanic(err error) {
if err != nil {
log.Panicln()
}
}