Skip to content

Commit

Permalink
Extract Nip27 mentions from content (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero committed Jan 29, 2024
1 parent 922237f commit 162593e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
13 changes: 5 additions & 8 deletions Nostra.Tests/Bech32.fs → Nostra.Tests/Bech32Tests.fs
Original file line number Diff line number Diff line change
@@ -1,11 1,8 @@
module Bech32Tests

open System
open System.Text
open System.Security.Cryptography
open NBitcoin.Secp256k1
open Nostra
open Nostra.Shareable
open Xunit
open Xunit.Abstractions
open FsUnit.Xunit
Expand All @@ -14,8 11,8 @@ type ``Nip19 Bech32-Shareable entities``(output:ITestOutputHelper) =

let encodeDecode entity =
entity
|> encode
|> decode
|> Shareable.encode
|> Shareable.decode
|> Option.get

[<Fact>]
Expand Down Expand Up @@ -58,13 55,13 @@ type ``Nip19 Bech32-Shareable entities``(output:ITestOutputHelper) =
"wss://djbas.sadkb.com"
])

let encodedNprofile = encode nprofile
let encodedNprofile = Shareable.encode nprofile
should equal "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" encodedNprofile

let reconcodedNprofile =
encodedNprofile
|> decode
|> Option.get |> encode
|> Shareable.decode
|> Option.get |> Shareable.encode
should equal "nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p" reconcodedNprofile

[<Fact>]
Expand Down
23 changes: 23 additions & 0 deletions Nostra.Tests/ContentTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
module ContentTests

open Xunit
open Xunit.Abstractions
open FsUnit.Xunit
open Nostra

type ``Nip27 Mentions``(output:ITestOutputHelper) =

[<Fact>]
let ``Parse npub reference`` () =
"Hello nostr:npub1nccwjspr3nv7h67xx2qhdh2dzzvpyy55gte2dsu8yl7xd7n74y9qydz7mj !"
|> Content.extractNip27Mentions
|> should equal [("p", ["9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a"])]

[<Fact>]
let ``Parse multiple npub references`` () =
"Hello nostr:npub1nccwjspr3nv7h67xx2qhdh2dzzvpyy55gte2dsu8yl7xd7n74y9qydz7mj and nostr:npub14zln9kg0yx7qdn2kx8p2z9zdrz3ujfhyx6adeepmjsl98gjlfj5sr6fcn4!"
|> Content.extractNip27Mentions
|> should equal [
("p", ["9e30e940238cd9ebebc6328176dd4d109812129442f2a6c38727fc66fa7ea90a"])
("p", ["a8bf32d90f21bc06cd5631c2a1144d18a3c926e436badce43b943e53a25f4ca9"])
]
3 changes: 2 additions & 1 deletion Nostra.Tests/Nostra.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 13,10 @@
<Compile Include="RelaySimpleTests.fs" />
<Compile Include="RelayIntegrationTests.fs" />
<Compile Include="ClientTests.fs" />
<Compile Include="Bech32.fs" />
<Compile Include="Bech32Tests.fs" />
<Compile Include="PayloadEncryptionTests.fs" />
<Compile Include="QueryBuildingTests.fs" />
<Compile Include="ContentTests.fs" />
<Content Include="TestData\nip44.vectors.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
20 changes: 11 additions & 9 deletions Nostra/Bech32.fs
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 110,22 @@ module Bech32 =
None)
|> Option.map (fun data -> (hrp, toBase256 data |> List.toArray))


type RelayT = string
type ShareableEntity =
| NSec of SecretKey
| NPub of AuthorId
| Note of EventId
| NProfile of AuthorId * RelayT list
| NEvent of EventId * RelayT list * AuthorId option * Kind option
| NRelay of RelayT

[<RequireQualifiedAccess>]
module Shareable =
open System.Text
open Microsoft.FSharp.Collections
open NBitcoin.Secp256k1

type Relay = string
type ShareableEntity =
| NSec of SecretKey
| NPub of AuthorId
| Note of EventId
| NProfile of AuthorId * Relay list
| NEvent of EventId * Relay list * AuthorId option * Kind option
| NRelay of Relay

let private _encode hrp bytesArr =
Bech32.encode hrp (bytesArr |> Array.toList)

Expand Down
23 changes: 23 additions & 0 deletions Nostra/Content.fs
Original file line number Diff line number Diff line change
@@ -0,0 1,23 @@
namespace Nostra

module Content =
let (|Nip27Mention|_|) = Regex.matches @"\bnostr:((?:note|npub|naddr|nevent|nprofile)1\w )\b"

let extractNip27Mentions content =
let rec parseMentions content (mentions : Tag list) =
match content with
| Nip27Mention (mention, endPos) ->
Shareable.decode mention
|> Option.map (function
| NPub authorId -> Tag.authorTag authorId
| Note eventId -> Tag.eventRefTag eventId
| NProfile(AuthorId authorId, relays) -> Tag("p", (Utils.toHex (authorId.ToBytes()))::relays)
| NEvent(EventId eventId, relays, _, _) -> Tag("e", (Utils.toHex eventId)::relays)
| NRelay relay -> Tag.relayTag [relay]
| NSec _ -> failwith "Are you crazy!?" )
|> function
| Some mention -> parseMentions content[endPos..] (mention::mentions)
| None -> parseMentions content[endPos..] mentions
| _ -> mentions
parseMentions content []
|> List.rev
1 change: 1 addition & 0 deletions Nostra/Nostra.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 14,7 @@
<Compile Include="Event.fs" />
<Compile Include="EncryptedPayload.fs" />
<Compile Include="Bech32.fs" />
<Compile Include="Content.fs" />
<Compile Include="Client.fs" />
<Compile Include="Relay.fs" />
</ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Nostra/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 67,16 @@ module Result =
| Choice1Of2 v -> Ok v
| Choice2Of2 e -> Error e

[<RequireQualifiedAccess>]
module Regex =
open System.Text.RegularExpressions
let matches pattern input =
let m = Regex.Match(input, pattern, RegexOptions.Multiline ||| RegexOptions.Compiled ||| RegexOptions.CultureInvariant)
if m.Success then
Some(m.Groups[1].Value, m.Index m.Length)
else
None

module Monad =
type Reader<'environment, 'a> = Reader of ('environment -> 'a)

Expand Down

0 comments on commit 162593e

Please sign in to comment.