-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from KyberNetwork/feat/eth/recover-signer
feat: ✨ eth: RecoverSignerAddress
- Loading branch information
Showing
4 changed files
with
65 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package eth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
const signatureLength = 65 | ||
|
||
func RecoverSignerAddress(hexEncodedHash string, hexEncodedSignature string) (common.Address, error) { | ||
hash, err := hexutil.Decode(hexEncodedHash) | ||
if err != nil { | ||
return common.Address{}, fmt.Errorf("decode hash error: %w", err) | ||
} | ||
|
||
signature, err := hexutil.Decode(hexEncodedSignature) | ||
if err != nil { | ||
return common.Address{}, fmt.Errorf("decode signature error: %w", err) | ||
} | ||
|
||
// Signature in Ethereum consists of R, S, V; the V is at last byte, R and S are the rest. | ||
// The standard Ethereum signature is 65 bytes (R: 32 bytes, S: 32 bytes, V: 1 byte). | ||
// Ensure the signature is 65 bytes and split it into R, S, and V components. | ||
if len(signature) != signatureLength { | ||
return common.Address{}, fmt.Errorf("invalid signature length, expect: %d, got: %v", signatureLength, len(signature)) | ||
} | ||
|
||
// Ethereum uses a 'recovery id' for V, but go-ethereum expects V to be 27 or 28. | ||
signature[64] -= 27 | ||
|
||
// Recover the public key from the signature | ||
pubKey, err := crypto.SigToPub(hash, signature) | ||
if err != nil { | ||
return common.Address{}, fmt.Errorf("signature to public key error: %w", err) | ||
} | ||
|
||
// Derive the Ethereum address from the public key | ||
address := crypto.PubkeyToAddress(*pubKey) | ||
|
||
return address, 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,18 @@ | ||
package eth_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/KyberNetwork/tradinglib/pkg/eth" | ||
"github.com/test-go/testify/require" | ||
) | ||
|
||
func TestRecover(t *testing.T) { | ||
hashHex := "0xef6777697377372751e5daa1772d3b2b03f6f50b85dd3d7a79b4f12381897ec8" | ||
signatureHex := "0xf89e6165aa337b8ebf74effa1a2b0980cc54e3780af077337b12438f30c901ab69ad655518007bd15073c919780ac61d3a4d5918c8966ffdb07256776ca6223f1c" // nolint: lll | ||
|
||
addr, err := eth.RecoverSignerAddress(hashHex, signatureHex) | ||
require.NoError(t, err) | ||
|
||
t.Log(addr.String()) | ||
} |