// Package nutreq builds and parses Cashu payment requests in the NUT-18 // CBOR+base64url ("creqA") encoding. // // Musiquay hosts return a NUT-24 HTTP 402 with an X-Cashu header that carries // one of these strings. A wallet that speaks NUT-18 can decode it, build a // matching token for one of the listed mints, and retry. NUT-26 bech32m // ("creqB") is a decode-only convenience: wallets MUST parse both encodings, so // a host that only emits creqA is still reachable, but accepting creqB on the // inbound side costs a TLV decoder we may add later. package nutreq import ( "encoding/base64" "fmt" "strings" "unicode/utf8" "github.com/fxamacker/cbor/v2" ) // PaymentRequest is the NUT-18 payment request object. Field names follow the // NUT-18 JSON keys so the CBOR encoding is wire-compatible. type PaymentRequest struct { // ID is the payment id, echoed in the payment payload. ID string `cbor:"i,omitempty" json:"i,omitempty"` // Amount is the requested amount, net of input fees. Amount uint64 `cbor:"a,omitempty" json:"a,omitempty"` // Unit is the currency unit, for example "sat". Unit string `cbor:"u,omitempty" json:"u,omitempty"` // SingleUse marks the request as valid once. SingleUse bool `cbor:"s,omitempty" json:"s,omitempty"` // Mints is the accepted mint list. Mints []string `cbor:"m,omitempty" json:"m,omitempty"` // MintPreferred makes the mint list advisory when true. MintPreferred bool `cbor:"mp,omitempty" json:"mp,omitempty"` // Methods lists the accepted payment methods and their fees. Methods []SupportedMethod `cbor:"sm,omitempty" json:"sm,omitempty"` // Description is shown to the payer. Description string `cbor:"d,omitempty" json:"d,omitempty"` // Transports are the ways the payment may be sent. Transports []Transport `cbor:"t,omitempty" json:"t,omitempty"` // Nut10 is the required NUT-10 locking condition, when the receiver wants a // P2PK-locked token. Nut10 *NUT10Option `cbor:"nut10,omitempty" json:"nut10,omitempty"` } // SupportedMethod is one entry of the NUT-18 "sm" list. type SupportedMethod struct { // Method is the payment method name, "bolt11", "bolt12", or "onchain". Method string `cbor:"mn" json:"mn"` // Fee is the additional per-method fee in request units; omitted is zero. Fee uint64 `cbor:"mf,omitempty" json:"mf,omitempty"` } // Transport is one NUT-18 transport entry. type Transport struct { // Type is "nostr" or "post". Type string `cbor:"t" json:"t"` // Target is an nprofile for nostr or a URL for post. Target string `cbor:"a" json:"a"` // Tags are optional string tuples. Tags [][]string `cbor:"g,omitempty" json:"g,omitempty"` } // NUT10Option is the payee's required locking condition. type NUT10Option struct { // Kind is the NUT-10 secret kind: "P2PK" or "HTLC". Kind string `cbor:"k" json:"k"` // Data is the kind-specific secret data: a pubkey or a hash. Data string `cbor:"d" json:"d"` // Tags are optional NUT-10 payment tags. Tags [][]string `cbor:"t,omitempty" json:"t,omitempty"` } // Prefix is the NUT-18 encoded-request prefix. const Prefix = "creqA" // EncodeA returns the "creqA" + base64url(CBOR(request)) encoding. func (p *PaymentRequest) EncodeA() (string, error) { if p == nil { return "", fmt.Errorf("nutreq: nil request") } if p.Amount > 0 && p.Unit == "" { return "", fmt.Errorf("nutreq: unit is required when amount is set") } enc, err := cbor.Marshal(p) if err != nil { return "", fmt.Errorf("nutreq: cbor: %w", err) } return Prefix + base64.RawURLEncoding.EncodeToString(enc), nil } // Decode parses a creqA string. It is the inverse of EncodeA, and it is // deliberately strict about the version character so a future creqB cannot be // silently misread. func Decode(s string) (*PaymentRequest, error) { s = strings.TrimSpace(s) if !strings.HasPrefix(s, Prefix) { if strings.HasPrefix(strings.ToUpper(s), "CREQB1") || strings.HasPrefix(s, "creqb1") { return nil, fmt.Errorf("nutreq: NUT-26 bech32m requests are not decoded here") } return nil, fmt.Errorf("nutreq: not a NUT-18 payment request") } body := strings.TrimRight(s[len(Prefix):], "=") raw, err := base64.RawURLEncoding.DecodeString(body) if err != nil { // Tolerate a standard-base64 body from a sloppy encoder. raw, err = base64.StdEncoding.DecodeString(s[len(Prefix):]) if err != nil { return nil, fmt.Errorf("nutreq: base64: %w", err) } } p := &PaymentRequest{} if err = cbor.Unmarshal(raw, p); err != nil { return nil, fmt.Errorf("nutreq: cbor: %w", err) } if p.Amount > 0 && p.Unit == "" { return nil, fmt.Errorf("nutreq: amount set without a unit") } for _, m := range p.Mints { if !utf8.ValidString(m) { return nil, fmt.Errorf("nutreq: mint is not valid utf-8") } } return p, nil } // AcceptsMint reports whether the request authorizes a mint URL. func (p *PaymentRequest) AcceptsMint(mint string) bool { if len(p.Mints) == 0 { // No mint list means any mint. return true } for _, m := range p.Mints { if strings.EqualFold(strings.TrimRight(m, "/"), strings.TrimRight(mint, "/")) { return true } } return false } // New builds the payment request a Blossom host returns with a 402. func New(amount uint64, mintURL, description string) *PaymentRequest { p := &PaymentRequest{ Amount: amount, Unit: "sat", Description: description, } if mintURL != "" { p.Mints = []string{mintURL} } return p } // NewP2PK builds a payment request that requires a P2PK-locked token to the // supplied x-only public key. func NewP2PK(amount uint64, mintURL, description, receiverPubHex string) *PaymentRequest { p := New(amount, mintURL, description) if receiverPubHex != "" { p.Nut10 = &NUT10Option{Kind: "P2PK", Data: receiverPubHex} } return p }