diff --git a/CHANGELOG.md b/CHANGELOG.md index 37dca531c..64e5c433d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - JSON API: `listfunds` now lists a blockheight for confirmed transactions, and has `connected` and `state` fields for channels, like `listpeers`. - JSON API: `fundchannel_start` now includes field `scriptpubkey` - JSON API: New method `listtransactions` +- JSON API: `signmessage` will now create a signature from your node on a message. - Plugin: new notifications `sendpay_success` and `sendpay_failure`. - Protocol: nodes now announce features in `node_announcement` broadcasts. - Protocol: we now offer `option_gossip_queries_ex` for finegrained gossip control. diff --git a/lightningd/Makefile b/lightningd/Makefile index b99b3fd82..8dfff023c 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -96,7 +96,10 @@ LIGHTNINGD_SRC := \ lightningd/subd.c \ lightningd/watch.c -LIGHTNINGD_OBJS := $(LIGHTNINGD_SRC:.c=.o) +LIGHTNINGD_SRC_NOHDR := \ + lightningd/signmessage.c + +LIGHTNINGD_OBJS := $(LIGHTNINGD_SRC:.c=.o) $(LIGHTNINGD_SRC_NOHDR:.c=.o) # Make sure these depend on everything. ALL_OBJS += $(LIGHTNINGD_OBJS) @@ -124,9 +127,9 @@ lightningd/gen_channel_state_names.h: lightningd/channel_state.h ccan/ccan/cdump check-source: $(LIGHTNINGD_SRC:%=check-src-include-order/%) $(LIGHTNINGD_SRC_NOHDR:%=check-src-include-order/%) check-source: $(LIGHTNINGD_HEADERS_NOGEN:%=check-hdr-include-order/%) -check-source-bolt: $(LIGHTNINGD_SRC:%=bolt-check/%) $(LIGHTNINGD_HEADERS_NOGEN:%=bolt-check/%) +check-source-bolt: $(LIGHTNINGD_SRC:%=bolt-check/%) $(LIGHTNINGD_SRC_NOHDR:%=bolt-check/%) $(LIGHTNINGD_HEADERS_NOGEN:%=bolt-check/%) -check-whitespace: $(LIGHTNINGD_SRC:%=check-whitespace/%) $(LIGHTNINGD_HEADERS_NOGEN:%=check-whitespace/%) +check-whitespace: $(LIGHTNINGD_SRC:%=check-whitespace/%) $(LIGHTNINGD_SRC_NOHDR:%=check-whitespace/%) $(LIGHTNINGD_HEADERS_NOGEN:%=check-whitespace/%) check-makefile: check-lightningd-makefile check-lightningd-makefile: diff --git a/lightningd/signmessage.c b/lightningd/signmessage.c new file mode 100644 index 000000000..e5144aeff --- /dev/null +++ b/lightningd/signmessage.c @@ -0,0 +1,60 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct command_result *json_signmessage(struct command *cmd, + const char *buffer, + const jsmntok_t *obj UNNEEDED, + const jsmntok_t *params) +{ + const char *message; + secp256k1_ecdsa_recoverable_signature rsig; + struct json_stream *response; + u8 sig[64], recidu8, *msg; + int recid; + + if (!param(cmd, buffer, params, + p_req("message", param_string, &message), + NULL)) + return command_param_failed(); + + if (strlen(message) > 65535) + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "Message must be < 64k"); + + msg = towire_hsm_sign_message(NULL, + tal_dup_arr(tmpctx, u8, (u8 *)message, + strlen(message), 0)); + if (!wire_sync_write(cmd->ld->hsm_fd, take(msg))) + fatal("Could not write to HSM: %s", strerror(errno)); + + msg = wire_sync_read(tmpctx, cmd->ld->hsm_fd); + if (!fromwire_hsm_sign_message_reply(msg, &rsig)) + fatal("HSM gave bad hsm_sign_message_reply %s", + tal_hex(msg, msg)); + + secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_ctx, + sig, &recid, + &rsig); + response = json_stream_success(cmd); + json_add_hex(response, "signature", sig, sizeof(sig)); + recidu8 = recid; + json_add_hex(response, "recid", &recidu8, sizeof(recidu8)); + return command_success(cmd, response); +} + +static const struct json_command json_signmessage_cmd = { + "signmessage", + "utility", + json_signmessage, + "Create a digital signature of {message}", +}; +AUTODATA(json_command, &json_signmessage_cmd); +