Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update nbitcoin #208

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ jobs:
shell: bash

- name: Publish
run: dotnet publish -c Debug -r ${{matrix.runtime}} /p:Version=${{ env.VERSION }}.${{ github.run_number }} -v m -o publish ${{env.PROJECT_PATH}}
run: dotnet publish -c Release -r ${{matrix.runtime}} /p:Version=${{ env.VERSION }}.${{ github.run_number }} -v m -o publish ${{env.PROJECT_PATH}}
env:
matrix.runtime: ${{ matrix.os == 'windows-latest' && 'win-x64' || matrix.os == 'ubuntu-latest' && 'linux-x64' || 'osx-x64' }}

- name: Run Tests
continue-on-error: true
run: dotnet test -c Debug -r ${{matrix.runtime}} --verbosity normal ${{env.SOLUTION_PATH}}
run: dotnet test -c Release -r ${{matrix.runtime}} --verbosity normal ${{env.SOLUTION_PATH}}
env:
matrix.runtime: ${{ matrix.os == 'windows-latest' && 'win-x64' || matrix.os == 'ubuntu-latest' && 'linux-x64' || 'osx-x64' }}

Expand Down
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
13 changes: 8 additions & 5 deletions src/Angor.Test/DataBuilders/AngorScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static Script CreateControlBlock(ProjectScripts scripts, Expression<Func<

var script = func.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 @@ -81,8 +80,7 @@ public static (Script controlBlock, Script execute, Script[] secrets) CreateCont
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 All @@ -94,7 +92,12 @@ private static TaprootSpendInfo BuildTaprootSpendInfo(ProjectScripts scripts)

var scriptWeights = BuildTaprootScripts(scripts);

var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, scriptWeights.ToArray());
// Transform the scripts to TapScript format
var tapScriptWeights = scriptWeights
.Select(sw => (sw.Item1, sw.Item2.ToTapScript(TapLeafVersion.C0)))
.ToList();

var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, tapScriptWeights.ToArray());

return treeInfo;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Angor.Test/DataBuilders/InvestmentOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public Transaction SpendFounderStage(Network network, FounderContext context, in

var allSpendingOutputs = signingContext.Select(s => s.spendingOutput.TxOut).ToArray();
var trxData = spender.PrecomputeTransactionData(allSpendingOutputs);
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sighash };
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sighash };
var hash = spender.GetSignatureHashTaproot(trxData, execData);

var key = new Key(Encoders.Hex.DecodeData(founderPrivateKey));
Expand Down Expand Up @@ -287,7 +287,7 @@ public List<string> FounderSignInvestorRecoveryTransactions(InvestorContext cont

var hash = stageTransaction.GetSignatureHashTaproot(new[] { investmentTransaction.Outputs[i+2] },
new TaprootExecutionData(0,
new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash)
new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0).LeafHash)
{ SigHash = sigHash });


Expand Down Expand Up @@ -329,7 +329,7 @@ public void AddWitScriptToInvestorRecoveryTransactions(InvestorContext context,

var hash = stageTransaction.GetSignatureHashTaproot(new[] { investmentTransaction.Outputs[index + 2] },
new TaprootExecutionData(0,
new NBitcoin.Script(projectScripts.Recover.ToBytes()).TaprootV1LeafHash)
new NBitcoin.Script(projectScripts.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0).LeafHash)
{ SigHash = sigHash });

var investorSignature = key.SignTaprootKeySpend(hash, sigHash);
Expand Down Expand Up @@ -446,7 +446,7 @@ public Transaction RecoverEndOfProjectFunds(Network network, InvestorContext con

var allSpendingOutputs = signingContext.Select(s => s.spendingOutput.TxOut).ToArray();
var trxData = spender.PrecomputeTransactionData(allSpendingOutputs);
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sighash };
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sighash };
var hash = spender.GetSignatureHashTaproot(trxData, execData);

var key = new Key(Encoders.Hex.DecodeData(investorPrivateKey));
Expand Down Expand Up @@ -566,7 +566,7 @@ public Transaction RecoverFundsNoPenalty(Network network, InvestorContext contex

var allSpendingOutputs = signingContext.Select(s => s.spendingOutput.TxOut).ToArray();
var trxData = spender.PrecomputeTransactionData(allSpendingOutputs);
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sighash };
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sighash };
var hash = spender.GetSignatureHashTaproot(trxData, execData);

var key = new Key(Encoders.Hex.DecodeData(investorPrivateKey));
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
13 changes: 8 additions & 5 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 All @@ -91,7 +89,12 @@ private static TaprootSpendInfo BuildTaprootSpendInfo(ProjectScripts scripts)

var scriptWeights = BuildTaprootScripts(scripts);

var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, scriptWeights.ToArray());
// Transform the scripts to TapScript format
var tapScriptWeights = scriptWeights
.Select(sw => (sw.Item1, sw.Item2.ToTapScript(TapLeafVersion.C0)))
.ToList();

var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, tapScriptWeights.ToArray());

return treeInfo;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Angor/Shared/ProtocolNew/SeederTransactionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public Transaction AddSignaturesToRecoverSeederFundsTransaction(ProjectInfo proj

var controlBlock = _taprootScriptBuilder.CreateControlBlock(projectScripts, _ => _.Recover);

var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(projectScripts.Recover.ToBytes()).TaprootV1LeafHash) { SigHash = sigHash };
var tapScript = new NBitcoin.Script(projectScripts.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}; seeder-pubkey={key.PubKey.ToHex()}; stage={stageIndex}; hash={hash}");
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
Loading