From e7d94e940ced7eec0c6eabe7b8c1a7a77de2433b Mon Sep 17 00:00:00 2001 From: Alright Date: Tue, 26 Nov 2024 16:53:01 -0500 Subject: [PATCH] Commit to using "pubkey padding hack" for now. The typing for pubkeys in various trait methods assumes pubkeys will be 33 bytes. Sia pubkeys(ed25519) are 32 bytes. This is a remnant from when KDF only supported BTC-like and ETH-like coins. --- mm2src/coins/siacoin.rs | 73 ++++++++++++++++++++++++++++------- mm2src/coins/siacoin/error.rs | 18 ++++++++- 2 files changed, 76 insertions(+), 15 deletions(-) diff --git a/mm2src/coins/siacoin.rs b/mm2src/coins/siacoin.rs index ce3bfd229d..dc1f06e148 100644 --- a/mm2src/coins/siacoin.rs +++ b/mm2src/coins/siacoin.rs @@ -916,7 +916,13 @@ impl SiaCoin { let my_keypair = self.my_keypair().map_err(SendMakerPaymentError::MyKeypair)?; let maker_public_key = my_keypair.public(); - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pubkey.len() != 33 { + return Err(SendMakerPaymentError::InvalidTakerPublicKeyLength( + args.other_pubkey.to_vec(), + )); + } let taker_public_key = PublicKey::from_bytes(&args.other_pubkey[..32]).map_err(SendMakerPaymentError::InvalidTakerPublicKey)?; @@ -963,7 +969,13 @@ impl SiaCoin { let my_keypair = self.my_keypair().map_err(SendTakerPaymentError::MyKeypair)?; let taker_public_key = my_keypair.public(); - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pubkey.len() != 33 { + return Err(SendTakerPaymentError::InvalidMakerPublicKeyLength( + args.other_pubkey.to_vec(), + )); + } let maker_public_key = PublicKey::from_bytes(&args.other_pubkey[..32]).map_err(SendTakerPaymentError::InvalidMakerPublicKey)?; @@ -1010,7 +1022,13 @@ impl SiaCoin { let my_keypair = self.my_keypair().map_err(MakerSpendsTakerPaymentError::MyKeypair)?; let maker_public_key = my_keypair.public(); - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pubkey.len() != 33 { + return Err(MakerSpendsTakerPaymentError::InvalidTakerPublicKeyLength( + args.other_pubkey.to_vec(), + )); + } let taker_public_key = PublicKey::from_bytes(&args.other_pubkey[..32]) .map_err(MakerSpendsTakerPaymentError::InvalidTakerPublicKey)?; @@ -1067,7 +1085,13 @@ impl SiaCoin { let my_keypair = self.my_keypair().map_err(TakerSpendsMakerPaymentError::MyKeypair)?; let taker_public_key = my_keypair.public(); - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pubkey.len() != 33 { + return Err(TakerSpendsMakerPaymentError::InvalidMakerPublicKeyLength( + args.other_pubkey.to_vec(), + )); + }; let maker_public_key = PublicKey::from_bytes(&args.other_pubkey[..32]) .map_err(TakerSpendsMakerPaymentError::InvalidMakerPublicKey)?; @@ -1497,7 +1521,7 @@ impl TryFrom for SiaValidatePaymentInput { let payment_tx = SiaTransaction::try_from(args.payment_tx.to_vec()).map_err(SiaValidatePaymentInputError::ParseTx)?; - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey if args.other_pub.len() != 33 { return Err(SiaValidatePaymentInputError::InvalidOtherPublicKeyLength( args.other_pub, @@ -1534,7 +1558,12 @@ impl TryFrom> for SiaRefundPaymentArgs { let time_lock = args.time_lock; - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pubkey.len() != 33 { + return Err(SiaRefundPaymentArgsError::InvalidOtherPublicKeyLength( + args.other_pubkey.to_vec(), + )); + } let success_public_key = PublicKey::from_bytes(&args.other_pubkey[..32]).map_err(SiaRefundPaymentArgsError::ParseOtherPublicKey)?; @@ -1586,7 +1615,13 @@ impl TryFrom> for SiaValidateFeeArgs { wrong_variant => return Err(SiaValidateFeeArgsError::TxEnumVariant(wrong_variant.clone())), }; - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.expected_sender.len() != 33 { + return Err(SiaValidateFeeArgsError::InvalidTakerPublicKeyLength( + args.expected_sender.to_vec(), + )); + } + let expected_sender_public_key = PublicKey::from_bytes(&args.expected_sender[..32]) .map_err(SiaValidateFeeArgsError::InvalidTakerPublicKey)?; @@ -1662,7 +1697,13 @@ impl TryFrom> for SiaCheckIfMyPaymentSentArgs { fn try_from(args: CheckIfMyPaymentSentArgs<'_>) -> Result { let time_lock = args.time_lock; - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if args.other_pub.len() != 33 { + return Err(SiaCheckIfMyPaymentSentArgsError::InvalidOtherPublicKeyLength( + args.other_pub.to_vec(), + )); + } let success_public_key = PublicKey::from_bytes(&args.other_pub[..32]) .map_err(SiaCheckIfMyPaymentSentArgsError::ParseOtherPublicKey)?; let secret_hash = @@ -1803,15 +1844,14 @@ impl SwapOps for SiaCoin { /// Return the iguana ed25519 public key /// This is the public key that will be used inside the HTLC SpendPolicy - // TODO Alright - method signature needs to change to use Result<> + // TODO Alright - MakerSwapData is badly designed and assumes this is a 33 byte array aka H264 + // we pad it then drop the last byte when we use it for now fn derive_htlc_pubkey(&self, _swap_unique_data: &[u8]) -> Vec { let my_keypair = self .my_keypair() .expect("SiaCoin::derive_htlc_pubkey: failed to get my_keypair"); let mut ret = my_keypair.public().to_bytes().to_vec(); - // FIXME Alright - MakerSwapData is badly designed and assumes this is a 33 byte array aka H264 - // we will pad it then drop the last byte when we use it for now ret.push(0u8); ret } @@ -1827,10 +1867,17 @@ impl SwapOps for SiaCoin { /// Validate the PublicKey the other party provided /// The other party generates this PublicKey via SwapOps::derive_htlc_pubkey fn validate_other_pubkey(&self, raw_pubkey: &[u8]) -> MmResult<(), ValidateOtherPubKeyErr> { - // FIXME Alright - pubkey padding hack, see SiaCoin::derive_htlc_pubkey + // TODO Alright - pubkey padding, see SiaCoin::derive_htlc_pubkey + if raw_pubkey.len() != 33 { + return Err(ValidateOtherPubKeyErr::InvalidPubKey(format!( + "SiaCoin::validate_other_pubkey: invalid raw_pubkey, expected 33 bytes found: {:?}", + raw_pubkey.to_vec() + )) + .into()); + } let _public_key = PublicKey::from_bytes(&raw_pubkey[..32]).map_err(|e| { ValidateOtherPubKeyErr::InvalidPubKey(format!( - "SiaCoin::validate_other_pubkey validate pubkey:{:?} failed: {}", + "SiaCoin::validate_other_pubkey: validate pubkey:{:?} failed: {}", raw_pubkey, e )) })?; diff --git a/mm2src/coins/siacoin/error.rs b/mm2src/coins/siacoin/error.rs index 3ba50b35f4..df8bc2ed33 100644 --- a/mm2src/coins/siacoin/error.rs +++ b/mm2src/coins/siacoin/error.rs @@ -35,6 +35,8 @@ pub enum SendTakerFeeError { #[derive(Debug, Error)] pub enum SendMakerPaymentError { + #[error("SiaCoin::new_send_maker_payment: invalid taker pubkey, expected 33 bytes found: {0:?}")] + InvalidTakerPublicKeyLength(Vec), #[error("SiaCoin::new_send_maker_payment: invalid taker pubkey {0}")] InvalidTakerPublicKey(#[from] PublicKeyError), #[error("SiaCoin::new_send_maker_payment: failed to fetch my_keypair {0}")] @@ -51,6 +53,8 @@ pub enum SendMakerPaymentError { #[derive(Debug, Error)] pub enum SendTakerPaymentError { + #[error("SiaCoin::new_send_taker_payment: invalid maker pubkey, expected 33 bytes found: {0:?}")] + InvalidMakerPublicKeyLength(Vec), #[error("SiaCoin::new_send_taker_payment: invalid maker pubkey {0}")] InvalidMakerPublicKey(#[from] PublicKeyError), #[error("SiaCoin::new_send_taker_payment: failed to fetch my_keypair {0}")] @@ -132,6 +136,8 @@ pub enum ValidateFeeError { pub enum TakerSpendsMakerPaymentError { #[error("SiaCoin::new_send_taker_spends_maker_payment: failed to fetch my_keypair {0}")] MyKeypair(#[from] SiaCoinError), + #[error("SiaCoin::new_send_taker_spends_maker_payment: invalid maker pubkey, expected 33 bytes found: {0:?}")] + InvalidMakerPublicKeyLength(Vec), #[error("SiaCoin::new_send_taker_spends_maker_payment: invalid maker pubkey {0}")] InvalidMakerPublicKey(#[from] PublicKeyError), #[error("SiaCoin::new_send_taker_spends_maker_paymentt: failed to parse taker_payment_tx {0}")] @@ -152,6 +158,8 @@ pub enum TakerSpendsMakerPaymentError { pub enum MakerSpendsTakerPaymentError { #[error("SiaCoin::new_send_maker_spends_taker_payment: failed to fetch my_keypair {0}")] MyKeypair(#[from] SiaCoinError), + #[error("SiaCoin::new_send_maker_spends_taker_payment: invalid taker pubkey, expected 33 bytes found: {0:?}")] + InvalidTakerPublicKeyLength(Vec), #[error("SiaCoin::new_send_maker_spends_taker_payment: invalid taker pubkey {0}")] InvalidTakerPublicKey(#[from] PublicKeyError), #[error("SiaCoin::new_send_maker_spends_taker_payment: failed to parse taker_payment_tx {0}")] @@ -170,10 +178,12 @@ pub enum MakerSpendsTakerPaymentError { #[derive(Debug, Error)] pub enum SiaRefundPaymentArgsError { - #[error("SiaRefundPaymentArgs::TryFrom: failed to parse other_pubkey {0}")] - ParseOtherPublicKey(#[from] PublicKeyError), #[error("SiaRefundPaymentArgs::TryFrom: failed to parse payment_tx {0}")] ParseTx(#[from] SiaTransactionError), + #[error("SiaRefundPaymentArgs::TryFrom: invalid other_pubkey, expected 33 bytes found: {0:?}")] + InvalidOtherPublicKeyLength(Vec), + #[error("SiaRefundPaymentArgs::TryFrom: failed to parse other_pubkey {0}")] + ParseOtherPublicKey(#[from] PublicKeyError), #[error("SiaRefundPaymentArgs::TryFrom: failed to parse secret_hash {0}")] ParseSecretHash(#[from] Hash256Error), // SwapTxTypeVariant uses String Debug trait representation to avoid explicit lifetime annotations @@ -188,6 +198,8 @@ pub enum SiaValidateFeeArgsError { ParseUuid(#[from] uuid::Error), #[error("SiaValidateFeeArgs::TryFrom: Unexpected Uuid version {0}")] UuidVersion(usize), + #[error("SiaValidateFeeArgs::TryFrom: invalid taker pubkey, expected 33 bytes found: {0:?}")] + InvalidTakerPublicKeyLength(Vec), #[error("SiaValidateFeeArgs::TryFrom: invalid taker pubkey {0}")] InvalidTakerPublicKey(#[from] PublicKeyError), #[error("SiaValidateFeeArgs::TryFrom: failed to convert trade_fee_amount to Currency {0}")] @@ -235,6 +247,8 @@ pub enum SiaCoinError { #[derive(Debug, Error)] pub enum SiaCheckIfMyPaymentSentArgsError { + #[error("SiaCheckIfMyPaymentSentArgs::TryFrom: invalid other_pub, expected 33 bytes found: {0:?}")] + InvalidOtherPublicKeyLength(Vec), #[error("SiaCheckIfMyPaymentSentArgs::TryFrom: failed to parse other_pub {0}")] ParseOtherPublicKey(#[from] PublicKeyError), #[error("SiaCheckIfMyPaymentSentArgs::TryFrom: failed to parse secret_hash {0}")]