Implement human-readable serialization for monero private key

Some of the swap states were unable to be serialized using
serde_json because this was not implemented.
This commit is contained in:
rishflab 2021-10-06 18:58:13 +11:00
parent 8fb15ba7aa
commit cdfc8419ad
4 changed files with 45 additions and 10 deletions

1
Cargo.lock generated
View File

@ -4128,6 +4128,7 @@ dependencies = [
"ed25519-dalek", "ed25519-dalek",
"futures", "futures",
"get-port", "get-port",
"hex 0.4.3",
"hyper 0.14.12", "hyper 0.14.12",
"itertools 0.10.1", "itertools 0.10.1",
"libp2p", "libp2p",

View File

@ -29,6 +29,7 @@ directories-next = "2"
ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde" ] } ecdsa_fun = { git = "https://github.com/LLFourn/secp256kfun", default-features = false, features = [ "libsecp_compat", "serde" ] }
ed25519-dalek = "1" ed25519-dalek = "1"
futures = { version = "0.3", default-features = false } futures = { version = "0.3", default-features = false }
hex = "0.4"
itertools = "0.10" itertools = "0.10"
libp2p = { git = "https://github.com/comit-network/rust-libp2p", branch = "rendezvous", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous" ] } libp2p = { git = "https://github.com/comit-network/rust-libp2p", branch = "rendezvous", default-features = false, features = [ "tcp-tokio", "yamux", "mplex", "dns-tokio", "noise", "request-response", "websocket", "ping", "rendezvous" ] }
miniscript = { version = "5", features = [ "serde" ] } miniscript = { version = "5", features = [ "serde" ] }

View File

@ -234,6 +234,15 @@ pub mod monero_private_key {
let mut s = s; let mut s = s;
PrivateKey::consensus_decode(&mut s).map_err(|err| E::custom(format!("{:?}", err))) PrivateKey::consensus_decode(&mut s).map_err(|err| E::custom(format!("{:?}", err)))
} }
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
let bytes = hex::decode(s).map_err(|err| E::custom(format!("{:?}", err)))?;
PrivateKey::consensus_decode(&mut bytes.as_slice())
.map_err(|err| E::custom(format!("{:?}", err)))
}
} }
pub fn serialize<S>(x: &PrivateKey, s: S) -> Result<S::Ok, S::Error> pub fn serialize<S>(x: &PrivateKey, s: S) -> Result<S::Ok, S::Error>
@ -243,8 +252,12 @@ pub mod monero_private_key {
let mut bytes = Cursor::new(vec![]); let mut bytes = Cursor::new(vec![]);
x.consensus_encode(&mut bytes) x.consensus_encode(&mut bytes)
.map_err(|err| S::Error::custom(format!("{:?}", err)))?; .map_err(|err| S::Error::custom(format!("{:?}", err)))?;
if s.is_human_readable() {
s.serialize_str(&hex::encode(bytes.into_inner()))
} else {
s.serialize_bytes(bytes.into_inner().as_ref()) s.serialize_bytes(bytes.into_inner().as_ref())
} }
}
pub fn deserialize<'de, D>( pub fn deserialize<'de, D>(
deserializer: D, deserializer: D,
@ -252,7 +265,13 @@ pub mod monero_private_key {
where where
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
let key = deserializer.deserialize_bytes(BytesVisitor)?; let key = {
if deserializer.is_human_readable() {
deserializer.deserialize_string(BytesVisitor)?
} else {
deserializer.deserialize_bytes(BytesVisitor)?
}
};
Ok(key) Ok(key)
} }
} }
@ -351,7 +370,17 @@ mod tests {
pub struct MoneroAmount(#[serde(with = "monero_amount")] crate::monero::Amount); pub struct MoneroAmount(#[serde(with = "monero_amount")] crate::monero::Amount);
#[test] #[test]
fn serde_monero_private_key() { fn serde_monero_private_key_json() {
let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(
crate::monero::Scalar::random(&mut OsRng),
));
let encoded = serde_json::to_vec(&key).unwrap();
let decoded: MoneroPrivateKey = serde_json::from_slice(&encoded).unwrap();
assert_eq!(key, decoded);
}
#[test]
fn serde_monero_private_key_cbor() {
let key = MoneroPrivateKey(monero::PrivateKey::from_scalar( let key = MoneroPrivateKey(monero::PrivateKey::from_scalar(
crate::monero::Scalar::random(&mut OsRng), crate::monero::Scalar::random(&mut OsRng),
)); ));

View File

@ -18,6 +18,7 @@ use swap::asb::FixedRate;
use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund}; use swap::bitcoin::{CancelTimelock, PunishTimelock, TxCancel, TxPunish, TxRedeem, TxRefund};
use swap::database::SledDatabase; use swap::database::SledDatabase;
use swap::env::{Config, GetConfig}; use swap::env::{Config, GetConfig};
use swap::fs::ensure_directory_exists;
use swap::network::swarm; use swap::network::swarm;
use swap::protocol::alice::{AliceState, Swap}; use swap::protocol::alice::{AliceState, Swap};
use swap::protocol::bob::BobState; use swap::protocol::bob::BobState;
@ -156,13 +157,16 @@ async fn init_containers(cli: &Cli) -> (Monero, Containers<'_>) {
.await .await
.unwrap(); .unwrap();
(monero, Containers { (
monero,
Containers {
bitcoind_url, bitcoind_url,
bitcoind, bitcoind,
monerod_container, monerod_container,
monero_wallet_rpc_containers, monero_wallet_rpc_containers,
electrs, electrs,
}) },
)
} }
async fn init_bitcoind_container( async fn init_bitcoind_container(