-
Notifications
You must be signed in to change notification settings - Fork 10
/
addentry.go
299 lines (259 loc) · 6.74 KB
/
addentry.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
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/FactomProject/factom"
"github.com/posener/complete"
)
var addentry = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli addentry [-fq] [-n NAME1 -h HEXNAME2" +
" ...|-c CHAINID] [-e EXTID1 -e EXTID2 -x HEXEXTID ...] [-CET]" +
" ECADDRESS <STDIN>"
cmd.description = "Create a new Factom Entry. Read data for the Entry" +
" from stdin. Use the Entry Credits from the specified address." +
" -C ChainID. -E EntryHash. -T TxID."
cmd.completion = complete.Command{
Flags: complete.Flags{
"-f": complete.PredictNothing,
"-q": complete.PredictNothing,
"-n": complete.PredictAnything,
"-h": complete.PredictAnything,
"-C": complete.PredictNothing,
"-E": complete.PredictNothing,
"-T": complete.PredictNothing,
},
Args: predictAddress,
}
cmd.execFunc = func(args []string) {
os.Args = args
var (
cid = flag.String("c", "", "hex encoded chainid for the entry")
eAcii extidsASCII
eHex extidsHex
nAcii namesASCII
nHex namesHex
)
// -e -x extids
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "e", "external id for the entry in ascii")
flag.Var(&eHex, "x", "external id for the entry in hex")
// -n -h names
nameCollector = make([][]byte, 0)
flag.Var(&nAcii, "n", "ascii name element")
flag.Var(&nHex, "h", "hex binary name element")
// -f force
fflag := flag.Bool(
"f",
false,
"force the entry to commit and reveal without waiting on any"+
" acknowledgement checks",
)
// -CET display flags
cdisp := flag.Bool("C", false, "display only the ChainID")
edisp := flag.Bool("E", false, "display only the Entry Hash")
tdisp := flag.Bool("T", false, "display only the TxID")
// -q quiet flags
qflag := flag.Bool("q", false, "quiet mode; no output")
flag.Parse()
args = flag.Args()
if len(args) < 1 {
fmt.Println(cmd.helpMsg)
return
}
ecpub := args[0]
// display normal output iff no display flags are set and quiet is unspecified
display := true
if *cdisp || *edisp || *tdisp || *qflag {
display = false
}
e := new(factom.Entry)
// set the chainid from -c or from -n -h
if *cid != "" {
e.ChainID = *cid
} else if len(nameCollector) != 0 {
e.ChainID = nametoid(nameCollector)
} else {
fmt.Println(cmd.helpMsg)
return
}
e.ExtIDs = exidCollector
// Entry.Content is read from stdin
if p, err := ioutil.ReadAll(os.Stdin); err != nil {
errorln(err)
return
} else if size := len(p); size > 10240 {
errorln(fmt.Sprintf("Entry of %d bytes is too large", size))
return
} else {
e.Content = p
}
// get the ec address from the wallet
ec, err := factom.FetchECAddress(ecpub)
if err != nil {
errorln(err)
return
}
if !*fflag {
if !factom.ChainExists(e.ChainID) {
errorln("Chain", e.ChainID, "was not found")
return
}
// check ec address balance
balance, err := factom.GetECBalance(ecpub)
if err != nil {
errorln(err)
return
}
if cost, err := factom.EntryCost(e); err != nil {
errorln(err)
return
} else if balance < int64(cost) {
errorln("Not enough Entry Credits")
return
}
}
// commit entry
var repeated bool
txid, err := factom.CommitEntry(e, ec)
if err != nil {
if len(err.Error()) > 15 && err.Error()[:15] != "Repeated Commit" {
errorln(err)
return
}
fmt.Println("Repeated Commit: A commit with equal or greater payment already exists, skipping commit")
repeated = true
}
if !repeated {
if display {
fmt.Println("CommitTxID:", txid)
} else if *tdisp {
fmt.Println(txid)
}
if !*fflag {
if _, err := waitOnCommitAck(txid); err != nil {
errorln(err)
return
}
}
}
// reveal entry
hash, err := factom.RevealEntry(e)
if err != nil {
errorln(err)
return
}
if !*fflag {
if _, err := waitOnRevealAck(hash); err != nil {
errorln(err)
return
}
}
if display {
fmt.Println("ChainID:", e.ChainID)
fmt.Println("Entryhash:", hash)
} else if *cdisp {
fmt.Println(e.ChainID)
} else if *edisp {
fmt.Println(hash)
}
}
help.Add("addentry", cmd)
return cmd
}()
var composeentry = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli composeentry [-f] [-n NAME1 -h HEXNAME2" +
" ...|-c CHAINID] [-e EXTID1 -e EXTID2 -x HEXEXTID ...] ECADDRESS" +
" <STDIN>"
cmd.description = "Create API calls to create a new Factom Entry. Read" +
" data for the Entry from stdin. Use the Entry Credits from the" +
" specified address."
cmd.completion = complete.Command{
Flags: complete.Flags{
"-f": complete.PredictNothing,
"-c": complete.PredictAnything,
"-e": complete.PredictAnything,
"-x": complete.PredictAnything,
"-n": complete.PredictAnything,
"-h": complete.PredictAnything,
},
Args: predictAddress,
}
cmd.execFunc = func(args []string) {
os.Args = args
var (
cid = flag.String("c", "", "hex encoded chainid for the entry")
eAcii extidsASCII
eHex extidsHex
nAcii namesASCII
nHex namesHex
)
// -e -x extids
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "e", "external id for the entry in ascii")
flag.Var(&eHex, "x", "external id for the entry in hex")
// -n -h names
nameCollector = make([][]byte, 0)
flag.Var(&nAcii, "n", "ascii name element")
flag.Var(&nHex, "h", "hex binary name element")
// -f force
fflag := flag.Bool(
"f",
false,
"force the entry to commit and reveal without waiting on any"+
" acknowledgement checks",
)
flag.Parse()
args = flag.Args()
if len(args) < 1 {
fmt.Println(cmd.helpMsg)
return
}
ecpub := args[0]
e := new(factom.Entry)
// set the chainid from -c or from -n -h
if *cid != "" {
e.ChainID = *cid
} else if len(nameCollector) != 0 {
e.ChainID = nametoid(nameCollector)
} else {
fmt.Println(cmd.helpMsg)
return
}
e.ExtIDs = exidCollector
// Entry.Content is read from stdin
if p, err := ioutil.ReadAll(os.Stdin); err != nil {
errorln(err)
return
} else if size := len(p); size > 10240 {
errorln(fmt.Sprintf("Entry of %d bytes is too large", size))
return
} else {
e.Content = p
}
commit, reveal, err := factom.WalletComposeEntryCommitReveal(e, ecpub, *fflag)
if err != nil {
errorln(err)
return
}
factomdServer := GetFactomdServer()
fmt.Println(
"curl -X POST --data-binary",
"'"+commit.String()+"'",
"-H 'content-type:text/plain;' http://"+factomdServer+"/v2",
)
fmt.Println(
"curl -X POST --data-binary",
"'"+reveal.String()+"'",
"-H 'content-type:text/plain;' http://"+factomdServer+"/v2",
)
}
help.Add("composeentry", cmd)
return cmd
}()