-
Notifications
You must be signed in to change notification settings - Fork 885
Require matching ModeInfo and nested signature counts (CON-393) #3863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a86c7ea
e92c707
c754c39
8b01a64
e3d7133
688f6ac
66af35d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,10 @@ func ModeInfoAndSigToSignatureData(modeInfo *tx.ModeInfo, sig []byte) (signing.S | |
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // ModeInfos and nested signatures are 1:1 (see SignatureDataToModeInfoAndSig). | ||
| if len(multi.ModeInfos) != len(sigs) { | ||
| return nil, fmt.Errorf("invalid multisig: %d mode infos, %d signatures", len(multi.ModeInfos), len(sigs)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] Consider wrapping with a registered SDK error, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] Two notes on this check:
The count invariant itself checks out: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] Same as the builder.go case: consider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] This surfaces through |
||
| } | ||
|
|
||
| sigv2s := make([]signing.SignatureData, len(sigs)) | ||
| for i, mi := range multi.ModeInfos { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,11 @@ import ( | |
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/sei-cosmos/crypto/types" | ||
|
|
||
| cryptotypes "github.com/sei-protocol/sei-chain/sei-cosmos/crypto/types" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/crypto/types/multisig" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/testutil/testdata" | ||
| txtypes "github.com/sei-protocol/sei-chain/sei-cosmos/types/tx" | ||
| "github.com/sei-protocol/sei-chain/sei-cosmos/types/tx/signing" | ||
| ) | ||
|
|
||
| func TestDecodeMultisignatures(t *testing.T) { | ||
|
|
@@ -27,7 +29,7 @@ func TestDecodeMultisignatures(t *testing.T) { | |
| _, err = decodeMultisignatures(bz) | ||
| require.Error(t, err) | ||
|
|
||
| goodMultisig := types.MultiSignature{ | ||
| goodMultisig := cryptotypes.MultiSignature{ | ||
| Signatures: testSigs, | ||
| } | ||
| bz, err = goodMultisig.Marshal() | ||
|
|
@@ -38,3 +40,28 @@ func TestDecodeMultisignatures(t *testing.T) { | |
|
|
||
| require.Equal(t, testSigs, decodedSigs) | ||
| } | ||
|
|
||
| func TestModeInfoAndSigToSignatureData(t *testing.T) { | ||
| msig := multisig.NewMultisig(2) | ||
| multisig.AddSignature(msig, &signing.SingleSignatureData{ | ||
| SignMode: signing.SignMode_SIGN_MODE_DIRECT, | ||
| Signature: []byte("a"), | ||
| }, 0) | ||
| modeInfo, raw := SignatureDataToModeInfoAndSig(msig) | ||
| got, err := ModeInfoAndSigToSignatureData(modeInfo, raw) | ||
| require.NoError(t, err) | ||
| require.Equal(t, msig, got) | ||
|
|
||
| // fewer nested sigs than ModeInfos must error | ||
| rawShort, err := (&cryptotypes.MultiSignature{Signatures: [][]byte{[]byte("a")}}).Marshal() | ||
| require.NoError(t, err) | ||
| mi := &txtypes.ModeInfo_Single_{Single: &txtypes.ModeInfo_Single{Mode: signing.SignMode_SIGN_MODE_DIRECT}} | ||
| bad := &txtypes.ModeInfo{Sum: &txtypes.ModeInfo_Multi_{ | ||
| Multi: &txtypes.ModeInfo_Multi{ | ||
| Bitarray: cryptotypes.NewCompactBitArray(2), | ||
| ModeInfos: []*txtypes.ModeInfo{{Sum: mi}, {Sum: mi}}, | ||
| }, | ||
| }} | ||
| _, err = ModeInfoAndSigToSignatureData(bad, rawShort) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] Only the "fewer sigs than ModeInfos" direction is covered — the one that used to panic. The other direction was arguably the worse pre-fix bug and is now also rejected by the new check, but is untested: with |
||
| require.Error(t, err) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit]
append(origInfos, origInfos[0])can write intoorigInfos' backing array when spare capacity exists. It's benign here because the restore on line 209 resets the slice header and elements[0:n)are untouched, but a full-slice expression makes the intent explicit and removes the aliasing:append(origInfos[:len(origInfos):len(origInfos)], origInfos[0]).Also, only
len(SignerInfos) > len(Signatures)is exercised. The<direction (e.g.origInfos[:1]) hits the same new branch and previously fell through to asigverifymismatch error instead — cheap to add alongside.