-
Notifications
You must be signed in to change notification settings - Fork 113
/
account.go
179 lines (155 loc) · 3.65 KB
/
account.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
package itest
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"sync"
"github.com/iost-official/go-iost/account"
"github.com/iost-official/go-iost/common"
"github.com/iost-official/go-iost/core/tx"
"github.com/iost-official/go-iost/crypto"
"github.com/iost-official/go-iost/ilog"
"strings"
)
// Account is account of user
type Account struct {
ID string
balance string
vote string
rw sync.RWMutex
key *Key
}
// AccountJSON is the json serialization of account
type AccountJSON struct {
ID string `json:"id"`
Balance string `json:"balance"`
Vote string `json:"vote"`
Seckey string `json:"seckey"`
Algorithm string `json:"algorithm"`
}
// NewAccount return a new account
func NewAccount(id string, seckey string, algorithm string) *Account {
account := &Account{
ID: id,
}
if seckey != "" {
account.key = NewKey(
common.Base58Decode(seckey),
crypto.NewAlgorithm(algorithm),
)
}
return account
}
// LoadAccounts will load accounts from file
func LoadAccounts(file string) ([]*Account, error) {
ilog.Infof("Load accounts from %v", file)
accounts := []*Account{}
files := []string{}
if strings.HasSuffix(file, "/") {
fis, err := ioutil.ReadDir(file)
if err != nil {
return nil, err
}
for _, f := range fis {
if !f.IsDir() && strings.HasSuffix(f.Name(), ".json") {
files = append(files, file+f.Name())
}
}
} else {
files = append(files, file)
}
for i := 0; i < len(files); i++ {
var data []byte
var err error
data, err = ioutil.ReadFile(files[i])
if err != nil {
return nil, err
}
taccs := []*Account{}
if err := json.Unmarshal(data, &taccs); err != nil {
return nil, err
}
accounts = append(accounts, taccs...)
}
return accounts, nil
}
// DumpAccounts will dump the accounts to file
func DumpAccounts(accounts []*Account, file string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
b, err := json.MarshalIndent(&accounts, "", " ")
if err != nil {
return err
}
if _, err := f.Write(b); err != nil {
return err
}
return nil
}
// Sign will sign the transaction by current account
func (a *Account) Sign(t *Transaction) (*Transaction, error) {
st, err := tx.SignTx(t.Tx, a.ID, []*account.KeyPair{a.key.KeyPair})
if err != nil {
return nil, err
}
transaction := &Transaction{
Tx: st,
}
return transaction, nil
}
// Balance will return the balance of this account
func (a *Account) Balance() float64 {
a.rw.RLock()
defer a.rw.RUnlock()
balance, err := strconv.ParseFloat(a.balance, 64)
if err != nil {
ilog.Errorf("Convert balance %v to string failed: %v", a.balance, err)
return 0
}
return balance
}
// AddBalance will add the balance of this account
func (a *Account) AddBalance(amount float64) {
a.rw.Lock()
defer a.rw.Unlock()
balance, err := strconv.ParseFloat(a.balance, 64)
if err != nil {
ilog.Errorf("Convert balance %v to string failed: %v", a.balance, err)
return
}
a.balance = fmt.Sprintf("%0.8f", balance+amount)
}
// UnmarshalJSON will unmarshal account from json
func (a *Account) UnmarshalJSON(b []byte) error {
aux := &AccountJSON{}
err := json.Unmarshal(b, aux)
if err != nil {
return err
}
a.ID = aux.ID
a.balance = aux.Balance
a.vote = aux.Vote
if aux.Seckey != "" {
a.key = NewKey(
common.Base58Decode(aux.Seckey),
crypto.NewAlgorithm(aux.Algorithm),
)
}
return nil
}
// MarshalJSON will marshal account to json
func (a *Account) MarshalJSON() ([]byte, error) {
aux := &AccountJSON{
ID: a.ID,
Balance: a.balance,
Vote: a.vote,
Seckey: common.Base58Encode(a.key.Seckey),
Algorithm: a.key.Algorithm.String(),
}
return json.Marshal(aux)
}