From 797604ad73490b079e9262c7d8df9c81a7e70574 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 19 Nov 2023 18:01:13 -0500 Subject: [PATCH] Replace usage of `io::Error::new(io::ErrorKind::Other, ` with `io::Error::other` Newly possible with Rust 1.74. --- coins/bitcoin/src/wallet/mod.rs | 6 +-- coins/bitcoin/tests/wallet.rs | 2 +- coins/monero/src/lib.rs | 16 ++++---- coins/monero/src/ringct/clsag/multisig.rs | 4 +- coins/monero/src/ringct/mod.rs | 12 +++--- coins/monero/src/rpc/mod.rs | 39 ++++++++----------- coins/monero/src/serialize.rs | 14 +++---- coins/monero/src/transaction.rs | 28 +++++-------- coins/monero/src/wallet/extra.rs | 6 +-- coins/monero/src/wallet/scan.rs | 2 +- coins/monero/src/wallet/send/mod.rs | 6 +-- common/std-shims/src/io.rs | 4 ++ coordinator/src/tributary/mod.rs | 39 ++++++++----------- coordinator/tributary/src/lib.rs | 2 +- coordinator/tributary/src/tests/blockchain.rs | 2 +- coordinator/tributary/src/transaction.rs | 6 +-- crypto/ciphersuite/src/lib.rs | 6 +-- crypto/dkg/src/lib.rs | 9 ++--- crypto/dleq/src/cross_group/mod.rs | 6 +-- crypto/dleq/src/lib.rs | 4 +- crypto/frost/src/curve/mod.rs | 2 +- crypto/frost/src/nonce.rs | 2 +- processor/src/networks/bitcoin.rs | 13 +++---- processor/src/networks/mod.rs | 2 +- processor/src/plan.rs | 36 +++++++---------- 25 files changed, 114 insertions(+), 154 deletions(-) diff --git a/coins/bitcoin/src/wallet/mod.rs b/coins/bitcoin/src/wallet/mod.rs index 1036ccf8..3f099faa 100644 --- a/coins/bitcoin/src/wallet/mod.rs +++ b/coins/bitcoin/src/wallet/mod.rs @@ -91,10 +91,8 @@ impl ReceivedOutput { pub fn read(r: &mut R) -> io::Result { Ok(ReceivedOutput { offset: Secp256k1::read_F(r)?, - output: TxOut::consensus_decode(r) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid TxOut"))?, - outpoint: OutPoint::consensus_decode(r) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid OutPoint"))?, + output: TxOut::consensus_decode(r).map_err(|_| io::Error::other("invalid TxOut"))?, + outpoint: OutPoint::consensus_decode(r).map_err(|_| io::Error::other("invalid OutPoint"))?, }) } diff --git a/coins/bitcoin/tests/wallet.rs b/coins/bitcoin/tests/wallet.rs index e4616c3e..7106a456 100644 --- a/coins/bitcoin/tests/wallet.rs +++ b/coins/bitcoin/tests/wallet.rs @@ -304,7 +304,7 @@ async_sequential! { } // Make sure the change is correct - assert_eq!(needed_fee, u64::try_from(tx.weight()).unwrap() * FEE); + assert_eq!(needed_fee, u64::from(tx.weight()) * FEE); let input_value = output.value() + offset_output.value(); let output_value = tx.output.iter().map(|output| output.value.to_sat()).sum::(); assert_eq!(input_value - output_value, needed_fee); diff --git a/coins/monero/src/lib.rs b/coins/monero/src/lib.rs index a33e4049..fa2482df 100644 --- a/coins/monero/src/lib.rs +++ b/coins/monero/src/lib.rs @@ -141,7 +141,7 @@ impl Protocol { 0 => match read_byte(r)? { 14 => Protocol::v14, 16 => Protocol::v16, - _ => Err(io::Error::new(io::ErrorKind::Other, "unrecognized monero protocol"))?, + _ => Err(io::Error::other("unrecognized monero protocol"))?, }, // Custom 1 => match read_byte(r)? { @@ -150,26 +150,24 @@ impl Protocol { bp_plus: match read_byte(r)? { 0 => false, 1 => true, - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid bool serialization"))?, + _ => Err(io::Error::other("invalid bool serialization"))?, }, optimal_rct_type: RctType::from_byte(read_byte(r)?) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid RctType serialization"))?, + .ok_or_else(|| io::Error::other("invalid RctType serialization"))?, view_tags: match read_byte(r)? { 0 => false, 1 => true, - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid bool serialization"))?, + _ => Err(io::Error::other("invalid bool serialization"))?, }, v16_fee: match read_byte(r)? { 0 => false, 1 => true, - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid bool serialization"))?, + _ => Err(io::Error::other("invalid bool serialization"))?, }, }, - _ => { - Err(io::Error::new(io::ErrorKind::Other, "unrecognized custom protocol serialization"))? - } + _ => Err(io::Error::other("unrecognized custom protocol serialization"))?, }, - _ => Err(io::Error::new(io::ErrorKind::Other, "unrecognized protocol serialization"))?, + _ => Err(io::Error::other("unrecognized protocol serialization"))?, }) } } diff --git a/coins/monero/src/ringct/clsag/multisig.rs b/coins/monero/src/ringct/clsag/multisig.rs index d1f70333..9cb930ce 100644 --- a/coins/monero/src/ringct/clsag/multisig.rs +++ b/coins/monero/src/ringct/clsag/multisig.rs @@ -184,10 +184,10 @@ impl Algorithm for ClsagMultisig { reader.read_exact(&mut bytes)?; // dfg ensures the point is torsion free let xH = Option::::from(dfg::EdwardsPoint::from_bytes(&bytes)) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid key image"))?; + .ok_or_else(|| io::Error::other("invalid key image"))?; // Ensure this is a canonical point if xH.to_bytes() != bytes { - Err(io::Error::new(io::ErrorKind::Other, "non-canonical key image"))?; + Err(io::Error::other("non-canonical key image"))?; } Ok(ClsagAddendum { key_image: xH, dleq: DLEqProof::::read(reader)? }) diff --git a/coins/monero/src/ringct/mod.rs b/coins/monero/src/ringct/mod.rs index 608e129c..c86f9676 100644 --- a/coins/monero/src/ringct/mod.rs +++ b/coins/monero/src/ringct/mod.rs @@ -147,8 +147,8 @@ impl RctBase { } pub fn read(inputs: usize, outputs: usize, r: &mut R) -> io::Result<(RctBase, RctType)> { - let rct_type = RctType::from_byte(read_byte(r)?) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid RCT type"))?; + let rct_type = + RctType::from_byte(read_byte(r)?).ok_or_else(|| io::Error::other("invalid RCT type"))?; match rct_type { RctType::Null => {} @@ -164,7 +164,7 @@ impl RctBase { // If there are Bulletproofs, there must be a matching amount of outputs, implicitly // banning 0 outputs // Since HF 12 (CLSAG being 13), a 2-output minimum has also been enforced - Err(io::Error::new(io::ErrorKind::Other, "RCT with Bulletproofs(+) had 0 outputs"))?; + Err(io::Error::other("RCT with Bulletproofs(+) had 0 outputs"))?; } } } @@ -273,7 +273,7 @@ impl RctPrunable { // And then for RctNull, that's only allowed for miner TXs which require one input of // Input::Gen if decoys.is_empty() { - Err(io::Error::new(io::ErrorKind::Other, "transaction had no inputs"))?; + Err(io::Error::other("transaction had no inputs"))?; } Ok(match rct_type { @@ -295,7 +295,7 @@ impl RctPrunable { read_varint(r)? }) != 1 { - Err(io::Error::new(io::ErrorKind::Other, "n bulletproofs instead of one"))?; + Err(io::Error::other("n bulletproofs instead of one"))?; } Bulletproofs::read(r)? }, @@ -306,7 +306,7 @@ impl RctPrunable { RctType::Clsag | RctType::BulletproofsPlus => RctPrunable::Clsag { bulletproofs: { if read_varint::<_, u64>(r)? != 1 { - Err(io::Error::new(io::ErrorKind::Other, "n bulletproofs instead of one"))?; + Err(io::Error::other("n bulletproofs instead of one"))?; } (if rct_type == RctType::Clsag { Bulletproofs::read } else { Bulletproofs::read_plus })( r, diff --git a/coins/monero/src/rpc/mod.rs b/coins/monero/src/rpc/mod.rs index eaa8f4f4..0f0122d2 100644 --- a/coins/monero/src/rpc/mod.rs +++ b/coins/monero/src/rpc/mod.rs @@ -385,7 +385,7 @@ impl Rpc { let mut is_okay = false; if read_bytes::<_, { EPEE_HEADER.len() }>(&mut indexes)? != EPEE_HEADER { - Err(io::Error::new(io::ErrorKind::Other, "invalid header"))?; + Err(io::Error::other("invalid header"))?; } let read_object = |reader: &mut &[u8]| -> io::Result> { @@ -401,7 +401,7 @@ impl Rpc { let iters = if type_with_array_flag != kind { read_epee_vi(reader)? } else { 1 }; if (&name == b"o_indexes") && (kind != 5) { - Err(io::Error::new(io::ErrorKind::Other, "o_indexes weren't u64s"))?; + Err(io::Error::other("o_indexes weren't u64s"))?; } let f = match kind { @@ -428,28 +428,19 @@ impl Rpc { let len = read_epee_vi(reader)?; read_raw_vec( read_byte, - len - .try_into() - .map_err(|_| io::Error::new(io::ErrorKind::Other, "u64 length exceeded usize"))?, + len.try_into().map_err(|_| io::Error::other("u64 length exceeded usize"))?, reader, ) }, // bool 11 => |reader: &mut &[u8]| read_raw_vec(read_byte, 1, reader), // object, errors here as it shouldn't be used on this call - 12 => |_: &mut &[u8]| { - Err(io::Error::new( - io::ErrorKind::Other, - "node used object in reply to get_o_indexes", - )) - }, - // array, so far unused - 13 => |_: &mut &[u8]| { - Err(io::Error::new(io::ErrorKind::Other, "node used the unused array type")) - }, - _ => { - |_: &mut &[u8]| Err(io::Error::new(io::ErrorKind::Other, "node used an invalid type")) + 12 => { + |_: &mut &[u8]| Err(io::Error::other("node used object in reply to get_o_indexes")) } + // array, so far unused + 13 => |_: &mut &[u8]| Err(io::Error::other("node used the unused array type")), + _ => |_: &mut &[u8]| Err(io::Error::other("node used an invalid type")), }; let mut bytes_res = vec![]; @@ -461,21 +452,23 @@ impl Rpc { match name.as_slice() { b"o_indexes" => { for o_index in bytes_res { - actual_res.push(u64::from_le_bytes(o_index.try_into().map_err(|_| { - io::Error::new(io::ErrorKind::Other, "node didn't provide 8 bytes for a u64") - })?)); + actual_res.push(u64::from_le_bytes( + o_index + .try_into() + .map_err(|_| io::Error::other("node didn't provide 8 bytes for a u64"))?, + )); } res = Some(actual_res); } b"status" => { if bytes_res .first() - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "status wasn't a string"))? + .ok_or_else(|| io::Error::other("status wasn't a string"))? .as_slice() != b"OK" { // TODO: Better handle non-OK responses - Err(io::Error::new(io::ErrorKind::Other, "response wasn't OK"))?; + Err(io::Error::other("response wasn't OK"))?; } is_okay = true; } @@ -490,7 +483,7 @@ impl Rpc { // Didn't return a response with a status // (if the status wasn't okay, we would've already errored) if !is_okay { - Err(io::Error::new(io::ErrorKind::Other, "response didn't contain a status"))?; + Err(io::Error::other("response didn't contain a status"))?; } // If the Vec was empty, it would've been omitted, hence the unwrap_or diff --git a/coins/monero/src/serialize.rs b/coins/monero/src/serialize.rs index 971d9f71..fe7ca28f 100644 --- a/coins/monero/src/serialize.rs +++ b/coins/monero/src/serialize.rs @@ -100,19 +100,17 @@ pub(crate) fn read_varint(r: &mut R) -> io::Result 64) && (b >= (1 << (64 - bits))) { - Err(io::Error::new(io::ErrorKind::Other, "varint overflow"))?; + Err(io::Error::other("varint overflow"))?; } res += u64::from(b & (!VARINT_CONTINUATION_MASK)) << bits; bits += 7; b & VARINT_CONTINUATION_MASK == VARINT_CONTINUATION_MASK } {} - res - .try_into() - .map_err(|_| io::Error::new(io::ErrorKind::Other, "VarInt does not fit into integer type")) + res.try_into().map_err(|_| io::Error::other("VarInt does not fit into integer type")) } // All scalar fields supported by monero-serai are checked to be canonical for valid transactions @@ -123,7 +121,7 @@ pub(crate) fn read_varint(r: &mut R) -> io::Result(r: &mut R) -> io::Result { Option::from(Scalar::from_canonical_bytes(read_bytes(r)?)) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "unreduced scalar")) + .ok_or_else(|| io::Error::other("unreduced scalar")) } pub(crate) fn read_point(r: &mut R) -> io::Result { @@ -132,14 +130,14 @@ pub(crate) fn read_point(r: &mut R) -> io::Result { .decompress() // Ban points which are either unreduced or -0 .filter(|point| point.compress().to_bytes() == bytes) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid point")) + .ok_or_else(|| io::Error::other("invalid point")) } pub(crate) fn read_torsion_free_point(r: &mut R) -> io::Result { read_point(r) .ok() .filter(EdwardsPoint::is_torsion_free) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid point")) + .ok_or_else(|| io::Error::other("invalid point")) } pub(crate) fn read_raw_vec io::Result>( diff --git a/coins/monero/src/transaction.rs b/coins/monero/src/transaction.rs index 23f9dd7f..dceccfb2 100644 --- a/coins/monero/src/transaction.rs +++ b/coins/monero/src/transaction.rs @@ -67,9 +67,7 @@ impl Input { key_image: read_torsion_free_point(r)?, } } - _ => { - Err(io::Error::new(io::ErrorKind::Other, "Tried to deserialize unknown/unused input type"))? - } + _ => Err(io::Error::other("Tried to deserialize unknown/unused input type"))?, }) } } @@ -109,7 +107,7 @@ impl Output { let amount = read_varint(r)?; let amount = if rct { if amount != 0 { - Err(io::Error::new(io::ErrorKind::Other, "RCT TX output wasn't 0"))?; + Err(io::Error::other("RCT TX output wasn't 0"))?; } None } else { @@ -119,10 +117,7 @@ impl Output { let view_tag = match read_byte(r)? { 2 => false, 3 => true, - _ => Err(io::Error::new( - io::ErrorKind::Other, - "Tried to deserialize unknown/unused output type", - ))?, + _ => Err(io::Error::other("Tried to deserialize unknown/unused output type"))?, }; Ok(Output { @@ -220,14 +215,14 @@ impl TransactionPrefix { let version = read_varint(r)?; // TODO: Create an enum out of version if (version == 0) || (version > 2) { - Err(io::Error::new(io::ErrorKind::Other, "unrecognized transaction version"))?; + Err(io::Error::other("unrecognized transaction version"))?; } let timelock = Timelock::from_raw(read_varint(r)?); let inputs = read_vec(|r| Input::read(r), r)?; if inputs.is_empty() { - Err(io::Error::new(io::ErrorKind::Other, "transaction had no inputs"))?; + Err(io::Error::other("transaction had no inputs"))?; } let is_miner_tx = matches!(inputs[0], Input::Gen { .. }); @@ -310,9 +305,7 @@ impl Transaction { .inputs .iter() .map(|input| match input { - Input::Gen(..) => { - Err(io::Error::new(io::ErrorKind::Other, "Input::Gen present in non-coinbase v1 TX"))? - } + Input::Gen(..) => Err(io::Error::other("Input::Gen present in non-coinbase v1 TX"))?, // v1 TXs can burn v2 outputs // dcff3fe4f914d6b6bd4a5b800cc4cca8f2fdd1bd73352f0700d463d36812f328 is one such TX // It includes a pre-RCT signature for a RCT output, yet if you interpret the RCT @@ -326,16 +319,13 @@ impl Transaction { let mut out = 0; for output in &prefix.outputs { if output.amount.is_none() { - Err(io::Error::new(io::ErrorKind::Other, "v1 transaction had a 0-amount output"))?; + Err(io::Error::other("v1 transaction had a 0-amount output"))?; } out += output.amount.unwrap(); } if in_amount < out { - Err(io::Error::new( - io::ErrorKind::Other, - "transaction spent more than it had as inputs", - ))?; + Err(io::Error::other("transaction spent more than it had as inputs"))?; } rct_signatures.base.fee = in_amount - out; } @@ -353,7 +343,7 @@ impl Transaction { r, )?; } else { - Err(io::Error::new(io::ErrorKind::Other, "Tried to deserialize unknown version"))?; + Err(io::Error::other("Tried to deserialize unknown version"))?; } Ok(Transaction { prefix, signatures, rct_signatures }) diff --git a/coins/monero/src/wallet/extra.rs b/coins/monero/src/wallet/extra.rs index b8de7016..93cac861 100644 --- a/coins/monero/src/wallet/extra.rs +++ b/coins/monero/src/wallet/extra.rs @@ -62,7 +62,7 @@ impl PaymentId { Ok(match read_byte(r)? { 0 => PaymentId::Unencrypted(read_bytes(r)?), 1 => PaymentId::Encrypted(read_bytes(r)?), - _ => Err(io::Error::new(io::ErrorKind::Other, "unknown payment ID type"))?, + _ => Err(io::Error::other("unknown payment ID type"))?, }) } } @@ -106,13 +106,13 @@ impl ExtraField { 2 => ExtraField::Nonce({ let nonce = read_vec(read_byte, r)?; if nonce.len() > MAX_TX_EXTRA_NONCE_SIZE { - Err(io::Error::new(io::ErrorKind::Other, "too long nonce"))?; + Err(io::Error::other("too long nonce"))?; } nonce }), 3 => ExtraField::MergeMining(read_varint(r)?, read_bytes(r)?), 4 => ExtraField::PublicKeys(read_vec(read_point, r)?), - _ => Err(io::Error::new(io::ErrorKind::Other, "unknown extra field"))?, + _ => Err(io::Error::other("unknown extra field"))?, }) } } diff --git a/coins/monero/src/wallet/scan.rs b/coins/monero/src/wallet/scan.rs index 8b5b1650..611793fe 100644 --- a/coins/monero/src/wallet/scan.rs +++ b/coins/monero/src/wallet/scan.rs @@ -146,7 +146,7 @@ impl Metadata { let subaddress = if read_byte(r)? == 1 { Some( SubaddressIndex::new(read_u32(r)?, read_u32(r)?) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid subaddress in metadata"))?, + .ok_or_else(|| io::Error::other("invalid subaddress in metadata"))?, ) } else { None diff --git a/coins/monero/src/wallet/send/mod.rs b/coins/monero/src/wallet/send/mod.rs index 941ab34a..de7c0fe4 100644 --- a/coins/monero/src/wallet/send/mod.rs +++ b/coins/monero/src/wallet/send/mod.rs @@ -965,7 +965,7 @@ impl Eventuality { String::from_utf8(read_vec(read_byte, r)?) .ok() .and_then(|str| MoneroAddress::from_str_raw(&str).ok()) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid address")) + .ok_or_else(|| io::Error::other("invalid address")) } fn read_payment(r: &mut R) -> io::Result { @@ -977,12 +977,12 @@ impl Eventuality { view: match read_byte(r)? { 0 => None, 1 => Some(Zeroizing::new(read_scalar(r)?)), - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid change payment"))?, + _ => Err(io::Error::other("invalid change payment"))?, }, }, read_u64(r)?, ), - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid payment"))?, + _ => Err(io::Error::other("invalid payment"))?, }) } diff --git a/common/std-shims/src/io.rs b/common/std-shims/src/io.rs index 5f13aff4..5950d6ee 100644 --- a/common/std-shims/src/io.rs +++ b/common/std-shims/src/io.rs @@ -28,6 +28,10 @@ mod shims { Error { kind, error: Box::new(error) } } + pub fn other(error: E) -> Error { + Error { kind: ErrorKind::Other, error: Box::new(error) } + } + pub fn kind(&self) -> ErrorKind { self.kind } diff --git a/coordinator/src/tributary/mod.rs b/coordinator/src/tributary/mod.rs index b16305d4..8e0dee5f 100644 --- a/coordinator/src/tributary/mod.rs +++ b/coordinator/src/tributary/mod.rs @@ -151,8 +151,8 @@ impl TributarySpec { let mut network = [0; 1]; reader.read_exact(&mut network)?; - let network = NetworkId::decode(&mut &network[..]) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid network"))?; + let network = + NetworkId::decode(&mut &network[..]).map_err(|_| io::Error::other("invalid network"))?; let mut validators_len = [0; 4]; reader.read_exact(&mut validators_len)?; @@ -194,7 +194,7 @@ impl Debug for SignData ReadWrite for SignData { fn read(reader: &mut R) -> io::Result { let plan = Id::decode(&mut scale::IoReader(&mut *reader)) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid plan in SignData"))?; + .map_err(|_| io::Error::other("invalid plan in SignData"))?; let mut attempt = [0; 4]; reader.read_exact(&mut attempt)?; @@ -204,7 +204,7 @@ impl ReadWrite for SignDat let mut data_pieces = [0]; reader.read_exact(&mut data_pieces)?; if data_pieces[0] == 0 { - Err(io::Error::new(io::ErrorKind::Other, "zero pieces of data in SignData"))?; + Err(io::Error::other("zero pieces of data in SignData"))?; } let mut all_data = vec![]; for _ in 0 .. data_pieces[0] { @@ -236,7 +236,7 @@ impl ReadWrite for SignDat // Monero is limited to ~120 inputs per TX // // Bitcoin has a much higher input count of 520, yet it only uses 64 bytes per preprocess - Err(io::Error::new(io::ErrorKind::Other, "signing data exceeded 65535 bytes"))?; + Err(io::Error::other("signing data exceeded 65535 bytes"))?; } writer.write_all(&u16::try_from(data.len()).unwrap().to_le_bytes())?; writer.write_all(data)?; @@ -370,9 +370,8 @@ impl ReadWrite for Transaction { 0 => Ok(Transaction::RemoveParticipant({ let mut participant = [0; 2]; reader.read_exact(&mut participant)?; - Participant::new(u16::from_le_bytes(participant)).ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "invalid participant in RemoveParticipant") - })? + Participant::new(u16::from_le_bytes(participant)) + .ok_or_else(|| io::Error::other("invalid participant in RemoveParticipant"))? })), 1 => { @@ -385,15 +384,14 @@ impl ReadWrite for Transaction { reader.read_exact(&mut commitments_len)?; let commitments_len = usize::from(commitments_len[0]); if commitments_len == 0 { - Err(io::Error::new(io::ErrorKind::Other, "zero commitments in DkgCommitments"))?; + Err(io::Error::other("zero commitments in DkgCommitments"))?; } let mut each_commitments_len = [0; 2]; reader.read_exact(&mut each_commitments_len)?; let each_commitments_len = usize::from(u16::from_le_bytes(each_commitments_len)); if (commitments_len * each_commitments_len) > TRANSACTION_SIZE_LIMIT { - Err(io::Error::new( - io::ErrorKind::Other, + Err(io::Error::other( "commitments present in transaction exceeded transaction size limit", ))?; } @@ -454,15 +452,13 @@ impl ReadWrite for Transaction { let mut accuser = [0; 2]; reader.read_exact(&mut accuser)?; - let accuser = Participant::new(u16::from_le_bytes(accuser)).ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "invalid participant in InvalidDkgShare") - })?; + let accuser = Participant::new(u16::from_le_bytes(accuser)) + .ok_or_else(|| io::Error::other("invalid participant in InvalidDkgShare"))?; let mut faulty = [0; 2]; reader.read_exact(&mut faulty)?; - let faulty = Participant::new(u16::from_le_bytes(faulty)).ok_or_else(|| { - io::Error::new(io::ErrorKind::Other, "invalid participant in InvalidDkgShare") - })?; + let faulty = Participant::new(u16::from_le_bytes(faulty)) + .ok_or_else(|| io::Error::other("invalid participant in InvalidDkgShare"))?; let mut blame_len = [0; 2]; reader.read_exact(&mut blame_len)?; @@ -534,7 +530,7 @@ impl ReadWrite for Transaction { Ok(Transaction::SignCompleted { plan, tx_hash, first_signer, signature }) } - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid transaction type")), + _ => Err(io::Error::other("invalid transaction type")), } } @@ -549,15 +545,12 @@ impl ReadWrite for Transaction { writer.write_all(&[1])?; writer.write_all(&attempt.to_le_bytes())?; if commitments.is_empty() { - Err(io::Error::new(io::ErrorKind::Other, "zero commitments in DkgCommitments"))? + Err(io::Error::other("zero commitments in DkgCommitments"))? } writer.write_all(&[u8::try_from(commitments.len()).unwrap()])?; for commitments_i in commitments { if commitments_i.len() != commitments[0].len() { - Err(io::Error::new( - io::ErrorKind::Other, - "commitments of differing sizes in DkgCommitments", - ))? + Err(io::Error::other("commitments of differing sizes in DkgCommitments"))? } } writer.write_all(&u16::try_from(commitments[0].len()).unwrap().to_le_bytes())?; diff --git a/coordinator/tributary/src/lib.rs b/coordinator/tributary/src/lib.rs index 12623373..752178c3 100644 --- a/coordinator/tributary/src/lib.rs +++ b/coordinator/tributary/src/lib.rs @@ -81,7 +81,7 @@ impl ReadWrite for Transaction { let tx = T::read(reader)?; Ok(Transaction::Application(tx)) } - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid transaction type")), + _ => Err(io::Error::other("invalid transaction type")), } } fn write(&self, writer: &mut W) -> io::Result<()> { diff --git a/coordinator/tributary/src/tests/blockchain.rs b/coordinator/tributary/src/tests/blockchain.rs index 70a8c48c..79f304f0 100644 --- a/coordinator/tributary/src/tests/blockchain.rs +++ b/coordinator/tributary/src/tests/blockchain.rs @@ -412,7 +412,7 @@ async fn block_tx_ordering() { match kind[0] { 0 => Ok(SignedTx::Signed(Box::new(SignedTransaction::read(reader)?))), 1 => Ok(SignedTx::Provided(Box::new(ProvidedTransaction::read(reader)?))), - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid transaction type")), + _ => Err(io::Error::other("invalid transaction type")), } } diff --git a/coordinator/tributary/src/transaction.rs b/coordinator/tributary/src/transaction.rs index c9c5ba1a..221db619 100644 --- a/coordinator/tributary/src/transaction.rs +++ b/coordinator/tributary/src/transaction.rs @@ -55,7 +55,7 @@ impl ReadWrite for Signed { reader.read_exact(&mut nonce)?; let nonce = u32::from_le_bytes(nonce); if nonce >= (u32::MAX - 1) { - Err(io::Error::new(io::ErrorKind::Other, "nonce exceeded limit"))?; + Err(io::Error::other("nonce exceeded limit"))?; } let mut signature = SchnorrSignature::::read(reader)?; @@ -64,7 +64,7 @@ impl ReadWrite for Signed { // We should never produce zero signatures though meaning this should never come up // If it does somehow come up, this is a decent courtesy signature.zeroize(); - Err(io::Error::new(io::ErrorKind::Other, "signature nonce was identity"))?; + Err(io::Error::other("signature nonce was identity"))?; } Ok(Signed { signer, nonce, signature }) @@ -73,7 +73,7 @@ impl ReadWrite for Signed { fn write(&self, writer: &mut W) -> io::Result<()> { // This is either an invalid signature or a private key leak if self.signature.R.is_identity().into() { - Err(io::Error::new(io::ErrorKind::Other, "signature nonce was identity"))?; + Err(io::Error::other("signature nonce was identity"))?; } writer.write_all(&self.signer.to_bytes())?; writer.write_all(&self.nonce.to_le_bytes())?; diff --git a/crypto/ciphersuite/src/lib.rs b/crypto/ciphersuite/src/lib.rs index 18ce1290..3954047d 100644 --- a/crypto/ciphersuite/src/lib.rs +++ b/crypto/ciphersuite/src/lib.rs @@ -93,7 +93,7 @@ pub trait Ciphersuite: // ff mandates this is canonical let res = Option::::from(Self::F::from_repr(encoding)) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "non-canonical scalar")); + .ok_or_else(|| io::Error::other("non-canonical scalar")); encoding.as_mut().zeroize(); res } @@ -106,9 +106,9 @@ pub trait Ciphersuite: reader.read_exact(encoding.as_mut())?; let point = Option::::from(Self::G::from_bytes(&encoding)) - .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "invalid point"))?; + .ok_or_else(|| io::Error::other("invalid point"))?; if point.to_bytes().as_ref() != encoding.as_ref() { - Err(io::Error::new(io::ErrorKind::Other, "non-canonical point"))?; + Err(io::Error::other("non-canonical point"))?; } Ok(point) } diff --git a/crypto/dkg/src/lib.rs b/crypto/dkg/src/lib.rs index 574cff39..9648a6a6 100644 --- a/crypto/dkg/src/lib.rs +++ b/crypto/dkg/src/lib.rs @@ -306,8 +306,7 @@ mod lib { /// Read keys from a type satisfying std::io::Read. pub fn read(reader: &mut R) -> io::Result> { { - let different = - || io::Error::new(io::ErrorKind::Other, "deserializing ThresholdCore for another curve"); + let different = || io::Error::other("deserializing ThresholdCore for another curve"); let mut id_len = [0; 4]; reader.read_exact(&mut id_len)?; @@ -331,8 +330,7 @@ mod lib { ( read_u16()?, read_u16()?, - Participant::new(read_u16()?) - .ok_or(io::Error::new(io::ErrorKind::Other, "invalid participant index"))?, + Participant::new(read_u16()?).ok_or(io::Error::other("invalid participant index"))?, ) }; @@ -344,8 +342,7 @@ mod lib { } Ok(ThresholdCore::new( - ThresholdParams::new(t, n, i) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid parameters"))?, + ThresholdParams::new(t, n, i).map_err(|_| io::Error::other("invalid parameters"))?, secret_share, verification_shares, )) diff --git a/crypto/dleq/src/cross_group/mod.rs b/crypto/dleq/src/cross_group/mod.rs index 8087a273..17264866 100644 --- a/crypto/dleq/src/cross_group/mod.rs +++ b/crypto/dleq/src/cross_group/mod.rs @@ -55,11 +55,9 @@ pub(crate) fn read_point(r: &mut R) -> io::Result { let mut repr = G::Repr::default(); r.read_exact(repr.as_mut())?; let point = G::from_bytes(&repr); - let Some(point) = Option::::from(point) else { - Err(io::Error::new(io::ErrorKind::Other, "invalid point"))? - }; + let Some(point) = Option::::from(point) else { Err(io::Error::other("invalid point"))? }; if point.to_bytes().as_ref() != repr.as_ref() { - Err(io::Error::new(io::ErrorKind::Other, "non-canonical point"))?; + Err(io::Error::other("non-canonical point"))?; } Ok(point) } diff --git a/crypto/dleq/src/lib.rs b/crypto/dleq/src/lib.rs index 40f578b9..ae539192 100644 --- a/crypto/dleq/src/lib.rs +++ b/crypto/dleq/src/lib.rs @@ -14,7 +14,7 @@ use ff::{Field, PrimeField}; use group::prime::PrimeGroup; #[cfg(feature = "serialize")] -use std::io::{self, ErrorKind, Error, Read, Write}; +use std::io::{self, Error, Read, Write}; /// A cross-group DLEq proof capable of proving that two public keys, across two different curves, /// share a private key. @@ -91,7 +91,7 @@ fn read_scalar(r: &mut R) -> io::Result { r.read_exact(repr.as_mut())?; let scalar = F::from_repr(repr); if scalar.is_none().into() { - Err(Error::new(ErrorKind::Other, "invalid scalar"))?; + Err(Error::other("invalid scalar"))?; } Ok(scalar.unwrap()) } diff --git a/crypto/frost/src/curve/mod.rs b/crypto/frost/src/curve/mod.rs index f79c10d2..5012a619 100644 --- a/crypto/frost/src/curve/mod.rs +++ b/crypto/frost/src/curve/mod.rs @@ -125,7 +125,7 @@ pub trait Curve: Ciphersuite { fn read_G(reader: &mut R) -> io::Result { let res = ::read_G(reader)?; if res.is_identity().into() { - Err(io::Error::new(io::ErrorKind::Other, "identity point"))?; + Err(io::Error::other("identity point"))?; } Ok(res) } diff --git a/crypto/frost/src/nonce.rs b/crypto/frost/src/nonce.rs index 89b95240..921480a0 100644 --- a/crypto/frost/src/nonce.rs +++ b/crypto/frost/src/nonce.rs @@ -223,7 +223,7 @@ impl Commitments { let dleq = MultiDLEqProof::read(reader, dleq_generators.len())?; dleq .verify(&mut dleq_transcript::(context), &dleq_generators, &dleq_nonces) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid DLEq proof"))?; + .map_err(|_| io::Error::other("invalid DLEq proof"))?; Some(dleq) } else { None diff --git a/processor/src/networks/bitcoin.rs b/processor/src/networks/bitcoin.rs index efcea0bb..6bd2d1fb 100644 --- a/processor/src/networks/bitcoin.rs +++ b/processor/src/networks/bitcoin.rs @@ -184,8 +184,7 @@ impl TransactionTrait for Transaction { buf } fn read(reader: &mut R) -> io::Result { - Transaction::consensus_decode(reader) - .map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e}"))) + Transaction::consensus_decode(reader).map_err(|e| io::Error::other(format!("{e}"))) } #[cfg(test)] @@ -223,12 +222,10 @@ impl EventualityTrait for Eventuality { } fn read(reader: &mut R) -> io::Result { - let plan_binding_input = OutPoint::consensus_decode(reader).map_err(|_| { - io::Error::new(io::ErrorKind::Other, "couldn't decode outpoint in eventuality") - })?; - let outputs = Vec::::consensus_decode(reader).map_err(|_| { - io::Error::new(io::ErrorKind::Other, "couldn't decode outputs in eventuality") - })?; + let plan_binding_input = OutPoint::consensus_decode(reader) + .map_err(|_| io::Error::other("couldn't decode outpoint in eventuality"))?; + let outputs = Vec::::consensus_decode(reader) + .map_err(|_| io::Error::other("couldn't decode outputs in eventuality"))?; Ok(Eventuality { plan_binding_input, outputs }) } fn serialize(&self) -> Vec { diff --git a/processor/src/networks/mod.rs b/processor/src/networks/mod.rs index 3a19e132..d77d43f1 100644 --- a/processor/src/networks/mod.rs +++ b/processor/src/networks/mod.rs @@ -94,7 +94,7 @@ impl OutputType { 1 => OutputType::Branch, 2 => OutputType::Change, 3 => OutputType::Forwarded, - _ => Err(io::Error::new(io::ErrorKind::Other, "invalid OutputType"))?, + _ => Err(io::Error::other("invalid OutputType"))?, }) } } diff --git a/processor/src/plan.rs b/processor/src/plan.rs index 6dcf74da..35146a9c 100644 --- a/processor/src/plan.rs +++ b/processor/src/plan.rs @@ -34,7 +34,7 @@ impl Payment { .address .clone() .try_into() - .map_err(|_| io::Error::new(io::ErrorKind::Other, "address couldn't be serialized"))?; + .map_err(|_| io::Error::other("address couldn't be serialized"))?; writer.write_all(&u32::try_from(address.len()).unwrap().to_le_bytes())?; writer.write_all(&address)?; @@ -52,8 +52,7 @@ impl Payment { reader.read_exact(&mut buf)?; let mut address = vec![0; usize::try_from(u32::from_le_bytes(buf)).unwrap()]; reader.read_exact(&mut address)?; - let address = N::Address::try_from(address) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid address"))?; + let address = N::Address::try_from(address).map_err(|_| io::Error::other("invalid address"))?; let mut buf = [0; 1]; reader.read_exact(&mut buf)?; @@ -68,7 +67,7 @@ impl Payment { }; let balance = Balance::decode(&mut scale::IoReader(reader)) - .map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid balance"))?; + .map_err(|_| io::Error::other("invalid balance"))?; Ok(Payment { address, data, balance }) } @@ -152,13 +151,10 @@ impl Plan { // TODO: Have Plan construction fail if change cannot be serialized let change = if let Some(change) = &self.change { change.clone().try_into().map_err(|_| { - io::Error::new( - io::ErrorKind::Other, - format!( - "an address we said to use as change couldn't be convered to a Vec: {}", - change.to_string(), - ), - ) + io::Error::other(format!( + "an address we said to use as change couldn't be convered to a Vec: {}", + change.to_string(), + )) })? } else { vec![] @@ -188,16 +184,14 @@ impl Plan { reader.read_exact(&mut len)?; let mut change = vec![0; usize::from(len[0])]; reader.read_exact(&mut change)?; - let change = if change.is_empty() { - None - } else { - Some(N::Address::try_from(change).map_err(|_| { - io::Error::new( - io::ErrorKind::Other, - "couldn't deserialize an Address serialized into a Plan", - ) - })?) - }; + let change = + if change.is_empty() { + None + } else { + Some(N::Address::try_from(change).map_err(|_| { + io::Error::other("couldn't deserialize an Address serialized into a Plan") + })?) + }; Ok(Plan { key, inputs, payments, change }) }