-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- extracted common Ecrecover func which is used in benthos-plugin, de…
…vices-api and vsd
- Loading branch information
1 parent
6ce137e
commit 13336bf
Showing
3 changed files
with
89 additions
and
20 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,59 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
const sigLen = 65 | ||
|
||
var zeroAddr common.Address | ||
|
||
// Ecrecover mimics the ecrecover opcode, returning the address that signed | ||
// hash with signature. sig must have length 65 and the last byte, the recovery | ||
// byte usually denoted v, must be 27 or 28. | ||
func Ecrecover(hash, sig []byte) (common.Address, error) { | ||
if len(sig) != sigLen { | ||
return zeroAddr, fmt.Errorf("signature has invalid length %d", len(sig)) | ||
} | ||
|
||
// Defensive copy: the caller shouldn't have to worry about us modifying | ||
// the signature. We adjust because crypto.Ecrecover demands 0 <= v <= 4. | ||
fixedSig := make([]byte, sigLen) | ||
copy(fixedSig, sig) | ||
fixedSig[64] -= 27 | ||
|
||
rawPk, err := crypto.Ecrecover(hash, fixedSig) | ||
if err != nil { | ||
return zeroAddr, err | ||
} | ||
|
||
pk, err := crypto.UnmarshalPubkey(rawPk) | ||
if err != nil { | ||
return zeroAddr, err | ||
} | ||
|
||
return crypto.PubkeyToAddress(*pk), nil | ||
} | ||
|
||
// VerifySignature godoc | ||
// checks if the provided signature corresponds to the given message and Ethereum address. | ||
// payload []byte The original message that was signed. | ||
// signature string The signature that needs to be verified. | ||
// ethAddr string The Ethereum address of the signer. | ||
// @return bool Indicates whether the signature is valid or not. | ||
// @return error If there was an issue during the verification process. | ||
func VerifySignature(payload []byte, signature string, ethAddr string) (bool, error) { | ||
addr := common.HexToAddress(ethAddr) | ||
sig := common.FromHex(signature) | ||
hash := crypto.Keccak256Hash(payload) | ||
|
||
recAddr, err := Ecrecover(hash.Bytes(), sig) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
return recAddr == addr, 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
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