Ejemplo n.º 1
0
        public void SetToP2WPKH_FromAddressTest()
        {
            PubkeyScript scr = new PubkeyScript();

            scr.SetToP2WPKH(KeyHelper.Pub1BechAddr);
            byte[] expected = Helper.HexToBytes($"0014{KeyHelper.Pub1BechAddrHex}");
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 2
0
        public void SetToP2WPKH_FromByteTest()
        {
            PubkeyScript scr = new PubkeyScript();

            scr.SetToP2WPKH(Helper.GetBytes(20));
            byte[] expected = Helper.HexToBytes($"0014{Helper.GetBytesHex(20)}");
            Assert.Equal(expected, scr.Data);
        }
Ejemplo n.º 3
0
        public void SetToP2WPKH_ExceptionTest()
        {
            PubkeyScript scr = new PubkeyScript();

            byte[]    nba   = null;
            PublicKey npub  = null;
            string    naddr = null;

            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WPKH(nba));
            Assert.Throws <ArgumentOutOfRangeException>(() => scr.SetToP2WPKH(new byte[19]));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WPKH(npub));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WPKH(naddr));
            Assert.Throws <ArgumentNullException>(() => scr.SetToP2WPKH(""));
            Assert.Throws <FormatException>(() => scr.SetToP2WPKH(KeyHelper.Pub1CompAddr));
        }
        public override bool SetOperations()
        {
            Errors = null;

            if (string.IsNullOrEmpty(Address))
            {
                Errors = "Address can not be empty.";
                return(false);
            }
            else if (addrManager.TryGetType(Address, out PubkeyScriptType scrType, out byte[] hash))
            {
                PubkeyScript scr = new PubkeyScript();
                switch (scrType)
                {
                case PubkeyScriptType.P2PKH:
                    scr.SetToP2PKH(hash);
                    break;

                case PubkeyScriptType.P2SH:
                    scr.SetToP2SH(hash);
                    break;

                case PubkeyScriptType.P2WPKH:
                    scr.SetToP2WPKH(hash);
                    break;

                case PubkeyScriptType.P2WSH:
                    scr.SetToP2WSH(hash);
                    break;

                default:
                    Errors = "Undefined script type.";     // This should never happen.
                    return(false);
                }

                OpsToAdd = scr.OperationList;
                return(true);
            }
Ejemplo n.º 5
0
        public async Task <IBlock> Start(IBlock prev, int height, CancellationToken token)
        {
            // Certain things are hard-coded here because this tool is meant for testing.
            // Eventually it will use IWallet.NextAddress() to get a new address to mine to from the wallet instance
            // and IBlockchain.GetTarget() to mine at the correct difficulty.
            // For now it is a good way of mining any transaction that won't propagate in TestNet by bitcoin core clients.

            var    consensus = new Consensus(height, NetworkType.TestNet);
            string cbText    = "Mined using Denovo v0.1.0";

            // A weak key used only for testing
            using var key = new PrivateKey(new Sha256().ComputeHash(Encoding.UTF8.GetBytes(cbText)));
            var pkScr = new PubkeyScript();

            pkScr.SetToP2WPKH(key.ToPublicKey());

            byte[] commitment = null;

            //var tx1 = new Transaction();
            //tx1.TryDeserialize(new FastStreamReader(Base16.Decode("")), out _);

            ulong fee = 0;

            var coinbase = new Transaction()
            {
                Version  = 1,
                TxInList = new TxIn[]
                {
                    new TxIn(new byte[32], uint.MaxValue, new SignatureScript(height, Encoding.UTF8.GetBytes($"{cbText} by Coding Enthusiast")), uint.MaxValue)
                },
                TxOutList = new TxOut[]
                {
                    new TxOut(consensus.BlockReward + fee, pkScr),
                    new TxOut(0, new PubkeyScript())
                },
                LockTime = new LockTime(height)
            };


            ((PubkeyScript)coinbase.TxOutList[1].PubScript).SetToReturn(Encoding.UTF8.GetBytes("Testing Mining View Model."));


            var block = new Block()
            {
                Header = new BlockHeader()
                {
                    Version   = prev.Header.Version,
                    BlockTime = (uint)UnixTimeStamp.TimeToEpoch(DateTime.UtcNow.AddMinutes(22)),
                    NBits     = 0x1d00ffffU,
                    PreviousBlockHeaderHash = prev.GetBlockHash(false),
                },
                TransactionList = new Transaction[] { coinbase }
            };

            if (block.TransactionList.Length > 1 && commitment != null)
            {
                coinbase.WitnessList = new Witness[1]
                {
                    new Witness(new byte[][] { commitment })
                };

                var temp = new TxOut[coinbase.TxOutList.Length + 1];
                Array.Copy(coinbase.TxOutList, 0, temp, 0, coinbase.TxOutList.Length);
                temp[^ 1] = new TxOut();