package app import ( "context" "crypto/rand" "encoding/json" "fmt" "os" "git.smesh.lol/orly/pkg/blossom" "git.smesh.lol/orly/pkg/database" "git.smesh.lol/orly/pkg/lol/log" "git.smesh.lol/orly/pkg/musiquay/hostkey" "git.smesh.lol/orly/pkg/musiquay/receipts" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/hex" ) // musiquayGateConfig builds the access gate from the environment. It returns // nil when ORLY_MUSIQUAY_HOST is unset, so an ordinary Blossom deployment is // unaffected. // // Environment: // // ORLY_MUSIQUAY_HOST public host of this Blossom server (required) // ORLY_MUSIQUAY_COOKIE_SECRET hex HMAC key for the session cookie // ORLY_MUSIQUAY_KEY_DIR directory for K_receipt/K_agg/K_play // ORLY_MUSIQUAY_PUBLISHER_PUBKEY optional publisher pubkey for receipt copies // ORLY_MUSIQUAY_RECEIPT_NSEC explicit receipt key func musiquayGateConfig(db database.Database) (*blossom.GateConfig, func(context.Context, blossom.ServeInfo)) { host := os.Getenv("ORLY_MUSIQUAY_HOST") if host == "" { return nil, nil } secret := cookieSecret(db) gc := &blossom.GateConfig{Host: host, CookieSecret: secret} dir := os.Getenv("ORLY_MUSIQUAY_KEY_DIR") explicit := os.Getenv("ORLY_MUSIQUAY_RECEIPT_NSEC") if dir == "" && explicit == "" { log.W.F("musiquay: gate enabled without a receipt key; receipts are disabled") return gc, nil } keys := hostkey.New(hostkey.FromEnv(os.Getenv, dir)) sign, err := keys.Signer(hostkey.Receipt) if err != nil { log.E.F("musiquay: receipt key: %v", err) return gc, nil } issuer := &receipts.Issuer{ Sign: sign, Archive: receipts.NewArchive(db), Publisher: localWrapPublisher{db: db}, PublisherPub: publisherPubkey(), } hook := func(ctx context.Context, info blossom.ServeInfo) { if _, _, err := issuer.Issue(ctx, receipts.Serve{ AssetID: info.AssetID, Coordinate: info.Coordinate, Buyer: info.Buyer, Blob: info.Blob, Host: info.Host, Mode: info.Mode, Bytes: info.Bytes, AmountSats: info.AmountSats, }); err != nil { log.W.F("musiquay: receipt for %s: %v", info.Blob, err) } } return gc, hook } // publisherPubkey reads the optional publisher address for receipt copies. func publisherPubkey() []byte { s := os.Getenv("ORLY_MUSIQUAY_PUBLISHER_PUBKEY") if s == "" { return nil } pub, err := hex.Dec(s) if err != nil || len(pub) != 32 { log.W.F("musiquay: invalid ORLY_MUSIQUAY_PUBLISHER_PUBKEY") return nil } return pub } // cookieSecret loads the session-cookie HMAC key, generating and persisting one // on first use. It must be stable across workers. func cookieSecret(db database.Database) []byte { const marker = "musiquay:cookie-secret" if v, err := db.GetMarker(marker); err == nil && len(v) >= 32 { return v } if v := os.Getenv("ORLY_MUSIQUAY_COOKIE_SECRET"); v != "" { if b, err := hex.Dec(v); err == nil && len(b) >= 32 { _ = db.SetMarker(marker, b) return b } } b := make([]byte, 32) if _, err := rand.Read(b); err != nil { // Without a secret the cookie is disabled; the gate still works through // BUD-11 tokens. return nil } if err := db.SetMarker(marker, b); err != nil { log.W.F("musiquay: persist cookie secret: %v", err) } return b } // localWrapPublisher writes a gift wrap to the relay database so the recipient // can fetch it. The relay serves a wrap only to its p-tagged recipient, so the // write does not leak the artifact. type localWrapPublisher struct{ db database.Database } func (p localWrapPublisher) Publish(ctx context.Context, ev *event.E) error { if p.db == nil { return fmt.Errorf("musiquay: no database for wrap publication") } // Round-trip through the wire form so the stored event and its indexes are // identical to one received over NIP-01. raw, err := ev.MarshalJSON() if err != nil { return err } var wire event.E if err = json.Unmarshal(raw, &wire); err != nil { return err } _, err = p.db.SaveEvent(ctx, &wire) return err }