Example #1
0
        public static Hash ExecuteTransaction(SpookSettings settings, NexusAPI api, byte[] script, ProofOfWork proofOfWork, params IKeyPair[] keys)
        {
            var tx = new Blockchain.Transaction(settings.Node.NexusName, DomainSettings.RootChainName, script, Timestamp.Now + TimeSpan.FromMinutes(5), Spook.TxIdentifier);

            if (proofOfWork != ProofOfWork.None)
            {
                logger.Message($"Mining proof of work with difficulty: {proofOfWork}...");
                tx.Mine(proofOfWork);
            }

            logger.Message("Signing message...");
            foreach (var keyPair in keys)
            {
                tx.Sign(keyPair);
            }

            var rawTx = tx.ToByteArray(true);

            var encodedRawTx = Base16.Encode(rawTx);

            try
            {
                api.Execute("sendRawTransaction", new[] { encodedRawTx });
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            Thread.Sleep(4000);
            var hash = tx.Hash.ToString();

            do
            {
                var     resultStr = api.Execute("getTransaction", new[] { hash });
                dynamic result    = JsonConvert.DeserializeObject <TransactionResult>(resultStr);

                if (result is TransactionResult)
                {
                    _transactionResults[hash] = (TransactionResult)result;
                }
                else
                if (result is ErrorResult)
                {
                    var temp = (ErrorResult)result;
                    if (temp.error.Contains("pending"))
                    {
                        Thread.Sleep(1000);
                        continue;
                    }
                    else
                    {
                        throw new CommandException(temp.error);
                    }
                }
                else
                {
                    throw new Exception("Something weird happened with transaction " + hash);
                }

                break;
            } while (true);
            logger.Success($"Sent transaction with hash {hash}!");

            return(Hash.Parse(hash));
        }
Example #2
0
        private static Hash ExecuteTransaction(NexusAPI api, byte[] script, ProofOfWork proofOfWork, params IKeyPair[] keys)
        {
            var tx = new Blockchain.Transaction(api.Nexus.Name, DomainSettings.RootChainName, script, Timestamp.Now + TimeSpan.FromMinutes(5), CLI.Identifier);

            if (proofOfWork != ProofOfWork.None)
            {
                logger.Message($"Mining proof of work with difficulty: {proofOfWork}...");
                tx.Mine(proofOfWork);
            }

            logger.Message("Signing message...");
            foreach (var keyPair in keys)
            {
                tx.Sign(keyPair);
            }

            var rawTx = tx.ToByteArray(true);

            try
            {
                api.SendRawTransaction(Base16.Encode(rawTx));
            }
            catch (Exception e)
            {
                throw new CommandException(e.Message);
            }

            Thread.Sleep(3000);
            var hash = tx.Hash.ToString();

            do
            {
                try
                {
                    var result = api.GetTransaction(hash);
                }
                catch (Exception e)
                {
                    throw new CommandException(e.Message);
                }

                /*if (result is ErrorResult)
                 * {
                 *  var temp = (ErrorResult)result;
                 *  if (temp.error.Contains("pending"))
                 *  {
                 *      Thread.Sleep(1000);
                 *  }
                 *  else
                 *  {
                 *      throw new CommandException(temp.error);
                 *  }
                 * }
                 * else*/
                {
                    break;
                }
            } while (true);
            logger.Success($"Sent transaction with hash {hash}!");

            return(Hash.Parse(hash));
        }