Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4e885de
Prepare parser for scripting
joemphilips May 22, 2019
58fa6bb
Rename ParseException -> ParsingException and inherit from FormatExce…
joemphilips May 23, 2019
ff398a5
fixup! Rename ParseException -> ParsingException and inherit from For…
joemphilips May 23, 2019
fa33110
Update output descriptor according to bitcoin core
joemphilips May 19, 2019
8f70153
Update OutputDescriptor Parser
joemphilips May 19, 2019
17ca10a
Update OutputDescriptor
joemphilips May 20, 2019
2f61e1d
Handle Empty KeyPath correctly
joemphilips May 20, 2019
cd348b2
Fix OutputDescriptorGenerator
joemphilips May 21, 2019
b5d1731
Fix bug in OutputDescriptor CheckSum && Use IDestination for AddressD…
joemphilips May 21, 2019
df9665f
Update Parser
joemphilips May 21, 2019
6d0aad0
Update
joemphilips May 21, 2019
817685c
Update OD generator
joemphilips May 21, 2019
608c80c
Finish InferFromScript method
joemphilips May 21, 2019
e3a532e
Pass Core test for path derivation
joemphilips May 21, 2019
f4313c8
Remove Miniscript related things
joemphilips May 22, 2019
85eda4e
Move reuseable helpers in transaction_tests to separate class
joemphilips May 1, 2019
1073da3
Update comment
joemphilips May 23, 2019
ca9f9cb
Test Signability by txBuilder
joemphilips May 23, 2019
0008256
Pass all OutputDescriptorTests
joemphilips May 23, 2019
326d26e
Clean the code a bit
joemphilips May 23, 2019
6617bdb
Rename ParseException -> ParsingException and get rid of some compile…
joemphilips May 26, 2019
f300f35
Use string.ToCharArray instead of ToArray
joemphilips May 26, 2019
11dcd1b
Update test
joemphilips May 26, 2019
29f3756
nit: use switch expression in output descriptor when possible
joemphilips Aug 25, 2020
bd7ae77
Mark ScanTxOutSet as obsolete
joemphilips Aug 25, 2020
ba591b7
support "sortedmulti" in output descriptor
joemphilips Aug 26, 2020
b554c75
abstract SigninigRepository awai in OutputDescriptor
joemphilips Aug 26, 2020
fb9a69d
Use interface for ISigningRepository
joemphilips Aug 26, 2020
4553385
nit: update test
joemphilips Aug 27, 2020
a30c06b
Make parsing more robust and fast.
joemphilips Aug 31, 2020
4d7df4f
Pursue more functional beauty.
joemphilips Aug 31, 2020
589dc83
use nullable reference type everywhere
joemphilips Aug 31, 2020
7881ac2
parameterize OutputDescriptorGenerator with Network
joemphilips Sep 1, 2020
bcf6320
remove prefix from subtype name
joemphilips Sep 1, 2020
47b95fd
Make an OD and PKProvider a bit stylish and safe
joemphilips Sep 17, 2020
2d521d4
Revert the behavior of RootedKeyPath.ToString()
joemphilips Sep 24, 2020
dceeaf9
add RPCPropertyTests
joemphilips Sep 24, 2020
4d3d8b7
fixup! Revert the behavior of RootedKeyPath.ToString()
joemphilips Sep 24, 2020
65abec5
Enable to pass OutputDescriptor with importmulti
joemphilips Oct 28, 2020
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
21 changes: 20 additions & 1 deletion NBitcoin.Tests/Generators/CryptoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,24 @@ from raw in Gen.NonEmptyListOf(PrimitiveGenerator.RandomBytes(4))
select NBitcoin.KeyPath.FromBytes(flattenBytes);

public static Gen<ExtPubKey> ExtPubKey() => ExtKey().Select(ek => ek.Neuter());

public static Gen<BitcoinExtPubKey> BitcoinExtPubKey() =>
from extKey in ExtPubKey()
from network in ChainParamsGenerator.NetworkGen()
select new BitcoinExtPubKey(extKey, network);

