lightning-invoice 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 375d36d83038fe6b7217886fdcfc6319ebec19e9d166f3f730bb8c77101f1540
4
- data.tar.gz: f174bf5270d239dabffd270d7e9b432c9b38b6c5b535f17f639f6ca0a5534775
3
+ metadata.gz: da417a5937a2c557525216152eaa8c781a61149e9a3570bebf025d9b39f8cc05
4
+ data.tar.gz: 2e06f5a1c4ce90876e538cd37d630e872c5954c7f82f6df172513b97e3fd7c66
5
5
  SHA512:
6
- metadata.gz: aa04a1fbf65880f4b74888a37dc7a972343acc4482eaf479d4231cc763e2615f328edd493cb78a991fbc84338d1288226a77c3af591e859384fdcd37bbc18dd8
7
- data.tar.gz: dfacafa8dd99a981463d19bc261f758281a1fc3941c854b2ce372827e8e8c396ee3ee93fc32c35bbad5e255fb657bbdc6f9000b29f5905a83ff46e2949b77bcb
6
+ metadata.gz: 6bdfd0640908019f31e1ecbd1b30eea3ba26f5f93787b924a61a94671de9888ce547dd48e2e1684ffe498cea18b8be6e375eabcf0adbab667338660c455067a1
7
+ data.tar.gz: 9266c30da04a5dc4d9cdcf4b79cdc744e7c3c7f87497bde42287035127cf2e8407653bb3f9135849100dbad9a11171e389f1e302f354d43f1d2dce669ee8d9b9
@@ -15,10 +15,6 @@ module Lightning
15
15
  attr_accessor :timestamp, :signature, :payment_hash, :description, :pubkey, :description_hash, :expiry, :min_final_cltv_expiry, :fallback_address, :routing_info
16
16
 
17
17
  def initialize
18
- @amount = -1
19
- @timestamp = 0
20
- @expiry = 3600
21
- @min_final_cltv_expiry = 9
22
18
  @routing_info = []
23
19
  end
24
20
 
@@ -29,40 +25,40 @@ module Lightning
29
25
  human << multiplier if multiplier
30
26
  data = []
31
27
  data += Invoice.int_to_array(timestamp)
32
- if payment_hash
28
+ if payment_hash && !payment_hash.empty?
33
29
  data += [1]
34
30
  data += [1, 20]
35
31
  data += Invoice.buffer_to_word(payment_hash.htb)
36
32
  end
37
- if description
33
+ if description && !description.empty?
38
34
  data += [13]
39
35
  description_word = Invoice.buffer_to_word(description)
40
36
  data += Invoice.int_to_array(description_word.size)
41
37
  data += description_word
42
38
  end
43
- if pubkey
39
+ if pubkey && !pubkey.empty?
44
40
  data += [19]
45
41
  data += [1, 21]
46
42
  data += Invoice.buffer_to_word(pubkey.htb)
47
43
  end
48
- if description_hash
44
+ if description_hash && !description_hash.empty?
49
45
  data += [23]
50
46
  data += [1, 20]
51
47
  data += Invoice.buffer_to_word(description_hash.htb)
52
48
  end
53
- if expiry && expiry != 3600
49
+ if expiry
54
50
  data += [6]
55
51
  expiry_word = Invoice.int_to_array(expiry)
56
52
  data += Invoice.int_to_array(expiry_word.size)
57
53
  data += expiry_word
58
54
  end
59
- if min_final_cltv_expiry && min_final_cltv_expiry != 9
55
+ if min_final_cltv_expiry
60
56
  data += [24]
61
57
  min_final_cltv_expiry_word = Invoice.int_to_array(min_final_cltv_expiry)
62
58
  data += Invoice.int_to_array(min_final_cltv_expiry_word.size)
63
59
  data += min_final_cltv_expiry_word
64
60
  end
65
- if fallback_address
61
+ if fallback_address && !fallback_address.empty?
66
62
  data += [9]
67
63
  type = fallback_address_type(fallback_address)
68
64
  case type
@@ -123,53 +119,58 @@ module Lightning
123
119
  message.timestamp = to_int(data_part[0...7])
124
120
  tags = data_part[7...data_part.size - 104]
125
121
  index = 0
