コード例 #1
0
        public static async Task <string> Meacoin_Offline_Transaction(string privateKey, string _addressFrom, string _addressTo, int amount)
        {
            //get the function
            var function = meaToken.Contract.GetFunction("transfer");
            //get the data
            var data = function.GetData(new object[] { _addressTo, new BigInteger(amount) });
            //nonce
            var txCount = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(_addressFrom, BlockParameter.CreatePending());

            //signing the transaction
            var encoded = transactionSigner.SignTransaction(privateKey, meaToken.Contract.Address, 0, txCount.Value, 1000000000000L, 900000, data);
            //transaction hash
            var txId = await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + encoded);

            //verify transaction
            var status = transactionSigner.VerifyTransaction(encoded);

            Console.WriteLine("Status : " + status);
            //returning transaction hash
            return(txId);
        }
コード例 #2
0
    public IEnumerator SendRawTransaction()
    {
        //unpack first
        string json = File.ReadAllText(@"ks");

        KeyStoreService keystore = new KeyStoreService();

        byte[] privateKey;
        try
        {
            privateKey = keystore.DecryptKeyStoreFromJson("", json);  //live
        }
        catch (DecryptionException exc)
        {
            Debug.Log("password invalid");
            throw exc;
        }


        int nonce = 1; // GetPending transaction count + 1;

        TransactionSigner signer = new TransactionSigner();
        string            signedTransactionData = signer.SignTransaction(privateKey, m_addr,
                                                                         new BigInteger(0.002f * m_ether),
                                                                         new BigInteger(nonce),
                                                                         new BigInteger(20L * m_gwei),
                                                                         new BigInteger(21000));

        Assert.IsTrue(signer.VerifyTransaction(signedTransactionData));

        Debug.Log(signer.GetSenderAddress(signedTransactionData));

        EthSendRawTransactionUnityRequest req = new EthSendRawTransactionUnityRequest(m_endpoint);

        yield return(req.SendRequest(signedTransactionData));

        Debug.Log(req.Result);
    }
コード例 #3
0
        private async Task SendRawTransaction(TimestampDao timestamp, IWeb3 web3, string secretKey, double estimateGasPrice, EthSettings ethSettings)
        {
            if (!Enum.TryParse(ethSettings.Network, true, out Chain networkChain))
            {
                networkChain = Chain.MainNet;
                _logger.Warning($"Unable to parse '{ethSettings.Network}' to type '{typeof(Chain)}', so setting default to '{networkChain}'.");
            }

            bool proofVerified = _ethHelper.VerifyStamp(timestamp);

            if (!proofVerified)
            {
                var message = $"Unable to verify the signature '{timestamp.Signature}'.";
                _logger.Warning(message);
                throw new TimestampException(message);
            }

            string proofStr = JsonConvert.SerializeObject(
                new
            {
                file      = timestamp.FileName,
                hash      = timestamp.FileHash,
                publicKey = timestamp.PublicKey,
                signature = timestamp.Signature
            });

            var txData = HexStringUTF8ConvertorExtensions.ToHexUTF8(proofStr);

            var fromAddress = web3.TransactionManager.Account.Address;
            var futureNonce = await web3.TransactionManager.Account.NonceService.GetNextNonceAsync();

            _logger.Information($"Signed transaction on chain: {networkChain}, To: {ethSettings.ToAddress}, Nonce: {futureNonce}, GasPrice: {estimateGasPrice}, From Address :{fromAddress}");

            var offlineTransactionSigner = new TransactionSigner();
            var encoded = offlineTransactionSigner.SignTransaction(
                secretKey,
                networkChain,
                ethSettings.ToAddress,
                Web3.Convert.ToWei(0, UnitConversion.EthUnit.Gwei),
                futureNonce,
                Web3.Convert.ToWei(estimateGasPrice, UnitConversion.EthUnit.Gwei),
                new BigInteger(100000),
                txData);

            var verified = offlineTransactionSigner.VerifyTransaction(encoded);

            if (!verified)
            {
                var message = $"Unable to verify the transaction for data '{txData}'.";
                _logger.Error(message);
                throw new TimestampException(message);
            }

            try
            {
                var txId = await web3.Eth.Transactions.SendRawTransaction.SendRequestAsync("0x" + encoded);

                timestamp.Address       = fromAddress;
                timestamp.Nonce         = (long)futureNonce.Value;
                timestamp.TransactionId = txId;
                timestamp.Network       = networkChain.ToString();
                timestamp.BlockNumber   = -1;

                if (string.IsNullOrWhiteSpace(txId))
                {
                    timestamp.Status = TimestampState.Failed;
                    var message = $"Transaction failed for an user '{timestamp.UserId}' with file name '{timestamp.FileName}'.";
                    _logger.Error(message);
                }
            }
            catch (RpcResponseException ex)
            {
                await web3.TransactionManager.Account.NonceService.ResetNonce();

                if (ex.Message.Contains("nonce too low", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new RpcClientNonceException(ex.Message);
                }
                else if (ex.Message.Contains("transaction underpriced", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new RpcClientUnderpricedException(ex.Message);
                }

                throw;
            }
        }