Skip to content

Commit

Permalink
Restore nbitcoin (#214)
Browse files Browse the repository at this point in the history
* Simplify the launchsettings

* Revert to NBitcoin 7.0.25
  • Loading branch information
sondreb authored Dec 12, 2024
1 parent b795a04 commit 823878a
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 62 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.46" />
<PackageReference Include="NBitcoin" Version="7.0.25" />

<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
13 changes: 4 additions & 9 deletions src/Angor.Test/DataBuilders/AngorScripts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static Script CreateControlBlock(ProjectScripts scripts, Expression<Func<
var treeInfo = AngorScripts.BuildTaprootSpendInfo(scripts);

var script = func.Compile().Invoke(scripts);
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(script.ToBytes()).ToTapScript(TapLeafVersion.C0));

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

return new Script(controlBlock.ToBytes());
}
Expand Down Expand Up @@ -80,7 +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()).ToTapScript(TapLeafVersion.C0));
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(execute.ToBytes()), (byte)TaprootConstants.TAPROOT_LEAF_TAPSCRIPT);

return (new Script(controlBlock.ToBytes()), execute, secretHashes.ToArray());
}
Expand All @@ -92,12 +92,7 @@ private static TaprootSpendInfo BuildTaprootSpendInfo(ProjectScripts scripts)

var scriptWeights = BuildTaprootScripts(scripts);

// 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());
var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, scriptWeights.ToArray());

return treeInfo;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Angor.Test/DataBuilders/InvestmentOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public Transaction SpendFounderStage(Network network, FounderContext context, in
var sighash = TaprootSigHash.All;// TaprootSigHash.Single | TaprootSigHash.AnyoneCanPay;

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

var key = new Key(Encoders.Hex.DecodeData(founderPrivateKey));
Expand Down Expand Up @@ -286,9 +286,9 @@ public List<string> FounderSignInvestorRecoveryTransactions(InvestorContext cont
const TaprootSigHash sigHash = TaprootSigHash.Single | TaprootSigHash.AnyoneCanPay;

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


var signature = key.SignTaprootKeySpend(hash, sigHash);
Expand Down Expand Up @@ -329,8 +329,8 @@ public void AddWitScriptToInvestorRecoveryTransactions(InvestorContext context,

var hash = stageTransaction.GetSignatureHashTaproot(new[] { investmentTransaction.Outputs[index + 2] },
new TaprootExecutionData(0,
new NBitcoin.Script(projectScripts.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0).LeafHash)
{ SigHash = sigHash });
new NBitcoin.Script(projectScripts.Recover.ToBytes()).TaprootV1LeafHash)
{ 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.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sighash };
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { 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.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sighash };
var execData = new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { 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 @@ -10,7 +10,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.46" />
<PackageReference Include="NBitcoin" Version="7.0.25" />
<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
17 changes: 1 addition & 16 deletions src/Angor/Client/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:15856",
"sslPort": 44308
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"http": {
"commandName": "Project",
Expand All @@ -27,14 +20,6 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
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.46" />
<PackageReference Include="NBitcoin" Version="7.0.25" />
<PackageReference Include="Nostr.Client" Version="2.0.0" />
</ItemGroup>

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

var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash) { SigHash = sigHash };
var hash = nbitcoinRecoveryTransaction.GetSignatureHashTaproot(outputs, execData);

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

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

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

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

var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(scriptStages.Recover.ToBytes()).TaprootV1LeafHash) { 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 @@ -292,9 +290,7 @@ public bool CheckInvestorRecoverySignatures(ProjectInfo projectInfo, Transaction
{
var scriptStages = _investmentScriptBuilder.BuildProjectScriptsForStage(projectInfo, investorKey, stageIndex, secretHash);

var tapScript = new NBitcoin.Script(scriptStages.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

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

Expand Down
13 changes: 4 additions & 9 deletions src/Angor/Shared/ProtocolNew/Scripts/TaprootScriptBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public Script CreateControlBlock(ProjectScripts scripts, Expression<Func<Project
var treeInfo = BuildTaprootSpendInfo(scripts);

var script = scriptSelector.Compile().Invoke(scripts);
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(script.ToBytes()).ToTapScript(TapLeafVersion.C0));

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

return new Script(controlBlock.ToBytes());
}
Expand Down Expand Up @@ -78,7 +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()).ToTapScript(TapLeafVersion.C0));
ControlBlock controlBlock = treeInfo.GetControlBlock(new NBitcoin.Script(execute.ToBytes()), (byte)TaprootConstants.TAPROOT_LEAF_TAPSCRIPT);

return (new Script(controlBlock.ToBytes()), execute, secretHashes.ToArray());
}
Expand All @@ -89,12 +89,7 @@ private static TaprootSpendInfo BuildTaprootSpendInfo(ProjectScripts scripts)

var scriptWeights = BuildTaprootScripts(scripts);

// 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());
var treeInfo = TaprootSpendInfo.WithHuffmanTree(taprootKey, scriptWeights.ToArray());

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

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

var tapScript = new NBitcoin.Script(projectScripts.Recover.ToBytes()).ToTapScript(TapLeafVersion.C0);

var execData = new TaprootExecutionData(stageIndex, tapScript.LeafHash) { SigHash = sigHash };
var execData = new TaprootExecutionData(stageIndex, new NBitcoin.Script(projectScripts.Recover.ToBytes()).TaprootV1LeafHash) { 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.ToTapScript(TapLeafVersion.C0).LeafHash) { SigHash = sigHash });
new TaprootExecutionData(inputIndex, scriptToExecute.TaprootV1LeafHash) { SigHash = sigHash });

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

Expand Down

0 comments on commit 823878a

Please sign in to comment.