lightning-onion 0.2.2 → 0.2.3

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
  SHA1:
3
- metadata.gz: 8e96fee0d17c3fa8c0d25227c524d114420a3379
4
- data.tar.gz: ace242df5e184ca510f36c08e637cff9cdbaec45
3
+ metadata.gz: 5ab9c2e28c21df3eaba32de0c6f94e42fd20706c
4
+ data.tar.gz: e959dd65032a7f2da7691bce6fb88b2fb204b3ba
5
5
  SHA512:
6
- metadata.gz: 23f082d3364b7f6055196c53eebf9f4b2c27c1f745bc212013d264905709683b4780f6e8c4ba12fcf610729ced94ffd753fad525723db4a9568d44666772121c
7
- data.tar.gz: f261e6f469659ee727ece29a27df3fedfab34c93eccfc053c605f3181a87f2f719a6efee6a6eec3e879da7cd87e104ad72d68c8c4bb0bb9bd26e1c223d014f71
6
+ metadata.gz: a1d3b593b58cb084effcadf2ba13e005aad49883cd1f51913fedb9cc9a5e6513878378511b76374007f7e63db62932908b1d7c9c3bddfc78db79a2e94314bb23
7
+ data.tar.gz: 30f204b6fffe908f3a9d8cc233d30285610a374f50f3e55ce82e7c021788d5a59618ac4049ad41177890a3ce968ec33f4a52ed91107c7d2e8f4dedf870764ac9
@@ -3,29 +3,27 @@
3
3
  module Lightning
4
4
  module Onion
5
5
  class Packet
6
- attr_accessor :version, :public_key, :hops_data, :hmac
7
- def initialize(version, public_key, hops_data, hmac)
6
+ attr_accessor :version, :public_key, :routing_info, :hmac
7
+ def initialize(version, public_key, routing_info, hmac)
8
8
  @version = version
9
9
  @public_key = public_key
10
- @hops_data = hops_data
10
+ @routing_info = routing_info
11
+ raise "invalid size #{routing_info.size}" unless routing_info.size == 1300 * 2
11
12
  @hmac = hmac
12
13
  end
13
14
 
14
15
  def self.parse(payload)
15
- version, public_key, rest = payload.unpack('H2H66a*')
16
- hops_data = []
17
- 20.times do |i|
18
- hops_data << Lightning::Onion::HopData.parse(rest[i * 65...i * 65 + 65])
19
- end
20
- hmac = rest[21 * 65..-1]
21
- new(version, public_key, hops_data, hmac)
16
+ version, public_key, rest = payload.unpack('aH66a*')
17
+ routing_info = rest[0...20 * 65].bth
18
+ hmac = rest[20 * 65..-1].bth
19
+ new(version, public_key, routing_info, hmac)
22
20
  end
23
21
 
24
22
  def to_payload
25
23
  payload = +''
26
- payload << [version.bth, public_key].pack('H2H66')
27
- payload << [hops_data.map(&:to_payload).join].pack('a1300')
28
- payload << hmac
24
+ payload << [version, public_key].pack('aH66')
25
+ payload << routing_info.htb
26
+ payload << hmac.htb
29
27
  payload
30
28
  end
31
29
  end
@@ -12,13 +12,29 @@ module Lightning
12
12
  end
13
13
 
14
14
  def self.parse(payload)
15
- new(*payload.unpack('Q>2Na16'))
15
+ new(*payload.unpack('Q>2Na12'))
16
16
  end
17
+ LAST_NODE = PerHop.parse("\x00" * 32)
17
18
 
18
19
  def to_payload
19
- [short_channel_id, amt_to_forward, outgoing_cltv_value, padding].pack('Q>2Na12')
20
+ to_a.pack('Q>2Na12')
21
+ end
22
+
23
+ def ==(other)
24
+ other.class == self.class && other.to_a == to_a
25
+ end
26
+
27
+ alias eql? ==
28
+
29
+ def hash
30
+ to_a.hash
31
+ end
32
+
33
+ protected
34
+
35
+ def to_a
36
+ [short_channel_id, amt_to_forward, outgoing_cltv_value, padding]
20
37
  end
21
- LAST_NODE = PerHop.parse("\x00" * 32)
22
38
  end
23
39
  end
24
40
  end
@@ -11,8 +11,7 @@ module Lightning
11
11
  MAX_ERROR_PAYLOAD_LENGTH = 256
12
12
  ERROR_PACKET_LENGTH = MAC_LENGTH + MAX_ERROR_PAYLOAD_LENGTH + 2 + 2
13
13
 
14
- ZERO_HOP = Lightning::Onion::HopData.parse("\x00" * HOP_LENGTH)
15
- LAST_PACKET = Lightning::Onion::Packet.new(VERSION, "\x00" * 33, [ZERO_HOP] * MAX_HOPS, "\x00" * MAC_LENGTH)
14
+ LAST_PACKET = Lightning::Onion::Packet.new(VERSION, "\x00" * 33, '00' * MAX_HOPS * HOP_LENGTH, '00' * MAC_LENGTH)
16
15
 
17
16
  def self.make_packet(session_key, public_keys, payloads, associated_data)
18
17
  ephemereal_public_keys, shared_secrets = compute_keys_and_secrets(session_key, public_keys)
@@ -35,6 +34,25 @@ module Lightning
35
34
  [packet, shared_secrets.zip(public_keys)]
36
35
  end
37
36
 
