Class: Lightning::Onion::Packet

Inherits:
Object
  • Object
show all
Defined in:
lib/lightning/onion/packet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, public_key, routing_info, hmac) ⇒ Packet

Returns a new instance of Packet.



8
9
10
11
12
13
14
# File 'lib/lightning/onion/packet.rb', line 8

def initialize(version, public_key, routing_info, hmac)
  @version = version
  @public_key = public_key
  @routing_info = routing_info
  raise "invalid size #{routing_info.size}" unless routing_info.size == Packet.routing_bytesize * 2
  @hmac = hmac
end

Instance Attribute Details

#hmacObject

Returns the value of attribute hmac.



6
7
8
# File 'lib/lightning/onion/packet.rb', line 6

def hmac
  @hmac
end

#public_keyObject

Returns the value of attribute public_key.



6
7
8
# File 'lib/lightning/onion/packet.rb', line 6

def public_key
  @public_key
end

#routing_infoObject

Returns the value of attribute routing_info.



6
7
8
# File 'lib/lightning/onion/packet.rb', line 6

def routing_info
  @routing_info
end

#versionObject

Returns the value of attribute version.



6
7
8
# File 'lib/lightning/onion/packet.rb', line 6

def version
  @version
end

Class Method Details

.parse(payload) ⇒ Object



20
21
22
23
24
25
# File 'lib/lightning/onion/packet.rb', line 20

def self.parse(payload)
  version, public_key, rest = payload.unpack('aH66a*')
  routing_info = rest[0...routing_bytesize].bth
  hmac = rest[routing_bytesize..-1].bth
  new(version, public_key, routing_info, hmac)
end

.routing_bytesizeObject



16
17
18
# File 'lib/lightning/onion/packet.rb', line 16

def self.routing_bytesize
  Lightning::Onion::Sphinx::MAX_HOPS * Lightning::Onion::Sphinx::HOP_LENGTH
end

Instance Method Details

#last?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/lightning/onion/packet.rb', line 35

def last?
  hmac == '00' * 32
end

#to_payloadObject



27
28
29
30
31
32
33
# File 'lib/lightning/onion/packet.rb', line 27

def to_payload
  payload = +''
  payload << [version, public_key].pack('aH66')
  payload << routing_info.htb
  payload << hmac.htb
  payload
end