def get_ln_stub(): def metadata_callback(context, callback): # for more info see grpc docs callback([('macaroon', macaroon)], None) os.environ["GRPC_SSL_CIPHER_SUITES"] = 'HIGH+ECDSA' # Lnd admin macaroon is at ~/.lnd/data/chain/bitcoin/simnet/admin.macaroon on Linux and # ~/Library/Application Support/Lnd/data/chain/bitcoin/simnet/admin.macaroon on Mac with open(os.path.expanduser(settings.MACAROON_PATH), 'rb') as f: macaroon_bytes = f.read() macaroon = codecs.encode(macaroon_bytes, 'hex') cert = open(settings.CERT_PATH, 'rb').read() creds = grpc.ssl_channel_credentials(cert) # now build meta data credentials auth_creds = grpc.metadata_call_credentials(metadata_callback) # combine the cert credentials and the macaroon auth credentials # such that every call is properly encrypted and authenticated combined_creds = grpc.composite_channel_credentials(creds, auth_creds) # channel = grpc.secure_channel(settings.LND_RPCHOST, creds) channel = grpc.secure_channel(settings.LND_RPCHOST, combined_creds) # stub.GetInfo(ln.GetInfoRequest(), metadata=[('macaroon', macaroon)]) stub = lnrpc.LightningStub(channel) return stub
def check_payment(self): """ Checks if the Lightning payment has been received for this invoice """ if self.status == 'pending_invoice': return False os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" cert = open('/home/tom/.lnd/tls.cert').read() creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:10002', creds) # channel = grpc.insecure_channel(settings.LND_RPCHOST) stub = lnrpc.LightningStub(channel) r_hash_base64 = self.r_hash.encode('utf-8') r_hash_bytes = str(codecs.decode(r_hash_base64, 'base64')) invoice_resp = stub.LookupInvoice(ln.PaymentHash(r_hash=r_hash_bytes)) if invoice_resp.settled: # Payment complete self.status = 'complete' self.save() return True else: # Payment not received return False
def authenticate(self, request, signature, csrf_token, username=None): creds = grpc.ssl_channel_credentials(open(settings.CERT_PATH).read()) channel = grpc.secure_channel(settings.LND_RPCHOST, creds) stub = lnrpc.LightningStub(channel) verifymessage_resp = stub.VerifyMessage( ln.VerifyMessageRequest(msg=csrf_token, signature=signature)) if not verifymessage_resp.valid: print "Invalid signature" return None pubkey = verifymessage_resp.pubkey # Try fetching an existing profile try: profile = Profile.objects.get(identity_pubkey=pubkey) return profile.user except Profile.DoesNotExist: # Create a new profile if they provided a username if len(username) > 3 and len(username) < 36: user = User(username=username) user.save() profile, created = Profile.objects.get_or_create( user=user, identity_pubkey=pubkey) assert created is True # TODO Auth them in else: raise Exception("No username provided") return user
def stub(): creds = grpc.ssl_channel_credentials(open(settings.CERT_PATH).read()) channel = grpc.secure_channel(settings.LND_RPCHOST, creds) print(channel) stub = lnrpc.LightningStub(channel) print(stub) return stub
def generate_invoice(self, user, article): """ Generates a new invoice """ assert self.status == 'pending_invoice', "Already generated invoice" channel = grpc.insecure_channel(settings.LND_RPCHOST) stub = lnrpc.LightningStub(channel) add_invoice_resp = stub.AddInvoice( ln.Invoice(value=settings.MIN_VIEW_AMOUNT, memo="User '{}' | ArticleId {}".format( user.username, article.id))) r_hash_base64 = codecs.encode(add_invoice_resp.r_hash, 'base64') self.r_hash = r_hash_base64.decode('utf-8') self.payment_request = add_invoice_resp.payment_request self.status = 'pending_payment' self.save()
def check_payment(self): """ Checks if the Lightning payment has been received for this invoice """ if self.status == 'pending_invoice': return False channel = grpc.insecure_channel(settings.LND_RPCHOST) stub = lnrpc.LightningStub(channel) r_hash_base64 = self.r_hash.encode('utf-8') r_hash_bytes = str(codecs.decode(r_hash_base64, 'base64')) invoice_resp = stub.LookupInvoice(ln.PaymentHash(r_hash=r_hash_bytes)) if invoice_resp.settled: # Payment complete self.status = 'complete' self.save() return True else: # Payment not received return False
def authenticate(self, request, signature, csrf_token, username=None): os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" cert = open('/home/tom/.lnd/tls.cert').read() creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:10002', creds) # Create a new 'stub' object that will allow us to interact with our "Bob" lnd node. stub = lnrpc.LightningStub(channel) # channel = grpc.insecure_channel(settings.LND_RPCHOST) # stub = lnrpc.LightningStub(channel) verifymessage_resp = stub.VerifyMessage(ln.VerifyMessageRequest(msg=csrf_token, signature=signature)) if not verifymessage_resp.valid: print "Invalid signature" return None pubkey = verifymessage_resp.pubkey # Try fetching an existing profile try: profile = Profile.objects.get(identity_pubkey=pubkey) return profile.user except Profile.DoesNotExist: # Create a new profile if they provided a username if len(username) > 3 and len(username) < 36: user = User(username=username) user.save() profile, created = Profile.objects.get_or_create( user=user, identity_pubkey=pubkey) assert created is True # TODO Auth them in else: raise Exception("No username provided") return user
def generate_invoice(self, user, article): """ Generates a new invoice """ assert self.status == 'pending_invoice', "Already generated invoice" os.environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" cert = open('/home/tom/.lnd/tls.cert').read() creds = grpc.ssl_channel_credentials(cert) channel = grpc.secure_channel('localhost:10002', creds) # Create a new 'stub' object that will allow us to interact with our "Bob" lnd node. # channel = grpc.insecure_channel(settings.LND_RPCHOST) stub = lnrpc.LightningStub(channel) add_invoice_resp = stub.AddInvoice( ln.Invoice(value=settings.MIN_VIEW_AMOUNT, memo="User '{}' | ArticleId {}".format( user.username, article.id))) r_hash_base64 = codecs.encode(add_invoice_resp.r_hash, 'base64') self.r_hash = r_hash_base64.decode('utf-8') self.payment_request = add_invoice_resp.payment_request self.status = 'pending_payment' self.save()