Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Playground

admin.gno

package cla

import (
	"chain"

	"gno.land/p/moul/addrset/v0"
	"gno.land/p/moul/helplink/v0"
	"gno.land/r/gov/dao"
)

const RequiredHashChangedEvent = "CLARequiredHashChanged"

// ProposeNewCLA creates a govdao proposal to update the CLA document hash and URL.
// When executed, it resets all existing signatures.
// Propose an empty hash to disable CLA enforcement.
func ProposeNewCLA(cur realm, newHash, newURL string) dao.ProposalRequest {
	cb := func(cur realm) error {
		setRequiredHash(newHash)
		claURL = newURL
		return nil
	}

	desc := "Propose updating the CLA requirement.\n\n"
	if requiredHash != "" {
		desc += "Current hash: " + requiredHash + "\n"
	}
	if claURL != "" {
		desc += "Current URL: " + claURL + "\n"
	}
	if newHash != "" {
		desc += "New hash: " + newHash + "\n"
	}
	if newURL != "" {
		desc += "New URL: " + newURL + "\n"
	}
	if newHash == "" {
		desc += "This proposal disables CLA enforcement.\n"
	}

	return dao.NewProposalRequest(
		"Update CLA requirement",
		desc,
		dao.NewSimpleExecutor(0, cur, cb, helplink.Realm("gno.land/r/sys/cla").Home()),
	)
}

func setRequiredHash(newHash string) {
	prevHash := requiredHash
	requiredHash = newHash
	signatures = addrset.Set{} // reset all signatures

	chain.Emit(
		RequiredHashChangedEvent,
		"from", prevHash,
		"to", newHash,
	)
}

cla.gno

package cla

import (
	"chain"

	"gno.land/p/moul/addrset/v0"
)

const SignedEvent = "CLASigned"

var (
	requiredHash string // SHA256 hash of the CLA document; empty = enforcement disabled
	claURL       string // URL where the CLA document can be found
	signatures   addrset.Set
)

// Sign records a CLA signature for the caller.
// The hash must match the current required hash.
func Sign(cur realm, hash string) {
	if hash != requiredHash {
		panic("hash does not match required CLA hash")
	}

	caller := cur.Previous().Address()
	signatures.Add(caller)

	chain.Emit(
		SignedEvent,
		"signer", caller.String(),
		"hash", hash,
	)
}

// HasValidSignature checks if an address has signed the current required CLA.
// Returns true if CLA enforcement is disabled (requiredHash == ""),
// or if the address has signed.
func HasValidSignature(addr address) bool {
	if requiredHash == "" {
		return true
	}
	return signatures.Has(addr)
}

cla_test.gno

package cla

import (
	"testing"

	"gno.land/p/moul/addrset/v0"
	"gno.land/p/nt/uassert/v0"
)

// cur is a zero-value realm used as a placeholder when forwarding to
// uassert/urequire dispatch helpers that gained an `rlm realm` param.
// These tests pass `func()` callbacks (no crossing inside the callback),
// so rlm is ignored — a nil realm here is safe.
var cur realm

const (
	testHash1 = "abc123def456"
	testHash2 = "xyz789uvw012"
	testUser1 = "g1user1address1234567890"
	testUser2 = "g1user2address0987654321"
)

func resetState() {
	signatures = addrset.Set{}
	requiredHash = ""
	claURL = ""
}

func TestSign(cur realm, t *testing.T) {
	resetState()

	setRequiredHash(testHash1)

	testing.SetRealm(testing.NewUserRealm(testUser1))
	Sign(cross(cur), testHash1)

	uassert.True(t, HasValidSignature(address(testUser1)))
}

func TestSign_WrongHash(cur realm, t *testing.T) {
	resetState()

	setRequiredHash(testHash1)

	testing.SetRealm(testing.NewUserRealm(testUser1))
	uassert.AbortsWithMessage(t, cur, "hash does not match required CLA hash", func() {
		Sign(cross(cur), testHash2)
	})

	uassert.False(t, HasValidSignature(address(testUser1)))
}

func TestHasValidSignature_Disabled(t *testing.T) {
	resetState()

	uassert.Equal(t, "", requiredHash)
	uassert.True(t, HasValidSignature(address(testUser1)))
	uassert.True(t, HasValidSignature(address(testUser2)))
}

func TestHasValidSignature_Valid(cur realm, t *testing.T) {
	resetState()

	setRequiredHash(testHash1)

	testing.SetRealm(testing.NewUserRealm(testUser1))
	Sign(cross(cur), testHash1)

	uassert.True(t, HasValidSignature(address(testUser1)))
}

func TestHasValidSignature_NotSigned(t *testing.T) {
	resetState()

	setRequiredHash(testHash1)

	uassert.False(t, HasValidSignature(address(testUser1)))
}

func TestSetRequiredHash_ResetsSignatures(cur realm, t *testing.T) {
	resetState()

	setRequiredHash(testHash1)

	testing.SetRealm(testing.NewUserRealm(testUser1))
	Sign(cross(cur), testHash1)
	uassert.True(t, HasValidSignature(address(testUser1)))
	uassert.Equal(t, 1, signatures.Size())

	// Update hash - should reset signatures
	setRequiredHash(testHash2)

	uassert.False(t, HasValidSignature(address(testUser1)))
	uassert.Equal(t, 0, signatures.Size())
}

gnomod.toml

module = "gno.land/r/sys/cla"
gno = "0.9"

[addpkg]
  creator = "g1edq4dugw0sgat4zxcw9xardvuydqf6cgleuc8p"

render.gno

package cla

import (
	"gno.land/p/moul/helplink/v0"
	"gno.land/p/moul/md/v0"
	"gno.land/p/moul/mdtable/v0"
	"gno.land/p/nt/ufmt/v0"
)

func Render(path string) string {
	out := md.H1("Contributor License Agreement (CLA)")

	out += md.Paragraph("A Contributor License Agreement (CLA) must be signed before deploying packages.")
	out += md.Paragraph(
		"The Agreement governs Contributions uploaded, published, or made available " +
			"for execution on the Gno.land blockchain network, and the " +
			"related software and repositories used to publish such Contributions.",
	)

	if requiredHash == "" {
		out += md.HorizontalRule()
		out += md.H2("Status")
		out += md.Paragraph(md.Bold("CLA enforcement is currently DISABLED."))
		out += md.Paragraph("All package deployments are allowed.")
		return out
	}

	out += md.HorizontalRule()
	out += md.H2("Status")
	out += md.Paragraph(md.Bold("CLA enforcement is ENABLED"))

	if claURL != "" {
		out += md.Paragraph("You can read the full agreement here: " + md.Link(claURL, claURL))
	}

	table := mdtable.Table{Headers: []string{"", ""}}
	table.Append([]string{md.Bold("Required Hash"), md.InlineCode(requiredHash)})
	table.Append([]string{md.Bold("Signers"), ufmt.Sprintf("%d contributor(s)", signatures.Size())})
	out += table.String()

	out += md.H3("Actions")
	out += md.Paragraph(helplink.Func("Sign CLA", "Sign", "hash", requiredHash))
	return out
}