lightning-onion 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1a47804ce3e79e397833b1e477eda7bf1531a025
4
+ data.tar.gz: 1b790426dada286bf65e320870b0f8f65a95b758
5
+ SHA512:
6
+ metadata.gz: d993a816fed1fb15c3fc20e1b306397766289ad125e2bb08dd8f3eff7444e27cc0e24a6ff7efc04bbe193d79d95d641cefa4c7e0230b46b0a035858234206bfe
7
+ data.tar.gz: 879b2eb37d5c7c87c026021c6a6b14cab285e876edd477dd13c4ac007ad24aeb5f2e6fafd3213eb341146b1d6a5e35b8115596d18f1f54a212d6b5f7687cafdc
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,26 @@
1
+ Metrics/AbcSize:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ Enabled: false
6
+
7
+ Metrics/LineLength:
8
+ Max: 120
9
+
10
+ Metrics/MethodLength:
11
+ Max: 30
12
+
13
+ Style/Documentation:
14
+ Enabled: false
15
+
16
+ Style/LambdaCall:
17
+ Enabled: false
18
+
19
+ Style/SymbolArray:
20
+ MinSize: 100
21
+
22
+ Style/WordArray:
23
+ MinSize: 100
24
+
25
+ AllCops:
26
+ TargetRubyVersion: 2.4.1
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at gen.yamaguchi0@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in lightning-onion.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Hajime Yamaguchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Lightning::Onion
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/lightning/onion`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'lightningrb-onion'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install lightningrb-onion
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Yamaguchi/lightning-onion. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require 'bundler/setup'
6
+ require 'lightning/onion'
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don't forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require 'irb'
16
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lightning/onion/version'
4
+
5
+ require 'rbnacl'
6
+ require 'bitcoin'
7
+
8
+ module Lightning
9
+ module Onion
10
+ autoload :ChaCha20, 'lightning/onion/chacha20'
11
+ autoload :HopData, 'lightning/onion/hop_data'
12
+ autoload :PerHop, 'lightning/onion/per_hop'
13
+ autoload :Packet, 'lightning/onion/packet'
14
+ autoload :Sphinx, 'lightning/onion/sphinx'
15
+ end
16
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ module ChaCha20
6
+ def self.constants
7
+ [0x61707865, 0x3320646e, 0x79622d32, 0x6b206574].pack('N*')
8
+ end
9
+
10
+ def self.chacha20_encrypt(key, counter, nonce, plaintext)
11
+ encrypted_message = +''
12
+ (plaintext.length / 64).times do |i|
13
+ key_stream = chacha20_block(key, counter + i, nonce)
14
+ block = plaintext[(i * 64)...(i + 1) * 64]
15
+ encrypted_message += xor(block, key_stream)
16
+ end
17
+ if plaintext.length % 64 != 0
18
+ i = plaintext.length / 64
19
+ key_stream = chacha20_block(key, counter + i, nonce)
20
+ block = plaintext[(i * 64)...plaintext.length]
21
+ block = block.ljust(64, "\x00")
22
+ encrypted_message += xor(block, key_stream)[0...(plaintext.length % 64)]
23
+ end
24
+ encrypted_message
25
+ end
26
+
27
+ def self.xor(a, b)
28
+ a = a.unpack('N*')
29
+ b = b.unpack('N*')
30
+ a.zip(b).map { |x, y| (x ^ y) & 0xffffffff }.pack('N*')
31
+ end
32
+
33
+ # key: 32 bytes
34
+ # counter: integer (4 bytes)
35
+ # nonce: 12 bytes
36
+ def self.chacha20_block(key, counter, nonce)
37
+ # reverse order
38
+ key = key.unpack('V*').pack('N*')
39
+ counter = [counter].pack('N*')
40
+ nonce = nonce.unpack('V*').pack('N*')
41
+ state = constants + key + counter + nonce
42
+ working_state = state.unpack('N*')
43
+ 10.times do
44
+ inner_block(working_state)
45
+ end
46
+ plus_for_string(state, working_state)
47
+ end
48
+
49
+ def self.inner_block(x)
50
+ # column rounds
51
+ x[0], x[4], x[8], x[12] = quater_round(x[0], x[4], x[8], x[12])
52
+ x[1], x[5], x[9], x[13] = quater_round(x[1], x[5], x[9], x[13])
53
+ x[2], x[6], x[10], x[14] = quater_round(x[2], x[6], x[10], x[14])
54
+ x[3], x[7], x[11], x[15] = quater_round(x[3], x[7], x[11], x[15])
55
+ # diagonal rounds
56
+ x[0], x[5], x[10], x[15] = quater_round(x[0], x[5], x[10], x[15])
57
+ x[1], x[6], x[11], x[12] = quater_round(x[1], x[6], x[11], x[12])
58
+ x[2], x[7], x[8], x[13] = quater_round(x[2], x[7], x[8], x[13])
59
+ x[3], x[4], x[9], x[14] = quater_round(x[3], x[4], x[9], x[14])
60
+ end
61
+
62
+ def self.plus_for_string(a, b)
63
+ a.unpack('N*').map.with_index do |x, i|
64
+ plus(x, b[i])
65
+ end.pack('V*')
66
+ end
67
+
68
+ def self.plus(x, y)
69
+ (x + y) & 0xffffffff
70
+ end
71
+
72
+ def self.rotate(x, n)
73
+ y = x << n
74
+ z = x >> (32 - n)
75
+ (y | z) & 0xffffffff
76
+ end
77
+
78
+ def self.quater_round(a, b, c, d)
79
+ a = plus(a, b)
80
+ d ^= a
81
+ d = rotate(d, 16)
82
+
83
+ c = plus(c, d)
84
+ b ^= c
85
+ b = rotate(b, 12)
86
+
87
+ a = plus(a, b)
88
+ d ^= a
89
+ d = rotate(d, 8)
90
+
91
+ c = plus(c, d)
92
+ b ^= c
93
+ b = rotate(b, 7)
94
+ [a, b, c, d]
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ class HopData
6
+ attr_accessor :realm, :per_hop, :hmac
7
+ def initialize(realm, per_hop, hmac)
8
+ @realm = realm
9
+ @per_hop = per_hop
10
+ @hmac = hmac
11
+ end
12
+
13
+ def self.parse(payload)
14
+ realm, per_hop_payload, hmac = payload.unpack('Ca32a32')
15
+ per_hop = Lightning::Onion::PerHop.parse(per_hop_payload)
16
+ new(realm, per_hop, hmac)
17
+ end
18
+
19
+ def to_payload
20
+ [realm, per_hop.to_payload, hmac].pack('Ca32a32')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ class Packet
6
+ attr_accessor :version, :public_key, :hops_data, :hmac
7
+ def initialize(version, public_key, hops_data, hmac)
8
+ @version = version
9
+ @public_key = public_key
10
+ @hops_data = hops_data
11
+ @hmac = hmac
12
+ end
13
+
14
+ 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)
22
+ end
23
+
24
+ def to_payload
25
+ payload = +''
26
+ payload << [version.bth, public_key].pack('H2H66')
27
+ payload << [hops_data.map(&:to_payload).join].pack('a1300')
28
+ payload << hmac
29
+ payload
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ class PerHop
6
+ attr_accessor :channel_id, :amt_to_forward, :outgoing_cltv_value, :padding
7
+ def initialize(channel_id, amt_to_forward, outgoing_cltv_value, padding)
8
+ @channel_id = channel_id
9
+ @amt_to_forward = amt_to_forward
10
+ @outgoing_cltv_value = outgoing_cltv_value
11
+ @padding = padding
12
+ end
13
+
14
+ def self.parse(payload)
15
+ new(*payload.unpack('a8N2a16'))
16
+ end
17
+
18
+ def to_payload
19
+ [channel_id, amt_to_forward, outgoing_cltv_value, padding].pack('a8N2a16')
20
+ end
21
+ LAST_NODE = PerHop.parse("\x00" * 32)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ module Sphinx
6
+ VERSION = "\x00"
7
+ PAYLOAD_LENGTH = 33
8
+ MAC_LENGTH = 32
9
+ MAX_HOPS = 20
10
+ HOP_LENGTH = PAYLOAD_LENGTH + MAC_LENGTH
11
+
12
+ ZERO_HOP = Lightning::Onion::HopData.parse("\x00" * HOP_LENGTH)
13
+ LAST_PACKET = Lightning::Onion::Packet.new(VERSION, "\x00" * PAYLOAD_LENGTH, [ZERO_HOP] * MAX_HOPS, "\x00" * MAC_LENGTH)
14
+
15
+ def self.loop(hop_payloads, keys, shared_secrets, packet, associated_data)
16
+ return packet if hop_payloads.empty?
17
+ next_packet = make_next_packet(hop_payloads.last, associated_data, keys.last, shared_secrets.last, packet)
18
+ loop(hop_payloads[0...-1], keys[0...-1], shared_secrets[0...-1], next_packet, associated_data)
19
+ end
20
+
21
+ def self.make_packet(session_key, public_keys, payloads, associated_data)
22
+ ephemereal_public_keys, shared_secrets = compute_ephemereal_public_keys_and_shared_secrets(session_key, public_keys)
23
+ filler = generate_filler('rho', shared_secrets[0...-1], HOP_LENGTH, MAX_HOPS)
24
+ last_packet = make_next_packet(payloads.last, associated_data, ephemereal_public_keys.last, shared_secrets.last, LAST_PACKET, filler)
25
+ packet = loop(payloads[0...-1], ephemereal_public_keys[0...-1], shared_secrets[0...-1], last_packet, associated_data)
26
+ [packet, shared_secrets.zip(public_keys)]
27
+ end
28
+
29
+ def self.make_next_packet(payload, associated_data, ephemereal_public_key, shared_secret, packet, filler = '')
30
+ hops_data1 = payload.htb << packet.hmac << packet.hops_data.map(&:to_payload).join[0...-HOP_LENGTH]
31
+ stream = generate_cipher_stream(generate_key('rho', shared_secret), MAX_HOPS * HOP_LENGTH)
32
+ hops_data2 = xor(hops_data1.unpack('C*'), stream.unpack('C*'))
33
+ next_hops_data =
34
+ if filler.empty?
35
+ hops_data2
36
+ else
37
+ hops_data2[0...-filler.htb.unpack('C*').size] + filler.htb.unpack('C*')
38
+ end
39
+ next_hmac = mac(generate_key('mu', shared_secret), next_hops_data + associated_data.htb.unpack('C*'))
40
+ hops_data = []
41
+ 20.times do |i|
42
+ payload = next_hops_data.pack('C*')[i * HOP_LENGTH...(i + 1) * HOP_LENGTH]
43
+ hops_data << Lightning::Onion::HopData.parse(payload)
44
+ end
45
+
46
+ Lightning::Onion::Packet.new(VERSION, ephemereal_public_key, hops_data, next_hmac)
47
+ end
48
+
49
+ def self.compute_ephemereal_public_keys_and_shared_secrets(session_key, public_keys)
50
+ point = ECDSA::Group::Secp256k1.generator
51
+ generator_pubkey = ECDSA::Format::PointOctetString.encode(point, compression: true)
52
+ ephemereal_public_key0 = make_blind(generator_pubkey.bth, session_key)
53
+ secret0 = compute_shared_secret(public_keys[0], session_key)
54
+ blinding_factor0 = compute_blinding_factor(ephemereal_public_key0, secret0)
55
+ internal_compute_ephemereal_public_keys_and_shared_secrets(session_key, public_keys[1..-1], [ephemereal_public_key0], [blinding_factor0], [secret0])
56
+ end
57
+
58
+ def self.internal_compute_ephemereal_public_keys_and_shared_secrets(session_key, public_keys, ephemereal_public_keys, blinding_factors, shared_secrets)
59
+ if public_keys.empty?
60
+ [ephemereal_public_keys, shared_secrets]
61
+ else
62
+ ephemereal_public_key = make_blind(ephemereal_public_keys.last, blinding_factors.last)
63
+ secret = compute_shared_secret(make_blinds(public_keys.first, blinding_factors), session_key)
64
+ blinding_factor = compute_blinding_factor(ephemereal_public_key, secret)
65
+ ephemereal_public_keys << ephemereal_public_key
66
+ blinding_factors << blinding_factor
67
+ shared_secrets << secret
68
+ internal_compute_ephemereal_public_keys_and_shared_secrets(session_key, public_keys[1..-1], ephemereal_public_keys, blinding_factors, shared_secrets)
69
+ end
70
+ end
71
+
72
+ def self.make_blind(public_key, blinding_factor)
73
+ point = Bitcoin::Key.new(pubkey: public_key).to_point
74
+ scalar = ECDSA::Format::IntegerOctetString.decode(blinding_factor.htb)
75
+ point = point.multiply_by_scalar(scalar)
76
+ public_key = ECDSA::Format::PointOctetString.encode(point, compression: true)
77
+ public_key.bth
78
+ end
79
+
80
+ def self.make_blinds(public_key, blinding_factors)
81
+ blinding_factors.inject(public_key) { |p, factor| make_blind(p, factor) }
82
+ end
83
+
84
+ def self.compute_shared_secret(public_key, secret)
85
+ scalar = ECDSA::Format::IntegerOctetString.decode(secret.htb)
86
+ point = Bitcoin::Key.new(pubkey: public_key).to_point.multiply_by_scalar(scalar)
87
+ public_key = ECDSA::Format::PointOctetString.encode(point, compression: true)
88
+ Bitcoin.sha256(public_key).bth
89
+ end
90
+
91
+ def self.compute_blinding_factor(public_key, secret)
92
+ Bitcoin.sha256(public_key.htb + secret.htb).bth
93
+ end
94
+
95
+ def self.generate_filler(key_type, shared_secrets, hop_size, max_number_of_hops = MAX_HOPS)
96
+ shared_secrets.inject([]) do |padding, secret|
97
+ key = generate_key(key_type, secret)
98
+ padding1 = padding + [0] * hop_size
99
+ stream = generate_cipher_stream(key, hop_size * (max_number_of_hops + 1))
100
+ stream = stream.reverse[0...padding1.size].reverse.unpack('c*')
101
+ new_padding = xor(padding1, stream)
102
+ new_padding
103
+ end.pack('C*').bth
104
+ end
105
+
106
+ module KeyType
107
+ RHO = 0x72686F
108
+ MU = 0x6d75
109
+ UM = 0x756d
110
+ end
111
+
112
+ def self.hmac256(key, message)
113
+ OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, key, message)
114
+ end
115
+
116
+ # Key generation
117
+ def self.generate_key(key_type, secret)
118
+ hmac256(key_type, secret.htb)
119
+ end
120
+
121
+ def self.generate_cipher_stream(key, length)
122
+ Lightning::Onion::ChaCha20.chacha20_encrypt(key, 0, "\x00" * 12, "\x00" * length)
123
+ end
124
+
125
+ def self.xor(a, b)
126
+ a.zip(b).map { |x, y| ((x ^ y) & 0xff) }
127
+ end
128
+
129
+ def self.mac(key, message)
130
+ hmac256(key, message.pack('C*'))[0...MAC_LENGTH]
131
+ end
132
+
133
+ def self.parse(private_key, raw_packet)
134
+ packet = Lightning::Onion::Packet.parse(raw_packet)
135
+ shared_secret = compute_shared_secret(packet.public_key, private_key)
136
+ rho = generate_key('rho', shared_secret)
137
+ bin = xor(
138
+ (packet.hops_data.map(&:to_payload).join + "\x00" * HOP_LENGTH).unpack('C*'),
139
+ generate_cipher_stream(rho, HOP_LENGTH + MAX_HOPS * HOP_LENGTH).unpack('C*')
140
+ )
141
+ payload = bin[0...PAYLOAD_LENGTH].pack('C*')
142
+ hmac = bin[PAYLOAD_LENGTH...HOP_LENGTH].pack('C*')
143
+ next_hops_data = bin[HOP_LENGTH..-1]
144
+
145
+ next_public_key = make_blind(packet.public_key, compute_blinding_factor(packet.public_key, shared_secret))
146
+ hops_data = []
147
+ 20.times do |i|
148
+ hop_payload = next_hops_data.pack('C*')[i * HOP_LENGTH...(i + 1) * HOP_LENGTH]
149
+ hops_data << Lightning::Onion::HopData.parse(hop_payload)
150
+ end
151
+ [payload, Lightning::Onion::Packet.new(VERSION, next_public_key, hops_data, hmac), shared_secret]
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lightning
4
+ module Onion
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lightning/onion/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lightning-onion'
9
+ spec.version = Lightning::Onion::VERSION
10
+ spec.authors = ['Hajime Yamaguchi']
11
+ spec.email = ['gen.yamaguchi0@gmail.com']
12
+
13
+ spec.summary = 'Onion routing for the Lightning Network.'
14
+ spec.description = 'Onion routing for the Lightning Network.'
15
+ spec.homepage = 'https://github.com/Yamaguchi/lightningrb-onion'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'bitcoinrb'
24
+ spec.add_runtime_dependency 'rbnacl'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightning-onion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hajime Yamaguchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bitcoinrb
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rbnacl
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Onion routing for the Lightning Network.
84
+ email:
85
+ - gen.yamaguchi0@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/lightning/onion.rb
102
+ - lib/lightning/onion/chacha20.rb
103
+ - lib/lightning/onion/hop_data.rb
104
+ - lib/lightning/onion/packet.rb
105
+ - lib/lightning/onion/per_hop.rb
106
+ - lib/lightning/onion/sphinx.rb
107
+ - lib/lightning/onion/version.rb
108
+ - lightningrb-onion.gemspec
109
+ homepage: https://github.com/Yamaguchi/lightningrb-onion
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.11
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Onion routing for the Lightning Network.
133
+ test_files: []