MCPcopy Create free account
hub / github.com/ElementsProject/lightning / guess_to_remote

Function guess_to_remote

tools/hsmtool.c:360–420  ·  view source on GitHub ↗

In case of an unilateral close from the remote side while we suffered a * loss of data, this tries to recover the private key from the `to_remote` * output. * This basically iterates over every `dbid` to derive the channel_seed and * then derives the payment basepoint to compare to the pubkey hash specified * in the witness programm. * Note that since a node generates the key for the to_remo

Source from the content-addressed store, hash-verified

358 * :param passwd: The *optional* hsm_secret password
359 */
360static int guess_to_remote(const char *address, struct node_id *node_id,
361 u64 tries, char *hsm_secret_path)
362{
363 struct secret hsm_secret, channel_seed, basepoint_secret;
364 char *passwd, *err;
365 struct pubkey basepoint;
366 struct ripemd160 pubkeyhash;
367 /* We only support P2WPKH, hence 20. */
368 u8 goal_pubkeyhash[20];
369 /* See common/bech32.h for buffer size. */
370 char hrp[strlen(address) - 6];
371 int witver, exit_code = 0;
372 size_t witlen;
373
374 /* Get the hrp to accept addresses from any network. */
375 if (bech32_decode(hrp, goal_pubkeyhash, &witlen, address, 90) != BECH32_ENCODING_BECH32)
376 errx(ERROR_USAGE, "Could not get address' network");
377 if (segwit_addr_decode(&witver, goal_pubkeyhash, &witlen, hrp, address) != 1)
378 errx(ERROR_USAGE, "Wrong bech32 address");
379
380 secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
381 | SECP256K1_CONTEXT_SIGN);
382
383 /* This checks the file existence, too. */
384 if (hsm_secret_is_encrypted(hsm_secret_path)) {
385 printf("Enter hsm_secret password:\n");
386 fflush(stdout);
387 passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
388 if (!passwd)
389 errx(exit_code, "%s", err);
390 get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd);
391 free(passwd);
392 } else
393 get_hsm_secret(&hsm_secret, hsm_secret_path);
394
395 for (u64 dbid = 1; dbid < tries ; dbid++) {
396 get_channel_seed(&channel_seed, node_id, dbid, &hsm_secret);
397 if (!derive_payment_basepoint(&channel_seed,
398 &basepoint, &basepoint_secret))
399 errx(ERROR_KEYDERIV, "Could not derive basepoints for dbid %"PRIu64
400 " and channel seed %s.", dbid,
401 type_to_string(tmpctx,
402 struct secret, &channel_seed));
403
404 pubkey_to_hash160(&basepoint, &pubkeyhash);
405 if (memcmp(pubkeyhash.u.u8, goal_pubkeyhash, 20) == 0) {
406 printf("bech32 : %s\n", address);
407 printf("pubkey hash : %s\n",
408 tal_hexstr(tmpctx, pubkeyhash.u.u8, 20));
409 printf("pubkey : %s \n",
410 type_to_string(tmpctx, struct pubkey, &basepoint));
411 printf("privkey : %s \n",
412 type_to_string(tmpctx, struct secret, &basepoint_secret));
413 return 0;
414 }
415 }
416
417 printf("Could not find any basepoint matching the provided witness programm.\n"

Callers 1

mainFunction · 0.85

Calls 12

errxFunction · 0.85
hsm_secret_is_encryptedFunction · 0.85
get_encrypted_hsm_secretFunction · 0.85
get_hsm_secretFunction · 0.85
derive_payment_basepointFunction · 0.85
tal_hexstrFunction · 0.85
get_channel_seedFunction · 0.70
bech32_decodeFunction · 0.50
segwit_addr_decodeFunction · 0.50
type_to_stringClass · 0.50
pubkey_to_hash160Function · 0.50

Tested by

no test coverage detected