Merge pull request #650 from wilmerpaulino/invoice-negative-amount
invoice: avoid negative msat amounts
This commit is contained in:
commit
42014f5b7b
@ -127,6 +127,10 @@ func decodeAmount(amount string) (lnwire.MilliSatoshi, error) {
|
|||||||
// encodeAmount encodes the provided millisatoshi amount using as few characters
|
// encodeAmount encodes the provided millisatoshi amount using as few characters
|
||||||
// as possible.
|
// as possible.
|
||||||
func encodeAmount(msat lnwire.MilliSatoshi) (string, error) {
|
func encodeAmount(msat lnwire.MilliSatoshi) (string, error) {
|
||||||
|
if msat < 0 {
|
||||||
|
return "", fmt.Errorf("amount must be positive: %v", msat)
|
||||||
|
}
|
||||||
|
|
||||||
// If possible to express in BTC, that will always be the shortest
|
// If possible to express in BTC, that will always be the shortest
|
||||||
// representation.
|
// representation.
|
||||||
if msat%mSatPerBtc == 0 {
|
if msat%mSatPerBtc == 0 {
|
||||||
|
@ -514,6 +514,11 @@ func validateInvoice(invoice *Invoice) error {
|
|||||||
return fmt.Errorf("net params not set")
|
return fmt.Errorf("net params not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that if there is an amount set, it is not negative.
|
||||||
|
if invoice.MilliSat != nil && *invoice.MilliSat < 0 {
|
||||||
|
return fmt.Errorf("negative amount: %v", *invoice.MilliSat)
|
||||||
|
}
|
||||||
|
|
||||||
// The invoice must contain a payment hash.
|
// The invoice must contain a payment hash.
|
||||||
if invoice.PaymentHash == nil {
|
if invoice.PaymentHash == nil {
|
||||||
return fmt.Errorf("no payment hash found")
|
return fmt.Errorf("no payment hash found")
|
||||||
|
@ -55,6 +55,10 @@ func TestDecodeAmount(t *testing.T) {
|
|||||||
amount: "1109p", // pBTC
|
amount: "1109p", // pBTC
|
||||||
valid: false, // not divisible by 10
|
valid: false, // not divisible by 10
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
amount: "-10p", // pBTC
|
||||||
|
valid: false, // negative amount
|
||||||
|
},
|
||||||
{
|
{
|
||||||
amount: "10p", // pBTC
|
amount: "10p", // pBTC
|
||||||
valid: true,
|
valid: true,
|
||||||
@ -145,6 +149,10 @@ func TestEncodeAmount(t *testing.T) {
|
|||||||
valid bool
|
valid bool
|
||||||
result string
|
result string
|
||||||
}{
|
}{
|
||||||
|
{
|
||||||
|
msat: -10, // mSat
|
||||||
|
valid: false, // negative amount
|
||||||
|
},
|
||||||
{
|
{
|
||||||
msat: 1, // mSat
|
msat: 1, // mSat
|
||||||
valid: true,
|
valid: true,
|
||||||
|
Loading…
Reference in New Issue
Block a user