revocation.go raw
1 package sales
2
3 import (
4 "context"
5 "encoding/json"
6 "fmt"
7
8 "git.smesh.lol/orly/pkg/nostr/encoders/event"
9 "git.smesh.lol/orly/pkg/nostr/encoders/hex"
10 "git.smesh.lol/orly/pkg/nostr/encoders/kind"
11 "git.smesh.lol/orly/pkg/nostr/encoders/tag"
12 "git.smesh.lol/orly/pkg/nostr/encoders/timestamp"
13 )
14
15 // RevocationNamespace and RevocationLabel are the NIP-32 label a seller uses to
16 // revoke a deed for fraud or chargeback.
17 const (
18 RevocationNamespace = "musiquay.revocation"
19 RevocationLabel = "revoked"
20 )
21
22 // RevocationArchiver records a revocation idempotently. DBDeeds implements it.
23 type RevocationArchiver interface {
24 RecordRevocation(deedID, eventID string) error
25 RevokedEvent(deedID string) (string, error)
26 }
27
28 // Revoke publishes a best-effort revocation label for a deed.
29 //
30 // Revocation is the only exception to append-only ownership, and it is best
31 // effort: a host that cannot see the label honours the deed. The label is
32 // published to the same relays as the filters, and the seller archives it so a
33 // repeat call is idempotent.
34 func (s *Service) Revoke(ctx context.Context, deedID string) (*event.E, error) {
35 if len(deedID) != 64 {
36 return nil, fmt.Errorf("sales: deed id must be 64 hex characters")
37 }
38 if _, err := hex.Dec(deedID); err != nil {
39 return nil, fmt.Errorf("sales: deed id is not hex")
40 }
41 if arch, ok := s.cfg.Archive.(RevocationArchiver); ok {
42 if existing, err := arch.RevokedEvent(deedID); err == nil && existing != "" {
43 // Already published; return a synthetic-free marker by loading the
44 // stored event is out of scope, so report the id in the error path.
45 return nil, fmt.Errorf("sales: deed %s was already revoked in %s", deedID, existing)
46 }
47 }
48
49 ev := event.New()
50 ev.Kind = kind.New(1985).ToU16()
51 ev.CreatedAt = timestamp.Now().I64()
52 ev.Tags = tag.NewS()
53 ev.Tags.Append(tag.NewFromBytesSlice([]byte("e"), []byte(deedID)))
54 ev.Tags.Append(tag.NewFromBytesSlice([]byte("L"), []byte(RevocationNamespace)))
55 ev.Tags.Append(tag.NewFromBytesSlice([]byte("l"), []byte(RevocationLabel), []byte(RevocationNamespace)))
56 if err := ev.Sign(s.cfg.Sign); err != nil {
57 return nil, fmt.Errorf("sales: sign revocation: %w", err)
58 }
59 if err := s.cfg.Relay.Publish(ctx, ev); err != nil {
60 return nil, fmt.Errorf("sales: publish revocation: %w", err)
61 }
62 if arch, ok := s.cfg.Archive.(RevocationArchiver); ok {
63 if err := arch.RecordRevocation(deedID, hex.Enc(ev.ID)); err != nil {
64 return ev, fmt.Errorf("sales: archive revocation: %w", err)
65 }
66 }
67 return ev, nil
68 }
69
70 // MarshalRevocation is a small helper for CLI output.
71 func MarshalRevocation(ev *event.E) ([]byte, error) { return json.Marshal(ev) }
72