mirror of
https://github.com/comit-network/xmr-btc-swap.git
synced 2025-08-20 03:58:13 -04:00
Deterministic peer id from seed for alice
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`.
This commit is contained in:
parent
64ba8d6a87
commit
0a21040e08
18 changed files with 377 additions and 35 deletions
58
swap/src/seed.rs
Normal file
58
swap/src/seed.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue