Ejemplo n.º 1
0
        public static Block CreateNewBlock(Account genesisAccount, Dapp dapp)
        {
            var transactions = new List <Transaction>();
            var delegates    = new List <Account>();

            var sender = AccountHelper.GetAccount(CryptoHelper.GenerateSecret());

            long totalAmount = 0;

            var balTransaction = new Transaction
            {
                Type            = TransactionType.Send,
                Amount          = 10000000000000000,
                Fee             = 0,
                Timestamp       = 0,
                RecipientId     = genesisAccount.Address.IdString,
                SenderId        = sender.Address.IdString,
                SenderPublicKey = sender.Address.KeyPair.PublicKey.ToHex()
            };

            totalAmount += balTransaction.Amount;

            var bytes = balTransaction.GetBytes();

            balTransaction.Signature = CryptoHelper.Sign(bytes, sender.Address.KeyPair.PrivateKey).ToHex();
            balTransaction.Id        = CryptoHelper.GetId(sender.Address.KeyPair.PublicKey);

            transactions.Add(balTransaction);

            for (int index = 0; index < Constants.ActiveDelegates; index++)
            {
                var delegateSecret = CryptoHelper.GenerateSecret();
                var delAccount     = AccountHelper.GetAccount(delegateSecret);
                delegates.Add(delAccount);
                var userName = string.Format("genesisDelegate{0}", index + 1);

                var delTransaction = new Transaction
                {
                    Type            = TransactionType.Delegate,
                    Amount          = 0,
                    Fee             = 0,
                    Timestamp       = 0,
                    RecipientId     = null,
                    SenderId        = delAccount.Address.IdString,
                    SenderPublicKey = delAccount.Address.KeyPair.PublicKey.ToHex(),

                    Asset = new DelegateUsernameAsset
                    {
                        Delegate = new DelegateUsername
                        {
                            Username = userName
                        }
                    }
                };

                var delBytes = delTransaction.GetBytes();
                delTransaction.Signature = CryptoHelper.Sign(delBytes, delAccount.Address.KeyPair.PrivateKey).ToHex();
                delTransaction.Id        = CryptoHelper.GetId(delBytes);

                transactions.Add(delTransaction);
            }

            var votes = delegates.Select(vote => string.Format("+{0}", vote.Address.KeyPair.PublicKey.ToHex()));

            var voteAsset = new DelegateVoteAsset();

            voteAsset.Votes.AddRange(votes);

            var voteTransaction = new Transaction
            {
                Type            = TransactionType.Vote,
                Amount          = 0,
                Fee             = 0,
                Timestamp       = 0,
                RecipientId     = genesisAccount.Address.IdString,
                SenderId        = genesisAccount.Address.IdString,
                SenderPublicKey = genesisAccount.Address.KeyPair.PublicKey.ToHex(),
                Asset           = voteAsset
            };
            var voteBytes = voteTransaction.GetBytes();

            voteTransaction.Signature = CryptoHelper.Sign(voteBytes, genesisAccount.Address.KeyPair.PrivateKey).ToHex();
            voteTransaction.Id        = CryptoHelper.GetId(voteBytes);

            transactions.Add(voteTransaction);

            var dappTransaction = new Transaction
            {
                Type            = TransactionType.Dapp,
                Amount          = 0,
                Fee             = 0,
                Timestamp       = 0,
                RecipientId     = null,
                SenderId        = genesisAccount.Address.IdString,
                SenderPublicKey = genesisAccount.Address.KeyPair.PublicKey.ToHex(),
                Asset           = new DappAsset
                {
                    Dapp = dapp
                }
            };

            var dappBytes = dappTransaction.GetBytes();

            dappTransaction.Signature = CryptoHelper.Sign(dappBytes, genesisAccount.Address.KeyPair.PrivateKey).ToHex();
            dappTransaction.Id        = CryptoHelper.GetId(dappBytes);

            transactions.Add(dappTransaction);

            var sortedTransactions = transactions.OrderBy(t => (int)t.Type).ThenBy(t => t.Amount).ToList();
            var payloadLength      = 0;
            var hashBuf            = new List <byte>();

            foreach (var sortedTransaction in sortedTransactions)
            {
                var trBytes = sortedTransaction.GetBytes();
                payloadLength += trBytes.Length;
                hashBuf.AddRange(trBytes);
            }
            var payloadHash = CryptoHelper.Sha256(hashBuf.ToArray());
            var block       = new Block
            {
                Version              = 0,
                TotalAmount          = 0,
                TotalFee             = 0,
                PayloadHash          = payloadHash.ToHex(),
                Timestamp            = 0,
                NumberOfTransactions = sortedTransactions.Count,
                PayloadLength        = payloadLength,
                PreviousBlock        = null,
                GeneratorPublicKey   = sender.Address.KeyPair.PublicKey.ToHex(),
                Transactions         = sortedTransactions,
                Height = 1
            };
            var blockBytes = block.GetBytes();

            block.BlockSignature = CryptoHelper.Sign(blockBytes, sender.Address.KeyPair.PrivateKey).ToHex();
            block.Id             = CryptoHelper.GetId(blockBytes).ToString();
            return(block);
        }
Ejemplo n.º 2
0
        public static Block CreateNewBlock(Account genesisAccount, Dapp dapp)
        {
            var transactions = new List<Transaction>();
            var delegates = new List<Account>();

            var sender = AccountHelper.GetAccount(CryptoHelper.GenerateSecret());

            long totalAmount = 0;

            var balTransaction = new Transaction
            {
                Type = TransactionType.Send,
                Amount = 10000000000000000,
                Fee = 0,
                Timestamp = 0,
                RecipientId = genesisAccount.Address.IdString,
                SenderId = sender.Address.IdString,
                SenderPublicKey = sender.Address.KeyPair.PublicKey.ToHex()
            };
            totalAmount += balTransaction.Amount;

            var bytes = balTransaction.GetBytes();
            balTransaction.Signature = CryptoHelper.Sign(bytes, sender.Address.KeyPair.PrivateKey).ToHex();
            balTransaction.Id = CryptoHelper.GetId(sender.Address.KeyPair.PublicKey);

            transactions.Add(balTransaction);

            for (int index = 0; index < Constants.ActiveDelegates; index++)
            {
                var delegateSecret = CryptoHelper.GenerateSecret();
                var delAccount = AccountHelper.GetAccount(delegateSecret);
                delegates.Add(delAccount);
                var userName = string.Format("genesisDelegate{0}", index + 1);

                var delTransaction = new Transaction
                {
                    Type = TransactionType.Delegate,
                    Amount = 0,
                    Fee = 0,
                    Timestamp = 0,
                    RecipientId = null,
                    SenderId = delAccount.Address.IdString,
                    SenderPublicKey = delAccount.Address.KeyPair.PublicKey.ToHex(),

                    Asset = new DelegateUsernameAsset
                    {
                        Delegate = new DelegateUsername
                        {
                            Username = userName
                        }
                    }
                };

                var delBytes = delTransaction.GetBytes();
                delTransaction.Signature = CryptoHelper.Sign(delBytes, delAccount.Address.KeyPair.PrivateKey).ToHex();
                delTransaction.Id = CryptoHelper.GetId(delBytes);

                transactions.Add(delTransaction);
            }

            var votes = delegates.Select(vote => string.Format("+{0}", vote.Address.KeyPair.PublicKey.ToHex()));

            var voteAsset = new DelegateVoteAsset();
            voteAsset.Votes.AddRange(votes);

            var voteTransaction = new Transaction
            {
                Type = TransactionType.Vote,
                Amount = 0,
                Fee = 0,
                Timestamp = 0,
                RecipientId = genesisAccount.Address.IdString,
                SenderId = genesisAccount.Address.IdString,
                SenderPublicKey = genesisAccount.Address.KeyPair.PublicKey.ToHex(),
                Asset = voteAsset
            };
            var voteBytes = voteTransaction.GetBytes();
            voteTransaction.Signature = CryptoHelper.Sign(voteBytes, genesisAccount.Address.KeyPair.PrivateKey).ToHex();
            voteTransaction.Id = CryptoHelper.GetId(voteBytes);

            transactions.Add(voteTransaction);

            var dappTransaction = new Transaction
            {
                Type = TransactionType.Dapp,
                Amount = 0,
                Fee = 0,
                Timestamp = 0,
                RecipientId = null,
                SenderId = genesisAccount.Address.IdString,
                SenderPublicKey = genesisAccount.Address.KeyPair.PublicKey.ToHex(),
                Asset = new DappAsset
                {
                    Dapp = dapp
                }
            };

            var dappBytes = dappTransaction.GetBytes();
            dappTransaction.Signature = CryptoHelper.Sign(dappBytes, genesisAccount.Address.KeyPair.PrivateKey).ToHex();
            dappTransaction.Id = CryptoHelper.GetId(dappBytes);

            transactions.Add(dappTransaction);

            var sortedTransactions = transactions.OrderBy(t => (int) t.Type).ThenBy(t => t.Amount).ToList();
            var payloadLength = 0;
            var hashBuf = new List<byte>();
            foreach (var sortedTransaction in sortedTransactions)
            {
                var trBytes = sortedTransaction.GetBytes();
                payloadLength += trBytes.Length;
                hashBuf.AddRange(trBytes);
            }
            var payloadHash = CryptoHelper.Sha256(hashBuf.ToArray());
            var block = new Block
            {
               Version = 0,
               TotalAmount = 0,
               TotalFee = 0,
               PayloadHash = payloadHash.ToHex(),
               Timestamp = 0,
               NumberOfTransactions = sortedTransactions.Count,
               PayloadLength = payloadLength,
               PreviousBlock = null,
               GeneratorPublicKey = sender.Address.KeyPair.PublicKey.ToHex(),
               Transactions = sortedTransactions,
               Height = 1
            };
            var blockBytes = block.GetBytes();
            block.BlockSignature = CryptoHelper.Sign(blockBytes, sender.Address.KeyPair.PrivateKey).ToHex();
            block.Id = CryptoHelper.GetId(blockBytes).ToString();
            return block;
        }