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);
    }
Beispiel #2
0
        public async Task <IActionResult> Post(JsonRpcClientReq req, int chainid = 0)
        {
            if (chainid == 0)
            {
                return(BadRequest("chainid error"));
            }

            switch (req.Method)
            {
            case "eth_sendRawTransaction": {
                var signer = new TransactionSigner();

                var addr = signer.GetSenderAddress(req.Params[0].ToString());
                var rpc  = NodeChoose.ChooseServer(chainid, addr);

                if (string.IsNullOrEmpty(rpc))
                {
                    return(BadRequest("not find suitable rpc server"));
                }

                var sendResult = await SendRequest(req, rpc);

                if (sendResult.result)
                {
                    return(new ContentResult()
                        {
                            Content = sendResult.response,
                            ContentType = "application/json",
                            StatusCode = 200
                        });
                }
                return(BadRequest(sendResult.response));
            }

            default: {
                var cache = EthRpcCache.Instance.CheckCache(chainid, req);
                if (cache.hasCache)
                {
                    Logger.Debug($"{req} in cache");

                    //update resp id
                    var respObj = JObject.Parse(cache.result);
                    respObj["id"]    = req.Id;
                    respObj["cache"] = cache.strategy.ToString();

                    return(new ContentResult()
                        {
                            Content = respObj.ToString(Formatting.None),
                            ContentType = "application/json",
                            StatusCode = 200
                        });
                }

                var rpc = NodeChoose.ChooseServer(chainid);
                if (string.IsNullOrEmpty(rpc))
                {
                    return(BadRequest("not find suitable rpc server"));
                }

                var sendResult = await SendRequest(req, rpc);

                if (sendResult.result)
                {
                    var respObj = JObject.Parse(sendResult.response);

                    switch (cache.strategy)
                    {
                    case RpcCacheStrategy.NotCache:
                        break;

                    case RpcCacheStrategy.CacheInMemory:
                    case RpcCacheStrategy.CacheInDb:
                        var cacheResult = EthRpcCache.Instance.AddCache(chainid, req, sendResult.response);
                        respObj["cacheOption"] = cache.strategy.ToString();
                        respObj["cacheResult"] = cacheResult;
                        break;
                    }
                    return(new ContentResult()
                        {
                            Content = respObj.ToString(Formatting.None),
                            ContentType = "application/json",
                            StatusCode = 200
                        });
                }

                return(BadRequest(sendResult.response));
            }
            }
        }