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.

player.gno

1.45 Kb · 53 lines
 1package flag
 2
 3import (
 4	"errors"
 5	"strings"
 6)
 7
 8// ErrNoPlayer is returned when a caller cannot be tied to a player.
 9var ErrNoPlayer = errors.New("flag: deploy your solution under gno.land/r/<your-g1-address>/ so the hall knows who you are")
10
11// Caller is the part of a realm value Player needs. Pass cur.Previous().
12type Caller interface {
13	Address() address
14	PkgPath() string
15}
16
17// Player returns the player credited for a call made by caller, which must
18// be the previous realm of the challenge's crossing function
19// (cur.Previous()).
20//
21// A user calling directly is credited as themselves. A realm is credited
22// to the owner of its namespace, so a solution deployed at
23// gno.land/r/<g1address>/... earns points for <g1address>. Namespaces are
24// enforced by the chain, so nobody can deploy under your address.
25func Player(caller Caller) (address, error) {
26	path := caller.PkgPath()
27	if path == "" {
28		return caller.Address(), nil
29	}
30	return namespaceOwner(path)
31}
32
33// namespaceOwner extracts the address from "<domain>/<r|e>/<g1...>/...".
34func namespaceOwner(path string) (address, error) {
35	parts := strings.Split(path, "/")
36	if len(parts) < 4 || (parts[1] != "r" && parts[1] != "e") {
37		return "", ErrNoPlayer
38	}
39	addr := address(parts[2])
40	if !addr.IsValid() {
41		return "", ErrNoPlayer
42	}
43	return addr, nil
44}
45
46// MustPlayer is Player that panics with ErrNoPlayer.
47func MustPlayer(caller Caller) address {
48	p, err := Player(caller)
49	if err != nil {
50		panic(err)
51	}
52	return p
53}