mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-06-21 14:22:43 +02:00
nip60: upon receive do not swap into mints unless explicitly asked for.
This commit is contained in:
parent
0330c198fb
commit
41b7261735
@ -47,12 +47,8 @@ func (w *Wallet) Receive(
|
||||
|
||||
source, _ := nostr.NormalizeHTTPURL(mint)
|
||||
destination := rs.intoMint
|
||||
if len(destination) == 0 {
|
||||
destination = w.Mints
|
||||
}
|
||||
|
||||
lightningSwap := slices.Contains(destination, source)
|
||||
swapOpts := make([]SwapOption, 0, 1)
|
||||
swapSettings := swapSettings{}
|
||||
|
||||
for i, proof := range proofs {
|
||||
if proof.Secret != "" {
|
||||
@ -60,7 +56,8 @@ func (w *Wallet) Receive(
|
||||
if err == nil {
|
||||
switch nut10Secret.Kind {
|
||||
case nut10.P2PK:
|
||||
swapOpts = append(swapOpts, WithSignedOutputs())
|
||||
swapSettings.mustSignOutputs = true
|
||||
|
||||
proofs[i].Witness, err = signInput(w.PrivateKey, proof)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sign locked proof %d: %w", i, err)
|
||||
@ -80,7 +77,7 @@ func (w *Wallet) Receive(
|
||||
}
|
||||
|
||||
// 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 {
|
||||
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
|
||||
// and telling the current mint to pay it
|
||||
lightningSwap := slices.Contains(destination, source)
|
||||
if lightningSwap {
|
||||
for _, targetMint := range destination {
|
||||
swappedProofs, err, status := lightningMeltMint(
|
||||
|
@ -68,8 +68,7 @@ func (w *Wallet) Send(ctx context.Context, amount uint64, opts ...SendOption) (c
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
swapOpts := make([]SwapOption, 0, 2)
|
||||
|
||||
swapSettings := swapSettings{}
|
||||
if ss.p2pk != nil {
|
||||
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)
|
||||
@ -85,17 +84,15 @@ func (w *Wallet) Send(ctx context.Context, amount uint64, opts ...SendOption) (c
|
||||
tags.Locktime = ss.refundtimelock
|
||||
}
|
||||
|
||||
swapOpts = append(swapOpts, WithSpendingCondition(
|
||||
nut10.SpendingCondition{
|
||||
Kind: nut10.P2PK,
|
||||
Data: hex.EncodeToString(ss.p2pk.SerializeCompressed()),
|
||||
Tags: nut11.SerializeP2PKTags(tags),
|
||||
},
|
||||
))
|
||||
swapSettings.spendingCondition = &nut10.SpendingCondition{
|
||||
Kind: nut10.P2PK,
|
||||
Data: hex.EncodeToString(ss.p2pk.SerializeCompressed()),
|
||||
Tags: nut11.SerializeP2PKTags(tags),
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return nil, chosen.mint, err
|
||||
}
|
||||
|
@ -13,20 +13,6 @@ import (
|
||||
"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 {
|
||||
spendingCondition *nut10.SpendingCondition
|
||||
mustSignOutputs bool
|
||||
@ -37,13 +23,8 @@ func (w *Wallet) swapProofs(
|
||||
mint string,
|
||||
proofs cashu.Proofs,
|
||||
targetAmount uint64,
|
||||
opts ...SwapOption,
|
||||
ss swapSettings,
|
||||
) (principal cashu.Proofs, change cashu.Proofs, err error) {
|
||||
var ss swapSettings
|
||||
for _, opt := range opts {
|
||||
opt(&ss)
|
||||
}
|
||||
|
||||
keysets, err := client.GetAllKeysets(ctx, mint)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to get all keysets for %s: %w", mint, err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user