From 7ae616ef60413428f40a5f77bffdf9576d49dc30 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Tue, 12 Jul 2022 10:55:02 +0100 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 | 7 ++- 4 files changed, 25 insertions(+), 68 deletions(-) diff --git a/doc/lightning-checkmessage.7.md b/doc/lightning-checkmessage.7.md index 3980f8c51..bccdb1073 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:7dcca1fd1708d93b4a0c9b83955630fc4f551c4ffd452fb866c624c72aeaa44d) +[comment]: # ( SHA256STAMP:af2feeb4eddafc509dff150ec4b11225618f1cbbea06ef81f6d97a1bece3e94c) diff --git a/doc/schemas/checkmessage.schema.json b/doc/schemas/checkmessage.schema.json index c4bd81988..0bc52e7e6 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 81b584543..368e18ba3 100644 --- a/lightningd/signmessage.c +++ b/lightningd/signmessage.c @@ -135,9 +135,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 059eacdce..20ff77156 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -2722,9 +2722,12 @@ 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 - assert check_result["verified"] is True \ No newline at end of file + assert check_result["verified"] is True