Ejemplo n.º 1
0
        //web server calls

        /************************************************************************
        * Mines a new block, sending the reward to the specified address
        ************************************************************************/
        public string Mine(string address)
        {
            Logger.Log(string.Format("Mining for new block for ID={0}", address));

            //Rudimentary consensus algorithm
            //Resolve conflicts this and registered server nodes (ie. update the chain)
            Consensus();

            //create proof of work
            int proof = CreateProofOfWork(_lastBlock.Proof, _lastBlock.PreviousHash);

            //create a transaction object
            Transaction tx = new Transaction();

            tx.id      = Guid.NewGuid();             //set a new ID for the transaction
            tx.Inputs  = new List <Input>();         //a list to hold the inputs to the transaction
            tx.Outputs = new List <Output>();        //a list to hold the outputs from the transaction
            tx.Outputs.Add(new Output(address, 10)); //add reward to the mine transaction
            //sign the transaction with the server keys and add the public key so the signature can be checked
            tx.Signature = _cryptoProvider.SignMessage(tx.ToString(), _privateKey, _publicKey);
            tx.PublicKey = _publicKey.ToBase64String();


            //create a new transaction containing the reward. IsMining == true, so there are no input nodes to validate
            CreateTransaction(tx, true);

            //create a new block (commit all uncommitted transactions, including the reward transaction)
            Block block = CreateNewBlock(proof);

            //create a response object, which will be serialized and returned
            var response = new
            {
                Message      = "New Block Forged",
                Index        = block.Index,
                Transactions = block.Transactions.ToArray(),
                Proof        = block.Proof,
                PreviousHash = block.PreviousHash
            };

            Logger.Log(string.Format("Mined new block Index={0}, Proof={1}, previous Hash={2}", block.Index, block.Proof, block.PreviousHash));

            //serialize and return the response
            return(JsonConvert.SerializeObject(response));
        }
Ejemplo n.º 2
0
 /*********************************************
 * Creates a signature for an address string
 *********************************************/
 public static string SignAddress(string address, string salt, ICryptoProvider provider)
 {
     return(provider.SignMessage(salt + address));
 }