mirror of
				https://github.com/bumi/lnrpc
				synced 2025-10-31 08:42:32 +00:00 
			
		
		
		
	Use proper grpc requres_response interceptor for macaroon metadata
...so much to learn :)
This commit is contained in:
		
							parent
							
								
									251a8c94da
								
							
						
					
					
						commit
						39f97e69ca
					
				| @ -4,6 +4,7 @@ require "lnrpc/rpc_services_pb" | |||||||
| module Lnrpc | module Lnrpc | ||||||
|   class Error < StandardError; end |   class Error < StandardError; end | ||||||
|   autoload :Client, 'lnrpc/client' |   autoload :Client, 'lnrpc/client' | ||||||
|  |   autoload :MacaroonInterceptor, 'lnrpc/macaroon_interceptor' | ||||||
| end | end | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,3 +1,5 @@ | |||||||
|  | require "grpc" | ||||||
|  | require "lnrpc/macaroon_interceptor" | ||||||
| module Lnrpc | module Lnrpc | ||||||
|   class Client |   class Client | ||||||
|     attr_accessor :address, :credentials, :macaroon, :grpc_client |     attr_accessor :address, :credentials, :macaroon, :grpc_client | ||||||
| @ -38,7 +40,10 @@ module Lnrpc | |||||||
|       end |       end | ||||||
|       self.macaroon = options[:macaroon] |       self.macaroon = options[:macaroon] | ||||||
| 
 | 
 | ||||||
|       self.grpc_client = Lnrpc::Lightning::Stub.new(self.address, GRPC::Core::ChannelCredentials.new(self.credentials)) |       self.grpc_client = Lnrpc::Lightning::Stub.new(self.address, | ||||||
|  |                                                     GRPC::Core::ChannelCredentials.new(self.credentials), | ||||||
|  |                                                     interceptors: [Lnrpc::MacaroonInterceptor.new(self.macaroon)] | ||||||
|  |                                                    ) | ||||||
|     end |     end | ||||||
| 
 | 
 | ||||||
|     def pay(payreq) |     def pay(payreq) | ||||||
| @ -48,10 +53,9 @@ module Lnrpc | |||||||
|     def method_missing(m, *args, &block) |     def method_missing(m, *args, &block) | ||||||
|       if self.grpc_client.respond_to?(m) |       if self.grpc_client.respond_to?(m) | ||||||
|         params  = args[0] |         params  = args[0] | ||||||
|         options = args[1] || { metadata: { macaroon: self.macaroon } } |  | ||||||
| 
 | 
 | ||||||
|         request = params.nil? ? request_class_for(m).new : request_class_for(m).new(params) |         args[0] = params.nil? ? request_class_for(m).new : request_class_for(m).new(params) | ||||||
|         self.grpc_client.send(m, request, options) |         self.grpc_client.send(m, *args) | ||||||
|       else |       else | ||||||
|         super |         super | ||||||
|       end |       end | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								lib/lnrpc/macaroon_interceptor.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								lib/lnrpc/macaroon_interceptor.rb
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | |||||||
|  | require "grpc" | ||||||
|  | 
 | ||||||
|  | module Lnrpc | ||||||
|  |   class MacaroonInterceptor < GRPC::ClientInterceptor | ||||||
|  |     def initialize(macaroon_hex) | ||||||
|  |       @macaroon = macaroon_hex | ||||||
|  |     end | ||||||
|  | 
 | ||||||
|  |     def request_response(request:, call:, method:, metadata:) | ||||||
|  |       metadata['macaroon'] = @macaroon | ||||||
|  |       yield | ||||||
|  |     end | ||||||
|  |   end | ||||||
|  | end | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user