Skip to content

Commit

Permalink
nip60: make it work with emitting events to be published dynamically …
Browse files Browse the repository at this point in the history
…and stuff.
  • Loading branch information
fiatjaf committed Jan 28, 2025
1 parent 07b9b3e commit 2244740
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 142 deletions.
35 changes: 28 additions & 7 deletions nip60/eventcodec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,33 @@ func TestWalletRoundtrip(t *testing.T) {
}

// convert wallets to events
events1, err := wallet1.ToPublishableEvents(ctx, kr, false)
require.NoError(t, err)
events := [][]nostr.Event{
make([]nostr.Event, 0, 4),
make([]nostr.Event, 0, 4),
}

events2, err := wallet2.ToPublishableEvents(ctx, kr, false)
require.NoError(t, err)
for i, w := range []*Wallet{&wallet1, &wallet2} {
evt := nostr.Event{}
err := w.toEvent(ctx, kr, &evt)
require.NoError(t, err)
events[i] = append(events[i], evt)

for _, token := range w.Tokens {
evt = nostr.Event{}
err = token.toEvent(ctx, kr, w.Identifier, &evt)
require.NoError(t, err)
events[i] = append(events[i], evt)
}

for _, he := range w.History {
evt = nostr.Event{}
err = he.toEvent(ctx, kr, w.Identifier, &evt)
require.NoError(t, err)
events[i] = append(events[i], evt)
}
}

events1, events2 := events[0], events[1]

// combine all events
allEvents := append(events1, events2...)
Expand All @@ -127,13 +149,12 @@ func TestWalletRoundtrip(t *testing.T) {
}()

// load wallets from events
errorChan := make(chan error)
walletStash := LoadStash(ctx, kr, eventChan, errorChan)
walletStash := loadStash(ctx, kr, eventChan, make(chan struct{}))

var errorChanErr error
go func() {
for {
errorChanErr = <-errorChan
errorChanErr = <-walletStash.Processed
if errorChanErr != nil {
return
}
Expand Down
18 changes: 15 additions & 3 deletions nip60/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,25 @@ func (w *Wallet) ReceiveToken(ctx context.Context, serializedToken string) error
}

saveproofs:
w.tokensMu.Lock()
w.Tokens = append(w.Tokens, Token{
newToken := Token{
Mint: newMint,
Proofs: newProofs,
mintedAt: nostr.Now(),
})
event: &nostr.Event{},
}
if err := newToken.toEvent(ctx, w.wl.kr, w.Identifier, newToken.event); err != nil {
return fmt.Errorf("failed to make new token: %w", err)
}

w.wl.Changes <- *newToken.event

w.tokensMu.Lock()
w.Tokens = append(w.Tokens, newToken)
w.tokensMu.Unlock()

wevt := nostr.Event{}
w.toEvent(ctx, w.wl.kr, &wevt)
w.wl.Changes <- wevt

return nil
}
40 changes: 33 additions & 7 deletions nip60/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,50 @@ found:
}

// delete spent tokens and save our change
newTokens := make([]Token, 0, len(w.Tokens))
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
}
newTokens = append(newTokens, token)
updatedTokens = append(updatedTokens, token)
}
w.Tokens = append(newTokens, Token{
mintedAt: nostr.Now(),
Mint: target.mint,
Proofs: changeProofs,
})

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)
if err != nil {
return "", err
}

wevt := nostr.Event{}
w.toEvent(ctx, w.wl.kr, &wevt)
w.wl.Changes <- wevt

return token.Serialize()
}
Loading

0 comments on commit 2244740

Please sign in to comment.