Skip to content

Commit

Permalink
fix(): fix ecrecover
Browse files Browse the repository at this point in the history
  • Loading branch information
zyjblockchain committed Jul 20, 2023
1 parent 1591dce commit 6060a45
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
2 changes: 1 addition & 1 deletion signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestSignTypedData(t *testing.T) {
assert.Equal(t, "0x0a80cc322f7a5e5e0965ff84fd76ea6479fae9d8ce29f7e076c3b9cb8e8097b80052ee91f1fad54d4348c518e42395848431746c8fee40420041f87ea05b5a281c", hexutil.Encode(sig))

hash, err := EIP712Hash(typedData)
addr, err := Ecrecover(hash, sig)
_, addr, err := Ecrecover(hash, sig)
assert.Equal(t, "0xab6c371B6c466BcF14d4003601951e5873dF2AcA", addr.String())
}

Expand Down
11 changes: 3 additions & 8 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func EIP712Hash(typedData apitypes.TypedData) (hash []byte, err error) {
return
}

func Ecrecover(hash, signature []byte) (addr common.Address, err error) {
func Ecrecover(hash, signature []byte) (publicBy []byte, address common.Address, err error) {
sig := make([]byte, len(signature))
copy(sig, signature)
if len(sig) != 65 {
Expand All @@ -53,18 +53,13 @@ func Ecrecover(hash, signature []byte) (addr common.Address, err error) {
sig[64] -= 27
}

recoverPub, err := crypto.Ecrecover(hash, sig)
publicBy, err = crypto.Ecrecover(hash, sig)
if err != nil {
err = fmt.Errorf("can not ecrecover: %v", err)
return
}
pubKey, err := crypto.UnmarshalPubkey(recoverPub)
if err != nil {
err = fmt.Errorf("can not unmarshal pubkey: %v", err)
return
}

addr = crypto.PubkeyToAddress(*pubKey)
address = common.BytesToAddress(crypto.Keccak256(publicBy[1:])[12:])
return
}

Expand Down
8 changes: 4 additions & 4 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestEIP712(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "0xcf3985dd9eb11ce656eafc2dddd08ce3058ad00c74669b3d171f31e9a0472d8e", hexutil.Encode(hash))

addr, err := Ecrecover(hash, hexutil.MustDecode("0xa9a3e5f72b48651b735d0908f1f240b06eafe7166dbe6b4fc8b57d8b8515ef555fe4b124c2b50d6907423426ec46bc12c5956942dcfd01e02d70912c87a389c41b"))
_, addr, err := Ecrecover(hash, hexutil.MustDecode("0xa9a3e5f72b48651b735d0908f1f240b06eafe7166dbe6b4fc8b57d8b8515ef555fe4b124c2b50d6907423426ec46bc12c5956942dcfd01e02d70912c87a389c41b"))
assert.NoError(t, err)
assert.Equal(t, "0xf9593A9d7F735814B87D08e8D8aD624f58d53B10", addr.String())
}
Expand All @@ -41,11 +41,11 @@ func TestEcrecover(t *testing.T) {
hash := accounts.TextHash([]byte("123"))
sig, _ := hexutil.Decode("0x409c16579b4fc162f199f897497f5142101992af82cc6a0b9521413cf721151817e52781c0341fa333cdfea6ebe945b9231f8a8b3df7e7040203f9d7df26c2f21c")

addr, err := Ecrecover(hash, sig)
_, addr, err := Ecrecover(hash, sig)
assert.NoError(t, err)
assert.Equal(t, "0xab6c371B6c466BcF14d4003601951e5873dF2AcA", addr.String())
// run again
addr, err = Ecrecover(hash, sig)
_, addr, err = Ecrecover(hash, sig)
assert.NoError(t, err)
assert.Equal(t, "0xab6c371B6c466BcF14d4003601951e5873dF2AcA", addr.String())

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestEcrecover(t *testing.T) {
sig = append(r.Bytes(), s.Bytes()...)
sig = append(sig, byte(v.Uint64()))
// verify
addr, err = Ecrecover(sigHash.Bytes(), sig)
_, addr, err = Ecrecover(sigHash.Bytes(), sig)
assert.NoError(t, err)
assert.Equal(t, "0xcba9f09e7e6b4a41a9d11f347416b75ee100344f", strings.ToLower(addr.String()))
}

0 comments on commit 6060a45

Please sign in to comment.