-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy patharns.go
90 lines (81 loc) · 2.29 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
package arseeding
import (
"encoding/json"
"errors"
"fmt"
"github.com/everFinance/arseeding/schema"
"github.com/everFinance/goether"
"github.com/permadao/goao"
goarSchema "github.com/permadao/goar/schema"
"strings"
)
const (
ArNsPid = "agYcCFJtrMG6cqMuZfskIkFTGvUPddICmtQSBIoPdiA"
)
type ArNs struct {
aoCli *goao.Client
}
func NewArNs(cuUrl, muUrl string) *ArNs {
eccSigner, _ := goether.NewSigner("4c3f9a1e5b234ce8f1ab58d82f849c0f70a4d5ceaf2b6e2d9a6c58b1f897ef0a")
aoCli, err := goao.NewClient(muUrl, cuUrl, eccSigner)
if err != nil {
panic(err)
}
return &ArNs{aoCli: aoCli}
}
func (a *ArNs) GetRootDomainRecord(rootDomain string) (record *schema.DomainRecord, err error) {
resp, err := a.aoCli.DryRun(ArNsPid, "", []goarSchema.Tag{
{Name: "Action", Value: "Record"},
{Name: "Name", Value: rootDomain},
})
if err != nil {
return nil, err
}
if len(resp.Messages) != 1 {
return nil, errors.New("empty messages")
}
res := resp.Messages[0].(map[string]interface{})
err = json.Unmarshal([]byte(fmt.Sprintf("%v", res["Data"])), &record)
return
}
func (a *ArNs) GetDomainProcessState(pid string) (state *schema.DomainState, err error) {
resp, err := a.aoCli.DryRun(pid, "", []goarSchema.Tag{
{Name: "Action", Value: "State"},
})
if err != nil {
return
}
if len(resp.Messages) != 1 {
return nil, errors.New("empty messages")
}
res := resp.Messages[0].(map[string]interface{})
err = json.Unmarshal([]byte(fmt.Sprintf("%v", res["Data"])), &state)
return
}
func (a *ArNs) QueryDomainTxId(domain string) (string, error) {
spliteDomains := strings.Split(domain, "_")
if len(spliteDomains) > 2 { // todo now only support level-2 subdomain
return "", errors.New("current arseeding gw not support over level-2 subdomain")
}
rootDomain := spliteDomains[len(spliteDomains)-1]
// step1 query root domain process state
record, err := a.GetRootDomainRecord(rootDomain)
if err != nil {
return "", err
}
state, err := a.GetDomainProcessState(record.ProcessId)
if err != nil {
return "", err
}
// step2 query latest txId
// Currently, only level-1 domain name resolution is queried
subdomain := spliteDomains[0]
if subdomain == rootDomain {
subdomain = "@"
}
res, ok := state.Records[subdomain]
if !ok {
return "", errors.New("not exist record")
}
return res.TransactionId, nil
}