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
+2 -2
View File
@@ -81,8 +81,8 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
c.Flags().Bool(FlagIndentResponse, false, "Add indent to JSON response")
c.Flags().String(FlagFrom, "", "Name or address of private key with which to sign")
c.Flags().Uint64(FlagAccountNumber, 0, "AccountNumber number to sign the tx")
c.Flags().Uint64(FlagSequence, 0, "Sequence number to sign the tx")
c.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number number of the signing account (offline mode only)")
c.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)")
c.Flags().String(FlagMemo, "", "Memo to send along with transaction")
c.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom")
c.Flags().String(FlagGasPrices, "", "Gas prices to determine the transaction fee (e.g. 10uatom)")
+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.