/// <summary>
        /// Tries to commit a transaction to the DAppChain.
        /// </summary>
        /// <param name="tx">Transaction to commit.</param>
        /// <returns>Commit metadata.</returns>
        /// <exception cref="InvalidTxNonceException">Thrown when transaction is rejected by the DAppChain due to a bad nonce.</exception>
        private async Task <BroadcastTxResult> TryCommitTxAsync(IMessage tx)
        {
            byte[] txBytes = tx.ToByteArray();
            if (this.TxMiddleware != null)
            {
                txBytes = await this.TxMiddleware.Handle(txBytes);
            }
            string payload = CryptoBytes.ToBase64String(txBytes);
            var    result  = await this.writeClient.SendAsync <BroadcastTxResult, string[]>("broadcast_tx_commit", new string[] { payload });

            if (result != null)
            {
                if (result.CheckTx.Code != 0)
                {
                    if ((result.CheckTx.Code == 1) && (result.CheckTx.Error == "sequence number does not match"))
                    {
                        throw new InvalidTxNonceException(result.CheckTx.Code, result.CheckTx.Error);
                    }
                    throw new TxCommitException(result.CheckTx.Code, result.CheckTx.Error);
                }
                if (result.DeliverTx.Code != 0)
                {
                    throw new TxCommitException(result.DeliverTx.Code, result.DeliverTx.Error);
                }
            }
            return(result);
        }
        /// <summary>
        /// Commits a transaction to the DAppChain.
        /// </summary>
        /// <param name="tx">Transaction to commit.</param>
        /// <param name="callDescription">Call high-level description.</param>
        /// <returns>Commit metadata.</returns>
        /// <exception cref="InvalidTxNonceException">Thrown when transaction is rejected by the DAppChain due to a bad nonce.</exception>
        internal async Task <BroadcastTxResult> CommitTxAsync(IMessage tx, CallDescription callDescription)
        {
            if (this.writeClient == null)
            {
                throw new InvalidOperationException("Write client was not set");
            }

            return(await this.CallExecutor.Call(
                       async() =>
            {
                await EnsureConnected();

                byte[] txBytes = tx.ToByteArray();
                if (this.TxMiddleware != null)
                {
                    txBytes = await this.TxMiddleware.Handle(txBytes);
                }

                try
                {
                    string payload = CryptoBytes.ToBase64String(txBytes);
                    var result = await this.writeClient.SendAsync <BroadcastTxResult, string[]>("broadcast_tx_commit", new[] { payload });
                    if (result == null)
                    {
                        return null;
                    }

                    CheckForTxError(result.CheckTx);
                    CheckForTxError(result.DeliverTx);

                    if (this.TxMiddleware != null)
                    {
                        this.TxMiddleware.HandleTxResult(result);
                    }

                    return result;
                }
                catch (LoomException e)
                {
                    if (this.TxMiddleware != null)
                    {
                        this.TxMiddleware.HandleTxException(e);
                    }

                    throw;
                }
            },
                       callDescription
                       ));
        }
예제 #3
0
        public void Should_Sign_Data_And_Verify_Signature()
        {
            var pk      = "A574ECE8F79DE11A39C6BADF0EF87C2C88730A5EA4CF2C0BD7E27103390BC4F4";
            var pubKey  = "2B0FF0CADE0D945A23D1AF7AF266A0BAB7E07B163756F5F16CC75F24A4EEF23B";
            var keyPair = KeyPair.CreateFromPrivateKey(pk);
            var message = "This is a test data";
            var data    = Encoding.UTF8.GetBytes(message);

            var signature = keyPair.Sign(data);
            var sigHex    = CryptoBytes.ToBase64String(signature);

            var publicAccount = PublicAccount.CreateFromPublicKey(pubKey, NetworkType.MIJIN_TEST);

            var isValid = publicAccount.VerifySignature(data, signature);

            isValid.Should().BeTrue();
        }
예제 #4
0
        /// <summary>
        /// Commits a transaction to the DAppChain.
        /// </summary>
        /// <param name="tx">Transaction to commit.</param>
        /// <returns>Commit metadata.</returns>
        /// <exception cref="InvalidTxNonceException">Thrown when transaction is rejected by the DAppChain due to a bad nonce.</exception>
        internal async Task <BroadcastTxResult> CommitTxAsync(IMessage tx)
        {
            if (this.writeClient == null)
            {
                throw new InvalidOperationException("Write client was not set");
            }

            return(await this.CallExecutor.Call(async() =>
            {
                await EnsureConnected();

                byte[] txBytes = tx.ToByteArray();
                if (this.TxMiddleware != null)
                {
                    txBytes = await this.TxMiddleware.Handle(txBytes);
                }

                string payload = CryptoBytes.ToBase64String(txBytes);
                var result = await this.writeClient.SendAsync <BroadcastTxResult, string[]>("broadcast_tx_commit", new[] { payload });
                if (result == null)
                {
                    return null;
                }

                if (result.CheckTx.Code != 0)
                {
                    if ((result.CheckTx.Code == 1) && (result.CheckTx.Error == "sequence number does not match"))
                    {
                        throw new InvalidTxNonceException(result.CheckTx.Code, result.CheckTx.Error);
                    }

                    throw new TxCommitException(result.CheckTx.Code, result.CheckTx.Error);
                }

                if (result.DeliverTx.Code != 0)
                {
                    throw new TxCommitException(result.DeliverTx.Code, result.DeliverTx.Error);
                }

                return result;
            }));
        }
예제 #5
0
        /// <summary>
        /// Commits a transaction to the DAppChain.
        /// </summary>
        /// <param name="tx">Transaction to commit.</param>
        /// <returns>Commit metadata.</returns>
        public async Task <BroadcastTxResult> CommitTxAsync(IMessage tx)
        {
            byte[] txBytes = tx.ToByteArray();
            if (this.TxMiddleware != null)
            {
                txBytes = await this.TxMiddleware.Handle(txBytes);
            }
            string payload = CryptoBytes.ToBase64String(txBytes);

            Logger.Log(LogTag, "Tx: " + payload);
            var req  = new TxJsonRpcRequest("broadcast_tx_commit", new string[] { payload }, Guid.NewGuid().ToString());
            var resp = await this.PostTxAsync(req);

            var result = resp.Result;

            if (result != null)
            {
                if (result.CheckTx.Code != 0)
                {
                    if (string.IsNullOrEmpty(result.CheckTx.Error))
                    {
                        throw new Exception(String.Format("Failed to commit Tx: {0}", result.CheckTx.Code));
                    }
                    throw new Exception(String.Format("Failed to commit Tx: {0}", result.CheckTx.Error));
                }
                if (result.DeliverTx.Code != 0)
                {
                    if (string.IsNullOrEmpty(result.DeliverTx.Error))
                    {
                        throw new Exception(String.Format("Failed to commit Tx: {0}", result.DeliverTx.Code));
                    }
                    throw new Exception(String.Format("Failed to commit Tx: {0}", result.DeliverTx.Error));
                }
            }
            return(result);
        }
예제 #6
0
 public void ToBase64StringNull()
 {
     Assert.AreEqual(null, CryptoBytes.ToBase64String(null));
 }
예제 #7
0
 public void ToBase64String()
 {
     Assert.AreEqual(Base64String, CryptoBytes.ToBase64String(_bytes));
 }
예제 #8
0
 public void ToBase64StringNull()
 {
     CryptoBytes.ToBase64String(null).Should().BeNull();
 }
예제 #9
0
 public void ToBase64String()
 {
     CryptoBytes.ToBase64String(_bytes).Should().Be(Base64String);
 }