forked from lizc2003/hdwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
168 lines (142 loc) · 4.42 KB
/
common.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
package wallet
import (
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/wire"
"github.com/ethereum/go-ethereum/params"
"github.com/tyler-smith/go-bip39"
"math"
"strconv"
)
type SegWitType int
const (
SymbolEth = "ETH"
SymbolBtc = "BTC"
SymbolTrx = "TRX"
BtcChainMainNet = int(wire.MainNet)
BtcChainTestNet3 = int(wire.TestNet3)
BtcChainRegtest = int(wire.TestNet)
BtcChainSimNet = int(wire.SimNet)
ChainMainNet = 1 // for ETH
ChainRopsten = 3 // for ETH
ChainRinkeby = 4 // for ETH
ChainGoerli = 5 // for ETH
ChainSepolia = 11155111
ChainBsc = 56 // for Binance Smart Chain Mainnet
ChainBscTestnet = 97 // for Binance Smart Chain Testnet
ChainMatic = 137 // for Polygon Matic Mainnet
ChainMaticTestnet = 80001 // for Polygon Matic Testnet
ChainPrivate = 1337 // for ETH
SegWitNone SegWitType = 0
SegWitScript SegWitType = 1
SegWitNative SegWitType = 2
ChangeTypeExternal = 0
ChangeTypeInternal = 1 // Usually used for change, not visible to the outside world
SatoshiPerBitcoin = 1e8
SunPerTrx = 1e6
GweiPerEther = 1e9
WeiPerGwei = 1e9
WeiPerEther = 1e18
EtherTransferGas = 21000
TokenShowDecimals = 9
)
var IsFixIssue172 = false
func GetBtcChainParams(chainId int) (*chaincfg.Params, error) {
switch chainId {
case BtcChainMainNet:
return &chaincfg.MainNetParams, nil
case BtcChainTestNet3:
return &chaincfg.TestNet3Params, nil
case BtcChainRegtest:
return &chaincfg.RegressionNetParams, nil
case BtcChainSimNet:
return &chaincfg.SimNetParams, nil
default:
return nil, fmt.Errorf("unknown btc chainId: %d", chainId)
}
}
func GetEthChainParams(chainId int) (*params.ChainConfig, error) {
switch chainId {
case ChainMainNet:
return params.MainnetChainConfig, nil
case ChainGoerli:
return params.GoerliChainConfig, nil
case ChainBsc:
return BscChainConfig, nil
case ChainBscTestnet:
return BscTestnetChainConfig, nil
case ChainPrivate:
return params.AllEthashProtocolChanges, nil
default:
return nil, fmt.Errorf("unknown eth chainId: %d", chainId)
}
}
func NewEntropy(bits int) (entropy []byte, err error) {
return bip39.NewEntropy(bits)
}
func NewMnemonic(bits int) (mnemonic string, err error) {
entropy, err := NewEntropy(bits)
if err != nil {
return "", err
}
return NewMnemonicByEntropy(entropy)
}
func NewMnemonicByEntropy(entropy []byte) (mnemonic string, err error) {
return bip39.NewMnemonic(entropy)
}
func EntropyFromMnemonic(mnemonic string) (entropy []byte, err error) {
return bip39.EntropyFromMnemonic(mnemonic)
}
func NewSeedFromMnemonic(mnemonic, password string) ([]byte, error) {
if mnemonic == "" {
return nil, errors.New("mnemonic is required")
}
return bip39.NewSeedWithErrorChecking(mnemonic, password)
}
func MakeBip44Path(symbol string, chainId int, accountIndex, changeType, index int) (string, error) {
return MakeBipXPath(44, symbol, chainId, accountIndex, changeType, index)
}
func MakeBip49Path(symbol string, chainId int, accountIndex, changeType, index int) (string, error) {
return MakeBipXPath(49, symbol, chainId, accountIndex, changeType, index)
}
func MakeBip84Path(symbol string, chainId int, accountIndex, changeType, index int) (string, error) {
return MakeBipXPath(84, symbol, chainId, accountIndex, changeType, index)
}
func MakeBipXPath(bipType int, symbol string, chainId int, accountIndex, changeType, index int) (string, error) {
var coinType int
switch symbol {
case SymbolEth:
coinType = 60
case SymbolBtc:
chainParams, err := GetBtcChainParams(chainId)
if err != nil {
return "", err
}
coinType = int(chainParams.HDCoinType)
case SymbolTrx:
coinType = 195
default:
return "", fmt.Errorf("invalid symbol: %s", symbol)
}
if accountIndex < 0 || index < 0 {
return "", errors.New("invalid account index or index")
}
if changeType != ChangeTypeExternal && changeType != ChangeTypeInternal {
return "", errors.New("invalid change type")
}
return fmt.Sprintf("m/%d'/%d'/%d'/%d/%d", bipType, coinType, accountIndex, changeType, index), nil
}
func FormatBtc(amount int64) string {
return FormatFloat(float64(amount)/SatoshiPerBitcoin, 8)
}
func FormatEth(amount int64) string {
return FormatFloat(float64(amount)/GweiPerEther, 9)
}
func FormatFloat(f float64, precision int) string {
d := float64(1)
if precision > 0 {
d = math.Pow10(precision)
}
return strconv.FormatFloat(math.Trunc(f*d)/d, 'f', -1, 64)
}