-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_document.go
198 lines (166 loc) · 5.18 KB
/
create_document.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
package cmds
import (
"bytes"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/key"
"github.com/spikeekips/mitum/base/operation"
"github.com/spikeekips/mitum/base/seal"
"github.com/spikeekips/mitum/util"
"github.com/soonkuk/mitum-blocksign/blocksign"
currencycmds "github.com/spikeekips/mitum-currency/cmds"
)
type CreateDocumentCommand struct {
*BaseCommand
currencycmds.OperationFlags
Sender currencycmds.AddressFlag `arg:"" name:"sender" help:"sender address" required:""`
FileHash string `arg:"" name:"filehash" help:"filehash" required:""`
Currency currencycmds.CurrencyIDFlag `arg:"" name:"currency" help:"currency id" required:""`
Signers []currencycmds.AddressFlag `name:"signers" help:"signers for document"`
// Keys []KeyFlag `name:"key" help:"key for new document account (ex: \"<public key>,<weight>\")" sep:"@"`
Seal currencycmds.FileLoad `help:"seal" optional:""`
sender base.Address
}
func NewCreateDocumentCommand() CreateDocumentCommand {
return CreateDocumentCommand{
BaseCommand: NewBaseCommand("create-document-operation"),
}
}
func (cmd *CreateDocumentCommand) Run(version util.Version) error { // nolint:dupl
if err := cmd.Initialize(cmd, version); err != nil {
return errors.Errorf("failed to initialize command: %w", err)
}
if err := cmd.parseFlags(); err != nil {
return err
}
var op operation.Operation
if o, err := cmd.createOperation(); err != nil {
return err
} else {
op = o
}
if sl, err := loadSealAndAddOperation(
cmd.Seal.Bytes(),
cmd.Privatekey,
cmd.NetworkID.NetworkID(),
op,
); err != nil {
return err
} else {
currencycmds.PrettyPrint(cmd.Out, cmd.Pretty, sl)
}
return nil
}
func (cmd *CreateDocumentCommand) parseFlags() error {
if err := cmd.OperationFlags.IsValid(nil); err != nil {
return err
}
if a, err := cmd.Sender.Encode(jenc); err != nil {
return errors.Errorf("invalid sender format, %q: %w", cmd.Sender.String(), err)
} else {
cmd.sender = a
}
return nil
}
func (cmd *CreateDocumentCommand) createOperation() (operation.Operation, error) { // nolint:dupl
var items []blocksign.CreateDocumentsItem
if i, err := loadOperations(cmd.Seal.Bytes(), cmd.NetworkID.NetworkID()); err != nil {
return nil, err
} else {
for j := range i {
if t, ok := i[j].(blocksign.CreateDocuments); ok {
items = t.Fact().(blocksign.CreateDocumentsFact).Items()
}
}
}
var signers []base.Address
for i := range cmd.Signers {
if signer, err := cmd.Signers[i].Encode(jenc); err != nil {
return nil, err
} else {
signers = append(signers, signer)
}
}
item := blocksign.NewCreateDocumentsItemSingleFile(blocksign.FileHash(cmd.FileHash), signers, cmd.Currency.CID)
if err := item.IsValid(nil); err != nil {
return nil, err
} else {
items = append(items, item)
}
fact := blocksign.NewCreateDocumentsFact([]byte(cmd.Token), cmd.sender, items)
var fs []operation.FactSign
if sig, err := operation.NewFactSignature(cmd.Privatekey, fact, cmd.NetworkID.NetworkID()); err != nil {
return nil, err
} else {
fs = append(fs, operation.NewBaseFactSign(cmd.Privatekey.Publickey(), sig))
}
if op, err := blocksign.NewCreateDocuments(fact, fs, cmd.Memo); err != nil {
return nil, errors.Errorf("failed to create create-account operation: %w", err)
} else {
return op, nil
}
}
func loadSeal(b []byte, networkID base.NetworkID) (seal.Seal, error) {
if len(bytes.TrimSpace(b)) < 1 {
return nil, errors.Errorf("empty input")
}
if sl, err := seal.DecodeSeal(b, jenc); err != nil {
return nil, err
} else if err := sl.IsValid(networkID); err != nil {
return nil, errors.Wrap(err, "invalid seal")
} else {
return sl, nil
}
}
func loadSealAndAddOperation(
b []byte,
privatekey key.Privatekey,
networkID base.NetworkID,
op operation.Operation,
) (operation.Seal, error) {
if b == nil {
bs, err := operation.NewBaseSeal(
privatekey,
[]operation.Operation{op},
networkID,
)
if err != nil {
return nil, errors.Wrap(err, "failed to create operation.Seal")
}
return bs, nil
}
var sl operation.Seal
if s, err := loadSeal(b, networkID); err != nil {
return nil, err
} else if so, ok := s.(operation.Seal); !ok {
return nil, errors.Errorf("seal is not operation.Seal, %T", s)
} else if _, ok := so.(operation.SealUpdater); !ok {
return nil, errors.Errorf("seal is not operation.SealUpdater, %T", s)
} else {
sl = so
}
// NOTE add operation to existing seal
sl = sl.(operation.SealUpdater).SetOperations([]operation.Operation{op}).(operation.Seal)
s, err := currencycmds.SignSeal(sl, privatekey, networkID)
if err != nil {
return nil, err
}
sl = s.(operation.Seal)
return sl, nil
}
func loadOperations(b []byte, networkID base.NetworkID) ([]operation.Operation, error) {
if len(bytes.TrimSpace(b)) < 1 {
return nil, nil
}
var sl seal.Seal
if s, err := loadSeal(b, networkID); err != nil {
return nil, err
} else if so, ok := s.(operation.Seal); !ok {
return nil, errors.Errorf("seal is not operation.Seal, %T", s)
} else if _, ok := so.(operation.SealUpdater); !ok {
return nil, errors.Errorf("seal is not operation.SealUpdater, %T", s)
} else {
sl = so
}
return sl.(operation.Seal).Operations(), nil
}