MCPcopy Create free account
hub / github.com/ElementsProject/lightning / write_scb

Function write_scb

plugins/chanbackup.c:140–186  ·  view source on GitHub ↗

This writes encrypted static backup in the recovery file */

Source from the content-addressed store, hash-verified

138
139/* This writes encrypted static backup in the recovery file */
140static void write_scb(struct plugin *p,
141 int fd,
142 struct modern_scb_chan **scb_chan_arr)
143{
144 const struct chanbackup *cb = chanbackup(p);
145 u32 timestamp = clock_time().ts.tv_sec;
146
147 u8 *decrypted_scb = towire_static_chan_backup_with_tlvs(tmpctx,
148 VERSION,
149 timestamp,
150 cast_const2(const struct modern_scb_chan **,
151 scb_chan_arr));
152
153 u8 *encrypted_scb = tal_arr(tmpctx,
154 u8,
155 tal_bytelen(decrypted_scb) +
156 ABYTES +
157 HEADER_LEN);
158
159 crypto_secretstream_xchacha20poly1305_state crypto_state;
160
161 if (crypto_secretstream_xchacha20poly1305_init_push(&crypto_state,
162 encrypted_scb,
163 cb->secret.data) != 0)
164 {
165 plugin_err(p, "Can't encrypt the data!");
166 return;
167 }
168
169 if (crypto_secretstream_xchacha20poly1305_push(&crypto_state,
170 encrypted_scb +
171 HEADER_LEN,
172 NULL, decrypted_scb,
173 tal_bytelen(decrypted_scb),
174 /* Additional data and tag */
175 NULL, 0, 0)) {
176 plugin_err(p, "Can't encrypt the data!");
177 return;
178 }
179
180 if (!write_all(fd, encrypted_scb, tal_bytelen(encrypted_scb))) {
181 unlink_noerr("scb.tmp");
182 plugin_err(p, "Writing encrypted SCB: %s",
183 strerror(errno));
184 }
185
186}
187
188/* checks if the SCB file exists, creates a new one in case it doesn't. */
189static void maybe_create_new_scb(struct plugin *p,

Callers 2

maybe_create_new_scbFunction · 0.85
update_scbFunction · 0.85

Calls 6

clock_timeFunction · 0.85
tal_bytelenFunction · 0.85
unlink_noerrFunction · 0.85
chanbackupFunction · 0.70
plugin_errFunction · 0.70
write_allFunction · 0.50

Tested by

no test coverage detected