You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bit4sat/ln/invoice.go

95 lines
1.7 KiB
Go

package ln
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type Currency int
const (
CurMSat Currency = iota
CurSat
CurBTC
CurUSD
CurEur
)
const (
InfoEndpoint = "info"
InvoiceEndpoint = "invoice"
)
var (
CurrencyString = map[Currency]string{
CurUSD: "USD",
CurSat: "SAT",
CurBTC: "BTC",
CurMSat: "MSAT",
CurEur: "EUR",
}
CurrencyID = map[string]Currency{
"USD": CurUSD,
"SAT": CurSat,
"MSAT": CurMSat,
"BTC": CurBTC,
"EUR": CurEur,
}
)
type timestamp time.Time
func (t *timestamp) UnmarshalJSON(in []byte) error {
if string(in) == "null" {
*t = timestamp(time.Unix(0, 0))
return nil
}
val, err := strconv.Atoi(string(in))
if err != nil {
return fmt.Errorf("cannot unmarshal timestamp int")
}
parsedTime := time.Unix(int64(val), 0)
*t = timestamp(parsedTime)
return nil
}
func (t timestamp) MarshalJSON() ([]byte, error) {
str := strconv.Itoa(int(time.Time(t).Unix()))
return []byte(str), nil
}
func (t timestamp) String() string {
return time.Time(t).Format(time.RFC3339)
}
type Invoice struct {
Id string `json:"id"`
Description string `json:"description"`
Msatoshi string `json:"msatoshi"`
Payreq string `json:"payreq"`
RHash string `json:"rhash"`
Status string `json:"status"`
QuotedCurrency string `json:"quoted_currency"`
QuotedAmount string `json:"quoted_amount"`
PaidAt timestamp `json:"paid_at"`
CreatedAt timestamp `json:"created_at"`
ExpiresAt timestamp `json:"expires_at"`
Expired bool `json:"-"`
}
func (i Invoice) MarshalBinary() ([]byte, error) {
return json.Marshal(i)
}
func (i *Invoice) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, &i)
}