Browse Source

contrib: Rewrite contrib/lightning-pay in Python, support bolt11

fixes #718
ppa-0.6.1
Zhen Zhang 7 years ago
committed by Christian Decker
parent
commit
d1010673b5
  1. 4
      Makefile
  2. 32
      contrib/lightning-pay
  3. 54
      contrib/pylightning/lightning-pay
  4. 1
      contrib/pylightning/setup.py

4
Makefile

@ -248,10 +248,12 @@ check-markdown:
check-spelling: check-spelling:
@tools/check-spelling.sh @tools/check-spelling.sh
PYSRC=$(shell git ls-files "*.py") contrib/pylightning/lightning-pay
check-python: check-python:
@# E501 line too long (N > 79 characters) @# E501 line too long (N > 79 characters)
@# E731 do not assign a lambda expression, use a def @# E731 do not assign a lambda expression, use a def
@git ls-files "*.py" | xargs flake8 --ignore=E501,E731 --exclude=contrib/pylightning/lightning/__init__.py @flake8 --ignore=E501,E731 --exclude=contrib/pylightning/lightning/__init__.py ${PYSRC}
check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python check-source: check-makefile check-source-bolt check-whitespace check-markdown check-spelling check-python

32
contrib/lightning-pay

@ -1,32 +0,0 @@
#! /bin/sh -e
# A terrible script to make a payment.
if [ $# -ne 3 ]; then
echo "Usage: <destination-id> <amount-in-milli-satoshi> <payment-hash>" >&2
exit 1
fi
DEST="$1"
AMOUNT="$2"
PHASH="$3"
if ROUTE=`cli/lightning-cli getroute $DEST $AMOUNT 1`; then
# Strip down to raw array.
ROUTE=`echo $ROUTE | sed 's/^{ "route" : \(.*\) }$/\1/'`
# Get first amount.
PAID=`echo $ROUTE | tr , '\012' | sed -n 's/.*msatoshi[^0-9]*\([0-9]*\).*/\1/p' | head -n1`
echo -n "Paying fee $(($PAID - $AMOUNT)) on $AMOUNT ("`echo "scale=3; ($PAID - $AMOUNT) * 100 / $AMOUNT" | bc`"%). Send [Y/n]? "
read REPLY
case $REPLY in
""|y*|Y)
cli/lightning-cli sendpay "$ROUTE" "$PHASH"
exit
;;
*)
echo Not sending.
exit 1
;;
esac
else
echo Routing failed >&2
exit 1
fi

54
contrib/pylightning/lightning-pay

@ -0,0 +1,54 @@
#!/usr/bin/env python3
import argparse
import os
import sys
from lightning import LightningRpc
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("bolt11_or_destination_id")
parser.add_argument("amount_in_milli_satoshi", default=None, type=int, nargs="?")
parser.add_argument("payment_hash", nargs="?")
args = parser.parse_args()
def default_configdir():
home = os.getenv("HOME")
if home:
return os.path.join(home, ".lightning")
return "."
rpc_path = os.path.join(default_configdir(), "lightning-rpc")
ld = LightningRpc(rpc_path)
assert len(args.bolt11_or_destination_id) > 2, "argument bolt11_or_destination_id is invalid"
prefix = args.bolt11_or_destination_id[:2]
if prefix == "ln":
bolt11 = ld.decodepay(args.bolt11_or_destination_id)
id_ = bolt11["payee"]
payment_hash = bolt11["payment_hash"]
if "msatoshi" in bolt11:
amount = bolt11["msatoshi"]
else:
assert args.amount_in_milli_satoshi, "need argument amount_in_milli_satoshi"
amount = args.amount_in_milli_satoshi
else:
assert args.amount_in_milli_satoshi, "need argument amount_in_milli_satoshi"
assert args.payment_hash, "need argument payment_hash"
amount = args.amount_in_milli_satoshi
id_ = args.bolt11_or_destination_id
payment_hash = args.payment_hash
route = ld.getroute(id_, amount, 1)
fee = route["route"][0]["msatoshi"] - amount
reply = input("Paying fee %s on amount %s (%.3f%%). Send [Y/n]?" % (fee, amount, fee / amount * 100.0))
if reply in ["y", "Y"]:
ld.sendpay(route["route"], payment_hash)
else:
print("Not sending.")
sys.exit(1)

1
contrib/pylightning/setup.py

@ -8,4 +8,5 @@ setup(name='pylightning',
author_email='decker.christian@gmail.com', author_email='decker.christian@gmail.com',
license='MIT', license='MIT',
packages=['lightning'], packages=['lightning'],
scripts=['lightning-pay'],
zip_safe=True) zip_safe=True)

Loading…
Cancel
Save