-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
176 lines (163 loc) · 5.24 KB
/
Copy pathvalidator.go
File metadata and controls
176 lines (163 loc) · 5.24 KB
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
package transactions
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"time"
)
// Validator handles transaction validation for all Dharma Protocol transaction types
type Validator struct{}
// NewValidator creates a new transaction validator
func NewValidator() *Validator {
return &Validator{}
}
// ValidateCreateProject validates a CREATE_PROJECT transaction
func (v *Validator) ValidateCreateProject(msg *CreateProjectMsg) error {
if msg.Name == "" {
return fmt.Errorf("project name is required")
}
if len(msg.Name) > 100 {
return fmt.Errorf("project name exceeds 100 characters")
}
if msg.Description == "" {
return fmt.Errorf("project description is required")
}
if len(msg.Description) > 1000 {
return fmt.Errorf("description exceeds 1000 characters")
}
if msg.Sender == "" {
return fmt.Errorf("sender address is required")
}
if msg.Category == "" {
return fmt.Errorf("project category is required")
}
validCategories := map[string]bool{
CategoryDeFi: true, CategoryNFT: true, CategoryDAO: true,
CategoryInfra: true, CategorySocial: true, CategoryGaming: true,
CategoryDeveloper: true, CategoryOther: true,
}
if !validCategories[msg.Category] {
return fmt.Errorf("invalid category: %s", msg.Category)
}
return nil
}
// ValidateJoinProject validates a JOIN_PROJECT transaction
func (v *Validator) ValidateJoinProject(msg *JoinProjectMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.Sender == "" {
return fmt.Errorf("sender address is required")
}
return nil
}
// ValidateAddTimelineEvent validates an ADD_TIMELINE_EVENT transaction
func (v *Validator) ValidateAddTimelineEvent(msg *AddTimelineEventMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.Title == "" {
return fmt.Errorf("event title is required")
}
if len(msg.Title) > 200 {
return fmt.Errorf("event title exceeds 200 characters")
}
if msg.Category == "" {
return fmt.Errorf("event category is required")
}
validCategories := map[string]bool{
EventIdea: true, EventPrototype: true, EventResearch: true,
EventDesign: true, EventDevelopment: true, EventTesting: true,
EventDeployment: true, EventFunding: true, EventCommunity: true,
}
if !validCategories[msg.Category] {
return fmt.Errorf("invalid event category: %s", msg.Category)
}
return nil
}
// ValidateCreateMilestone validates a CREATE_MILESTONE transaction
func (v *Validator) ValidateCreateMilestone(msg *CreateMilestoneMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.Title == "" {
return fmt.Errorf("milestone title is required")
}
if len(msg.Title) > 200 {
return fmt.Errorf("milestone title exceeds 200 characters")
}
return nil
}
// ValidateEndorseContribution validates an ENDORSE_CONTRIBUTION transaction
func (v *Validator) ValidateEndorseContribution(msg *EndorseContributionMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.TargetAddress == "" {
return fmt.Errorf("target address is required")
}
if msg.Sender == msg.TargetAddress {
return fmt.Errorf("cannot endorse your own contribution")
}
validTypes := map[string]bool{
EndorseBrilliant: true, EndorseHelpful: true, EndorseCriticalFix: true,
EndorseDesign: true, EndorseResearch: true, EndorseMentorship: true,
}
if !validTypes[msg.EndorsementType] {
return fmt.Errorf("invalid endorsement type: %s", msg.EndorsementType)
}
return nil
}
// ValidateCreateDiscussion validates a CREATE_DISCUSSION transaction
func (v *Validator) ValidateCreateDiscussion(msg *CreateDiscussionMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.Title == "" {
return fmt.Errorf("discussion title is required")
}
if msg.Body == "" {
return fmt.Errorf("discussion body is required")
}
if len(msg.Body) > 5000 {
return fmt.Errorf("discussion body exceeds 5000 characters")
}
return nil
}
// ValidateAddRelease validates an ADD_RELEASE transaction
func (v *Validator) ValidateAddRelease(msg *AddReleaseMsg) error {
if msg.ProjectID == "" {
return fmt.Errorf("project ID is required")
}
if msg.Version == "" {
return fmt.Errorf("release version is required")
}
if msg.Summary == "" {
return fmt.Errorf("release summary is required")
}
return nil
}
// GenerateTxHash creates a deterministic transaction hash from the message
func GenerateTxHash(msg interface{}) (string, error) {
data, err := json.Marshal(msg)
if err != nil {
return "", fmt.Errorf("failed to marshal message: %w", err)
}
// Append timestamp for uniqueness
data = append(data, []byte(time.Now().String())...)
hash := sha256.Sum256(data)
return "0x" + hex.EncodeToString(hash[:]), nil
}
// GenerateProjectID creates a deterministic project ID
func GenerateProjectID(creator, name string, timestamp time.Time) string {
data := fmt.Sprintf("%s:%s:%d", creator, name, timestamp.UnixNano())
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])[:16]
}
// GenerateEventID creates an event ID for timeline events
func GenerateEventID(projectID, sender string, timestamp time.Time) string {
data := fmt.Sprintf("%s:%s:%d", projectID, sender, timestamp.UnixNano())
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])[:16]
}