CR suggestions and additional improvements
DELETE
WalletWasabi/CoinJoin/Common/Models/InputOwnershipProofModel.cs
-using NBitcoin;
-using Newtonsoft.Json;
-using System.ComponentModel.DataAnnotations;
-using WalletWasabi.JsonConverters;
-
-namespace WalletWasabi.CoinJoin.Common.Models
-{
- public class InputOwnershipProofModel
- {
- [Required]
- [JsonConverter(typeof(OutPointAsTxoRefJsonConverter))]
- public OutPoint Input { get; set; }
-
- // Minimum length of P2WPHK proof of ownership is (4 + 1 + 1 + 32) + (1 + 1 + 1 + (1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1) + 1 + 33) = 84
- // Typical length of P2WPHK proof of ownership is (4 + 1 + 1 + 32) + (1 + 1 + 1 + (1 + 1 + 1 + 1 + 32 + 1 + 1 + 32 + 1) + 1 + 33) = 147
- // Maximum length of P2WPHK proof of ownership is (4 + 1 + 1 + 32) + (8 + 8 + 8 + (1 + 1 + 1 + 1 + 33 + 1 + 1 + 33 + 1) + 8 + 33) = 176
- [Required]
- [MinLength(84, ErrorMessage = "Provided proof is invalid")]
- [MaxLength(176, ErrorMessage = "Provided proof is invalid")]
- [JsonConverter(typeof(ByteArrayJsonConverter))]
- public byte[] Proof { get; set; }
- }
-}

MODIFY
WalletWasabi/Crypto/Bip322Signature.cs
WalletWasabi/Crypto/Bip322Signature.cs
@@ -35,17 +35,35 @@ namespace WalletWasabi.Crypto
}
}
- public bool Verify(uint256 hash, Script scriptPubKey)
+ public static Bip322Signature FromBytes(byte[] bip322SignatureBytes) =>
+ NBitcoinExtensions.FromBytes<Bip322Signature>(bip322SignatureBytes);
+
+ public bool Verify(uint256 hash, Script scriptPubKey) =>
+ scriptPubKey.IsScriptType(ScriptType.P2WPKH) switch
{
- if (scriptPubKey.IsScriptType(ScriptType.P2WPKH))
+ true => VerifySegwit(hash, scriptPubKey),
+ false => throw new NotImplementedException("Only P2WPKH scripts are supported.")
+ };
+
+ public static Bip322Signature Generate(Key key, uint256 hash, ScriptPubKeyType scriptPubKeyType) =>
+ scriptPubKeyType switch
{
- if (ScriptSig.Length != 0)
+ ScriptPubKeyType.Segwit => new Bip322Signature(
+ Script.Empty,
+ PayToWitPubKeyHashTemplate.Instance.GenerateWitScript(
+ key.Sign(hash, SigHash.All, false),
+ key.PubKey)),
+ _ => throw new NotImplementedException("Only P2WPKH scripts are supported.")
+ };
+
+ private bool VerifySegwit(uint256 hash, Script scriptPubKey)
+ {
+ if (ScriptSig != Script.Empty)
{
return false;
}
- var witnessParameters = PayToWitPubKeyHashTemplate.Instance.ExtractWitScriptParameters(Witness);
- if (witnessParameters is null)
+ if (PayToWitPubKeyHashTemplate.Instance.ExtractWitScriptParameters(Witness) is not { } witnessParameters)
{
return false;
}
@@ -55,28 +73,7 @@ namespace WalletWasabi.Crypto
return false;
}
- // if (witnessParameters.TransactionSignature.SigHash != SigHash.All)
- // return false;
-
return witnessParameters.PublicKey.Verify(hash, witnessParameters.TransactionSignature.Signature);
}
-
- throw new NotImplementedException();
- }
-
- public static Bip322Signature FromBytes(byte[] bip322SignatureBytes) =>
- NBitcoinExtensions.FromBytes<Bip322Signature>(bip322SignatureBytes);
-
- public static Bip322Signature Generate(Key key, uint256 hash, ScriptPubKeyType scriptPubKeyType)
- {
- if (scriptPubKeyType == ScriptPubKeyType.Segwit)
- {
- var signature = key.Sign(hash, SigHash.All, false);
- var witness = PayToWitPubKeyHashTemplate.Instance.GenerateWitScript(signature, key.PubKey);
- return new Bip322Signature(Script.Empty, witness);
- }
-
- throw new NotImplementedException(); // TODO: Should we implement it?
- }
}
}

CR suggestions and additional improvements