lib/lightning/onion/packet.rb in lightning-onion-0.2.5 vs lib/lightning/onion/packet.rb in lightning-onion-0.2.6

- old
+ new

@@ -1,26 +1,28 @@ # frozen_string_literal: true module Lightning module Onion class Packet - include Lightning::Onion::Sphinx - attr_accessor :version, :public_key, :routing_info, :hmac 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 == MAX_HOPS * HOP_LENGTH * 2 + raise "invalid size #{routing_info.size}" unless routing_info.size == Packet.routing_bytesize * 2 @hmac = hmac end + def self.routing_bytesize + Lightning::Onion::Sphinx::MAX_HOPS * Lightning::Onion::Sphinx::HOP_LENGTH + end + def self.parse(payload) version, public_key, rest = payload.unpack('aH66a*') - routing_info = rest[0...MAX_HOPS * HOP_LENGTH].bth - hmac = rest[MAX_HOPS * HOP_LENGTH..-1].bth + routing_info = rest[0...routing_bytesize].bth + hmac = rest[routing_bytesize..-1].bth new(version, public_key, routing_info, hmac) end def to_payload payload = +'' @@ -29,10 +31,10 @@ payload << hmac.htb payload end def last? - hmac == "\x00" * 32 + hmac == '00' * 32 end end end end