Class: Bech32::Nostr::BareEntity

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hrp, data) ⇒ BareEntity

Initialize bare entity.

Parameters:

  • hrp (String)
  • data (String)


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

#dataString (readonly)

Returns the value of attribute data.

Returns:

  • (String)


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

def data
  @data
end

#hrpString (readonly)

Returns the value of attribute hrp.

Returns:

  • (String)


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

def hrp
  @hrp
end

Instance Method Details

#encodeString

Encode to bech32 string.

Returns:

  • (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