84 lines
2.1 KiB
Go
Raw Normal View History

2019-10-01 12:22:30 -03:00
package libwallet
import (
2020-11-09 10:05:29 -03:00
"fmt"
2019-10-01 12:22:30 -03:00
"github.com/lightningnetwork/lnd/zpay32"
"github.com/pkg/errors"
)
// Invoice is muun's invoice struct
type Invoice struct {
RawInvoice string
FallbackAddress *MuunPaymentURI
Network *Network
MilliSat string
Destination []byte
2019-12-17 17:32:12 -03:00
PaymentHash []byte
2019-10-01 12:22:30 -03:00
Expiry int64
Description string
2020-11-09 10:05:29 -03:00
Sats int64
2019-10-01 12:22:30 -03:00
}
const lightningScheme = "lightning:"
// ParseInvoice parses an Invoice from an invoice string and a network
2020-11-09 10:05:29 -03:00
func ParseInvoice(rawInput string, network *Network) (*Invoice, error) {
2019-10-01 12:22:30 -03:00
2020-11-09 10:05:29 -03:00
_, components := buildUriFromString(rawInput, lightningScheme)
if components == nil {
return nil, errors.Errorf("failed to parse uri %v", rawInput)
}
if components.Scheme != "lightning" {
return nil, errors.Errorf("invalid scheme %v", components.Scheme)
}
invoice := components.Opaque
// When URIs are scheme:// the address comes in host
// this happens in iOS that mostly ignores scheme: format
if len(invoice) == 0 {
invoice = components.Host
2019-10-01 12:22:30 -03:00
}
parsedInvoice, err := zpay32.Decode(invoice, network.network)
if err != nil {
return nil, errors.Wrapf(err, "Couldnt parse invoice")
}
var fallbackAdd *MuunPaymentURI
if parsedInvoice.FallbackAddr != nil {
fallbackAdd, err = GetPaymentURI(parsedInvoice.FallbackAddr.String(), network)
if err != nil {
return nil, errors.Wrapf(err, "Couldnt get address")
}
}
var description string
if parsedInvoice.Description != nil {
description = *parsedInvoice.Description
}
var milliSats string
2020-11-09 10:05:29 -03:00
var sats int64
2019-10-01 12:22:30 -03:00
if parsedInvoice.MilliSat != nil {
2020-11-09 10:05:29 -03:00
milliSat := uint64(*parsedInvoice.MilliSat)
milliSats = fmt.Sprintf("%v", milliSat)
sats = int64(milliSat / 1000)
2019-10-01 12:22:30 -03:00
}
return &Invoice{
RawInvoice: invoice,
FallbackAddress: fallbackAdd,
Network: network,
MilliSat: milliSats,
Destination: parsedInvoice.Destination.SerializeCompressed(),
2019-12-17 17:32:12 -03:00
PaymentHash: parsedInvoice.PaymentHash[:],
2019-10-01 12:22:30 -03:00
Expiry: parsedInvoice.Timestamp.Unix() + int64(parsedInvoice.Expiry().Seconds()),
Description: description,
2020-11-09 10:05:29 -03:00
Sats: sats,
2019-10-01 12:22:30 -03:00
}, nil
}