-
Notifications
You must be signed in to change notification settings - Fork 113
/
domain.go
147 lines (120 loc) · 3.5 KB
/
domain.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package native
import (
"errors"
"fmt"
"github.com/bitly/go-simplejson"
"github.com/iost-official/go-iost/core/contract"
"github.com/iost-official/go-iost/vm/host"
"strings"
)
// DomainABIs list of domain abi
var domainABIs *abiSet
var domain0ABIs *abiSet
func init() {
domainABIs = newAbiSet()
domainABIs.Register(initDomainABI, true)
domainABIs.Register(linkDomainABI)
domainABIs.Register(transferDomainABI)
domain0ABIs = newAbiSet()
domain0ABIs.Register(initDomainABI, true)
}
func checkURLValid(name string) error {
if len(name) < 5 || len(name) > 16 {
return fmt.Errorf("url invalid. url length should be between 5,16 got %v", name)
}
if !strings.Contains(name, ".") {
return fmt.Errorf("url invalid. url must contain '.'")
}
for _, ch := range name {
if !(ch >= 'a' && ch <= 'z' || ch >= '0' && ch <= '9' || ch == '_' || ch == '.') {
return fmt.Errorf("url invalid. url contains invalid character %v", ch)
}
}
return nil
}
var (
initDomainABI = &abi{
name: "init",
args: []string{},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
return []interface{}{}, host.CommonErrorCost(1), nil
},
}
linkDomainABI = &abi{
name: "link",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
url := args[0].(string)
cid := args[1].(string)
cost.AddAssign(host.CommonOpCost(1))
err = checkURLValid(url)
if err != nil {
return nil, cost, err
}
if strings.HasSuffix(url, ".iost") {
ok, c := h.RequireAuth(AdminAccount, DomainPermission)
cost.AddAssign(c)
if !ok {
return nil, cost, errors.New("only admin has permission to claim url .iost")
}
}
txInfo, c := h.TxInfo()
cost.AddAssign(c)
tij, err := simplejson.NewJson(txInfo)
if err != nil {
panic(err)
}
applicant := tij.Get("publisher").MustString()
owner := h.DNS.URLOwner(url)
if owner != "" && owner != applicant {
cost.AddAssign(host.CommonErrorCost(1))
return nil, cost, errors.New("no privilege of claimed url")
}
ok, c := h.RequireAuth(applicant, DomainPermission)
cost.AddAssign(c)
if !ok {
return nil, cost, errors.New("no permission of claimed url")
}
// todo check cid and url valid
h.WriteLink(url, cid, applicant)
cost.AddAssign(host.Costs["PutCost"])
cost.AddAssign(host.Costs["PutCost"])
cost.AddAssign(host.Costs["PutCost"])
return nil, cost, nil
},
}
transferDomainABI = &abi{
name: "transfer",
args: []string{"string", "string"},
do: func(h *host.Host, args ...interface{}) (rtn []interface{}, cost contract.Cost, err error) {
cost = contract.Cost0()
url := args[0].(string)
to := args[1].(string)
txInfo, c := h.TxInfo()
cost.AddAssign(c)
tij, err := simplejson.NewJson(txInfo)
if err != nil {
panic(err)
}
applicant := tij.Get("publisher").MustString()
owner := h.DNS.URLOwner(url)
if owner == "" {
cost.AddAssign(host.CommonErrorCost(1))
return nil, cost, errors.New("url doesn't have owner. Link directly")
}
if owner != applicant {
cost.AddAssign(host.CommonErrorCost(1))
return nil, cost, errors.New("no privilege of claimed url")
}
ok, c := h.RequireAuth(applicant, DomainPermission)
cost.AddAssign(c)
if !ok {
return nil, cost, errors.New("no permission of claimed url")
}
h.URLTransfer(url, to)
cost.AddAssign(host.Costs["PutCost"])
return nil, cost, nil
},
}
)