| 634 | } |
| 635 | |
| 636 | bool wallet_can_spend(struct wallet *w, const u8 *script, |
| 637 | u32 *index, bool *output_is_p2sh) |
| 638 | { |
| 639 | struct ext_key ext; |
| 640 | u64 bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0); |
| 641 | u32 i; |
| 642 | |
| 643 | /* If not one of these, can't be for us. */ |
| 644 | if (is_p2sh(script, NULL)) |
| 645 | *output_is_p2sh = true; |
| 646 | else if (is_p2wpkh(script, NULL)) |
| 647 | *output_is_p2sh = false; |
| 648 | else |
| 649 | return false; |
| 650 | |
| 651 | for (i = 0; i <= bip32_max_index + w->keyscan_gap; i++) { |
| 652 | u8 *s; |
| 653 | |
| 654 | if (bip32_key_from_parent(w->bip32_base, i, |
| 655 | BIP32_FLAG_KEY_PUBLIC, &ext) |
| 656 | != WALLY_OK) { |
| 657 | abort(); |
| 658 | } |
| 659 | s = scriptpubkey_p2wpkh_derkey(w, ext.pub_key); |
| 660 | if (*output_is_p2sh) { |
| 661 | u8 *p2sh = scriptpubkey_p2sh(w, s); |
| 662 | tal_free(s); |
| 663 | s = p2sh; |
| 664 | } |
| 665 | if (scripteq(s, script)) { |
| 666 | /* If we found a used key in the keyscan_gap we should |
| 667 | * remember that. */ |
| 668 | if (i > bip32_max_index) |
| 669 | db_set_intvar(w->db, "bip32_max_index", i); |
| 670 | tal_free(s); |
| 671 | *index = i; |
| 672 | return true; |
| 673 | } |
| 674 | tal_free(s); |
| 675 | } |
| 676 | return false; |
| 677 | } |
| 678 | |
| 679 | s64 wallet_get_newindex(struct lightningd *ld) |
| 680 | { |
no test coverage detected