bech32 1.6.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/exe/bech32 +38 -33
- data/lib/bech32/nostr/entity.rb +113 -45
- data/lib/bech32/nostr/nip19.rb +7 -1
- data/lib/bech32/segwit_addr.rb +1 -0
- data/lib/bech32/silent_payment_addr.rb +41 -5
- data/lib/bech32/version.rb +1 -1
- data/lib/bech32.rb +13 -0
- data/sig/bech32/bech32.rbs +4 -0
- data/sig/bech32/nostr/entity.rbs +13 -0
- data/sig/bech32/nostr/nip19.rbs +1 -0
- data/sig/bech32/silent_payment_addr.rbs +8 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 385b10727c1105f233c1c387c5b03aa6f12b9e2c131261cc99bc865f95df0eb1
|
|
4
|
+
data.tar.gz: c629dcae33f0edddba292c6a7a9803ef5d7fa4f366ebda3c69b1c2b08316040f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 84ec7efd44d61918d538193c7cedaf2049a7bb4c0a7f724fc11a1230b0d957b04ab56ee73ee06fd7ce14b64ea0f069be0472f62c1393ba21eb5e33f05b0cb921
|
|
7
|
+
data.tar.gz: 999a0096238621ad5ff3e889ae12b8e77aec1fbb740e26cc04e799af02979e008451e30a7cc4422f2f3992c4254bc26f80c373bfde7b4224e9d5388e6a95e21d
|
data/README.md
CHANGED
|
@@ -157,11 +157,12 @@ entity.encode
|
|
|
157
157
|
=> 'npub10elfcs4fr0l0r8af98jlmgdh9c8tcxjvz9qkw038js35mp4dma8qzvjptg'
|
|
158
158
|
|
|
159
159
|
# Encode tlv entity
|
|
160
|
+
entry_id = Bech32::Nostr::TLVEntry.new(Bech32::Nostr::TLVEntity::TYPE_SPECIAL, 'b9f5441e45ca39179320e0031cfb18e34078673dcc3d3e3a3b3a981760aa5696')
|
|
160
161
|
entry_relay = Bech32::Nostr::TLVEntry.new(Bech32::Nostr::TLVEntity::TYPE_RELAY, 'wss://relay.nostr.example')
|
|
161
162
|
entry_author = Bech32::Nostr::TLVEntry.new(Bech32::Nostr::TLVEntity::TYPE_AUTHOR, '97c70a44366a6535c145b333f973ea86dfdc2d7a99da618c40c64705ad98e322')
|
|
162
|
-
entity = Bech32::Nostr::TLVEntity.new(Bech32::Nostr::NIP19::HRP_EVENT, [entry_relay, entry_author])
|
|
163
|
+
entity = Bech32::Nostr::TLVEntity.new(Bech32::Nostr::NIP19::HRP_EVENT, [entry_id, entry_relay, entry_author])
|
|
163
164
|
entity.encode
|
|
164
|
-
=> '
|
|
165
|
+
=> 'nevent1qqstna2yrezu5wghjvswqqculvvwxsrcvu7uc0f78gan4xqhvz49d9spr9mhxue69uhhyetvv9ujumn0wd68ytn90psk6urvv5pzp978pfzrv6n9xhq5tvenl9e74pklmskh4xw6vxxyp3j8qkke3cez9uxngd'
|
|
165
166
|
```
|
|
166
167
|
|
|
167
168
|
### CLI
|
data/exe/bech32
CHANGED
|
@@ -5,6 +5,11 @@ require 'thor'
|
|
|
5
5
|
|
|
6
6
|
class CLI < Thor
|
|
7
7
|
|
|
8
|
+
# Exit with a non-zero status when Thor fails on arguments (e.g. missing or unknown command).
|
|
9
|
+
def self.exit_on_failure?
|
|
10
|
+
true
|
|
11
|
+
end
|
|
12
|
+
|
|
8
13
|
desc 'encode hrp data bech32/bech32m', 'Encoding data to bech32/bech32m string.'
|
|
9
14
|
def encode(hrp, data, type)
|
|
10
15
|
unless ['bech32', 'bech32m'].include?(type)
|
|
@@ -12,6 +17,7 @@ class CLI < Thor
|
|
|
12
17
|
exit 1
|
|
13
18
|
end
|
|
14
19
|
spec = type == 'bech32' ? Bech32::Encoding::BECH32 : Bech32::Encoding::BECH32M
|
|
20
|
+
raise ArgumentError, 'data must be a hex string.' unless Bech32.hex_string?(data)
|
|
15
21
|
data = [data].pack('H*').unpack('C*')
|
|
16
22
|
puts Bech32.encode(hrp, data, spec)
|
|
17
23
|
rescue ArgumentError => e
|
|
@@ -22,43 +28,42 @@ class CLI < Thor
|
|
|
22
28
|
desc 'decode "bech32string"', 'Decoding bech32/bech32m string.'
|
|
23
29
|
def decode(bech32_str)
|
|
24
30
|
hrp, data, spec = Bech32.decode(bech32_str, bech32_str.length)
|
|
25
|
-
|
|
26
|
-
puts
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
unless hrp
|
|
32
|
+
puts 'Can not decode.'
|
|
33
|
+
exit 1
|
|
34
|
+
end
|
|
35
|
+
# Build all the output first, so that nothing is printed if parsing the address or entity fails.
|
|
36
|
+
output = <<~EOS
|
|
37
|
+
HRP: #{hrp}
|
|
38
|
+
DATA: #{data.pack('C*').unpack1('H*')}
|
|
39
|
+
TYPE: #{spec == Bech32::Encoding::BECH32 ? 'bech32' : 'bech32m'}
|
|
40
|
+
EOS
|
|
41
|
+
case hrp
|
|
42
|
+
when Bech32::SegwitAddr::HRP_MAINNET, Bech32::SegwitAddr::HRP_TESTNET, Bech32::SegwitAddr::HRP_REGTEST
|
|
43
|
+
addr = Bech32::SegwitAddr.new(bech32_str)
|
|
44
|
+
output += <<~EOS
|
|
45
|
+
WITNESS VERSION: #{addr.ver}
|
|
46
|
+
WITNESS PROGRAM: #{addr.prog.pack('C*').unpack1('H*')}
|
|
30
47
|
EOS
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
entity = Bech32::Nostr::NIP19.decode(bech32_str)
|
|
40
|
-
if entity.is_a?(Bech32::Nostr::BareEntity)
|
|
41
|
-
puts <<~EOS
|
|
42
|
-
NIP19 Entity: #{entity.data}
|
|
43
|
-
EOS
|
|
44
|
-
else
|
|
45
|
-
puts 'NIP19 Entities:'
|
|
46
|
-
entity.entries.each do |entry|
|
|
47
|
-
puts " #{entry.to_s}"
|
|
48
|
-
end
|
|
48
|
+
when *Bech32::Nostr::NIP19::ALL_PREFIXES
|
|
49
|
+
entity = Bech32::Nostr::NIP19.decode(bech32_str)
|
|
50
|
+
if entity.is_a?(Bech32::Nostr::BareEntity)
|
|
51
|
+
output += "NIP19 Entity: #{entity.data}\n"
|
|
52
|
+
else
|
|
53
|
+
output += "NIP19 Entities:\n"
|
|
54
|
+
entity.entries.each do |entry|
|
|
55
|
+
output += " #{entry.to_s}\n"
|
|
49
56
|
end
|
|
50
|
-
when Bech32::SilentPaymentAddr::HRP_MAINNET, Bech32::SilentPaymentAddr::HRP_TESTNET
|
|
51
|
-
addr = Bech32::SilentPaymentAddr.parse(bech32_str)
|
|
52
|
-
puts <<~EOS
|
|
53
|
-
version: #{addr.version}
|
|
54
|
-
scan public key: #{addr.scan_key}
|
|
55
|
-
spend public key: #{addr.spend_key}
|
|
56
|
-
EOS
|
|
57
57
|
end
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
when Bech32::SilentPaymentAddr::HRP_MAINNET, Bech32::SilentPaymentAddr::HRP_TESTNET
|
|
59
|
+
addr = Bech32::SilentPaymentAddr.parse(bech32_str)
|
|
60
|
+
output += <<~EOS
|
|
61
|
+
version: #{addr.version}
|
|
62
|
+
scan public key: #{addr.scan_key}
|
|
63
|
+
spend public key: #{addr.spend_key}
|
|
64
|
+
EOS
|
|
61
65
|
end
|
|
66
|
+
puts output
|
|
62
67
|
rescue ArgumentError => e
|
|
63
68
|
puts "Can not decode: #{e.message}"
|
|
64
69
|
exit 1
|
data/lib/bech32/nostr/entity.rb
CHANGED
|
@@ -11,6 +11,7 @@ module Bech32
|
|
|
11
11
|
# @param [String] data Entity data(hex string).
|
|
12
12
|
def initialize(hrp, data)
|
|
13
13
|
raise ArgumentError, "HRP #{hrp} is unsupported." unless NIP19::BARE_PREFIXES.include?(hrp)
|
|
14
|
+
raise ArgumentError, "Data must be a hex string." unless Bech32.hex_string?(data)
|
|
14
15
|
raise ArgumentError, "Data whose HRP is #{hrp} must be 32 bytes." unless [data].pack('H*').bytesize == 32
|
|
15
16
|
@hrp = hrp
|
|
16
17
|
@data = data
|
|
@@ -31,6 +32,11 @@ module Bech32
|
|
|
31
32
|
|
|
32
33
|
def initialize(type, value, label = nil)
|
|
33
34
|
raise ArgumentError, "Type #{type} unsupported." unless TLVEntity::TYPES.include?(type)
|
|
35
|
+
if type == TLVEntity::TYPE_KIND
|
|
36
|
+
raise ArgumentError, "Value of kind must be Integer. #{value.class} given." unless value.is_a?(Integer)
|
|
37
|
+
else
|
|
38
|
+
raise ArgumentError, "Value of type #{type} must be String. #{value.class} given." unless value.is_a?(String)
|
|
39
|
+
end
|
|
34
40
|
|
|
35
41
|
@type = type
|
|
36
42
|
@value = value
|
|
@@ -100,10 +106,7 @@ module Bech32
|
|
|
100
106
|
# @param [String] str string.
|
|
101
107
|
# @return [Boolean]
|
|
102
108
|
def hex_string?(str)
|
|
103
|
-
|
|
104
|
-
return false if str.length % 2 != 0
|
|
105
|
-
hex_chars = str.chars.to_a
|
|
106
|
-
hex_chars.all? { |c| c =~ /[0-9a-fA-F]/ }
|
|
109
|
+
Bech32.hex_string?(str)
|
|
107
110
|
end
|
|
108
111
|
end
|
|
109
112
|
|
|
@@ -116,6 +119,25 @@ module Bech32
|
|
|
116
119
|
|
|
117
120
|
TYPES = [TYPE_SPECIAL, TYPE_RELAY, TYPE_AUTHOR, TYPE_KIND]
|
|
118
121
|
|
|
122
|
+
# TLV types each HRP supports. Other types are ignored when parsing (NIP-19).
|
|
123
|
+
SUPPORTED_TYPES = {
|
|
124
|
+
NIP19::HRP_PROFILE => [TYPE_SPECIAL, TYPE_RELAY].freeze,
|
|
125
|
+
NIP19::HRP_EVENT => [TYPE_SPECIAL, TYPE_RELAY, TYPE_AUTHOR, TYPE_KIND].freeze,
|
|
126
|
+
NIP19::HRP_RELAY => [TYPE_SPECIAL].freeze,
|
|
127
|
+
NIP19::HRP_EVENT_COORDINATE => [TYPE_SPECIAL, TYPE_RELAY, TYPE_AUTHOR, TYPE_KIND].freeze
|
|
128
|
+
}.freeze
|
|
129
|
+
|
|
130
|
+
# TLV types each HRP requires (NIP-19).
|
|
131
|
+
REQUIRED_TYPES = {
|
|
132
|
+
NIP19::HRP_PROFILE => [TYPE_SPECIAL].freeze,
|
|
133
|
+
NIP19::HRP_EVENT => [TYPE_SPECIAL].freeze,
|
|
134
|
+
NIP19::HRP_RELAY => [TYPE_SPECIAL].freeze,
|
|
135
|
+
NIP19::HRP_EVENT_COORDINATE => [TYPE_SPECIAL, TYPE_AUTHOR, TYPE_KIND].freeze
|
|
136
|
+
}.freeze
|
|
137
|
+
|
|
138
|
+
# Byte length of a public key or an event id (NIP-19).
|
|
139
|
+
KEY_LENGTH = 32
|
|
140
|
+
|
|
119
141
|
attr_reader :hrp
|
|
120
142
|
attr_reader :entries
|
|
121
143
|
|
|
@@ -138,6 +160,7 @@ module Bech32
|
|
|
138
160
|
# @param [String] data Entity data(binary format).
|
|
139
161
|
# @return [TLVEntity]
|
|
140
162
|
def self.parse(hrp, data)
|
|
163
|
+
supported = SUPPORTED_TYPES.fetch(hrp) { raise ArgumentError, "HRP #{hrp} is unsupported." }
|
|
141
164
|
buf = ::StringIO.new(data)
|
|
142
165
|
entries = []
|
|
143
166
|
until buf.eof?
|
|
@@ -147,55 +170,100 @@ module Bech32
|
|
|
147
170
|
value = buf.read(len)
|
|
148
171
|
raise ArgumentError, "Invalid TLV: truncated value." if value.nil? ? len > 0 : value.bytesize < len
|
|
149
172
|
value ||= ''
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
case hrp
|
|
173
|
-
when NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
|
|
174
|
-
entries << TLVEntry.new(type, value.unpack1('H*'), 'author')
|
|
175
|
-
else
|
|
176
|
-
raise ArgumentError, "Type: #{type} does not supported for HRP: #{hrp}"
|
|
177
|
-
end
|
|
178
|
-
when TYPE_KIND # kind
|
|
179
|
-
case hrp
|
|
180
|
-
when NIP19::HRP_EVENT, NIP19::HRP_EVENT_COORDINATE
|
|
181
|
-
raise ArgumentError, 'Invalid TLV: kind must be 4 bytes.' unless value.bytesize == 4
|
|
182
|
-
entries << TLVEntry.new(type, value.unpack1('N'), 'kind')
|
|
183
|
-
else
|
|
184
|
-
raise ArgumentError, "Type: #{type} does not supported for HRP: #{hrp}"
|
|
185
|
-
end
|
|
186
|
-
else
|
|
187
|
-
raise ArgumentError, "Unknown TLV type: #{type}"
|
|
188
|
-
end
|
|
173
|
+
# NIP-19: TLVs that are not recognized or supported should be ignored, rather than causing an error.
|
|
174
|
+
next unless supported.include?(type)
|
|
175
|
+
|
|
176
|
+
check_value_length(hrp, type, value.bytesize)
|
|
177
|
+
check_value_encoding(hrp, type, value)
|
|
178
|
+
|
|
179
|
+
entries << case type
|
|
180
|
+
when TYPE_SPECIAL
|
|
181
|
+
case hrp
|
|
182
|
+
when NIP19::HRP_PROFILE then TLVEntry.new(type, value.unpack1('H*'), 'pubkey')
|
|
183
|
+
when NIP19::HRP_RELAY then TLVEntry.new(type, value.force_encoding(::Encoding::UTF_8), 'relay')
|
|
184
|
+
when NIP19::HRP_EVENT then TLVEntry.new(type, value.unpack1('H*'), 'id')
|
|
185
|
+
else TLVEntry.new(type, value.force_encoding(::Encoding::UTF_8), 'identifier')
|
|
186
|
+
end
|
|
187
|
+
when TYPE_RELAY
|
|
188
|
+
TLVEntry.new(type, value.force_encoding(::Encoding::UTF_8), 'relay')
|
|
189
|
+
when TYPE_AUTHOR
|
|
190
|
+
TLVEntry.new(type, value.unpack1('H*'), 'author')
|
|
191
|
+
else # TYPE_KIND
|
|
192
|
+
raise ArgumentError, 'Invalid TLV: kind must be 4 bytes.' unless value.bytesize == 4
|
|
193
|
+
TLVEntry.new(type, value.unpack1('N'), 'kind')
|
|
194
|
+
end
|
|
189
195
|
end
|
|
190
196
|
|
|
197
|
+
check_required_types(hrp, entries)
|
|
191
198
|
TLVEntity.new(hrp, entries)
|
|
192
199
|
end
|
|
193
200
|
|
|
194
201
|
# Encode tlv entity to bech32 string.
|
|
202
|
+
# The entity is checked against the same rules as {.parse}, so that the result can be decoded.
|
|
195
203
|
# @return [String] bech32 string.
|
|
204
|
+
# @raise [ArgumentError] if an entry type is unsupported for the HRP, a value has an invalid length
|
|
205
|
+
# or encoding, a required entry is missing, or the result is longer than NIP19::MAX_LENGTH characters.
|
|
196
206
|
def encode
|
|
197
|
-
|
|
198
|
-
|
|
207
|
+
supported = SUPPORTED_TYPES[hrp]
|
|
208
|
+
entries.each do |e|
|
|
209
|
+
raise ArgumentError, "Type #{e.type} is unsupported for HRP: #{hrp}" unless supported.include?(e.type)
|
|
210
|
+
end
|
|
211
|
+
payloads = entries.map { |e| e.to_payload(hrp) }
|
|
212
|
+
entries.zip(payloads) do |e, payload|
|
|
213
|
+
TLVEntity.check_value_length(hrp, e.type, payload.bytesize - 2)
|
|
214
|
+
TLVEntity.check_value_encoding(hrp, e.type, payload.byteslice(2..))
|
|
215
|
+
end
|
|
216
|
+
TLVEntity.check_required_types(hrp, entries)
|
|
217
|
+
data = payloads.join
|
|
218
|
+
bech32 = Bech32.encode(hrp, Bech32.convert_bits(data.unpack('C*'), 8, 5), Bech32::Encoding::BECH32)
|
|
219
|
+
if bech32.length > NIP19::MAX_LENGTH
|
|
220
|
+
raise ArgumentError, "nip19 string must be at most #{NIP19::MAX_LENGTH} characters."
|
|
221
|
+
end
|
|
222
|
+
bech32
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Check the byte length of a TLV value.
|
|
226
|
+
# Public keys and event ids must be 32 bytes, and the relay of nrelay must not be empty.
|
|
227
|
+
# @param [String] hrp human-readable part.
|
|
228
|
+
# @param [Integer] type TLV type.
|
|
229
|
+
# @param [Integer] length byte length of the value.
|
|
230
|
+
# @raise [ArgumentError] if the length is invalid.
|
|
231
|
+
def self.check_value_length(hrp, type, length)
|
|
232
|
+
if type == TYPE_AUTHOR || (type == TYPE_SPECIAL && [NIP19::HRP_PROFILE, NIP19::HRP_EVENT].include?(hrp))
|
|
233
|
+
return if length == KEY_LENGTH
|
|
234
|
+
|
|
235
|
+
raise ArgumentError, "Invalid TLV: type #{type} for HRP: #{hrp} must be #{KEY_LENGTH} bytes."
|
|
236
|
+
elsif type == TYPE_SPECIAL && hrp == NIP19::HRP_RELAY && length.zero?
|
|
237
|
+
raise ArgumentError, "Invalid TLV: type #{type} for HRP: #{hrp} must not be empty."
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Check the character encoding of a relay or identifier value.
|
|
242
|
+
# NIP-19: a relay is ASCII. The naddr identifier ("d" tag) must be valid UTF-8.
|
|
243
|
+
# @param [String] hrp human-readable part.
|
|
244
|
+
# @param [Integer] type TLV type.
|
|
245
|
+
# @param [String] value raw value bytes.
|
|
246
|
+
# @raise [ArgumentError] if the encoding is invalid.
|
|
247
|
+
def self.check_value_encoding(hrp, type, value)
|
|
248
|
+
if type == TYPE_RELAY || (type == TYPE_SPECIAL && hrp == NIP19::HRP_RELAY)
|
|
249
|
+
raise ArgumentError, 'Invalid TLV: relay must be ASCII.' unless value.b.ascii_only?
|
|
250
|
+
elsif type == TYPE_SPECIAL && hrp == NIP19::HRP_EVENT_COORDINATE
|
|
251
|
+
unless value.dup.force_encoding(::Encoding::UTF_8).valid_encoding?
|
|
252
|
+
raise ArgumentError, 'Invalid TLV: identifier must be valid UTF-8.'
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Check that all TLV types required for the HRP are present.
|
|
258
|
+
# @param [String] hrp human-readable part.
|
|
259
|
+
# @param [Array<TLVEntry>] entries TLV entries.
|
|
260
|
+
# @raise [ArgumentError] if a required type is missing.
|
|
261
|
+
def self.check_required_types(hrp, entries)
|
|
262
|
+
REQUIRED_TYPES[hrp].each do |required|
|
|
263
|
+
next if entries.any? { |e| e.type == required }
|
|
264
|
+
|
|
265
|
+
raise ArgumentError, "Invalid TLV: missing required type #{required} for HRP: #{hrp}"
|
|
266
|
+
end
|
|
199
267
|
end
|
|
200
268
|
end
|
|
201
269
|
end
|
data/lib/bech32/nostr/nip19.rb
CHANGED
|
@@ -16,14 +16,20 @@ module Bech32
|
|
|
16
16
|
TLV_PREFIXES = [HRP_PROFILE, HRP_EVENT, HRP_RELAY, HRP_EVENT_COORDINATE]
|
|
17
17
|
ALL_PREFIXES = BARE_PREFIXES + TLV_PREFIXES
|
|
18
18
|
|
|
19
|
+
# Maximum length of a NIP-19 string (NIP-19: "SHOULD be limited in size to 5000 characters").
|
|
20
|
+
MAX_LENGTH = 5000
|
|
21
|
+
|
|
19
22
|
module_function
|
|
20
23
|
|
|
21
24
|
# Decode nip19 string.
|
|
22
25
|
# @param [String] string Bech32 string.
|
|
23
26
|
# @return [BareEntity]
|
|
24
27
|
# @return [TLVEntity]
|
|
28
|
+
# @raise [ArgumentError] if the string is invalid or longer than MAX_LENGTH characters.
|
|
25
29
|
def decode(string)
|
|
26
|
-
|
|
30
|
+
raise ArgumentError, 'nip19 string must be String.' unless string.is_a?(String)
|
|
31
|
+
raise ArgumentError, "nip19 string must be at most #{MAX_LENGTH} characters." if string.length > MAX_LENGTH
|
|
32
|
+
hrp, data, spec = Bech32.decode(string, MAX_LENGTH)
|
|
27
33
|
|
|
28
34
|
raise ArgumentError, 'Invalid nip19 string.' if hrp.nil?
|
|
29
35
|
raise ArgumentError, 'Invalid bech32 spec.' unless spec == Bech32::Encoding::BECH32
|
data/lib/bech32/segwit_addr.rb
CHANGED
|
@@ -28,6 +28,7 @@ module Bech32
|
|
|
28
28
|
# @raise [ArgumentError] if the script is not a valid segwit script pubkey.
|
|
29
29
|
def script_pubkey=(script_pubkey)
|
|
30
30
|
raise ArgumentError, 'script_pubkey must be String.' unless script_pubkey.is_a?(String)
|
|
31
|
+
raise ArgumentError, 'script_pubkey must be a hex string.' unless Bech32.hex_string?(script_pubkey)
|
|
31
32
|
values = [script_pubkey].pack('H*').unpack('C*')
|
|
32
33
|
raise ArgumentError, 'Invalid segwit script pubkey.' if values.length < 2
|
|
33
34
|
op, push_len = values[0], values[1]
|
|
@@ -24,33 +24,69 @@ module Bech32
|
|
|
24
24
|
raise ArgumentError, "version must be between 0 and #{MAX_VERSION}." unless version.between?(0, MAX_VERSION)
|
|
25
25
|
raise ArgumentError, "scan_key must be String." unless scan_key.is_a?(String)
|
|
26
26
|
raise ArgumentError, "spend_key must be String." unless spend_key.is_a?(String)
|
|
27
|
+
raise ArgumentError, 'scan_key must be a hex string.' unless Bech32.hex_string?(scan_key)
|
|
28
|
+
raise ArgumentError, 'spend_key must be a hex string.' unless Bech32.hex_string?(spend_key)
|
|
27
29
|
raise ArgumentError, 'scan_key must be 33 bytes.' unless [scan_key].pack('H*').bytesize == 33
|
|
28
30
|
raise ArgumentError, 'spend_key must be 33 bytes.' unless [spend_key].pack('H*').bytesize == 33
|
|
31
|
+
# BIP-352: keys are serialized in SEC1 compressed form (0x02 or 0x03 followed by the x coordinate).
|
|
32
|
+
# Whether the x coordinate is on the curve is not checked.
|
|
33
|
+
raise ArgumentError, 'scan_key must be a compressed public key.' unless compressed_key?(scan_key)
|
|
34
|
+
raise ArgumentError, 'spend_key must be a compressed public key.' unless compressed_key?(spend_key)
|
|
29
35
|
@hrp = hrp
|
|
30
36
|
@version = version
|
|
31
37
|
@scan_key = scan_key
|
|
32
38
|
@spend_key = spend_key
|
|
33
39
|
end
|
|
34
40
|
|
|
41
|
+
# Byte length of the data part (scan key + spend key) for version 0.
|
|
42
|
+
KEYS_LENGTH = 66
|
|
43
|
+
|
|
44
|
+
# Number of 5-bit groups that cover KEYS_LENGTH bytes.
|
|
45
|
+
KEYS_GROUPS = (KEYS_LENGTH * 8 + 4) / 5
|
|
46
|
+
|
|
47
|
+
# Maximum length of an address string recommended by BIP-352.
|
|
48
|
+
MAX_LENGTH = 1023
|
|
49
|
+
|
|
35
50
|
# Parse silent payment address.
|
|
51
|
+
# Following BIP-352, the data part of a version 0 address must be exactly 66 bytes.
|
|
52
|
+
# For versions 1 through 30, the first 66 bytes are read and the remaining bytes are discarded,
|
|
53
|
+
# so {#to_s} of such an address does not reproduce the original string if it had extra bytes.
|
|
36
54
|
# @param [String] addr Silent payment address.
|
|
37
55
|
# @return [Bech32::SilentPaymentAddr]
|
|
38
56
|
# @raise [ArgumentError]
|
|
39
57
|
def self.parse(addr)
|
|
40
58
|
raise ArgumentError, "addr must be String." unless addr.is_a?(String)
|
|
41
|
-
|
|
59
|
+
raise ArgumentError, "addr must be at most #{MAX_LENGTH} characters." if addr.length > MAX_LENGTH
|
|
60
|
+
hrp, data, spec = Bech32.decode(addr, MAX_LENGTH)
|
|
42
61
|
raise ArgumentError, "addr must be encoded with bech32m." unless spec == Bech32::Encoding::BECH32M
|
|
43
62
|
raise ArgumentError, 'Invalid hrp.' if hrp.nil? || data[0].nil? || ![HRP_MAINNET, HRP_TESTNET].include?(hrp)
|
|
44
63
|
version = data[0]
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
64
|
+
if version.zero?
|
|
65
|
+
converted = Bech32.convert_bits(data[1..-1], 5, 8, false)
|
|
66
|
+
raise ArgumentError, "Invalid data." if converted.nil?
|
|
67
|
+
raise ArgumentError, "Invalid key size." unless converted.length == KEYS_LENGTH
|
|
68
|
+
else
|
|
69
|
+
# Convert only the 5-bit groups that cover the first 66 bytes, so that the discarded rest
|
|
70
|
+
# does not need to end on a byte boundary.
|
|
71
|
+
groups = data[1..-1]
|
|
72
|
+
raise ArgumentError, "Invalid key size." if groups.length < KEYS_GROUPS
|
|
73
|
+
converted = Bech32.convert_bits(groups[0...KEYS_GROUPS], 5, 8, true)
|
|
74
|
+
raise ArgumentError, "Invalid data." if converted.nil?
|
|
75
|
+
end
|
|
76
|
+
keys = converted[0...KEYS_LENGTH].pack('C*').unpack1('H*')
|
|
49
77
|
scan = keys[0...66]
|
|
50
78
|
spend = keys[66..-1]
|
|
51
79
|
Bech32::SilentPaymentAddr.new(hrp, version, scan, spend)
|
|
52
80
|
end
|
|
53
81
|
|
|
82
|
+
# Check whether the first byte of the 33-byte hex key is 0x02 or 0x03.
|
|
83
|
+
# @param [String] key public key with hex format.
|
|
84
|
+
# @return [Boolean]
|
|
85
|
+
def compressed_key?(key)
|
|
86
|
+
[0x02, 0x03].include?([key].pack('H*').getbyte(0))
|
|
87
|
+
end
|
|
88
|
+
private :compressed_key?
|
|
89
|
+
|
|
54
90
|
# Get silent payment address.
|
|
55
91
|
# @return [String]
|
|
56
92
|
def to_s
|
data/lib/bech32/version.rb
CHANGED
data/lib/bech32.rb
CHANGED
|
@@ -59,6 +59,8 @@ module Bech32
|
|
|
59
59
|
# check data length
|
|
60
60
|
pos = bech.rindex(SEPARATOR)
|
|
61
61
|
return nil if pos.nil? || pos < 1 || pos + 7 > bech.length || bech.length > max_length
|
|
62
|
+
# check hrp length (BIP-173: 1 to 83 characters), regardless of max_length
|
|
63
|
+
return nil if pos > HRP_MAX_LENGTH
|
|
62
64
|
# check valid charset
|
|
63
65
|
bech[pos+1..-1].each_char{|c|return nil unless CHARSET.include?(c)}
|
|
64
66
|
# split hrp and data
|
|
@@ -125,6 +127,17 @@ module Bech32
|
|
|
125
127
|
ret
|
|
126
128
|
end
|
|
127
129
|
|
|
130
|
+
# Check whether +str+ is a hex string: an even number of hex characters (0-9, a-f, A-F).
|
|
131
|
+
# An empty string is a hex string of zero bytes. It never raises, whatever the encoding of +str+.
|
|
132
|
+
# @param [Object] str the value to check.
|
|
133
|
+
# @return [Boolean]
|
|
134
|
+
def hex_string?(str)
|
|
135
|
+
return false unless str.is_a?(String)
|
|
136
|
+
# Match on the raw bytes, so that invalid or non-ASCII-compatible encodings return false instead of raising.
|
|
137
|
+
bytes = str.b
|
|
138
|
+
bytes.bytesize.even? && bytes.match?(/\A\h*\z/n)
|
|
139
|
+
end
|
|
140
|
+
|
|
128
141
|
# Validate arguments of encode.
|
|
129
142
|
# @param [String] hrp human-readable part.
|
|
130
143
|
# @param [Array<Integer>] data data values.
|
data/sig/bech32/bech32.rbs
CHANGED
|
@@ -26,12 +26,16 @@ module Bech32
|
|
|
26
26
|
# Verify checksum. Returns encoding spec if valid, nil otherwise.
|
|
27
27
|
def self.verify_checksum: (String hrp, Array[Integer] data) -> Integer?
|
|
28
28
|
|
|
29
|
+
# Check whether the value is a hex string.
|
|
30
|
+
def self.hex_string?: (untyped str) -> bool
|
|
31
|
+
|
|
29
32
|
# module_function methods are also available as instance methods
|
|
30
33
|
def encode: (String hrp, Array[Integer] data, Integer spec) -> String
|
|
31
34
|
def decode: (String bech, ?Integer max_length) -> [String, Array[Integer], Integer]?
|
|
32
35
|
def convert_bits: (Array[Integer] data, Integer from, Integer to, ?bool padding) -> Array[Integer]?
|
|
33
36
|
def create_checksum: (String hrp, Array[Integer] data, Integer spec) -> Array[Integer]
|
|
34
37
|
def verify_checksum: (String hrp, Array[Integer] data) -> Integer?
|
|
38
|
+
def hex_string?: (untyped str) -> bool
|
|
35
39
|
|
|
36
40
|
private
|
|
37
41
|
|
data/sig/bech32/nostr/entity.rbs
CHANGED
|
@@ -43,6 +43,10 @@ module Bech32
|
|
|
43
43
|
TYPE_KIND: Integer
|
|
44
44
|
TYPES: Array[Integer]
|
|
45
45
|
|
|
46
|
+
SUPPORTED_TYPES: Hash[String, Array[Integer]]
|
|
47
|
+
REQUIRED_TYPES: Hash[String, Array[Integer]]
|
|
48
|
+
KEY_LENGTH: Integer
|
|
49
|
+
|
|
46
50
|
attr_reader hrp: String
|
|
47
51
|
attr_reader entries: Array[TLVEntry]
|
|
48
52
|
|
|
@@ -54,6 +58,15 @@ module Bech32
|
|
|
54
58
|
|
|
55
59
|
# Encode to bech32 string.
|
|
56
60
|
def encode: () -> String
|
|
61
|
+
|
|
62
|
+
# Check the byte length of a TLV value.
|
|
63
|
+
def self.check_value_length: (String hrp, Integer type, Integer length) -> void
|
|
64
|
+
|
|
65
|
+
# Check that all required TLV types are present.
|
|
66
|
+
def self.check_required_types: (String hrp, Array[TLVEntry] entries) -> void
|
|
67
|
+
|
|
68
|
+
# Check the character encoding of a relay or identifier value.
|
|
69
|
+
def self.check_value_encoding: (String hrp, Integer type, String value) -> void
|
|
57
70
|
end
|
|
58
71
|
end
|
|
59
72
|
end
|
data/sig/bech32/nostr/nip19.rbs
CHANGED
|
@@ -4,6 +4,9 @@ module Bech32
|
|
|
4
4
|
HRP_MAINNET: String
|
|
5
5
|
HRP_TESTNET: String
|
|
6
6
|
MAX_VERSION: Integer
|
|
7
|
+
KEYS_LENGTH: Integer
|
|
8
|
+
KEYS_GROUPS: Integer
|
|
9
|
+
MAX_LENGTH: Integer
|
|
7
10
|
|
|
8
11
|
attr_reader hrp: String
|
|
9
12
|
attr_reader version: Integer
|
|
@@ -18,5 +21,10 @@ module Bech32
|
|
|
18
21
|
|
|
19
22
|
# Generate silent payment address string.
|
|
20
23
|
def to_s: () -> String
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
# Check whether the key starts with 0x02 or 0x03.
|
|
28
|
+
def compressed_key?: (String key) -> bool
|
|
21
29
|
end
|
|
22
30
|
end
|