Module: Nostr::Bech32

Defined in:
lib/nostr/bech32.rb

Overview

Bech32 encoding and decoding

Class Method Summary collapse

Class Method Details

.decode(bech32_value) ⇒ Array<String, String>

Decodes a bech32-encoded string

Examples:

bech32_value = 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'
Nostr::Bech32.decode(bech32_value) # => ['npub', '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d8...']

Parameters:

  • bech32_value (String)

    The bech32-encoded string to decode

Returns:

  • (Array<String, String>)

    The human readable part and the data



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

Examples:

Nostr::Bech32.encode(hrp: 'npub', data: '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e')
# => 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'

Parameters:

  • hrp (String)

    The human readable part (npub, nsec, nprofile, nrelay, nevent, naddr, etc)

  • data (String)

    The data to encode

Returns:

  • (String)

    The bech32-encoded 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

Examples:

naddr = Nostr::Bech32.naddr_encode(
  pubkey: '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e',
  relays: ['wss://relay.damus.io', 'wss://nos.lol'],
  kind: Nostr::EventKind::TEXT_NOTE,
  identifier: 'damus'
)
naddr # => 'naddr1qgs8ul5ug253hlh3n75jne0a5xmjur4urfxpzst88cnegg6ds6ka7ns...'

Parameters:

  • pubkey (PublicKey)

    The public key to encode

  • relays (Array<String>) (defaults to: [])

    The relays to encode

  • kind (String) (defaults to: nil)

    The kind of address to encode

  • identifier (String) (defaults to: nil)

    The identifier of the address to encode

Returns:

  • (String)

    The bech32-encoded 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

Examples:

nevent = Nostr::Bech32.nevent_encode(
  id: '0fdb90f8e234d3400edafdd26d493f12efc0d7de2c6f9f21f997847d33ad2ea3',
  relays: ['wss://relay.damus.io', 'wss://nos.lol'],
  kind: Nostr::EventKind::TEXT_NOTE,
)
nevent # => 'nevent1qgsqlkuslr3rf56qpmd0m5ndfyl39m7q6l0zcmuly8ue0pra...'

Parameters:

  • id (PublicKey)

    The id the event to encode

  • relays (Array<String>) (defaults to: [])

    The relays to encode

  • kind (String) (defaults to: nil)

    The kind of event to encode

Returns:

  • (String)

    The bech32-encoded 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

Examples:

nprofile = Nostr::Bech32.nprofile_encode(
  pubkey: '7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e',
  relays: ['wss://relay.damus.io', 'wss://nos.lol']
)

Parameters:

  • pubkey (PublicKey)

    The public key to encode

  • relays (Array<String>) (defaults to: [])

    The relays to encode

Returns:

  • (String)

    The bech32-encoded 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

Examples:

Nostr::Bech32.npub_encode('7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e')
# => 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'

Parameters:

  • npub (String)

    The public key to encode

Returns:

  • (String)

    The bech32-encoded string

See Also:

  • #encode
  • PublicKey#to_bech32
  • PrivateKey#to_bech32


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

Examples:

nrelay = Nostr::Bech32.nrelay_encode('wss://relay.damus.io')
nrelay # => 'nrelay1qq28wumn8ghj7un9d3shjtnyv9kh2uewd9hsc5zt2x'

Parameters:

  • relay_url (String)

    The relay url to encode

Returns:

  • (String)

    The bech32-encoded 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

Examples:

Nostr::Bech32.nsec_encode('7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e')
# => 'nsec10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'

Parameters:

  • nsec (String)

    The private key to encode

Returns:

  • (String)

    The bech32-encoded string

See Also:

  • #encode
  • PrivateKey#to_bech32
  • PublicKey#to_bech32


89
90
91
# File 'lib/nostr/bech32.rb', line 89

def self.nsec_encode(nsec)
  encode(hrp: 'nsec', data: nsec)
end