/
main.go
260 lines (240 loc) · 5.66 KB
/
main.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"flag"
"fmt"
"math/big"
"os"
"github.com/codesoap/atto"
)
var usage = `Usage:
atto -v
atto n[ew]
atto [-a ACCOUNT_INDEX] a[ddress]
atto [-a ACCOUNT_INDEX] b[alance]
atto [-a ACCOUNT_INDEX] r[epresentative] REPRESENTATIVE
atto [-a ACCOUNT_INDEX] [-y] s[end] AMOUNT RECEIVER
If the -v flag is provided, atto will print its version number.
The new subcommand generates a new seed, which can later be used with
the other subcommands.
The address, balance, representative and send subcommands expect a seed
as the first line of their standard input. Showing the first address of
a newly generated key could work like this:
atto new | tee seed.txt | atto address
The send subcommand also expects manual confirmation of the transaction,
unless the -y flag is given.
The address subcommand displays addresses for a seed, the balance
subcommand receives pending sends and shows the balance of an account,
the representative subcommand changes the account's representative and
the send subcommand sends funds to an address.
ACCOUNT_INDEX is an optional parameter, which must be a number between 0
and 4,294,967,295. It allows you to use multiple accounts derived from
the same seed. By default the account with index 0 is chosen.
`
var accountIndexFlag uint
var yFlag bool
func init() {
var vFlag bool
flag.Usage = func() { fmt.Fprint(os.Stderr, usage) }
flag.UintVar(&accountIndexFlag, "a", 0, "")
flag.BoolVar(&yFlag, "y", false, "")
flag.BoolVar(&vFlag, "v", false, "")
flag.Parse()
if vFlag {
fmt.Println("1.4.0")
os.Exit(0)
}
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
var ok bool
switch flag.Arg(0)[:1] {
case "n", "a", "b":
ok = flag.NArg() == 1
case "r":
ok = flag.NArg() == 2
case "s":
ok = flag.NArg() == 3
}
if !ok {
flag.Usage()
os.Exit(1)
}
}
func main() {
var err error
switch flag.Arg(0)[:1] {
case "n":
err = printNewSeed()
case "a":
err = printAddress()
case "b":
err = printBalance()
case "r":
err = changeRepresentative()
case "s":
err = sendFunds()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(2)
}
}
func printNewSeed() error {
seed, err := atto.GenerateSeed()
if err == nil {
fmt.Println(seed)
}
return err
}
func printAddress() error {
seed, err := getSeed()
if err != nil {
return err
}
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err == nil {
fmt.Println(account.Address)
}
return err
}
func printBalance() error {
seed, err := getSeed()
if err != nil {
return err
}
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err
}
firstReceive := false // Is this the very first block of the account?
info, err := account.FetchAccountInfo(node)
if err == atto.ErrAccountNotFound {
// Needed for printing balance, even if nothing is pending:
info.Balance = "0"
firstReceive = true
} else if err != nil {
return err
}
pendings, err := account.FetchPending(node)
if err != nil {
return err
}
for _, pending := range pendings {
txt := "Creating receive block for %s from %s... "
amount, ok := big.NewInt(0).SetString(pending.Amount, 10)
if !ok {
return fmt.Errorf("cannot parse '%s' as an integer", pending.Amount)
}
fmt.Fprintf(os.Stderr, txt, rawToNanoString(amount), pending.Source)
var block atto.Block
if firstReceive {
fmt.Fprintf(os.Stderr, "opening account... ")
info, block, err = account.FirstReceive(pending, defaultRepresentative)
firstReceive = false
} else {
block, err = info.Receive(pending)
}
if err != nil {
return err
}
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "done")
}
newBalance, ok := big.NewInt(0).SetString(info.Balance, 10)
if !ok {
return fmt.Errorf("cannot parse '%s' as an integer", info.Balance)
}
fmt.Println(rawToNanoString(newBalance))
return nil
}
func changeRepresentative() error {
representative := flag.Arg(1)
seed, err := getSeed()
if err != nil {
return err
}
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err
}
info, err := account.FetchAccountInfo(node)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Creating change block... ")
block, err := info.Change(representative)
if err != nil {
return err
}
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "done")
return nil
}
func sendFunds() error {
amount := flag.Arg(1)
recipient := flag.Arg(2)
seed, err := getSeed()
if err != nil {
return err
}
privateKey, err := atto.NewPrivateKey(seed, uint32(accountIndexFlag))
if err != nil {
return err
}
account, err := atto.NewAccount(privateKey)
if err != nil {
return err
}
if err = letUserVerifySend(amount, recipient); err != nil {
return err
}
info, err := account.FetchAccountInfo(node)
if err != nil {
return err
}
fmt.Fprintf(os.Stderr, "Creating send block... ")
block, err := info.Send(amount, recipient)
if err != nil {
return err
}
if err = block.Sign(privateKey); err != nil {
return err
}
if err = block.FetchWork(node); err != nil {
return err
}
if err = block.Submit(node); err != nil {
return err
}
fmt.Fprintln(os.Stderr, "done")
return nil
}