|
| 1 | +using FsCheck; |
| 2 | +using NBitcoin.Scripting; |
| 3 | + |
| 4 | +namespace NBitcoin.Tests.Generators |
| 5 | +{ |
| 6 | + public class OutputDescriptorGenerator |
| 7 | + { |
| 8 | + public static Arbitrary<OutputDescriptor> OutputDescriptorArb() => |
| 9 | + Arb.From(OutputDescriptorGen()); |
| 10 | + |
| 11 | + public static Gen<OutputDescriptor> OutputDescriptorGen() => |
| 12 | + Gen.OneOf( |
| 13 | + AddrOutputDescriptorGen(), |
| 14 | + RawOutputDescriptorGen(), |
| 15 | + PKOutputDescriptorGen(), |
| 16 | + PKHOutputDescriptorGen(), |
| 17 | + WPKHOutputDescriptorGen(), |
| 18 | + ComboOutputDescriptorGen(), |
| 19 | + MultisigOutputDescriptorGen(), |
| 20 | + SHOutputDescriptorGen(), |
| 21 | + WSHOutputDescriptorGen() |
| 22 | + ); |
| 23 | + private static Gen<OutputDescriptor> AddrOutputDescriptorGen() => |
| 24 | + from addr in AddressGenerator.RandomAddress() |
| 25 | + select OutputDescriptor.NewAddr(addr); |
| 26 | + |
| 27 | + private static Gen<OutputDescriptor> RawOutputDescriptorGen() => |
| 28 | + from addr in ScriptGenerator.RandomScriptSig() |
| 29 | + select OutputDescriptor.NewRaw(addr); |
| 30 | + private static Gen<OutputDescriptor> PKOutputDescriptorGen() => |
| 31 | + from pkProvider in PubKeyProviderGen() |
| 32 | + select OutputDescriptor.NewPK(pkProvider); |
| 33 | + |
| 34 | + private static Gen<OutputDescriptor> PKHOutputDescriptorGen() => |
| 35 | + from pkProvider in PubKeyProviderGen() |
| 36 | + select OutputDescriptor.NewPKH(pkProvider); |
| 37 | + |
| 38 | + private static Gen<OutputDescriptor> WPKHOutputDescriptorGen() => |
| 39 | + from pkProvider in PubKeyProviderGen() |
| 40 | + select OutputDescriptor.NewWPKH(pkProvider); |
| 41 | + |
| 42 | + private static Gen<OutputDescriptor> ComboOutputDescriptorGen() => |
| 43 | + from pkProvider in PubKeyProviderGen() |
| 44 | + select OutputDescriptor.NewCombo(pkProvider); |
| 45 | + |
| 46 | + private static Gen<OutputDescriptor> MultisigOutputDescriptorGen() => |
| 47 | + from pkProviders in Gen.NonEmptyListOf(PubKeyProviderGen()) |
| 48 | + select OutputDescriptor.NewMulti(pkProviders); |
| 49 | + |
| 50 | + private static Gen<OutputDescriptor> InnerOutputDescriptorGen() => |
| 51 | + Gen.OneOf( |
| 52 | + PKOutputDescriptorGen(), |
| 53 | + PKHOutputDescriptorGen(), |
| 54 | + WPKHOutputDescriptorGen(), |
| 55 | + MultisigOutputDescriptorGen() |
| 56 | + ); |
| 57 | + private static Gen<OutputDescriptor> SHOutputDescriptorGen() => |
| 58 | + from inner in Gen.OneOf(InnerOutputDescriptorGen(), WSHOutputDescriptorGen()) |
| 59 | + select OutputDescriptor.NewSH(inner); |
| 60 | + |
| 61 | + private static Gen<OutputDescriptor> WSHOutputDescriptorGen() => |
| 62 | + from inner in InnerOutputDescriptorGen() |
| 63 | + select OutputDescriptor.NewWSH(inner); |
| 64 | + |
| 65 | + #region pubkey providers |
| 66 | + |
| 67 | + private static Gen<PubKeyProvider> PubKeyProviderGen() => |
| 68 | + Gen.OneOf(OriginPubKeyProviderGen(), ConstPubKeyProviderGen(), HDPubKeyProviderGen()); |
| 69 | + |
| 70 | + private static Gen<PubKeyProvider> OriginPubKeyProviderGen() => |
| 71 | + from keyOrigin in CryptoGenerator.RootedKeyPath() |
| 72 | + from inner in Gen.OneOf(ConstPubKeyProviderGen(), HDPubKeyProviderGen()) |
| 73 | + select PubKeyProvider.NewOrigin(keyOrigin, inner); |
| 74 | + |
| 75 | + private static Gen<PubKeyProvider> ConstPubKeyProviderGen() => |
| 76 | + from pk in CryptoGenerator.PublicKey() |
| 77 | + select PubKeyProvider.NewConst(pk); |
| 78 | + |
| 79 | + private static Gen<PubKeyProvider> HDPubKeyProviderGen() => |
| 80 | + from extPk in CryptoGenerator.BitcoinExtPubKey() |
| 81 | + from kp in CryptoGenerator.KeyPath() |
| 82 | + from t in Arb.Generate<PubKeyProvider.DeriveType>() |
| 83 | + select PubKeyProvider.NewHD(extPk, kp, t); |
| 84 | + |
| 85 | + # endregion |
| 86 | + } |
| 87 | +} |
0 commit comments