Module: Nostr::Bech32
- Defined in:
- lib/nostr/bech32.rb
Overview
Bech32 encoding and decoding
Class Method Summary collapse
-
.decode(bech32_value) ⇒ Array<String, String>
Decodes a bech32-encoded string.
-
.encode(hrp:, data:) ⇒ String
Encodes data into a bech32 string.
-
.naddr_encode(pubkey:, relays: [], kind: nil, identifier: nil) ⇒ String
Encodes an address into a bech32 string.
-
.nevent_encode(id:, relays: [], kind: nil) ⇒ String
Encodes an event into a bech32 string.
-
.nprofile_encode(pubkey:, relays: []) ⇒ String
Encodes a profile into a bech32 string.
-
.npub_encode(npub) ⇒ String
Encodes a hex-encoded public key into a bech32 string.
-
.nrelay_encode(relay_url) ⇒ String
Encodes a relay URL into a bech32 string.
-
.nsec_encode(nsec) ⇒ String
Encodes a hex-encoded private key into a bech32 string.
Class Method Details
.decode(bech32_value) ⇒ Array<String, String>
Decodes a bech32-encoded string
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/nostr/bech32.rb', line 25 def self.decode(bech32_value) entity = ::Bech32::Nostr::NIP19.decode(bech32_value) case entity in ::Bech32::Nostr::BareEntity [entity.hrp, entity.data] in ::Bech32::Nostr::TLVEntity [entity.hrp, entity.entries] end end |
.encode(hrp:, data:) ⇒ String
Encodes data into a bech32 string
49 50 51 |
# File 'lib/nostr/bech32.rb', line 49 def self.encode(hrp:, data:) ::Bech32::Nostr::BareEntity.new(hrp, data).encode end |
.naddr_encode(pubkey:, relays: [], kind: nil, identifier: nil) ⇒ String
Encodes an address into a bech32 string
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/nostr/bech32.rb', line 113 def self.naddr_encode(pubkey:, relays: [], kind: nil, identifier: nil) 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_AUTHOR, pubkey) kind_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_KIND, kind) identifier_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_SPECIAL, identifier) entries = [pubkey_entry, *entry_relays, kind_entry, identifier_entry].compact entity = ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_EVENT_COORDINATE, entries) entity.encode end |
.nevent_encode(id:, relays: [], kind: nil) ⇒ String
Encodes an event into a bech32 string
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/nostr/bech32.rb', line 145 def self.nevent_encode(id:, relays: [], kind: nil) entry_relays = relays.map do |relay_url| ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_RELAY, relay_url) end id_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_AUTHOR, id) kind_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_KIND, kind) entries = [id_entry, *entry_relays, kind_entry].compact entity = ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_EVENT, entries) entity.encode end |
.nprofile_encode(pubkey:, relays: []) ⇒ String
Encodes a profile into a bech32 string
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/nostr/bech32.rb', line 173 def self.nprofile_encode(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 entity = ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_PROFILE, entries) entity.encode end |
.npub_encode(npub) ⇒ String
Encodes a hex-encoded public key into a bech32 string
69 70 71 |
# File 'lib/nostr/bech32.rb', line 69 def self.npub_encode(npub) encode(hrp: 'npub', data: npub) end |
.nrelay_encode(relay_url) ⇒ String
Encodes a relay URL into a bech32 string
196 197 198 199 200 201 |
# File 'lib/nostr/bech32.rb', line 196 def self.nrelay_encode(relay_url) relay_entry = ::Bech32::Nostr::TLVEntry.new(::Bech32::Nostr::TLVEntity::TYPE_SPECIAL, relay_url) entity = ::Bech32::Nostr::TLVEntity.new(::Bech32::Nostr::NIP19::HRP_RELAY, [relay_entry]) entity.encode end |
.nsec_encode(nsec) ⇒ String
Encodes a hex-encoded private key into a bech32 string
89 90 91 |
# File 'lib/nostr/bech32.rb', line 89 def self.nsec_encode(nsec) encode(hrp: 'nsec', data: nsec) end |