|
| 1 | +package nut18 |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/elnosh/gonuts/cashu" |
| 9 | + "github.com/fxamacker/cbor/v2" |
| 10 | +) |
| 11 | + |
| 12 | +const PaymentRequestPrefix = "creq" |
| 13 | +const PaymentRequestV1 = "A" |
| 14 | + |
| 15 | +type TransportTypes string |
| 16 | + |
| 17 | +const Nostr TransportTypes = "nostr" |
| 18 | +const Http TransportTypes = "http" |
| 19 | + |
| 20 | +// const Nostr = "nostr" |
| 21 | +const Rest = "rest" |
| 22 | +const NIP17 = "17" |
| 23 | +const NIP60 = "60" |
| 24 | + |
| 25 | +var ( |
| 26 | + ErrUnitNotSet = errors.New("You need to set the Unit when using amounts") |
| 27 | +) |
| 28 | + |
| 29 | +type PaymentRequest struct { |
| 30 | + Id *string `json:"i,omitempty" cbor:"i,omitempty"` |
| 31 | + Amount *uint64 `json:"a,omitempty" cbor:"a,omitempty"` |
| 32 | + Unit *string `json:"u,omitempty" cbor:"u,omitempty"` |
| 33 | + Single *bool `json:"s,omitempty" cbor:"s,omitempty"` |
| 34 | + Mints []string `json:"m,omitempty" cbor:"m,omitempty"` |
| 35 | + Description *string `json:"d,omitempty" cbor:"d,omitempty"` |
| 36 | + Transport []Transport `json:"t,omitempty" cbor:"t,omitempty"` |
| 37 | + Nut10 *Nut10Lock `json:"nut10,omitempty" cbor:"nut10,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +type Transport struct { |
| 41 | + Type TransportTypes `json:"t" cbor:"t"` |
| 42 | + Target string `json:"a" cbor:"a"` |
| 43 | + Tags [][]string `json:"g,omitempty" cbor:"g,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +type Nut10Lock struct { |
| 47 | + Key string `json:"k" cbor:"k"` |
| 48 | + Data string `json:"d" cbor:"d"` |
| 49 | + Tags [][]string `json:"t,omitempty" cbor:"t,omitempty"` |
| 50 | +} |
| 51 | + |
| 52 | +func (p PaymentRequest) Encode() (string, error) { |
| 53 | + tokenBytes, err := cbor.Marshal(p) |
| 54 | + if err != nil { |
| 55 | + return "", fmt.Errorf("cbor.Marshal(p): %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + return PaymentRequestPrefix + PaymentRequestV1 + base64.URLEncoding.EncodeToString(tokenBytes), nil |
| 59 | +} |
| 60 | + |
| 61 | +func (p *PaymentRequest) AddAmount(amount uint64, unit string) error { |
| 62 | + if unit == "" { |
| 63 | + return ErrUnitNotSet |
| 64 | + } |
| 65 | + |
| 66 | + p.Amount = &amount |
| 67 | + p.Unit = &unit |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | +func (p *PaymentRequest) SetSingleUse() { |
| 72 | + single := true |
| 73 | + p.Single = &single |
| 74 | +} |
| 75 | + |
| 76 | +func (p *PaymentRequest) SetMints(mints []string) { |
| 77 | + p.Mints = mints |
| 78 | +} |
| 79 | + |
| 80 | +func (p *PaymentRequest) SetDescription(desc string) { |
| 81 | + p.Description = &desc |
| 82 | +} |
| 83 | + |
| 84 | +func (p *PaymentRequest) SetNostr(nprofile string) { |
| 85 | + transportTags := [][]string{ |
| 86 | + {"n", NIP17}, |
| 87 | + {"n", NIP60}, |
| 88 | + } |
| 89 | + transport := Transport{ |
| 90 | + Type: Nostr, |
| 91 | + Target: nprofile, |
| 92 | + Tags: transportTags, |
| 93 | + } |
| 94 | + p.Transport = append(p.Transport, transport) |
| 95 | +} |
| 96 | + |
| 97 | +func (p *PaymentRequest) AddNut10Lock(nut10Lock Nut10Lock) { |
| 98 | + p.Nut10 = &nut10Lock |
| 99 | +} |
| 100 | + |
| 101 | +func (p *PaymentRequest) GetNostrTransport() *Transport { |
| 102 | + for i := range p.Transport { |
| 103 | + if p.Transport[i].Type == Nostr { |
| 104 | + return &p.Transport[i] |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func DecodePaymentRequest(requestString string) (PaymentRequest, error) { |
| 111 | + if len(requestString) < len(PaymentRequestPrefix)+len(PaymentRequestV1) { |
| 112 | + return PaymentRequest{}, fmt.Errorf("payment request is too small") |
| 113 | + } |
| 114 | + encodedToken := requestString[len(PaymentRequestPrefix)+len(PaymentRequestV1):] |
| 115 | + base64DecodedToken, err := base64.URLEncoding.DecodeString(encodedToken) |
| 116 | + if err != nil { |
| 117 | + return PaymentRequest{}, fmt.Errorf("base64.URLEncoding.DecodeString(encodedToken): %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + var payReq PaymentRequest |
| 121 | + err = cbor.Unmarshal(base64DecodedToken, &payReq) |
| 122 | + if err != nil { |
| 123 | + return PaymentRequest{}, fmt.Errorf("cbor.Marshal(p): %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + return payReq, nil |
| 127 | +} |
| 128 | + |
| 129 | +type PaymentRequestPayload struct { |
| 130 | + Id string `json:"id,omitempty"` |
| 131 | + Memo string `json:"memo,omitempty"` |
| 132 | + Mint string `json:"mint"` |
| 133 | + Unit string `json:"unit"` |
| 134 | + Proofs cashu.Proofs `json:"proofs"` |
| 135 | +} |
0 commit comments