-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl network management + some refactors
- Loading branch information
1 parent
ec20db5
commit 817416e
Showing
28 changed files
with
552 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::core::{key_pair::KeyPair, seed_phrase::SeedPhrase}; | ||
use super::db::Db; | ||
|
||
const ROOT_KEYPAIR: &[u8] = b"root_keypair"; | ||
const ROOT_SEED_PHRASE: &[u8] = b"root_seed_phrase"; | ||
|
||
const ERR_SEED_PHRASE_NOT_FOUND: &str = "Seed phrase not found"; | ||
const ERR_KEYPAIR_NOT_FOUND: &str = "Keypair not found"; | ||
|
||
impl Db { | ||
pub fn save_seed_phrase(&self, seed_phrase: &SeedPhrase) -> anyhow::Result<()> { | ||
let words = seed_phrase.get_words(); | ||
let serialized_seed_phrase = serde_json::to_vec(&words)?; | ||
self.insert(ROOT_SEED_PHRASE, &serialized_seed_phrase) | ||
} | ||
|
||
pub fn get_seed_phrase(&self) -> anyhow::Result<SeedPhrase> { | ||
let serialized_seed_phrase: Option<Vec<u8>> = self.get(ROOT_SEED_PHRASE)?; | ||
if let Some(serialized_seed_phrase) = serialized_seed_phrase { | ||
let words: Vec<String> = serde_json::from_slice(&serialized_seed_phrase)?; | ||
return SeedPhrase::from_words(words); | ||
} | ||
Err(anyhow::anyhow!(ERR_SEED_PHRASE_NOT_FOUND)) | ||
} | ||
|
||
pub fn delete_seed_phrase(&self) -> anyhow::Result<()> { | ||
match self.remove(ROOT_SEED_PHRASE) { | ||
Ok(_) => Ok(()), | ||
Err(_) => Err(anyhow::anyhow!(ERR_SEED_PHRASE_NOT_FOUND)) | ||
} | ||
} | ||
|
||
pub fn save_keypair(&self, keypair: &KeyPair) -> anyhow::Result<()> { | ||
let serialized_keypair = serde_json::to_vec(&keypair)?; | ||
self.insert(ROOT_KEYPAIR, &serialized_keypair) | ||
} | ||
|
||
pub fn get_keypair(&self) -> anyhow::Result<KeyPair> { | ||
let serialized_keypair: Option<Vec<u8>> = self.get(ROOT_KEYPAIR)?; | ||
if let Some(serialized_keypair) = serialized_keypair { | ||
let keypair: KeyPair = serde_json::from_slice(&serialized_keypair)?; | ||
keypair.validate()?; | ||
return Ok(keypair); | ||
} | ||
Err(anyhow::anyhow!(ERR_KEYPAIR_NOT_FOUND)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use crate::core::chain::Chain; | ||
use super::db::Db; | ||
|
||
const ACTIVE_NETWORKS: &[u8] = b"active_networks"; | ||
|
||
impl Db { | ||
pub fn save_active_networks(&self, chains: &[Chain]) -> anyhow::Result<()> { | ||
let serialized_chains = serde_json::to_vec(chains)?; | ||
self.insert(ACTIVE_NETWORKS, &serialized_chains) | ||
} | ||
|
||
pub fn get_active_networks(&self) -> anyhow::Result<Vec<Chain>> { | ||
let serialized_chains: Option<Vec<u8>> = self.get(ACTIVE_NETWORKS)?; | ||
if let Some(serialized_chains) = serialized_chains { | ||
let chains: Vec<Chain> = serde_json::from_slice(&serialized_chains)?; | ||
return Ok(chains); | ||
} | ||
Ok(vec![]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
mod cipher; | ||
mod cipher_test; | ||
pub mod db; | ||
pub mod db_accounts; | ||
pub mod db_chains; | ||
mod db_test; | ||
pub mod manage; | ||
mod manage_test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod receive; | ||
pub mod networks; | ||
pub mod receive; |
Oops, something went wrong.