mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-03 16:32:10 +02:00
nip60: emit history.
This commit is contained in:
20
nip60/pay.go
20
nip60/pay.go
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/elnosh/gonuts/cashu"
|
"github.com/elnosh/gonuts/cashu"
|
||||||
"github.com/elnosh/gonuts/cashu/nuts/nut05"
|
"github.com/elnosh/gonuts/cashu/nuts/nut05"
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/nbd-wtf/go-nostr/nip60/client"
|
"github.com/nbd-wtf/go-nostr/nip60/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -117,14 +118,29 @@ inspectmeltstatusresponse:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// the invoice has been paid, now we save the change we got
|
// the invoice has been paid, now we save the change we got
|
||||||
change, err := constructProofs(preChange, meltStatus.Change, ksKeys)
|
changeProofs, err := constructProofs(preChange, meltStatus.Change, ksKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("failed to construct principal proofs: %w", err)
|
return "", fmt.Errorf("failed to construct principal proofs: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := w.saveChangeAndDeleteUsedTokens(ctx, chosen.mint, change, chosen.tokenIndexes); err != nil {
|
he := HistoryEntry{
|
||||||
|
event: &nostr.Event{},
|
||||||
|
tokenEventIDs: make([]string, 0, 1),
|
||||||
|
nutZaps: make([]bool, 0, 1),
|
||||||
|
createdAt: nostr.Now(),
|
||||||
|
In: false,
|
||||||
|
Amount: chosen.proofs.Amount() - changeProofs.Amount(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.saveChangeAndDeleteUsedTokens(ctx, chosen.mint, changeProofs, chosen.tokenIndexes, &he); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.wl.Lock()
|
||||||
|
if err := he.toEvent(ctx, w.wl.kr, w.Identifier, he.event); err == nil {
|
||||||
|
w.wl.PublishUpdate(*he.event, nil, nil, nil, true)
|
||||||
|
}
|
||||||
|
w.wl.Unlock()
|
||||||
|
|
||||||
return meltStatus.Preimage, nil
|
return meltStatus.Preimage, nil
|
||||||
}
|
}
|
||||||
|
@@ -106,8 +106,20 @@ saveproofs:
|
|||||||
return fmt.Errorf("failed to make new token: %w", err)
|
return fmt.Errorf("failed to make new token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
he := HistoryEntry{
|
||||||
|
event: &nostr.Event{},
|
||||||
|
tokenEventIDs: []string{newToken.event.ID},
|
||||||
|
nutZaps: []bool{false},
|
||||||
|
createdAt: nostr.Now(),
|
||||||
|
In: true,
|
||||||
|
Amount: newToken.Proofs.Amount(),
|
||||||
|
}
|
||||||
|
|
||||||
w.wl.Lock()
|
w.wl.Lock()
|
||||||
w.wl.PublishUpdate(*newToken.event, nil, &newToken, nil, false)
|
w.wl.PublishUpdate(*newToken.event, nil, &newToken, nil, false)
|
||||||
|
if err := he.toEvent(ctx, w.wl.kr, w.Identifier, he.event); err == nil {
|
||||||
|
w.wl.PublishUpdate(*he.event, nil, nil, nil, true)
|
||||||
|
}
|
||||||
w.wl.Unlock()
|
w.wl.Unlock()
|
||||||
|
|
||||||
w.tokensMu.Lock()
|
w.tokensMu.Lock()
|
||||||
|
@@ -100,10 +100,25 @@ func (w *Wallet) SendToken(ctx context.Context, amount uint64, opts ...SendOptio
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := w.saveChangeAndDeleteUsedTokens(ctx, chosen.mint, changeProofs, chosen.tokenIndexes); err != nil {
|
he := HistoryEntry{
|
||||||
|
event: &nostr.Event{},
|
||||||
|
tokenEventIDs: make([]string, 0, 1),
|
||||||
|
nutZaps: make([]bool, 0, 1),
|
||||||
|
createdAt: nostr.Now(),
|
||||||
|
In: false,
|
||||||
|
Amount: chosen.proofs.Amount() - changeProofs.Amount(),
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := w.saveChangeAndDeleteUsedTokens(ctx, chosen.mint, changeProofs, chosen.tokenIndexes, &he); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.wl.Lock()
|
||||||
|
if err := he.toEvent(ctx, w.wl.kr, w.Identifier, he.event); err == nil {
|
||||||
|
w.wl.PublishUpdate(*he.event, nil, nil, nil, true)
|
||||||
|
}
|
||||||
|
w.wl.Unlock()
|
||||||
|
|
||||||
// serialize token we're sending out
|
// serialize token we're sending out
|
||||||
token, err := cashu.NewTokenV4(proofsToSend, chosen.mint, cashu.Sat, true)
|
token, err := cashu.NewTokenV4(proofsToSend, chosen.mint, cashu.Sat, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -118,6 +133,7 @@ func (w *Wallet) saveChangeAndDeleteUsedTokens(
|
|||||||
mintURL string,
|
mintURL string,
|
||||||
changeProofs cashu.Proofs,
|
changeProofs cashu.Proofs,
|
||||||
usedTokenIndexes []int,
|
usedTokenIndexes []int,
|
||||||
|
he *HistoryEntry,
|
||||||
) error {
|
) error {
|
||||||
// delete spent tokens and save our change
|
// delete spent tokens and save our change
|
||||||
updatedTokens := make([]Token, 0, len(w.Tokens))
|
updatedTokens := make([]Token, 0, len(w.Tokens))
|
||||||
@@ -161,6 +177,10 @@ func (w *Wallet) saveChangeAndDeleteUsedTokens(
|
|||||||
|
|
||||||
// we don't have to lock tokensMu here because this function will always be called with that lock already held
|
// we don't have to lock tokensMu here because this function will always be called with that lock already held
|
||||||
w.Tokens = append(updatedTokens, changeToken)
|
w.Tokens = append(updatedTokens, changeToken)
|
||||||
|
|
||||||
|
// fill in the history created token
|
||||||
|
he.tokenEventIDs = append(he.tokenEventIDs, changeToken.event.ID)
|
||||||
|
he.nutZaps = append(he.nutZaps, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user