-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaddrgen.go
192 lines (161 loc) · 4.47 KB
/
addrgen.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package addrgen
import (
"crypto/ecdsa"
"strings"
"github.com/btcsuite/btcd/btcutil"
hd "github.com/btcsuite/btcd/btcutil/hdkeychain"
"errors"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/ethereum/go-ethereum/crypto"
tronAddress "github.com/fbsobreira/gotron-sdk/pkg/address"
)
type param struct {
network *chaincfg.Params
exec func(string, int, *chaincfg.Params) (string, error)
}
var keyMap = map[string]param{
"upub": {&chaincfg.TestNet3Params, bip49},
"tpub": {&chaincfg.TestNet3Params, bip44},
"vpub": {&chaincfg.TestNet3Params, bip141},
"xpub": {&chaincfg.MainNetParams, bip44},
"ypub": {&chaincfg.MainNetParams, bip49},
"zpub": {&chaincfg.MainNetParams, bip141},
}
var dashKeyHashAddrID = map[string]byte{
"xpub": 0x4c,
"tpub": 0x8c,
}
type Network string
const (
ETH Network = "ETH"
BTC Network = "BTC"
tBTC Network = "tBTC"
TRON Network = "TRON"
DASH Network = "DASH"
)
var networkGenerators = map[Network]func(string, int) (string, error){
ETH: GenerateETH,
BTC: GenerateBTC,
TRON: GenerateTron,
DASH: GenerateDash,
}
func Generate(network Network, pubKey string, index int) (string, error) {
generator, ok := networkGenerators[network]
if !ok {
return "", errors.New("unsupported network")
}
return generator(pubKey, index)
}
func GenerateBTC(pubKey string, index int) (string, error) {
keyType := strings.ToLower(pubKey)[:4]
executor, ok := keyMap[keyType]
if !ok {
return "", errors.New("invalid pubkey")
}
return executor.exec(pubKey, index, executor.network)
}
func GenerateDash(pubKey string, index int) (string, error) {
keyType := strings.ToLower(pubKey)[:4]
hashAddrID, ok := dashKeyHashAddrID[keyType]
if !ok {
return "", errors.New("invalid pubkey")
}
extKey, _ := hd.NewKeyFromString(pubKey)
extKeyChild0, _ := extKey.Derive(0)
extKeyChild01, _ := extKeyChild0.Derive(uint32(index))
net := chaincfg.Params{PubKeyHashAddrID: hashAddrID}
pk01, _ := extKeyChild01.Address(&net)
return pk01.EncodeAddress(), nil
}
func GenerateETH(xpubKey string, index int) (string, error) {
pkECDSA, err := getPubKey(xpubKey, index)
if err != nil {
return "", err
}
address := crypto.PubkeyToAddress(*pkECDSA)
return address.Hex(), nil
}
func getPubKey(xpubKey string, index int) (pkECDSA *ecdsa.PublicKey, err error) {
extKey, err := hd.NewKeyFromString(xpubKey)
if err != nil {
return pkECDSA, err
}
extKeyChild0, err := extKey.Derive(uint32(index))
if err != nil {
return pkECDSA, err
}
pubKey, err := extKeyChild0.ECPubKey()
if err != nil {
return pkECDSA, err
}
pkECDSA = pubKey.ToECDSA()
return pkECDSA, nil
}
func GenerateTron(xpubKey string, index int) (string, error) {
pkECDSA, err := getPubKey(xpubKey, index)
if err != nil {
return "", err
}
address := tronAddress.PubkeyToAddress(*pkECDSA)
return address.String(), nil
}
func bip44(mpubKey string, n int, ntwk *chaincfg.Params) (string, error) {
extKey, _ := hd.NewKeyFromString(mpubKey)
extKeyChild0, _ := extKey.Derive(0)
extKeyChild01, _ := extKeyChild0.Derive(uint32(n))
pk01, _ := extKeyChild01.Address(ntwk)
return pk01.EncodeAddress(), nil
}
func bip49(mpubKey string, n int, ntwk *chaincfg.Params) (string, error) {
acct0Pub, err := hd.NewKeyFromString(mpubKey)
if err != nil {
return "", err
}
acct0ExternalPub, err := acct0Pub.Derive(0)
if err != nil {
return "", err
}
acct0External0Pub, err := acct0ExternalPub.Derive(uint32(n))
if err != nil {
return "", err
}
pubKey, err := acct0External0Pub.ECPubKey()
if err != nil {
return "", err
}
keyHash := btcutil.Hash160(pubKey.SerializeCompressed())
scriptSig, err := txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(keyHash).Script()
if err != nil {
return "", err
}
acct0ExtAddr0, err := btcutil.NewAddressScriptHash(scriptSig, ntwk)
if err != nil {
return "", err
}
return acct0ExtAddr0.EncodeAddress(), nil
}
func bip141(mpubKey string, n int, ntwk *chaincfg.Params) (string, error) {
acct0Pub, err := hd.NewKeyFromString(mpubKey)
if err != nil {
return "", err
}
acct0ExternalPub, err := acct0Pub.Derive(0)
if err != nil {
return "", err
}
acct0External0Pub, err := acct0ExternalPub.Derive(uint32(n))
if err != nil {
return "", err
}
pubKey, err := acct0External0Pub.ECPubKey()
if err != nil {
return "", err
}
keyHash := btcutil.Hash160(pubKey.SerializeCompressed())
acct0ExtAddr0, err := btcutil.NewAddressWitnessPubKeyHash(keyHash, ntwk)
if err != nil {
return "", err
}
return acct0ExtAddr0.EncodeAddress(), nil
}