-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.go
67 lines (54 loc) · 1.47 KB
/
mem.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
package common
import (
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/cosmos/btcutil/bech32"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/go-bip39"
)
func getPrivKeyFromMnemonic(mnemonic string) ([]byte, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return nil, err
}
masterpriv, ch := hd.ComputeMastersFromSeed(seed)
path := "m/44'/118'/0'/0/0'"
priv, err := hd.DerivePrivateKeyForPath(masterpriv, ch, path)
if err != nil {
return nil, err
}
return priv, nil
}
func getSeedFromMnemonic(mnemonic string) (string, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "")
if err != nil {
return "", err
}
return hex.EncodeToString(seed), nil
}
// func getAddressFromPrivKey(privKey secp256k1.PrivKey) (sdk.AccAddress, error) {
// return sdk.AccAddress(privKey.PubKey().Address()), nil
// }
func GetPubKeyFromPriv(priv []byte) []byte {
_, ecPub := btcec.PrivKeyFromBytes(priv[:])
pubkeyBytes := ecPub.SerializeCompressed()
return pubkeyBytes
}
func GetAddrFromPriv(priv []byte) (string, error) {
pubkeyBytes := GetPubKeyFromPriv(priv)
decodeString, err := hex.DecodeString(fmt.Sprintf("04%x", pubkeyBytes))
if err != nil {
return "", err
}
// Convert test data to base32:
conv, err := bech32.ConvertBits(decodeString, 8, 5, true)
if err != nil {
return "", err
}
encoded, err := bech32.Encode("atom", conv)
if err != nil {
return "", err
}
return encoded, nil
}