forked from withoutboats/bpb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # bpb-pkgx-cli/src/legacy_config.rs
- Loading branch information
Showing
5 changed files
with
128 additions
and
90 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
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,59 @@ | ||
use failure::Error; | ||
use std::io::Read; | ||
|
||
use crate::config::Config; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct LegacyConfig { | ||
public: PublicKey, | ||
secret: SecretKey, | ||
} | ||
|
||
impl LegacyConfig { | ||
pub fn convert(file: &mut impl Read) -> Result<(Config, [u8; 32]), Error> { | ||
let mut buf = vec![]; | ||
file.read_to_end(&mut buf)?; | ||
let config: LegacyConfig = toml::from_slice(&buf)?; | ||
Ok(( | ||
Config::create( | ||
config.public.key, | ||
config.public.userid, | ||
config.public.timestamp, | ||
)?, | ||
config.secret.secret()?, | ||
)) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct PublicKey { | ||
pub key: String, | ||
pub userid: String, | ||
pub timestamp: u64, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct SecretKey { | ||
key: Option<String>, | ||
program: Option<String>, | ||
} | ||
|
||
impl SecretKey { | ||
pub fn secret(&self) -> Result<[u8; 32], Error> { | ||
if let Some(key) = &self.key { | ||
to_32_bytes(key) | ||
} else if let Some(_program) = &self.program { | ||
bail!("unsupported program configuration") | ||
} else { | ||
bail!("no secret key found") | ||
} | ||
} | ||
} | ||
|
||
fn to_32_bytes(slice: &String) -> Result<[u8; 32], Error> { | ||
let vector = hex::decode(slice)?; | ||
let mut array = [0u8; 32]; | ||
let len = std::cmp::min(vector.len(), 32); | ||
array[..len].copy_from_slice(&vector[..len]); | ||
Ok(array) | ||
} |
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