-
Notifications
You must be signed in to change notification settings - Fork 2
/
wallet.go
44 lines (36 loc) · 1.48 KB
/
wallet.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
package wallet
import (
"github.com/rs/zerolog/log"
"github.com/cosmos/cosmos-sdk/crypto/keys/hd"
sdk "github.com/cosmos/cosmos-sdk/types"
bip39 "github.com/cosmos/go-bip39"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/libs/bech32"
)
// GetBech32AccAddrFromMnemonic returns bech32 account address from mnemonic seeds
func GetBech32AccAddrFromMnemonic(mnemonic string, password string) (string, error) {
seed := bip39.NewSeed(mnemonic, password)
masterKey, ch := hd.ComputeMastersFromSeed(seed)
privateKey, err := hd.DerivePrivateKeyForPath(masterKey, ch, hd.FullFundraiserPath)
if err != nil {
log.Error().Err(err).Msg("failed to derive private key from hd path")
return "", err
}
address, err := bech32.ConvertAndEncode(sdk.Bech32PrefixAccAddr, secp256k1.PrivKeySecp256k1(privateKey).PubKey().Address())
if err != nil {
log.Error().Err(err).Msg("failed to convert and encode to bech32 account address")
return "", err
}
return address, nil
}
// GetPrivateKeyFromMnemonic returns a private key from mnemonic seeds
func GetPrivateKeyFromMnemonic(mnemonic string, password string) ([32]byte, error) {
seed := bip39.NewSeed(mnemonic, password)
masterKey, ch := hd.ComputeMastersFromSeed(seed)
privateKey, err := hd.DerivePrivateKeyForPath(masterKey, ch, hd.FullFundraiserPath) // "44'/118'/0'/0/0"
if err != nil {
log.Error().Err(err).Msg("failed to derive private key from hd path")
return privateKey, err
}
return privateKey, nil
}