public string RpcGenerateTransactionRawTx(CommandInfo ci) { JObject j = JObject.Parse(ci.Parameter); Transaction tr = _transactionManager.ConvertFromJson(j); string hex = tr.To.Value.ToHex(); Module m = null; if (!_loadedModules.TryGetValue(hex.Replace("0x", ""), out m)) { if (!_loadedModules.TryGetValue("0x" + hex.Replace("0x", ""), out m)) { ci.ErrorMsg.Add("Abi Not Loaded."); return(string.Empty); } } Method method = m.Methods?.FirstOrDefault(mt => mt.Name.Equals(tr.MethodName)); if (method == null) { ci.ErrorMsg.Add("Method not found."); return(string.Empty); } JArray p = j["params"] == null ? null : JArray.Parse(j["params"].ToString()); tr.Params = j["params"] == null ? null : method.SerializeParams(p.ToObject <string[]>()); tr.type = TransactionType.ContractTransaction; tr = tr.AddBlockReference(_rpcAddress); _transactionManager.SignTransaction(tr); var rawtx = _transactionManager.ConvertTransactionRawTx(tr); return(rawtx["rawtx"].ToString()); }
public Transaction SignTransaction(Transaction tx) { string addr = tx.From.Value.ToHex(); ECKeyPair kp = _keyStore.GetAccountKeyPair(addr); if (kp == null) { Console.WriteLine("The following account is locked:" + addr); return(null); } MemoryStream ms = new MemoryStream(); Serializer.Serialize(ms, tx); byte[] b = ms.ToArray(); byte[] toSig = SHA256.Create().ComputeHash(b); // Sign the hash ECSigner signer = new ECSigner(); ECSignature signature = signer.Sign(kp, toSig); // Update the signature tx.R = signature.R; tx.S = signature.S; tx.P = kp.PublicKey.Q.GetEncoded(); return(tx); }
public JObject ConvertTransactionRawTx(Transaction tx) { MemoryStream ms = new MemoryStream(); Serializer.Serialize(ms, tx); byte[] b = ms.ToArray(); string payload = b.ToHex(); var reqParams = new JObject { ["rawtx"] = payload }; return(reqParams); }
public static Transaction AddBlockReference(this Transaction transaction, string rpcAddress) { var height = _cachedHeight; var hash = _cachedHash; if (height == default(ulong) || (DateTime.Now - _refBlockTime).TotalSeconds > 60) { height = ulong.Parse(GetBlkHeight(rpcAddress)); hash = GetBlkHash(rpcAddress, height.ToString()); _cachedHeight = height; _cachedHash = hash; _refBlockTime = DateTime.Now; } transaction.RefBlockNumber = height; transaction.RefBlockPrefix = ByteArrayHelpers.FromHexString(hash).Where((b, i) => i < 4).ToArray(); return(transaction); }
public Transaction ConvertFromJson(JObject j) { try { Transaction tr = new Transaction(); tr.From = ByteArrayHelpers.FromHexString(j["from"].ToString()); tr.To = ByteArrayHelpers.FromHexString(j["to"].ToString()); tr.IncrementId = j["incr"].ToObject <ulong>(); tr.MethodName = j["method"].ToObject <string>(); return(tr); } catch (Exception e) { Console.WriteLine("Invalid transaction data."); Console.WriteLine("Exception message: " + e.Message); return(null); } }
public Transaction CreateTransaction(string elementAt, string genesisAddress, string incrementid, string methodName, byte[] serializedParams, TransactionType contracttransaction) { try { Transaction t = new Transaction(); t.From = ByteArrayHelpers.FromHexString(elementAt); t.To = ByteArrayHelpers.FromHexString(genesisAddress); t.IncrementId = Convert.ToUInt64(incrementid); t.MethodName = methodName; t.Params = serializedParams; t.type = contracttransaction; _cmdInfo.Result = true; return(t); } catch (Exception e) { _cmdInfo.ErrorMsg.Add("Invalid transaction data: " + e.Message); return(null); } }
public void RpcBroadcastTx(CommandInfo ci) { if (!ci.Parameter.Contains("{")) { RpcBroadcastWithRawTx(ci); return; } JObject j = JObject.Parse(ci.Parameter); Transaction tr = _transactionManager.ConvertFromJson(j); if (tr == null) { return; } string hex = tr.To.Value.ToHex(); Module m = null; if (!_loadedModules.TryGetValue(hex.Replace("0x", ""), out m)) { if (!_loadedModules.TryGetValue("0x" + hex.Replace("0x", ""), out m)) { ci.ErrorMsg.Add("Abi Not Loaded."); return; } } Method method = m.Methods?.FirstOrDefault(mt => mt.Name.Equals(tr.MethodName)); if (method == null) { ci.ErrorMsg.Add("Method not found."); return; } JArray p = j["params"] == null ? null : JArray.Parse(j["params"].ToString()); tr.Params = j["params"] == null ? null : method.SerializeParams(p.ToObject <string[]>()); tr.type = TransactionType.ContractTransaction; tr = tr.AddBlockReference(_rpcAddress); _transactionManager.SignTransaction(tr); var rawtx = _transactionManager.ConvertTransactionRawTx(tr); var req = RpcRequestManager.CreateRequest(rawtx, ci.Category, 1); string returnCode = string.Empty; long timeSpan = 0; string resp = _requestManager.PostRequest(req.ToString(), out returnCode, out timeSpan); ci.TimeSpan = timeSpan; if (!CheckResponse(ci, returnCode, resp)) { return; } JObject rObj = JObject.Parse(resp); var rj = rObj["result"]; string hash = rj["hash"] == null ? rj["error"].ToString() :rj["hash"].ToString(); string res = rj["hash"] == null ? "error" : "txId"; var jobj = new JObject { [res] = hash }; ci.InfoMsg.Add(jobj.ToString()); ci.Result = true; }
public void RpcDeployContract(CommandInfo ci) { if (!ci.CheckParameterValid(3)) { return; } string filename = ci.Parameter.Split(" ")[0]; // Read sc bytes SmartContractReader screader = new SmartContractReader(); byte[] sc = screader.Read(filename); string hex = sc.ToHex(); if (!_loadedModules.TryGetValue(_genesisAddress, out var m)) { ci.ErrorMsg.Add("ABI not loaded."); return; } Method meth = m.Methods.FirstOrDefault(mt => mt.Name.Equals("DeploySmartContract")); if (meth == null) { ci.ErrorMsg.Add("Method not Found."); return; } byte[] serializedParams = meth.SerializeParams(new List <string> { "1", hex }); _transactionManager.SetCmdInfo(ci); Transaction tx = _transactionManager.CreateTransaction(ci.Parameter.Split(" ")[2], _genesisAddress, ci.Parameter.Split(" ")[1], "DeploySmartContract", serializedParams, TransactionType.ContractTransaction); tx = tx.AddBlockReference(_rpcAddress); if (tx == null) { return; } tx = _transactionManager.SignTransaction(tx); if (tx == null) { return; } var rawtx = _transactionManager.ConvertTransactionRawTx(tx); var req = RpcRequestManager.CreateRequest(rawtx, "broadcast_tx", 1); string returnCode = string.Empty; long timeSpan = 0; string resp = _requestManager.PostRequest(req.ToString(), out returnCode, out timeSpan); ci.TimeSpan = timeSpan; if (!CheckResponse(ci, returnCode, resp)) { return; } JObject jObj = JObject.Parse(resp); var j = jObj["result"]; if (j["error"] != null) { ci.ErrorMsg.Add(j["error"].ToString()); ci.Result = false; return; } string hash = j["hash"] == null ? j["error"].ToString() :j["hash"].ToString(); string res = j["hash"] == null ? "error" : "txId"; var jobj = new JObject { [res] = hash }; ci.InfoMsg.Add(jobj.ToString()); ci.Result = true; }