Skip to content

Commit

Permalink
mint - test NUT-09 signature restore
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Sep 13, 2024
1 parent d6b91a3 commit 1c943c3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
4 changes: 2 additions & 2 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,9 +898,9 @@ func (m *Mint) SetMintInfo(mintInfo MintInfo) error {
},
Disabled: false,
},
7: map[string]bool{"supported": false},
7: map[string]bool{"supported": true},
8: map[string]bool{"supported": false},
9: map[string]bool{"supported": false},
9: map[string]bool{"supported": true},
10: map[string]bool{"supported": true},
11: map[string]bool{"supported": true},
12: map[string]bool{"supported": false},
Expand Down
54 changes: 53 additions & 1 deletion mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -621,6 +622,58 @@ func TestProofsStateCheck(t *testing.T) {
}
}

func TestRestoreSignatures(t *testing.T) {
// create blinded messages
blindedMessages, _, _, blindedSignatures, err := testutils.GetBlindedSignatures(5000, testMint, lnd2)
if err != nil {
t.Fatalf("error generating blinded signatures: %v", err)
}

outputs, signatures, err := testMint.RestoreSignatures(blindedMessages)
if err != nil {
t.Fatalf("unexpected error restoring signatures: %v\n", err)
}

if len(outputs) != len(signatures) {
t.Fatalf("length of ouputs '%v' does not match length of signatures '%v'\n", len(outputs), len(signatures))
}
if !reflect.DeepEqual(blindedMessages, outputs) {
t.Fatal("outputs in request do not match outputs from mint response")
}
if !reflect.DeepEqual(blindedSignatures, signatures) {
t.Fatal("blinded signatures do not match signatures from mint response")
}

// test with blinded messages that have not been previously signed
unsigned, _, _, _ := testutils.CreateBlindedMessages(4200, testMint.GetActiveKeyset())
outputs, signatures, err = testMint.RestoreSignatures(unsigned)
if err != nil {
t.Fatalf("unexpected error restoring signatures: %v\n", err)
}

// response should be empty
if len(outputs) != 0 && len(signatures) != 0 {
t.Fatalf("expected empty outputs and signatures but got %v and %v\n", len(outputs), len(signatures))
}

// test with only a portion of blinded messages signed
partial := append(blindedMessages, unsigned...)
outputs, signatures, err = testMint.RestoreSignatures(partial)
if err != nil {
t.Fatalf("unexpected error restoring signatures: %v\n", err)
}

if len(outputs) != len(signatures) {
t.Fatalf("length of ouputs '%v' does not match length of signatures '%v'\n", len(outputs), len(signatures))
}
if !reflect.DeepEqual(blindedMessages, outputs) {
t.Fatal("outputs in request do not match outputs from mint response")
}
if !reflect.DeepEqual(blindedSignatures, signatures) {
t.Fatal("blinded signatures do not match signatures from mint response")
}
}

func TestMintLimits(t *testing.T) {
// setup mint with limits
limitsMintPath := filepath.Join(".", "limitsMint")
Expand Down Expand Up @@ -905,5 +958,4 @@ func TestNUT11P2PK(t *testing.T) {
if !errors.Is(err, nut11.SigAllOnlySwap) {
t.Fatalf("expected error '%v' but got '%v' instead", nut11.SigAllOnlySwap, err)
}

}
27 changes: 21 additions & 6 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,22 @@ func ConstructProofs(blindedSignatures cashu.BlindedSignatures,
return proofs, nil
}

func GetValidProofsForAmount(amount uint64, mint *mint.Mint, payer *btcdocker.Lnd) (cashu.Proofs, error) {
func GetBlindedSignatures(amount uint64, mint *mint.Mint, payer *btcdocker.Lnd) (
cashu.BlindedMessages,
[]string,
[]*secp256k1.PrivateKey,
cashu.BlindedSignatures,
error) {

mintQuoteResponse, err := mint.RequestMintQuote(BOLT11_METHOD, amount, SAT_UNIT)
if err != nil {
return nil, fmt.Errorf("error requesting mint quote: %v", err)
return nil, nil, nil, nil, fmt.Errorf("error requesting mint quote: %v", err)
}

keyset := mint.GetActiveKeyset()

blindedMessages, secrets, rs, err := CreateBlindedMessages(amount, keyset)
if err != nil {
return nil, fmt.Errorf("error creating blinded message: %v", err)
return nil, nil, nil, nil, fmt.Errorf("error creating blinded message: %v", err)
}

ctx := context.Background()
Expand All @@ -431,12 +436,22 @@ func GetValidProofsForAmount(amount uint64, mint *mint.Mint, payer *btcdocker.Ln
}
response, _ := payer.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
return nil, fmt.Errorf("error paying invoice: %v", response.PaymentError)
return nil, nil, nil, nil, fmt.Errorf("error paying invoice: %v", response.PaymentError)
}

blindedSignatures, err := mint.MintTokens(BOLT11_METHOD, mintQuoteResponse.Id, blindedMessages)
if err != nil {
return nil, fmt.Errorf("got unexpected error minting tokens: %v", err)
return nil, nil, nil, nil, fmt.Errorf("got unexpected error minting tokens: %v", err)
}

return blindedMessages, secrets, rs, blindedSignatures, nil
}

func GetValidProofsForAmount(amount uint64, mint *mint.Mint, payer *btcdocker.Lnd) (cashu.Proofs, error) {
keyset := mint.GetActiveKeyset()
_, secrets, rs, blindedSignatures, err := GetBlindedSignatures(amount, mint, payer)
if err != nil {
return nil, fmt.Errorf("error generating blinded signatures: %v", err)
}

proofs, err := ConstructProofs(blindedSignatures, secrets, rs, &keyset)
Expand Down

0 comments on commit 1c943c3

Please sign in to comment.