Last active
April 16, 2019 01:11
-
-
Save dgnsrekt/52a845b95e3584a8dc7e1a50be6b5301 to your computer and use it in GitHub Desktop.
lnd_grpc hodl invoice testing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lnd_grpc import Client | |
from random import randint | |
from hashlib import sha256 | |
from time import sleep | |
def create_random_hash(): | |
n = str(randint(0, 1e32)) | |
hash = sha256(n.encode()) | |
return hash.digest(), n | |
lnd_readonly = Client( | |
macaroon_path="readonly.macaroon", | |
tls_cert_path="tls.cert", | |
network="testnet", | |
grpc_host="192.168.1.47", | |
grpc_port="10009", | |
) | |
print(lnd_readonly.get_info()) | |
print(lnd_readonly.get_network_info()) | |
print(lnd_readonly.list_channels()) | |
lnd_invoices = Client( | |
macaroon_path="invoices.macaroon", | |
tls_cert_path="tls.cert", | |
network="testnet", | |
grpc_host="192.168.1.47", | |
grpc_port="10009", | |
) | |
HASH, PREIMAGE = create_random_hash() | |
MEMO = "TESTING HODL INVOICES" | |
SATOSHIS = 10 | |
print("HASH", HASH) | |
print("PREIMAGE", PREIMAGE) | |
invoice = lnd_invoices.add_hold_invoice(memo=MEMO, hash=HASH, value=SATOSHIS) | |
invoice_info = lnd_readonly.subscribe_single_invoice(r_hash=HASH) | |
for status in invoice_info: | |
print("status:", status.state) | |
print(status) | |
if status.state == 3: | |
print("ACCEPTED") | |
print("VERIFYING SOMETHING", end="") | |
for _ in range(10): | |
print(".", end="", flush=True) | |
sleep(1) | |
else: | |
print() | |
if randint(0, 1): | |
print("EVERYTHING LOOKS GOOD. SETTLING PAYMENT.") | |
sleep(1) | |
lnd_invoices.settle_invoice(preimage=PREIMAGE.encode()) | |
print("PAYMENT SETTLED!") | |
sleep(1) | |
else: | |
print("OH NO! SOMETHING WENT WRONG. CANCELING PAYMENT.") | |
sleep(1) | |
lnd_invoices.cancel_invoice(HASH) | |
print("PAYMENT CANCELED!") | |
sleep(1) | |
break | |
else: | |
print(status) | |
sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For testnet off chain bitcoin use.
https://htlc.me/