Class: Bech32::Nostr::TLVEntity

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

Constant Summary collapse

TYPE_SPECIAL =

Returns:

  • (Integer)
0
TYPE_RELAY =

Returns:

  • (Integer)
1
TYPE_AUTHOR =

Returns:

  • (Integer)
2
TYPE_KIND =

Returns:

  • (Integer)
3
TYPES =

Returns:

  • (Array[Integer])
[TYPE_SPECIAL, TYPE_RELAY, TYPE_AUTHOR, TYPE_KIND]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hrp, entries) ⇒ TLVEntity

Initialize TLV entity.

Parameters:

  • hrp (String)
  • entries (Array[TLVEntry])


95
96
97
98
99
100
101
102
103
# File 'lib/bech32/nostr/entity.rb', line 95

def initialize(hrp, entries)
  raise ArgumentError, "HRP #{hrp} is unsupported." unless NIP19::TLV_PREFIXES.include?(hrp)
  entries.each do |e|
    raise ArgumentError, "Entries must be TLVEntry. #{e.class} given." unless e.is_a?(TLVEntry)
  end

  @hrp = hrp
  @entries = entries
end

Instance Attribute Details

#entriesArray[TLVEntry] (readonly)

Returns the value of attribute entries.

Returns:



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

def entries
  @entries
end

#hrpString (readonly)

Returns the value of attribute hrp.

Returns:

  • (String)


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

def hrp
  @hrp
end

Class Method Details

.parse(hrp, data) ⇒ TLVEntity

Parse TLV entity from binary data.

Parameters:

  • hrp (String)
  • data (String)

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bech32/nostr/entity.rb', line 109

def self.parse(hrp, data)
  buf = ::StringIO.new(data)
  entries = []
  until buf.eof?
    header = buf.read(2)
    raise ArgumentError, "Invalid TLV: truncated header." if header.nil? || header.bytesize < 2
    type, len = header.unpack('CC')
    value = buf.read(len)
    raise ArgumentError, "Invalid TLV: truncated value." if value.nil? ? len > 0 : value.bytesize < len
    value ||= ''
    case type
    when TYPE_SPECIAL # special
      case hrp
      when NIP19::HRP_PROFILE
        entries << TLVEntry.new(type, value.unpack1('H*'), 'pubkey')
      when NIP19::HRP_RELAY
        entries << TLVEntry.new(type, value, 'relay')
      when NIP19::HRP_EVENT
        entries << TLVEntry.new(type, value.unpack1('H*'), 'id')
      when NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, value, 'identifier')
      end
    when TYPE_RELAY # relay
      case hrp
      when NIP19::HRP_PROFILE, NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, value, 'relay')
      else
        raise ArgumentError, "Type: #{type} does not supported for HRP: #{hrp}"
      end
    when TYPE_AUTHOR # author
      case hrp
      when NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, value.unpack1('H*'), 'author')
      else
        raise ArgumentError, "Type: #{type} does not supported for HRP: #{hrp}"
      end
    when TYPE_KIND # kind
      case hrp
      when NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, value.unpack1('H*').to_s.to_i(16), 'kind')
      else
        raise ArgumentError, "Type: #{type} does not supported for HRP: #{hrp}"
      end
    else
      raise ArgumentError, "Unknown TLV type: #{type}"
    end
  end

  TLVEntity.new(hrp, entries)
end

Instance Method Details

#encodeString

Encode to bech32 string.

Returns:

  • (String)


162
163
164
165
# File 'lib/bech32/nostr/entity.rb', line 162

def encode
  data = entries.map(&:to_payload).join
  Bech32.encode(hrp, Bech32.convert_bits(data.unpack('C*'), 8, 5), Bech32::Encoding::BECH32)
end