Merge PR #3970: Fix Tx Sign Offline Mode

- Add shorthand flags `-a` and `-s` for the account and sequence numbers respectively
- Mark the account and sequence numbers required during "offline" mode
- Always do an RPC query for account and sequence number during "online" mode
  - If clients wish to provide such values, they must use `--offline`. This makes the whole flow/UX easier to reason about.

closes: #3893
This commit is contained in:
Alexander Bezobchuk
2019-03-26 10:36:10 -04:00
committed by GitHub
parent 83f3d1e192
commit ea46da7126
6 changed files with 58 additions and 45 deletions
+19 -22
View File
@@ -164,15 +164,17 @@ func PrintUnsignedStdTx(
return
}
// SignStdTx appends a signature to a StdTx and returns a copy of a it. If appendSig
// SignStdTx appends a signature to a StdTx and returns a copy of it. If appendSig
// is false, it replaces the signatures already attached with the new signature.
// Don't perform online validation or lookups if offline is true.
func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string, stdTx auth.StdTx, appendSig bool, offline bool) (auth.StdTx, error) {
func SignStdTx(
txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string,
stdTx auth.StdTx, appendSig bool, offline bool,
) (auth.StdTx, error) {
var signedStdTx auth.StdTx
keybase := txBldr.Keybase()
info, err := keybase.Get(name)
info, err := txBldr.Keybase().Get(name)
if err != nil {
return signedStdTx, err
}
@@ -185,8 +187,7 @@ func SignStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, name string,
}
if !offline {
txBldr, err = populateAccountFromState(
txBldr, cliCtx, sdk.AccAddress(addr))
txBldr, err = populateAccountFromState(txBldr, cliCtx, sdk.AccAddress(addr))
if err != nil {
return signedStdTx, err
}
@@ -244,25 +245,21 @@ func ReadStdTxFromFile(cdc *amino.Codec, filename string) (stdTx auth.StdTx, err
return
}
func populateAccountFromState(txBldr authtxb.TxBuilder, cliCtx context.CLIContext,
addr sdk.AccAddress) (authtxb.TxBuilder, error) {
if txBldr.AccountNumber() == 0 {
accNum, err := cliCtx.GetAccountNumber(addr)
if err != nil {
return txBldr, err
}
txBldr = txBldr.WithAccountNumber(accNum)
func populateAccountFromState(
txBldr authtxb.TxBuilder, cliCtx context.CLIContext, addr sdk.AccAddress,
) (authtxb.TxBuilder, error) {
accNum, err := cliCtx.GetAccountNumber(addr)
if err != nil {
return txBldr, err
}
if txBldr.Sequence() == 0 {
accSeq, err := cliCtx.GetAccountSequence(addr)
if err != nil {
return txBldr, err
}
txBldr = txBldr.WithSequence(accSeq)
accSeq, err := cliCtx.GetAccountSequence(addr)
if err != nil {
return txBldr, err
}
return txBldr, nil
return txBldr.WithAccountNumber(accNum).WithSequence(accSeq), nil
}
// GetTxEncoder return tx encoder from global sdk configuration if ones is defined.