| 317 | } |
| 318 | |
| 319 | static struct command_result *json_makesecret(struct command *cmd, |
| 320 | const char *buffer, |
| 321 | const jsmntok_t *obj UNNEEDED, |
| 322 | const jsmntok_t *params) |
| 323 | { |
| 324 | u8 *data; |
| 325 | const char *strdata; |
| 326 | struct json_stream *response; |
| 327 | struct secret secret; |
| 328 | |
| 329 | if (!param_check(cmd, buffer, params, |
| 330 | p_opt("hex", param_bin_from_hex, &data), |
| 331 | p_opt("string", param_string, &strdata), |
| 332 | NULL)) |
| 333 | return command_param_failed(); |
| 334 | |
| 335 | if (strdata) { |
| 336 | if (data) |
| 337 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 338 | "Cannot have both hex and string"); |
| 339 | data = tal_dup_arr(cmd, u8, (u8 *)strdata, strlen(strdata), 0); |
| 340 | } else { |
| 341 | if (!data) |
| 342 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 343 | "Must have either hex or string"); |
| 344 | } |
| 345 | |
| 346 | if (memeq(data, tal_bytelen(data), |
| 347 | RUNES_SECRET_LABEL, strlen(RUNES_SECRET_LABEL))) |
| 348 | return command_fail(cmd, JSONRPC2_INVALID_PARAMS, |
| 349 | "'%s' is reserved for internal use", |
| 350 | RUNES_SECRET_LABEL); |
| 351 | |
| 352 | if (command_check_only(cmd)) |
| 353 | return command_check_done(cmd); |
| 354 | |
| 355 | u8 *msg = towire_hsmd_derive_secret(cmd, data); |
| 356 | if (!wire_sync_write(cmd->ld->hsm_fd, take(msg))) |
| 357 | return command_fail(cmd, LIGHTNINGD, |
| 358 | "Could not write to HSM: %s", strerror(errno)); |
| 359 | |
| 360 | |
| 361 | msg = wire_sync_read(tmpctx, cmd->ld->hsm_fd); |
| 362 | if (!fromwire_hsmd_derive_secret_reply(msg, &secret)) |
| 363 | return command_fail(cmd, LIGHTNINGD, |
| 364 | "Bad reply from HSM: %s", strerror(errno)); |
| 365 | |
| 366 | |
| 367 | response = json_stream_success(cmd); |
| 368 | json_add_secret(response, "secret", &secret); |
| 369 | return command_success(cmd, response); |
| 370 | } |
| 371 | |
| 372 | static const struct json_command makesecret_command = { |
| 373 | "makesecret", |
nothing calls this directly
no test coverage detected