From c197e5e901f48e2fb430465d670991fdec3824a7 Mon Sep 17 00:00:00 2001 From: elnosh Date: Tue, 2 Jul 2024 12:58:34 -0500 Subject: [PATCH] tests for nut04 state changes --- mint/mint_integration_test.go | 81 +++++++++++++++++++++++++++++++++++ wallet/wallet.go | 2 + 2 files changed, 83 insertions(+) diff --git a/mint/mint_integration_test.go b/mint/mint_integration_test.go index 84c8b46..ca03799 100644 --- a/mint/mint_integration_test.go +++ b/mint/mint_integration_test.go @@ -13,6 +13,7 @@ import ( btcdocker "github.com/elnosh/btc-docker-test" "github.com/elnosh/gonuts/cashu" + "github.com/elnosh/gonuts/cashu/nuts/nut04" "github.com/elnosh/gonuts/crypto" "github.com/elnosh/gonuts/mint" "github.com/elnosh/gonuts/testutils" @@ -110,6 +111,86 @@ func TestRequestMintQuote(t *testing.T) { } } +func TestMintQuoteState(t *testing.T) { + var mintAmount uint64 = 42000 + mintQuoteResponse, err := testMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT) + if err != nil { + t.Fatalf("error requesting mint quote: %v", err) + } + + var keyset crypto.Keyset + for _, k := range testMint.ActiveKeysets { + keyset = k + break + } + + // test invalid method + _, err = testMint.GetMintQuoteState("strike", mintQuoteResponse.Quote) + if !errors.Is(err, cashu.PaymentMethodNotSupportedErr) { + t.Fatalf("expected error '%v' but got '%v' instead", cashu.PaymentMethodNotSupportedErr, err) + } + + // test invalid quote + _, err = testMint.GetMintQuoteState(testutils.BOLT11_METHOD, "mintquote1234") + if !errors.Is(err, cashu.QuoteNotExistErr) { + t.Fatalf("expected error '%v' but got '%v' instead", cashu.QuoteNotExistErr, err) + } + + // test quote state before paying invoice + quoteStateResponse, err := testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote) + if err != nil { + t.Fatalf("unexpected error getting quote state: %v", err) + } + if quoteStateResponse.Paid { + t.Fatalf("expected quote.Paid '%v' but got '%v' instead", false, quoteStateResponse.Paid) + } + if quoteStateResponse.State != nut04.Unpaid { + t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Unpaid.String(), quoteStateResponse.State.String()) + } + + //pay invoice + sendPaymentRequest := lnrpc.SendRequest{ + PaymentRequest: mintQuoteResponse.Request, + } + response, _ := lnd2.Client.SendPaymentSync(ctx, &sendPaymentRequest) + if len(response.PaymentError) > 0 { + t.Fatalf("error paying invoice: %v", response.PaymentError) + } + + // test quote state after paying invoice + quoteStateResponse, err = testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote) + if err != nil { + t.Fatalf("unexpected error getting quote state: %v", err) + } + if !quoteStateResponse.Paid { + t.Fatalf("expected quote.Paid '%v' but got '%v' instead", true, quoteStateResponse.Paid) + } + if quoteStateResponse.State != nut04.Paid { + t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Paid.String(), quoteStateResponse.State.String()) + } + + blindedMessages, _, _, err := testutils.CreateBlindedMessages(mintAmount, keyset) + + // mint tokens + _, err = testMint.MintTokens(testutils.BOLT11_METHOD, mintQuoteResponse.Quote, blindedMessages) + if err != nil { + t.Fatalf("got unexpected error minting tokens: %v", err) + } + + // test quote state after minting tokens + quoteStateResponse, err = testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote) + if err != nil { + t.Fatalf("unexpected error getting quote state: %v", err) + } + if !quoteStateResponse.Paid { + t.Fatalf("expected quote.Paid '%v' but got '%v' instead", true, quoteStateResponse.Paid) + } + if quoteStateResponse.State != nut04.Issued { + t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Issued.String(), quoteStateResponse.State.String()) + } + +} + func TestMintTokens(t *testing.T) { var mintAmount uint64 = 42000 mintQuoteResponse, err := testMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT) diff --git a/wallet/wallet.go b/wallet/wallet.go index c5db48a..ff4f853 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -325,6 +325,8 @@ func (w *Wallet) MintTokens(quoteId string) (cashu.Proofs, error) { return nil, errors.New("invoice not paid") } + // TODO: do not try mint if state == ISSUED + invoice, err := w.GetInvoiceByPaymentRequest(mintQuote.Request) if err != nil { return nil, err