2727/* VERSION is the current version of the data encrypted in the file */
2828#define VERSION ((u64)1)
2929
30- /* Global secret object to keep the derived encryption key for the SCB */
31- static struct secret secret ;
32- static bool peer_backup ;
30+ struct chanbackup {
31+ bool peer_backup ;
32+ /* Global secret object to keep the derived encryption key for the SCB */
33+ struct secret secret ;
34+ };
35+
36+ static struct chanbackup * chanbackup (struct plugin * plugin )
37+ {
38+ return plugin_get_data (plugin , struct chanbackup );
39+ }
3340
3441/* Helper to fetch out SCB from the RPC call */
3542static bool json_to_scb_chan (const char * buffer ,
@@ -65,6 +72,7 @@ static void write_scb(struct plugin *p,
6572 int fd ,
6673 struct modern_scb_chan * * scb_chan_arr )
6774{
75+ const struct chanbackup * cb = chanbackup (p );
6876 u32 timestamp = time_now ().ts .tv_sec ;
6977
7078 u8 * decrypted_scb = towire_static_chan_backup_with_tlvs (tmpctx ,
@@ -83,7 +91,7 @@ static void write_scb(struct plugin *p,
8391
8492 if (crypto_secretstream_xchacha20poly1305_init_push (& crypto_state ,
8593 encrypted_scb ,
86- ( & secret ) -> data ) != 0 )
94+ cb -> secret . data ) != 0 )
8795 {
8896 plugin_err (p , "Can't encrypt the data!" );
8997 return ;
@@ -183,6 +191,7 @@ static u8 *get_file_data(const tal_t *ctx, struct plugin *p)
183191/* Returns decrypted SCB in form of a u8 array */
184192static u8 * decrypt_scb (struct plugin * p )
185193{
194+ const struct chanbackup * cb = chanbackup (p );
186195 u8 * filedata = get_file_data (tmpctx , p );
187196
188197 crypto_secretstream_xchacha20poly1305_state crypto_state ;
@@ -198,7 +207,7 @@ static u8 *decrypt_scb(struct plugin *p)
198207 /* The header part */
199208 if (crypto_secretstream_xchacha20poly1305_init_pull (& crypto_state ,
200209 filedata ,
201- ( & secret ) -> data ) != 0 )
210+ cb -> secret . data ) != 0 )
202211 {
203212 plugin_err (p , "SCB file is corrupted!" );
204213 }
@@ -383,7 +392,7 @@ static struct command_result *peer_after_listdatastore(struct command *cmd,
383392 return command_hook_success (cmd );
384393 struct out_req * req ;
385394
386- if (!peer_backup )
395+ if (!chanbackup ( cmd -> plugin ) -> peer_backup )
387396 return command_hook_success (cmd );
388397
389398 /* BOLT #1:
@@ -495,7 +504,7 @@ static struct command_result *after_listpeers(struct command *cmd,
495504 bool is_connected ;
496505 u8 * serialise_scb ;
497506
498- if (!peer_backup )
507+ if (!chanbackup ( cmd -> plugin ) -> peer_backup )
499508 return notification_or_hook_done (cmd );
500509
501510 /* BOLT #1:
@@ -611,7 +620,7 @@ static struct command_result *peer_connected(struct command *cmd,
611620 const char * err ;
612621 u8 * features ;
613622
614- if (!peer_backup )
623+ if (!chanbackup ( cmd -> plugin ) -> peer_backup )
615624 return command_hook_success (cmd );
616625
617626 serialise_scb = towire_peer_storage (cmd ,
@@ -690,8 +699,9 @@ static struct command_result *handle_your_peer_storage(struct command *cmd,
690699 struct node_id node_id ;
691700 u8 * payload , * payload_deserialise ;
692701 const char * err ;
702+ const struct chanbackup * cb = chanbackup (cmd -> plugin );
693703
694- if (!peer_backup )
704+ if (!cb -> peer_backup )
695705 return command_hook_success (cmd );
696706
697707 err = json_scan (cmd , buf , params ,
@@ -750,7 +760,7 @@ static struct command_result *handle_your_peer_storage(struct command *cmd,
750760 /* The header part */
751761 if (crypto_secretstream_xchacha20poly1305_init_pull (& crypto_state ,
752762 payload_deserialise ,
753- ( & secret ) -> data ) != 0 )
763+ cb -> secret . data ) != 0 )
754764 return failed_peer_restore (cmd , & node_id ,
755765 "Peer altered our data" );
756766
@@ -904,6 +914,7 @@ static const char *init(struct command *init_cmd,
904914 const char * buf UNUSED ,
905915 const jsmntok_t * config UNUSED )
906916{
917+ struct chanbackup * cb = tal (init_cmd -> plugin , struct chanbackup );
907918 struct modern_scb_chan * * scb_chan ;
908919 const char * info = "scb secret" ;
909920 u8 * info_hex = tal_dup_arr (tmpctx , u8 , (u8 * )info , strlen (info ), 0 );
@@ -914,7 +925,7 @@ static const char *init(struct command *init_cmd,
914925 take (json_out_obj (NULL , NULL , NULL )),
915926 "{our_features:{init:%}}" ,
916927 JSON_SCAN_TAL (tmpctx , json_tok_bin_from_hex , & features ));
917- peer_backup = feature_offered (features , OPT_PROVIDE_STORAGE );
928+ cb -> peer_backup = feature_offered (features , OPT_PROVIDE_STORAGE );
918929
919930 rpc_scan (init_cmd , "staticbackup" ,
920931 take (json_out_obj (NULL , NULL , NULL )),
@@ -925,8 +936,9 @@ static const char *init(struct command *init_cmd,
925936 tal_hexstr (tmpctx ,
926937 info_hex ,
927938 tal_bytelen (info_hex )))),
928- "{secret:%}" , JSON_SCAN (json_to_secret , & secret ));
939+ "{secret:%}" , JSON_SCAN (json_to_secret , & cb -> secret ));
929940
941+ plugin_set_data (init_cmd -> plugin , cb );
930942 plugin_log (init_cmd -> plugin , LOG_DBG , "Chanbackup Initialised!" );
931943
932944 /* flush the tmp file, if exists */
0 commit comments