package sales import ( "context" "encoding/json" "fmt" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/hex" "git.smesh.lol/orly/pkg/nostr/encoders/kind" "git.smesh.lol/orly/pkg/nostr/encoders/tag" "git.smesh.lol/orly/pkg/nostr/encoders/timestamp" ) // RevocationNamespace and RevocationLabel are the NIP-32 label a seller uses to // revoke a deed for fraud or chargeback. const ( RevocationNamespace = "musiquay.revocation" RevocationLabel = "revoked" ) // RevocationArchiver records a revocation idempotently. DBDeeds implements it. type RevocationArchiver interface { RecordRevocation(deedID, eventID string) error RevokedEvent(deedID string) (string, error) } // Revoke publishes a best-effort revocation label for a deed. // // Revocation is the only exception to append-only ownership, and it is best // effort: a host that cannot see the label honours the deed. The label is // published to the same relays as the filters, and the seller archives it so a // repeat call is idempotent. func (s *Service) Revoke(ctx context.Context, deedID string) (*event.E, error) { if len(deedID) != 64 { return nil, fmt.Errorf("sales: deed id must be 64 hex characters") } if _, err := hex.Dec(deedID); err != nil { return nil, fmt.Errorf("sales: deed id is not hex") } if arch, ok := s.cfg.Archive.(RevocationArchiver); ok { if existing, err := arch.RevokedEvent(deedID); err == nil && existing != "" { // Already published; return a synthetic-free marker by loading the // stored event is out of scope, so report the id in the error path. return nil, fmt.Errorf("sales: deed %s was already revoked in %s", deedID, existing) } } ev := event.New() ev.Kind = kind.New(1985).ToU16() ev.CreatedAt = timestamp.Now().I64() ev.Tags = tag.NewS() ev.Tags.Append(tag.NewFromBytesSlice([]byte("e"), []byte(deedID))) ev.Tags.Append(tag.NewFromBytesSlice([]byte("L"), []byte(RevocationNamespace))) ev.Tags.Append(tag.NewFromBytesSlice([]byte("l"), []byte(RevocationLabel), []byte(RevocationNamespace))) if err := ev.Sign(s.cfg.Sign); err != nil { return nil, fmt.Errorf("sales: sign revocation: %w", err) } if err := s.cfg.Relay.Publish(ctx, ev); err != nil { return nil, fmt.Errorf("sales: publish revocation: %w", err) } if arch, ok := s.cfg.Archive.(RevocationArchiver); ok { if err := arch.RecordRevocation(deedID, hex.Enc(ev.ID)); err != nil { return ev, fmt.Errorf("sales: archive revocation: %w", err) } } return ev, nil } // MarshalRevocation is a small helper for CLI output. func MarshalRevocation(ev *event.E) ([]byte, error) { return json.Marshal(ev) }