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.

vault.gno

2.36 Kb · 93 lines
 1package vault
 2
 3import (
 4	"chain"
 5	"time"
 6
 7	"gno.land/p/nt/avl/v0"
 8	"gno.land/p/nt/ufmt/v0"
 9)
10
11type InvoiceRecord struct {
12	Fingerprint  string
13	Submitter    string
14	Timestamp    int64
15	DocumentName string
16}
17
18var (
19	records = avl.NewTree()
20	count   int64
21)
22
23// RegisterInvoice adds an invoice fingerprint to the on-chain vault.
24func RegisterInvoice(cur realm, fingerprint, docName string) string {
25	if len(fingerprint) != 64 {
26		panic("fingerprint must be exactly 64 hex characters")
27	}
28	if records.Has(fingerprint) {
29		panic("fingerprint already registered")
30	}
31
32	addr := cur.Previous().Address().String()
33	ts := time.Now().Unix()
34
35	rec := &InvoiceRecord{
36		Fingerprint:  fingerprint,
37		Submitter:    addr,
38		Timestamp:    ts,
39		DocumentName: docName,
40	}
41
42	records.Set(fingerprint, rec)
43	count++
44
45	chain.Emit("InvoiceRegistered", "fingerprint", fingerprint, "submitter", addr, "doc", docName)
46	return ufmt.Sprintf("SUCCESS: registered %s", fingerprint)
47}
48
49// GetRecord returns pipe-delimited record info.
50func GetRecord(fingerprint string) string {
51	val := records.Get(fingerprint)
52	if val == nil {
53		return ""
54	}
55	r := val.(*InvoiceRecord)
56	return ufmt.Sprintf("%s|%s|%d|%s", r.Fingerprint, r.Submitter, r.Timestamp, r.DocumentName)
57}
58
59// HasRecord checks if fingerprint exists.
60func HasRecord(fingerprint string) bool {
61	return records.Has(fingerprint)
62}
63
64// GetCount returns total registered fingerprints.
65func GetCount() int64 {
66	return count
67}
68
69// Render returns markdown view of the vault.
70func Render(path string) string {
71	if path == "" {
72		md := "# Invoice Fingerprint Vault\n\n"
73		md += "Proof-of-existence registry for invoice fingerprints on Gno.land.\n\n"
74		md += ufmt.Sprintf("- **Total Registered Documents:** %d\n\n", count)
75		md += "### Verification Instructions\n"
76		md += "Append `/verify/<64-char-hex-hash>` to verify any invoice fingerprint.\n"
77		return md
78	}
79
80	prefix := "verify/"
81	hash := path
82	if len(path) > len(prefix) && path[:len(prefix)] == prefix {
83		hash = path[len(prefix):]
84	}
85
86	if records.Has(hash) {
87		val := records.Get(hash)
88		r := val.(*InvoiceRecord)
89		return ufmt.Sprintf("# Fingerprint Verified\n\n- **Fingerprint:** `%s`\n- **Document:** %s\n- **Submitter:** `%s`\n- **Timestamp:** %d\n- **Status:** VALID ON-CHAIN", r.Fingerprint, r.DocumentName, r.Submitter, r.Timestamp)
90	}
91
92	return ufmt.Sprintf("# Not Found\n\nFingerprint `%s` was NOT found in this vault.", hash)
93}