Skip to content

Commit b0c2185

Browse files
authored
set network to identity and check for network version on startup (#545)
1 parent 2b74b2e commit b0c2185

File tree

8 files changed

+56
-1
lines changed

8 files changed

+56
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.3.16
2+
3+
* Set the BTC network in the identity and check, if the persisted network is the same as the one configured in the application, failing if it doesn't.
4+
15
# 0.3.15
26

37
* Upload and download files to and from Nostr using Blossom

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace.package]
2-
version = "0.3.15"
2+
version = "0.3.16"
33
edition = "2024"
44
license = "MIT"
55

crates/bcr-ebill-api/src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ pub mod tests {
244244
async fn get_seedphrase(&self) -> Result<String>;
245245
async fn get_current_identity(&self) -> Result<ActiveIdentityState>;
246246
async fn set_current_identity(&self, identity_state: &ActiveIdentityState) -> Result<()>;
247+
async fn set_or_check_network(&self, configured_network: bitcoin::Network) -> Result<()>;
247248
}
248249
}
249250

crates/bcr-ebill-persistence/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ nostr.workspace = true
2121
bcr-ebill-core = { path = "../bcr-ebill-core" }
2222
tokio.workspace = true
2323
tokio_with_wasm.workspace = true
24+
bitcoin.workspace = true
2425
arc-swap = "1.7"
2526

2627
# Enable "kv-indxdb" only for WebAssembly (wasm32)

crates/bcr-ebill-persistence/src/db/identity.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ impl SurrealIdentityStore {
1616
const IDENTITY_TABLE: &'static str = "identity";
1717
const ACTIVE_IDENTITY_TABLE: &'static str = "active_identity";
1818
const KEY_TABLE: &'static str = "identity_key";
19+
const NETWORK_TABLE: &'static str = "identity_network";
1920
const UNIQUE_ID: &'static str = "unique_record";
2021

2122
pub fn new(db: SurrealWrapper) -> Self {
@@ -29,6 +30,14 @@ impl SurrealIdentityStore {
2930
.select_one(Self::KEY_TABLE, Self::UNIQUE_ID.to_owned())
3031
.await
3132
}
33+
34+
async fn get_db_network(&self) -> Result<Option<bitcoin::Network>> {
35+
let result: Option<NetworkDb> = self
36+
.db
37+
.select_one(Self::NETWORK_TABLE, Self::UNIQUE_ID.to_owned())
38+
.await?;
39+
Ok(result.map(|nw| nw.network))
40+
}
3241
}
3342

3443
impl ServiceTraitBounds for SurrealIdentityStore {}
@@ -84,6 +93,31 @@ impl IdentityStoreApi for SurrealIdentityStore {
8493
}
8594
}
8695

96+
async fn set_or_check_network(&self, configured_network: bitcoin::Network) -> Result<()> {
97+
let network = self.get_db_network().await?;
98+
match network {
99+
None => {
100+
let _: Option<NetworkDb> = self
101+
.db
102+
.create(
103+
Self::NETWORK_TABLE,
104+
Some(Self::UNIQUE_ID.to_owned()),
105+
NetworkDb {
106+
network: configured_network,
107+
},
108+
)
109+
.await?;
110+
Ok(())
111+
}
112+
Some(nw) => {
113+
if configured_network != nw {
114+
return Err(Error::NetworkDoesNotMatch);
115+
}
116+
Ok(())
117+
}
118+
}
119+
}
120+
87121
async fn get_or_create_key_pair(&self) -> Result<BcrKeys> {
88122
let keys = match self.get_key_pair().await {
89123
Ok(keys) => keys,
@@ -134,6 +168,12 @@ impl IdentityStoreApi for SurrealIdentityStore {
134168
Ok(())
135169
}
136170
}
171+
172+
#[derive(Debug, Clone, Serialize, Deserialize)]
173+
pub struct NetworkDb {
174+
pub network: bitcoin::Network,
175+
}
176+
137177
#[derive(Debug, Clone, Serialize, Deserialize)]
138178
pub struct ActiveIdentityDb {
139179
pub personal: String,

crates/bcr-ebill-persistence/src/identity.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub trait IdentityStoreApi: ServiceTraitBounds {
3232
async fn get_current_identity(&self) -> Result<ActiveIdentityState>;
3333
/// Sets the given current active identity state
3434
async fn set_current_identity(&self, identity_state: &ActiveIdentityState) -> Result<()>;
35+
/// Sets the network for this identity, or, if it's set, checks if the set network is the same as the configured one
36+
async fn set_or_check_network(&self, configured_network: bitcoin::Network) -> Result<()>;
3537
}
3638

3739
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]

crates/bcr-ebill-persistence/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ pub enum Error {
8282
#[error("no identity key found")]
8383
NoIdentityKey,
8484

85+
#[error("Network does not match")]
86+
NetworkDoesNotMatch,
87+
8588
#[allow(dead_code)]
8689
#[error("Failed to convert integer {0}")]
8790
FromInt(#[from] std::num::TryFromIntError),

crates/bcr-ebill-wasm/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ pub async fn initialize_api(
8787

8888
// init db
8989
let db = get_db_context(&api_config).await?;
90+
// set the network and check if the configured network matches the persisted network and fail, if not
91+
db.identity_store
92+
.set_or_check_network(api_config.bitcoin_network())
93+
.await?;
9094
let keys = db.identity_store.get_or_create_key_pair().await?;
9195

9296
info!("Initialized WASM API {}", VERSION);

0 commit comments

Comments
 (0)