Skip to content

Commit

Permalink
NUT11 tests for melting and use mint from wallet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Aug 14, 2024
1 parent ccee4d2 commit 71bf544
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 4 deletions.
51 changes: 51 additions & 0 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,4 +801,55 @@ func TestNUT11P2PK(t *testing.T) {
t.Fatalf("unexpected error in swap: %v", err)
}

// get locked proofs for melting
lockedProofs, err = testutils.GetProofsWithLock(mintAmount, lock.PubKey(), nut11.P2PKTags{}, p2pkMint, lnd2)
if err != nil {
t.Fatalf("error getting locked proofs: %v", err)
}

invoice := lnrpc.Invoice{Value: 500}
addInvoiceResponse, err := lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}
meltQuote, err := p2pkMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}

_, err = p2pkMint.MeltTokens(testutils.BOLT11_METHOD, meltQuote.Id, lockedProofs)
if !errors.Is(err, nut11.InvalidWitness) {
t.Fatalf("expected error '%v' but got '%v' instead", nut11.InvalidWitness, err)
}

signedProofs, _ = testutils.AddSignaturesToInputs(lockedProofs, []*btcec.PrivateKey{lock})
_, err = p2pkMint.MeltTokens(testutils.BOLT11_METHOD, meltQuote.Id, lockedProofs)
if err != nil {
t.Fatalf("unexpected error melting: %v", err)
}

// test melt with SIG_ALL fails
tags = nut11.P2PKTags{
Sigflag: nut11.SIGALL,
}
lockedProofs, err = testutils.GetProofsWithLock(mintAmount, lock.PubKey(), tags, p2pkMint, lnd2)
if err != nil {
t.Fatalf("error getting locked proofs: %v", err)
}
signedProofs, _ = testutils.AddSignaturesToInputs(lockedProofs, []*btcec.PrivateKey{lock})

invoice = lnrpc.Invoice{Value: 500}
addInvoiceResponse, err = lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}
meltQuote, err = p2pkMint.RequestMeltQuote(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}
_, err = p2pkMint.MeltTokens(testutils.BOLT11_METHOD, meltQuote.Id, lockedProofs)
if !errors.Is(err, nut11.SigAllOnlySwap) {
t.Fatalf("expected error '%v' but got '%v' instead", nut11.SigAllOnlySwap, err)
}

}
75 changes: 71 additions & 4 deletions wallet/wallet_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,57 @@ func TestWalletBalanceFees(t *testing.T) {
t.Fatalf("expected balance of '%v' but got '%v' instead", expectedBalance, balanceTestWallet2.GetBalance())
}
}

}

func TestSendToPubkey(t *testing.T) {
p2pkMintPath := filepath.Join(".", "p2pkmint1")
p2pkMint, err := testutils.CreateTestMintServer(lnd1, "8889", p2pkMintPath, dbMigrationPath, 0)
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(p2pkMintPath)
}()
go func() {
t.Fatal(p2pkMint.Start())
}()
p2pkMintURL := "http://127.0.0.1:8889"

p2pkMintPath2 := filepath.Join(".", "p2pkmint2")
p2pkMint2, err := testutils.CreateTestMintServer(lnd2, "8890", p2pkMintPath, dbMigrationPath, 0)
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(p2pkMintPath2)
}()
go func() {
t.Fatal(p2pkMint2.Start())
}()
p2pkMintURL2 := "http://127.0.0.1:8890"

testWalletPath := filepath.Join(".", "/testwalletp2pk")
testWallet, err := testutils.CreateTestWallet(testWalletPath, p2pkMintURL)
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(testWalletPath)
}()

testWalletPath2 := filepath.Join(".", "/testwalletp2pk2")
testWallet2, err := testutils.CreateTestWallet(testWalletPath2, p2pkMintURL2)
if err != nil {
t.Fatal(err)
}
defer func() {
os.RemoveAll(testWalletPath2)
}()

testP2PK(t, testWallet, testWallet2, false)
}

func TestSendToPubkeyNutshell(t *testing.T) {
nutshellMint, err := testutils.CreateNutshellMintContainer(ctx)
if err != nil {
t.Fatalf("error starting nutshell mint: %v", err)
Expand Down Expand Up @@ -615,18 +662,38 @@ func TestSendToPubkey(t *testing.T) {
os.RemoveAll(testWalletPath2)
}()

testP2PK(t, testWallet, testWallet2, true)
}

func testP2PK(
t *testing.T,
testWallet *wallet.Wallet,
testWallet2 *wallet.Wallet,
fakeBackend bool,
) {
mintRequest, err := testWallet.RequestMint(20000)
if err != nil {
t.Fatalf("unexpected error in mint request: %v", err)
}

if !fakeBackend {
//pay invoice
sendPaymentRequest := lnrpc.SendRequest{
PaymentRequest: mintRequest.Request,
}
response, _ := lnd2.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
t.Fatalf("error paying invoice: %v", response.PaymentError)
}
}

_, err = testWallet.MintTokens(mintRequest.Quote)
if err != nil {
t.Fatalf("unexpected error in mint tokens: %v", err)
}

receiverPubkey := testWallet2.GetReceivePubkey()

lockedEcash, err := testWallet.SendToPubkey(500, nutshellURL, receiverPubkey, true)
lockedEcash, err := testWallet.SendToPubkey(500, testWallet.CurrentMint(), receiverPubkey, true)
if err != nil {
t.Fatalf("unexpected error generating locked ecash: %v", err)
}
Expand All @@ -653,7 +720,7 @@ func TestSendToPubkey(t *testing.T) {
t.Fatalf("expected balance of '%v' but got '%v' instead", amountReceived, balance)
}

lockedEcash, err = testWallet.SendToPubkey(500, nutshellURL, receiverPubkey, true)
lockedEcash, err = testWallet.SendToPubkey(500, testWallet.CurrentMint(), receiverPubkey, true)
if err != nil {
t.Fatalf("unexpected error generating locked ecash: %v", err)
}
Expand Down

0 comments on commit 71bf544

Please sign in to comment.