v0 source pure
Package bylaws stores a DAO's governing documents — bylaws and mandates — as named plaintext files and amends them wi...
Package bylaws stores a DAO's governing documents — bylaws and mandates — as named plaintext files and amends them with verifiable diff patches.
Documents are keyed by a slash-separated path ("mandates/treasury.md"); folders are a naming convention over the path, not stored objects — a folder exists exactly when a document path has it as a prefix. The only mutation is Apply: a Patch carries the sha256 of the document text it was diffed against plus the edit script that transforms that text into the proposed one. Apply rejects the patch when the document has changed since (optimistic concurrency, no clobbering) and otherwise replays the script. An amendment whose result is empty removes the document, so a stored document is never empty.
The package is governance-agnostic: it decides nothing about WHO may amend. A consuming realm (e.g. a DAO) gates Apply behind its own vote and keeps the *Bylaws handle private — Apply mutates, so the handle must never be exposed to untrusted callers.
3
const MaxOps
MaxOps bounds the number of ops a decoded patch may carry. DiffTexts output always stays far below it (see maxMyersRunes), so the cap only rejects hand-built pathological payloads at the wire boundary.
const OpKeep, OpDelete, OpInsert
1
var ErrInvalidPath, ErrInvalidPatch, ErrInvalidText, ErrStalePatch, ErrDocTooLarge
1var (
2 ErrInvalidPath = errors.New("bylaws: invalid document path")
3 ErrInvalidPatch = errors.New("bylaws: invalid patch")
4 ErrInvalidText = errors.New("bylaws: text is not valid UTF-8")
5 ErrStalePatch = errors.New("bylaws: document changed since the patch base")
6 ErrDocTooLarge = errors.New("bylaws: document exceeds the maximum size")
7)5
func HashText
HashText returns the hex sha256 of a text.
func IsValidPath
IsValidPath reports whether a path names a document: one or more non-empty "/"-separated segments of [a-zA-Z0-9._-] characters, where no segment is "." or "..". The restricted charset keeps paths render- and link-safe and the patch encoding delimiter-free.
func New
New creates an empty document set.
func DecodePatch
DecodePatch parses an Encode payload back into a Patch, validating the path, the base hash shape, and every op (counts must be canonical, so DecodePatch accepts exactly Encode's output shape; insert literals must be valid UTF-8, keeping documents plaintext and Keep's rune math byte-faithful). Replay validity against the actual document is Apply's job; DecodePatch only guarantees the patch is well-formed.
func DiffTexts
DiffTexts builds the patch transforming base into proposed for the document at path. exists reports whether the document currently exists (base must be "" when it does not); a patch built with exists=false creates the document. Both texts must be valid UTF-8 — documents are plaintext, and the invariant keeps Keep/Delete rune math byte-faithful.
4
type Bylaws
structBylaws is one DAO's set of governing documents, keyed by path.
Methods on Bylaws
func Apply
method on BylawsApply verifies the patch and amends the document set: the target's current text must hash to the patch base ("" base means the document must not exist), and the edit script must consume exactly that text. An empty result removes the document. Apply is the package's only mutation; on any error the set is unchanged.
func Diff
method on BylawsDiff builds the patch that changes the document at path to the proposed text: it diffs against the document's current text and pins its hash. A new path yields a create patch; empty proposed text yields a remove patch.
func Get
method on BylawsGet returns a document's text and whether it exists.
func Has
method on BylawsHas reports whether a document exists.
func Hash
method on BylawsHash returns the hex sha256 of a document's text, or an empty string when the document does not exist. It is the base a Patch must pin to amend the document (an empty hash pins "the document must not exist").
func Iterate
method on BylawsIterate walks the documents under a prefix in sorted path order until fn returns true. It returns true when the walk was stopped by fn. The set must not be amended during iteration (no Apply from fn).
func List
method on BylawsList returns the sorted document paths under a prefix. An empty prefix lists every document. The prefix is a raw path prefix: include the trailing slash to scope to a folder (e.g. "mandates/"), or "mandates" also matches a sibling file like "mandates-old.md".
func Size
method on BylawsSize returns the number of documents.
type Op
structOp is one run of a patch's edit script. Keep and Delete address the base text positionally (a rune count), so only Insert carries bytes — a small edit to a large document stays a small patch.
type OpType
identOpType is the kind of a patch operation.
type Patch
structPatch is a verifiable amendment to one document: the edit script that transforms the document's base text into the proposed text, pinned to that base by hash. Apply rejects the patch unless the target currently hashes to Base, so a patch can never be applied to text it was not diffed against.
Methods on Patch
func Encode
method on PatchEncode serializes the patch to a compact single-string payload fit for a transaction argument: "v0:<path>:<base>:" followed by one segment per op — "K<n>;" and "D<n>;" carry rune counts, "I<len>:<bytes>;" carries the inserted literal length-prefixed by its byte length (no escaping needed). DecodePatch is the exact inverse.
func Format
method on PatchFormat renders the patch against its base text as a plain-text change summary: kept runs collapse to a marker, deleted and inserted text is shown literally with every line marker-prefixed ("- "/"+ "), so a multi-line literal cannot masquerade as the summary's own markers (an insertion containing "\n- fake" renders as "+ …" and "+ - fake"). It fails like replay when the script does not fit the base. The output is raw text — callers rendering markdown must escape it (the content is document text, and insertions are proposer-controlled).
func IsCreate
method on PatchIsCreate reports whether the patch creates the document (its base pins "document absent").
func IsNoop
method on PatchIsNoop reports whether applying the patch leaves the document set unchanged (the script only keeps text, or creates an empty document). The check is shape-based: a hand-built script that deletes and reinserts identical text is not detected (Diff never produces one).
func IsRemove
method on PatchIsRemove reports whether applying the patch removes the document (the script deletes the whole base text and inserts nothing).
8
- crypto/sha256 stdlib
- encoding/hex stdlib
- errors stdlib
- gno.land/p/nt/bptree/v0 package
- gno.land/p/onbloc/diff/v0 package
- strconv stdlib
- strings stdlib
- unicode/utf8 stdlib