Backup&Restore

This commit is contained in:
ericz committed 2026-11-08 00:40:23 +01:00
1 parent 394df3109b
commit aae2bf6d60
29 files changed
+2769 -120

No files matched your search

+30
View File
@@ -4334,6 +4334,36 @@ class MeshCoreConnector extends ChangeNotifier {
} catch (_) {}
}
Future<Uint8List> exportPrivateKey() async {
if (!isConnected) throw StateError('Not connected to a MeshCore device');
final response = Completer<Uint8List>();
final subscription = receivedFrames.listen((frame) {
if (frame.isNotEmpty &&
frame[0] == respCodePrivateKey &&
!response.isCompleted) {
response.complete(Uint8List.fromList(frame));
}
});
try {
await sendFrame(buildExportPrivateKeyFrame(), waitForGenericAck: true);
final frame = await response.future.timeout(const Duration(seconds: 1));
if (frame.length != 65) {
throw FormatException('Firmware returned an invalid private-key frame');
}
return Uint8List.fromList(frame.sublist(1));
} finally {
await subscription.cancel();
}
}
Future<void> importPrivateKey(Uint8List keyData) async {
if (!isConnected) throw StateError('Not connected to a MeshCore device');
await sendFrame(
buildImportPrivateKeyFrame(keyData),
waitForGenericAck: true,
);
}
/// Sends CMD_SET_RADIO_PARAMS (same arguments as [buildSetRadioParamsFrame])
/// and waits for the reply. Returns false when the firmware rejects it with
/// ERR_ILLEGAL_ARG (values out of range, or [clientRepeat] on a frequency
+14
View File
@@ -195,6 +195,8 @@ const int cmdImportContact = 18;
const int cmdReboot = 19;
const int cmdGetBattAndStorage = 20;
const int cmdDeviceQuery = 22;
const int cmdExportPrivateKey = 23;
const int cmdImportPrivateKey = 24;
const int cmdSendLogin = 26;
const int cmdSendStatusReq = 27;
const int cmdGetContactByKey = 30;
@@ -259,6 +261,8 @@ const int respCodeNoMoreMessages = 10;
const int respCodeExportContact = 11;
const int respCodeBattAndStorage = 12;
const int respCodeDeviceInfo = 13;
const int respCodePrivateKey = 14;
const int respCodeDisabled = 15;
const int respCodeContactMsgRecvV3 = 16;
const int respCodeChannelMsgRecvV3 = 17;
const int respCodeChannelInfo = 18;
@@ -1087,6 +1091,16 @@ Uint8List buildTraceReq(int tag, int auth, int flag, {Uint8List? payload}) {
return writer.toBytes();
}
Uint8List buildExportPrivateKeyFrame() =>
Uint8List.fromList([cmdExportPrivateKey]);
Uint8List buildImportPrivateKeyFrame(Uint8List keyData) {
if (keyData.length != 64) {
throw ArgumentError.value(keyData.length, 'keyData.length', 'must be 64');
}
return Uint8List.fromList([cmdImportPrivateKey, ...keyData]);
}
// Build a export contact frame
// [cmd][pub_key x32 / if empty exports your contact info]
Uint8List buildExportContactFrame(Uint8List pubKey) {
@@ -63,6 +63,7 @@ int? replyCodeForCommand(Uint8List data) {
case cmdRemoveContact:
case cmdShareContact:
case cmdImportContact:
case cmdImportPrivateKey:
case cmdSetChannel:
case cmdSetOtherParams:
case cmdSetCustomVar:
@@ -95,6 +96,8 @@ int? replyCodeForCommand(Uint8List data) {
return respCodeCurrTime;
case cmdExportContact:
return respCodeExportContact;
case cmdExportPrivateKey:
return respCodePrivateKey;
case cmdGetBattAndStorage:
return respCodeBattAndStorage;
case cmdGetChannel:
@@ -180,6 +183,19 @@ class PendingCommandReplies {
if (frame.isEmpty) return false;
_pruneStale();
final code = frame[0];
if (code == respCodeDisabled) {
final index = _queue.indexWhere(
(pending) =>
pending.commandCode == cmdExportPrivateKey ||
pending.commandCode == cmdImportPrivateKey,
);
if (index < 0) return false;
final pending = _queue.removeAt(index);
pending.completer?.completeError(
UnsupportedError('Private-key transfer is disabled by firmware'),
);
return true;
}
if (code == respCodeOk || code == respCodeErr) return false;
final key = code == respCodeContact && frame.length >= 1 + pubKeySize
? frame.sublist(1, 1 + pubKeySize)