Remove redundant serialisation implementation

ECDSAfun's serialisation implementation was already imported
making the custom implementation redundant

Remove remnants of stale comment

Remove redundant conversion

Rename params to be consistent
pull/14/head
rishflab 4 years ago
parent 71e09413aa
commit 8eda051087

@ -3,7 +3,7 @@ use crate::{
bitcoin::{BroadcastSignedTransaction, WatchForRawTransaction},
bob, monero,
monero::{CreateWalletForOutput, Transfer},
serde::{bitcoin_amount, cross_curve_dleq_scalar, ecdsa_fun_signature},
serde::{bitcoin_amount, cross_curve_dleq_scalar},
transport::{ReceiveMessage, SendMessage},
};
use anyhow::{anyhow, Result};
@ -347,17 +347,15 @@ pub struct State3 {
pub v: monero::PrivateViewKey,
#[serde(with = "bitcoin_amount")]
btc: bitcoin::Amount,
pub xmr: monero::Amount,
pub refund_timelock: u32,
pub punish_timelock: u32,
pub refund_address: bitcoin::Address,
pub redeem_address: bitcoin::Address,
pub punish_address: bitcoin::Address,
pub tx_lock: bitcoin::TxLock,
#[serde(with = "ecdsa_fun_signature")]
pub tx_punish_sig_bob: bitcoin::Signature,
#[serde(with = "ecdsa_fun_signature")]
pub tx_cancel_sig_bob: bitcoin::Signature,
xmr: monero::Amount,
refund_timelock: u32,
punish_timelock: u32,
refund_address: bitcoin::Address,
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock,
tx_punish_sig_bob: bitcoin::Signature,
tx_cancel_sig_bob: bitcoin::Signature,
}
impl State3 {
@ -411,9 +409,7 @@ pub struct State4 {
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock,
#[serde(with = "ecdsa_fun_signature")]
tx_punish_sig_bob: bitcoin::Signature,
#[serde(with = "ecdsa_fun_signature")]
tx_cancel_sig_bob: bitcoin::Signature,
}
@ -519,9 +515,9 @@ pub struct State5 {
punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock,
tx_lock_proof: monero::TransferProof,
#[serde(with = "ecdsa_fun_signature")]
tx_punish_sig_bob: bitcoin::Signature,
#[serde(with = "ecdsa_fun_signature")]
tx_cancel_sig_bob: bitcoin::Signature,
lock_xmr_fee: monero::Amount,
}
@ -613,7 +609,7 @@ pub struct State6 {
redeem_address: bitcoin::Address,
punish_address: bitcoin::Address,
tx_lock: bitcoin::TxLock,
#[serde(with = "ecdsa_fun_signature")]
tx_punish_sig_bob: bitcoin::Signature,
tx_redeem_encsig: EncryptedSignature,
lock_xmr_fee: monero::Amount,

@ -1,48 +1,3 @@
pub mod ecdsa_fun_signature {
use serde::{de, de::Visitor, Deserializer, Serializer};
use std::{convert::TryFrom, fmt};
struct Bytes64Visitor;
impl<'de> Visitor<'de> for Bytes64Visitor {
type Value = ecdsa_fun::Signature;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a string containing 64 bytes")
}
fn visit_bytes<E>(self, s: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
if let Ok(value) = <[u8; 64]>::try_from(s) {
let sig = ecdsa_fun::Signature::from_bytes(value)
.expect("bytes represent an integer greater than or equal to the curve order");
Ok(sig)
} else {
Err(de::Error::invalid_length(s.len(), &self))
}
}
}
pub fn serialize<S>(x: &ecdsa_fun::Signature, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
s.serialize_bytes(&x.to_bytes())
}
pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<ecdsa_fun::Signature, <D as Deserializer<'de>>::Error>
where
D: Deserializer<'de>,
{
let sig = deserializer.deserialize_bytes(Bytes64Visitor)?;
Ok(sig)
}
}
pub mod cross_curve_dleq_scalar {
use serde::{de, de::Visitor, Deserializer, Serializer};
use std::{convert::TryFrom, fmt};
@ -178,7 +133,6 @@ pub mod monero_amount {
#[cfg(test)]
mod tests {
use super::*;
use ::bitcoin::SigHash;
use curve25519_dalek::scalar::Scalar;
use rand::rngs::OsRng;
use serde::{Deserialize, Serialize};
@ -188,9 +142,6 @@ mod tests {
#[serde(with = "cross_curve_dleq_scalar")] cross_curve_dleq::Scalar,
);
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct ECDSAFunSignature(#[serde(with = "ecdsa_fun_signature")] ecdsa_fun::Signature);
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct MoneroPrivateKey(#[serde(with = "monero_private_key")] crate::monero::PrivateKey);
@ -205,15 +156,6 @@ mod tests {
assert_eq!(scalar, decoded);
}
#[test]
fn serde_ecdsa_fun_sig() {
let secret_key = crate::bitcoin::SecretKey::new_random(&mut OsRng);
let sig = ECDSAFunSignature(secret_key.sign(SigHash::default()));
let encoded = serde_cbor::to_vec(&sig).unwrap();
let decoded: ECDSAFunSignature = serde_cbor::from_slice(&encoded).unwrap();
assert_eq!(sig, decoded);
}
#[test]
fn serde_monero_private_key() {
let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(Scalar::random(&mut OsRng)));

@ -10,10 +10,8 @@ impl Database {
const LAST_STATE_KEY: &'static str = "latest_state";
pub fn open(path: &Path) -> Result<Self> {
let path = path
.to_str()
.ok_or_else(|| anyhow!("The path is not utf-8 valid: {:?}", path))?;
let db = sled::open(path).with_context(|| format!("Could not open the DB at {}", path))?;
let db =
sled::open(path).with_context(|| format!("Could not open the DB at {:?}", path))?;
Ok(Database { db })
}
@ -30,7 +28,7 @@ impl Database {
self.db
.compare_and_swap(key, old_value, Some(new_value))
.context("Could not write in the DB")?
.context("Stored swap somehow changed, aborting saving")?; // let _ =
.context("Stored swap somehow changed, aborting saving")?;
self.db
.flush_async()
@ -77,9 +75,7 @@ mod tests {
use curve25519_dalek::scalar::Scalar;
use ecdsa_fun::fun::rand_core::OsRng;
use std::str::FromStr;
use xmr_btc::serde::{
bitcoin_amount, cross_curve_dleq_scalar, ecdsa_fun_signature, monero_private_key,
};
use xmr_btc::serde::{bitcoin_amount, cross_curve_dleq_scalar, monero_private_key};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct TestState {
@ -98,7 +94,6 @@ mod tests {
refund_timelock: u32,
refund_address: ::bitcoin::Address,
transaction: ::bitcoin::Transaction,
#[serde(with = "ecdsa_fun_signature")]
tx_punish_sig: xmr_btc::bitcoin::Signature,
}

Loading…
Cancel
Save