-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_pair.go
62 lines (51 loc) · 1.75 KB
/
key_pair.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
package crypto
import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
)
type KeyPair struct {
Public cryptotypes.PubKey
Private cryptotypes.PrivKey
}
var _ BytesSigner = (*KeyPair)(nil)
// NewCosmosKeyPairFromMnemonic returns a key pair derived from the given mnemonic, with coin type 118 (cosmos)
func NewCosmosKeyPairFromMnemonic(mnemonic string) *KeyPair {
bip44Path := sdk.GetConfig().GetFullBIP44Path()
return newKeyPairFromMnemonic(mnemonic, bip44Path)
}
// Return a key pair derived from the given mnemonic with the given coin type
func NewKeyPairFromMnemonic(mnemonic string, coinType uint32) *KeyPair {
// Futz with the config but reset it so that we don't get confused in future calls
config := sdk.GetConfig()
config.SetCoinType(coinType)
bip44Path := config.GetFullBIP44Path()
config.SetCoinType(118)
return newKeyPairFromMnemonic(mnemonic, bip44Path)
}
func newKeyPairFromMnemonic(mnemonic, bip44Path string) *KeyPair {
// create master key and derive first key for keyring
algo := hd.Secp256k1
derivedPriv, _ := algo.Derive()(mnemonic, keyring.DefaultBIP39Passphrase, bip44Path)
privKey := algo.Generate()(derivedPriv)
pubKey := privKey.PubKey()
return &KeyPair{
Public: pubKey,
Private: privKey,
}
}
func (kp *KeyPair) GetAddress(prefix string) string {
address := sdk.AccAddress(kp.Public.Address())
encoded, _ := bech32.ConvertAndEncode(prefix, address)
return encoded
}
func (kp *KeyPair) SignBytes(
bytesToSign []byte,
) ([]byte, error) {
return kp.Private.Sign(bytesToSign)
}
func (kp *KeyPair) GetPublicKey() cryptotypes.PubKey {
return kp.Public
}