-
Notifications
You must be signed in to change notification settings - Fork 12
/
account.go
185 lines (171 loc) · 4.62 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
180
181
182
183
184
185
package wallet
import (
"encoding/hex"
"errors"
"math/big"
"github.com/hectorchu/gonano/rpc"
"github.com/hectorchu/gonano/util"
)
// Account represents a wallet account.
type Account struct {
w *Wallet
index uint32
key, pubkey []byte
address string
representative string
}
// Address returns the address of the account.
func (a *Account) Address() string {
return a.address
}
// Index returns the derivation index of the account.
func (a *Account) Index() uint32 {
return a.index
}
// Balance gets the confirmed and pending balances for account.
func (a *Account) Balance() (balance, pending *big.Int, err error) {
b, p, err := a.w.RPC.AccountBalance(a.address)
if err != nil {
return
}
return &b.Int, &p.Int, nil
}
// Send sends an amount to an account.
func (a *Account) Send(account string, amount *big.Int) (hash rpc.BlockHash, err error) {
block, err := a.SendBlock(account, amount)
if err != nil {
return
}
if block.Work, err = a.w.workGenerate(block.Previous); err != nil {
return
}
return a.w.RPC.Process(block, "send")
}
// SendBlock generates a signed send block.
func (a *Account) SendBlock(account string, amount *big.Int) (block *rpc.Block, err error) {
link, err := util.AddressToPubkey(account)
if err != nil {
return
}
info, err := a.w.RPC.AccountInfo(a.address)
if err != nil {
return
}
if a.representative == "" {
a.representative = info.Representative
}
if info.Balance.Sub(&info.Balance.Int, amount).Sign() < 0 {
return nil, errors.New("insufficient funds")
}
block = &rpc.Block{
Type: "state",
Account: a.address,
Previous: info.Frontier,
Representative: a.representative,
Balance: info.Balance,
Link: link,
}
return block, a.w.impl.signBlock(a, block)
}
// ReceivePendings pockets all pending amounts.
func (a *Account) ReceivePendings() (err error) {
pendings, err := a.w.RPC.AccountsPending([]string{a.address}, -1)
if err != nil {
return
}
return a.receivePendings(pendings[a.address])
}
// ReceivePending pockets the specified link block.
func (a *Account) ReceivePending(link rpc.BlockHash) (hash rpc.BlockHash, err error) {
info, err := a.w.RPC.AccountInfo(a.address)
if err != nil {
info.Balance = &rpc.RawAmount{}
}
block, err := a.w.RPC.BlockInfo(link)
if err != nil {
return
}
info.Balance.Add(&info.Balance.Int, &block.Amount.Int)
return a.receivePending(info, link)
}
func (a *Account) receivePendings(pendings rpc.HashToPendingMap) (err error) {
if len(pendings) == 0 {
return
}
info, err := a.w.RPC.AccountInfo(a.address)
if err != nil {
info.Balance = &rpc.RawAmount{}
}
for hash, pending := range pendings {
var link rpc.BlockHash
if link, err = hex.DecodeString(hash); err != nil {
return
}
info.Balance.Add(&info.Balance.Int, &pending.Amount.Int)
if info.Frontier, err = a.receivePending(info, link); err != nil {
return
}
}
return
}
func (a *Account) receivePending(info rpc.AccountInfo, link rpc.BlockHash) (hash rpc.BlockHash, err error) {
workHash := info.Frontier
if info.Frontier == nil {
info.Frontier = make(rpc.BlockHash, 32)
workHash = a.pubkey
}
if a.representative == "" {
a.representative = info.Representative
if a.representative == "" {
a.representative = "nano_3gonano8jnse4zm65jaiki9tk8ry4jtgc1smarinukho6fmbc45k3icsh6en"
}
}
block := &rpc.Block{
Type: "state",
Account: a.address,
Previous: info.Frontier,
Representative: a.representative,
Balance: info.Balance,
Link: link,
}
if err = a.w.impl.signBlock(a, block); err != nil {
return
}
if block.Work, err = a.w.workGenerateReceive(workHash); err != nil {
return
}
return a.w.RPC.Process(block, "receive")
}
// SetRep sets the account's representative for future blocks.
func (a *Account) SetRep(representative string) (err error) {
if _, err = util.AddressToPubkey(representative); err != nil {
return
}
a.representative = representative
return
}
// ChangeRep changes the account's representative.
func (a *Account) ChangeRep(representative string) (hash rpc.BlockHash, err error) {
info, err := a.w.RPC.AccountInfo(a.address)
if err != nil {
return
}
block := &rpc.Block{
Type: "state",
Account: a.address,
Previous: info.Frontier,
Representative: representative,
Balance: info.Balance,
Link: make(rpc.BlockHash, 32),
}
if err = a.w.impl.signBlock(a, block); err != nil {
return
}
if block.Work, err = a.w.workGenerate(info.Frontier); err != nil {
return
}
if hash, err = a.w.RPC.Process(block, "change"); err == nil {
a.representative = representative
}
return
}