Skip to content

Commit

Permalink
Remove setEth function and initialize eth client in connector.Open
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdev22 committed Jan 18, 2024
1 parent 3fd9f67 commit 9b4c897
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
3 changes: 0 additions & 3 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package connector

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

Expand Down Expand Up @@ -132,6 +131,4 @@ type Web3Connector interface {
// 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)
}
26 changes: 22 additions & 4 deletions connector/web3/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web3

import (
"bytes"
"errors"
"fmt"
"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
Expand All @@ -11,6 +12,8 @@ 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"
"os"
)

type Config struct {
Expand All @@ -19,6 +22,12 @@ type Config struct {

func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
w := &web3Connector{infuraID: c.InfuraID, logger: logger}

ethClient, err := createEthClient()
if err != nil {
return nil, err
}
w.ethClient = ethClient
return w, nil
}

Expand All @@ -32,10 +41,6 @@ func (c *web3Connector) InfuraID() string {
return c.infuraID
}

func (c *web3Connector) SetEthClient(ethClient bind.ContractBackend) {
c.ethClient = ethClient
}

// https://gist.github.com/dcb9/385631846097e1f59e3cba3b1d42f3ed#file-eth_sign_verify-go
func (c *web3Connector) Verify(address, msg, signedMsg string) (identity connector.Identity, err error) {
addrb := common.HexToAddress(address)
Expand Down Expand Up @@ -114,3 +119,16 @@ func (c *web3Connector) VerifyERC1271Signature(contractAddress common.Address, h
func signHash(data []byte) []byte {
return accounts.TextHash(data)
}

func createEthClient() (bind.ContractBackend, error) {
rpcUrl := os.Getenv("ETH_RPC_CLIENT")
if rpcUrl != "" {
client, err := ethclient.Dial(rpcUrl)
if err != nil {
return nil, err
}
return client, nil
}

return nil, errors.New("could not initialize eth client with url")
}
25 changes: 7 additions & 18 deletions connector/web3/web3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,11 @@ type BkTest struct {
t *testing.T
}

func newConnector(t *testing.T) *web3Connector {
func newConnector() *web3Connector {
log := logrus.New()

testConfig := Config{
InfuraID: "mockInfuraID",
}

conn, err := testConfig.Open("id", log)
if err != nil {
t.Fatal(err)
}

web3Conn, ok := conn.(*web3Connector)
if !ok {
t.Fatal(err)
}
web3Conn := &web3Connector{}
web3Conn.logger = log

return web3Conn
}
Expand Down Expand Up @@ -66,7 +55,7 @@ func signMessage(msg string, pk *ecdsa.PrivateKey) ([]byte, []byte, error) {
}

func TestEOALogin(t *testing.T) {
conn := newConnector(t)
conn := newConnector()
// Create and deploy eth client
bk := BkTest{
t: t,
Expand All @@ -75,7 +64,7 @@ func TestEOALogin(t *testing.T) {
sim, auth, ctrPk, err := bk.createMockBlockchain()
assert.NoError(t, err)

conn.SetEthClient(sim)
conn.ethClient = sim

defer func(sim *backends.SimulatedBackend) {
err := sim.Close()
Expand Down Expand Up @@ -200,12 +189,12 @@ func TestBlockchainBackend(t *testing.T) {
bk := BkTest{
t: t,
}
conn := newConnector(t)
conn := newConnector()

sim, auth, pk, err := bk.createMockBlockchain()
assert.NoError(t, err)

conn.SetEthClient(sim)
conn.ethClient = sim

defer func(sim *backends.SimulatedBackend) {
err := sim.Close()
Expand Down
14 changes: 0 additions & 14 deletions server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,6 @@ func (s *Server) handleVerify(w http.ResponseWriter, r *http.Request) {
return
}

ethClient, err := createEthClient()
if err != nil {
s.renderErrorJSON(w, http.StatusBadRequest, "Could not verify signature.")
return
}
w3Conn.SetEthClient(ethClient)

identity, err := w3Conn.Verify(data.Address, data.Nonce, verifyReq.Signed)
if err != nil {
s.renderErrorJSON(w, http.StatusBadRequest, "Could not verify signature.")
Expand Down Expand Up @@ -472,13 +465,6 @@ func (s *Server) handleVerifyDirect(w http.ResponseWriter, r *http.Request) {
return
}

ethClient, err := createEthClient()
if err != nil {
s.renderErrorJSON(w, http.StatusBadRequest, "Could not verify signature.")
return
}
w3Conn.SetEthClient(ethClient)

identity, err := w3Conn.Verify(data.Address, data.Nonce, verifyReq.Signed)
if err != nil {
s.renderErrorJSON(w, http.StatusBadRequest, "Could not verify signature.")
Expand Down

0 comments on commit 9b4c897

Please sign in to comment.