@@ -360,6 +360,7 @@ static struct io_plan *init_hsm(struct io_conn *conn,
360360 struct sha256 * force_channel_secrets_shaseed ;
361361 struct secret * hsm_encryption_key ;
362362 struct secret hsm_secret ;
363+ bool warmstart ;
363364
364365 /* This must be lightningd. */
365366 assert (is_lightningd (c ));
@@ -406,9 +407,12 @@ static struct io_plan *init_hsm(struct io_conn *conn,
406407 randombytes_buf (& hsm_secret , sizeof (hsm_secret ));
407408 }
408409
410+ /* Is this a warm start (restart) or a cold start (first time)? */
411+ warmstart = access ("INITED" , F_OK ) != -1 ;
412+
409413 proxy_stat rv = proxy_init_hsm (& bip32_key_version , chainparams ,
410- & hsm_secret , & node_id ,
411- & pubstuff .bip32 );
414+ warmstart , & hsm_secret ,
415+ & node_id , & pubstuff .bip32 );
412416 if (PROXY_PERMANENT (rv )) {
413417 status_failed (STATUS_FAIL_INTERNAL_ERROR ,
414418 "proxy_%s failed: %s" , __FUNCTION__ ,
@@ -422,6 +426,11 @@ static struct io_plan *init_hsm(struct io_conn *conn,
422426 proxy_last_message ());
423427 }
424428
429+ /* Mark this node as already inited. */
430+ int fd = open ("INITED" , O_WRONLY |O_TRUNC |O_CREAT , 0666 );
431+ assert (fd != -1 );
432+ close (fd );
433+
425434 return req_reply (conn , c ,
426435 take (towire_hsm_init_reply (NULL , & node_id , & pubstuff .bip32 )));
427436}
@@ -1116,6 +1125,84 @@ static struct io_plan *pass_client_hsmfd(struct io_conn *conn,
11161125 send_pending_client_fd , c );
11171126}
11181127
1128+ /*~ This is used to declare a new channel. */
1129+ static struct io_plan * handle_new_channel (struct io_conn * conn ,
1130+ struct client * c ,
1131+ const u8 * msg_in )
1132+ {
1133+ struct node_id peer_id ;
1134+ u64 dbid ;
1135+
1136+ if (!fromwire_hsm_new_channel (msg_in , & peer_id , & dbid ))
1137+ return bad_req (conn , c , msg_in );
1138+
1139+ proxy_stat rv = proxy_handle_new_channel (& peer_id , dbid );
1140+ if (PROXY_PERMANENT (rv ))
1141+ status_failed (STATUS_FAIL_INTERNAL_ERROR ,
1142+ "proxy_%s failed: %s" , __FUNCTION__ ,
1143+ proxy_last_message ());
1144+ else if (!PROXY_SUCCESS (rv ))
1145+ return bad_req_fmt (conn , c , msg_in ,
1146+ "proxy_%s error: %s" , __FUNCTION__ ,
1147+ proxy_last_message ());
1148+
1149+ return req_reply (conn , c ,
1150+ take (towire_hsm_new_channel_reply (NULL )));
1151+ }
1152+
1153+ /*~ This is used to provide all unchanging public channel parameters. */
1154+ static struct io_plan * handle_ready_channel (struct io_conn * conn ,
1155+ struct client * c ,
1156+ const u8 * msg_in )
1157+ {
1158+ bool is_outbound ;
1159+ struct amount_sat channel_value ;
1160+ struct bitcoin_txid funding_txid ;
1161+ u16 funding_txout ;
1162+ u16 local_to_self_delay ;
1163+ u8 * local_shutdown_script ;
1164+ struct basepoints remote_basepoints ;
1165+ struct pubkey remote_funding_pubkey ;
1166+ u16 remote_to_self_delay ;
1167+ u8 * remote_shutdown_script ;
1168+ bool option_static_remotekey ;
1169+
1170+ if (!fromwire_hsm_ready_channel (tmpctx , msg_in , & is_outbound ,
1171+ & channel_value , & funding_txid ,
1172+ & funding_txout , & local_to_self_delay ,
1173+ & local_shutdown_script ,
1174+ & remote_basepoints ,
1175+ & remote_funding_pubkey ,
1176+ & remote_to_self_delay ,
1177+ & remote_shutdown_script ,
1178+ & option_static_remotekey ))
1179+ return bad_req (conn , c , msg_in );
1180+
1181+ proxy_stat rv = proxy_handle_ready_channel (
1182+ & c -> id , c -> dbid ,
1183+ is_outbound ,
1184+ & channel_value ,
1185+ & funding_txid ,
1186+ funding_txout ,
1187+ local_to_self_delay ,
1188+ local_shutdown_script ,
1189+ & remote_basepoints ,
1190+ & remote_funding_pubkey ,
1191+ remote_to_self_delay ,
1192+ remote_shutdown_script ,
1193+ option_static_remotekey );
1194+ if (PROXY_PERMANENT (rv ))
1195+ status_failed (STATUS_FAIL_INTERNAL_ERROR ,
1196+ "proxy_%s failed: %s" , __FUNCTION__ ,
1197+ proxy_last_message ());
1198+ else if (!PROXY_SUCCESS (rv ))
1199+ return bad_req_fmt (conn , c , msg_in ,
1200+ "proxy_%s error: %s" , __FUNCTION__ ,
1201+ proxy_last_message ());
1202+ return req_reply (conn , c ,
1203+ take (towire_hsm_ready_channel_reply (NULL )));
1204+ }
1205+
11191206/*~ lightningd asks us to sign a withdrawal; same as above but in theory
11201207 * we can do more to check the previous case is valid. */
11211208static struct io_plan * handle_sign_withdrawal_tx (struct io_conn * conn ,
@@ -1361,6 +1448,7 @@ static bool check_client_capabilities(struct client *client,
13611448
13621449 case WIRE_HSM_GET_PER_COMMITMENT_POINT :
13631450 case WIRE_HSM_CHECK_FUTURE_SECRET :
1451+ case WIRE_HSM_READY_CHANNEL :
13641452 return (client -> capabilities & HSM_CAP_COMMITMENT_POINT ) != 0 ;
13651453
13661454 case WIRE_HSM_SIGN_REMOTE_COMMITMENT_TX :
@@ -1371,6 +1459,7 @@ static bool check_client_capabilities(struct client *client,
13711459 return (client -> capabilities & HSM_CAP_SIGN_CLOSING_TX ) != 0 ;
13721460
13731461 case WIRE_HSM_INIT :
1462+ case WIRE_HSM_NEW_CHANNEL :
13741463 case WIRE_HSM_CLIENT_HSMFD :
13751464 case WIRE_HSM_SIGN_FUNDING :
13761465 case WIRE_HSM_SIGN_WITHDRAWAL :
@@ -1388,6 +1477,8 @@ static bool check_client_capabilities(struct client *client,
13881477 case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY :
13891478 case WIRE_HSM_CUPDATE_SIG_REPLY :
13901479 case WIRE_HSM_CLIENT_HSMFD_REPLY :
1480+ case WIRE_HSM_NEW_CHANNEL_REPLY :
1481+ case WIRE_HSM_READY_CHANNEL_REPLY :
13911482 case WIRE_HSM_SIGN_FUNDING_REPLY :
13921483 case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY :
13931484 case WIRE_HSM_SIGN_WITHDRAWAL_REPLY :
@@ -1428,6 +1519,12 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
14281519 case WIRE_HSM_CLIENT_HSMFD :
14291520 return pass_client_hsmfd (conn , c , c -> msg_in );
14301521
1522+ case WIRE_HSM_NEW_CHANNEL :
1523+ return handle_new_channel (conn , c , c -> msg_in );
1524+
1525+ case WIRE_HSM_READY_CHANNEL :
1526+ return handle_ready_channel (conn , c , c -> msg_in );
1527+
14311528 case WIRE_HSM_GET_CHANNEL_BASEPOINTS :
14321529 return handle_get_channel_basepoints (conn , c , c -> msg_in );
14331530
@@ -1496,6 +1593,8 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
14961593 case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY :
14971594 case WIRE_HSM_CUPDATE_SIG_REPLY :
14981595 case WIRE_HSM_CLIENT_HSMFD_REPLY :
1596+ case WIRE_HSM_NEW_CHANNEL_REPLY :
1597+ case WIRE_HSM_READY_CHANNEL_REPLY :
14991598 case WIRE_HSM_SIGN_FUNDING_REPLY :
15001599 case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY :
15011600 case WIRE_HSM_SIGN_WITHDRAWAL_REPLY :
0 commit comments