37
+ # @return payload 33bytes payload of the outermost layer of onions,which including realm
38
+ # @return packet
39
+ def self.parse(private_key, raw_packet)
40
+ packet = Lightning::Onion::Packet.parse(raw_packet)
41
+ shared_secret = compute_shared_secret(packet.public_key, private_key)
42
+ rho = generate_key('rho', shared_secret)
43
+ bin = xor(
44
+ (packet.routing_info + '00' * HOP_LENGTH).htb.unpack('C*'),
45
+ generate_cipher_stream(rho, HOP_LENGTH + MAX_HOPS * HOP_LENGTH).unpack('C*')
46
+ )
47
+ payload = bin[0...HOP_LENGTH].pack('C*')
48
+ hmac = bin[PAYLOAD_LENGTH...HOP_LENGTH].pack('C*')
49
+ next_hops_data = bin[HOP_LENGTH..-1]
50
+
51
+ next_public_key = make_blind(packet.public_key, compute_blinding_factor(packet.public_key, shared_secret))
52
+ routing_info = next_hops_data.pack('C*').bth
53
+ [Lightning::Onion::HopData.parse(payload), Lightning::Onion::Packet.new(VERSION, next_public_key, routing_info, hmac.bth), shared_secret]
54
+ end
55
+
38
56
  def self.internal_make_packet(hop_payloads, keys, shared_secrets, packet, associated_data)
39
57
  return packet if hop_payloads.empty?
40
58
  next_packet = make_next_packet(hop_payloads.last, associated_data, keys.last, shared_secrets.last, packet)
@@ -42,8 +60,9 @@ module Lightning
42
60
  end
43
61
 
44
62
  def self.make_next_packet(payload, associated_data, ephemereal_public_key, shared_secret, packet, filler = '')
45
- hops_data1 = payload.htb << packet.hmac << packet.hops_data.map(&:to_payload).join[0...-HOP_LENGTH]
46
- stream = generate_cipher_stream(generate_key('rho', shared_secret), MAX_HOPS * HOP_LENGTH)
63
+ hops_data1 = payload.htb << packet.hmac.htb << packet.routing_info.htb[0...-HOP_LENGTH]
64
+ rho_key = generate_key('rho', shared_secret)
65
+ stream = generate_cipher_stream(rho_key, MAX_HOPS * HOP_LENGTH)
47
66
  hops_data2 = xor(hops_data1.unpack('C*'), stream.unpack('C*'))
48
67
  next_hops_data =
49
68
  if filler.empty?
@@ -51,14 +70,10 @@ module Lightning
51
70
  else
52
71
  hops_data2[0...-filler.htb.unpack('C*').size] + filler.htb.unpack('C*')
53
72
  end
54
- next_hmac = mac(generate_key('mu', shared_secret), next_hops_data + associated_data.htb.unpack('C*'))
55
- hops_data = []
56
- 20.times do |i|
57
- payload = next_hops_data.pack('C*')[i * HOP_LENGTH...(i + 1) * HOP_LENGTH]
58
- hops_data << Lightning::Onion::HopData.parse(payload)
59
- end
60
-
61
- Lightning::Onion::Packet.new(VERSION, ephemereal_public_key, hops_data, next_hmac)
73
+ mu_key = generate_key('mu', shared_secret)
74
+ next_hmac = mac(mu_key, next_hops_data + associated_data.htb.unpack('C*'))
75
+ routing_info = next_hops_data.pack('C*').bth
76
+ Lightning::Onion::Packet.new(VERSION, ephemereal_public_key, routing_info, next_hmac.bth)
62
77
  end
63
78
 
64
79
  def self.compute_keys_and_secrets(session_key, public_keys)
@@ -157,27 +172,6 @@ module Lightning
157
172
  hmac256(key, message.pack('C*'))[0...MAC_LENGTH]
158
173
  end
159
174
 
160
- def self.parse(private_key, raw_packet)
161
- packet = Lightning::Onion::Packet.parse(raw_packet)
162
- shared_secret = compute_shared_secret(packet.public_key, private_key)
163
- rho = generate_key('rho', shared_secret)
164
- bin = xor(
165
- (packet.hops_data.map(&:to_payload).join + "\x00" * HOP_LENGTH).unpack('C*'),
166
- generate_cipher_stream(rho, HOP_LENGTH + MAX_HOPS * HOP_LENGTH).unpack('C*')
167
- )
168
- payload = bin[0...PAYLOAD_LENGTH].pack('C*')
169
- hmac = bin[PAYLOAD_LENGTH...HOP_LENGTH].pack('C*')
170
- next_hops_data = bin[HOP_LENGTH..-1]
171
-
172
- next_public_key = make_blind(packet.public_key, compute_blinding_factor(packet.public_key, shared_secret))
173
- hops_data = []
174
- 20.times do |i|
175
- hop_payload = next_hops_data.pack('C*')[i * HOP_LENGTH...(i + 1) * HOP_LENGTH]
176
- hops_data << Lightning::Onion::HopData.parse(hop_payload)
177
- end
178
- [payload, Lightning::Onion::Packet.new(VERSION, next_public_key, hops_data, hmac), shared_secret]
179
- end
180
-
181
175
  def self.make_error_packet(shared_secret, failure)
182
176
  message = failure.to_payload
183
177
  um = generate_key('um', shared_secret)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Lightning
4
4
  module Onion
5
- VERSION = '0.2.2'
5
+ VERSION = '0.2.3'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lightning-onion
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
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-06-06 00:00:00.000000000 Z
11
+ date: 2018-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: algebrick