Ejemplo n.º 1
0
        public void SetToP2SH_P2WPKH_FromPubkeyTest(PublicKey pub, bool comp, byte[] expected)
        {
            SignatureScript scr = new SignatureScript();

            scr.SetToP2SH_P2WPKH(pub, comp);
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 2
0
        public void SetToP2PKH_ExceptionTest()
        {
            SignatureScript scr = new SignatureScript();

            Assert.Throws <ArgumentNullException>(() => scr.SetToP2PKH(null, KeyHelper.Pub1, true));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2PKH(Helper.ShortSig1, null, true));
        }
Ejemplo n.º 3
0
        public void Constructor_WithNullBytesTest()
        {
            byte[]          data = null;
            SignatureScript scr  = new SignatureScript(data);

            Assert.Empty(scr.Data); // NotNull
        }
Ejemplo n.º 4
0
        public void VerifyCoinbaseTest(byte[] data, IConsensus consensus, bool expected)
        {
            SignatureScript scr    = new SignatureScript(data);
            bool            actual = scr.VerifyCoinbase(consensus);

            Assert.Equal(expected, actual);
        }
Ejemplo n.º 5
0
        public void SetToP2PKHTest(bool useComp, PublicKey pub, Signature sig, byte[] expected)
        {
            SignatureScript scr = new SignatureScript();

            scr.SetToP2PKH(sig, pub, useComp);
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 6
0
        public void SetToMultiSig_ArgumentExceptionTest(Signature sig, IRedeemScript rdm, ITransaction tx, int index, string expErr)
        {
            var       scr = new SignatureScript();
            Exception ex  = Assert.Throws <ArgumentException>(() => scr.SetToMultiSig(sig, rdm, tx, index));

            Assert.Contains(expErr, ex.Message);
        }
Ejemplo n.º 7
0
        public void SetToMultiSigTest(byte[] scrData, Signature sig, IRedeemScript rdm, ITransaction tx, int index, byte[] expected)
        {
            var scr = new SignatureScript(scrData);

            scr.SetToMultiSig(sig, rdm, tx, index);
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 8
0
        public void Constructor_WithBytesTest()
        {
            byte[]          data = { 1, 2, 3 };
            SignatureScript scr  = new SignatureScript(data);

            data[0] = 255; // Make sure data is cloned
            Assert.Equal(new byte[] { 1, 2, 3 }, scr.Data);
        }
Ejemplo n.º 9
0
        public void SetToP2SH_P2WSH_ExceptionTest()
        {
            SignatureScript scr = new SignatureScript();
            var             rdm = new MockSerializableRedeemScript(RedeemScriptType.Empty, new byte[0], 0);

            Assert.Throws <ArgumentNullException>(() => scr.SetToP2SH_P2WSH(null));
            Assert.Throws <ArgumentException>(() => scr.SetToP2SH_P2WSH(rdm));
        }
Ejemplo n.º 10
0
        public void SetToEmptyTest()
        {
            SignatureScript scr = new SignatureScript(new byte[2]);

            Assert.NotEmpty(scr.Data);
            scr.SetToEmpty();
            Assert.Empty(scr.Data);
        }
Ejemplo n.º 11
0
        public void SetToP2SH_P2WSHTest()
        {
            SignatureScript scr = new SignatureScript();
            var             rdm = new MockSerializableRedeemScript(RedeemScriptType.P2SH_P2WSH, new byte[] { 1, 2, 3 }, 255);

            scr.SetToP2SH_P2WSH(rdm);
            byte[] expected = new byte[] { 3, 1, 2, 3 };
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 12
0
        public void SetToP2PKTest()
        {
            SignatureScript scr = new SignatureScript();

            scr.SetToP2PK(Helper.ShortSig1);
            byte[] expected = Helper.HexToBytes($"{Helper.ShortSig1Hex.Length / 2:x2}{Helper.ShortSig1Hex}");

            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 13
0
        public void SetToCheckLocktimeVerify_ExceptionTest()
        {
            SignatureScript scr = new SignatureScript();
            var             rdm = new MockSerializableRedeemScript(RedeemScriptType.Empty, new byte[] { 1, 2, 3 }, 255);

            Assert.Throws <ArgumentException>(() => scr.SetToCheckLocktimeVerify(Helper.ShortSig1, rdm));
            Assert.Throws <ArgumentNullException>(() => scr.SetToCheckLocktimeVerify(null, rdm));
            Assert.Throws <ArgumentNullException>(() => scr.SetToCheckLocktimeVerify(Helper.ShortSig1, null));
        }
Ejemplo n.º 14
0
        public void Data_PropertySetter_Test()
        {
            SignatureScript scr = new SignatureScript()
            {
                Data = null
            };

            Assert.Empty(scr.Data); // NotNull
        }
Ejemplo n.º 15
0
        public void SetToCheckLocktimeVerifyTest()
        {
            SignatureScript scr = new SignatureScript();
            var             rdm = new MockSerializableRedeemScript(RedeemScriptType.CheckLocktimeVerify, new byte[] { 1, 2, 3 }, 255);

            scr.SetToCheckLocktimeVerify(Helper.ShortSig1, rdm);
            byte[] expected = Helper.HexToBytes($"{Helper.ShortSig1Hex.Length / 2:x2}{Helper.ShortSig1Hex}03010203");

            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 16
0
        public void SignatureScriptTest()
        {
            var signatureScript = new SignatureScript(
                _blockTemplate.Height,
                _blockTemplate.CoinBaseAux.Flags,
                1402265775319,
                (byte)_extraNonce.ExtraNoncePlaceholder.Length,
                "/nodeStratum/");

            // test the objects.
            signatureScript.Initial.ToHexString().Should().Equal("03547f04062f503253482f04afe0945308");
            signatureScript.Final.ToHexString().Should().Equal("0d2f6e6f64655374726174756d2f");
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Initializes a new instance of <see cref="TxIn"/> using given parameters.
        /// </summary>
        /// <exception cref="ArgumentNullException"/>
        /// <param name="op">Transaction output</param>
        /// <param name="sigScript">Signature script</param>
        /// <param name="seq">Sequence</param>
        public TxIn(Outpoint op, SignatureScript sigScript, uint seq)
        {
            SetFields();
            if (op == null)
            {
                throw new ArgumentNullException(nameof(op), "Outpoint can not be null!");
            }
            if (sigScript == null)
            {
                throw new ArgumentNullException(nameof(sigScript), "Signature script can not be null!");
            }

            Outpoint  = op;
            SigScript = sigScript;
            Sequence  = seq;
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Creates a new instance of generation transaction.
        /// </summary>
        /// <param name="extraNonce">The extra nonce.</param>
        /// <param name="blockTemplate">The block template.</param>
        /// <param name="poolConfig">The associated pool's configuration</param>
        /// <remarks>
        /// Reference implementations:
        /// https://github.com/zone117x/node-stratum-pool/blob/b24151729d77e0439e092fe3a1cdbba71ca5d12e/lib/transactions.js
        /// https://github.com/Crypto-Expert/stratum-mining/blob/master/lib/coinbasetx.py
        /// </remarks>
        public GenerationTransaction(IExtraNonce extraNonce, IBlockTemplate blockTemplate, IPoolConfig poolConfig)
        {
            _logger = Log.ForContext <GenerationTransaction>();

            BlockTemplate = blockTemplate;
            ExtraNonce    = extraNonce;
            PoolConfig    = poolConfig;

            Version   = blockTemplate.Version;
            TxMessage = Serializers.SerializeString(poolConfig.Meta.TxMessage);
            LockTime  = 0;

            CoinbaseSignatureScript =
                new SignatureScript(
                    blockTemplate.Height,
                    blockTemplate.CoinBaseAux.Flags,
                    TimeHelpers.NowInUnixTimestamp(),
                    (byte)extraNonce.ExtraNoncePlaceholder.Length,
                    "/MeritPool/");
        }
        public override byte[] Serialize()
        {
            var result = new List <byte>();

            result.AddRange(Outpoint.Serialize());
            var compactSize = new CompactSize();

            compactSize.Size = 0;
            if (SignatureScript != null)
            {
                compactSize.Size = (ulong)SignatureScript.Count();
                result.AddRange(compactSize.Serialize());
                result.AddRange(SignatureScript);
            }
            else
            {
                result.AddRange(compactSize.Serialize());
            }

            result.AddRange(BitConverter.GetBytes(Sequence));
            return(result.ToArray());
        }
Ejemplo n.º 20
0
        public static IEnumerable <object[]> GetTxVerifyCases()
        {
            var c = new MockConsensus()
            {
                expHeight = MockHeight,
                bip65     = true,
                bip112    = true,
                strictDer = true,
                bip147    = true,
                bip16     = true,
                segWit    = true,
            };

            byte[] simpTxHash1 = new byte[32];
            simpTxHash1[1] = 1;
            byte[] simpTxHash2 = new byte[32];
            simpTxHash2[1] = 2;
            byte[] simpTxHash3 = new byte[32];
            simpTxHash3[1] = 3;

            var simpPubScr = new PubkeyScript();
            var simpSigScr = new SignatureScript(new byte[1] {
                (byte)OP._1
            });

            // *** Fee Tests ***

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 0, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(0, simpPubScr)
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                0,     // Added fee
                false, // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 0, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(1, simpPubScr)
                    }
                },
                false,                                       // Verification success
                "Transaction is spending more than it can.", // Error
                0,                                           // Added SigOpCount
                0,                                           // Added fee
                false,                                       // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = 123, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(3, simpPubScr)
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                120,   // Added fee
                false, // AnySegWit
            });

            yield return(new object[]
            {
                // Make sure ulong is being used for calculation of fee
                new MockUtxoDatabase(simpTxHash1, new MockUtxo()
                {
                    Amount = Constants.TotalSupply, Index = 0, PubScript = simpPubScr
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[1]
                    {
                        new TxIn(simpTxHash1, 0, simpSigScr, uint.MaxValue)
                    },
                    TxOutList = new TxOut[1]
                    {
                        new TxOut(1000, simpPubScr)
                    }
                },
                true,                           // Verification success
                null,                           // Error
                0,                              // Added SigOpCount
                Constants.TotalSupply - 1000UL, // Added fee
                false,                          // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(new byte[][] { simpTxHash1, simpTxHash2, simpTxHash3 },
                                     new IUtxo[]
                {
                    new MockUtxo()
                    {
                        Amount = 13, Index = 3, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 57, Index = 7, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 73, Index = 5, PubScript = simpPubScr
                    },
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[3]
                    {
                        new TxIn(simpTxHash1, 3, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash2, 7, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash3, 5, simpSigScr, uint.MaxValue),
                    },
                    TxOutList = new TxOut[3]
                    {
                        new TxOut(140, simpPubScr),
                        new TxOut(3, simpPubScr),
                        new TxOut(1, simpPubScr),
                    }
                },
                false,                                       // Verification success
                "Transaction is spending more than it can.", // Error
                0,                                           // Added SigOpCount
                0,                                           // Added fee
                false,                                       // AnySegWit
            });

            yield return(new object[]
            {
                new MockUtxoDatabase(new byte[][] { simpTxHash1, simpTxHash2, simpTxHash3 },
                                     new IUtxo[]
                {
                    new MockUtxo()
                    {
                        Amount = 13, Index = 3, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 57, Index = 7, PubScript = simpPubScr
                    },
                    new MockUtxo()
                    {
                        Amount = 73, Index = 5, PubScript = simpPubScr
                    },
                }),
                new MockMempool(null),
                c,
                new Transaction()
                {
                    TxInList = new TxIn[3]
                    {
                        new TxIn(simpTxHash1, 3, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash2, 7, simpSigScr, uint.MaxValue),
                        new TxIn(simpTxHash3, 5, simpSigScr, uint.MaxValue),
                    },
                    TxOutList = new TxOut[3]
                    {
                        new TxOut(12, simpPubScr),
                        new TxOut(6, simpPubScr),
                        new TxOut(50, simpPubScr),
                    }
                },
                true,  // Verification success
                null,  // Error
                0,     // Added SigOpCount
                75,    // Added fee
                false, // AnySegWit
            });

            // *** Transaction Tests (from signtx cases) ***
            foreach (var Case in Helper.ReadResource <JArray>("SignedTxTestData"))
            {
                Transaction prevTx = new Transaction();
                prevTx.TryDeserialize(new FastStreamReader(Helper.HexToBytes(Case["TxToSpend"].ToString())), out string _);
                byte[] prevTxHash = prevTx.GetTransactionHash();
                var    utxoDb     = new MockUtxoDatabase();

                for (int i = 0; i < prevTx.TxOutList.Length; i++)
                {
                    var utxo = new MockUtxo()
                    {
                        Amount    = prevTx.TxOutList[i].Amount,
                        Index     = (uint)i,
                        PubScript = prevTx.TxOutList[i].PubScript
                    };

                    utxoDb.Add(prevTxHash, utxo);
                }

                foreach (var item in Case["Cases"])
                {
                    Transaction      tx     = new Transaction();
                    FastStreamReader stream = new FastStreamReader(Helper.HexToBytes(item["SignedTx"].ToString()));
                    if (!tx.TryDeserialize(stream, out string err))
                    {
                        throw new ArgumentException($"Could not deseralize the given tx case: " +
                                                    $"{item["TestName"]}. Error: {err}");
                    }

                    int   sigOpCount = (int)item["SigOpCount"];
                    ulong fee        = (ulong)item["Fee"];

                    yield return(new object[]
                    {
                        utxoDb,
                        new MockMempool(null),
                        c,
                        tx,
                        true, // Verification success
                        null, // Error
                        sigOpCount,
                        fee,
                        tx.WitnessList != null, // AnySegWit
                    });
                }
            }
        }
Ejemplo n.º 21
0
        public void Constructor_CoinbaseTest(int height, byte[] extra, byte[] expected)
        {
            SignatureScript scr = new SignatureScript(height, extra);

            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 22
0
        public void SetToP2PKTest_ExceptionTest()
        {
            SignatureScript scr = new SignatureScript();

            Assert.Throws <ArgumentNullException>(() => scr.SetToP2PK(null));
        }
Ejemplo n.º 23
0
        public void Constructor_EmptyOpsTest()
        {
            SignatureScript scr = new SignatureScript(new IOperation[0]);

            Assert.Equal(new byte[0], scr.Data);
        }
Ejemplo n.º 24
0
        public void ConstructorTest()
        {
            SignatureScript scr = new SignatureScript();

            Assert.Empty(scr.Data);
        }
Ejemplo n.º 25
0
 /// <summary>
 /// Initializes a new instance of <see cref="TxIn"/> using parameters of the <see cref="ICoin"/>.
 /// </summary>
 public TxIn()
 {
     SetFields();
     Outpoint  = new Outpoint();
     SigScript = new SignatureScript();
 }
Ejemplo n.º 26
0
        public void Constructor_OpsTest()
        {
            SignatureScript scr = new SignatureScript(new IOperation[] { new DUPOp(), new PushDataOp(new byte[] { 10, 20, 30 }) });

            Assert.Equal(new byte[] { (byte)OP.DUP, 3, 10, 20, 30 }, scr.Data);
        }