def create_invoice(self, amount: int, memo: str = "", description_hash: bytes = b"") -> InvoiceResponse: lnd_rpc = lnd_grpc.Client( lnd_dir=None, macaroon_path=self.auth_invoice, tls_cert_path=self.auth_cert, network="mainnet", grpc_host=self.endpoint, grpc_port=self.port, ) lndResponse = lnd_rpc.add_invoice( memo=memo, description_hash=base64.b64encode(description_hash).decode( "ascii"), value=amount, expiry=600, private=True, ) decoded_hash = base64.b64encode( lndResponse.r_hash).decode("utf-8").replace("/", "_") print(lndResponse.r_hash) ok, checking_id, payment_request, error_message = True, decoded_hash, str( lndResponse.payment_request), None return InvoiceResponse(ok, checking_id, payment_request, error_message)
def create_invoice( self, amount: int, memo: Optional[str] = None, description_hash: Optional[bytes] = None) -> InvoiceResponse: lnd_rpc = lnd_grpc.Client( lnd_dir=None, macaroon_path=self.auth_invoice, tls_cert_path=self.auth_cert, network="mainnet", grpc_host=self.endpoint, grpc_port=self.port, ) params: Dict = {"value": amount, "expiry": 600, "private": True} if description_hash: params["description_hash"] = description_hash # as bytes directly else: params["memo"] = memo or "" lndResponse = lnd_rpc.add_invoice(**params) decoded_hash = base64.b64encode( lndResponse.r_hash).decode("utf-8").replace("/", "_") ok, checking_id, payment_request, error_message = True, decoded_hash, str( lndResponse.payment_request), None return InvoiceResponse(ok, checking_id, payment_request, error_message)
def __init__(self, user): self.config = {} self.read_config(user) self.rpc = lnd_grpc.Client(lnd_dir=self.config['lnd_dir'], network=self.config['network'], grpc_host=self.config['grpc_host'], grpc_port=self.config['grpc_port'], macaroon_path=self.config['macaroon_path'])
async def connect(request): lnd_rpc = lnd_grpc.Client( network=config('LND_NETWORK', default='mainnet'), grpc_host=config('LND_GRPC_HOST', default='localhost'), grpc_port=config('LND_GRPC_PORT', default='10009')) try: req = ChannelOpenRequest(**request.query_params) try: query = invites.select().where(invites.c.invite_code == req.k1) res = await database.fetch_one(query) if res: if res['is_used'] == False: next(lnd_rpc.open_channel( node_pubkey=req.remoteid, private=1 if config('LND_FORCE_PRIVATE', cast=bool, default=False) else req.private, # noqa local_funding_amount=res['funding_amount'], push_sat=res['push_amount'], spend_unconfirmed=config('LND_SPEND_UNCONFIRMED', cast=bool, default=True), # noqa sat_per_byte=config('LND_FEE_RATE', cast=int, default=None))) # noqa q = invites.update().where(invites.c.invite_code == req.k1) \ .values( is_used=True, node_id=req.remoteid.hex(), used_at=datetime.utcnow()) await database.execute(q) return UJSONResponse({'status': 'OK'}) else: return UJSONResponse({ 'status': 'ERROR', 'reason': 'this invite code has been used' }) else: return UJSONResponse({ 'status': 'ERROR', 'reason': 'this invite code is invalid' }) except _MultiThreadedRendezvous as e: return UJSONResponse({ 'status': 'ERROR', 'reason': e.details() }) except ValidationError as e: return UJSONResponse({ 'status': 'ERROR', 'reason': 'invalid parameter(s) provided' })
def __init__(self): endpoint = getenv("LND_GRPC_ENDPOINT") self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint self.port = getenv("LND_GRPC_PORT") self.auth_admin = getenv("LND_ADMIN_MACAROON") self.auth_invoice = getenv("LND_INVOICE_MACAROON") self.auth_read = getenv("LND_READ_MACAROON") self.auth_cert = getenv("LND_CERT") lnd_rpc = lnd_grpc.Client(lnd_dir=None, tls_cert_path=self.auth_cert, network='mainnet', grpc_host=self.endpoint, grpc_port=self.port)
def get_invoice_status(self, checking_id: str) -> PaymentStatus: check_id = base64.b64decode(checking_id.replace("_", "/")) print(check_id) lnd_rpc = lnd_grpc.Client( lnd_dir=None, macaroon_path=self.auth_invoice, tls_cert_path=self.auth_cert, network="mainnet", grpc_host=self.endpoint, grpc_port=self.port, ) for _response in lnd_rpc.subscribe_single_invoice(check_id): if _response.state == 1: return PaymentStatus(True) return PaymentStatus(None)
def pay_invoice(self, bolt11: str) -> PaymentResponse: lnd_rpc = lnd_grpc.Client(lnd_dir=None, macaroon_path=self.auth_admin, tls_cert_path=self.auth_cert, network='mainnet', grpc_host=self.endpoint, grpc_port=self.port) payinvoice = lnd_rpc.pay_invoice(payment_request=bolt11, ) ok, checking_id, fee_msat, error_message = True, None, 0, None if payinvoice.payment_error: ok, error_message = False, payinvoice.payment_error else: checking_id = base64.b64encode( payinvoice.payment_hash).decode('utf-8').replace("/", "_") return PaymentResponse(ok, checking_id, fee_msat, error_message)
def get_invoice_status(self, checking_id: str) -> PaymentStatus: check_id = base64.b64decode(checking_id.replace("_", "/")) print(check_id) lnd_rpc = lnd_grpc.Client(lnd_dir=None, macaroon_path=self.auth_invoice, tls_cert_path=self.auth_cert, network='mainnet', grpc_host=self.endpoint, grpc_port=self.port) for _response in lnd_rpc.subscribe_single_invoice(check_id): if _response.state == 1: return PaymentStatus(True) invoiceThread = threading.Thread(target=detectPayment, args=[ lndResponse.check_id, ], daemon=True) invoiceThread.start()
# import the lnd_grpc module import lnd_grpc # instantiate a new gRPC client object lnd = lnd_grpc.Client(network='testnet') # create a random wallet seed # required to start things on lnd backend seed = lnd.gen_seed() # commit the seed and create the new wallet lnd.init_wallet(wallet_password='******', cipher_seed_mnemonic=seed.cipher_seed_mnemonic) # check that the Lightning Servicer is running and we can connect to it lnd.get_info() lnd.wallet_balance() # get a new testnet p2wkh address to receive some coins to lnd.new_address('p2wkh') # connect (but not open channel) to a new peer lnd.connect(address="pubkey@host:port") # Open a channel with a peer print( lnd.open_channel_sync(node_pubkey_string=pubkey, local_funding_amount=amt, push_sat=(amt / 2), spend_unconfirmed=True))