refactor: secret as hex bytes

This commit is contained in:
thesimplekid
2024-02-10 14:44:01 +00:00
parent 1aea753ea2
commit 7de2413dc0
4 changed files with 18 additions and 9 deletions

View File

@@ -6,7 +6,7 @@ pub use mint::{sign_message, verify_message};
#[cfg(feature = "wallet")]
pub use wallet::{blind_message, construct_proofs, unblind_message};
fn hash_to_curve(message: &[u8]) -> k256::PublicKey {
pub fn hash_to_curve(message: &[u8]) -> k256::PublicKey {
let mut msg_to_hash = message.to_vec();
loop {
@@ -130,7 +130,7 @@ mod mint {
msg: &Secret,
) -> Result<(), error::mint::Error> {
// Y
let y = hash_to_curve(msg.as_bytes());
let y = hash_to_curve(&msg.to_bytes()?);
if unblinded_message
== k256::PublicKey::try_from(*y.as_affine() * Scalar::from(a.as_scalar_primitive()))?
@@ -144,6 +144,8 @@ mod mint {
#[cfg(test)]
mod tests {
use core::panic;
use hex::decode;
use k256::elliptic_curve::scalar::ScalarPrimitive;
@@ -349,7 +351,7 @@ mod tests {
let x = Secret::new();
// Y
let y = hash_to_curve(x.as_bytes());
let y = hash_to_curve(&x.to_bytes().unwrap());
// B_
let blinded = blind_message(&y.to_sec1_bytes(), None).unwrap();

View File

@@ -69,6 +69,8 @@ pub mod wallet {
/// Url Parse error
#[error("Url Parse")]
UrlParse,
#[error("`{0}`")]
Secret(#[from] crate::secret::Error),
/// Custom Error message
#[error("`{0}`")]
CustomError(String),
@@ -110,6 +112,8 @@ pub mod mint {
#[error("Unknown Keyset")]
UnknownKeySet,
#[error("`{0}`")]
Secret(#[from] crate::secret::Error),
#[error("`{0}`")]
CustomError(String),
}
}

View File

@@ -165,7 +165,7 @@ pub mod wallet {
for amount in amount_split {
let secret = Secret::new();
let (blinded, r) = blind_message(secret.as_bytes(), None)?;
let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
let blinded_message = BlindedMessage {
amount,
@@ -192,7 +192,7 @@ pub mod wallet {
let mut output = Vec::with_capacity(secrets.len());
for (secret, amount) in secrets.into_iter().zip(amounts) {
let (blinded, r) = blind_message(secret.as_bytes(), None)?;
let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
let blinded_message = BlindedMessage {
amount,
@@ -219,7 +219,7 @@ pub mod wallet {
for _i in 0..count {
let secret = Secret::new();
let (blinded, r) = blind_message(secret.as_bytes(), None)?;
let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
let blinded_message = BlindedMessage {
amount: Amount::ZERO,
@@ -255,7 +255,8 @@ pub mod wallet {
let secret = Secret::from_seed(mnemonic, keyset_id, counter);
let blinding_factor = SecretKey::from_seed(mnemonic, keyset_id, counter);
let (blinded, r) = blind_message(secret.as_bytes(), Some(blinding_factor.into()))?;
let (blinded, r) =
blind_message(&secret.to_bytes()?, Some(blinding_factor.into()))?;
let blinded_message = BlindedMessage {
keyset_id,

View File

@@ -18,6 +18,8 @@ pub struct Secret(String);
pub enum Error {
#[error("Invalid secret length: `{0}`")]
InvalidLength(u64),
#[error("Hex error: `{0}`")]
Hex(#[from] hex::FromHexError),
}
impl Default for Secret {
@@ -56,8 +58,8 @@ impl Secret {
Self(hex::encode(xpriv.private_key().to_bytes()))
}
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
Ok(hex::decode(&self.0)?)
}
}