mirror of
https://github.com/getAlby/hub.git
synced 2026-08-13 12:33:39 +02:00
fix: add validation on MakeInvoice for zero and non-whole satoshi amounts (#2413)
This commit is contained in:
parent
6a15ebadad
commit
64afc2227f
2 changed files with 40 additions and 3 deletions
|
|
@ -26,7 +26,7 @@ func TestMakeInvoice_NoApp(t *testing.T) {
|
|||
txMetadata["randomkey"] = strings.Repeat("a", constants.INVOICE_METADATA_MAX_LENGTH-16) // json encoding adds 16 characters - {"randomkey":""}
|
||||
|
||||
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1234, "Hello world", "", 0, txMetadata, svc.LNClient, nil, nil, nil)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1000, "Hello world", "", 0, txMetadata, svc.LNClient, nil, nil, nil)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var metadata map[string]interface{}
|
||||
|
|
@ -50,13 +50,43 @@ func TestMakeInvoice_MetadataTooLarge(t *testing.T) {
|
|||
metadata["randomkey"] = strings.Repeat("a", constants.INVOICE_METADATA_MAX_LENGTH-15) // json encoding adds 16 characters
|
||||
|
||||
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1234, "Hello world", "", 0, metadata, svc.LNClient, nil, nil, nil)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1000, "Hello world", "", 0, metadata, svc.LNClient, nil, nil, nil)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, fmt.Sprintf("encoded invoice metadata provided is too large. Limit: %d Received: %d", constants.INVOICE_METADATA_MAX_LENGTH, constants.INVOICE_METADATA_MAX_LENGTH+1), err.Error())
|
||||
assert.Nil(t, transaction)
|
||||
}
|
||||
|
||||
func TestMakeInvoice_AmountNotWholeSats(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
svc, err := tests.CreateTestService(t)
|
||||
require.NoError(t, err)
|
||||
defer svc.Remove()
|
||||
|
||||
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1234, "Hello world", "", 0, nil, svc.LNClient, nil, nil, nil)
|
||||
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "the amount must be a whole number of satoshis", err.Error())
|
||||
assert.Nil(t, transaction)
|
||||
}
|
||||
|
||||
func TestMakeInvoice_AmountTooLow(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
svc, err := tests.CreateTestService(t)
|
||||
require.NoError(t, err)
|
||||
defer svc.Remove()
|
||||
|
||||
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 0, "Hello world", "", 0, nil, svc.LNClient, nil, nil, nil)
|
||||
|
||||
require.Error(t, err)
|
||||
assert.Equal(t, "the amount must be at least 1 satoshi", err.Error())
|
||||
assert.Nil(t, transaction)
|
||||
}
|
||||
|
||||
func TestMakeInvoice_App(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
|
||||
|
|
@ -72,7 +102,7 @@ func TestMakeInvoice_App(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
|
||||
transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1234, "Hello world", "", 0, nil, svc.LNClient, &app.ID, &dbRequestEvent.ID, nil)
|
||||
transaction, err := transactionsService.MakeInvoice(ctx, 1000, "Hello world", "", 0, nil, svc.LNClient, &app.ID, &dbRequestEvent.ID, nil)
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, uint64(tests.MockLNClientTransaction.AmountMsat), transaction.AmountMsat)
|
||||
|
|
|
|||
|
|
@ -152,6 +152,13 @@ func (svc *transactionsService) MakeInvoice(ctx context.Context, amountMsat uint
|
|||
"metadata": metadata,
|
||||
}).Debug("Making invoice")
|
||||
|
||||
if amountMsat%1000 != 0 {
|
||||
return nil, errors.New("the amount must be a whole number of satoshis")
|
||||
}
|
||||
if amountMsat < 1000 {
|
||||
return nil, errors.New("the amount must be at least 1 satoshi")
|
||||
}
|
||||
|
||||
var metadataBytes []byte
|
||||
if metadata != nil {
|
||||
var err error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue