-
Notifications
You must be signed in to change notification settings - Fork 244
/
ens.go
101 lines (82 loc) · 2.66 KB
/
ens.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ens
import (
"bytes"
"context"
"crypto/elliptic"
"encoding/hex"
"math/big"
"time"
ens "github.com/wealdtech/go-ens/v3"
"go.uber.org/zap"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/status-im/status-go/eth-node/crypto"
enstypes "github.com/status-im/status-go/eth-node/types/ens"
)
const (
contractQueryTimeout = 5000 * time.Millisecond
)
type Verifier struct {
logger *zap.Logger
}
// NewVerifier returns a Verifier attached to the specified logger
func NewVerifier(logger *zap.Logger) *Verifier {
return &Verifier{logger: logger}
}
func (m *Verifier) verifyENSName(ensInfo enstypes.ENSDetails, ethclient *ethclient.Client) enstypes.ENSResponse {
publicKeyStr := ensInfo.PublicKeyString
ensName := ensInfo.Name
m.logger.Info("Resolving ENS name", zap.String("name", ensName), zap.String("publicKey", publicKeyStr))
response := enstypes.ENSResponse{
Name: ensName,
PublicKeyString: publicKeyStr,
VerifiedAt: time.Now().Unix(),
}
expectedPubKeyBytes, err := hex.DecodeString(publicKeyStr)
if err != nil {
response.Error = err
return response
}
publicKey, err := crypto.UnmarshalPubkey(expectedPubKeyBytes)
if err != nil {
response.Error = err
return response
}
// Resolve ensName
resolver, err := ens.NewResolver(ethclient, ensName)
if err != nil {
m.logger.Error("error while creating ENS name resolver", zap.String("ensName", ensName), zap.Error(err))
response.Error = err
return response
}
x, y, err := resolver.PubKey()
if err != nil {
m.logger.Error("error while resolving public key from ENS name", zap.String("ensName", ensName), zap.Error(err))
response.Error = err
return response
}
// Assemble the bytes returned for the pubkey
pubKeyBytes := elliptic.Marshal(crypto.S256(), new(big.Int).SetBytes(x[:]), new(big.Int).SetBytes(y[:]))
response.PublicKey = publicKey
response.Verified = bytes.Equal(pubKeyBytes, expectedPubKeyBytes)
return response
}
// CheckBatch verifies that a registered ENS name matches the expected public key
func (m *Verifier) CheckBatch(ensDetails []enstypes.ENSDetails, rpcEndpoint, contractAddress string) (map[string]enstypes.ENSResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), contractQueryTimeout)
defer cancel()
ch := make(chan enstypes.ENSResponse)
response := make(map[string]enstypes.ENSResponse)
ethclient, err := ethclient.DialContext(ctx, rpcEndpoint)
if err != nil {
return nil, err
}
for _, ensInfo := range ensDetails {
go func(info enstypes.ENSDetails) { ch <- m.verifyENSName(info, ethclient) }(ensInfo)
}
for range ensDetails {
r := <-ch
response[r.PublicKeyString] = r
}
close(ch)
return response, nil
}