2021-03-08 17:02:36 +05:30
|
|
|
/*
|
|
|
|
* mCaptcha - A proof of work based DoS protection system
|
|
|
|
* Copyright © 2021 Aravinth Manivannan <realravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-08 19:43:26 +05:30
|
|
|
//! PoW datatypes used in client-server interaction
|
|
|
|
use pow_sha256::PoW;
|
2021-03-12 13:52:49 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-08 17:02:36 +05:30
|
|
|
|
2021-03-09 17:37:46 +05:30
|
|
|
pub use pow_sha256::ConfigBuilder;
|
|
|
|
|
2021-03-08 19:43:26 +05:30
|
|
|
/// PoW requirement datatype that is be sent to clients for generating PoW
|
2021-03-12 13:52:49 +05:30
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
2021-03-08 17:02:36 +05:30
|
|
|
pub struct PoWConfig {
|
|
|
|
pub string: String,
|
|
|
|
pub difficulty_factor: u32,
|
|
|
|
}
|
|
|
|
impl PoWConfig {
|
2021-03-08 19:43:26 +05:30
|
|
|
/// create new instance of [PoWConfig]
|
2021-03-08 17:02:36 +05:30
|
|
|
pub fn new(m: u32) -> Self {
|
2021-03-08 18:14:34 +05:30
|
|
|
use crate::utils::get_random;
|
2021-03-08 17:02:36 +05:30
|
|
|
|
|
|
|
PoWConfig {
|
2021-03-08 18:14:34 +05:30
|
|
|
string: get_random(32),
|
2021-03-08 17:02:36 +05:30
|
|
|
difficulty_factor: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:43:26 +05:30
|
|
|
/// PoW datatype that clients send to server
|
2021-03-12 13:52:49 +05:30
|
|
|
#[derive(Clone, Serialize, Deserialize, Debug)]
|
2021-03-08 18:14:34 +05:30
|
|
|
pub struct Work {
|
|
|
|
pub string: String,
|
|
|
|
pub result: String,
|
|
|
|
pub nonce: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Work> for PoW<String> {
|
|
|
|
fn from(w: Work) -> Self {
|
|
|
|
use pow_sha256::PoWBuilder;
|
|
|
|
PoWBuilder::default()
|
|
|
|
.result(w.result)
|
|
|
|
.nonce(w.nonce)
|
|
|
|
.build()
|
|
|
|
.unwrap()
|
|
|
|
}
|
2021-03-08 17:02:36 +05:30
|
|
|
}
|