Class: Bech32::Nostr::TLVEntity
- Inherits:
-
Object
- Object
- Bech32::Nostr::TLVEntity
- 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
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#hrp ⇒ Object
readonly
Returns the value of attribute hrp.
Class Method Summary collapse
-
.parse(hrp, data) ⇒ TLVEntity
Parse TLV entity from data.
Instance Method Summary collapse
-
#encode ⇒ String
Encode tlv entity to bech32 string.
-
#initialize(hrp, entries) ⇒ TLVEntity
constructor
Initialize TLV entity.
Constructor Details
#initialize(hrp, entries) ⇒ TLVEntity
Initialize TLV entity.
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
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
89 90 91 |
# File 'lib/bech32/nostr/entity.rb', line 89 def entries @entries end |
#hrp ⇒ Object (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.
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 |