forked from ontio/ontology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
176 lines (165 loc) · 4.71 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
174
175
176
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package common
import (
"fmt"
"strconv"
"github.com/qbyyf/ontology/account"
"github.com/qbyyf/ontology/cmd/utils"
"github.com/qbyyf/ontology/common"
"github.com/qbyyf/ontology/common/config"
"github.com/qbyyf/ontology/common/password"
"github.com/urfave/cli"
)
func GetPasswd(ctx *cli.Context) ([]byte, error) {
var passwd []byte
var err error
if ctx.IsSet(utils.GetFlagName(utils.AccountPassFlag)) {
passwd = []byte(ctx.String(utils.GetFlagName(utils.AccountPassFlag)))
} else {
passwd, err = password.GetAccountPassword()
if err != nil {
return nil, fmt.Errorf("input password error: %s", err)
}
}
return passwd, nil
}
func OpenWallet(ctx *cli.Context) (account.Client, error) {
walletFile := ctx.String(utils.GetFlagName(utils.WalletFileFlag))
if walletFile == "" {
walletFile = config.DEFAULT_WALLET_FILE_NAME
}
if !common.FileExisted(walletFile) {
return nil, fmt.Errorf("cannot find wallet file: %s", walletFile)
}
wallet, err := account.Open(walletFile)
if err != nil {
return nil, err
}
return wallet, nil
}
func GetAccountMulti(wallet account.Client, passwd []byte, accAddr string) (*account.Account, error) {
//Address maybe address in base58, label or index
if accAddr == "" {
defAcc, err := wallet.GetDefaultAccount(passwd)
if err != nil {
return nil, err
}
return defAcc, nil
}
acc, err := wallet.GetAccountByAddress(accAddr, passwd)
if err != nil {
return nil, fmt.Errorf("getAccountByAddress %s error: %s", accAddr, err)
}
if acc != nil {
return acc, nil
}
acc, err = wallet.GetAccountByLabel(accAddr, passwd)
if err != nil {
return nil, fmt.Errorf("getAccountByLabel %s error: %s", accAddr, err)
}
if acc != nil {
return acc, nil
}
index, err := strconv.ParseInt(accAddr, 10, 32)
if err != nil {
return nil, fmt.Errorf("cannot get account by address: %s", accAddr)
}
acc, err = wallet.GetAccountByIndex(int(index), passwd)
if err != nil {
return nil, fmt.Errorf("getAccountByIndex %d error: %s", index, err)
}
if acc != nil {
return acc, nil
}
return nil, fmt.Errorf("cannot get account by address: %s", accAddr)
}
func GetAccountMetadataMulti(wallet account.Client, accAddr string) *account.AccountMetadata {
//Address maybe address in base58, label or index
if accAddr == "" {
fmt.Printf("Using default account: %s\n", accAddr)
return wallet.GetDefaultAccountMetadata()
}
acc := wallet.GetAccountMetadataByAddress(accAddr)
if acc != nil {
return acc
}
acc = wallet.GetAccountMetadataByLabel(accAddr)
if acc != nil {
return acc
}
index, err := strconv.ParseInt(accAddr, 10, 32)
if err != nil {
return nil
}
return wallet.GetAccountMetadataByIndex(int(index))
}
func GetAccount(ctx *cli.Context, address ...string) (*account.Account, error) {
wallet, err := OpenWallet(ctx)
if err != nil {
return nil, err
}
passwd, err := GetPasswd(ctx)
if err != nil {
return nil, err
}
defer ClearPasswd(passwd)
accAddr := ""
if len(address) > 0 {
accAddr = address[0]
} else {
accAddr = ctx.String(utils.GetFlagName(utils.AccountAddressFlag))
}
return GetAccountMulti(wallet, passwd, accAddr)
}
func IsBase58Address(address string) bool {
if address == "" {
return false
}
_, err := common.AddressFromBase58(address)
return err == nil
}
//ParseAddress return base58 address from base58, label or index
func ParseAddress(address string, ctx *cli.Context) (string, error) {
if IsBase58Address(address) {
return address, nil
}
wallet, err := OpenWallet(ctx)
if err != nil {
return "", err
}
acc := wallet.GetAccountMetadataByLabel(address)
if acc != nil {
return acc.Address, nil
}
index, err := strconv.ParseInt(address, 10, 32)
if err != nil {
return "", fmt.Errorf("cannot get account by address: %s", address)
}
acc = wallet.GetAccountMetadataByIndex(int(index))
if acc != nil {
return acc.Address, nil
}
return "", fmt.Errorf("cannot get account by address: %s", address)
}
func ClearPasswd(passwd []byte) {
size := len(passwd)
for i := 0; i < size; i++ {
passwd[i] = 0
}
}