Skip to content

Commit

Permalink
Update to latest NBitcoin
Browse files Browse the repository at this point in the history
- This needs to be properly verified. There has been breaking changes in NBitcoin.
  • Loading branch information
sondreb committed Dec 10, 2024
1 parent 96ae388 commit 6ce1292
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Angor.Test/Angor.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="xunit" Version="2.9.2" />

<PackageReference Include="Blockcore.Core" Version="1.2.42" />
<PackageReference Include="NBitcoin" Version="7.0.25" />
<PackageReference Include="NBitcoin" Version="7.0.46" />

<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Client/Angor.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageReference Include="Blazored.SessionStorage" Version="2.4.0" />
<PackageReference Include="Blockcore.Core" Version="1.2.42" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.71" />
<PackageReference Include="NBitcoin" Version="7.0.25" />
<PackageReference Include="NBitcoin" Version="7.0.46" />
<PackageReference Include="Nostr.Client" Version="2.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="QRCoder" Version="1.6.0" />
Expand Down
2 changes: 1 addition & 1 deletion src/Angor/Shared/Angor.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="Blockcore.Core" Version="1.2.42" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.0" />
<PackageReference Include="NBitcoin" Version="7.0.25" />
<PackageReference Include="NBitcoin" Version="7.0.46" />
<PackageReference Include="Nostr.Client" Version="2.0.0" />
</ItemGroup>

Expand Down
10 changes: 7 additions & 3 deletions src/Angor/Shared/ProtocolNew/FounderTransactionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public SignatureInfo SignInvestorRecoveryTransactions(ProjectInfo projectInfo, s
{
var scriptStages = _investmentScriptBuilder.BuildProjectScriptsForStage(projectInfo, investorKey, stageIndex, secretHash);

var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash) { SigHash = sigHash };
var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var hash = nbitcoinRecoveryTransaction.GetSignatureHashTaproot(outputs, execData);

var sig = key.SignTaprootKeySpend(hash, sigHash).ToString();
Expand Down Expand Up @@ -123,8 +125,10 @@ public TransactionInfo SpendFounderStage(ProjectInfo projectInfo, IEnumerable<st
{
var scriptToExecute = new NBitcoin.Script(input.WitScript[1]);
var controlBlock = input.WitScript[2];

var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sigHash };

var tapScript = scriptToExecute.ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(inputIndex, tapScript.LeafHash) { SigHash = sigHash };
var hash = spendingTransaction.GetSignatureHashTaproot(trxData, execData);

var sig = key.SignTaprootKeySpend(hash, sigHash);
Expand Down
8 changes: 6 additions & 2 deletions src/Angor/Shared/ProtocolNew/InvestorTransactionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ public Transaction AddSignaturesToRecoverSeederFundsTransaction(ProjectInfo proj
var scriptStages = _investmentScriptBuilder.BuildProjectScriptsForStage(projectInfo, investorKey, stageIndex, secretHash);
var controlBlock = _taprootScriptBuilder.CreateControlBlock(scriptStages, _ => _.Recover);

var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash) { SigHash = sigHash };
var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var hash = nbitcoinRecoveryTransaction.GetSignatureHashTaproot(outputs, execData);

_logger.LogInformation($"project={projectInfo.ProjectIdentifier}; investor-pubkey={key.PubKey.ToHex()}; stage={stageIndex}; hash={hash}");
Expand Down Expand Up @@ -290,7 +292,9 @@ public bool CheckInvestorRecoverySignatures(ProjectInfo projectInfo, Transaction
{
var scriptStages = _investmentScriptBuilder.BuildProjectScriptsForStage(projectInfo, investorKey, stageIndex, secretHash);

var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash) { SigHash = sigHash };
var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var hash = nBitcoinRecoveryTransaction.GetSignatureHashTaproot(outputs, execData);
var sig = founderSignatures.Signatures.First(f => f.StageIndex == stageIndex).Signature;

Expand Down
6 changes: 2 additions & 4 deletions src/Angor/Shared/ProtocolNew/Scripts/TaprootScriptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public Script CreateControlBlock(ProjectScripts scripts, Expression<Func<Project

var script = scriptSelector.Compile().Invoke(scripts);

ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(script.ToBytes()),
(byte)TaprootConstants.TAPROOT_LEAF_TAPSCRIPT);
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(script.ToBytes()).ToTapScript(TapLeafVersion.C0));

return new Script(controlBlock.ToBytes());
}
Expand Down Expand Up @@ -79,8 +78,7 @@ public Script CreateControlBlock(ProjectScripts scripts, Expression<Func<Project
throw new Exception("no secret found that matches the given scripts");
}

ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(execute.ToBytes()),
(byte)TaprootConstants.TAPROOT_LEAF_TAPSCRIPT);
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(execute.ToBytes()).ToTapScript(TapLeafVersion.C0));

return (new Script(controlBlock.ToBytes()), execute, secretHashes.ToArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public TransactionInfo BuildRecoverInvestorRemainingFundsInProject(string invest
input.WitScript[input.WitScript.PushCount - 2]); //control block is the last and execute one before it

var hash = spendingTrx.GetSignatureHashTaproot(trxData,
new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sigHash });
new TaprootExecutionData(inputIndex, scriptToExecute.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sigHash });

var sig = key.SignTaprootKeySpend(hash, sigHash);

Expand Down

0 comments on commit 6ce1292

Please sign in to comment.