forked from jl777/SuperNET
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl detect_secret_hash_algo_v2 function
- Loading branch information
Showing
8 changed files
with
83 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use bitcrypto::{dhash160, sha256}; | ||
use derive_more::Display; | ||
use std::convert::TryFrom; | ||
|
||
/// Algorithm used to hash swap secret. | ||
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Default)] | ||
pub enum SecretHashAlgo { | ||
/// ripemd160(sha256(secret)) | ||
#[default] | ||
DHASH160 = 1, | ||
/// sha256(secret) | ||
SHA256 = 2, | ||
} | ||
|
||
#[derive(Debug, Display)] | ||
pub struct UnsupportedSecretHashAlgo(u8); | ||
|
||
impl std::error::Error for UnsupportedSecretHashAlgo {} | ||
|
||
impl TryFrom<u8> for SecretHashAlgo { | ||
type Error = UnsupportedSecretHashAlgo; | ||
|
||
fn try_from(value: u8) -> Result<Self, Self::Error> { | ||
match value { | ||
1 => Ok(SecretHashAlgo::DHASH160), | ||
2 => Ok(SecretHashAlgo::SHA256), | ||
unsupported => Err(UnsupportedSecretHashAlgo(unsupported)), | ||
} | ||
} | ||
} | ||
|
||
impl SecretHashAlgo { | ||
pub fn hash_secret(&self, secret: &[u8]) -> Vec<u8> { | ||
match self { | ||
SecretHashAlgo::DHASH160 => dhash160(secret).take().into(), | ||
SecretHashAlgo::SHA256 => sha256(secret).take().into(), | ||
} | ||
} | ||
} |
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