// Package play implements certified play reports. // // A paid play is certified with a BDHKE blind signature from the serving host's // play key, accompanied by a NUT-12 DLEQ proof. The host never learns which // listener received a signature, and the listener can prove the signature was // issued for a specific host, asset, blob, and time window. // // The statement deliberately covers only facts the host itself observed. It is // an oracle: a host that signs a blind point without a bound statement can be // made to certify a fiction. Money is accounted by receipts and aggregates, so // price and tier are not part of the statement. package play import ( "fmt" "strconv" "strings" "sync" "time" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/elnosh/gonuts/crypto" "git.smesh.lol/orly/pkg/nostr/encoders/hex" "git.smesh.lol/orly/pkg/nostr/interfaces/signer" ) // Domain is the statement domain separator. const Domain = "musiquay.play:v1" // Statement builds the signed statement bytes: // // musiquay.play:v1|||| func Statement(assetAddress, blobHex string, start, end int64) []byte { return []byte(fmt.Sprintf("%s|%s|%s|%d|%d", Domain, assetAddress, strings.ToLower(blobHex), start, end)) } // ParsedStatement is a decoded statement. type ParsedStatement struct { AssetAddress string Blob string Start int64 End int64 } // ParseStatement validates the domain and shape. func ParseStatement(raw []byte) (*ParsedStatement, error) { parts := strings.Split(string(raw), "|") if len(parts) != 5 { return nil, fmt.Errorf("play: statement must have five fields") } if parts[0] != Domain { return nil, fmt.Errorf("play: wrong domain %q", parts[0]) } if len(parts[2]) != 64 { return nil, fmt.Errorf("play: blob hash must be 64 hex characters") } start, err := strconv.ParseInt(parts[3], 10, 64) if err != nil { return nil, fmt.Errorf("play: invalid start") } end, err := strconv.ParseInt(parts[4], 10, 64) if err != nil { return nil, fmt.Errorf("play: invalid end") } if end < start { return nil, fmt.Errorf("play: end precedes start") } return &ParsedStatement{AssetAddress: parts[1], Blob: parts[2], Start: start, End: end}, nil } // DLEQ is the NUT-12 proof that the blind signature used the host's key. type DLEQ struct { E string `json:"e"` S string `json:"s"` } // Certification is the host's response to a blind-sign request. type Certification struct { // C_ is the blind signature, compressed hex. C_ string `json:"C_"` // DLEQ proves C_ = k*B_ without revealing k. DLEQ DLEQ `json:"dleq"` } // Sign produces C_ and a DLEQ proof for one blinded point. // // The blinded point must be a valid compressed secp256k1 key. The signature is // over the point, not the statement: the statement binding is enforced by the // caller's entitlement check and by the host's record of what it served. func Sign(sign signer.I, blindedHex string) (*Certification, error) { b, err := hex.Dec(blindedHex) if err != nil || len(b) == 0 { return nil, fmt.Errorf("play: invalid blinded point") } B_, err := secp256k1.ParsePubKey(b) if err != nil { return nil, fmt.Errorf("play: blinded point is not a public key: %w", err) } sec := sign.Sec() if len(sec) != 32 { return nil, fmt.Errorf("play: signer has no secret key") } k := secp256k1.PrivKeyFromBytes(sec) C_ := crypto.SignBlindedMessage(B_, k) e, s := crypto.GenerateDLEQ(k, B_, C_) eb := e.Key.Bytes() sb := s.Key.Bytes() return &Certification{ C_: hex.Enc(C_.SerializeCompressed()), DLEQ: DLEQ{ E: hex.Enc(eb[:]), S: hex.Enc(sb[:]), }, }, nil } // Verify checks a DLEQ proof for a host, a blinded point, and a blind // signature. It is the independent verifier a play-report consumer runs. func Verify(hostPubHex, blindedHex, cHex, eHex, sHex string) bool { host, err := hex.Dec(hostPubHex) if err != nil || len(host) != 32 { return false } // The host key is x-only on Nostr; the DLEQ A is a 33-byte point. A, err := secp256k1.ParsePubKey(append([]byte{0x02}, host...)) if err != nil { return false } return verifyPoints(A, blindedHex, cHex, eHex, sHex) } func verifyPoints(A *secp256k1.PublicKey, blindedHex, cHex, eHex, sHex string) bool { bb, err := hex.Dec(blindedHex) if err != nil { return false } B_, err := secp256k1.ParsePubKey(bb) if err != nil { return false } cb, err := hex.Dec(cHex) if err != nil { return false } C_, err := secp256k1.ParsePubKey(cb) if err != nil { return false } eb, err := hex.Dec(eHex) if err != nil || len(eb) == 0 { return false } sb, err := hex.Dec(sHex) if err != nil || len(sb) == 0 { return false } e := secp256k1.PrivKeyFromBytes(eb) s := secp256k1.PrivKeyFromBytes(sb) return crypto.VerifyDLEQ(e, s, A, B_, C_) } // IssuanceLimiter allows at most one signature per (identity, asset, window) // within a window. It is in-memory and honour-system across restarts, which is // the right trade: the limit is a sybil speed bump, not accounting. type IssuanceLimiter struct { mu sync.Mutex seen map[string]time.Time ttl time.Duration } // NewIssuanceLimiter creates a limiter whose entries live for ttl. func NewIssuanceLimiter(ttl time.Duration) *IssuanceLimiter { if ttl <= 0 { ttl = 15 * time.Minute } return &IssuanceLimiter{seen: make(map[string]time.Time), ttl: ttl} } // Allow records an issuance and reports whether it is the first. func (l *IssuanceLimiter) Allow(identityHex, asset string, start, end int64) bool { key := fmt.Sprintf("%s|%s|%d|%d", identityHex, asset, start, end) now := time.Now() l.mu.Lock() defer l.mu.Unlock() for k, at := range l.seen { if now.Sub(at) > l.ttl { delete(l.seen, k) } } if _, ok := l.seen[key]; ok { return false } l.seen[key] = now return true }