diff --git a/daemon/invoice.c b/daemon/invoice.c index 4b1c4a372..70d8709c5 100644 --- a/daemon/invoice.c +++ b/daemon/invoice.c @@ -125,3 +125,43 @@ const struct json_command invoice_command = { "Create invoice for {msatoshi} with {label} (with a set {r}, otherwise generate one)", "Returns the {rhash} on success. " }; + +static void json_listinvoice(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct invoice *i; + jsmntok_t *label = NULL; + struct json_result *response = new_json_result(cmd); + + if (!json_get_params(buffer, params, + "?label", &label, + NULL)) { + command_fail(cmd, "Invalid arguments"); + return; + } + + + json_object_start(response, NULL); + json_array_start(response, NULL); + list_for_each(&cmd->dstate->invoices, i, list) { + if (label && !json_tok_streq(buffer, label, i->label)) + continue; + json_object_start(response, NULL); + json_add_string(response, "label", i->label); + json_add_hex(response, "rhash", &i->rhash, sizeof(i->rhash)); + json_add_u64(response, "msatoshi", i->msatoshi); + json_add_bool(response, "complete", i->complete); + json_object_end(response); + } + json_array_end(response); + json_object_end(response); + command_success(cmd, response); +} + +const struct json_command listinvoice_command = { + "listinvoice", + json_listinvoice, + "Show invoice {label} (or all, if no {label}))", + "Returns an array of {label}, {rhash}, {msatoshi} and {complete} on success. " +}; + diff --git a/daemon/jsonrpc.c b/daemon/jsonrpc.c index 3ee34171c..d9a22e15d 100644 --- a/daemon/jsonrpc.c +++ b/daemon/jsonrpc.c @@ -290,6 +290,7 @@ static const struct json_command *cmdlist[] = { &close_command, &newaddr_command, &invoice_command, + &listinvoice_command, &getroute_command, &sendpay_command, &feerate_command, diff --git a/daemon/jsonrpc.h b/daemon/jsonrpc.h index 96fe85aba..388d28de2 100644 --- a/daemon/jsonrpc.h +++ b/daemon/jsonrpc.h @@ -72,6 +72,7 @@ extern const struct json_command disconnect_command; extern const struct json_command signcommit_command; extern const struct json_command output_command; extern const struct json_command invoice_command; +extern const struct json_command listinvoice_command; extern const struct json_command add_route_command; extern const struct json_command routefail_command; extern const struct json_command getroute_command; diff --git a/daemon/test/test.sh b/daemon/test/test.sh index 33bdfa935..64a017629 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -961,6 +961,9 @@ lcli1 dev-routefail true lcli2 dev-routefail true RHASH3=`lcli2 invoice $HTLC_AMOUNT RHASH3 | sed 's/.*"\([0-9a-f]*\)".*/\1/'` +lcli2 listinvoice +[ "`lcli2 listinvoice | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH3 , rhash : $RHASH3 , msatoshi : $HTLC_AMOUNT, complete : false } ] } " ] + HTLCID3=`lcli1 newhtlc $ID2 $HTLC_AMOUNT $EXPIRY $RHASH3 | extract_id` [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 @@ -973,11 +976,19 @@ A_AMOUNT=$(($A_AMOUNT - $HTLC_AMOUNT)) B_AMOUNT=$(($B_AMOUNT + $HTLC_AMOUNT)) check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE "" +[ "`lcli2 listinvoice | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH3 , rhash : $RHASH3 , msatoshi : $HTLC_AMOUNT, complete : true } ] } " ] + # Now, failed payment (didn't pay enough) RHASH4=`lcli2 invoice $HTLC_AMOUNT RHASH4 | sed 's/.*"\([0-9a-f]*\)".*/\1/'` # Shouldn't have this already. -if lcli2 getlog | $FGREP 'Short payment for HTLC'; then exit 1; fi +if lcli2 getlog | $FGREP 'Short payment for'; then exit 1; fi + +# Test listinvoice with both, or subset (either order possible!) +INVOICES=`lcli2 listinvoice | tr -s '\012\011\" ' ' '` +[ "$INVOICES" = "{ [ { label : RHASH3 , rhash : $RHASH3 , msatoshi : $HTLC_AMOUNT, complete : true }, { label : RHASH4 , rhash : $RHASH4 , msatoshi : $HTLC_AMOUNT, complete : false } ] } " ] || [ "$INVOICES" = "{ [ { label : RHASH4 , rhash : $RHASH4 , msatoshi : $HTLC_AMOUNT, complete : false }, { label : RHASH3 , rhash : $RHASH3 , msatoshi : $HTLC_AMOUNT, complete : true } ] } " ] +[ "`lcli2 listinvoice RHASH3 | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH3 , rhash : $RHASH3 , msatoshi : $HTLC_AMOUNT, complete : true } ] } " ] +[ "`lcli2 listinvoice RHASH4 | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH4 , rhash : $RHASH4 , msatoshi : $HTLC_AMOUNT, complete : false } ] } " ] HTLCID4=`lcli1 newhtlc $ID2 $(($HTLC_AMOUNT - 1)) $EXPIRY $RHASH4 | extract_id` [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 @@ -986,7 +997,7 @@ HTLCID4=`lcli1 newhtlc $ID2 $(($HTLC_AMOUNT - 1)) $EXPIRY $RHASH4 | extract_id` [ ! -n "$MANUALCOMMIT" ] || lcli2 commit $ID1 [ ! -n "$MANUALCOMMIT" ] || lcli1 commit $ID2 -check lcli2 "getlog | $FGREP 'Short payment for HTLC'" +check lcli2 "getlog | $FGREP 'Short payment for'" check_status $A_AMOUNT $A_FEE "" $B_AMOUNT $B_FEE "" if [ ! -n "$MANUALCOMMIT" ]; then @@ -1039,6 +1050,7 @@ if [ ! -n "$MANUALCOMMIT" ]; then fi fi + [ "`lcli3 listinvoice RHASH5 | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH5 , rhash : $RHASH5 , msatoshi : $HTLC_AMOUNT, complete : false } ] } " ] # Pay correctly. lcli1 sendpay "$ROUTE" $RHASH5 @@ -1057,6 +1069,8 @@ if [ ! -n "$MANUALCOMMIT" ]; then fi fi + [ "`lcli3 listinvoice RHASH5 | tr -s '\012\011\" ' ' '`" = "{ [ { label : RHASH5 , rhash : $RHASH5 , msatoshi : $HTLC_AMOUNT, complete : true } ] } " ] + # Can't pay twice (try from node2) ROUTE2=`lcli2 getroute $ID3 $HTLC_AMOUNT 1` ROUTE2=`echo $ROUTE2 | sed 's/^{ "route" : \(.*\) }$/\1/'`