|
7 | 7 | #include <channeld/channeld_wiregen.h>
|
8 | 8 | #include <common/blockheight_states.h>
|
9 | 9 | #include <common/fee_states.h>
|
| 10 | +#include <common/memleak.h> |
10 | 11 | #include <common/onionreply.h>
|
11 | 12 | #include <common/trace.h>
|
12 | 13 | #include <db/bindings.h>
|
@@ -64,6 +65,111 @@ static enum state_change state_change_in_db(enum state_change s)
|
64 | 65 | fatal("%s: %u is invalid", __func__, s);
|
65 | 66 | }
|
66 | 67 |
|
| 68 | +/* We keep a hash of these, for fast lookup */ |
| 69 | +struct wallet_address { |
| 70 | + u32 index; |
| 71 | + enum addrtype addrtype; |
| 72 | + const u8 *scriptpubkey; |
| 73 | +}; |
| 74 | + |
| 75 | +static const u8 *wallet_address_keyof(const struct wallet_address *waddr) |
| 76 | +{ |
| 77 | + return waddr->scriptpubkey; |
| 78 | +} |
| 79 | + |
| 80 | +static bool wallet_address_eq_scriptpubkey(const struct wallet_address *waddr, |
| 81 | + const u8 *scriptpubkey) |
| 82 | +{ |
| 83 | + return tal_arr_eq(waddr->scriptpubkey, scriptpubkey); |
| 84 | +} |
| 85 | + |
| 86 | +HTABLE_DEFINE_NODUPS_TYPE(struct wallet_address, |
| 87 | + wallet_address_keyof, |
| 88 | + scriptpubkey_hash, |
| 89 | + wallet_address_eq_scriptpubkey, |
| 90 | + wallet_address_htable); |
| 91 | + |
| 92 | +static void our_addresses_add(struct wallet_address_htable *our_addresses, |
| 93 | + u32 index, |
| 94 | + const u8 *scriptpubkey TAKES, |
| 95 | + enum addrtype addrtype) |
| 96 | +{ |
| 97 | + struct wallet_address *waddr = tal(our_addresses, struct wallet_address); |
| 98 | + |
| 99 | + waddr->index = index; |
| 100 | + waddr->addrtype = addrtype; |
| 101 | + waddr->scriptpubkey = tal_dup_talarr(waddr, u8, scriptpubkey); |
| 102 | + wallet_address_htable_add(our_addresses, waddr); |
| 103 | +} |
| 104 | + |
| 105 | +static void our_addresses_add_for_index(struct wallet *w, u32 i) |
| 106 | +{ |
| 107 | + struct ext_key ext; |
| 108 | + enum addrtype addrtype; |
| 109 | + const u8 *scriptpubkey; |
| 110 | + const u32 flags = BIP32_FLAG_KEY_PUBLIC | BIP32_FLAG_SKIP_HASH; |
| 111 | + |
| 112 | + if (bip32_key_from_parent(w->ld->bip32_base, i, |
| 113 | + flags, &ext) != WALLY_OK) { |
| 114 | + abort(); |
| 115 | + } |
| 116 | + |
| 117 | + /* If we don't know (prior to 24.11), just add all |
| 118 | + * possibilities. */ |
| 119 | + /* FIXME: We could deprecate P2SH once we don't see |
| 120 | + * any, since we stopped publishing them in 24.02 */ |
| 121 | + if (!wallet_get_addrtype(w, i, &addrtype)) { |
| 122 | + scriptpubkey = scriptpubkey_p2wpkh_derkey(NULL, ext.pub_key); |
| 123 | + our_addresses_add(w->our_addresses, |
| 124 | + i, |
| 125 | + take(scriptpubkey_p2sh(NULL, scriptpubkey)), |
| 126 | + ADDR_P2SH_SEGWIT); |
| 127 | + our_addresses_add(w->our_addresses, |
| 128 | + i, |
| 129 | + take(scriptpubkey), |
| 130 | + ADDR_BECH32); |
| 131 | + scriptpubkey = scriptpubkey_p2tr_derkey(NULL, ext.pub_key); |
| 132 | + our_addresses_add(w->our_addresses, |
| 133 | + i, |
| 134 | + take(scriptpubkey), |
| 135 | + ADDR_P2TR); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + switch (addrtype) { |
| 140 | + /* This doesn't happen */ |
| 141 | + case ADDR_P2SH_SEGWIT: |
| 142 | + abort(); |
| 143 | + case ADDR_BECH32: |
| 144 | + case ADDR_ALL: |
| 145 | + scriptpubkey = scriptpubkey_p2wpkh_derkey(NULL, ext.pub_key); |
| 146 | + our_addresses_add(w->our_addresses, |
| 147 | + i, |
| 148 | + take(scriptpubkey), |
| 149 | + ADDR_BECH32); |
| 150 | + if (addrtype != ADDR_ALL) |
| 151 | + return; |
| 152 | + /* Fall thru */ |
| 153 | + case ADDR_P2TR: |
| 154 | + scriptpubkey = scriptpubkey_p2tr_derkey(NULL, ext.pub_key); |
| 155 | + our_addresses_add(w->our_addresses, |
| 156 | + i, |
| 157 | + take(scriptpubkey), |
| 158 | + ADDR_P2TR); |
| 159 | + return; |
| 160 | + } |
| 161 | + abort(); |
| 162 | +} |
| 163 | + |
| 164 | +static void our_addresses_init(struct wallet *w) |
| 165 | +{ |
| 166 | + w->our_addresses_maxindex = 0; |
| 167 | + w->our_addresses = tal(w, struct wallet_address_htable); |
| 168 | + wallet_address_htable_init(w->our_addresses); |
| 169 | + |
| 170 | + our_addresses_add_for_index(w, w->our_addresses_maxindex); |
| 171 | +} |
| 172 | + |
67 | 173 | static void outpointfilters_init(struct wallet *w)
|
68 | 174 | {
|
69 | 175 | struct db_stmt *stmt;
|
@@ -114,6 +220,10 @@ struct wallet *wallet_new(struct lightningd *ld, struct timers *timers)
|
114 | 220 | outpointfilters_init(wallet);
|
115 | 221 | trace_span_end(wallet);
|
116 | 222 |
|
| 223 | + trace_span_start("our_addresses_init", wallet); |
| 224 | + our_addresses_init(wallet); |
| 225 | + trace_span_end(wallet); |
| 226 | + |
117 | 227 | db_commit_transaction(wallet->db);
|
118 | 228 | return wallet;
|
119 | 229 | }
|
@@ -6420,6 +6530,7 @@ void wallet_memleak_scan(struct htable *memtable, const struct wallet *w)
|
6420 | 6530 | {
|
6421 | 6531 | memleak_scan_outpointfilter(memtable, w->utxoset_outpoints);
|
6422 | 6532 | memleak_scan_outpointfilter(memtable, w->owned_outpoints);
|
| 6533 | + memleak_scan_htable(memtable, &w->our_addresses->raw); |
6423 | 6534 | }
|
6424 | 6535 |
|
6425 | 6536 | struct issued_address_type *wallet_list_addresses(const tal_t *ctx, struct wallet *wallet,
|
|
0 commit comments