digest.gno
1.17 Kb · 43 lines
1package rulecalc
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "strings"
7)
8
9// App is one grant application. Its ID is its index in the revealed list.
10type App struct {
11 Title string
12 PkgPath string
13 Payout address
14}
15
16// AppsDigest is the commitment to an application list, published before the
17// rule phase and checked at reveal: hex(sha256) of one "Title|PkgPath|Payout\n"
18// line per app, in order, followed by salt.
19// It panics if a field contains "|" or a newline, or the salt contains a
20// newline, since the same digest could then open to a different list.
21//
22// Off-chain equivalent:
23//
24// printf 'Title|PkgPath|Payout\n...salt' | sha256sum
25func AppsDigest(apps []App, salt string) string {
26 if strings.Contains(salt, "\n") {
27 panic("rulecalc: salt contains newline")
28 }
29 var b strings.Builder
30 for _, a := range apps {
31 fields := []string{a.Title, a.PkgPath, string(a.Payout)}
32 for _, f := range fields {
33 if strings.ContainsAny(f, "|\n") {
34 panic("rulecalc: application field contains | or newline")
35 }
36 }
37 b.WriteString(strings.Join(fields, "|"))
38 b.WriteString("\n")
39 }
40 b.WriteString(salt)
41 sum := sha256.Sum256([]byte(b.String()))
42 return hex.EncodeToString(sum[:])
43}