package deed import ( "fmt" "time" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/hex" ) // AssetInfo is what a resolver knows about an asset at verification time. // // A host that has onboarded the catalogue builds this from its pinned copy. A // cold host builds it from a relay query and fails closed when the query fails. type AssetInfo struct { AssetID string // Coordinate is the current addressable coordinate. Coordinate string // Owner is the key current at the tip of the succession chain. Owner []byte // Retailers are keys authorized to sell the asset. Retailers [][]byte // Succession is the valid delegation chain from the original author. Succession []*event.E // Channel, when non-empty, is the channel this asset belongs to. A sub deed // for that channel covers the asset. Channel string } // AllowedSigners returns the keys that may sign a deed for this asset: the // current owner, every authorized retailer, and every key reachable through a // valid succession chain from the current owner. func (a *AssetInfo) AllowedSigners() [][]byte { if a == nil { return nil } out := make([][]byte, 0, 1+len(a.Retailers)+len(a.Succession)) out = append(out, a.Owner) out = append(out, a.Retailers...) for _, ev := range a.Succession { if s, err := ParseSuccession(ev); err == nil { out = append(out, s.Incoming) } } return out } // CatalogHas reports whether the asset belongs to the named channel. func (a *AssetInfo) CatalogHas(channel, assetID string) bool { if a == nil { return false } return a.Channel != "" && a.Channel == channel && a.AssetID == assetID } // AssetLookup resolves an asset id. A nil lookup, an error, or a nil result // fails closed. type AssetLookup func(assetID string) (*AssetInfo, error) // Verifier checks a deed and its presentation proof in one place. type Verifier struct { // Host is this server's domain or origin, used to bind the proof. Host string // Lookup resolves an asset id to the current owner and its signers. Lookup AssetLookup // Nonces enforces proof single use. Required. Nonces *NonceCache // Revoked is a best-effort revocation lookup; nil honours the deed. Revoked func(deedID string) bool // Now overrides the clock. Now func() time.Time // MaxProofAge bounds proof age; zero means five minutes. MaxProofAge time.Duration } // Presentation is a successful verification. type Presentation struct { Deed *Deed Proof *PresentationProof Asset *AssetInfo } // VerifyPresentation verifies a deed and a proof for one request. // // deedJSON and proofJSON are the exact signed events the client presented. // requestedAsset is the asset the request is for; requestPubkey is the BUD-11 // request identity. The proof must name both, the deed must cover the asset, // and the deed signer must be authorized for it. func (v *Verifier) VerifyPresentation( deedJSON, proofJSON []byte, requestedAsset string, requestPubkey []byte, ) (*Presentation, error) { if v == nil || v.Lookup == nil { return nil, fmt.Errorf("deed: verifier has no asset lookup") } now := time.Now() if v.Now != nil { now = v.Now() } if !IsAssetID(requestedAsset) { return nil, fmt.Errorf("deed: requested asset id is invalid") } deedEv := event.New() if err := deedEv.UnmarshalJSON(deedJSON); err != nil { return nil, fmt.Errorf("deed: decode deed: %w", err) } d, err := ParseDeed(deedEv) if err != nil { return nil, err } info, err := v.Lookup(requestedAsset) if err != nil { return nil, fmt.Errorf("deed: resolve asset %s: %w", requestedAsset, err) } if info == nil || info.AssetID != requestedAsset { return nil, fmt.Errorf("deed: asset %s is unknown", requestedAsset) } // A sub deed may cover an asset whose AssetInfo names a channel. The deed // check below uses the catalog function, which is the only scope check that // applies to a subscription. var catalog func(string, string) bool if info.Channel != "" { catalog = info.CatalogHas } if err = d.Verify(DeedVerifyOptions{ RequestedAssetID: requestedAsset, Now: now, AllowedSigners: info.AllowedSigners(), CatalogHas: catalog, Revoked: v.Revoked, }); err != nil { return nil, err } proofEv := event.New() if err := proofEv.UnmarshalJSON(proofJSON); err != nil { return nil, fmt.Errorf("deed: decode presentation proof: %w", err) } p, err := ParseProof(proofEv) if err != nil { return nil, err } if err = p.Verify(ProofVerifyOptions{ Deed: d, RequestedAssetID: requestedAsset, Server: v.Host, RequestPubkey: requestPubkey, Now: now, MaxAge: v.MaxProofAge, Nonces: v.Nonces, }); err != nil { return nil, err } return &Presentation{Deed: d, Proof: p, Asset: info}, nil } // SessionKey returns the session this presentation establishes. func (p *Presentation) SessionKey() SessionKey { return SessionKey{ Buyer: hex.Enc(p.Deed.Buyer), Asset: p.Asset.AssetID, } }