-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive.py
115 lines (98 loc) · 3.07 KB
/
receive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
from dotenv import load_dotenv
import requests
import qrcode
load_dotenv()
auth_token = os.getenv("API_KEY")
def get_wallet_id(auth_token):
url = "https://api.blink.sv/graphql"
headers = {
"content-type": "application/json",
"X-API-KEY": auth_token,
}
query = """
query Me {
me {
defaultAccount {
wallets {
id
walletCurrency
balance
}
}
}
}
"""
response = requests.post(url, json={"query": query}, headers=headers)
if response.status_code == 200:
data = response.json()
wallets = data["data"]["me"]["defaultAccount"]["wallets"]
for wallet in wallets:
if wallet["walletCurrency"] == "BTC":
return wallet["id"]
print("BTC wallet not found.")
return None
else:
print("Failed to fetch wallet ID. Status code:", response.status_code)
print("Response:", response.text)
return None
def create_lightning_invoice(auth_token, wallet_id, amount_satoshis):
url = "https://api.blink.sv/graphql"
headers = {
"content-type": "application/json",
"X-API-KEY": auth_token,
}
query = """
mutation LnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) {
invoice {
paymentRequest
paymentHash
paymentSecret
satoshis
}
errors {
message
}
}
}
"""
variables = {
"input": {
"amount": amount_satoshis,
"walletId": wallet_id
}
}
response = requests.post(url, json={"query": query, "variables": variables}, headers=headers)
if response.status_code == 200:
data = response.json()
if "errors" in data["data"]["lnInvoiceCreate"] and data["data"]["lnInvoiceCreate"]["errors"]:
print("Error:", data["data"]["lnInvoiceCreate"]["errors"])
else:
return data["data"]["lnInvoiceCreate"]["invoice"]
else:
print("Failed to connect to API. Status code:", response.status_code)
print("Response:", response.text)
return None
def display_qr_code(payment_request):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(payment_request)
qr.make(fit=True)
img = qr.make_image(fill="black", back_color="white")
img.show()
wallet_id = get_wallet_id(auth_token)
if wallet_id:
amount_satoshis = int(input("Enter the amount in satoshis: "))
invoice = create_lightning_invoice(auth_token, wallet_id, amount_satoshis)
if invoice:
print("Invoice created successfully:")
print("Payment Request:", invoice["paymentRequest"])
print("Payment Hash:", invoice["paymentHash"])
print("Payment Secret:", invoice["paymentSecret"])
print("Satoshis:", invoice["satoshis"])
display_qr_code(invoice["paymentRequest"])