-
Notifications
You must be signed in to change notification settings - Fork 3
/
chains.go
149 lines (128 loc) · 4.04 KB
/
chains.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"encoding/hex"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/btcsuite/btcd/chaincfg"
coskey "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/crypto"
)
type EthereumKeys struct {
PrivateKey string
PublicKey string
Address string
}
type BitcoinKeys struct {
ExtendedPrivateKey string
PublicKey string
Address string
WIF string
}
type ThorchainKeys struct {
ExtendedPrivateKey string
PrivateKey string
PublicKey string
Address string
}
type MayachainKeys struct {
ExtendedPrivateKey string
PrivateKey string
PublicKey string
Address string
}
type Keys interface{}
type ActionFunc func(*hdkeychain.ExtendedKey) (Keys, error)
func showEthereumKey(extendedPrivateKey *hdkeychain.ExtendedKey) (Keys, error) {
nonHardenedPubKey, err := extendedPrivateKey.ECPubKey()
if err != nil {
return nil, err
}
nonHardenedPrivKey, err := extendedPrivateKey.ECPrivKey()
if err != nil {
return nil, err
}
keys := &EthereumKeys{
PrivateKey: hex.EncodeToString(nonHardenedPrivKey.Serialize()),
PublicKey: hex.EncodeToString(nonHardenedPubKey.SerializeCompressed()),
Address: crypto.PubkeyToAddress(*nonHardenedPubKey.ToECDSA()).Hex(),
}
return keys, nil
}
func showBitcoinKey(extendedPrivateKey *hdkeychain.ExtendedKey) (Keys, error) {
net := &chaincfg.MainNetParams
nonHardenedPubKey, err := extendedPrivateKey.ECPubKey()
if err != nil {
return nil, err
}
nonHardenedPrivKey, err := extendedPrivateKey.ECPrivKey()
if err != nil {
return nil, err
}
wif, err := btcutil.NewWIF(nonHardenedPrivKey, net, true)
if err != nil {
return nil, err
}
addressPubKey, err := btcutil.NewAddressWitnessPubKeyHash(btcutil.Hash160(nonHardenedPubKey.SerializeCompressed()), net)
if err != nil {
return nil, err
}
keys := &BitcoinKeys{
ExtendedPrivateKey: extendedPrivateKey.String(),
PublicKey: hex.EncodeToString(nonHardenedPubKey.SerializeCompressed()),
Address: addressPubKey.EncodeAddress(),
WIF: wif.String(),
}
return keys, nil
}
func showThorchainKey(extendedPrivateKey *hdkeychain.ExtendedKey) (Keys, error) {
nonHardenedPubKey, err := extendedPrivateKey.ECPubKey()
if err != nil {
return nil, err
}
nonHardenedPrivKey, err := extendedPrivateKey.ECPrivKey()
if err != nil {
return nil, err
}
config := sdk.GetConfig()
config.SetBech32PrefixForAccount("thor", "thorpub")
config.SetBech32PrefixForValidator("thorv", "thorvpub")
config.SetBech32PrefixForConsensusNode("thorc", "thorcpub")
compressedPubkey := coskey.PubKey{
Key: nonHardenedPubKey.SerializeCompressed(),
}
addr := types.AccAddress(compressedPubkey.Address().Bytes())
keys := &ThorchainKeys{
ExtendedPrivateKey: extendedPrivateKey.String(),
PrivateKey: hex.EncodeToString(nonHardenedPrivKey.Serialize()),
PublicKey: hex.EncodeToString(nonHardenedPubKey.SerializeCompressed()),
Address: addr.String(),
}
return keys, nil
}
func showMayachainKey(extendedPrivateKey *hdkeychain.ExtendedKey) (Keys, error) {
nonHardenedPubKey, err := extendedPrivateKey.ECPubKey()
if err != nil {
return nil, err
}
nonHardenedPrivKey, err := extendedPrivateKey.ECPrivKey()
if err != nil {
return nil, err
}
config := sdk.GetConfig()
config.SetBech32PrefixForAccount("maya", "mayapub")
config.SetBech32PrefixForValidator("mayav", "mayavpub")
config.SetBech32PrefixForConsensusNode("mayac", "mayacpub")
compressedPubkey := coskey.PubKey{
Key: nonHardenedPubKey.SerializeCompressed(),
}
addr := types.AccAddress(compressedPubkey.Address().Bytes())
keys := &MayachainKeys{
ExtendedPrivateKey: extendedPrivateKey.String(),
PrivateKey: hex.EncodeToString(nonHardenedPrivKey.Serialize()),
PublicKey: hex.EncodeToString(nonHardenedPubKey.SerializeCompressed()),
Address: addr.String(),
}
return keys, nil
}