-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add
IsMessageSignatureCorrect
- Loading branch information
1 parent
98e7825
commit 001cfef
Showing
2 changed files
with
41 additions
and
0 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,16 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
// IsMessageSignatureCorrect checks whether the message ECDSA signature is correct. | ||
func IsMessageSignatureCorrect(address common.Address, msg, sig []byte) (bool, error) { | ||
publicKey, err := crypto.SigToPub(accounts.TextHash(msg), sig) | ||
if err != nil { | ||
return false, err | ||
} | ||
return address == crypto.PubkeyToAddress(*publicKey), 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,25 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIsMessageSignatureCorrect(t *testing.T) { | ||
const message = "Hello, world!" | ||
const signature = "0xb04f825363596418c630425916f73447d636094a75e47b45e2eb59d8a6c7d5035355f03b903b84700376f0efa23f3b095815776c5c6daf2b371a0a61b5f7034500" | ||
address := common.HexToAddress("0x36615Cf349d7F6344891B1e7CA7C72883F5dc049") | ||
actual, err := IsMessageSignatureCorrect(address, []byte(message), common.FromHex(signature)) | ||
assert.NoError(t, err, "Should throw error for valid message signature") | ||
assert.True(t, actual, "Message signature should be valid") | ||
} | ||
|
||
func TestIsMessageSignatureCorrectInvalidSignature(t *testing.T) { | ||
const message = "Hello world" | ||
const signature = "0xb04f825363596418c630425916f73447d636094a75e47b45e2eb59d8a6c7d5035355f03b903b84700376f0efa23f3b095815776c5c6daf2b371a0a61b5f7034500" | ||
address := common.HexToAddress("0x36615Cf349d7F6344891B1e7CA7C72883F5dc049") | ||
actual, err := IsMessageSignatureCorrect(address, []byte(message), common.FromHex(signature)) | ||
assert.NoError(t, err, "Should throw error for valid message signature") | ||
assert.False(t, actual, "Message signature should be invalid") | ||
} |