From 392013fa25028d8244a65f8506731dcadd1281b5 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 27 Feb 2023 16:45:45 -0300 Subject: [PATCH] sdk package with higher-level functions. --- helpers.go | 1 - sdk/input.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) delete mode 100644 helpers.go create mode 100644 sdk/input.go diff --git a/helpers.go b/helpers.go deleted file mode 100644 index b8f6fcf..0000000 --- a/helpers.go +++ /dev/null @@ -1 +0,0 @@ -package nostr diff --git a/sdk/input.go b/sdk/input.go new file mode 100644 index 0000000..3dddebf --- /dev/null +++ b/sdk/input.go @@ -0,0 +1,62 @@ +package sdk + +import ( + "encoding/hex" + + "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip05" + "github.com/nbd-wtf/go-nostr/nip19" +) + +// InputToProfile turns any npub/nprofile/hex/nip05 input into a ProfilePointer (or nil) +func InputToProfile(input string) *nostr.ProfilePointer { + // handle if it is a hex string + if len(input) == 64 { + if _, err := hex.DecodeString(input); err == nil { + return &nostr.ProfilePointer{PublicKey: input} + } + } + + // handle nip19 codes, if that's the case + prefix, data, _ := nip19.Decode(input) + switch prefix { + case "npub": + input = data.(string) + return &nostr.ProfilePointer{PublicKey: input} + case "nprofile": + pp := data.(nostr.ProfilePointer) + return &pp + } + + // handle nip05 ids, if that's the case + pp := nip05.QueryIdentifier(input) + if pp != nil { + return pp + } + + return nil +} + +// InputToEventPointer turns any note/nevent/hex input into a EventPointer (or nil) +func InputToEventPointer(input string) *nostr.EventPointer { + // handle if it is a hex string + if len(input) == 64 { + if _, err := hex.DecodeString(input); err == nil { + return &nostr.EventPointer{ID: input} + } + } + + // handle nip19 codes, if that's the case + prefix, data, _ := nip19.Decode(input) + switch prefix { + case "note": + input = data.(string) + return &nostr.EventPointer{ID: input} + case "nevent": + ep := data.(nostr.EventPointer) + return &ep + } + + // handle nip05 ids, if that's the case + return nil +}