cosmos-sdk/x/auth/tx/sigs.go

105 lines
2.8 KiB
Go
Raw Normal View History

Add support for protobuf TxGenerator and SIGN_MODE_DIRECT (#6385) * Add TxWrapper, encoder, decoder and DirectModeHandler * fix pkg name * Update API and leave test TODO's * Update TxWrapper API * tests for tx wrapper (#6410) * WIP: added test for direct mode handler * updated code * Add msg * Update TxWrapper API * Fix pubkey declaration * Add pubkey for tests * Fix SetFee * Remove logs * Avoid global var declaration for tests * Add test for GetPubKeys * Fix direct signing tests * Add more test cases for GetSignBytes * Revert SetFee API * Remove logs * Refactor tests Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> * Refactoring * Refactoring * Integrate SignatureV2 API * Fix wrapper tests * Fix tests * Linting and API tweaks * Update API * WIP on updating API * Fix tests * Update to new SigVerifiableTx * Rename * Update docs to reflect ADR 020 * proto-gen * proto docs * cleanup * cleanup * cleanup * cleanup * cleanup * cleanup * cleanup * Add tests * Refactor and improving test coverage * WIP on test coverage * WIP on test coverage * proto-gen * Fix CompactBitArray.Size() bug * Rename * Remove Builder interface * Address review comments * Update x/auth/tx/sigs.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/auth/tx/encoder.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Update x/auth/tx/encoder.go Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * Address review feedback * Fix build issues * Resolve conflicts * Fix ValidateBasic test coverage * Add test for malicious multisig Co-authored-by: atheeshp <59333759+atheeshp@users.noreply.github.com> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: sahith-narahari <sahithnarahari@gmail.com> Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-07-06 10:03:45 -07:00
package tx
import (
"fmt"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
)
// SignatureDataToModeInfoAndSig converts a SignatureData to a ModeInfo and raw bytes signature
func SignatureDataToModeInfoAndSig(data signing.SignatureData) (*tx.ModeInfo, []byte) {
if data == nil {
return nil, nil
}
switch data := data.(type) {
case *signing.SingleSignatureData:
return &tx.ModeInfo{
Sum: &tx.ModeInfo_Single_{
Single: &tx.ModeInfo_Single{Mode: data.SignMode},
},
}, data.Signature
case *signing.MultiSignatureData:
n := len(data.Signatures)
modeInfos := make([]*tx.ModeInfo, n)
sigs := make([][]byte, n)
for i, d := range data.Signatures {
modeInfos[i], sigs[i] = SignatureDataToModeInfoAndSig(d)
}
multisig := types.MultiSignature{
Signatures: sigs,
}
sig, err := multisig.Marshal()
if err != nil {
panic(err)
}
return &tx.ModeInfo{
Sum: &tx.ModeInfo_Multi_{
Multi: &tx.ModeInfo_Multi{
Bitarray: data.BitArray,
ModeInfos: modeInfos,
},
},
}, sig
default:
panic(fmt.Sprintf("unexpected signature data type %T", data))
}
}
// ModeInfoAndSigToSignatureData converts a ModeInfo and raw bytes signature to a SignatureData or returns
// an error
func ModeInfoAndSigToSignatureData(modeInfo *tx.ModeInfo, sig []byte) (signing.SignatureData, error) {
switch modeInfo := modeInfo.Sum.(type) {
case *tx.ModeInfo_Single_:
return &signing.SingleSignatureData{
SignMode: modeInfo.Single.Mode,
Signature: sig,
}, nil
case *tx.ModeInfo_Multi_:
multi := modeInfo.Multi
sigs, err := decodeMultisignatures(sig)
if err != nil {
return nil, err
}
sigv2s := make([]signing.SignatureData, len(sigs))
for i, mi := range multi.ModeInfos {
sigv2s[i], err = ModeInfoAndSigToSignatureData(mi, sigs[i])
if err != nil {
return nil, err
}
}
return &signing.MultiSignatureData{
BitArray: multi.Bitarray,
Signatures: sigv2s,
}, nil
default:
panic(fmt.Errorf("unexpected ModeInfo data type %T", modeInfo))
}
}
// decodeMultisignatures safely decodes the the raw bytes as a MultiSignature protobuf message
func decodeMultisignatures(bz []byte) ([][]byte, error) {
multisig := types.MultiSignature{}
err := multisig.Unmarshal(bz)
if err != nil {
return nil, err
}
// NOTE: it is import to reject multi-signatures that contain unrecognized fields because this is an exploitable
// malleability in the protobuf message. Basically an attacker could bloat a MultiSignature message with unknown
// fields, thus bloating the transaction and causing it to fail.
if len(multisig.XXX_unrecognized) > 0 {
return nil, fmt.Errorf("rejecting unrecognized fields found in MultiSignature")
}
return multisig.Signatures, nil
}