コード例 #1
0
        public async Task Create_NotExist_Ok()
        {
            const string bName = "myBlockchain";
            var          dir   = Path.Combine(MultiChainTools.GetAppDataFolder(), bName);

            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }

            await MultiChainUtilHandler.CreateBlockchain(bName);

            Assert.IsTrue(Directory.Exists(dir));
        }
コード例 #2
0
        public async Task Create_Exists_Exception()
        {
            const string bName = "myBlockchain";
            var          dir   = Path.Combine(MultiChainTools.GetAppDataFolder(), bName);

            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }

            await MultiChainUtilHandler.CreateBlockchain(bName);

            // Attempt to create it again
            try
            {
                await MultiChainUtilHandler.CreateBlockchain(bName);
            }
            catch (CouldNotCreateBlockchainException e)
            {
                Assert.AreEqual("ERROR: Blockchain parameter set was not generated.", e.Message);
            }
        }
コード例 #3
0
        public async Task <IHttpActionResult> CreateBlockchain(CreateBlockchain model)
        {
            // Must have at least one question
            if ((model.Questions == null) || !model.Questions.Any())
            {
                ModelState.AddModelError("Questions", "At least one question is required");
                return(BadRequest(ModelState));
            }

            // All questions must have at least one answer (although should probably have at least two?)
            if (model.Questions.Any(q => (q.Answers == null) || !q.Answers.Any()))
            {
                ModelState.AddModelError("Questions", "All questions must have at least one answer");
                return(BadRequest(ModelState));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Check we're not going to have a ChainString collision
            try
            {
                await _blockchainStore.GetBlockchainByChainString(model.ChainString);

                return(BadRequest("A Blockchain already exists with the same name as this!"));
            }
            catch (RecordNotFoundException)
            {
                // This is good
            }

            // multichain-util create {model.ChainString}
            await MultiChainUtilHandler.CreateBlockchain(model.ChainString);

            // Find a Port to run multichaind on
            var blockchains = await _blockchainStore.GetAllBlockchains();

            int port;

            while (true)
            {
                port = MultiChainTools.GetNewPort(EPortType.MultichainD);
                if (blockchains.All(b => b.Port != port))
                {
                    break;
                }
            }

            // Update the default multichain.params to adjust permissions etc.
            UpdateParams(model);

            // Run the blockchain for the first time
            var rpcPort = MultiChainTools.GetNewPort(EPortType.MultichainD);
            var chain   =
                await _multichaind.Connect(IPAddress.Loopback.ToString(), model.ChainString, port, port, rpcPort, false);

            // Convert questions. Each has a unique number (per blockchain) starting from 1
            var questionNumber = 1;

            foreach (var q in model.Questions.Select(q => RequestToBlockchainQuestion(q, ref questionNumber)))
            {
                await chain.WriteToStream(MultiChainTools.ROOT_STREAM_NAME, MultiChainTools.QUESTIONS_KEY, q);
            }

            // Create a new wallet ID for votes to be sent to
            var walletId = await chain.GetNewWalletAddress();

            var blockchain = new RegiBlockchain
            {
                Name        = model.Name,
                ExpiryDate  = model.ExpiryDate,
                Info        = model.Info,
                ChainString = model.ChainString,
                WalletId    = walletId,
                Port        = port
            };

            // Encrypt blockchain results
            if (model.Encrypted)
            {
                // Create encryption key TODO: Don't store on disk?
                var key = RsaTools.CreateKeyAndSave(blockchain.ChainString + "-encrypt");
                blockchain.EncryptKey = RsaTools.KeyToString(key.Public);
            }

            // Save blockchain data in store
            await _blockchainStore.CreateBlockchain(blockchain);

            // Create RSA keypair for blind signing
            RsaTools.CreateKeyAndSave(blockchain.ChainString);

            return(Ok());
        }