nip60: upon receive do not swap into mints unless explicitly asked for.

This commit is contained in:
fiatjaf
2025-02-05 16:33:10 -03:00
parent 0330c198fb
commit 41b7261735
3 changed files with 13 additions and 37 deletions

View File

@@ -47,12 +47,8 @@ func (w *Wallet) Receive(
source, _ := nostr.NormalizeHTTPURL(mint) source, _ := nostr.NormalizeHTTPURL(mint)
destination := rs.intoMint destination := rs.intoMint
if len(destination) == 0 {
destination = w.Mints
}
lightningSwap := slices.Contains(destination, source) swapSettings := swapSettings{}
swapOpts := make([]SwapOption, 0, 1)
for i, proof := range proofs { for i, proof := range proofs {
if proof.Secret != "" { if proof.Secret != "" {
@@ -60,7 +56,8 @@ func (w *Wallet) Receive(
if err == nil { if err == nil {
switch nut10Secret.Kind { switch nut10Secret.Kind {
case nut10.P2PK: case nut10.P2PK:
swapOpts = append(swapOpts, WithSignedOutputs()) swapSettings.mustSignOutputs = true
proofs[i].Witness, err = signInput(w.PrivateKey, proof) proofs[i].Witness, err = signInput(w.PrivateKey, proof)
if err != nil { if err != nil {
return fmt.Errorf("failed to sign locked proof %d: %w", i, err) return fmt.Errorf("failed to sign locked proof %d: %w", i, err)
@@ -80,7 +77,7 @@ func (w *Wallet) Receive(
} }
// get new proofs // get new proofs
newProofs, _, err := w.swapProofs(ctx, source, proofs, proofs.Amount(), swapOpts...) newProofs, _, err := w.swapProofs(ctx, source, proofs, proofs.Amount(), swapSettings)
if err != nil { if err != nil {
return err return err
} }
@@ -89,6 +86,7 @@ func (w *Wallet) Receive(
// if we have to swap to our own mint we do it now by getting a bolt11 invoice from our mint // if we have to swap to our own mint we do it now by getting a bolt11 invoice from our mint
// and telling the current mint to pay it // and telling the current mint to pay it
lightningSwap := slices.Contains(destination, source)
if lightningSwap { if lightningSwap {
for _, targetMint := range destination { for _, targetMint := range destination {
swappedProofs, err, status := lightningMeltMint( swappedProofs, err, status := lightningMeltMint(

View File

@@ -68,8 +68,7 @@ func (w *Wallet) Send(ctx context.Context, amount uint64, opts ...SendOption) (c
return nil, "", err return nil, "", err
} }
swapOpts := make([]SwapOption, 0, 2) swapSettings := swapSettings{}
if ss.p2pk != nil { if ss.p2pk != nil {
if info, err := client.GetMintInfo(ctx, chosen.mint); err != nil || !info.Nuts.Nut11.Supported { if info, err := client.GetMintInfo(ctx, chosen.mint); err != nil || !info.Nuts.Nut11.Supported {
return nil, chosen.mint, fmt.Errorf("mint doesn't support p2pk: %w", err) return nil, chosen.mint, fmt.Errorf("mint doesn't support p2pk: %w", err)
@@ -85,17 +84,15 @@ func (w *Wallet) Send(ctx context.Context, amount uint64, opts ...SendOption) (c
tags.Locktime = ss.refundtimelock tags.Locktime = ss.refundtimelock
} }
swapOpts = append(swapOpts, WithSpendingCondition( swapSettings.spendingCondition = &nut10.SpendingCondition{
nut10.SpendingCondition{ Kind: nut10.P2PK,
Kind: nut10.P2PK, Data: hex.EncodeToString(ss.p2pk.SerializeCompressed()),
Data: hex.EncodeToString(ss.p2pk.SerializeCompressed()), Tags: nut11.SerializeP2PKTags(tags),
Tags: nut11.SerializeP2PKTags(tags), }
},
))
} }
// get new proofs // get new proofs
proofsToSend, changeProofs, err := w.swapProofs(ctx, chosen.mint, chosen.proofs, amount, swapOpts...) proofsToSend, changeProofs, err := w.swapProofs(ctx, chosen.mint, chosen.proofs, amount, swapSettings)
if err != nil { if err != nil {
return nil, chosen.mint, err return nil, chosen.mint, err
} }

View File

@@ -13,20 +13,6 @@ import (
"github.com/nbd-wtf/go-nostr/nip60/client" "github.com/nbd-wtf/go-nostr/nip60/client"
) )
type SwapOption func(*swapSettings)
func WithSignedOutputs() SwapOption {
return func(ss *swapSettings) {
ss.mustSignOutputs = true
}
}
func WithSpendingCondition(sc nut10.SpendingCondition) SwapOption {
return func(ss *swapSettings) {
ss.spendingCondition = &sc
}
}
type swapSettings struct { type swapSettings struct {
spendingCondition *nut10.SpendingCondition spendingCondition *nut10.SpendingCondition
mustSignOutputs bool mustSignOutputs bool
@@ -37,13 +23,8 @@ func (w *Wallet) swapProofs(
mint string, mint string,
proofs cashu.Proofs, proofs cashu.Proofs,
targetAmount uint64, targetAmount uint64,
opts ...SwapOption, ss swapSettings,
) (principal cashu.Proofs, change cashu.Proofs, err error) { ) (principal cashu.Proofs, change cashu.Proofs, err error) {
var ss swapSettings
for _, opt := range opts {
opt(&ss)
}
keysets, err := client.GetAllKeysets(ctx, mint) keysets, err := client.GetAllKeysets(ctx, mint)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("failed to get all keysets for %s: %w", mint, err) return nil, nil, fmt.Errorf("failed to get all keysets for %s: %w", mint, err)