-
Notifications
You must be signed in to change notification settings - Fork 1
/
arns.go
132 lines (104 loc) · 2.93 KB
/
arns.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package goarns
import (
"fmt"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
"time"
)
type ArNS struct {
DreUrl string
ArNSAddress string
HttpClient *http.Client
}
func NewArNS(dreUrl string, arNSAddr string, timout time.Duration) *ArNS {
// default timeout is 5s
if timout == 0 {
timout = 5 * time.Second
}
httpClient := &http.Client{
Timeout: timout, // Set the timeout for HTTP requests
}
return &ArNS{
DreUrl: dreUrl,
ArNSAddress: arNSAddr,
HttpClient: httpClient,
}
}
func (a *ArNS) QueryLatestRecord(domain string) (txId string, err error) {
// step1 query NameCA address
caAddress, err := a.QueryNameCa(domain)
if err != nil {
return "", err
}
// step2 query latest txId
//Currently, only level-1 domain name resolution is queried
txId, err = a.GetArNSTxID(caAddress, "@")
return
}
func (a *ArNS) QueryNameCa(domain string) (caAddress string, err error) {
baseURL := a.DreUrl + "/contract/"
// Construct the complete URL
url := baseURL + "?id=" + a.ArNSAddress
// Make the HTTP request using the custom HTTP client
response, err := a.HttpClient.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
// Check the response status code
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected response status: %s", response.Status)
}
// Read the response body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
value := gjson.Get(string(body), "state.records."+domain+".contractTxId")
if !value.Exists() {
return "", fmt.Errorf("domain %s not exist", domain)
}
return value.String(), nil
}
func (a *ArNS) GetArNSTxID(caAddress string, domain string) (txId string, err error) {
baseURL := a.DreUrl + "/contract/"
// Construct the complete URL
url := baseURL + "?id=" + caAddress
// Make the HTTP request using the custom HTTP client
response, err := a.HttpClient.Get(url)
if err != nil {
return "", err
}
defer response.Body.Close()
// Check the response status code
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("GetArNSTxID: unexpected response status: %s", response.Status)
}
// Read the response body
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
value := gjson.Get(string(body), "state.records."+domain+".transactionId")
/** The ArNS interface will return two types of data for compatibility processing
data type 1: https://dre-1.warp.cc/contract?id=jr4P6Y_Olv3QGho0uo7p9DpvSn33mUC_XgJSKB3JDZ4
records:{
@:{
transactionId:"wQk7txuMvlrlYlVozj6aeF7E9dlwar8nNtfs3iNTpbQ"
ttlSeconds:900
}
}
data type 2: https://dre-3.warp.cc/contract?id=Vx4bW_bh7nXMyq-Jy24s9EiCyY_BXZuToshhSqabc9o
records:{
@:"wQk7txuMvlrlYlVozj6aeF7E9dlwar8nNtfs3iNTpbQ"
}
*/
if !value.Exists() {
value = gjson.Get(string(body), "state.records."+domain)
}
if !value.Exists() {
return "", fmt.Errorf("GetArNSTxID: domain %s not exist", domain)
}
return value.String(), nil
}