-
Notifications
You must be signed in to change notification settings - Fork 2
/
suffrage_candidate.go
70 lines (54 loc) · 1.8 KB
/
suffrage_candidate.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
package cmds
import (
"context"
"github.com/ProtoconNet/mitum-currency/v3/operation/isaac"
"github.com/ProtoconNet/mitum2/base"
"github.com/pkg/errors"
)
type SuffrageCandidateCommand struct {
BaseCommand
OperationFlags
Node AddressFlag `arg:"" name:"node" help:"node address" required:"true"`
PublicKey PublickeyFlag `arg:"" name:"public-key" help:"public key" required:"true"`
node base.Address
}
func (cmd *SuffrageCandidateCommand) Run(pctx context.Context) error { // nolint:dupl
if _, err := cmd.prepare(pctx); err != nil {
return err
}
encs = cmd.Encoders
enc = cmd.Encoder
if err := cmd.parseFlags(); err != nil {
return err
}
var op base.Operation
if i, err := cmd.createOperation(); err != nil {
return errors.Wrap(err, "create suffrage-candidate operation")
} else if err := i.IsValid([]byte(cmd.OperationFlags.NetworkID)); err != nil {
return errors.Wrap(err, "invalid suffrage-candidate operation")
} else {
cmd.Log.Debug().Interface("operation", i).Msg("operation loaded")
op = i
}
PrettyPrint(cmd.Out, op)
return nil
}
func (cmd *SuffrageCandidateCommand) parseFlags() error {
if err := cmd.OperationFlags.IsValid(nil); err != nil {
return err
}
a, err := cmd.Node.Encode(enc)
if err != nil {
return errors.Wrapf(err, "invalid node format, %v", cmd.Node.String())
}
cmd.node = a
return nil
}
func (cmd *SuffrageCandidateCommand) createOperation() (isaacoperation.SuffrageCandidate, error) {
fact := isaacoperation.NewSuffrageCandidateFact([]byte(cmd.Token), cmd.node, cmd.PublicKey.Publickey)
op := isaacoperation.NewSuffrageCandidate(fact)
if err := op.NodeSign(cmd.Privatekey, cmd.NetworkID.NetworkID(), cmd.node); err != nil {
return isaacoperation.SuffrageCandidate{}, errors.Wrap(err, "create suffrage-candidate operation")
}
return op, nil
}