-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
334 lines (308 loc) · 7.62 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"math/big"
"net/http"
"os"
"github.com/codesoap/atto"
)
var usage = `Usage:
atto-safesign -v
atto-safesign FILE receive
atto-safesign FILE representative REPRESENTATIVE
atto-safesign FILE send AMOUNT RECEIVER
atto-safesign [-a ACCOUNT_INDEX] [-y] FILE sign
atto-safesign FILE submit
If the -v flag is provided, atto-safesign will print its version number.
The receive, representative, send and submit subcommands expect a Nano
address as the first line of their standard input. This address will be
the account of the generated and submitted blocks.
The receive, representative and send subcommands will generate blocks
and append them to FILE. The blocks will still be lacking their
signature. The receive subcommand will create multiple blocks, if there
are multiple receivable blocks. The representative subcommand will
create a block for changing the representative and the send subcommand
will create a block for sending funds to an address.
The sign subcommand expects a seed as the first line of standard input.
It also expects manual confirmation before signing blocks, unless the
-y flag is given. The seed and ACCOUNT_INDEX must belong to the address
used when creating blocks with receive, representative or send.
The sign subcommand will add signatures to all blocks in FILE. It is the
only subcommand that requires no network connection.
The submit subcommand will submit all blocks contained in FILE to the
Nano network.
ACCOUNT_INDEX is an optional parameter, which allows you to use
different accounts derived from the given seed. By default the account
with index 0 is chosen.
Environment:
ATTO_BASIC_AUTH_USERNAME The username for HTTP Basic Authentication.
If set, HTTP Basic Authentication will be
used when making requests to the node.
ATTO_BASIC_AUTH_PASSWORD The password to use for HTTP Basic
Authentication.
`
type workSourceType int
const (
workSourceLocal workSourceType = iota
workSourceNode
workSourceLocalFallback
)
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 accountIndexFlag >= 1<<32 || flag.NArg() < 2 {
flag.Usage()
os.Exit(1)
}
var ok bool
switch flag.Arg(1) {
case "receive", "sign", "submit":
ok = flag.NArg() == 2
case "representative":
ok = flag.NArg() == 3
case "send":
ok = flag.NArg() == 4
}
if !ok {
flag.Usage()
os.Exit(1)
}
setUpNodeAuthentication()
}
func setUpNodeAuthentication() {
if os.Getenv("ATTO_BASIC_AUTH_USERNAME") != "" {
username := os.Getenv("ATTO_BASIC_AUTH_USERNAME")
password := os.Getenv("ATTO_BASIC_AUTH_PASSWORD")
atto.RequestInterceptor = func(request *http.Request) error {
request.SetBasicAuth(username, password)
return nil
}
}
}
func main() {
var err error
switch flag.Arg(1) {
case "receive":
err = receive()
case "representative":
err = change()
case "send":
err = send()
case "sign":
err = sign()
case "submit":
err = submit()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(2)
}
}
func receive() error {
addr, err := getFirstStdinLine()
if err != nil {
return err
}
account, err := atto.NewAccountFromAddress(addr)
if err != nil {
return err
}
firstReceive := false // Is this the very first block of the account?
info, err := getLatestAccountInfo(account)
if err == atto.ErrAccountNotFound {
firstReceive = true
} else if err != nil {
return err
}
receivables, err := account.FetchReceivable(node)
if err != nil {
return err
}
for _, receivable := range receivables {
var block atto.Block
if firstReceive {
info, block, err = account.FirstReceive(receivable, defaultRepresentative)
firstReceive = false
} else {
block, err = info.Receive(receivable)
}
if err != nil {
return err
}
if err = fillWork(&block, node); err != nil {
return err
}
blockJSON, err := json.Marshal(block)
if err != nil {
return err
}
err = appendLineToFile(blockJSON)
if err != nil {
return err
}
}
return nil
}
func change() error {
representative := flag.Arg(2)
addr, err := getFirstStdinLine()
if err != nil {
return err
}
account, err := atto.NewAccountFromAddress(addr)
if err != nil {
return err
}
info, err := getLatestAccountInfo(account)
if err != nil {
return err
}
block, err := info.Change(representative)
if err != nil {
return err
}
if err = fillWork(&block, node); err != nil {
return err
}
blockJSON, err := json.Marshal(block)
if err != nil {
return err
}
return appendLineToFile(blockJSON)
}
func send() error {
amount := flag.Arg(2)
receiver := flag.Arg(3)
addr, err := getFirstStdinLine()
if err != nil {
return err
}
account, err := atto.NewAccountFromAddress(addr)
if err != nil {
return err
}
info, err := getLatestAccountInfo(account)
if err != nil {
return err
}
block, err := info.Send(amount, receiver)
if err != nil {
return err
}
if err = fillWork(&block, node); err != nil {
return err
}
blockJSON, err := json.Marshal(block)
if err != nil {
return err
}
return appendLineToFile(blockJSON)
}
func sign() error {
seed, err := getFirstStdinLine()
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
}
blocks, err := getBlocksFromFile()
if err != nil {
return err
}
var outBuffer bytes.Buffer
for _, block := range blocks {
if account.Address != block.Account {
txt := "Used account with address '%s' cannot sign block with address '%s'"
return fmt.Errorf(txt, account.Address, block.Account)
}
if err = letUserVerifyBlock(block); err != nil {
return err
}
block.Sign(privateKey)
blockJSON, err := json.Marshal(block)
if err != nil {
return err
}
// Buffer output so that file can be overwritten as late as possible
// to avoid problems during the write as much as possible.
outBuffer.Write(blockJSON) // err is always nil.
outBuffer.Write([]byte{'\n'}) // err is always nil.
}
file, err := os.Create(flag.Arg(0))
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, &outBuffer)
return err
}
func submit() error {
addr, err := getFirstStdinLine()
if err != nil {
return err
}
account, err := atto.NewAccountFromAddress(addr)
if err != nil {
return err
}
blocks, err := getBlocksFromFile()
if err != nil {
return err
}
var oldBalance *big.Int
info, err := account.FetchAccountInfo(node)
if err == atto.ErrAccountNotFound {
oldBalance = big.NewInt(0)
} else if err != nil {
return err
} else {
var ok bool
oldBalance, ok = big.NewInt(0).SetString(info.Balance, 10)
if !ok {
return fmt.Errorf("cannot parse '%s' as an integer", info.Balance)
}
}
for _, block := range blocks {
newBalance, ok := big.NewInt(0).SetString(block.Balance, 10)
if !ok {
return fmt.Errorf("cannot parse '%s' as an integer", block.Balance)
}
switch oldBalance.Cmp(newBalance) {
case -1:
block.SubType = atto.SubTypeReceive
case 0:
// If the balance does not change, this should be a "change" block.
block.SubType = atto.SubTypeChange
case 1:
block.SubType = atto.SubTypeSend
}
fmt.Fprint(os.Stderr, "Submitting block... ")
err = block.Submit(node)
if err != nil {
return err
}
fmt.Fprintln(os.Stderr, "done")
oldBalance = newBalance
}
return nil
}