-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
162 lines (138 loc) · 4.26 KB
/
utils.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
package tx
import (
"fmt"
"math"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/thetatoken/theta/cmd/thetacli/cmd/utils"
"github.com/thetatoken/theta/common"
"github.com/thetatoken/theta/wallet"
"github.com/thetatoken/theta/wallet/types"
wtypes "github.com/thetatoken/theta/wallet/types"
)
const HARDENED_FLAG = 1 << 31
func walletUnlock(cmd *cobra.Command, addressStr string) (wtypes.Wallet, common.Address, error) {
return walletUnlockWithPath(cmd, addressStr, "")
}
func walletUnlockWithPath(cmd *cobra.Command, addressStr string, path string) (wtypes.Wallet, common.Address, error) {
var wallet wtypes.Wallet
var address common.Address
var err error
walletType := getWalletType(cmd)
if walletType == wtypes.WalletTypeSoft {
cfgPath := cmd.Flag("config").Value.String()
wallet, address, err = SoftWalletUnlock(cfgPath, addressStr)
} else {
derivationPath, err := parseDerivationPath(path, walletType)
if err != nil {
return nil, common.Address{}, err
}
wallet, address, err = ColdWalletUnlock(walletType, derivationPath)
}
return wallet, address, err
}
func ColdWalletUnlock(walletType wtypes.WalletType, derivationPath types.DerivationPath) (wtypes.Wallet, common.Address, error) {
wallet, err := wallet.OpenWallet("", walletType, true)
if err != nil {
fmt.Printf("Failed to open wallet: %v\n", err)
return nil, common.Address{}, err
}
err = wallet.Unlock(common.Address{}, "", derivationPath)
if err != nil {
fmt.Printf("Failed to unlock wallet: %v\n", err)
return nil, common.Address{}, err
}
addresses, err := wallet.List()
if err != nil {
fmt.Printf("Failed to list wallet addresses: %v\n", err)
return nil, common.Address{}, err
}
if len(addresses) == 0 {
errMsg := fmt.Sprintf("No address detected in the wallet\n")
fmt.Printf(errMsg)
return nil, common.Address{}, fmt.Errorf(errMsg)
}
address := addresses[0]
log.Infof("Wallet address: %v", address)
return wallet, address, nil
}
func SoftWalletUnlock(cfgPath, addressStr string) (wtypes.Wallet, common.Address, error) {
wallet, err := wallet.OpenWallet(cfgPath, wtypes.WalletTypeSoft, true)
if err != nil {
fmt.Printf("Failed to open wallet: %v\n", err)
return nil, common.Address{}, err
}
prompt := fmt.Sprintf("Please enter password: ")
password, err := utils.GetPassword(prompt)
if err != nil {
fmt.Printf("Failed to get password: %v\n", err)
return nil, common.Address{}, err
}
address := common.HexToAddress(addressStr)
err = wallet.Unlock(address, password, nil)
if err != nil {
fmt.Printf("Failed to unlock address %v: %v\n", address.Hex(), err)
return nil, common.Address{}, err
}
return wallet, address, nil
}
func getWalletType(cmd *cobra.Command) (walletType wtypes.WalletType) {
walletTypeStr := cmd.Flag("wallet").Value.String()
if walletTypeStr == "nano" {
walletType = wtypes.WalletTypeColdNano
} else if walletTypeStr == "trezor" {
walletType = wtypes.WalletTypeColdTrezor
} else {
walletType = wtypes.WalletTypeSoft
}
return walletType
}
func parseDerivationPath(nstr string, walletType wtypes.WalletType) (types.DerivationPath, error) {
if len(nstr) == 0 {
if walletType == wtypes.WalletTypeColdNano {
// nstr = "m/44'/60'/0'/0"
return types.DefaultRootDerivationPath, nil
} else if walletType == wtypes.WalletTypeColdTrezor {
// nstr = "m/44'/60'/0'/0/0"
return types.DefaultBaseDerivationPath, nil
} else {
return nil, fmt.Errorf("can't parse derivation path for soft wallet")
}
}
n := strings.Split(nstr, "/")
// m/a/b/c => a/b/c
if n[0] == "m" {
n = n[1:]
}
derivationPath := types.DerivationPath{}
for _, i := range n {
p, err := strToHarden(i)
if err != nil {
return derivationPath, err
}
derivationPath = append(derivationPath, uint32(p))
}
return derivationPath, nil
}
func strToHarden(x string) (int, error) {
if strings.HasPrefix(x, "-") {
i, err := strconv.Atoi(x)
if err != nil {
return 0, err
}
return H(int(math.Abs(float64(i)))), nil
} else if strings.HasSuffix(x, "h") || strings.HasSuffix(x, "'") {
i, err := strconv.Atoi(x[:len(x)-1])
if err != nil {
return 0, err
}
return H(i), nil
}
return strconv.Atoi(x)
}
// Shortcut function that "hardens" a number in a BIP44 path.
func H(x int) int {
return x | HARDENED_FLAG
}