forked from elastos/Elastos.ELA.SPV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
173 lines (141 loc) · 3.78 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
169
170
171
172
173
package client
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/elastos/Elastos.ELA.SPV/wallet/sutil"
"github.com/elastos/Elastos.ELA/common"
"github.com/howeyc/gopass"
"github.com/urfave/cli"
)
var CommonFlags = []cli.Flag{
cli.StringFlag{
Name: "password, p",
Usage: "keystore password",
},
}
func GetPassword(password []byte, confirmed bool) ([]byte, error) {
if len(password) > 0 {
return []byte(password), nil
}
fmt.Print("INPUT PASSWORD:")
password, err := gopass.GetPasswd()
if err != nil {
return nil, err
}
if !confirmed {
return password, nil
} else {
fmt.Print("CONFIRM PASSWORD:")
confirm, err := gopass.GetPasswd()
if err != nil {
return nil, err
}
if !bytes.Equal(password, confirm) {
return nil, errors.New("input password unmatched")
}
}
return password, nil
}
func ShowAccountInfo(password []byte) error {
var err error
password, err = GetPassword(password, false)
if err != nil {
return err
}
keyStore, err := OpenKeystore(password)
if err != nil {
return err
}
// print header
fmt.Printf("%5s %34s %66s %6s\n", "INDEX", "ADDRESS", "PUBLIC KEY", "TYPE")
fmt.Println("-----", strings.Repeat("-", 34), strings.Repeat("-", 66), "------")
// print accounts
for i, account := range keyStore.GetAccounts() {
accountType := "SUB"
if i == 0 {
accountType = "MASTER"
}
// print content
publicKey := account.PublicKey()
publicKeyBytes, _ := publicKey.EncodePoint(true)
fmt.Printf("%5d %-34s %-66s %6s\n", i+1, account.Address(), common.BytesToHexString(publicKeyBytes), accountType)
// print divider line
fmt.Println("-----", strings.Repeat("-", 34), strings.Repeat("-", 66), "------")
}
return nil
}
func SelectAccount(wallet *Wallet) (string, error) {
addrs, err := wallet.GetAddrs()
if err != nil || len(addrs) == 0 {
return "", errors.New("fail to load wallet addresses")
}
// only one address return it
if len(addrs) == 1 {
return addrs[0].String(), nil
}
// show accounts
err = ShowAccounts(addrs, nil, wallet)
if err != nil {
return "", err
}
// select address by index input
fmt.Println("Please input the address INDEX you want to use and press enter")
index := -1
for index == -1 {
index = getInput(len(addrs))
}
return addrs[index].String(), nil
}
func ShowAccounts(addrs []*sutil.Addr, newAddr *common.Uint168, wallet *Wallet) error {
// print header
fmt.Printf("%5s %34s %-20s%22s %6s\n", "INDEX", "ADDRESS", "BALANCE", "(LOCKED)", "TYPE")
fmt.Println("-----", strings.Repeat("-", 34), strings.Repeat("-", 42), "------")
currentHeight := wallet.BestHeight()
for i, addr := range addrs {
available := common.Fixed64(0)
locked := common.Fixed64(0)
UTXOs, err := wallet.GetAddressUTXOs(addr.Hash())
if err != nil {
return fmt.Errorf("get %s UTXOs failed, %s", addr, err)
}
for _, utxo := range UTXOs {
if utxo.LockTime >= currentHeight || utxo.AtHeight == 0 {
locked += utxo.Value
} else {
available += utxo.Value
}
}
var format = "%5d %34s %-20s%22s %6s\n"
if newAddr != nil && newAddr.IsEqual(*addr.Hash()) {
format = "\033[0;32m" + format + "\033[m"
}
fmt.Printf(format, i+1, addr.String(), available.String(), "("+locked.String()+")", addr.TypeName())
fmt.Println("-----", strings.Repeat("-", 34), strings.Repeat("-", 42), "------")
}
return nil
}
func getInput(max int) int {
fmt.Print("INPUT INDEX: ")
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
fmt.Println("read input falied")
return -1
}
// trim space
input = strings.TrimSpace(input)
index, err := strconv.ParseInt(input, 10, 32)
if err != nil {
fmt.Println("please input a positive integer")
return -1
}
if int(index) > max {
fmt.Println("INDEX should between 1 ~", max)
return -1
}
return int(index) - 1
}