| 164 | } |
| 165 | |
| 166 | void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db) |
| 167 | { |
| 168 | struct db_stmt *stmt; |
| 169 | |
| 170 | stmt = db_prepare_v2(db, SQL("SELECT" |
| 171 | " type" |
| 172 | ", keyindex" |
| 173 | ", prev_out_tx" |
| 174 | ", prev_out_index" |
| 175 | ", channel_id" |
| 176 | ", peer_id" |
| 177 | ", commitment_point" |
| 178 | " FROM outputs" |
| 179 | " WHERE scriptpubkey IS NULL;")); |
| 180 | |
| 181 | db_query_prepared(stmt); |
| 182 | while (db_step(stmt)) { |
| 183 | int type; |
| 184 | u8 *scriptPubkey; |
| 185 | struct bitcoin_txid txid; |
| 186 | u32 outnum, keyindex; |
| 187 | struct pubkey key; |
| 188 | struct db_stmt *update_stmt; |
| 189 | |
| 190 | type = db_col_int(stmt, "type"); |
| 191 | keyindex = db_col_int(stmt, "keyindex"); |
| 192 | db_col_txid(stmt, "prev_out_tx", &txid); |
| 193 | outnum = db_col_int(stmt, "prev_out_index"); |
| 194 | |
| 195 | /* This indiciates whether or not we have 'close_info' */ |
| 196 | if (!db_col_is_null(stmt, "channel_id")) { |
| 197 | struct pubkey *commitment_point; |
| 198 | struct node_id peer_id; |
| 199 | u64 channel_id; |
| 200 | u8 *msg; |
| 201 | |
| 202 | channel_id = db_col_u64(stmt, "channel_id"); |
| 203 | db_col_node_id(stmt, "peer_id", &peer_id); |
| 204 | commitment_point = db_col_optional(stmt, stmt, "commitment_point", pubkey); |
| 205 | |
| 206 | /* Have to go ask the HSM to derive the pubkey for us */ |
| 207 | msg = towire_hsmd_get_output_scriptpubkey(NULL, |
| 208 | channel_id, |
| 209 | &peer_id, |
| 210 | commitment_point); |
| 211 | if (!wire_sync_write(ld->hsm_fd, take(msg))) |
| 212 | fatal("Could not write to HSM: %s", strerror(errno)); |
| 213 | msg = wire_sync_read(stmt, ld->hsm_fd); |
| 214 | if (!fromwire_hsmd_get_output_scriptpubkey_reply(stmt, msg, |
| 215 | &scriptPubkey)) |
| 216 | fatal("HSM gave bad hsm_get_output_scriptpubkey_reply %s", |
| 217 | tal_hex(msg, msg)); |
| 218 | } else { |
| 219 | db_col_ignore(stmt, "peer_id"); |
| 220 | db_col_ignore(stmt, "commitment_point"); |
| 221 | bip32_pubkey(ld, &key, keyindex); |
| 222 | if (type == WALLET_OUTPUT_P2SH_WPKH) { |
| 223 | u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(stmt, &key); |
nothing calls this directly
no test coverage detected