From cf7617ea7294d08f5e66b29b992a28e27f5165f8 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Thu, 30 Jun 2022 22:05:30 +0000 Subject: [PATCH] rpc: improve error format Signed-off-by: Vincenzo Palazzo --- doc/lightning-checkmessage.7.md | 11 ++--- doc/schemas/checkmessage.schema.json | 66 +++++----------------------- lightningd/signmessage.c | 9 ++-- tests/test_misc.py | 5 ++- 4 files changed, 24 insertions(+), 67 deletions(-) diff --git a/doc/lightning-checkmessage.7.md b/doc/lightning-checkmessage.7.md index 6bbc48341f56..bccdb107385d 100644 --- a/doc/lightning-checkmessage.7.md +++ b/doc/lightning-checkmessage.7.md @@ -29,13 +29,8 @@ RETURN VALUE [comment]: # (GENERATE-FROM-SCHEMA-START) On success, an object is returned, containing: -- **verified** (boolean): Whether the signature was valid - -If **verified** is *true*: - - **pubkey** (pubkey): the *pubkey* parameter, or the pubkey found by looking for known nodes - -If **verified** is *false*: - - **pubkey** (pubkey): the *pubkey* (if any) which could have signed this; this is usually not useful! +- **verified** (boolean): whether the signature was valid (always *true*) +- **pubkey** (pubkey): the *pubkey* parameter, or the pubkey found by looking for known nodes [comment]: # (GENERATE-FROM-SCHEMA-END) @@ -54,4 +49,4 @@ RESOURCES Main web site: -[comment]: # ( SHA256STAMP:6df0e61e28118786861aacc073e3289268fe1b00837c3fd02a537aa13e5acae5) +[comment]: # ( SHA256STAMP:af2feeb4eddafc509dff150ec4b11225618f1cbbea06ef81f6d97a1bece3e94c) diff --git a/doc/schemas/checkmessage.schema.json b/doc/schemas/checkmessage.schema.json index c4bd81988416..0bc52e7e667a 100644 --- a/doc/schemas/checkmessage.schema.json +++ b/doc/schemas/checkmessage.schema.json @@ -2,65 +2,21 @@ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "required": [ - "verified" + "verified", + "pubkey" ], - "additionalProperties": true, + "additionalProperties": false, "properties": { "verified": { "type": "boolean", - "description": "Whether the signature was valid" - } - }, - "allOf": [ - { - "if": { - "properties": { - "verified": { - "type": "boolean", - "enum": [ - true - ] - } - } - }, - "then": { - "additionalProperties": false, - "required": [ - "pubkey" - ], - "properties": { - "verified": {}, - "pubkey": { - "type": "pubkey", - "description": "the *pubkey* parameter, or the pubkey found by looking for known nodes" - } - } - } + "enum": [ + true + ], + "description": "whether the signature was valid" }, - { - "if": { - "properties": { - "verified": { - "type": "boolean", - "enum": [ - false - ] - } - } - }, - "then": { - "additionalProperties": false, - "required": [ - "pubkey" - ], - "properties": { - "verified": {}, - "pubkey": { - "type": "pubkey", - "description": "the *pubkey* (if any) which could have signed this; this is usually not useful!" - } - } - } + "pubkey": { + "type": "pubkey", + "description": "the *pubkey* parameter, or the pubkey found by looking for known nodes" } - ] + } } diff --git a/lightningd/signmessage.c b/lightningd/signmessage.c index ec218dc42d83..a6089fe41f59 100644 --- a/lightningd/signmessage.c +++ b/lightningd/signmessage.c @@ -138,9 +138,12 @@ static void listnodes_done(const char *buffer, t = json_get_member(buffer, t, "nodes"); if (!deprecated_apis && (!t || t->size == 0)) { - was_pending(command_fail(can->cmd, SIGNMESSAGE_PUBKEY_NOT_FOUND, - "pub key not found in the graph, expected pubkey is %s", - node_id_to_hexstr(tmpctx, &can->id))); + struct json_stream *response; + response = json_stream_fail(can->cmd, SIGNMESSAGE_PUBKEY_NOT_FOUND, + "pubkey not found in the graph"); + json_add_node_id(response, "claimed_key", &can->id); + json_object_end(response); + was_pending(command_failed(can->cmd, response)); return; } response = json_stream_success(can->cmd); diff --git a/tests/test_misc.py b/tests/test_misc.py index 7deab26bd2e0..154d8a7afb10 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2661,8 +2661,11 @@ def test_checkmessage_pubkey_not_found(node_factory): pubkey = "03be3b0e9992153b1d5a6e1623670b6c3663f72ce6cf2e0dd39c0a373a7de5a3b7" zbase = "d66bqz3qsku5fxtqsi37j11pci47ydxa95iusphutggz9ezaxt56neh77kxe5hyr41kwgkncgiu94p9ecxiexgpgsz8daoq4tw8kj8yx" - with pytest.raises(RpcError, match="not found in the graph, expected pubkey is {}".format(pubkey)): + with pytest.raises(RpcError) as exception: l1.rpc.checkmessage(msg, zbase) + err = exception.value + assert err.error['message'] == "pubkey not found in the graph" + assert err.error['data']['claimed_key'] == pubkey check_result = l1.rpc.checkmessage(msg, zbase, pubkey=pubkey) assert check_result["pubkey"] == pubkey