Class: Lnrpc::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lnrpc/client.rb

Constant Summary collapse

LND_HOME_DIR =
ENV['LND_HOME'] || "~/.lnd"
DEFAULT_ADDRESS =
'localhost:10009'
DEFAULT_CREDENTIALS_PATH =
"#{LND_HOME_DIR}/tls.cert"
DEFAULT_MACAROON_PATH =
"#{LND_HOME_DIR}/data/chain/bitcoin/mainnet/admin.macaroon"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lnrpc/client.rb', line 12

def initialize(options={})
  self.address = options[:address] || DEFAULT_ADDRESS

  if options.has_key?(:credentials)
    self.credentials = options[:credentials]
  elsif File.exists?(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
    self.credentials = ::File.read(::File.expand_path(options[:credentials_path] || DEFAULT_CREDENTIALS_PATH))
  else
    self.credentials = nil
  end

  if !options.has_key?(:macaroon) && File.exists?(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH))
    options[:macaroon] = ::File.read(::File.expand_path(options[:macaroon_path] || DEFAULT_MACAROON_PATH)).unpack("H*")
  end
  self.macaroon = options[:macaroon]
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def address
  @address
end

#credentialsObject

Returns the value of attribute credentials.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def credentials
  @credentials
end

#macaroonObject

Returns the value of attribute macaroon.



5
6
7
# File 'lib/lnrpc/client.rb', line 5

def macaroon
  @macaroon
end

Instance Method Details

#inspectObject



56
57
58
# File 'lib/lnrpc/client.rb', line 56

def inspect
  "#{self.to_s} @address=\"#{self.address}\""
end

#keysend(args) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/lnrpc/client.rb', line 43

def keysend(args)
  args[:dest_custom_records] ||= {}
  args[:dest_custom_records][Lnrpc::KEY_SEND_PREIMAGE_TYPE] ||= Lnrpc.create_preimage
  args[:payment_hash] ||= Digest::SHA256.digest(args[:dest_custom_records][Lnrpc::KEY_SEND_PREIMAGE_TYPE])
  args[:timeout_seconds] ||= 60
  router.send_payment_v2(args)
end

#lightningObject



29
30
31
32
33
34
# File 'lib/lnrpc/client.rb', line 29

def lightning
  @lightning ||= GrpcWrapper.new(Lnrpc::Lightning::Stub.new(address,
                                                GRPC::Core::ChannelCredentials.new(credentials),
                                                interceptors: [Lnrpc::MacaroonInterceptor.new(macaroon)]
                                              ))
end

#pay(args) ⇒ Object



51
52
53
54
# File 'lib/lnrpc/client.rb', line 51

def pay(args)
  args[:timeout_seconds] ||= 60
  router.send_payment_v2(args)
end

#routerObject



36
37
38
39
40
41
# File 'lib/lnrpc/client.rb', line 36

def router
  @router ||= GrpcWrapper.new(Routerrpc::Router::Stub.new(address,
                                                GRPC::Core::ChannelCredentials.new(credentials),
                                                interceptors: [Lnrpc::MacaroonInterceptor.new(macaroon)]
                                              ))
end