musiquay.go raw

   1  package policy
   2  
   3  import (
   4  	"git.smesh.lol/orly/pkg/nostr/encoders/event"
   5  	"git.smesh.lol/orly/pkg/nostr/encoders/kind"
   6  )
   7  
   8  // CheckMusiquayWrite enforces the musiquay rule that private artifacts never
   9  // appear as plain events.
  10  //
  11  // Purchase orders (3222), entitlement deeds (3223), and delivery receipts
  12  // (32218) are only ever carried inside a NIP-59 kind 1059 gift wrap. The relay
  13  // sees the wrap; it must never see the payload. A public event of one of those
  14  // kinds is refused. Every other musiquay kind is accepted here and remains
  15  // subject to the operator's ordinary policy.
  16  //
  17  // It returns allowed=true for kinds it does not own, so it can be called from a
  18  // generic pipeline without suppressing unrelated events.
  19  func CheckMusiquayWrite(ev *event.E) (allowed bool, reason string) {
  20  	if ev == nil {
  21  		return false, "nil event"
  22  	}
  23  	if kind.IsMusiquayPrivate(ev.Kind) {
  24  		return false, "musiquay private kind must travel inside a NIP-59 gift wrap"
  25  	}
  26  	return true, ""
  27  }
  28  
  29  // MusiquayWhitelist returns the kind numbers an operator must add to a kind
  30  // whitelist to accept the musiquay block. Gift wrap is included because it is
  31  // the transport for orders, deeds, and receipts.
  32  func MusiquayWhitelist() []int {
  33  	out := make([]int, 0, len(kind.MusiquayAll)+1)
  34  	for _, k := range kind.MusiquayAll {
  35  		out = append(out, k.ToInt())
  36  	}
  37  	out = append(out, kind.GiftWrap.ToInt())
  38  	return out
  39  }
  40  
  41  // MusiquayWrapRecipientOnly reports whether a kind's read access is restricted
  42  // to the keys named in its p tags regardless of the server's global auth
  43  // configuration. Gift wraps always are: a wrap is addressed to one recipient
  44  // and serving it to anyone else leaks the artifact.
  45  func MusiquayWrapRecipientOnly(k uint16) bool {
  46  	return k == kind.GiftWrap.ToU16()
  47  }
  48  
  49  // NIP-32 label namespaces used by musiquay.
  50  const (
  51  	// LabelKind is the NIP-32 label event kind.
  52  	LabelKind = 1985
  53  	// RevocationNamespace labels a revoked deed.
  54  	RevocationNamespace = "musiquay.revocation"
  55  	// RevocationLabel is the revocation label value.
  56  	RevocationLabel = "revoked"
  57  	// AttestNamespace labels scene attestations.
  58  	AttestNamespace = "musiquay.attest"
  59  	// CreditNamespace labels credit confirmations.
  60  	CreditNamespace = "musiquay.credit"
  61  )
  62  
  63  // HasNIP32Label reports whether an event carries a NIP-32 namespace and value.
  64  // A host uses it to read revocation, credit, and attestation labels without
  65  // trusting an event kind alone.
  66  func HasNIP32Label(ev *event.E, namespace, value string) bool {
  67  	if ev == nil || ev.Tags == nil {
  68  		return false
  69  	}
  70  	okNamespace := false
  71  	for _, t := range ev.Tags.GetAll([]byte("L")) {
  72  		if string(t.Value()) == namespace {
  73  			okNamespace = true
  74  			break
  75  		}
  76  	}
  77  	if !okNamespace {
  78  		return false
  79  	}
  80  	for _, t := range ev.Tags.GetAll([]byte("l")) {
  81  		if string(t.Value()) != value {
  82  			continue
  83  		}
  84  		if len(t.T) >= 3 && string(t.T[2]) != namespace {
  85  			continue
  86  		}
  87  		return true
  88  	}
  89  	return false
  90  }
  91