Пример #1
0
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
Пример #2
0
    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
Пример #3
0
    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
Пример #4
0
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
Пример #5
0
    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()
Пример #6
0
    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
Пример #7
0
    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
Пример #8
0
    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()