From 9adf2b009ccb4fcadfee5530d1f242d530563eff Mon Sep 17 00:00:00 2001 From: joemphilips Date: Fri, 18 Oct 2019 01:38:31 +0900 Subject: [PATCH 01/50] WIP: prepare miniscript --- NBitcoin.Tests/Generators/PSBTGenerator.cs | 218 +++++----- NBitcoin/Scripting/Miniscript/AstElem.cs | 133 ++++++ NBitcoin/Scripting/Miniscript/Miniscript.cs | 128 ++++++ .../Scripting/Miniscript/MiniscriptKey.cs | 7 + .../Miniscript/MiniscriptScriptParser.cs | 391 ++++++++++++++++++ .../Scripting/Miniscript/Policy/Compiler.cs | 74 ++++ NBitcoin/Scripting/Miniscript/Property.cs | 7 + NBitcoin/Scripting/Miniscript/ScriptParser.cs | 7 + NBitcoin/Scripting/Miniscript/ScriptToken.cs | 210 ++++++++++ NBitcoin/Scripting/Miniscript/Tree.cs | 13 + .../Scripting/Miniscript/Types/ExtData.cs | 34 ++ .../Miniscript/Types/Malleability.cs | 51 +++ NBitcoin/Scripting/Miniscript/Types/Types.cs | 7 + 13 files changed, 1171 insertions(+), 109 deletions(-) create mode 100644 NBitcoin/Scripting/Miniscript/AstElem.cs create mode 100644 NBitcoin/Scripting/Miniscript/Miniscript.cs create mode 100644 NBitcoin/Scripting/Miniscript/MiniscriptKey.cs create mode 100644 NBitcoin/Scripting/Miniscript/MiniscriptScriptParser.cs create mode 100644 NBitcoin/Scripting/Miniscript/Policy/Compiler.cs create mode 100644 NBitcoin/Scripting/Miniscript/Property.cs create mode 100644 NBitcoin/Scripting/Miniscript/ScriptParser.cs create mode 100644 NBitcoin/Scripting/Miniscript/ScriptToken.cs create mode 100644 NBitcoin/Scripting/Miniscript/Tree.cs create mode 100644 NBitcoin/Scripting/Miniscript/Types/ExtData.cs create mode 100644 NBitcoin/Scripting/Miniscript/Types/Malleability.cs create mode 100644 NBitcoin/Scripting/Miniscript/Types/Types.cs diff --git a/NBitcoin.Tests/Generators/PSBTGenerator.cs b/NBitcoin.Tests/Generators/PSBTGenerator.cs index 639f220060..e1962786da 100644 --- a/NBitcoin.Tests/Generators/PSBTGenerator.cs +++ b/NBitcoin.Tests/Generators/PSBTGenerator.cs @@ -1,109 +1,109 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using FsCheck; -using NBitcoin.Crypto; -using Xunit; - -namespace NBitcoin.Tests.Generators -{ - using HDKeyPathKVMap = Dictionary>; - using PartialSigKVMap = Dictionary>; - using UnknownKVMap = Dictionary; - public class PSBTGenerator - { - public static Arbitrary PSBTArb() => Arb.From(SanePSBT()); - - #region PSBTInput - - public static Gen>> PartialSigs() => - from itemNum in Gen.Choose(0, 15) - from sigs in CryptoGenerator.ECDSAs(itemNum) - from pks in CryptoGenerator.PublicKeys(itemNum) - let items = pks.ToList().Zip(sigs.ToList(), (pk, sig) => Tuple.Create(pk, sig)).ToList() - select Utils.DictionaryFromList>(pks.Select(pk => pk.Hash).ToList(), items); - - #endregion - - #region PSBT - - public static Gen SanePSBT() => - from network in ChainParamsGenerator.NetworkGen() - from psbt in SanePSBT(network) - select psbt; - - /// - /// This is slow, provably because `Add*` methods will iterate over inputs. - /// - /// - /// - public static Gen SanePSBT(Network network) => - from inputN in Gen.Choose(0, 8) - from scripts in Gen.ListOf(inputN, ScriptGenerator.RandomScriptSig()) - from txOuts in Gen.Sequence(scripts.Select(sc => OutputFromRedeem(sc))) - from prevN in Gen.Choose(0, 5) - from prevTxs in Gen.Sequence(txOuts.Select(o => TXFromOutput(o, network, prevN))) - let txins = prevTxs.Select(tx => new TxIn(new OutPoint(tx.GetHash(), prevN))) - from locktime in PrimitiveGenerator.UInt32() - let tx = LegacyTransactionGenerators.ComposeTx(network.CreateTransaction(), txins.ToList(), txOuts.ToList(), locktime) - from TxsToAdd in Gen.SubListOf(prevTxs) - from CoinsToAdd in Gen.SubListOf(prevTxs.SelectMany(tx => tx.Outputs.AsCoins())) - from scriptsToAdd in Gen.SubListOf