mirror of
				https://github.com/bumi/lnrpc
				synced 2025-10-31 00:41:01 +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 | ||||
|   class Error < StandardError; end | ||||
|   autoload :Client, 'lnrpc/client' | ||||
|   autoload :MacaroonInterceptor, 'lnrpc/macaroon_interceptor' | ||||
| end | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -1,3 +1,5 @@ | ||||
| require "grpc" | ||||
| require "lnrpc/macaroon_interceptor" | ||||
| module Lnrpc | ||||
|   class Client | ||||
|     attr_accessor :address, :credentials, :macaroon, :grpc_client | ||||
| @ -38,7 +40,10 @@ module Lnrpc | ||||
|       end | ||||
|       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 | ||||
| 
 | ||||
|     def pay(payreq) | ||||
| @ -48,10 +53,9 @@ module Lnrpc | ||||
|     def method_missing(m, *args, &block) | ||||
|       if self.grpc_client.respond_to?(m) | ||||
|         params  = args[0] | ||||
|         options = args[1] || { metadata: { macaroon: self.macaroon } } | ||||
| 
 | ||||
|         request = params.nil? ? request_class_for(m).new : request_class_for(m).new(params) | ||||
|         self.grpc_client.send(m, request, options) | ||||
|         args[0] = params.nil? ? request_class_for(m).new : request_class_for(m).new(params) | ||||
|         self.grpc_client.send(m, *args) | ||||
|       else | ||||
|         super | ||||
|       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