126
- while index < tags.size
127
- type = tags[index]
128
- data_length = (tags[index + 1].to_i << 5) + tags[index + 2].to_i
129
- data = tags[index + 3 ... index + 3 + data_length]
130
- bytes = to_bytes(data)
131
- index += 3 + data_length
132
- case type
133
- when 1
134
- message.payment_hash = bytes[0...64].pack("C*").bth
135
- when 13
136
- message.description = bytes.pack("C*").force_encoding('utf-8')
137
- when 19
138
- message.pubkey = bytes[0...66].pack("C*").bth
139
- when 23
140
- message.description_hash = bytes[0...64].pack("C*").bth
141
- when 6
142
- message.expiry = to_int(data)
143
- when 24
144
- message.min_final_cltv_expiry = to_int(data)
145
- when 9
146
- address = to_bytes(data[1..-1])
147
- hex = address.pack("C*").unpack("H*").first
148
- case data[0]
149
- when 0
150
- message.fallback_address = Bitcoin::Script.to_p2wpkh(hex).addresses.first
151
- when 17
152
- message.fallback_address = Bitcoin.encode_base58_address(hex, Bitcoin.chain_params.address_version)
153
- when 18
154
- message.fallback_address = Bitcoin.encode_base58_address(hex, Bitcoin.chain_params.p2sh_version)
122
+ if tags
123
+ while index < tags.size
124
+ type = tags[index]
125
+ data_length = (tags[index + 1].to_i << 5) + tags[index + 2].to_i
126
+ data = tags[index + 3 ... index + 3 + data_length]
127
+ bytes = to_bytes(data)
128
+ index += 3 + data_length
129
+ case type
130
+ when 1
131
+ message.payment_hash = bytes[0...64].pack("C*").bth
132
+ when 13
133
+ message.description = bytes.pack("C*").force_encoding('utf-8')
134
+ when 19
135
+ message.pubkey = bytes[0...66].pack("C*").bth
136
+ when 23
137
+ message.description_hash = bytes[0...64].pack("C*").bth
138
+ when 6
139
+ message.expiry = to_int(data)
140
+ when 24
141
+ message.min_final_cltv_expiry = to_int(data)
142
+ when 9
143
+ address = to_bytes(data[1..-1])
144
+ hex = address.pack("C*").unpack("H*").first
145
+ case data[0]
146
+ when 0
147
+ message.fallback_address = Bitcoin::Script.to_p2wpkh(hex).addresses.first
148
+ when 17
149
+ message.fallback_address = Bitcoin.encode_base58_address(hex, Bitcoin.chain_params.address_version)
150
+ when 18
151
+ message.fallback_address = Bitcoin.encode_base58_address(hex, Bitcoin.chain_params.p2sh_version)
152
+ else
153
+ end
154
+ when 3
155
+ offset = 0
156
+ while offset < bytes.size
157
+ message.routing_info << Lightning::Invoice::RoutingInfo.new(
158
+ bytes[offset...offset + 33].pack("C*").bth,
159
+ bytes[offset + 33...offset + 41].pack("C*").bth,
160
+ to_int(bytes[offset + 41...offset + 45]),
161
+ to_int(bytes[offset + 45...offset + 49]),
162
+ to_int(bytes[offset + 49...offset + 51])
163
+ )
164
+ offset += 51
165
+ end
155
166
  else
156
167
  end
157
- when 3
158
- offset = 0
159
- while offset < bytes.size
160
- message.routing_info << Lightning::Invoice::RoutingInfo.new(
161
- bytes[offset...offset + 33].pack("C*").bth,
162
- bytes[offset + 33...offset + 41].pack("C*").bth,
163
- to_int(bytes[offset + 41...offset + 45]),
164
- to_int(bytes[offset + 45...offset + 49]),
165
- to_int(bytes[offset + 49...offset + 51])
166
- )
167
- offset += 51
168
- end
169
- else
170
168
  end
171
169
  end
172
- message.signature = word_to_buffer(data_part[data_part.size - 104..-1], true).bth
170
+ sig = data_part[data_part.size - 104..-1]
171
+ if sig
172
+ message.signature = word_to_buffer(sig).bth
173
+ end
173
174
  message
174
175
  end
175
176
 
@@ -177,21 +178,17 @@ module Lightning
177
178
  human.scan(/^([a-zA-Z]+)(\d*)([munp]?)$/)&.first
178
179
  end
179
180
 
180
- def self.word_to_buffer(data, trim)
181
- buffer = convert(data, 5, 8)
182
-
183
- if trim && (data.size * 5 % 8 != 0)
184
- buffer = buffer[0...-1]
185
- end
181
+ def self.word_to_buffer(data)
182
+ buffer = convert(data, 5, 8, false)
186
183
  return buffer.pack("C*")
187
184
  end
188
185
 
189
186
  def self.buffer_to_word(buffer)
190
- words = convert(buffer.unpack('C*'), 8, 5)
187
+ words = convert(buffer.unpack('C*'), 8, 5, true)
191
188
  return words
192
189
  end
193
190
 
194
- def self.convert(data, inbits, outbits)
191
+ def self.convert(data, inbits, outbits, padding)
195
192
  value = 0
196
193
  bits = 0
197
194
  max = (1 << outbits) - 1
@@ -207,7 +204,7 @@ module Lightning
207
204
  end
208
205
  end
209
206
 
210
- if bits > 0
207
+ if padding && bits > 0
211
208
  result << ((value << (outbits - bits)) & max)
212
209
  end
213
210
 
@@ -1,5 +1,5 @@
1
1
  module Lightning
2
2
  module Invoice
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightning-invoice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hajime Yamaguchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-19 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler