/
proposal.go
206 lines (163 loc) · 5.94 KB
/
proposal.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package types
import (
fmt "fmt"
"net/url"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
const (
ProposalTypeAddPublisher = "AddPublisher"
ProposalTypeRemovePublisher = "RemovePublisher"
ProposalTypeAddDefaultSubscription = "AddDefaultSubscription"
ProposalTypeRemoveDefaultSubscription = "RemoveDefaultSubscription"
)
var _ govtypesv1beta1.Content = &AddPublisherProposal{}
var _ govtypesv1beta1.Content = &RemovePublisherProposal{}
var _ govtypesv1beta1.Content = &AddDefaultSubscriptionProposal{}
var _ govtypesv1beta1.Content = &RemoveDefaultSubscriptionProposal{}
func init() {
// The RegisterProposalTypeCodec function was mysteriously removed by in 0.46.0 even though
// the claim was that the old API would be preserved in .../x/gov/types/v1beta1 so we have
// to interact with the codec directly.
//
// The PR that removed it: https://github.com/cosmos/cosmos-sdk/pull/11240
// This PR was later reverted, but RegisterProposalTypeCodec was still left out. Not sure if
// this was intentional or not.
govtypesv1beta1.RegisterProposalType(ProposalTypeAddPublisher)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&AddPublisherProposal{}, "sommelier/AddPublisherProposal", nil)
govtypesv1beta1.RegisterProposalType(ProposalTypeRemovePublisher)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&RemovePublisherProposal{}, "sommelier/RemovePublisherProposal", nil)
govtypesv1beta1.RegisterProposalType(ProposalTypeAddDefaultSubscription)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&AddDefaultSubscriptionProposal{}, "sommelier/AddDefaultSubscriptionProposal", nil)
govtypesv1beta1.RegisterProposalType(ProposalTypeRemoveDefaultSubscription)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&RemoveDefaultSubscriptionProposal{}, "sommelier/RemoveDefaultSubscriptionProposal", nil)
}
//////////////////////////
// AddPublisherProposal //
//////////////////////////
func NewAddPublisherProposal(title string, description string, domain string, address string, proofURL string, caCert string) *AddPublisherProposal {
return &AddPublisherProposal{
Title: title,
Description: description,
Domain: domain,
Address: address,
ProofUrl: proofURL,
CaCert: caCert,
}
}
func (p *AddPublisherProposal) ProposalRoute() string {
return RouterKey
}
func (p *AddPublisherProposal) ProposalType() string {
return ProposalTypeAddPublisher
}
func (p *AddPublisherProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(p); err != nil {
return err
}
publisher := Publisher{
Domain: p.Domain,
Address: p.Address,
CaCert: p.CaCert,
}
if err := publisher.ValidateBasic(); err != nil {
return err
}
return ValidateProofURL(p.ProofUrl, p.Domain, p.Address)
}
func ValidateProofURL(proofURL string, domain string, address string) error {
if proofURL == "" {
return fmt.Errorf("empty proof URL")
}
if err := ValidateGenericURL(proofURL); err != nil {
return err
}
validProofURL := fmt.Sprintf("%s/%s/cacert.pem", domain, address)
validProofURLWithHTTPS := fmt.Sprintf("https://%s", validProofURL)
if proofURL != validProofURL && proofURL != validProofURLWithHTTPS {
return fmt.Errorf("invalid proof URL format, should be: %s", validProofURLWithHTTPS)
}
return nil
}
func ValidateGenericURL(urlString string) error {
if urlString == "" {
return fmt.Errorf("empty URL")
}
if len(urlString) > MaxURLLength {
return fmt.Errorf("URL over max length of %d: %d", MaxURLLength, len(urlString))
}
if _, err := url.Parse(urlString); err != nil {
return fmt.Errorf("invalid URL format: %s", err.Error())
}
return nil
}
/////////////////////////////
// RemovePublisherProposal //
/////////////////////////////
func NewRemovePublisherProposal(title string, description string, domain string) *RemovePublisherProposal {
return &RemovePublisherProposal{
Title: title,
Description: description,
Domain: domain,
}
}
func (p *RemovePublisherProposal) ProposalRoute() string {
return RouterKey
}
func (p *RemovePublisherProposal) ProposalType() string {
return ProposalTypeRemovePublisher
}
func (p *RemovePublisherProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(p); err != nil {
return err
}
return ValidateDomain(p.Domain)
}
////////////////////////////////////
// AddDefaultSubscriptionProposal //
///////////////////////////////////
func NewAddDefaultSubscriptionProposal(title string, description string, subscriptionID string, publisherDomain string) *AddDefaultSubscriptionProposal {
return &AddDefaultSubscriptionProposal{
Title: title,
Description: description,
SubscriptionId: subscriptionID,
PublisherDomain: publisherDomain,
}
}
func (p *AddDefaultSubscriptionProposal) ProposalRoute() string {
return RouterKey
}
func (p *AddDefaultSubscriptionProposal) ProposalType() string {
return ProposalTypeAddDefaultSubscription
}
func (p *AddDefaultSubscriptionProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(p); err != nil {
return err
}
defaultSubscription := DefaultSubscription{
SubscriptionId: p.SubscriptionId,
PublisherDomain: p.PublisherDomain,
}
return defaultSubscription.ValidateBasic()
}
///////////////////////////////////////
// RemoveDefaultSubscriptionProposal //
///////////////////////////////////////
func NewRemoveDefaultSubscriptionProposal(title string, description string, subscriptionID string) *RemoveDefaultSubscriptionProposal {
return &RemoveDefaultSubscriptionProposal{
Title: title,
Description: description,
SubscriptionId: subscriptionID,
}
}
func (p *RemoveDefaultSubscriptionProposal) ProposalRoute() string {
return RouterKey
}
func (p *RemoveDefaultSubscriptionProposal) ProposalType() string {
return ProposalTypeRemoveDefaultSubscription
}
func (p *RemoveDefaultSubscriptionProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(p); err != nil {
return err
}
return ValidateSubscriptionID(p.SubscriptionId)
}