From 2b9c50a220c7e2f454c327d4a89082f2aebdd446 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Thu, 30 Jan 2025 16:08:21 -0300 Subject: [PATCH] nip60: EnsureWallet() publishes an update if a wallet wasn't found. --- nip60/stash.go | 12 ++++++++++-- nip60/wallet_test.go | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/nip60/stash.go b/nip60/stash.go index 583c113..cc7a76d 100644 --- a/nip60/stash.go +++ b/nip60/stash.go @@ -278,7 +278,7 @@ func loadStash( return wl } -func (wl *WalletStash) EnsureWallet(id string) *Wallet { +func (wl *WalletStash) EnsureWallet(ctx context.Context, id string) *Wallet { wl.Lock() defer wl.Unlock() if w, ok := wl.wallets[id]; ok { @@ -287,7 +287,7 @@ func (wl *WalletStash) EnsureWallet(id string) *Wallet { sk, err := btcec.NewPrivateKey() if err != nil { - panic(err) + return nil } w := &Wallet{ @@ -297,6 +297,14 @@ func (wl *WalletStash) EnsureWallet(id string) *Wallet { wl: wl, } wl.wallets[id] = w + + event := nostr.Event{} + if err := w.toEvent(ctx, wl.kr, &event); err != nil { + return nil + } + + wl.PublishUpdate(event, nil, nil, nil, false) + return w } diff --git a/nip60/wallet_test.go b/nip60/wallet_test.go index a0e2fd8..5c2e667 100644 --- a/nip60/wallet_test.go +++ b/nip60/wallet_test.go @@ -71,10 +71,10 @@ func TestWalletTransfer(t *testing.T) { } // ensure wallets exist and have tokens - w1 := stash1.EnsureWallet("test") + w1 := stash1.EnsureWallet(ctx, "test") require.Greater(t, w1.Balance(), uint64(0), "wallet 1 has no balance") - w2 := stash2.EnsureWallet("test") + w2 := stash2.EnsureWallet(ctx, "test") initialBalance1 := w1.Balance() initialBalance2 := w2.Balance()