/
addchain.go
176 lines (152 loc) · 3.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
// Copyright 2016 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"
)
var addchain = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli addchain [-e EXTID1 -e EXTID2 -E BINEXTID3 ...] 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."
cmd.execFunc = func(args []string) {
var (
eAcii extidsASCII
eHex extidsHex
)
os.Args = args
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "e", "external id for the entry in ascii")
flag.Var(&eHex, "E", "external id for the entry in hex")
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("Entry of %d bytes is too large", size)
return
} else {
e.Content = p
}
c := factom.NewChain(e)
if factom.ChainExists(c.ChainID) {
errorln("Chain", c.ChainID, "already exists")
return
}
// get the ec address from the wallet
ec, err := factom.FetchECAddress(ecpub)
if err != nil {
errorln(err)
return
}
balance, err := factom.GetECBalance(ecpub)
if err != nil {
errorln(err)
return
}
if balance == 0 {
errorln("Entry Credit balance is zero")
return
}
// commit the chain
txID, err := factom.CommitChain(c, ec)
if err != nil {
errorln(err)
return
}
fmt.Println("Commiting Chain Transaction ID:", txID)
// TODO - get commit acknowledgement
// reveal chain
hash, err := factom.RevealChain(c)
if err != nil {
errorln(err)
return
}
fmt.Println("ChainID:", c.ChainID)
fmt.Println("Entryhash:", hash)
// ? get reveal ack
}
help.Add("addchain", cmd)
return cmd
}()
var composechain = func() *fctCmd {
cmd := new(fctCmd)
cmd.helpMsg = "factom-cli composechain [-e EXTID1 -e EXTID2 -E BINEXTID3 ...] 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.execFunc = func(args []string) {
var (
eAcii extidsASCII
eHex extidsHex
)
os.Args = args
exidCollector = make([][]byte, 0)
flag.Var(&eAcii, "e", "external id for the entry in ascii")
flag.Var(&eHex, "E", "external id for the entry in hex")
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("Entry of %d bytes is too large", size)
return
} else {
e.Content = p
}
c := factom.NewChain(e)
if factom.ChainExists(c.ChainID) {
errorln("Chain", c.ChainID, "already exists")
//return
}
// get the ec address from the wallet
ec, err := factom.FetchECAddress(ecpub)
if err != nil {
errorln(err)
return
}
balance, err := factom.GetECBalance(ecpub)
if err != nil {
errorln(err)
//return
}
if balance == 0 {
errorln("Entry Credit balance is zero")
//return
}
j, err := factom.ComposeChainCommit(c, ec)
if err != nil {
errorln(err)
return
}
fmt.Println(j)
j, err = factom.ComposeChainReveal(c)
if err != nil {
errorln(err)
return
}
fmt.Println(j)
}
help.Add("composechain", cmd)
return cmd
}()