Class: Bech32::Nostr::TLVEntity

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

Constant Summary collapse

TYPE_SPECIAL =
0
TYPE_RELAY =
1
TYPE_AUTHOR =
2
TYPE_KIND =
3
TYPES =
[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)

    human-readable part.

  • entries (Array<TLVEntry>)

    TLV entries.

Raises:

  • (ArgumentError)


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

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

#hrpObject (readonly)

Returns the value of attribute hrp.



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 data.

Parameters:

  • hrp (String)

    human-readable part.

  • data (String)

    Entity data(binary format).

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
# File 'lib/bech32/nostr/entity.rb', line 109

def self.parse(hrp, data)
  buf = ::StringIO.new(data)
  entries = []
  until buf.eof?
    type, len = buf.read(2).unpack('CC')
    case type
    when TYPE_SPECIAL # special
      case hrp
      when NIP19::HRP_PROFILE
        entries << TLVEntry.new(type, buf.read(len).unpack1('H*'), 'pubkey')
      when NIP19::HRP_RELAY
        entries << TLVEntry.new(type, buf.read(len), 'relay')
      when NIP19::HRP_EVENT
        entries << TLVEntry.new(type, buf.read(len).unpack1('H*'), 'id')
      when NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, buf.read(len), 'identifier')
      end
    when TYPE_RELAY # relay
      case hrp
      when NIP19::HRP_PROFILE, NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
        entries << TLVEntry.new(type, buf.read(len), '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, buf.read(len).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, buf.read(len).unpack1('H*').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 tlv entity to bech32 string.

Returns:

  • (String)

    bech32 string.



157
158
159
160
# File 'lib/bech32/nostr/entity.rb', line 157

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