nip60: make it work with emitting events to be published dynamically and stuff.

This commit is contained in:
fiatjaf
2025-01-28 19:11:18 -03:00
parent 07b9b3e439
commit 2244740f61
6 changed files with 304 additions and 202 deletions

View File

@@ -130,18 +130,40 @@ found:
}
// delete spent tokens and save our change
newTokens := make([]Token, 0, len(w.Tokens))
for i, token := range w.Tokens {
if slices.Contains(target.tokenIndexes, i) {
continue
}
newTokens = append(newTokens, token)
}
w.Tokens = append(newTokens, Token{
updatedTokens := make([]Token, 0, len(w.Tokens))
changeToken := Token{
mintedAt: nostr.Now(),
Mint: target.mint,
Proofs: changeProofs,
})
Deleted: make([]string, 0, len(target.tokenIndexes)),
event: &nostr.Event{},
}
for i, token := range w.Tokens {
if slices.Contains(target.tokenIndexes, i) {
if token.event != nil {
token.Deleted = append(token.Deleted, token.event.ID)
deleteEvent := nostr.Event{
CreatedAt: nostr.Now(),
Kind: 5,
Tags: nostr.Tags{{"e", token.event.ID}},
}
w.wl.kr.SignEvent(ctx, &deleteEvent)
w.wl.Changes <- deleteEvent
}
continue
}
updatedTokens = append(updatedTokens, token)
}
if err := changeToken.toEvent(ctx, w.wl.kr, w.Identifier, changeToken.event); err != nil {
return "", fmt.Errorf("failed to make change token: %w", err)
}
w.wl.Changes <- *changeToken.event
w.Tokens = append(updatedTokens, changeToken)
// serialize token we're sending out
token, err := cashu.NewTokenV4(proofsToSend, target.mint, cashu.Sat, true)
@@ -149,5 +171,9 @@ found:
return "", err
}
wevt := nostr.Event{}
w.toEvent(ctx, w.wl.kr, &wevt)
w.wl.Changes <- wevt
return token.Serialize()
}