From a6350c8e80d5bd8ec0d6f7ebfe4aa37fcec9b47b Mon Sep 17 00:00:00 2001 From: mleku Date: Thu, 7 Aug 2025 18:31:42 +0100 Subject: [PATCH] Refactor GetWalletServiceInfo and update event and notification types - pkg/protocol/nwc/methods.go - Refactored `GetWalletServiceInfo` to improve context and error handling. - Simplified tag extraction and processing for encryption and notification types. - Optimized handling of WalletServiceInfo capabilities. - pkg/protocol/nwc/types.go - Added `HoldInvoiceAccepted` notification type. - Introduced `NotificationTag` constant. - pkg/encoders/kind/kind.go - Renamed `NWCWalletInfo` to `NWCWalletServiceInfo`. - Updated references and mappings to reflect the rename. --- pkg/encoders/kind/kind.go | 8 +++--- pkg/protocol/nwc/methods.go | 55 ++++++++++++++++--------------------- pkg/protocol/nwc/types.go | 6 ++-- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/pkg/encoders/kind/kind.go b/pkg/encoders/kind/kind.go index ea45843..1717531 100644 --- a/pkg/encoders/kind/kind.go +++ b/pkg/encoders/kind/kind.go @@ -263,9 +263,9 @@ var ( FileStorageServerList = &T{10096} // JWTBinding is an event kind that creates a link between a JWT certificate and a pubkey JWTBinding = &T{13004} - // NWCWalletInfo is an event type that... - NWCWalletInfo = &T{13194} - WalletInfo = NWCWalletInfo + // NWCWalletServiceInfo is an event type that... + NWCWalletServiceInfo = &T{13194} + WalletServiceInfo = &T{13194} // ReplaceableEnd is an event type that... ReplaceableEnd = &T{19999} // EphemeralStart is an event type that... @@ -384,7 +384,7 @@ var Map = map[uint16]string{ UserEmojiList.K: "UserEmojiList", DMRelaysList.K: "DMRelaysList", FileStorageServerList.K: "FileStorageServerList", - NWCWalletInfo.K: "NWCWalletInfo", + NWCWalletServiceInfo.K: "NWCWalletServiceInfo", LightningPubRPC.K: "LightningPubRPC", ClientAuthentication.K: "ClientAuthentication", WalletRequest.K: "WalletRequest", diff --git a/pkg/protocol/nwc/methods.go b/pkg/protocol/nwc/methods.go index 5ef2905..5a1d0cb 100644 --- a/pkg/protocol/nwc/methods.go +++ b/pkg/protocol/nwc/methods.go @@ -19,22 +19,18 @@ import ( func (cl *Client) GetWalletServiceInfo(c context.T, noUnmarshal bool) ( wsi *WalletServiceInfo, raw []byte, err error, ) { - timeout := 10 * time.Second - ctx, cancel := context.Timeout(c, timeout) + ctx, cancel := context.Timeout(c, 10*time.Second) defer cancel() var rc *ws.Client if rc, err = ws.RelayConnect(c, cl.relay); chk.E(err) { return } - if err = rc.Connect(c); chk.E(err) { - return - } var sub *ws.Subscription if sub, err = rc.Subscribe( ctx, filters.New( &filter.F{ Limit: values.ToUintPointer(1), - Kinds: kinds.New(kind.WalletRequest), + Kinds: kinds.New(kind.WalletServiceInfo), Authors: tag.New(cl.walletPublicKey), }, ), @@ -43,37 +39,32 @@ func (cl *Client) GetWalletServiceInfo(c context.T, noUnmarshal bool) ( } defer sub.Unsub() select { - case <-c.Done(): - err = fmt.Errorf("GetWalletServiceInfo canceled") + case <-ctx.Done(): + err = fmt.Errorf("context canceled") return - case ev := <-sub.Events: - var encryptionTypes []EncryptionType - var notificationTypes []NotificationType - encryptionTag := ev.Tags.GetFirst(tag.New("encryption")) - notificationsTag := ev.Tags.GetFirst(tag.New("notifications")) - if encryptionTag != nil { - et := encryptionTag.ToSliceOfBytes() - encType := bytes.Split(et[0], []byte(" ")) - for _, e := range encType { - encryptionTypes = append(encryptionTypes, e) + case e := <-sub.Events: + raw = e.Marshal(nil) + if noUnmarshal { + return + } + wsi = &WalletServiceInfo{} + encTag := e.Tags.GetFirst(tag.New(EncryptionTag)) + notTag := e.Tags.GetFirst(tag.New(NotificationTag)) + if encTag != nil { + et := bytes.Split(encTag.Value(), []byte(" ")) + for _, v := range et { + wsi.EncryptionTypes = append(wsi.EncryptionTypes, v) } } - if notificationsTag != nil { - nt := notificationsTag.ToSliceOfBytes() - notifs := bytes.Split(nt[0], []byte(" ")) - for _, e := range notifs { - notificationTypes = append(notificationTypes, e) + if notTag != nil { + nt := bytes.Split(notTag.Value(), []byte(" ")) + for _, v := range nt { + wsi.NotificationTypes = append(wsi.NotificationTypes, v) } } - cp := bytes.Split(ev.Content, []byte(" ")) - var capabilities []Capability - for _, capability := range cp { - capabilities = append(capabilities, capability) - } - wsi = &WalletServiceInfo{ - EncryptionTypes: encryptionTypes, - NotificationTypes: notificationTypes, - Capabilities: capabilities, + caps := bytes.Split(e.Content, []byte(" ")) + for _, v := range caps { + wsi.Capabilities = append(wsi.Capabilities, v) } } return diff --git a/pkg/protocol/nwc/types.go b/pkg/protocol/nwc/types.go index 5dbb943..621666a 100644 --- a/pkg/protocol/nwc/types.go +++ b/pkg/protocol/nwc/types.go @@ -33,8 +33,10 @@ var ( type NotificationType []byte var ( - PaymentReceived = NotificationType("payment_received") - PaymentSent = NotificationType("payment_sent") + NotificationTag = []byte("notification") + PaymentReceived = NotificationType("payment_received") + PaymentSent = NotificationType("payment_sent") + HoldInvoiceAccepted = NotificationType("hold_invoice_accepted") ) type WalletServiceInfo struct {