Merge PR #6815: Code & Doc Cleanup

This commit is contained in:
Alexander Bezobchuk 2020-07-22 11:02:36 -04:00 committed by GitHub
parent ef0a7344af
commit ab664e9a43
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 30 deletions

View File

@ -222,6 +222,7 @@ func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
if err := tx.SetMsgs(msgs...); err != nil { if err := tx.SetMsgs(msgs...); err != nil {
return nil, err return nil, err
} }
tx.SetMemo(txf.memo) tx.SetMemo(txf.memo)
tx.SetFeeAmount(fees) tx.SetFeeAmount(fees)
tx.SetGasLimit(txf.gas) tx.SetGasLimit(txf.gas)
@ -304,12 +305,10 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
// Helper function to retrieve sign bytes. // Helper function to retrieve sign bytes.
func getSignBytes( func getSignBytes(
signMode signing.SignMode, signMode signing.SignMode, signerData authsigning.SignerData,
signerData authsigning.SignerData, txBuilder client.TxBuilder, pubKey crypto.PubKey, txConfig client.TxConfig,
txBuilder client.TxBuilder,
pubKey crypto.PubKey,
txConfig client.TxConfig,
) ([]byte, error) { ) ([]byte, error) {
sigData := signing.SingleSignatureData{ sigData := signing.SingleSignatureData{
SignMode: signMode, SignMode: signMode,
Signature: nil, Signature: nil,
@ -318,24 +317,21 @@ func getSignBytes(
PubKey: pubKey, PubKey: pubKey,
Data: &sigData, Data: &sigData,
} }
// For SIGN_MODE_DIRECT, calling SetSignatures calls SetSignerInfos on // For SIGN_MODE_DIRECT, calling SetSignatures calls SetSignerInfos on
// TxBuilder under the hood, and SignerInfos is needed to generated the // TxBuilder under the hood, and SignerInfos is needed to generated the
// sign bytes. This is the reason for setting SetSignatures here, with a // sign bytes. This is the reason for setting SetSignatures here, with a
// nil signature. // nil signature.
//
// Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it // Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it
// also doesn't affect its generated sign bytes, so for code's simplicity // also doesn't affect its generated sign bytes, so for code's simplicity
// sake, we put it here, along with this comment. // sake, we put it here.
err := txBuilder.SetSignatures(sig) if err := txBuilder.SetSignatures(sig); err != nil {
if err != nil {
return nil, err return nil, err
} }
// Generate the bytes to be signed // generate the bytes to be signed
signBytes, err := txConfig.SignModeHandler().GetSignBytes( signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
signMode,
signerData,
txBuilder.GetTx(),
)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -346,12 +342,10 @@ func getSignBytes(
// SignWithPrivKey signs a given tx with the given private key, and returns the // SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful. // corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey( func SignWithPrivKey(
signMode signing.SignMode, signMode signing.SignMode, signerData authsigning.SignerData,
signerData authsigning.SignerData, txBuilder client.TxBuilder, priv crypto.PrivKey, txConfig client.TxConfig,
txBuilder client.TxBuilder,
priv crypto.PrivKey,
txConfig client.TxConfig,
) (signing.SignatureV2, error) { ) (signing.SignatureV2, error) {
var sigV2 signing.SignatureV2 var sigV2 signing.SignatureV2
// Generate the bytes to be signed // Generate the bytes to be signed
@ -371,6 +365,7 @@ func SignWithPrivKey(
SignMode: signMode, SignMode: signMode,
Signature: signature, Signature: signature,
} }
sigV2 = signing.SignatureV2{ sigV2 = signing.SignatureV2{
PubKey: priv.PubKey(), PubKey: priv.PubKey(),
Data: &sigData, Data: &sigData,
@ -379,14 +374,9 @@ func SignWithPrivKey(
return sigV2, nil return sigV2, nil
} }
// Sign signs a given tx with the provided name and passphrase. If the Factory's // Sign signs a given tx with the provided name and passphrase. The bytes signed
// Keybase is not set, a new one will be created based on the client's backend. // over are canconical. The resulting signature will be set on the transaction.
// The bytes signed over are canconical. The resulting signature will be set on // An error is returned upon failure.
// the transaction. Finally, the marshaled transaction is returned. An error is
// returned upon failure.
//
// Note, It is assumed the Factory has the necessary fields set that are required
// by the CanonicalSignBytes call.
func Sign(txf Factory, name string, txBuilder client.TxBuilder) error { func Sign(txf Factory, name string, txBuilder client.TxBuilder) error {
if txf.keybase == nil { if txf.keybase == nil {
return errors.New("keybase must be set prior to signing a transaction") return errors.New("keybase must be set prior to signing a transaction")

View File

@ -33,15 +33,20 @@ func (s *StdTxBuilder) SetMsgs(msgs ...sdk.Msg) error {
// SetSignatures implements TxBuilder.SetSignatures // SetSignatures implements TxBuilder.SetSignatures
func (s *StdTxBuilder) SetSignatures(signatures ...signing.SignatureV2) error { func (s *StdTxBuilder) SetSignatures(signatures ...signing.SignatureV2) error {
sigs := make([]StdSignature, len(signatures)) sigs := make([]StdSignature, len(signatures))
for i, sig := range signatures { for i, sig := range signatures {
pubKey := sig.PubKey
var pubKeyBz []byte var pubKeyBz []byte
pubKey := sig.PubKey
if pubKey != nil { if pubKey != nil {
pubKeyBz = pubKey.Bytes() pubKeyBz = pubKey.Bytes()
} }
var sigBz []byte var (
var err error sigBz []byte
err error
)
if sig.Data != nil { if sig.Data != nil {
sigBz, err = SignatureDataToAminoSignature(legacy.Cdc, sig.Data) sigBz, err = SignatureDataToAminoSignature(legacy.Cdc, sig.Data)
if err != nil { if err != nil {
@ -54,6 +59,7 @@ func (s *StdTxBuilder) SetSignatures(signatures ...signing.SignatureV2) error {
Signature: sigBz, Signature: sigBz,
} }
} }
s.Signatures = sigs s.Signatures = sigs
return nil return nil
} }