Class: Lightning::Invoice::Message

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



17
18
19
# File 'lib/lightning/invoice.rb', line 17

def initialize
  @routing_info = []
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



14
15
16
# File 'lib/lightning/invoice.rb', line 14

def amount
  @amount
end

#descriptionObject

Returns the value of attribute description.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def description
  @description
end

#description_hashObject

Returns the value of attribute description_hash.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def description_hash
  @description_hash
end

#expiryObject

Returns the value of attribute expiry.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def expiry
  @expiry
end

#fallback_addressObject

Returns the value of attribute fallback_address.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def fallback_address
  @fallback_address
end

#min_final_cltv_expiryObject

Returns the value of attribute min_final_cltv_expiry.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def min_final_cltv_expiry
  @min_final_cltv_expiry
end

#multiplierObject

Returns the value of attribute multiplier.



14
15
16
# File 'lib/lightning/invoice.rb', line 14

def multiplier
  @multiplier
end

#payment_hashObject

Returns the value of attribute payment_hash.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def payment_hash
  @payment_hash
end

#prefixObject

Returns the value of attribute prefix.



14
15
16
# File 'lib/lightning/invoice.rb', line 14

def prefix
  @prefix
end

#pubkeyObject

Returns the value of attribute pubkey.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def pubkey
  @pubkey
end

#routing_infoObject

Returns the value of attribute routing_info.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def routing_info
  @routing_info
end

#signatureObject

Returns the value of attribute signature.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def signature
  @signature
end

#timestampObject

Returns the value of attribute timestamp.



15
16
17
# File 'lib/lightning/invoice.rb', line 15

def timestamp
  @timestamp
end

Instance Method Details

#fallback_address_type(fallback_address) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/lightning/invoice.rb', line 91

def fallback_address_type(fallback_address)
  address_types = {
    'lnbc' => [0, 5],
    'lntb' => [111, 196]
  }

  hrp, data = Bech32.decode(fallback_address)
  if hrp
    0
  else
    decoded = [Bitcoin::Base58.decode(fallback_address)].pack("H*").unpack("C*")
    if decoded[0] == address_types[prefix][0]
      17
    elsif decoded[0] == address_types[prefix][1]
      18
    end
  end
end

#to_bech32Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lightning/invoice.rb', line 21

def to_bech32
  human = +''
  human << prefix
  human << amount.to_s if amount && amount > 0
  human << multiplier if multiplier
  data = []
  data += Invoice.int_to_array(timestamp)
  if payment_hash && !payment_hash.empty?
    data += [1]
    data += [1, 20]
    data += Invoice.buffer_to_word(payment_hash.htb)
  end
  if description && !description.empty?
    data += [13]
    description_word = Invoice.buffer_to_word(description)
    data += Invoice.int_to_array(description_word.size)
    data += description_word
  end
  if pubkey && !pubkey.empty?
    data += [19]
    data += [1, 21]
    data += Invoice.buffer_to_word(pubkey.htb)
  end
  if description_hash && !description_hash.empty?
    data += [23]
    data += [1, 20]
    data += Invoice.buffer_to_word(description_hash.htb)
  end
  if expiry
    data += [6]
    expiry_word = Invoice.int_to_array(expiry)
    data += Invoice.int_to_array(expiry_word.size)
    data += expiry_word
  end
  if min_final_cltv_expiry
    data += [24]
    min_final_cltv_expiry_word = Invoice.int_to_array(min_final_cltv_expiry)
    data += Invoice.int_to_array(min_final_cltv_expiry_word.size)
    data += min_final_cltv_expiry_word
  end
  if fallback_address && !fallback_address.empty?
    data += [9]
    type = fallback_address_type(fallback_address)
    case type
    when 0
      _, data_part = Bech32.decode(fallback_address)
      data += Invoice.int_to_array(data_part.size)
      data += data_part
    when 17, 18
      decoded = Bitcoin::Base58.decode(fallback_address)
      decoded = decoded.htb[1...-4]
      decoded = Invoice.buffer_to_word(decoded)
      data += Invoice.int_to_array(decoded.size + 1)
      data << type
      data += decoded
    end
  end
  if routing_info && !routing_info.empty?
    data += [3]
    data += Invoice.int_to_array(82 * routing_info.size)
    tmp = []
    routing_info.each do |r|
      tmp += r.to_array
    end
    data += Invoice.buffer_to_word(tmp.pack("C*"))
  end
  data += Invoice.buffer_to_word(signature.htb)
  Bech32.encode(human, data)
end