コード例 #1
0
        private void RemoteTimestamp(BlockchainProof proof)
        {
            var uri = _configuration.RemoteServer();

            uri = uri.Append($"/api/timestamp/{proof.MerkleRoot.ConvertToBase64()}");

            using (var client = new WebClient())
            {
                var json = client.DownloadString(uri);
                if (!string.IsNullOrEmpty(json))
                {
                    var timestamp = JsonConvert.DeserializeObject <Timestamp>(json);
                    if (timestamp.Proof != null)
                    {
                        if (timestamp.Proof.Confirmations > 0)
                        {
                            proof.Receipt       = ByteExtensions.Combine(timestamp.Path, timestamp.Proof.Receipt);
                            proof.Address       = timestamp.Proof.Address;
                            proof.Confirmations = timestamp.Proof.Confirmations;
                            proof.Blockchain    = timestamp.Proof.Blockchain;
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void RemoteTimestamp(BlockchainProof proof)
        {
            var uri = _configuration.RemoteServer().Append("/api/timestamp");

            var timestamp = _apiService.UploadData <Timestamp>(uri, proof.MerkleRoot).GetAwaiter().GetResult();

            proof.Receipt = timestamp.Path;
        }
コード例 #3
0
        public void Merkle(BlockchainProof proof)
        {
            foreach (var item in proof.Timestamps)
            {
                _merkleTree.Add(item);
            }


            proof.MerkleRoot = _merkleTree.Build().Hash;

            CombineLog(_logger, $"Proof ID:{proof.DatabaseID} Timestamp found {proof.Timestamps.Count} - Merkleroot: {proof.MerkleRoot.ConvertToHex()}");
        }
コード例 #4
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            BlockchainProof = await _context.Proofs.FirstOrDefaultAsync(m => m.DatabaseID == id);

            if (BlockchainProof == null)
            {
                return(NotFound());
            }
            return(Page());
        }
コード例 #5
0
        public override void Execute()
        {
            // Getting the current aggregator for timestamps
            var proof = new BlockchainProof();

            // TODO: Support more than one blockchain type!!
            proof.Timestamps = _db.Timestamps.Where(p => p.ProofDatabaseID == null).ToList();

            if (proof.Timestamps.Count == 0)
            {
                CombineLog(_logger, $"No timestamps found");
                Wait(_configuration.TimestampInterval()); // Default 10 min
                return;                                   // Exit workflow succesfully
            }

            // Ensure a new Proof object!
            try
            {
                Merkle(proof);

                // If funding key is available then use, local timestamping.

                if (IsFundingAvailable())
                {
                    LocalTimestamp(proof);
                }
                else
                {
                    RemoteTimestamp(proof);
                }

                proof.Status = ProofStatusType.Waiting.ToString();

                _db.Proofs.Add(proof);
                // Otherwise use the remote timestamp from trust.dance.
            }
            catch (Exception ex)
            {
                proof.Status = ProofStatusType.Failed.ToString();
                CombineLog(_logger, $"Error in proof ID:{proof.DatabaseID} " + ex.Message + ":" + ex.StackTrace);
            }
            finally
            {
                var result = _db.SaveChangesAsync().GetAwaiter().GetResult();
                _logger.LogTrace($"CreateProofWorkflow save with result code: {result}");
            }
            Wait(_configuration.ConfirmationWait(_configuration.Blockchain()));
        }
コード例 #6
0
        private void ProcessProof(BlockchainProof proof)
        {
            if (proof.Address == "Remote")
            {
                RemoteTimestamp(proof);
                return;
            }

            var addressTimestamp = _blockchainService.GetTimestamp(proof.MerkleRoot);

            if (addressTimestamp == null)
            {
                proof.Status = ProofStatusType.Failed.ToString();
                CombineLog(_logger, $"Proof ID:{proof.DatabaseID} failed with no blockchain transaction found.");
                return;
            }

            proof.RetryAttempts++;
            if (proof.RetryAttempts >= 60)
            {
                proof.Status = ProofStatusType.Failed.ToString();
                CombineLog(_logger, $"Proof ID:{proof.DatabaseID} failed with to many attempts to get a confirmation.");
                return;
            }


            proof.Confirmations = addressTimestamp.Confirmations;
            proof.BlockTime     = addressTimestamp.Time;

            _mediator.Publish(new BlockchainProofUpdatedNotification(proof));

            var confirmationThreshold = _configuration.ConfirmationThreshold(proof.Blockchain);

            CombineLog(_logger, $"Proof ID:{proof.DatabaseID} current confirmations {proof.Confirmations} of {confirmationThreshold}");

            if (proof.Confirmations >= confirmationThreshold)
            {
                proof.Status = ProofStatusType.Done.ToString();
                _mediator.Publish(new BlockchainProofDoneNotification(proof));
            }
        }
コード例 #7
0
        public BlockchainProof GetProof(Timestamp timestamp)
        {
            var merkleRoot = GetMerkleRoot(timestamp);

            // Check cache first, before database
            if (proofCache.TryGetValue(merkleRoot, out BlockchainProof proof))
            {
                return(proof);
            }

            if (timestamp.ProofDatabaseID > 0)
            {
                proof = DB.Proofs.FirstOrDefault(p => p.DatabaseID == timestamp.ProofDatabaseID);
                proofCache[merkleRoot] = proof;
                return(proof);
            }

            proof = new BlockchainProof();
            var blockchainService = _blockchainServiceFactory.GetService(timestamp.Blockchain);

            proof.Blockchain = timestamp.Blockchain;
            // TODO: The merkletee has to be selective from timestamp object and not predefined by the system.
            proof.MerkleRoot = merkleRoot;

            var addressTimestamp = blockchainService.GetTimestamp(proof.MerkleRoot);

            var derivationStrategy = blockchainService.DerivationStrategy;
            var merkleRootKey      = derivationStrategy.GetKey(proof.MerkleRoot);

            proof.Address = derivationStrategy.GetAddress(merkleRootKey);

            proof.Confirmations = addressTimestamp.Confirmations;
            proof.BlockTime     = addressTimestamp.Time;

            proofCache[merkleRoot] = proof;

            return(proof);
        }
コード例 #8
0
        public void LocalTimestamp(BlockchainProof proof)
        {
            var _fundingKeyWIF     = _configuration.FundingKey(proof.Blockchain);
            var _blockchainService = _blockchainServiceFactory.GetService(proof.Blockchain);
            var fundingKey         = _blockchainService.DerivationStrategy.KeyFromString(_fundingKeyWIF);

            var tempTxKey      = proof.Blockchain + "_previousTx";
            var previousTx     = _keyValueService.Get(tempTxKey);
            var previousTxList = (previousTx != null) ? new List <Byte[]> {
                previousTx
            } : null;

            var OutTx = _blockchainService.Send(proof.MerkleRoot, fundingKey, previousTxList);

            //OutTX needs to go to a central store for that blockchain
            _keyValueService.Set(tempTxKey, OutTx[0]);

            var merkleRootKey = _blockchainService.DerivationStrategy.GetKey(proof.MerkleRoot);

            proof.Address = _blockchainService.DerivationStrategy.GetAddress(merkleRootKey);

            CombineLog(_logger, $"Proof ID:{proof.DatabaseID} Merkle root: {proof.MerkleRoot.ConvertToHex()} has been timestamped with address: {proof.Address}");
        }
コード例 #9
0
 public BlockchainProofDoneNotification(BlockchainProof proof)
 {
     Proof = proof;
 }
コード例 #10
0
 public BlockchainProofCreatedNotification(BlockchainProof proof)
 {
     Proof = proof;
 }
コード例 #11
0
 private bool IsFundingAvailable(BlockchainProof proof)
 {
     return(!string.IsNullOrEmpty(_configuration.FundingKey(proof.Blockchain)));
 }