mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2024-08-21 09:45:37 +00:00
![Daniel Karzel](/assets/img/avatar_default.png)
This includes the introduction of the --data-dir parameter instead of the --database. Both the seed file and the database are stored in the data-dir, the database in sub-folder `database`.
59 lines
1.2 KiB
Rust
59 lines
1.2 KiB
Rust
use ::bitcoin::secp256k1::{self, constants::SECRET_KEY_SIZE, SecretKey};
|
|
use rand::prelude::*;
|
|
use std::fmt;
|
|
|
|
pub const SEED_LENGTH: usize = 32;
|
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
pub struct Seed([u8; SEED_LENGTH]);
|
|
|
|
impl Seed {
|
|
pub fn random() -> Result<Self, Error> {
|
|
let mut bytes = [0u8; SECRET_KEY_SIZE];
|
|
rand::thread_rng().fill_bytes(&mut bytes);
|
|
|
|
// If it succeeds once, it'll always succeed
|
|
let _ = SecretKey::from_slice(&bytes)?;
|
|
|
|
Ok(Seed(bytes))
|
|
}
|
|
|
|
pub fn bytes(&self) -> [u8; SEED_LENGTH] {
|
|
self.0
|
|
}
|
|
}
|
|
|
|
impl fmt::Debug for Seed {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "Seed([*****])")
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Seed {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "{:?}", self)
|
|
}
|
|
}
|
|
|
|
impl From<[u8; SEED_LENGTH]> for Seed {
|
|
fn from(bytes: [u8; SEED_LENGTH]) -> Self {
|
|
Seed(bytes)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, thiserror::Error)]
|
|
pub enum Error {
|
|
#[error("Secp256k1: ")]
|
|
Secp256k1(#[from] secp256k1::Error),
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn generate_random_seed() {
|
|
let _ = Seed::random().unwrap();
|
|
}
|
|
}
|