| 1065 | } |
| 1066 | |
| 1067 | void fillin_missing_scriptpubkeys(struct lightningd *ld, struct db *db, |
| 1068 | const struct migration_context *mc) |
| 1069 | { |
| 1070 | struct db_stmt *stmt; |
| 1071 | |
| 1072 | stmt = db_prepare_v2(db, SQL("SELECT" |
| 1073 | " type" |
| 1074 | ", keyindex" |
| 1075 | ", prev_out_tx" |
| 1076 | ", prev_out_index" |
| 1077 | ", channel_id" |
| 1078 | ", peer_id" |
| 1079 | ", commitment_point" |
| 1080 | " FROM outputs" |
| 1081 | " WHERE scriptpubkey IS NULL;")); |
| 1082 | |
| 1083 | db_query_prepared(stmt); |
| 1084 | while (db_step(stmt)) { |
| 1085 | int type; |
| 1086 | u8 *scriptPubkey; |
| 1087 | struct bitcoin_txid txid; |
| 1088 | u32 outnum, keyindex; |
| 1089 | struct pubkey key; |
| 1090 | struct db_stmt *update_stmt; |
| 1091 | |
| 1092 | type = db_col_int(stmt, "type"); |
| 1093 | keyindex = db_col_int(stmt, "keyindex"); |
| 1094 | db_col_txid(stmt, "prev_out_tx", &txid); |
| 1095 | outnum = db_col_int(stmt, "prev_out_index"); |
| 1096 | |
| 1097 | /* This indiciates whether or not we have 'close_info' */ |
| 1098 | if (!db_col_is_null(stmt, "channel_id")) { |
| 1099 | struct pubkey *commitment_point; |
| 1100 | struct node_id peer_id; |
| 1101 | u64 channel_id; |
| 1102 | u8 *msg; |
| 1103 | |
| 1104 | channel_id = db_col_u64(stmt, "channel_id"); |
| 1105 | db_col_node_id(stmt, "peer_id", &peer_id); |
| 1106 | if (!db_col_is_null(stmt, "commitment_point")) { |
| 1107 | commitment_point = tal(stmt, struct pubkey); |
| 1108 | db_col_pubkey(stmt, "commitment_point", commitment_point); |
| 1109 | } else |
| 1110 | commitment_point = NULL; |
| 1111 | |
| 1112 | /* Have to go ask the HSM to derive the pubkey for us */ |
| 1113 | msg = towire_hsmd_get_output_scriptpubkey(NULL, |
| 1114 | channel_id, |
| 1115 | &peer_id, |
| 1116 | commitment_point); |
| 1117 | if (!wire_sync_write(ld->hsm_fd, take(msg))) |
| 1118 | fatal("Could not write to HSM: %s", strerror(errno)); |
| 1119 | msg = wire_sync_read(stmt, ld->hsm_fd); |
| 1120 | if (!fromwire_hsmd_get_output_scriptpubkey_reply(stmt, msg, |
| 1121 | &scriptPubkey)) |
| 1122 | fatal("HSM gave bad hsm_get_output_scriptpubkey_reply %s", |
| 1123 | tal_hex(msg, msg)); |
| 1124 | } else { |
nothing calls this directly
no test coverage detected