/
invoice.go
54 lines (45 loc) · 1.18 KB
/
invoice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package request
import (
"encoding/json"
"github.com/buger/jsonparser"
"github.com/xplorfin/moneysocket-go/moneysocket/message/base"
)
// Invoice request a given number of Msats.
type Invoice struct {
BaseMoneySocketRequest
// Msats is the invoice amount
Msats int64
}
const msatsKey = "msats"
// NewRequestInvoice creates a new request for an invoice.
func NewRequestInvoice(msats int64) Invoice {
return Invoice{
NewBaseMoneySocketRequest(base.InvoiceRequest),
msats,
}
}
// ToJSON encodes an Invoice to json.
func (r Invoice) ToJSON() ([]byte, error) {
m := make(map[string]interface{})
m[msatsKey] = r.Msats
err := EncodeMoneysocketRequest(r, m)
if err != nil {
return nil, err
}
encodedRequest, err := json.Marshal(m)
return encodedRequest, err
}
// DecodeRequestInvoice turns a byte slice into a request invoice, return an error if not possible.
func DecodeRequestInvoice(payload []byte) (r Invoice, err error) {
request, err := DecodeRequest(payload)
if err != nil {
return Invoice{}, err
}
msats, err := jsonparser.GetInt(payload, msatsKey)
if err != nil {
return Invoice{}, err
}
r = Invoice{request, msats}
return r, nil
}
var _ MoneysocketRequest = &Invoice{}