public static Gen<BitcoinExtKey> BitcoinExtKey() =>
from extKey in ExtKey()
from network in ChainParamsGenerator.NetworkGen()
select new BitcoinExtKey(extKey, network);

public static Gen<RootedKeyPath> RootedKeyPath() =>
from parentFingerPrint in HDFingerPrint()
from kp in KeyPath()
select new RootedKeyPath(parentFingerPrint, kp);

public static Gen<HDFingerprint> HDFingerPrint() =>
from x in PrimitiveGenerator.UInt32()
select new HDFingerprint(x);
}
}
}
112 changes: 112 additions & 0 deletions NBitcoin.Tests/Generators/OutputDescriptorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System.Collections.Generic;
using System.Linq;
using FsCheck;
using NBitcoin.Altcoins;
using NBitcoin.Scripting;

#nullable enable
namespace NBitcoin.Tests.Generators
{
public class OutputDescriptorGenerator : OutputDescriptorGeneratorBase
{

public static Arbitrary<OutputDescriptor> OutputDescriptorArb() =>
Arb.From(OutputDescriptorGen());
}

public class RegtestOutputDescriptorGenerator : OutputDescriptorGeneratorBase
{
public static Arbitrary<OutputDescriptor> OutputDescriptorArb() =>
Arb.From(OutputDescriptorGen(Network.RegTest));
}

public class OutputDescriptorGeneratorBase
{
public static Gen<OutputDescriptor> OutputDescriptorGen(Network? n = null) =>
Gen.OneOf(
AddrOutputDescriptorGen(n),
RawOutputDescriptorGen(),
PKOutputDescriptorGen(n),
PKHOutputDescriptorGen(n),
WPKHOutputDescriptorGen(n),
ComboOutputDescriptorGen(n),
MultisigOutputDescriptorGen(3, n), // top level multisig can not have more than 3 pubkeys.
SHOutputDescriptorGen(n),
WSHOutputDescriptorGen(n)
);
private static Gen<OutputDescriptor> AddrOutputDescriptorGen(Network? n = null) =>
from addr in n is null ? AddressGenerator.RandomAddress() : AddressGenerator.RandomAddress(n)
select OutputDescriptor.NewAddr(addr);

private static Gen<OutputDescriptor> RawOutputDescriptorGen() =>
from addr in ScriptGenerator.RandomScriptSig()
where addr._Script.Length > 0
select OutputDescriptor.NewRaw(addr);
private static Gen<OutputDescriptor> PKOutputDescriptorGen(Network? n = null) =>
from pkProvider in PubKeyProviderGen(n)
select OutputDescriptor.NewPK(pkProvider);

private static Gen<OutputDescriptor> PKHOutputDescriptorGen(Network? n = null) =>
from pkProvider in PubKeyProviderGen(n)
select OutputDescriptor.NewPKH(pkProvider);

private static Gen<OutputDescriptor> WPKHOutputDescriptorGen(Network? n = null) =>
from pkProvider in PubKeyProviderGen(n)
select OutputDescriptor.NewWPKH(pkProvider);

private static Gen<OutputDescriptor> ComboOutputDescriptorGen(Network? n = null) =>
from pkProvider in PubKeyProviderGen(n)
select OutputDescriptor.NewCombo(pkProvider);

private static Gen<OutputDescriptor> MultisigOutputDescriptorGen(int maxN, Network? network = null) =>
from n in Gen.Choose(2, maxN)
from m in Gen.Choose(2, n).Select(i => (uint)i)
from pkProviders in Gen.ArrayOf(n, PubKeyProviderGen(network))
from isSorted in Arb.Generate<bool>()
select OutputDescriptor.NewMulti(m, pkProviders, isSorted);

private static Gen<OutputDescriptor> WSHInnerGen(int maxMultisigN, Network? n = null) =>
Gen.OneOf(
PKOutputDescriptorGen(n),
PKHOutputDescriptorGen(n),
MultisigOutputDescriptorGen(maxMultisigN, n)
);
private static Gen<OutputDescriptor> InnerOutputDescriptorGen(int maxMultisigN, Network? n = null) =>
Gen.OneOf(
WPKHOutputDescriptorGen(n),
WSHInnerGen(maxMultisigN, n)
);

// For sh-nested script, max multisig Number is 15.
private static Gen<OutputDescriptor> SHOutputDescriptorGen(Network? n = null) =>
from inner in Gen.OneOf(InnerOutputDescriptorGen(15, n), WSHOutputDescriptorGen(n))
select OutputDescriptor.NewSH(inner);

private static Gen<OutputDescriptor> WSHOutputDescriptorGen(Network? n = null) =>
from inner in WSHInnerGen(20, n)
select OutputDescriptor.NewWSH(inner);

#region pubkey providers

private static Gen<PubKeyProvider> PubKeyProviderGen(Network? n = null) =>
Gen.OneOf(OriginPubKeyProviderGen(n), ConstPubKeyProviderGen(), HDPubKeyProviderGen(n));

private static Gen<PubKeyProvider> OriginPubKeyProviderGen(Network? n = null) =>
from keyOrigin in CryptoGenerator.RootedKeyPath()
from inner in Gen.OneOf(ConstPubKeyProviderGen(), HDPubKeyProviderGen(n))
select PubKeyProvider.NewOrigin(keyOrigin, inner);

private static Gen<PubKeyProvider> ConstPubKeyProviderGen() =>
from pk in CryptoGenerator.PublicKey()
select PubKeyProvider.NewConst(pk);

private static Gen<PubKeyProvider> HDPubKeyProviderGen(Network? n = null) =>
from extPk in n is null ? CryptoGenerator.BitcoinExtPubKey() : CryptoGenerator.ExtPubKey().Select(e => new BitcoinExtPubKey(e, n))
from kp in CryptoGenerator.KeyPath()
from t in Arb.Generate<PubKeyProvider.DeriveType>()
select PubKeyProvider.NewHD(extPk, kp, t);

# endregion
}
}
#nullable disable
43 changes: 43 additions & 0 deletions NBitcoin.Tests/Helpers/PrimitiveUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

