bwatch: add scriptpubkey watch RPCs
What changed, and why it matters
This commit adds two new internal plugin commands, addscriptpubkeywatch and delscriptpubkeywatch, to the bwatch plugin in Core Lightning. They let lightningd ask bwatch to start or stop monitoring a Bitcoin output script (scriptpubkey) for a specific owner. The change is a straightforward feature addition with no obvious security bug in the diff itself.
No immediate security action is required based on this diff. As with any new RPC surface, ensure the commands are appropriately restricted to lightningd/internal callers and that the later rescan helper is reviewed when it lands.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch registers two new RPC handlers in plugins/bwatch/bwatch.c and implements them in plugins/bwatch/bwatch_interface.c. Both are thin wrappers around existing bwatch_add_watch and bwatch_del_watch functions. Parameters (owner, scriptpubkey hex, start_block for add; owner and scriptpubkey for del) are parsed using the standard param helpers. The commit message notes that historical rescan for start_block <= current_height is handled in a later commit. No input validation, memory handling, or authorization flaws are visible in the supplied diff.
Changed components
plugins/bwatch/bwatch.cplugins/bwatch/bwatch_interface.cplugins/bwatch/bwatch_interface.hInspect captured patch +67 / −1
diff --git a/plugins/bwatch/bwatch.c b/plugins/bwatch/bwatch.c
index 9228c758..6b8cb25e 100644
--- a/plugins/bwatch/bwatch.c
+++ b/plugins/bwatch/bwatch.c
@@ -288,7 +288,8 @@ static const char *init(struct command *cmd,
}
static const struct plugin_command commands[] = {
- /* Subsequent commits register addwatch / delwatch / listwatch here. */
+ { "addscriptpubkeywatch", json_bwatch_add_scriptpubkey },
+ { "delscriptpubkeywatch", json_bwatch_del_scriptpubkey },
};
int main(int argc, char *argv[])
diff --git a/plugins/bwatch/bwatch_interface.c b/plugins/bwatch/bwatch_interface.c
index 41206a47..178f3e81 100644
--- a/plugins/bwatch/bwatch_interface.c
+++ b/plugins/bwatch/bwatch_interface.c
@@ -3,6 +3,7 @@
#include <common/json_parse.h>
#include <common/json_stream.h>
#include <plugins/bwatch/bwatch_interface.h>
+#include <plugins/bwatch/bwatch_store.h>
/*
* ============================================================================
@@ -306,3 +307,59 @@ struct command_result *bwatch_send_chaininfo(struct command *cmd,
json_add_u32(req->js, "last_height", bwatch->current_height);
return send_outreq(req);
}
+
+/*
+ * ============================================================================
+ * RPC COMMAND HANDLERS
+ *
+ * Watch RPCs are thin wrappers over bwatch_add_watch / bwatch_del_watch.
+ * Adding a watch with start_block <= current_height needs a historical
+ * rescan; the helper for that lands in a later commit.
+ * ============================================================================
+ */
+
+/* Register a scriptpubkey watch for `owner` from `start_block` onwards. */
+struct command_result *json_bwatch_add_scriptpubkey(struct command *cmd,
+ const char *buffer,
+ const jsmntok_t *params)
+{
+ struct bwatch *bwatch = bwatch_of(cmd->plugin);
+ const char *owner;
+ u8 *scriptpubkey;
+ u32 *start_block;
+
+ if (!param(cmd, buffer, params,
+ p_req("owner", param_string, &owner),
+ p_req("scriptpubkey", param_bin_from_hex, &scriptpubkey),
+ p_req("start_block", param_u32, &start_block),
+ NULL))
+ return command_param_failed();
+
+ /* New owner is appended to the watch's owner list; same owner
+ * re-adding lowers start_block if needed (rescan handled later). */
+ bwatch_add_watch(cmd, bwatch, WATCH_SCRIPTPUBKEY,
+ NULL, scriptpubkey, NULL, NULL,
+ *start_block, owner);
+ return command_success(cmd, json_out_obj(cmd, NULL, NULL));
+}
+
+/* Drop one owner from a scriptpubkey watch; the watch itself goes away
+ * once the last owner is removed. */
+struct command_result *json_bwatch_del_scriptpubkey(struct command *cmd,
+ const char *buffer,
+ const jsmntok_t *params)
+{
+ struct bwatch *bwatch = bwatch_of(cmd->plugin);
+ const char *owner;
+ u8 *scriptpubkey;
+
+ if (!param(cmd, buffer, params,
+ p_req("owner", param_string, &owner),
+ p_req("scriptpubkey", param_bin_from_hex, &scriptpubkey),
+ NULL))
+ return command_param_failed();
+
+ bwatch_del_watch(cmd, bwatch, WATCH_SCRIPTPUBKEY,
+ NULL, scriptpubkey, NULL, NULL, owner);
+ return command_success(cmd, json_out_obj(cmd, "removed", "true"));
+}
diff --git a/plugins/bwatch/bwatch_interface.h b/plugins/bwatch/bwatch_interface.h
index 8e946cfe..8d705312 100644
--- a/plugins/bwatch/bwatch_interface.h
+++ b/plugins/bwatch/bwatch_interface.h
@@ -29,6 +29,14 @@ void bwatch_send_watch_revert(struct command *cmd,
* normal chain-poll loop afterwards. */
struct command_result *bwatch_send_chaininfo(struct command *cmd, void *unused);
+/* RPC handlers: add / remove a scriptpubkey watch. */
+struct command_result *json_bwatch_add_scriptpubkey(struct command *cmd,
+ const char *buffer,
+ const jsmntok_t *params);
+struct command_result *json_bwatch_del_scriptpubkey(struct command *cmd,
+ const char *buffer,
+ const jsmntok_t *params);
+
/* Send a block_processed RPC to watchman after a new block has been
* persisted. The next poll is started from the ack callback so we don't
* race ahead of watchman's view of the chain. Chains on the same poll
Why this scored 11/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.