-
Notifications
You must be signed in to change notification settings - Fork 19
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
feat(adapters): Add SignerExtractionAdapter [ENG-1916] #114
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
adapters/signer_extraction_adapter/signer_extraction_adapter.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package signerextraction | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
) | ||
|
||
type SignerData struct { | ||
Signer sdk.AccAddress | ||
Sequence uint64 | ||
} | ||
|
||
// SignerExtractionAdapter is an interface used to determine how the signers of a transaction should be extracted | ||
// from the transaction. | ||
type Adapter interface { | ||
GetSigners(sdk.Tx) ([]SignerData, error) | ||
} | ||
|
||
var _ Adapter = DefaultAdapter{} | ||
|
||
// DefaultSignerExtractionAdapter is the default implementation of SignerExtractionAdapter. It extracts the signers | ||
// from a cosmos-sdk tx via GetSignaturesV2. | ||
type DefaultAdapter struct{} | ||
|
||
func NewDefaultAdapter() DefaultAdapter { | ||
return DefaultAdapter{} | ||
} | ||
|
||
func (DefaultAdapter) GetSigners(tx sdk.Tx) ([]SignerData, error) { | ||
sigTx, ok := tx.(signing.SigVerifiableTx) | ||
if !ok { | ||
return nil, fmt.Errorf("tx of type %T does not implement SigVerifiableTx", tx) | ||
} | ||
|
||
sigs, err := sigTx.GetSignaturesV2() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signers := make([]SignerData, len(sigs)) | ||
for i, sig := range sigs { | ||
signers[i] = SignerData{ | ||
Signer: sig.PubKey.Address().Bytes(), | ||
Sequence: sig.Sequence, | ||
} | ||
} | ||
|
||
return signers, nil | ||
} |
53 changes: 53 additions & 0 deletions
53
adapters/signer_extraction_adapter/signer_extraction_adapter_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package signerextraction_test | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"cosmossdk.io/math" | ||
"github.com/cosmos/cosmos-sdk/client" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
signer_extraction "github.com/skip-mev/block-sdk/adapters/signer_extraction_adapter" | ||
testutils "github.com/skip-mev/block-sdk/testutils" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type SignerExtractionAdapterTestSuite struct { | ||
suite.Suite | ||
txConfig client.TxConfig | ||
accts []testutils.Account | ||
adapter signer_extraction.DefaultAdapter | ||
} | ||
|
||
func TestSignerExtractionAdapterTestSuite(t *testing.T) { | ||
suite.Run(t, new(SignerExtractionAdapterTestSuite)) | ||
} | ||
|
||
func (s *SignerExtractionAdapterTestSuite) SetupTest() { | ||
encodingConfig := testutils.CreateTestEncodingConfig() | ||
s.txConfig = encodingConfig.TxConfig | ||
|
||
accts := testutils.RandomAccounts(rand.New(rand.NewSource(1)), 2) | ||
|
||
s.accts = accts | ||
} | ||
|
||
func (s *SignerExtractionAdapterTestSuite) TestGetSigners() { | ||
acct := s.accts[0] | ||
tx, err := testutils.CreateTx(s.txConfig, acct, 1, 1, []sdk.Msg{ | ||
&banktypes.MsgSend{ | ||
FromAddress: acct.Address.String(), | ||
ToAddress: acct.Address.String(), | ||
Amount: sdk.NewCoins(sdk.NewInt64Coin("test", 1)), | ||
}, | ||
}, sdk.NewCoins(sdk.NewCoin("test", math.NewInt(1)))...) | ||
s.Require().NoError(err) | ||
|
||
signers, err := s.adapter.GetSigners(tx) | ||
s.Require().NoError(err) | ||
|
||
s.Require().Len(signers, 1) | ||
s.Require().Equal(acct.Address.String(), signers[0].Signer.String()) | ||
s.Require().Equal(uint64(1), signers[0].Sequence) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Curious to hear what you think, but I wonder if this needs to be an
sdk.AccAddress
or just a string. Also, we should probably bring this up to the cosmos sdk team. Ideally, we upstream these changes for others to also have, but for time being this works.