@@ -16,6 +16,7 @@ impl SurrealIdentityStore {
16
16
const IDENTITY_TABLE : & ' static str = "identity" ;
17
17
const ACTIVE_IDENTITY_TABLE : & ' static str = "active_identity" ;
18
18
const KEY_TABLE : & ' static str = "identity_key" ;
19
+ const NETWORK_TABLE : & ' static str = "identity_network" ;
19
20
const UNIQUE_ID : & ' static str = "unique_record" ;
20
21
21
22
pub fn new ( db : SurrealWrapper ) -> Self {
@@ -29,6 +30,14 @@ impl SurrealIdentityStore {
29
30
. select_one ( Self :: KEY_TABLE , Self :: UNIQUE_ID . to_owned ( ) )
30
31
. await
31
32
}
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
+ }
32
41
}
33
42
34
43
impl ServiceTraitBounds for SurrealIdentityStore { }
@@ -84,6 +93,31 @@ impl IdentityStoreApi for SurrealIdentityStore {
84
93
}
85
94
}
86
95
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
+
87
121
async fn get_or_create_key_pair ( & self ) -> Result < BcrKeys > {
88
122
let keys = match self . get_key_pair ( ) . await {
89
123
Ok ( keys) => keys,
@@ -134,6 +168,12 @@ impl IdentityStoreApi for SurrealIdentityStore {
134
168
Ok ( ( ) )
135
169
}
136
170
}
171
+
172
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
173
+ pub struct NetworkDb {
174
+ pub network : bitcoin:: Network ,
175
+ }
176
+
137
177
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
138
178
pub struct ActiveIdentityDb {
139
179
pub personal : String ,
0 commit comments