1 package deed
2 3 import (
4 "fmt"
5 "time"
6 7 "git.smesh.lol/orly/pkg/nostr/encoders/event"
8 "git.smesh.lol/orly/pkg/nostr/encoders/hex"
9 )
10 11 // AssetInfo is what a resolver knows about an asset at verification time.
12 //
13 // A host that has onboarded the catalogue builds this from its pinned copy. A
14 // cold host builds it from a relay query and fails closed when the query fails.
15 type AssetInfo struct {
16 AssetID string
17 // Coordinate is the current addressable coordinate.
18 Coordinate string
19 // Owner is the key current at the tip of the succession chain.
20 Owner []byte
21 // Retailers are keys authorized to sell the asset.
22 Retailers [][]byte
23 // Succession is the valid delegation chain from the original author.
24 Succession []*event.E
25 // Channel, when non-empty, is the channel this asset belongs to. A sub deed
26 // for that channel covers the asset.
27 Channel string
28 }
29 30 // AllowedSigners returns the keys that may sign a deed for this asset: the
31 // current owner, every authorized retailer, and every key reachable through a
32 // valid succession chain from the current owner.
33 func (a *AssetInfo) AllowedSigners() [][]byte {
34 if a == nil {
35 return nil
36 }
37 out := make([][]byte, 0, 1+len(a.Retailers)+len(a.Succession))
38 out = append(out, a.Owner)
39 out = append(out, a.Retailers...)
40 for _, ev := range a.Succession {
41 if s, err := ParseSuccession(ev); err == nil {
42 out = append(out, s.Incoming)
43 }
44 }
45 return out
46 }
47 48 // CatalogHas reports whether the asset belongs to the named channel.
49 func (a *AssetInfo) CatalogHas(channel, assetID string) bool {
50 if a == nil {
51 return false
52 }
53 return a.Channel != "" && a.Channel == channel && a.AssetID == assetID
54 }
55 56 // AssetLookup resolves an asset id. A nil lookup, an error, or a nil result
57 // fails closed.
58 type AssetLookup func(assetID string) (*AssetInfo, error)
59 60 // Verifier checks a deed and its presentation proof in one place.
61 type Verifier struct {
62 // Host is this server's domain or origin, used to bind the proof.
63 Host string
64 // Lookup resolves an asset id to the current owner and its signers.
65 Lookup AssetLookup
66 // Nonces enforces proof single use. Required.
67 Nonces *NonceCache
68 // Revoked is a best-effort revocation lookup; nil honours the deed.
69 Revoked func(deedID string) bool
70 // Now overrides the clock.
71 Now func() time.Time
72 // MaxProofAge bounds proof age; zero means five minutes.
73 MaxProofAge time.Duration
74 }
75 76 // Presentation is a successful verification.
77 type Presentation struct {
78 Deed *Deed
79 Proof *PresentationProof
80 Asset *AssetInfo
81 }
82 83 // VerifyPresentation verifies a deed and a proof for one request.
84 //
85 // deedJSON and proofJSON are the exact signed events the client presented.
86 // requestedAsset is the asset the request is for; requestPubkey is the BUD-11
87 // request identity. The proof must name both, the deed must cover the asset,
88 // and the deed signer must be authorized for it.
89 func (v *Verifier) VerifyPresentation(
90 deedJSON, proofJSON []byte, requestedAsset string, requestPubkey []byte,
91 ) (*Presentation, error) {
92 if v == nil || v.Lookup == nil {
93 return nil, fmt.Errorf("deed: verifier has no asset lookup")
94 }
95 now := time.Now()
96 if v.Now != nil {
97 now = v.Now()
98 }
99 if !IsAssetID(requestedAsset) {
100 return nil, fmt.Errorf("deed: requested asset id is invalid")
101 }
102 103 deedEv := event.New()
104 if err := deedEv.UnmarshalJSON(deedJSON); err != nil {
105 return nil, fmt.Errorf("deed: decode deed: %w", err)
106 }
107 d, err := ParseDeed(deedEv)
108 if err != nil {
109 return nil, err
110 }
111 112 info, err := v.Lookup(requestedAsset)
113 if err != nil {
114 return nil, fmt.Errorf("deed: resolve asset %s: %w", requestedAsset, err)
115 }
116 if info == nil || info.AssetID != requestedAsset {
117 return nil, fmt.Errorf("deed: asset %s is unknown", requestedAsset)
118 }
119 120 // A sub deed may cover an asset whose AssetInfo names a channel. The deed
121 // check below uses the catalog function, which is the only scope check that
122 // applies to a subscription.
123 var catalog func(string, string) bool
124 if info.Channel != "" {
125 catalog = info.CatalogHas
126 }
127 if err = d.Verify(DeedVerifyOptions{
128 RequestedAssetID: requestedAsset,
129 Now: now,
130 AllowedSigners: info.AllowedSigners(),
131 CatalogHas: catalog,
132 Revoked: v.Revoked,
133 }); err != nil {
134 return nil, err
135 }
136 137 proofEv := event.New()
138 if err := proofEv.UnmarshalJSON(proofJSON); err != nil {
139 return nil, fmt.Errorf("deed: decode presentation proof: %w", err)
140 }
141 p, err := ParseProof(proofEv)
142 if err != nil {
143 return nil, err
144 }
145 if err = p.Verify(ProofVerifyOptions{
146 Deed: d,
147 RequestedAssetID: requestedAsset,
148 Server: v.Host,
149 RequestPubkey: requestPubkey,
150 Now: now,
151 MaxAge: v.MaxProofAge,
152 Nonces: v.Nonces,
153 }); err != nil {
154 return nil, err
155 }
156 return &Presentation{Deed: d, Proof: p, Asset: info}, nil
157 }
158 159 // SessionKey returns the session this presentation establishes.
160 func (p *Presentation) SessionKey() SessionKey {
161 return SessionKey{
162 Buyer: hex.Enc(p.Deed.Buyer),
163 Asset: p.Asset.AssetID,
164 }
165 }
166