-
Notifications
You must be signed in to change notification settings - Fork 4
/
sign_fact.go
85 lines (69 loc) · 2.25 KB
/
sign_fact.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
package cmds
import (
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/operation"
mitumcmds "github.com/spikeekips/mitum/launch/cmds"
"github.com/spikeekips/mitum/util"
)
type SignFactCommand struct {
*BaseCommand
Privatekey PrivatekeyFlag `arg:"" name:"privatekey" help:"sender's privatekey" required:"true"`
NetworkID mitumcmds.NetworkIDFlag `name:"network-id" help:"network-id" required:"true"`
Pretty bool `name:"pretty" help:"pretty format"`
Seal mitumcmds.FileLoad `help:"seal" optional:""`
}
func NewSignFactCommand() SignFactCommand {
return SignFactCommand{
BaseCommand: NewBaseCommand("sign-fact"),
}
}
func (cmd *SignFactCommand) Run(version util.Version) error {
if err := cmd.Initialize(cmd, version); err != nil {
return errors.Wrap(err, "failed to initialize command")
}
var sl operation.Seal
if s, err := LoadSeal(cmd.Seal.Bytes(), cmd.NetworkID.NetworkID()); err != nil {
return err
} else if so, ok := s.(operation.Seal); !ok {
return errors.Errorf("seal is not operation.Seal, %T", s)
} else if _, ok := so.(operation.SealUpdater); !ok {
return errors.Errorf("seal is not operation.SealUpdater, %T", so)
} else {
sl = so
}
cmd.Log().Debug().Stringer("seal", sl.Hash()).Msg("seal loaded")
nops := make([]operation.Operation, len(sl.Operations()))
for i := range sl.Operations() {
op := sl.Operations()[i]
var fsu base.FactSignUpdater
if u, ok := op.(base.FactSignUpdater); !ok {
cmd.Log().Debug().
Interface("operation", op).
Str("operation_type", op.Hint().String()).
Msg("not base.FactSignUpdater")
nops[i] = op
} else {
fsu = u
}
sig, err := base.NewFactSignature(cmd.Privatekey, op.Fact(), cmd.NetworkID.NetworkID())
if err != nil {
return err
}
f := base.NewBaseFactSign(cmd.Privatekey.Publickey(), sig)
nop, err := fsu.AddFactSigns(f)
if err != nil {
return err
}
nops[i] = nop.(operation.Operation)
}
sl = sl.(operation.SealUpdater).SetOperations(nops).(operation.Seal)
s, err := SignSeal(sl, cmd.Privatekey, cmd.NetworkID.NetworkID())
if err != nil {
return err
}
sl = s.(operation.Seal)
cmd.Log().Debug().Msg("seal signed")
PrettyPrint(cmd.Out, cmd.Pretty, sl)
return nil
}