Skip to content

Commit ea61a99

Browse files
committed
profiles working with nostr basics
1 parent 37ae62f commit ea61a99

File tree

7 files changed

+114
-83
lines changed

7 files changed

+114
-83
lines changed

lib/examples/nostr_app/server.ex

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule NostrApp.Server do
1212
alias NostrBasics.Keys.{PrivateKey, PublicKey}
1313

1414
alias Nostr.Client
15-
alias Nostr.Event.Types.{MetadataEvent}
1615
alias Nostr.Models.{Profile}
1716

1817
@impl true
@@ -278,13 +277,6 @@ defmodule NostrApp.Server do
278277
{:noreply, socket}
279278
end
280279

281-
@impl true
282-
def handle_info({relay, %MetadataEvent{} = event}, socket) do
283-
Logger.info("From #{relay}, got a profile: #{inspect(event)}")
284-
285-
{:noreply, socket}
286-
end
287-
288280
@impl true
289281
def handle_info({relay, event}, socket) do
290282
# credo:disable-for-next-line

lib/nostr/client/workflows/update_profile.ex

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ defmodule Nostr.Client.Workflows.UpdateProfile do
88

99
require Logger
1010

11+
alias NostrBasics.Event
1112
alias NostrBasics.Event.{Signer, Validator}
1213
alias NostrBasics.Keys.PublicKey
1314

1415
alias Nostr.Client.Relays.RelaySocket
1516
alias Nostr.Models.Profile
16-
alias Nostr.Event.Types.{MetadataEvent}
1717

