public TaprootSignature(SchnorrSignature schnorrSignature, TaprootSigHash sigHash) { if (!TaprootExecutionData.IsValidSigHash((byte)sigHash)) { throw new ArgumentException("Invalid hash_type", nameof(sigHash)); } if (schnorrSignature == null) { throw new ArgumentNullException(nameof(schnorrSignature)); } SigHash = sigHash; SchnorrSignature = schnorrSignature; }
public static bool TryParse(byte[] bytes, [MaybeNullWhen(false)] out TaprootSignature signature) { if (bytes == null) { throw new ArgumentNullException(nameof(bytes)); } #if HAS_SPAN return(TryParse(bytes.AsSpan(), out signature)); #else if (bytes.Length == 64) { if (!SchnorrSignature.TryParse(bytes, out var sig)) { signature = null; return(false); } signature = new TaprootSignature(sig); return(true); } else if (bytes.Length == 65) { if (!TaprootExecutionData.IsValidSigHash(bytes[64]) || bytes[64] == 0) { signature = null; return(false); } var sighash = (TaprootSigHash)bytes[64]; if (sighash == TaprootSigHash.Default) { signature = null; return(false); } var buff = new byte[64]; Array.Copy(bytes, 0, buff, 0, 64); if (!SchnorrSignature.TryParse(buff, out var sig)) { signature = null; return(false); } signature = new TaprootSignature(sig, sighash); return(true); } else { signature = null; return(false); } #endif }
public static bool TryParse(ReadOnlySpan <byte> bytes, [MaybeNullWhen(false)] out TaprootSignature signature) { if (bytes.Length == 64) { if (!SchnorrSignature.TryParse(bytes, out var sig)) { signature = null; return(false); } signature = new TaprootSignature(sig); return(true); } else if (bytes.Length == 65) { if (!TaprootExecutionData.IsValidSigHash(bytes[64])) { signature = null; return(false); } var sighash = (TaprootSigHash)bytes[64]; if (sighash == TaprootSigHash.Default) { signature = null; return(false); } if (!SchnorrSignature.TryParse(bytes.Slice(0, 64), out var sig)) { signature = null; return(false); } signature = new TaprootSignature(sig, sighash); return(true); } else { signature = null; return(false); } }