package prooftrail import ( "chain/runtime/unsafe" "strconv" "strings" "time" ) const ( StatusPending = "pending" StatusAccepted = "accepted" StatusRejected = "rejected" TaskOpen = "open" TaskClosed = "closed" ) type Community struct { id int name string description string admin address reviewers []address createdAt int64 } type Task struct { id int communityID int title string description string requirements string status string createdBy address createdAt int64 } type Submission struct { id int taskID int communityID int contributor address evidenceURL string evidenceHash string // SHA-256 (hex) of the evidence, computed by the contributor. Proves integrity only. notes string status string reviewer address reviewReason string submittedAt int64 reviewedAt int64 } type Event struct { seq int kind string actor address subject int detail string at int64 } var ( communities []*Community tasks []*Task submissions []*Submission audit []*Event // append-only: no function edits or removes entries ) // ---------- helpers ---------- func now() int64 { return time.Now().Unix() } func logEvent(kind string, actor address, subject int, detail string) { audit = append(audit, &Event{seq: len(audit) + 1, kind: kind, actor: actor, subject: subject, detail: detail, at: now()}) } func checkText(field, s string, min, max int, multiline bool) { if len(s) < min { panic(field + " is too short") } if len(s) > max { panic(field + " is too long") } for i := 0; i < len(s); i++ { c := s[i] if c < 32 && !(multiline && (c == '\n' || c == '\t')) { panic(field + " contains invalid characters") } if c == 127 { panic(field + " contains invalid characters") } } } // Evidence URLs are untrusted: https only, no spaces, no markup characters. func checkURL(s string) { checkText("evidenceURL", s, 12, 300, false) if !strings.HasPrefix(s, "https://") { panic("evidenceURL must start with https://") } if strings.ContainsAny(s, " \"'<>`\\") { panic("evidenceURL contains forbidden characters") } } func checkHash(h string) { if len(h) != 64 { panic("evidenceHash must be 64 hex characters (SHA-256)") } for i := 0; i < len(h); i++ { c := h[i] if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { panic("evidenceHash must be lowercase hex") } } } func getCommunity(id int) *Community { if id < 1 || id > len(communities) { panic("community not found") } return communities[id-1] } func getTask(id int) *Task { if id < 1 || id > len(tasks) { panic("task not found") } return tasks[id-1] } func getSubmission(id int) *Submission { if id < 1 || id > len(submissions) { panic("submission not found") } return submissions[id-1] } func (c *Community) isReviewer(a address) bool { for _, r := range c.reviewers { if r == a { return true } } return false } func requireAdmin(c *Community, caller address) { if c.admin != caller { panic("only the community administrator can do this") } } // ---------- transactions (crossing functions) ---------- func CreateCommunity(cur realm, name, description string) int { caller := unsafe.PreviousRealm().Address() checkText("name", name, 3, 60, false) checkText("description", description, 0, 500, true) id := len(communities) + 1 communities = append(communities, &Community{id: id, name: name, description: description, admin: caller, reviewers: []address{caller}, createdAt: now()}) logEvent("community_created", caller, id, name) return id } func AddReviewer(cur realm, communityID int, reviewer address) { caller := unsafe.PreviousRealm().Address() c := getCommunity(communityID) requireAdmin(c, caller) if !reviewer.IsValid() { panic("invalid reviewer address") } if c.isReviewer(reviewer) { panic("already a reviewer") } c.reviewers = append(c.reviewers, reviewer) logEvent("reviewer_added", caller, communityID, reviewer.String()) } func RemoveReviewer(cur realm, communityID int, reviewer address) { caller := unsafe.PreviousRealm().Address() c := getCommunity(communityID) requireAdmin(c, caller) if !c.isReviewer(reviewer) { panic("not a reviewer") } out := []address{} for _, r := range c.reviewers { if r != reviewer { out = append(out, r) } } c.reviewers = out logEvent("reviewer_removed", caller, communityID, reviewer.String()) } func CreateTask(cur realm, communityID int, title, description, requirements string) int { caller := unsafe.PreviousRealm().Address() c := getCommunity(communityID) requireAdmin(c, caller) checkText("title", title, 3, 100, false) checkText("description", description, 0, 1000, true) checkText("requirements", requirements, 0, 1000, true) id := len(tasks) + 1 tasks = append(tasks, &Task{id: id, communityID: communityID, title: title, description: description, requirements: requirements, status: TaskOpen, createdBy: caller, createdAt: now()}) logEvent("task_created", caller, id, title) return id } // Description/requirements may only change while the task is open and has no submissions, // so contributors are never judged against rules that changed after they submitted. func UpdateTask(cur realm, taskID int, description, requirements string) { caller := unsafe.PreviousRealm().Address() t := getTask(taskID) requireAdmin(getCommunity(t.communityID), caller) if t.status != TaskOpen { panic("task is not open") } for _, s := range submissions { if s.taskID == taskID { panic("task already has submissions and can no longer be edited") } } checkText("description", description, 0, 1000, true) checkText("requirements", requirements, 0, 1000, true) t.description = description t.requirements = requirements logEvent("task_updated", caller, taskID, "") } func CloseTask(cur realm, taskID int) { caller := unsafe.PreviousRealm().Address() t := getTask(taskID) requireAdmin(getCommunity(t.communityID), caller) if t.status != TaskOpen { panic("task is already closed") } t.status = TaskClosed logEvent("task_closed", caller, taskID, "") } func SubmitContribution(cur realm, taskID int, evidenceURL, evidenceHash, notes string) int { caller := unsafe.PreviousRealm().Address() t := getTask(taskID) if t.status != TaskOpen { panic("task is closed") } checkURL(evidenceURL) checkHash(evidenceHash) checkText("notes", notes, 0, 500, true) for _, s := range submissions { if s.taskID != taskID { continue } if s.contributor == caller && s.status != StatusRejected { panic("you already have an active or accepted submission for this task") } if s.evidenceHash == evidenceHash && s.status != StatusRejected { panic("this evidence was already submitted for this task") } } id := len(submissions) + 1 submissions = append(submissions, &Submission{id: id, taskID: taskID, communityID: t.communityID, contributor: caller, evidenceURL: evidenceURL, evidenceHash: evidenceHash, notes: notes, status: StatusPending, submittedAt: now()}) logEvent("submitted", caller, id, evidenceHash) return id } func review(caller address, id int, to string, reason string) { s := getSubmission(id) c := getCommunity(s.communityID) if !c.isReviewer(caller) { panic("not an authorized reviewer for this community") } if s.contributor == caller { panic("reviewers cannot review their own submission") } if s.status != StatusPending { panic("submission is already " + s.status) } s.status = to s.reviewer = caller s.reviewReason = reason s.reviewedAt = now() logEvent("review_"+to, caller, id, reason) } func AcceptSubmission(cur realm, submissionID int, reason string) { checkText("reason", reason, 0, 500, true) review(unsafe.PreviousRealm().Address(), submissionID, StatusAccepted, reason) } func RejectSubmission(cur realm, submissionID int, reason string) { checkText("reason", reason, 3, 500, true) review(unsafe.PreviousRealm().Address(), submissionID, StatusRejected, reason) } // ---------- JSON helpers (read-only) ---------- func js(s string) string { r := strings.NewReplacer("\\", "\\\\", "\"", "\\\"", "\n", "\\n", "\t", "\\t") return "\"" + r.Replace(s) + "\"" } func i64(n int64) string { return strconv.FormatInt(n, 10) } func in(n int) string { return strconv.Itoa(n) } func (c *Community) json() string { rs := []string{} for _, r := range c.reviewers { rs = append(rs, js(r.String())) } return "{\"id\":" + in(c.id) + ",\"name\":" + js(c.name) + ",\"description\":" + js(c.description) + ",\"admin\":" + js(c.admin.String()) + ",\"reviewers\":[" + strings.Join(rs, ",") + "],\"createdAt\":" + i64(c.createdAt) + "}" } func (t *Task) json() string { return "{\"id\":" + in(t.id) + ",\"communityId\":" + in(t.communityID) + ",\"title\":" + js(t.title) + ",\"description\":" + js(t.description) + ",\"requirements\":" + js(t.requirements) + ",\"status\":" + js(t.status) + ",\"createdBy\":" + js(t.createdBy.String()) + ",\"createdAt\":" + i64(t.createdAt) + "}" } func (s *Submission) json() string { return "{\"id\":" + in(s.id) + ",\"taskId\":" + in(s.taskID) + ",\"communityId\":" + in(s.communityID) + ",\"contributor\":" + js(s.contributor.String()) + ",\"evidenceUrl\":" + js(s.evidenceURL) + ",\"evidenceHash\":" + js(s.evidenceHash) + ",\"notes\":" + js(s.notes) + ",\"status\":" + js(s.status) + ",\"reviewer\":" + js(s.reviewer.String()) + ",\"reviewReason\":" + js(s.reviewReason) + ",\"submittedAt\":" + i64(s.submittedAt) + ",\"reviewedAt\":" + i64(s.reviewedAt) + "}" } func (e *Event) json() string { return "{\"seq\":" + in(e.seq) + ",\"kind\":" + js(e.kind) + ",\"actor\":" + js(e.actor.String()) + ",\"subject\":" + in(e.subject) + ",\"detail\":" + js(e.detail) + ",\"at\":" + i64(e.at) + "}" } func arr(items []string) string { return "[" + strings.Join(items, ",") + "]" } // ---------- Render: markdown for gnoweb, "api/..." paths return JSON for the web app ---------- func mdEscape(s string) string { r := strings.NewReplacer("<", "\\<", ">", "\\>", "[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "*", "\\*", "_", "\\_", "`", "\\`") return r.Replace(s) } func Render(path string) string { if strings.HasPrefix(path, "api/") { return renderAPI(strings.TrimPrefix(path, "api/")) } out := "# ProofTrail\n\nVerifiable Contributions. Transparent History.\n\n" out += "Communities: " + in(len(communities)) + " | Tasks: " + in(len(tasks)) + " | Submissions: " + in(len(submissions)) + "\n\n" for _, s := range submissions { if s.status == StatusAccepted { out += "- Accepted #" + in(s.id) + " on task " + in(s.taskID) + " by " + s.contributor.String() + " (hash " + s.evidenceHash + ")\n" } } return out } func renderAPI(p string) string { parts := strings.Split(p, "/") switch parts[0] { case "stats": return "{\"communities\":" + in(len(communities)) + ",\"tasks\":" + in(len(tasks)) + ",\"submissions\":" + in(len(submissions)) + ",\"events\":" + in(len(audit)) + "}" case "communities": out := []string{} for _, c := range communities { out = append(out, c.json()) } return arr(out) case "tasks": // api/tasks or api/tasks/ out := []string{} for _, t := range tasks { if len(parts) > 1 && in(t.communityID) != parts[1] { continue } out = append(out, t.json()) } return arr(out) case "submissions": // api/submissions/task/ | contributor/ | community/ | accepted out := []string{} for _, s := range submissions { ok := len(parts) == 1 if len(parts) == 2 && parts[1] == "accepted" { ok = s.status == StatusAccepted } if len(parts) == 3 { switch parts[1] { case "task": ok = in(s.taskID) == parts[2] case "contributor": ok = s.contributor.String() == parts[2] case "community": ok = in(s.communityID) == parts[2] } } if ok { out = append(out, s.json()) } } return arr(out) case "audit": out := []string{} for _, e := range audit { out = append(out, e.json()) } return arr(out) } return "{\"error\":\"unknown path\"}" }