|
| 1 | +package arns |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/tidwall/gjson" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +type ArNS struct { |
| 14 | + DreUrl string |
| 15 | + ArNSAddress string |
| 16 | + HttpClient *http.Client |
| 17 | +} |
| 18 | + |
| 19 | +func NewArNS(dreUrl string, arNSAddr string, timout time.Duration) *ArNS { |
| 20 | + |
| 21 | + // default timeout is 5s |
| 22 | + if timout == 0 { |
| 23 | + timout = 5 * time.Second |
| 24 | + } |
| 25 | + |
| 26 | + httpClient := &http.Client{ |
| 27 | + Timeout: timout, // Set the timeout for HTTP requests |
| 28 | + } |
| 29 | + return &ArNS{ |
| 30 | + DreUrl: dreUrl, |
| 31 | + ArNSAddress: arNSAddr, |
| 32 | + HttpClient: httpClient, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (a *ArNS) QueryLatestRecord(domain string) (txId string, err error) { |
| 37 | + spliteDomains := strings.Split(domain, "_") |
| 38 | + if len(spliteDomains) > 2 { // todo now only support level-2 subdomain |
| 39 | + return "", errors.New("current arseeding gw not support over level-2 subdomain") |
| 40 | + } |
| 41 | + rootDomain := spliteDomains[len(spliteDomains)-1] |
| 42 | + // step1 query NameCA address |
| 43 | + caAddress, err := a.QueryNameCa(rootDomain) |
| 44 | + if err != nil { |
| 45 | + return "", err |
| 46 | + } |
| 47 | + // step2 query latest txId |
| 48 | + // Currently, only level-1 domain name resolution is queried |
| 49 | + subdomain := spliteDomains[0] |
| 50 | + if subdomain == rootDomain { |
| 51 | + subdomain = "@" |
| 52 | + } |
| 53 | + txId, err = a.GetArNSTxID(caAddress, subdomain) |
| 54 | + return |
| 55 | +} |
| 56 | + |
| 57 | +func (a *ArNS) QueryNameCa(domain string) (caAddress string, err error) { |
| 58 | + baseURL := a.DreUrl + "/contract/" |
| 59 | + |
| 60 | + // Construct the complete URL |
| 61 | + url := baseURL + "?id=" + a.ArNSAddress |
| 62 | + |
| 63 | + // Make the HTTP request using the custom HTTP client |
| 64 | + response, err := a.HttpClient.Get(url) |
| 65 | + if err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + defer response.Body.Close() |
| 69 | + |
| 70 | + // Check the response status code |
| 71 | + if response.StatusCode != http.StatusOK { |
| 72 | + return "", fmt.Errorf("unexpected response status: %s", response.Status) |
| 73 | + } |
| 74 | + |
| 75 | + // Read the response body |
| 76 | + body, err := ioutil.ReadAll(response.Body) |
| 77 | + if err != nil { |
| 78 | + return "", err |
| 79 | + } |
| 80 | + |
| 81 | + value := gjson.Get(string(body), "state.records."+domain+".contractTxId") |
| 82 | + |
| 83 | + if !value.Exists() { |
| 84 | + return "", fmt.Errorf("domain %s not exist", domain) |
| 85 | + } |
| 86 | + |
| 87 | + return value.String(), nil |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +func (a *ArNS) GetArNSTxID(caAddress string, domain string) (txId string, err error) { |
| 92 | + |
| 93 | + baseURL := a.DreUrl + "/contract/" |
| 94 | + |
| 95 | + // Construct the complete URL |
| 96 | + url := baseURL + "?id=" + caAddress |
| 97 | + |
| 98 | + // Make the HTTP request using the custom HTTP client |
| 99 | + response, err := a.HttpClient.Get(url) |
| 100 | + if err != nil { |
| 101 | + return "", err |
| 102 | + } |
| 103 | + defer response.Body.Close() |
| 104 | + |
| 105 | + // Check the response status code |
| 106 | + if response.StatusCode != http.StatusOK { |
| 107 | + return "", fmt.Errorf("GetArNSTxID: unexpected response status: %s", response.Status) |
| 108 | + } |
| 109 | + |
| 110 | + // Read the response body |
| 111 | + body, err := ioutil.ReadAll(response.Body) |
| 112 | + if err != nil { |
| 113 | + return "", err |
| 114 | + } |
| 115 | + |
| 116 | + value := gjson.Get(string(body), "state.records."+domain+".transactionId") |
| 117 | + |
| 118 | + /** The ArNS interface will return two types of data for compatibility processing |
| 119 | + data type 1: https://dre-1.warp.cc/contract?id=jr4P6Y_Olv3QGho0uo7p9DpvSn33mUC_XgJSKB3JDZ4 |
| 120 | + records:{ |
| 121 | + @:{ |
| 122 | + transactionId:"wQk7txuMvlrlYlVozj6aeF7E9dlwar8nNtfs3iNTpbQ" |
| 123 | + ttlSeconds:900 |
| 124 | + } |
| 125 | + } |
| 126 | + data type 2: https://dre-3.warp.cc/contract?id=Vx4bW_bh7nXMyq-Jy24s9EiCyY_BXZuToshhSqabc9o |
| 127 | + records:{ |
| 128 | + @:"wQk7txuMvlrlYlVozj6aeF7E9dlwar8nNtfs3iNTpbQ" |
| 129 | + } |
| 130 | + */ |
| 131 | + if !value.Exists() { |
| 132 | + value = gjson.Get(string(body), "state.records."+domain) |
| 133 | + } |
| 134 | + |
| 135 | + if !value.Exists() { |
| 136 | + return "", fmt.Errorf("GetArNSTxID: domain %s not exist", domain) |
| 137 | + } |
| 138 | + |
| 139 | + return value.String(), nil |
| 140 | + |
| 141 | +} |
0 commit comments