using System.Collections.Generic;

namespace NBitcoin.Tests.Helpers
{
internal static class PrimitiveUtils
{
internal static Coin RandomCoin(Money amount, Script scriptPubKey, bool p2sh)
{
var outpoint = RandOutpoint();
if(!p2sh)
return new Coin(outpoint, new TxOut(amount, scriptPubKey));
return new ScriptCoin(outpoint, new TxOut(amount, scriptPubKey.Hash), scriptPubKey);
}
internal static Coin RandomCoin(Money amount, Key receiver)
{
return RandomCoin(amount, receiver.PubKey.GetAddress(ScriptPubKeyType.Legacy, Network.Main));
}
internal static Coin RandomCoin(Money amount, IDestination receiver)
{
var outpoint = RandOutpoint();
return new Coin(outpoint, new TxOut(amount, receiver));
}

internal static List<ScriptCoin> GetRandomCoinsForAllScriptType(Money amount, Script scriptPubKey)
{
return new List<ScriptCoin> {
RandomCoin(Money.Coins(0.5m), scriptPubKey, true) as ScriptCoin,
new ScriptCoin(RandomCoin(Money.Coins(0.5m), scriptPubKey.WitHash), scriptPubKey),
new ScriptCoin(RandomCoin(Money.Coins(0.5m), scriptPubKey.WitHash.ScriptPubKey.Hash), scriptPubKey)
};
}

internal static OutPoint RandOutpoint()
{
return new OutPoint(Rand(), 0);
}
internal static uint256 Rand()
{
return new uint256(RandomUtils.GetBytes(32));
}
}
}
Loading