Skip to content

Commit 868c1ea

Browse files
authored
Merge pull request #44 from Foundation-Devices/magic-backup-protocol
magic backup protocol
2 parents 85b6ce4 + f5e1c92 commit 868c1ea

File tree

15 files changed

+134
-68
lines changed

15 files changed

+134
-68
lines changed

api/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ anyhow = "^1.0.0"
2828
minicbor = { workspace = true, features = ["alloc"] }
2929
minicbor-derive = { workspace = true }
3030
bc-xid = "0.5.0"
31-
rkyv = { workspace = true }
32-
flutter_rust_bridge = "=2.11.1"
31+
rkyv = { workspace = true, optional = true }
32+
flutter_rust_bridge = { version = "=2.11.1", optional = true }
3333
quantum-link-macros = { workspace = true }
3434
gstp = "0.5.0"
3535
bc-components = "0.17.0"
3636
dcbor = "0.16.5"
3737
chrono = "0.4"
3838

39+
[features]
40+
keyos = ["rkyv"]
41+
envoy = ["flutter_rust_bridge"]
42+
3943
[lints.rust]
4044
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }

api/src/api/backup.rs

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::api::quantum_link::QuantumLink;
6-
73
#[quantum_link]
84
pub struct Shard {
95
#[n(0)]
@@ -45,3 +41,105 @@ pub struct MagicBackupEnabledResponse {
4541
#[n(0)]
4642
pub enabled: bool,
4743
}
44+
45+
//
46+
// MAGIC BACKUPS
47+
//
48+
49+
pub type SeedFingerprint = [u8; 32];
50+
51+
#[quantum_link]
52+
#[derive(PartialEq, Eq)]
53+
pub struct BackupChunk {
54+
#[n(0)]
55+
pub chunk_index: u32,
56+
#[n(1)]
57+
pub total_chunks: u32,
58+
#[n(2)]
59+
pub data: Vec<u8>,
60+
}
61+
62+
impl BackupChunk {
63+
pub fn is_last(&self) -> bool {
64+
self.chunk_index == self.total_chunks - 1
65+
}
66+
}
67+
68+
//
69+
// CREATING BACKUP
70+
//
71+
72+
// from prime -> envoy
73+
#[quantum_link]
74+
pub enum CreateMagicBackupEvent {
75+
#[n(0)]
76+
Start(#[n(0)] StartMagicBackup),
77+
#[n(1)]
78+
Chunk(#[n(0)] BackupChunk),
79+
}
80+
81+
#[quantum_link]
82+
pub struct StartMagicBackup {
83+
#[n(0)]
84+
pub seed_fingerprint: SeedFingerprint,
85+
#[n(1)]
86+
pub total_chunks: u32,
87+
}
88+
89+
// envoy -> prime
90+
// error can be sent at any time
91+
// success is expected at the end of the flow
92+
#[quantum_link]
93+
pub enum CreateMagicBackupResult {
94+
#[n(0)]
95+
Success,
96+
#[n(1)]
97+
Error(#[n(0)] String),
98+
}
99+
100+
//
101+
// RESTORING BACKUP
102+
//
103+
104+
#[quantum_link]
105+
pub struct RestoreMagicBackupRequest {
106+
#[n(0)]
107+
pub seed_fingerprint: SeedFingerprint,
108+
/// if 0, then go from start
109+
#[n(1)]
110+
pub resume_from_chunk: u32,
111+
}
112+
113+
#[quantum_link]
114+
pub enum RestoreMagicBackupEvent {
115+
// there is no backup found from the provided fingerprint
116+
#[n(0)]
117+
NoBackupFound,
118+
// envoy found a backup and is beginning transmission
119+
#[n(1)]
120+
Starting(#[n(0)] BackupMetadata),
121+
// envoy is downloading the backup
122+
#[n(2)]
123+
Downloading,
124+
#[n(3)]
125+
Chunk(#[n(0)] BackupChunk),
126+
// envoy failed
127+
#[n(5)]
128+
Error(#[n(0)] String),
129+
}
130+
131+
#[quantum_link]
132+
#[derive(PartialEq, Eq)]
133+
pub struct BackupMetadata {
134+
#[n(0)]
135+
pub total_chunks: u32,
136+
}
137+
138+
// sent from prime -> envoy
139+
#[quantum_link]
140+
pub enum RestoreMagicBackupResult {
141+
#[n(0)]
142+
Success,
143+
#[n(1)]
144+
Error(#[n(0)] String),
145+
}

api/src/api/bitcoin.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::quantum_link::QuantumLink;
6-
73
#[quantum_link]
84
pub struct SignPsbt {
95
#[n(0)]

api/src/api/firmware.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::quantum_link::QuantumLink;
6-
73
// From Prime to Envoy
84
#[quantum_link]
95
pub struct FirmwareUpdateCheckRequest {

api/src/api/fx.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::api::quantum_link::QuantumLink;
6-
73
#[quantum_link]
84
pub struct ExchangeRate {
95
#[n(0)]
@@ -28,5 +24,3 @@ impl ExchangeRate {
2824
self.rate
2925
}
3026
}
31-
32-
//impl QuantumLinkMessage<ExchangeRate> for ExchangeRate {}

api/src/api/message.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

53
use super::onboarding::OnboardingState;
64
use crate::{
75
api::raw::RawData,
86
backup::{
9-
BackupShardRequest, BackupShardResponse, MagicBackupEnabledRequest,
10-
MagicBackupEnabledResponse, RestoreShardRequest, RestoreShardResponse,
7+
BackupShardRequest, BackupShardResponse, CreateMagicBackupEvent, CreateMagicBackupResult,
8+
MagicBackupEnabledRequest, MagicBackupEnabledResponse, RestoreMagicBackupEvent,
9+
RestoreMagicBackupRequest, RestoreMagicBackupResult, RestoreShardRequest,
10+
RestoreShardResponse,
1111
},
1212
bitcoin::*,
1313
firmware::{
@@ -16,7 +16,6 @@ use crate::{
1616
},
1717
fx::ExchangeRate,
1818
pairing::{PairingRequest, PairingResponse},
19-
quantum_link::QuantumLink,
2019
scv::SecurityCheck,
2120
status::{DeviceStatus, EnvoyStatus},
2221
};
@@ -116,5 +115,15 @@ pub enum QuantumLinkMessage {
116115
#[n(20)]
117116
RestoreShardResponse(#[n(0)] RestoreShardResponse),
118117
#[n(21)]
118+
CreateMagicBackupEvent(#[n(0)] CreateMagicBackupEvent),
119+
#[n(22)]
120+
CreateMagicBackupResult(#[n(0)] CreateMagicBackupResult),
121+
#[n(23)]
122+
RestoreMagicBackupRequest(#[n(0)] RestoreMagicBackupRequest),
123+
#[n(24)]
124+
RestoreMagicBackupEvent(#[n(0)] RestoreMagicBackupEvent),
125+
#[n(25)]
126+
RestoreMagicBackupResult(#[n(0)] RestoreMagicBackupResult),
127+
#[n(100)]
119128
RawData(#[n(0)] RawData),
120129
}

api/src/api/onboarding.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::api::quantum_link::QuantumLink;
6-
73
#[quantum_link]
84
pub enum OnboardingState {
95
#[n(0)]

api/src/api/pairing.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

53
use crate::{
6-
api::{
7-
passport::{PassportFirmwareVersion, PassportModel, PassportSerial},
8-
quantum_link::QuantumLink,
9-
},
4+
api::passport::{PassportFirmwareVersion, PassportModel, PassportSerial},
105
passport::PassportColor,
116
};
127

api/src/api/passport.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use flutter_rust_bridge::frb;
2-
use minicbor_derive::{Decode, Encode};
31
use quantum_link_macros::quantum_link;
42

5-
use crate::api::quantum_link::QuantumLink;
6-
73
#[quantum_link]
84
pub enum PassportModel {
95
#[n(0)]
@@ -14,10 +10,10 @@ pub enum PassportModel {
1410
Prime,
1511
}
1612

17-
#[derive(Debug, Clone, Encode, Decode, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
13+
#[quantum_link]
1814
pub struct PassportFirmwareVersion(#[n(0)] pub String);
1915

20-
#[derive(Debug, Clone, Encode, Decode, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
16+
#[quantum_link]
2117
pub struct PassportSerial(#[n(0)] pub String);
2218

2319
#[quantum_link]

api/src/api/quantum_link.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use bc_envelope::{
99
use bc_xid::XIDDocument;
1010
use chrono::{DateTime, Utc};
1111
use dcbor::Date;
12-
use flutter_rust_bridge::frb;
1312
use gstp::{SealedEvent, SealedEventBehavior};
1413

1514
use crate::message::{EnvoyMessage, PassportMessage};
@@ -66,11 +65,8 @@ impl ARIDCache {
6665
}
6766
}
6867

69-
pub trait QuantumLink<C>: minicbor::Encode<C> {
70-
fn encode(&self) -> Expression
71-
where
72-
Self: minicbor::Encode<()>,
73-
{
68+
pub trait QuantumLink: minicbor::Encode<()> + for<'a> minicbor::Decode<'a, ()> {
69+
fn encode(&self) -> Expression {
7470
let mut buffer: Vec<u8> = Vec::new();
7571

7672
minicbor::encode(self, &mut buffer).unwrap();
@@ -191,8 +187,10 @@ pub trait QuantumLink<C>: minicbor::Encode<C> {
191187
}
192188
}
193189

190+
impl<T> QuantumLink for T where T: minicbor::Encode<()> + for<'a> minicbor::Decode<'a, ()> {}
191+
194192
#[derive(Debug, Clone)]
195-
#[frb(opaque)]
193+
#[cfg_attr(feature = "envoy", flutter_rust_bridge::frb(non_opaque))]
196194
pub struct QuantumLinkIdentity {
197195
pub private_keys: Option<PrivateKeys>,
198196
pub xid_document: XIDDocument,

0 commit comments

Comments
 (0)