Ejemplo n.º 1
0
 public SingleBlockchainResponse(RegiBlockchain blockchain, int blocks)
 {
     Name        = blockchain.Name;
     ExpiryDate  = blockchain.ExpiryDate;
     ChainString = blockchain.ChainString;
     Info        = blockchain.Info;
     Port        = blockchain.Port;
     Host        = blockchain.Host;
     WalletId    = blockchain.WalletId;
     Blocks      = blocks;
     EncryptKey  = blockchain.EncryptKey;
 }
Ejemplo n.º 2
0
        public RegiDbBlockchain(RegiBlockchain model)
        {
            if (model == null)
            {
                throw new ArgumentException(nameof(model));
            }

            Name        = model.Name;
            ExpiryDate  = model.ExpiryDate;
            ChainString = model.ChainString;
            Info        = model.Info;
            Port        = model.Port;
            WalletId    = model.WalletId;
            EncryptKey  = model.EncryptKey;
        }
Ejemplo n.º 3
0
        public async Task <RegiBlockchain> CreateBlockchain(RegiBlockchain model)
        {
            try
            {
                var dbModel = new RegiDbBlockchain(model);
                using (var connection = await GetConnectionAsync())
                {
                    await connection.ExecuteAsync(RegistrarQueries.BlockchainCreate, dbModel);

                    return(model);
                }
            }
            catch (Exception e)
            {
                if (e is RecordNotFoundException)
                {
                    throw;
                }
                throw new Exception("Could not create Blockchain");
            }
        }
Ejemplo n.º 4
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());
        }