1 // Package nutreq builds and parses Cashu payment requests in the NUT-18
2 // CBOR+base64url ("creqA") encoding.
3 //
4 // Musiquay hosts return a NUT-24 HTTP 402 with an X-Cashu header that carries
5 // one of these strings. A wallet that speaks NUT-18 can decode it, build a
6 // matching token for one of the listed mints, and retry. NUT-26 bech32m
7 // ("creqB") is a decode-only convenience: wallets MUST parse both encodings, so
8 // a host that only emits creqA is still reachable, but accepting creqB on the
9 // inbound side costs a TLV decoder we may add later.
10 package nutreq
11 12 import (
13 "encoding/base64"
14 "fmt"
15 "strings"
16 "unicode/utf8"
17 18 "github.com/fxamacker/cbor/v2"
19 )
20 21 // PaymentRequest is the NUT-18 payment request object. Field names follow the
22 // NUT-18 JSON keys so the CBOR encoding is wire-compatible.
23 type PaymentRequest struct {
24 // ID is the payment id, echoed in the payment payload.
25 ID string `cbor:"i,omitempty" json:"i,omitempty"`
26 // Amount is the requested amount, net of input fees.
27 Amount uint64 `cbor:"a,omitempty" json:"a,omitempty"`
28 // Unit is the currency unit, for example "sat".
29 Unit string `cbor:"u,omitempty" json:"u,omitempty"`
30 // SingleUse marks the request as valid once.
31 SingleUse bool `cbor:"s,omitempty" json:"s,omitempty"`
32 // Mints is the accepted mint list.
33 Mints []string `cbor:"m,omitempty" json:"m,omitempty"`
34 // MintPreferred makes the mint list advisory when true.
35 MintPreferred bool `cbor:"mp,omitempty" json:"mp,omitempty"`
36 // Methods lists the accepted payment methods and their fees.
37 Methods []SupportedMethod `cbor:"sm,omitempty" json:"sm,omitempty"`
38 // Description is shown to the payer.
39 Description string `cbor:"d,omitempty" json:"d,omitempty"`
40 // Transports are the ways the payment may be sent.
41 Transports []Transport `cbor:"t,omitempty" json:"t,omitempty"`
42 // Nut10 is the required NUT-10 locking condition, when the receiver wants a
43 // P2PK-locked token.
44 Nut10 *NUT10Option `cbor:"nut10,omitempty" json:"nut10,omitempty"`
45 }
46 47 // SupportedMethod is one entry of the NUT-18 "sm" list.
48 type SupportedMethod struct {
49 // Method is the payment method name, "bolt11", "bolt12", or "onchain".
50 Method string `cbor:"mn" json:"mn"`
51 // Fee is the additional per-method fee in request units; omitted is zero.
52 Fee uint64 `cbor:"mf,omitempty" json:"mf,omitempty"`
53 }
54 55 // Transport is one NUT-18 transport entry.
56 type Transport struct {
57 // Type is "nostr" or "post".
58 Type string `cbor:"t" json:"t"`
59 // Target is an nprofile for nostr or a URL for post.
60 Target string `cbor:"a" json:"a"`
61 // Tags are optional string tuples.
62 Tags [][]string `cbor:"g,omitempty" json:"g,omitempty"`
63 }
64 65 // NUT10Option is the payee's required locking condition.
66 type NUT10Option struct {
67 // Kind is the NUT-10 secret kind: "P2PK" or "HTLC".
68 Kind string `cbor:"k" json:"k"`
69 // Data is the kind-specific secret data: a pubkey or a hash.
70 Data string `cbor:"d" json:"d"`
71 // Tags are optional NUT-10 payment tags.
72 Tags [][]string `cbor:"t,omitempty" json:"t,omitempty"`
73 }
74 75 // Prefix is the NUT-18 encoded-request prefix.
76 const Prefix = "creqA"
77 78 // EncodeA returns the "creqA" + base64url(CBOR(request)) encoding.
79 func (p *PaymentRequest) EncodeA() (string, error) {
80 if p == nil {
81 return "", fmt.Errorf("nutreq: nil request")
82 }
83 if p.Amount > 0 && p.Unit == "" {
84 return "", fmt.Errorf("nutreq: unit is required when amount is set")
85 }
86 enc, err := cbor.Marshal(p)
87 if err != nil {
88 return "", fmt.Errorf("nutreq: cbor: %w", err)
89 }
90 return Prefix + base64.RawURLEncoding.EncodeToString(enc), nil
91 }
92 93 // Decode parses a creqA string. It is the inverse of EncodeA, and it is
94 // deliberately strict about the version character so a future creqB cannot be
95 // silently misread.
96 func Decode(s string) (*PaymentRequest, error) {
97 s = strings.TrimSpace(s)
98 if !strings.HasPrefix(s, Prefix) {
99 if strings.HasPrefix(strings.ToUpper(s), "CREQB1") || strings.HasPrefix(s, "creqb1") {
100 return nil, fmt.Errorf("nutreq: NUT-26 bech32m requests are not decoded here")
101 }
102 return nil, fmt.Errorf("nutreq: not a NUT-18 payment request")
103 }
104 body := strings.TrimRight(s[len(Prefix):], "=")
105 raw, err := base64.RawURLEncoding.DecodeString(body)
106 if err != nil {
107 // Tolerate a standard-base64 body from a sloppy encoder.
108 raw, err = base64.StdEncoding.DecodeString(s[len(Prefix):])
109 if err != nil {
110 return nil, fmt.Errorf("nutreq: base64: %w", err)
111 }
112 }
113 p := &PaymentRequest{}
114 if err = cbor.Unmarshal(raw, p); err != nil {
115 return nil, fmt.Errorf("nutreq: cbor: %w", err)
116 }
117 if p.Amount > 0 && p.Unit == "" {
118 return nil, fmt.Errorf("nutreq: amount set without a unit")
119 }
120 for _, m := range p.Mints {
121 if !utf8.ValidString(m) {
122 return nil, fmt.Errorf("nutreq: mint is not valid utf-8")
123 }
124 }
125 return p, nil
126 }
127 128 // AcceptsMint reports whether the request authorizes a mint URL.
129 func (p *PaymentRequest) AcceptsMint(mint string) bool {
130 if len(p.Mints) == 0 {
131 // No mint list means any mint.
132 return true
133 }
134 for _, m := range p.Mints {
135 if strings.EqualFold(strings.TrimRight(m, "/"), strings.TrimRight(mint, "/")) {
136 return true
137 }
138 }
139 return false
140 }
141 142 // New builds the payment request a Blossom host returns with a 402.
143 func New(amount uint64, mintURL, description string) *PaymentRequest {
144 p := &PaymentRequest{
145 Amount: amount,
146 Unit: "sat",
147 Description: description,
148 }
149 if mintURL != "" {
150 p.Mints = []string{mintURL}
151 }
152 return p
153 }
154 155 // NewP2PK builds a payment request that requires a P2PK-locked token to the
156 // supplied x-only public key.
157 func NewP2PK(amount uint64, mintURL, description, receiverPubHex string) *PaymentRequest {
158 p := New(amount, mintURL, description)
159 if receiverPubHex != "" {
160 p.Nut10 = &NUT10Option{Kind: "P2PK", Data: receiverPubHex}
161 }
162 return p
163 }
164