Class: Bech32::Nostr::BareEntity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hrp, data) ⇒ BareEntity

Initialize bare entity.

Parameters:

  • hrp (String)

    human-readable part.

  • data (String)

    Entity data(hex string).

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/bech32/nostr/entity.rb', line 10

def initialize(hrp, data)
  raise ArgumentError, "HRP #{hrp} is unsupported." unless NIP19::BARE_PREFIXES.include?(hrp)
  raise ArgumentError, "Data whose HRP is #{hrp} must be 32 bytes." unless [data].pack('H*').bytesize == 32
  @hrp = hrp
  @data = data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/bech32/nostr/entity.rb', line 5

def data
  @data
end

#hrpObject (readonly)

Returns the value of attribute hrp.



4
5
6
# File 'lib/bech32/nostr/entity.rb', line 4

def hrp
  @hrp
end

Instance Method Details

#encodeString

Encode bare entity to bech32 string.

Returns:

  • (String)

    bech32 string.



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

def encode
  Bech32.encode(hrp, Bech32.convert_bits([data].pack('H*').unpack('C*'), 8, 5), Bech32::Encoding::BECH32)
end