Class: Nostr::Bech32

Inherits:
Object
  • Object
show all
Defined in:
lib/bech32.rb

Class Method Summary collapse

Class Method Details

.decode(bech32_entity) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/bech32.rb', line 8

def self.decode(bech32_entity)
  e = ::Bech32::Nostr::NIP19.decode(bech32_entity)

  case e
  in ::Bech32::Nostr::BareEntity
    { hrp: e.hrp, data: e.data }
  in ::Bech32::Nostr::TLVEntity
    { hrp: e.hrp, data: transform_entries(e.entries) }
  end
end

.encode(hrp, data) ⇒ Object



19
20
21
# File 'lib/bech32.rb', line 19

def self.encode(hrp, data)
  ::Bech32::Nostr::BareEntity.new(hrp, data).encode
end

.encode_naddr(author:, relays: [], kind: nil, identifier: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bech32.rb', line 57

def self.encode_naddr(author:, relays: [], kind: nil, identifier: nil)
  entry_relays = relays.map do |r|
    ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_RELAY, r)
  end

   = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_AUTHOR, author)
  entry_kind = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_KIND, kind)
  entry_identifier = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_SPECIAL, identifier)

  entries = [, *entry_relays, entry_kind, entry_identifier].compact
  ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_EVENT_COORDINATE, entries).encode
end

.encode_nevent(id:, relays: [], kind: nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/bech32.rb', line 45

def self.encode_nevent(id:, relays: [], kind: nil)
  entry_relays = relays.map do |r|
    ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_RELAY, r)
  end

  entry_id = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_SPECIAL, id)
  entry_kind = kind ? ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_KIND, kind) : nil

  entries = [entry_id, *entry_relays, entry_kind].compact
  ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_EVENT, entries).encode
end

.encode_note(data) ⇒ Object



41
42
43
# File 'lib/bech32.rb', line 41

def self.encode_note(data)
  encode("note", data)
end

.encode_nprofile(pubkey:, relays: []) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/bech32.rb', line 31

def self.encode_nprofile(pubkey:, relays: [])
  entry_relays = relays.map do |relay_url|
    ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_RELAY, relay_url)
  end

  pubkey_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_SPECIAL, pubkey)
  entries = [pubkey_entry, *entry_relays].compact
  ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_PROFILE, entries).encode
end

.encode_npub(data) ⇒ Object



23
24
25
# File 'lib/bech32.rb', line 23

def self.encode_npub(data)
  encode("npub", data)
end

.encode_nsec(data) ⇒ Object



27
28
29
# File 'lib/bech32.rb', line 27

def self.encode_nsec(data)
  encode("nsec", data)
end