public static Gen <TxIn> Input() => from prevout in OutPoint() from ss in ScriptGenerator.RandomScriptSig() from nSequence in PrimitiveGenerator.UInt32() select new TxIn(prevout, ss) { Sequence = nSequence };
public static Gen <UnknownKVMap> UnknownKVMap() => from itemNum in Gen.Choose(0, 15) from keys in Gen.ListOf(itemNum, PrimitiveGenerator.RandomBytes()) where PSBTConstants.PSBT_IN_ALL.All(i => keys.Select(k => k.First()).All(k => k != i)) let uniqKeys = keys.Distinct() from values in Gen.ListOf(uniqKeys.Count(), PrimitiveGenerator.RandomBytes()) select Utils.DictionaryFromList <byte[], byte[]>(uniqKeys.ToList(), values.ToList());
public static Gen <Op> NonPushOpcode() => from b in PrimitiveGenerator.RandomByte() where Enum.IsDefined(typeof(OpcodeType), b) let op = (OpcodeType)b where !Op.IsPushCode(op) let opc = (Op)(OpcodeType)b where !opc.IsInvalid select opc;
public static Gen <Transaction> TX(Network network) => from version in Gen.Choose(0, Int32.MaxValue) from inputs in NonEmptyInputs() from outputs in NonEmptyOutputs() from locktime in PrimitiveGenerator.UInt32() let tx = LegacyTransactionGenerators.ComposeTx(Transaction.Create(network), inputs, outputs, locktime) where tx.HasWitness select tx;
private static Gen <TxOut> OutputFromRedeem(Script sc) => from money in MoneyGenerator.Money() from isP2WSH in PrimitiveGenerator.Bool() from isP2SH in PrimitiveGenerator.Bool() where isP2WSH || isP2SH let redeem = (isP2SH && isP2WSH) ? sc.WitHash.ScriptPubKey : sc let scriptPubKey = isP2SH ? redeem.Hash.ScriptPubKey : redeem.WitHash.ScriptPubKey select new TxOut(money, scriptPubKey);
/// <summary> /// This is slow, provably because `Add*` methods will iterate over inputs. /// </summary> /// <param name="network"></param> /// <returns></returns> public static Gen <PSBT> 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 <Script>(scripts) let psbt = tx.CreatePSBT() .AddTransactions(prevTxs.ToArray()) .AddCoins(CoinsToAdd.ToArray()) .AddScripts(scriptsToAdd.ToArray()) select psbt;
private static Gen <TxOut> OutputFromKey(PubKey key) => from money in MoneyGenerator.Money() from isP2WPKH in PrimitiveGenerator.Bool() from isP2SH in PrimitiveGenerator.Bool() let scriptPubKey = (isP2SH && isP2WPKH) ? key.WitHash.ScriptPubKey.Hash.ScriptPubKey : isP2WPKH ? key.WitHash.ScriptPubKey : key.Hash.ScriptPubKey select new TxOut(money, scriptPubKey);
public static Gen <uint160> Hash160() => from bytes in PrimitiveGenerator.RandomBytes(20) select new uint160(bytes);
public static Gen <uint256> Hash256() => from bytes in PrimitiveGenerator.RandomBytes(32) select new uint256(bytes);
private static Gen <Op> PushOnlyOpcode() => from bytes in PrimitiveGenerator.RandomBytes(4) select Op.GetPushOp(bytes);
public static Gen <uint256> Hash256() => PrimitiveGenerator.RandomBytes().Select(bs => Hashes.Hash256(bs));
public static Gen <HDKeyPathKVMap> HDKeyPaths(IEnumerable <PubKey> pks, int itemNum) => from MasterKeyFingerPrints in Gen.ListOf(itemNum, PrimitiveGenerator.UInt32()) from paths in Gen.ListOf(itemNum, CryptoGenerator.KeyPath()) let fingerPrintAndPath = MasterKeyFingerPrints.ToArray().Zip(paths.ToArray(), (m, p) => Tuple.Create(m, p)).ToList() select Utils.DictionaryFromList <PubKey, Tuple <uint, KeyPath> >(pks.ToList(), fingerPrintAndPath);
public static Gen <Money> Money() => (from bytes in Gen.ListOf(6, PrimitiveGenerator.RandomByte()) // Make sure we are below 21M select Utils.ToUInt64(bytes.Concat(new byte[] { 0, 0 }).ToArray(), true)) .Select(u64 => new Money(u64));
public static Gen <Transaction> TX(Network network) => from version in Gen.Choose(0, Int32.MaxValue) from txin in NonEmptyInputs() from txout in NonEmptyOutputs() from locktime in PrimitiveGenerator.UInt32() select ComposeTx(Transaction.Create(network), txin, txout, locktime);
public static Gen <uint160> Hash160() => PrimitiveGenerator.RandomBytes().Select(bs => Hashes.Hash160(bs));
public static Gen <HDFingerprint> HDFingerprint() => from bytes in PrimitiveGenerator.RandomBytes(4) select new HDFingerprint(bytes);
public static Gen <KeyPath> KeyPath() => from raw in Gen.NonEmptyListOf(PrimitiveGenerator.RandomBytes(4)) let flattenBytes = raw.ToList().Aggregate((a, b) => a.Concat(b)) select NBitcoin.KeyPath.FromBytes(flattenBytes);
public static Gen <Dictionary <byte[], byte[]> > UnknownKVMap() => from itemNum in Gen.Choose(0, 15) from keys in Gen.ListOf(itemNum, PrimitiveGenerator.RandomBytes()) from values in Gen.ListOf(itemNum, PrimitiveGenerator.RandomBytes()) select Utils.DictionaryFromList <byte[], byte[]>(keys.ToList(), values.ToList());
public static Gen <HDFingerprint> HDFingerPrint() => from x in PrimitiveGenerator.UInt32() select new HDFingerprint(x);