-
Notifications
You must be signed in to change notification settings - Fork 10
/
addchain.go
255 lines (222 loc) · 5.71 KB
/
addchain.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
// 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 addchain = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli addchain [-fq] [-n NAME1 -n NAME2 -h HEXNAME3" +
" ] [-CET] ECADDRESS <STDIN>"
cmd.description = "Create a new Factom Chain. Read data for the First" +
" 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) {
var (
eAcii extidsASCII
eHex extidsHex
)
os.Args = args
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "n", "Chain name element in ascii. Also is extid of"+
" First Entry")
flag.Var(&eHex, "h", "Chain name element in hex. Also is extid of"+
" First Entry")
fflag := flag.Bool(
"f",
false,
"force the chain to commit and reveal without waiting on any"+
" acknowledgement checks",
)
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")
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 *tdisp || *cdisp || *edisp || *qflag {
display = false
}
e := new(factom.Entry)
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
}
c := factom.NewChain(e)
if !*fflag {
if factom.ChainExists(c.ChainID) {
errorln("Chain", c.ChainID, "already exists")
return
}
// check ec address balance
balance, err := factom.GetECBalance(ecpub)
if err != nil {
errorln(err)
return
}
if cost, err := factom.EntryCost(c.FirstEntry); err != nil {
errorln(err)
return
} else if balance < int64(cost)+10 {
errorln("Not enough Entry Credits")
return
}
}
// commit the chain
var repeated bool
txid, err := factom.CommitChain(c, 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 chain
hash, err := factom.RevealChain(c)
if err != nil {
errorln(err)
return
}
if display {
fmt.Println("ChainID:", c.ChainID)
fmt.Println("Entryhash:", hash)
} else if *cdisp {
fmt.Println(c.ChainID)
} else if *edisp {
fmt.Println(hash)
}
if !*fflag {
if _, err := waitOnRevealAck(hash); err != nil {
errorln(err)
return
}
}
}
help.Add("addchain", cmd)
return cmd
}()
var composechain = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli composechain [-f] [-n NAME1 -n NAME2" +
" -h HEXNAME3 ] ECADDRESS <STDIN>"
cmd.description = "Create API calls to create a new Factom Chain. Read" +
" data for the First Entry from stdin. Use the Entry Credits from the" +
" specified address."
cmd.completion = complete.Command{
Flags: complete.Flags{
"-f": complete.PredictNothing,
"-n": complete.PredictAnything,
"-h": complete.PredictAnything,
},
Args: predictAddress,
}
cmd.execFunc = func(args []string) {
var (
eAcii extidsASCII
eHex extidsHex
)
os.Args = args
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "n", "Chain name element in ascii. Also is extid of"+
" First Entry")
flag.Var(&eHex, "h", "Chain name element in hex. Also is extid of"+
" First Entry")
fflag := flag.Bool(
"f",
false,
"force the chain 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)
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
}
c := factom.NewChain(e)
commit, reveal, err := factom.WalletComposeChainCommitReveal(c, 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("composechain", cmd)
return cmd
}()