1818
def start_link(relay_pids, %Profile{} = new_profile, privkey) do
1919
GenServer.start(__MODULE__, %{
@@ -27,40 +27,44 @@ defmodule Nostr.Client.Workflows.UpdateProfile do
2727
def init(%{new_profile: new_profile} = state) do
2828
send(self(), {:update, new_profile})
2929

30-
{
31-
:ok,
32-
state
33-
|> Map.put(:treated, false)
34-
}
30+
{:ok, state}
3531
end
3632

3733
@impl true
3834
def handle_info(
3935
{:update, new_profile},
40-
%{treated: false, privkey: privkey, relay_pids: relay_pids} = state
36+
%{privkey: privkey, relay_pids: relay_pids} = state
4137
) do
4238
update_profile(new_profile, privkey, relay_pids)
4339

44-
{
45-
:noreply,
46-
state
47-
|> Map.put(:treated, true)
48-
}
40+
{:noreply, state}
4941
end
5042

51-
defp update_profile(%Profile{} = new_profile, privkey, relay_pids) do
52-
pubkey = PublicKey.from_private_key!(privkey)
43+
defp update_profile(%Profile{} = new_profile, private_key, relay_pids) do
44+
with {:ok, pubkey} <- PublicKey.from_private_key(private_key),
45+
{:ok, profile_event} <- create_profile_event(new_profile, pubkey),
46+
{:ok, signed_event} <- prepare_and_sign_event(profile_event, private_key) do
47+
:ok = Validator.validate_event(signed_event)
5348

54-
with {:ok, event} <- MetadataEvent.create_event(new_profile, pubkey),
55-
{:ok, signed_event} <- Signer.sign_event(event, privkey) do
56-
Validator.validate_event(signed_event)
5749
send_event(signed_event, relay_pids)
50+
51+
:ok
5852
else
59-
{:error, message} ->
60-
Logger.warning(message)
53+
{:error, message} -> {:error, message}
6154
end
6255
end
6356

57+
defp create_profile_event(%Profile{} = profile, pubkey) do
58+
profile
59+
|> Profile.to_event(pubkey)
60+
end
61+
62+
defp prepare_and_sign_event(event, private_key) do
63+
%Event{event | created_at: DateTime.utc_now()}
64+
|> Event.add_id()
65+
|> Signer.sign_event(private_key)
66+
end
67+
6468
defp send_event(validated_event, relay_pids) do
6569
for relay_pid <- relay_pids do
6670
RelaySocket.send_event(relay_pid, validated_event)

lib/nostr/event/types/metadata_event.ex

Lines changed: 0 additions & 56 deletions
This file was deleted.

lib/nostr/models/profile.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,34 @@ defmodule Nostr.Models.Profile do
2222
|> Jason.Encode.map(opts)
2323
end
2424
end
25+
26+
@doc """
27+
Creates a new nostr profile
28+
29+
## Examples
30+
iex> %Nostr.Models.Profile{
31+
...> about: "some user description",
32+
...> banner: "https://image.com/satoshi_banner",
33+
...> display_name: "satoshi nakamoto",
34+
...> lud16: "satoshi@nakamoto.jp",
35+
...> name: "satoshi nakamoto",
36+
...> nip05: "_@nakamoto.jp",
37+
...> picture: "https://image.com/satoshi_avatar",
38+
...> website: "https://bitcoin.org"
39+
...> }
40+
...> |> Nostr.Models.Profile.to_event(<<0x5ab9f2efb1fda6bc32696f6f3fd715e156346175b93b6382099d23627693c3f2::256>>)
41+
{
42+
:ok,
43+
%NostrBasics.Event{
44+
pubkey: <<0x5ab9f2efb1fda6bc32696f6f3fd715e156346175b93b6382099d23627693c3f2::256>>,
45+
kind: 0,
46+
tags: [],
47+
content: ~s({"about":"some user description","banner":"https://image.com/satoshi_banner","display_name":"satoshi nakamoto","lud16":"satoshi@nakamoto.jp","name":"satoshi nakamoto","nip05":"_@nakamoto.jp","picture":"https://image.com/satoshi_avatar","website":"https://bitcoin.org"})
48+
}
49+
}
50+
"""
51+
@spec to_event(Profile.t(), PublicKey.t()) :: {:ok, Event.t()} | {:error, String.t()}
52+
def to_event(profile, pubkey) do
53+
Profile.Convert.to_event(profile, pubkey)
54+
end
2555
end
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
defmodule Nostr.Models.Profile.Convert do
2+
alias NostrBasics.Keys.PublicKey
3+
alias NostrBasics.Event
4+
5+
alias Nostr.Models.Profile
6+
7+
@reaction_kind 0
8+
9+
@doc """
10+
Creates a new nostr profile
11+
12+
## Examples
13+
iex> %Nostr.Models.Profile{
14+
...> about: "some user description",
15+
...> banner: "https://image.com/satoshi_banner",
16+
...> display_name: "satoshi nakamoto",
17+
...> lud16: "satoshi@nakamoto.jp",
18+
...> name: "satoshi nakamoto",
19+
...> nip05: "_@nakamoto.jp",
20+
...> picture: "https://image.com/satoshi_avatar",
21+
...> website: "https://bitcoin.org"
22+
...> }
23+
...> |> Nostr.Models.Profile.Convert.to_event(<<0x5ab9f2efb1fda6bc32696f6f3fd715e156346175b93b6382099d23627693c3f2::256>>)
24+
{
25+
:ok,
26+
%NostrBasics.Event{
27+
pubkey: <<0x5ab9f2efb1fda6bc32696f6f3fd715e156346175b93b6382099d23627693c3f2::256>>,
28+
kind: 0,
29+
tags: [],
30+
content: ~s({"about":"some user description","banner":"https://image.com/satoshi_banner","display_name":"satoshi nakamoto","lud16":"satoshi@nakamoto.jp","name":"satoshi nakamoto","nip05":"_@nakamoto.jp","picture":"https://image.com/satoshi_avatar","website":"https://bitcoin.org"})
31+
}
32+
}
33+
"""
34+
@spec to_event(Profile.t(), PublicKey.t()) :: {:ok, Event.t()} | {:error, String.t()}
35+
def to_event(%Profile{} = profile, pubkey) do
36+
case Jason.encode(profile) do
37+
{:ok, json_profile} ->
38+
{
39+
:ok,
40+
Event.create(@reaction_kind, json_profile, pubkey)
41+
}
42+
43+
{:error, message} ->
44+
{:error, message}
45+
end
46+
end
47+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule Nostr.Models.Profile.ConvertTest do
2+
use ExUnit.Case, async: true
3+
4+
alias Nostr.Models.Profile.Convert
5+
6+
doctest Convert
7+
end

test/nostr/models/profile_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule Nostr.Models.ProfileTest do
2+
use ExUnit.Case, async: true
3+
4+
alias Nostr.Models.Profile
5+
6+
doctest Profile
7+
end

0 commit comments

Comments
 (0)