-
Notifications
You must be signed in to change notification settings - Fork 0
/
ens.go
51 lines (42 loc) · 972 Bytes
/
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
package main
import (
"encoding/json"
"errors"
"math/big"
"net/http"
)
var INDEXER_ENDPOINTS = [2]string{
"https://indexer-mainnet-v1.opti.domains",
"https://indexer-testnet-v1.opti.domains",
}
type DomainHasName struct {
Name string `json:"name"`
Node string `json:"node"`
Owner string `json:"owner"`
}
func getDomainNameFromId(id string) (string, error) {
idBig := new(big.Int)
idBig, ok := idBig.SetString(id, 10)
if !ok {
return "", errors.New("id is not a number")
}
namehash := "0x" + idBig.Text(16)
for i := 0; i < len(INDEXER_ENDPOINTS); i++ {
// Send GET request
resp, err := http.Get(INDEXER_ENDPOINTS[i] + "/node/" + namehash)
if err != nil {
continue
}
defer resp.Body.Close()
// Decode JSON response
var domains []DomainHasName
err = json.NewDecoder(resp.Body).Decode(&domains)
if err != nil {
continue
}
if len(domains) > 0 {
return domains[0].Name, nil
}
}
return "", errors.New("not found")
}