-
Notifications
You must be signed in to change notification settings - Fork 41
/
keys.go
83 lines (69 loc) · 1.92 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 integration_tests
import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/ethereum/go-ethereum/common/hexutil"
hdwallet "github.com/miguelmota/go-ethereum-hdwallet"
)
func createMnemonic() (string, error) { // nolint:unused
entropySeed, err := bip39.NewEntropy(256)
if err != nil {
return "", err
}
mnemonic, err := bip39.NewMnemonic(entropySeed)
if err != nil {
return "", err
}
return mnemonic, nil
}
func createMemoryKeyFromMnemonic(name string, mnemonic string, passphrase string, hdPath *hd.BIP44Params) (*keyring.Info, *keyring.Keyring, error) {
kb, err := keyring.New("testnet", keyring.BackendMemory, "", nil)
if err != nil {
return nil, nil, err
}
keyringAlgos, _ := kb.SupportedAlgorithms()
algo, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), keyringAlgos)
if err != nil {
return nil, nil, err
}
var pathString string
if hdPath == nil {
pathString = sdk.FullFundraiserPath
} else {
pathString = hdPath.String()
}
account, err := kb.NewAccount(name, mnemonic, passphrase, pathString, algo)
if err != nil {
return nil, nil, err
}
return &account, &kb, nil
}
func ethereumKeyFromMnemonic(mnemonic string) (*ethereumKey, error) {
wallet, err := hdwallet.NewFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
path, err := hdwallet.ParseDerivationPath(DerivationPath)
if err != nil {
return nil, err
}
account, err := wallet.Derive(path, false)
if err != nil {
return nil, err
}
privateKeyBytes, err := wallet.PrivateKeyBytes(account)
if err != nil {
return nil, err
}
publicKeyBytes, err := wallet.PublicKeyBytes(account)
if err != nil {
return nil, err
}
return ðereumKey{
privateKey: hexutil.Encode(privateKeyBytes),
publicKey: hexutil.Encode(publicKeyBytes),
address: account.Address.String(),
}, nil
}