-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Return local nonce when getTransactionCount request is signed (#…
…151)
- Loading branch information
1 parent
f59b440
commit cbffdfd
Showing
5 changed files
with
224 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Package flashbots provides methods for parsing the X-Flashbots-Signature header. | ||
package flashbots | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
var ( | ||
ErrNoSignature = errors.New("no signature provided") | ||
ErrInvalidSignature = errors.New("invalid signature provided") | ||
) | ||
|
||
func ParseSignature(header string, body []byte) (signingAddress string, err error) { | ||
if header == "" { | ||
return "", ErrNoSignature | ||
} | ||
|
||
splitSig := strings.Split(header, ":") | ||
if len(splitSig) != 2 { | ||
return "", ErrInvalidSignature | ||
} | ||
|
||
return VerifySignature(body, splitSig[0], splitSig[1]) | ||
} | ||
|
||
func VerifySignature(body []byte, signingAddressStr, signatureStr string) (signingAddress string, err error) { | ||
signature, err := hexutil.Decode(signatureStr) | ||
if err != nil || len(signature) == 0 { | ||
return "", fmt.Errorf("%w: %w", ErrInvalidSignature, err) | ||
} | ||
|
||
if signature[len(signature)-1] >= 27 { | ||
signature[len(signature)-1] -= 27 | ||
} | ||
|
||
hashedBody := crypto.Keccak256Hash(body).Hex() | ||
messageHash := accounts.TextHash([]byte(hashedBody)) | ||
signaturePublicKeyBytes, err := crypto.Ecrecover(messageHash, signature) | ||
if err != nil { | ||
return "", fmt.Errorf("%w: %w", ErrInvalidSignature, err) | ||
} | ||
|
||
publicKey, err := crypto.UnmarshalPubkey(signaturePublicKeyBytes) | ||
if err != nil { | ||
return "", fmt.Errorf("%w: %w", ErrInvalidSignature, err) | ||
} | ||
signaturePubkey := *publicKey | ||
signaturePubKeyAddress := crypto.PubkeyToAddress(signaturePubkey).Hex() | ||
|
||
// case-insensitive equality check | ||
if !strings.EqualFold(signaturePubKeyAddress, signingAddressStr) { | ||
return "", fmt.Errorf("%w: signing address mismatch", ErrInvalidSignature) | ||
} | ||
|
||
signatureNoRecoverID := signature[:len(signature)-1] // remove recovery id | ||
if !crypto.VerifySignature(signaturePublicKeyBytes, messageHash, signatureNoRecoverID) { | ||
return "", fmt.Errorf("%w: %w", ErrInvalidSignature, err) | ||
} | ||
|
||
return signaturePubKeyAddress, nil | ||
} |
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,80 @@ | ||
package flashbots_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/flashbots/rpc-endpoint/adapters/flashbots" | ||
) | ||
|
||
func TestParseSignature(t *testing.T) { | ||
|
||
// For most of these test cases, we first need to generate a signature | ||
privateKey, err := crypto.GenerateKey() | ||
require.NoError(t, err) | ||
|
||
address := crypto.PubkeyToAddress(privateKey.PublicKey).Hex() | ||
body := fmt.Sprintf( | ||
`{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["%s","pending"],"id":1}`, | ||
address, | ||
) | ||
|
||
signature, err := crypto.Sign( | ||
accounts.TextHash([]byte(hexutil.Encode(crypto.Keccak256([]byte(body))))), | ||
privateKey, | ||
) | ||
require.NoError(t, err) | ||
|
||
header := fmt.Sprintf("%s:%s", address, hexutil.Encode(signature)) | ||
|
||
t.Run("header is empty", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature("", []byte{}) | ||
require.ErrorIs(t, err, flashbots.ErrNoSignature) | ||
}) | ||
|
||
t.Run("header is valid", func(t *testing.T) { | ||
signingAddress, err := flashbots.ParseSignature(header, []byte(body)) | ||
require.NoError(t, err) | ||
require.Equal(t, address, signingAddress) | ||
}) | ||
|
||
t.Run("header is invalid", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature("invalid", []byte(body)) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("header has extra bytes", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header+"deadbeef", []byte(body)) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("header has missing bytes", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header[:len(header)-8], []byte(body)) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("body is empty", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header, []byte{}) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("body is invalid", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header, []byte(`{}`)) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("body has extra bytes", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header, []byte(body+"...")) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
|
||
t.Run("body has missing bytes", func(t *testing.T) { | ||
_, err := flashbots.ParseSignature(header, []byte(body[:len(body)-8])) | ||
require.ErrorIs(t, err, flashbots.ErrInvalidSignature) | ||
}) | ||
} |
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