private static void UpdateParams(CreateBlockchain model) { var p = MultiChainTools.GetBlockchainConfig(model.ChainString); p["anyone-can-connect"] = true; p["anyone-can-send"] = true; p["anyone-can-receive"] = true; p["anyone-can-mine"] = true; p["root-stream-open"] = false; // Target block time must be between 5 and 86400 seconds if ((model.BlockSpeed >= 5) && (model.BlockSpeed <= 86400)) { p["target-block-time"] = model.BlockSpeed; } MultiChainTools.WriteBlockchainConfig(model.ChainString, p); }
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()); }