From 19f07efdeb995593ff84a375e91de0f75f773432 Mon Sep 17 00:00:00 2001 From: Wilson Silva Date: Wed, 15 Feb 2023 18:29:12 +0700 Subject: [PATCH] Add client support for NIP-02 (manage contact lists) --- README.md | 30 +++++++++++++++++++++++++++++- lib/nostr/event_fragment.rb | 4 ++-- lib/nostr/event_kind.rb | 7 +++++++ lib/nostr/user.rb | 2 +- sig/nostr/event_kind.rbs | 2 +- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 591cfe5..3e32cb4 100644 --- a/README.md +++ b/README.md @@ -192,10 +192,38 @@ event = user.create_event( client.publish(event) ``` +### Creating/updating your contact list + +Every new contact list that gets published overwrites the past ones, so it should contain all entries. + +```ruby +# Creating a contact list event with 2 contacts +update_contacts_event = user.create_event( + kind: Nostr::EventKind::CONTACT_LIST, + tags: [ + [ + "p", # mandatory + "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245", # public key of the user to add to the contacts + "wss://alicerelay.com/", # can be an empty string or can be omitted + "alice" # can be an empty string or can be omitted + ], + [ + "p", # mandatory + "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681", # public key of the user to add to the contacts + "wss://bobrelay.com/nostr", # can be an empty string or can be omitted + "bob" # can be an empty string or can be omitted + ], + ], +) + +# Send it to the Relay +client.publish(update_contacts_event) +``` + ## NIPS - [x] [NIP-01 - Client](https://github.com/nostr-protocol/nips/blob/master/01.md) -- [ ] [NIP-01 - Relay](https://github.com/nostr-protocol/nips/blob/master/01.md) +- [x] [NIP-02 - Client](https://github.com/nostr-protocol/nips/blob/master/02.md) ## Development diff --git a/lib/nostr/event_fragment.rb b/lib/nostr/event_fragment.rb index b979778..b266123 100644 --- a/lib/nostr/event_fragment.rb +++ b/lib/nostr/event_fragment.rb @@ -25,7 +25,7 @@ module Nostr # attr_reader :created_at - # The kind of the event. An integer from 0 to 2 + # The kind of the event. An integer from 0 to 3 # # @api public # @@ -76,7 +76,7 @@ module Nostr # # @param pubkey [String] 32-bytes hex-encoded public key of the event creator. # @param created_at [Integer] Date of the creation of the vent. A UNIX timestamp, in seconds. - # @param kind [Integer] The kind of the event. An integer from 0 to 2. + # @param kind [Integer] The kind of the event. An integer from 0 to 3. # @param tags [Array] An array of tags. Each tag is an array of strings. # @param content [String] Arbitrary string. # diff --git a/lib/nostr/event_kind.rb b/lib/nostr/event_kind.rb index 3e25861..5cfadc1 100644 --- a/lib/nostr/event_kind.rb +++ b/lib/nostr/event_kind.rb @@ -24,5 +24,12 @@ module Nostr # @return [Integer] # RECOMMEND_SERVER = 2 + + # A special event with kind 3, meaning "contact list" is defined as having a list of p tags, one for each of + # the followed/known profiles one is following. + # + # @return [Integer] + # + CONTACT_LIST = 3 end end diff --git a/lib/nostr/user.rb b/lib/nostr/user.rb index a14d425..ccc7bee 100644 --- a/lib/nostr/user.rb +++ b/lib/nostr/user.rb @@ -50,7 +50,7 @@ module Nostr # @param event_attributes [Hash] # @option event_attributes [String] :pubkey 32-bytes hex-encoded public key of the event creator. # @option event_attributes [Integer] :created_at Date of the creation of the vent. A UNIX timestamp, in seconds. - # @option event_attributes [Integer] :kind The kind of the event. An integer from 0 to 2. + # @option event_attributes [Integer] :kind The kind of the event. An integer from 0 to 3. # @option event_attributes [Array] :tags An array of tags. Each tag is an array of strings. # @option event_attributes [String] :content Arbitrary string. # diff --git a/sig/nostr/event_kind.rbs b/sig/nostr/event_kind.rbs index ff12241..5dd603e 100644 --- a/sig/nostr/event_kind.rbs +++ b/sig/nostr/event_kind.rbs @@ -3,6 +3,6 @@ module Nostr SET_METADATA: Integer TEXT_NOTE: Integer RECOMMEND_SERVER: Integer - CONTACTS: Integer + CONTACT_LIST: Integer end end