-
Notifications
You must be signed in to change notification settings - Fork 871
Support Miniscript as a separate dll. #675
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
Closed
Closed
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
d4e820b
Add Miniscript folder as separate dll
joemphilips f254c5b
Move Miniscript to independent Directory
joemphilips 0a641be
Stop using the work FNBitcoin
joemphilips 788d459
Fromat: Fix some casing
joemphilips a53651e
fixup! Move Miniscript to independent Directory
joemphilips 3d2abb3
pack Miniscript without editing .nuspec manually
joemphilips c76955f
pack Miniscript without editing .nuspec manually.
joemphilips 47e3a46
Start Writing Tests for Miniscript
joemphilips 3c3227e
Update csproj a bit
joemphilips d43bf48
Include Miniscript to solution
joemphilips 50f9de3
Fix compile error in Miniscript.Tests
joemphilips 2d6a63d
Fix every compile warning when testing Miniscript
joemphilips d6b8c42
Fix bug in all Miniscript Test
joemphilips 0684c0f
Refactor Miniscript
joemphilips 1a2677d
Rename MiniScript -> Miniscript
joemphilips 7ea97ca
Small refactor
joemphilips 930d210
separate FSharp test and CSharp test for Miniscript
joemphilips 1483d98
Rebame Miniscript.dll -> NBitcoin.Miniscript.dll
joemphilips 80217a5
Restrict public-facing api in Miniscript as strict as possible
joemphilips 1d6803b
Enable to pass `Func` to `Satisfy`
joemphilips cb21a50
Fix bug when satisfying LockTime
joemphilips 55b2e3d
Update MiniscriptPSBTTests.cs
joemphilips 2da2ff3
Remove all unnecessary files from Miniscript
joemphilips c871a27
Fix bug in E.Or
joemphilips d645d9d
Update Miniscript.PSBTExtensions
joemphilips 39e3534
Fix bug in Satisfy
joemphilips 88c8ee5
Delete PSBT.Finalize() and reimplement it as Extension
joemphilips 872fc09
Fix all bug in finalization
joemphilips 7e036ac
Tweak *.[c|f]sproj files
joemphilips 67588fd
Delete useless file in Miniscript.Tests.CSharp
joemphilips 4f9a80f
Add description about netfx.props
joemphilips ff38b8d
Remove meaningless testcase in SatifyTests.fs
joemphilips c706db8
Run Miniscript test in appveyor
joemphilips e443fec
Speed up property test
joemphilips 6facd74
Run Miniscript tests in travis
joemphilips 32ef31f
Specify FSharp.Core as dep in FSharp test and bump Fsharp.Core versio…
joemphilips 43b19b0
Specify framework version in appveyor
joemphilips fc619da
Pass HTLC test for psbt
joemphilips cfed1ab
Remove unnecessary props from Miniscript/AssemblyInfo.fs
joemphilips f7f453a
remove nits
joemphilips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| using NBitcoin.Crypto; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace NBitcoin.Miniscript.Tests.CSharp | ||
| { | ||
| class AssertEx | ||
| { | ||
| [DebuggerHidden] | ||
| internal static void Error(string msg) | ||
| { | ||
| Assert.False(true, msg); | ||
| } | ||
| [DebuggerHidden] | ||
| internal static void Equal<T>(T actual, T expected) | ||
| { | ||
| Assert.Equal(expected, actual); | ||
| } | ||
| [DebuggerHidden] | ||
| internal static void CollectionEquals<T>(T[] actual, T[] expected) | ||
| { | ||
| if(actual.Length != expected.Length) | ||
| Assert.False(true, "Actual.Length(" + actual.Length + ") != Expected.Length(" + expected.Length + ")"); | ||
|
|
||
| for(int i = 0; i < actual.Length; i++) | ||
| { | ||
| if(!Object.Equals(actual[i], expected[i])) | ||
| Assert.False(true, "Actual[" + i + "](" + actual[i] + ") != Expected[" + i + "](" + expected[i] + ")"); | ||
| } | ||
| } | ||
|
|
||
| [DebuggerHidden] | ||
| internal static void StackEquals(ContextStack<byte[]> stack1, ContextStack<byte[]> stack2) | ||
| { | ||
| var hash1 = stack1.Select(o => Hashes.Hash256(o)).ToArray(); | ||
| var hash2 = stack2.Select(o => Hashes.Hash256(o)).ToArray(); | ||
| AssertEx.CollectionEquals(hash1, hash2); | ||
| } | ||
|
|
||
| internal static void CollectionEquals(System.Collections.BitArray bitArray, int p) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Xunit; | ||
| using NBitcoin.Crypto; | ||
| using NBitcoin.BIP174; | ||
| using NBitcoin.Miniscript; | ||
| using static NBitcoin.Miniscript.AbstractPolicy; | ||
| using System.Linq; | ||
|
|
||
| namespace NBitcoin.Miniscript.Tests.CSharp | ||
| { | ||
| public class MiniscriptPSBTTests | ||
| { | ||
| private Key[] privKeys { get; } | ||
| public Network Network { get; } | ||
|
|
||
| public MiniscriptPSBTTests() | ||
| { | ||
| privKeys = new[] { new Key(), new Key(), new Key(), new Key() }; | ||
| Network = Network.Main; | ||
| } | ||
|
|
||
| private TransactionSignature GetDummySig() | ||
| { | ||
| var hash = new uint256(); | ||
| var ecdsa = privKeys[0].Sign(hash); | ||
| return new TransactionSignature(ecdsa, SigHash.All); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldSatisfyMiniscript() | ||
| { | ||
| var policyStr = $"aor(and(pk({privKeys[0].PubKey}), time({10000})), multi(2, {privKeys[0].PubKey}, {privKeys[1].PubKey})"; | ||
| var ms = Miniscript.FromStringUnsafe(policyStr); | ||
| Assert.NotNull(ms); | ||
|
|
||
| // We can write AbstractPolicy directly instead of using string representation. | ||
| var pubKeys = privKeys.Select(p => p.PubKey).Take(2).ToArray(); | ||
| var policy = new AsymmetricOr( | ||
| new And( | ||
| new AbstractPolicy.Key(privKeys[0].PubKey), | ||
| new Time(new LockTime(10000)) | ||
| ), | ||
| new Multi(2, pubKeys) | ||
| ); | ||
| // And it is EqualityComparable by default. 🎉 | ||
| var msFromPolicy = Miniscript.FromPolicyUnsafe(policy); | ||
| Assert.Equal(ms, msFromPolicy); | ||
|
|
||
| Func<PubKey, TransactionSignature> dummySignatureProvider = | ||
| pk => pk == privKeys[0].PubKey ? GetDummySig() : null; | ||
| Assert.Throws<MiniscriptSatisfyException>(() => ms.SatisfyUnsafe(dummySignatureProvider)); | ||
|
|
||
| Assert.Throws<MiniscriptSatisfyException>(() => ms.SatisfyUnsafe(dummySignatureProvider, null, 9999u)); | ||
| var r3 = ms.Satisfy(dummySignatureProvider, null, 10000u); | ||
| Assert.True(r3.IsOk); | ||
|
|
||
| Func<PubKey, TransactionSignature> dummySignatureProvider2 = | ||
| pk => (pk == privKeys[0].PubKey || pk == privKeys[1].PubKey) ? GetDummySig() : null; | ||
| var r5 = ms.Satisfy(dummySignatureProvider2); | ||
| Assert.True(r5.IsOk); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ShouldSatisfyPSBTWithComplexScript() | ||
| { | ||
| // case 1: bip199 HTLC | ||
| var alice = privKeys[0]; | ||
| var bob = privKeys[1]; | ||
| var bobSecret = new uint256(0xdeadbeef); | ||
| var bobHash = new uint256(Hashes.SHA256(bobSecret.ToBytes()), false); | ||
| var policyStr = $"aor(and(hash({bobHash}), pk({bob.PubKey})), and(pk({alice.PubKey}), time({10000})))"; | ||
| var ms = Miniscript.FromStringUnsafe(policyStr); | ||
| var script = ms.ToScript(); | ||
| var funds = Utils.CreateDummyFunds(Network, privKeys, script); | ||
| var tx = Utils.CreateTxToSpendFunds(funds, privKeys, script, false, false); | ||
| var psbt = PSBT.FromTransaction(tx) | ||
| .AddTransactions(funds) | ||
| .AddScript(script); | ||
|
|
||
| // Can not finalize without signatures. | ||
| Assert.Throws<AggregateException>(() => psbt.FinalizeUnsafe(h => h == bobHash ? bobSecret : null, age: 10001u)); | ||
| // It has signature but it is not matured. | ||
| psbt.SignAll(alice); | ||
| Assert.Throws<AggregateException>(() => psbt.FinalizeUnsafe(h => h == bobHash ? bobSecret : null, age: 9999u)); | ||
|
|
||
| // it has both signature and a secret. | ||
| psbt.SignAll(bob); | ||
| psbt.FinalizeUnsafe(h => h == bobHash ? bobSecret : null); | ||
| Assert.True(psbt.CanExtractTX()); | ||
|
|
||
| var txExtracted = psbt.ExtractTX(); | ||
| var builder = Network.CreateTransactionBuilder(); | ||
| builder.AddCoins(Utils.DummyFundsToCoins(funds, script, privKeys[0])).AddKeys(privKeys); | ||
| if (!builder.Verify(txExtracted, (Money)null, out var errors)) | ||
| throw new InvalidOperationException(errors.Aggregate(string.Empty, (a, b) => a + ";\n" + b)); | ||
| } | ||
| } | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
NBitcoin.Miniscript.Tests/CSharp/NBitcoin.Miniscript.Tests.CSharp.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFrameworks>net461;netstandard2.0;netcoreapp2.1;</TargetFrameworks> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="FSharp.Core" Version="4.6.2" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" /> | ||
| <PackageReference Include="xunit" Version="2.4.0" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\NBitcoin\NBitcoin.csproj" /> | ||
| <ProjectReference Include="..\..\NBitcoin.TestFramework\NBitcoin.TestFramework.csproj" /> | ||
| <ProjectReference Include="..\..\NBitcoin.Tests\NBitcoin.Tests.csproj" /> | ||
| <ProjectReference Include="..\..\NBitcoin.Miniscript\NBitcoin.Miniscript.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\NBitcoin.Miniscript\NBitcoin.Miniscript.fsproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Include="data\psbt.json"> | ||
| <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI mixed tabs and spaces, misconfigured editor? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😭
I will fix later.