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

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

fairness source pure

Constants 1

const DigestHexLen, MinSecretLen, MaxSecretLen, SealDomain, SeedDomain

 1const (
 2	// DigestHexLen is the length of a hex-encoded SHA-256 digest.
 3	DigestHexLen = 64
 4
 5	// MinSecretLen is the shortest secret a reveal may carry. Shorter secrets
 6	// are guessable from the public seal and would let a watcher brute-force
 7	// an entrant's reveal before it lands.
 8	MinSecretLen = 16
 9
10	// MaxSecretLen bounds how much a single reveal may store on chain.
11	MaxSecretLen = 256
12
13	// SealDomain is the domain-separation tag of the sealing format.
14	SealDomain = "fairdrop/seal/v1"
15
16	// SeedDomain is the domain-separation tag of the seed fold.
17	SeedDomain = "fairdrop/seed/v1"
18)
source

Functions 13

func Canonical

1func Canonical(domain string, entrant string, weight int64, secret string) string
source

Canonical is the exact string a seal commits to. Binding the entrant id, the domain (chain + event) and the weight means a seal cannot be replayed in another event or on another chain, cannot be claimed by another entrant, and cannot be reused at a different weight.

func Digest

1func Digest(domain string, entrant string, weight int64, secret string) string
source

Digest is the seal of one entry: hex SHA-256 over Canonical.

func Draw

1func Draw(seed string, roster []Ticket, k int) []string
source

Draw samples up to k distinct tickets from roster, without replacement, with probability proportional to Weight, and returns their IDs in the order they were drawn (index 0 is rank 1). Zero-weight tickets are never drawn.

The order of roster must be canonical (as Insert maintains it), because the stream walks the roster in that order.

func Find

1func Find(roster []Ticket, id string) int
source

Find returns the index of the ticket held by id, or -1.

func IsDigest

1func IsDigest(s string) bool
source

IsDigest reports whether s is a well-formed lower-case hex SHA-256 digest.

func Leaf

1func Leaf(commit string, secret string) string
source

Leaf is one input of the seed fold: the seal followed by the secret that opened it. Leaves are folded in canonical entrant order.

func NormalizeDigest

1func NormalizeDigest(s string) string
source

NormalizeDigest returns the canonical form of a seal as submitted.

func Seed

1func Seed(domain string, chainID string, eventID uint64, leaves []string) string
source

Seed folds the revealed leaves into the draw seed. The domain, chain id and event id are part of the fold, so identical reveals in two events give different seeds and no seed can be lifted from one event to another.

func TotalWeight

1func TotalWeight(roster []Ticket) uint64
source

TotalWeight sums the weights of a roster, panicking rather than wrapping.

func ValidSecret

1func ValidSecret(s string) bool
source

ValidSecret reports whether s may be used as a reveal secret: printable ASCII without spaces, within the accepted length bounds.

func VerifySeal

1func VerifySeal(commit string, domain string, entrant string, weight int64, secret string) bool
source

VerifySeal reports whether secret opens commit for this entrant, weight and domain. Input is normalised (trimmed, lowercased) so a wallet or a human pasting an upper-case digest is not turned away.

func NewStream

1func NewStream(seed string) *Stream
source

NewStream starts a stream at seed.

func Insert

1func Insert(roster []Ticket, t Ticket) ([]Ticket, error)
source

Insert adds t to a roster kept in canonical (ascending ID) order and refuses an ID that is already present. One call therefore enforces both rules a draw depends on: a single ticket per entrant, and the fixed ordering the seed fold and the draw walk over.

The returned slice is a new slice; the caller must keep it.

Types 2

type Stream

struct
1type Stream struct {
2	seed string
3	ctr  uint64
4	pool []byte
5	pos  int
6}
source

Stream is a deterministic byte stream: SHA-256 in counter mode over the seed. It exists so a draw needs no randomness from the chain, which is what makes the result reproducible by anyone holding the seed.

Methods on Stream

func Below

method on Stream
1func (s *Stream) Below(n uint64) uint64
source

Below returns a uniform integer in [0, n) by rejection sampling, so no residue is favoured by the modulo. It panics on n == 0.

func Uint64

method on Stream
1func (s *Stream) Uint64() uint64
source

Uint64 returns the next 8 stream bytes as a big-endian unsigned integer.

type Ticket

struct
1type Ticket struct {
2	ID     string
3	Weight uint64
4	Commit string
5	Secret string
6	Rank   int
7}
source

Ticket is one entry in a draw.

ID is the entrant's stable identity — FairDrop uses the bech32 address string, so the roster is ordered and deduplicated by address. Commit is the seal, Secret the revealed preimage (empty until reveal) and Rank 0 until the draw, then 1..k for winners. The package only reads ID, Weight and Commit; Secret and Rank are carried so a realm can keep one record per ticket.

Dependencies

Source Files 3