Rely on NBitcoin to get the expected hash

This commit is contained in:
nicolas.dorier 2020-04-26 00:26:02 +09:00
parent f8b2b18c6e
commit 47f16aadd5
No known key found for this signature in database
GPG Key ID: 6618763EF09186FE
2 changed files with 8 additions and 20 deletions

View File

@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NBitcoin" Version="5.0.30" />
<PackageReference Include="NBitcoin" Version="5.0.31" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

View File

@ -460,7 +460,7 @@ namespace BTCPayServer.Payments.PayJoin
originalPaymentData.ConfirmationCount = -1;
originalPaymentData.PayjoinInformation = new PayjoinInformation()
{
CoinjoinTransactionHash = GetExpectedHash(newPsbt, sendersInputType, coins),
CoinjoinTransactionHash = GetExpectedHash(newPsbt, coins),
CoinjoinValue = originalPaymentValue - ourFeeContribution,
ContributedOutPoints = selectedUTXOs.Select(o => o.Key).ToArray()
};
@ -487,25 +487,13 @@ namespace BTCPayServer.Payments.PayJoin
return Ok(newTx.ToHex());
}
private uint256 GetExpectedHash(PSBT psbt, ScriptPubKeyType? sendersInputType, Coin[] coins)
private uint256 GetExpectedHash(PSBT psbt, Coin[] coins)
{
var tx = psbt.GetGlobalTransaction();
if (sendersInputType is ScriptPubKeyType.Segwit)
return tx.GetHash();
else if (sendersInputType is ScriptPubKeyType.SegwitP2SH)
{
for (int i = 0; i < psbt.Inputs.Count; i++)
{
tx.Inputs[i].ScriptSig = PayToScriptHashTemplate.Instance.GenerateScriptSig(Array.Empty<byte[]>(), ((ScriptCoin)coins.Single(coin => coin.Outpoint == psbt.Inputs[i].PrevOut)).GetP2SHRedeem());
}
return tx.GetHash();
}
else
{
throw new NotSupportedException();
}
psbt = psbt.Clone();
psbt.AddCoins(coins);
if (!psbt.TryGetFinalizedHash(out var hash))
throw new InvalidOperationException("Unable to get the finalized hash");
return hash;
}
private JObject CreatePayjoinError(int httpCode, string errorCode, string friendlyMessage)