-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
wrap.go
83 lines (65 loc) · 1.84 KB
/
wrap.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
package commands
import (
"fmt"
"github.com/spf13/pflag"
"github.com/spf13/viper"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/client/commands"
txcmd "github.com/cosmos/cosmos-sdk/client/commands/txs"
"github.com/cosmos/cosmos-sdk/modules/nonce"
)
// nolint
const (
FlagSequence = "sequence"
FlagNonceKey = "nonce-key"
)
// NonceWrapper wraps a tx with a nonce
type NonceWrapper struct{}
var _ txcmd.Wrapper = NonceWrapper{}
// Wrap grabs the sequence number from the flag and wraps
// the tx with this nonce. Grabs the permission from the signer,
// as we still only support single sig on the cli
func (NonceWrapper) Wrap(tx sdk.Tx) (res sdk.Tx, err error) {
signers, err := readNonceKey()
if err != nil {
return res, err
}
seq, err := readSequence(signers)
if err != nil {
return res, err
}
res = nonce.NewTx(seq, signers, tx)
return
}
// Register adds the sequence flags to the cli
func (NonceWrapper) Register(fs *pflag.FlagSet) {
fs.Int(FlagSequence, -1, "Sequence number for this transaction")
fs.String(FlagNonceKey, "", "Set of comma-separated addresses for the nonce (for multisig)")
}
func readNonceKey() ([]sdk.Actor, error) {
nonce := viper.GetString(FlagNonceKey)
if nonce == "" {
return []sdk.Actor{txcmd.GetSignerAct()}, nil
}
return commands.ParseActors(nonce)
}
// read the sequence from the flag or query for it if flag is -1
func readSequence(signers []sdk.Actor) (seq uint32, err error) {
//add the nonce tx layer to the tx
seqFlag := viper.GetInt(FlagSequence)
switch {
case seqFlag > 0:
seq = uint32(seqFlag)
case seqFlag == -1:
//autocalculation for default sequence
seq, _, err = doNonceQuery(signers)
if err != nil {
return
}
//increase the sequence by 1!
seq++
default:
err = fmt.Errorf("sequence must be either greater than 0, or -1 for autocalculation")
}
return
}