Skip to content

Commit

Permalink
Remove function to get rpc url, Add new function to set ethClient, fi…
Browse files Browse the repository at this point in the history
…x testing issues identified in review, Initialize and set eth client before calling verify
  • Loading branch information
0xdev22 committed Jan 17, 2024
1 parent 92c34c6 commit 0500b98
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 210 deletions.
6 changes: 3 additions & 3 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connector

import (
"context"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"net/http"
)

Expand Down Expand Up @@ -128,10 +129,9 @@ type Web3Connector interface {
// empty string.
InfuraID() string

// RpcUrl returns the configured eth rpc url to connect to
RpcURL() string

// Verify checks that the given message was signed by the private key of the given
// account.
Verify(address, msg, signedMsg string) (identity Identity, err error)

SetEthClient(ethClient bind.ContractBackend)

This comment has been minimized.

Copy link
@elffjs

elffjs Jan 17, 2024

Member

Why? I think this goes against the spirit of Open.

}
146 changes: 73 additions & 73 deletions connector/web3/contract_login.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 18 additions & 23 deletions connector/web3/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)

type Config struct {
Expand All @@ -21,20 +20,12 @@ type Config struct {
}

func (c *Config) Open(id string, logger lg.Logger) (connector.Connector, error) {
w := &web3Connector{infuraID: c.InfuraID, rpcUrl: c.RpcURL, logger: logger}
if c.RpcURL != "" {
client, err := ethclient.Dial(c.RpcURL)
if err != nil {
return nil, err
}
w.ethClient = client
}
w := &web3Connector{infuraID: c.InfuraID, logger: logger}
return w, nil
}

type web3Connector struct {
infuraID string
rpcUrl string
ethClient bind.ContractBackend
logger lg.Logger
}
Expand All @@ -43,8 +34,8 @@ func (c *web3Connector) InfuraID() string {
return c.infuraID
}

func (c *web3Connector) RpcURL() string {
return c.rpcUrl
func (c *web3Connector) SetEthClient(ethClient bind.ContractBackend) {
c.ethClient = ethClient
}

// https://gist.github.com/dcb9/385631846097e1f59e3cba3b1d42f3ed#file-eth_sign_verify-go
Expand All @@ -64,26 +55,32 @@ func (c *web3Connector) Verify(address, msg, signedMsg string) (identity connect
signb[64] -= 27
} else if signb[64] != 0 && signb[64] != 1 {
// We allow 0 or 1 because some non-conformant devices like Ledger use it.
return c.VerifyERC1271Signature(addrb, msgHash, signb)
return c.VerifyERC1271Signature(addrb, msgHash, signedMsg)
}

pubKey, err := crypto.SigToPub(msgHash, signb)
if err != nil {
return c.VerifyERC1271Signature(addrb, msgHash, signb)
return c.VerifyERC1271Signature(addrb, msgHash, signedMsg)
}

recoveredAddr := crypto.PubkeyToAddress(*pubKey)
// These are byte arrays, so this is okay to do.
if recoveredAddr != addrb {
return identity, fmt.Errorf("given address and address recovered from signed nonce do not match")
return c.VerifyERC1271Signature(addrb, msgHash, signedMsg)
// identity, fmt.Errorf("given address and address recovered from signed nonce do not match")
}

identity.UserID = address
identity.Username = address
return identity, nil
}

func (c *web3Connector) VerifyERC1271Signature(contractAddress common.Address, hash, signature []byte) (identity connector.Identity, err error) {
func (c *web3Connector) VerifyERC1271Signature(contractAddress common.Address, hash []byte, signedMsg string) (identity connector.Identity, err error) {
signature, err := hexutil.Decode(signedMsg)
if err != nil {
return identity, fmt.Errorf("could not decode hex string of signed nonce: %v", err)
}

if c.ethClient == nil {
c.logger.Errorf("Eth client was not initialized successfully %v", err)
return identity, errors.New("error occurred completing authentication, please try again")
Expand All @@ -95,22 +92,20 @@ func (c *web3Connector) VerifyERC1271Signature(contractAddress common.Address, h
/**
* function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
*/
ct, err := NewContractLogin(contractAddress, c.ethClient)
ct, err := NewErc1271(contractAddress, c.ethClient)
if err != nil {
return identity, errors.New("error occurred completing login")
}

isValid, err := ct.IsValidSignature(&bind.CallOpts{
Pending: false,
}, msgHash, signature)
isValid, err := ct.IsValidSignature(&bind.CallOpts{}, msgHash, signature)
if err != nil {
return identity, fmt.Errorf("error occurred completing login %w", err)
}
resultVal := common.BytesToAddress(isValid[:])
falsyVal := common.HexToAddress("0xffffffff")
truthyVal := common.HexToAddress("0x1626ba7e")

if bytes.Equal(falsyVal.Bytes(), resultVal.Bytes()) {
return identity, fmt.Errorf("given address and address recovered from signed nonce do not match")
if !bytes.Equal(truthyVal.Bytes(), resultVal.Bytes()) {
return identity, fmt.Errorf("given address and address recovered from signed message do not match")
}

return connector.Identity{
Expand Down
Loading

0 comments on commit 0500b98

Please sign in to comment.