private static async void TransGasForNep5(string address, byte[] prikey, byte[] pubkey, string targetAddr, decimal sendCount) { var assetnep5 = "0xa0b53d2efa8b1c4a62fcc1fcb54b7641510810c7"; var assetgas = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; string api = "https://api.nel.group/api/testnet"; Dictionary <string, List <Utxo> > dir = await GetBalanceByAddress(api, address); ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(assetnep5); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var nep5scripthash = new ThinNeo.Hash160(assetnep5); var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(nep5scripthash); Console.WriteLine("contract address=" + targetaddr); //往合约地址转账 //生成交易 tran = makeTran(dir[assetgas], targetaddr, new ThinNeo.Hash256(assetgas), 5); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = HttpHelper.MakeRpcUrlPost("http://127.0.0.1:20332", "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await HttpHelper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); if (txid.Length > 0) { Hash256 test = tran.GetHash(); } Console.WriteLine("txid=" + txid); } }
public async Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.test_wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); byte[] scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); subPrintLine("Get Total Supply for " + this.ID + ":"); var result = await nns_tools.api_InvokeScript(sc, "totalSupply"); subPrintLine("Total Supply : " + result.value.subItem[0].AsInteger()); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Nep55_1.api, address); if (dir.ContainsKey(Nep55_1.id_GAS) == false) { Console.WriteLine("no gas"); return; } //MakeTran ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + address); //from array.AddArrayValue("(str)" + "totalSupply"); //to array.AddArrayValue("(int)22"); //value sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)transfer")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(sc); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } tran = Helper.makeTran(dir[Nep55_1.id_GAS], address, new ThinNeo.Hash256(Nep55_1.id_GAS), 0); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata;//Nep55_1.api var url = Helper.MakeRpcUrlPost(nnc_1.api_local, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result2 = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result2); }
IEnumerator HTTP_nel_post_tan(byte[] script, MyJson.JsonNode_Array pararms, Action <bool, WWW> call_back) { WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(roleInfo.getInstance().address)); WWW www = new WWW(global.api, www_getuxo_form); yield return(www); Dictionary <string, List <Utxo> > _dir = get_utxo(www.text); if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); yield return(null); } ThinNeo.Transaction tran = null; { tran = Helper.makeTran(_dir[global.id_GAS], roleInfo.getInstance().address, new ThinNeo.Hash256(global.id_GAS), 0); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); WWWForm www_form_sendraw = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata)); WWW sendraw = new WWW(global.api, www_form_sendraw); yield return(sendraw); var json = MyJson.Parse(sendraw.text).AsDict(); if (json.ContainsKey("result")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); if (txid.Length > 0) { //Nep55_1.lastNep5Tran = tran.GetHash(); } Debug.Log("txid=" + txid); api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, txid, global.game_id.ToString(), "", "5", pararms.ToString(), global.netType, "0", (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); }); } else { Debug.Log("交易失败"); //testtool.showNotice("交易失败"); } }
async Task test_batchTransferNEO() { DateTime dt = DateTime.Now; Console.WriteLine("Start time:" + dt); byte[] postdata; string api = "http://api.alchemint.io/api/privatenet"; //查询交易,总数可能很多 var url = Helper.MakeRpcUrlPost(api, "getaddrs", out postdata, new JsonNode_ValueNumber(1000), new JsonNode_ValueNumber(1)); var result = await Helper.HttpPost(url, postdata); List <string> list = new List <string>(); MyJson.JsonNode_Object json = MyJson.Parse(result) as MyJson.JsonNode_Object; JsonNode_Array arrs = json["result"].AsList(); foreach (JsonNode_Object ob in arrs) { string to = ob["addr"].AsString(); Console.WriteLine("to:" + to); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, this.address); if (dir.ContainsKey(Config.id_NEO) == false) { Console.WriteLine("no neo"); return; } List <Utxo> newlist = dir[Config.id_NEO]; ThinNeo.Transaction tran = Helper.makeTran(newlist, to, new ThinNeo.Hash256(Config.id_NEO), 100); var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); string poststr = System.Text.Encoding.UTF8.GetString(postdata); result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); Thread.Sleep(40000); } DateTime end = DateTime.Now; Console.WriteLine("End time:" + end); //等待时间 }
async Task test_transferNEO() { Console.WriteLine("transfer to:"); var to = Console.ReadLine(); Console.WriteLine("send mount:"); var mount = Console.ReadLine(); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, this.address); if (dir.ContainsKey(Config.id_NEO) == false) { Console.WriteLine("no neo"); return; } List <Utxo> newlist = dir[Config.id_NEO]; ThinNeo.Transaction tran = Helper.makeTran(newlist, to, new ThinNeo.Hash256(Config.id_NEO), decimal.Parse(mount)); var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); string poststr = System.Text.Encoding.UTF8.GetString(postdata); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); }
private static void PubScDemo(string wif) { string assetid = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; string api = "https://api.nel.group/api/testnet"; byte[] prikey = ThinNeo.Helper_NEO.GetPrivateKeyFromWIF(wif); byte[] pubkey = ThinNeo.Helper_NEO.GetPublicKey_FromPrivateKey(prikey); string address = ThinNeo.Helper_NEO.GetAddress_FromPublicKey(pubkey); Dictionary <string, List <Utxo> > dir = GetBalanceByAddress(api, address); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes("TestCoin.avm"); //这里填你的合约所在地址 Console.WriteLine("合约脚本hash:" + ThinNeo.Helper_NEO.CalcHash160(script)); //合约 hash,也就是 assetId byte[] parameter__list = ThinNeo.Helper.HexString2Bytes("0710"); //合约入参类型 例:0610代表(string,[])参考:http://docs.neo.org/zh-cn/sc/Parameter.html byte[] return_type = ThinNeo.Helper.HexString2Bytes("05"); //合约返回值类型 05 代表 ByteArray int need_storage = 1; //是否需要使用存储 0false 1true int need_nep4 = 0; //是否需要动态调用 0false 2true int need_canCharge = 4; //是否支持收款 4true using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { //倒序插入数据 sb.EmitPushString("test"); //description sb.EmitPushString("*****@*****.**"); //email sb.EmitPushString("test"); //auther sb.EmitPushString("1.0"); //version sb.EmitPushString("ABC Coin"); //name sb.EmitPushNumber(need_storage | need_nep4 | need_canCharge); sb.EmitPushBytes(return_type); sb.EmitPushBytes(parameter__list); sb.EmitPushBytes(script); sb.EmitSysCall("Neo.Contract.Create"); string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); //用ivokescript试运行得到 gas 消耗 var result = HttpGet($"{api}?method=invokescript&id=1¶ms=[\"{scriptPublish}\"]"); var consume = (JObject.Parse(result)["result"] as JArray)[0]["gas_consumed"].ToString(); decimal gas_consumed = decimal.Parse(consume); ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = Math.Ceiling(gas_consumed - 10); //拼装交易体 ThinNeo.Transaction tran = MakeTran(dir, null, new ThinNeo.Hash256(assetid), extdata.gas); tran.version = 1; tran.extdata = extdata; tran.type = ThinNeo.TransactionType.InvocationTransaction; byte[] msg = tran.GetMessage(); byte[] signdata = ThinNeo.Helper_NEO.Sign(msg, prikey); tran.AddWitness(signdata, pubkey, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(data); string input = @"{ 'jsonrpc': '2.0', 'method': 'sendrawtransaction', 'params': ['#'], 'id': '1' }"; input = input.Replace("#", rawdata); result = HttpPost(api, input); Console.WriteLine(result.ToString()); } }
public async Task <JObject> sendTransaction(byte[] prikey, Hash160 schash, string method, string[] subparam) { // 构造合约交易 byte[] data = null; using (ScriptBuilder sb = new ScriptBuilder()) { MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); // byte[] randombytes = new byte[32]; using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) { rng.GetBytes(randombytes); } BigInteger randomNum = new BigInteger(randombytes); sb.EmitPushNumber(randomNum); sb.Emit(ThinNeo.VM.OpCode.DROP); for (var i = 0; i < subparam.Length; i++) { array.AddArrayValue(subparam[i]); } sb.EmitParamJson(array); sb.EmitPushString(method); sb.EmitAppCall(schash); data = sb.ToArray(); } // 获取余额 //string id_nnc = assetid; Dictionary <string, List <Utxo> > dir = getBalance(accountInfo.address); if (dir == null || dir[id_gas] == null) { // 余额不足 return(insufficientBalance()); } // 构造并签名 var tran = new ThinNeo.Transaction(); tran.type = ThinNeo.TransactionType.InvocationTransaction; tran.extdata = new ThinNeo.InvokeTransData { script = data, gas = 0 }; tran.inputs = new ThinNeo.TransactionInput[0]; tran.outputs = new ThinNeo.TransactionOutput[0]; tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = TransactionAttributeUsage.Script; tran.attributes[0].data = ThinNeo.Helper.GetPublicKeyHashFromAddress(accountInfo.address); byte[] signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, accountInfo.pubkey, accountInfo.address); string txid = tran.GetHash().ToString(); byte[] transdata = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(transdata); // 发送交易 try { byte[] postdata; string url = TransHelper.MakeRpcUrlPost(nelJsonRpcUrl, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); var result = await TransHelper.HttpPost(url, postdata); Console.WriteLine("result:" + result); if (JObject.Parse(result)["result"] == null) { return(txFail(txid)); } JObject res = (JObject)(((JArray)(JObject.Parse(result)["result"]))[0]); string flag = res["sendrawtransactionresult"].ToString(); if (flag.ToLower() != "true" && res["txid"].ToString() != txid) { return(txFail(txid)); } return(txSucc(txid)); } catch (Exception) { return(txFail(txid)); } }
private void on_getutxo(bool timeout, WWW www) { Debug.Log(www.text); MyJson.JsonNode_Object response = (MyJson.JsonNode_Object)MyJson.Parse(www.text); if (!response.AsDict().ContainsKey("result")) { Debug.Log("交易失败"); //testtool.showNotice("交易失败"); return; } MyJson.JsonNode_Array resJA = (MyJson.JsonNode_Array)response["result"]; Dictionary <string, List <Utxo> > _dir = new Dictionary <string, List <Utxo> >(); foreach (MyJson.JsonNode_Object j in resJA) { Utxo utxo = new Utxo(j["addr"].ToString(), new ThinNeo.Hash256(j["txid"].ToString()), j["asset"].ToString(), decimal.Parse(j["value"].ToString()), int.Parse(j["n"].ToString())); if (_dir.ContainsKey(j["asset"].ToString())) { _dir[j["asset"].ToString()].Add(utxo); } else { List <Utxo> l = new List <Utxo>(); l.Add(utxo); _dir[j["asset"].ToString()] = l; } } if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); } ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var nep5scripthash = new ThinNeo.Hash160(global.id_sgas); var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(nep5scripthash); Debug.Log("contract address=" + targetaddr);//往合约地址转账 //生成交易 tran = Helper.makeTran(_dir[global.id_GAS], targetaddr, new ThinNeo.Hash256(global.id_GAS), m_num); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); transaction_sgas(strtrandata); }
private void on_getutxo_1(bool timeout, WWW www) { Debug.Log(www.text); MyJson.JsonNode_Object response = (MyJson.JsonNode_Object)MyJson.Parse(www.text); if (!response.AsDict().ContainsKey("result")) { Debug.Log("交易失败"); return; } MyJson.JsonNode_Array resJA = (MyJson.JsonNode_Array)response["result"]; Dictionary <string, List <Utxo> > _dir = new Dictionary <string, List <Utxo> >(); foreach (MyJson.JsonNode_Object j in resJA) { Utxo utxo = new Utxo(j["addr"].ToString(), new ThinNeo.Hash256(j["txid"].ToString()), j["asset"].ToString(), decimal.Parse(j["value"].ToString()), int.Parse(j["n"].ToString())); if (_dir.ContainsKey(j["asset"].ToString())) { _dir[j["asset"].ToString()].Add(utxo); } else { List <Utxo> l = new List <Utxo>(); l.Add(utxo); _dir[j["asset"].ToString()] = l; } } if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); } ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + roleInfo.getInstance().address); //from array.AddArrayValue("(addr)" + global.cp_adress); //to array.AddArrayValue("(int)" + (m_num * 100000000)); //value sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)transfer")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 //拼接发给后台做验证的json字符串 MyJson.JsonNode_Object account = new MyJson.JsonNode_Object(); account["sbParamJson"] = array; account["sbPushString"] = new MyJson.JsonNode_ValueString("(str)transfer"); account["nnc"] = new MyJson.JsonNode_ValueString(global.id_sgas); script = sb.ToArray(); m_paparms = account.ToString(); Debug.Log(m_paparms); } tran = Helper.makeTran(_dir[global.id_GAS], roleInfo.getInstance().address, new ThinNeo.Hash256(global.id_GAS), 0); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); transaction_game_sgas(strtrandata); }
public async Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.test_wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); byte[] scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); Console.WriteLine("address=" + address); //得到注册器 var info_reg = await nns_tools.api_InvokeScript(Config.sc_nns, "getOwnerInfo", "(hex256)" + nns_tools.nameHash("sell").ToString()); var reg_sc = new Hash160(info_reg.value.subItem[0].subItem[1].data); Console.WriteLine("reg=" + reg_sc.ToString()); string addressto = ThinNeo.Helper.GetAddressFromScriptHash(reg_sc); Console.WriteLine("addressFrom=" + addressto); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Nep55_1.api, address); if (dir.ContainsKey(Nep55_1.id_GAS) == false) { Console.WriteLine("no gas"); return; } //MakeTran ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + address); //who array.AddArrayValue("(int)10000000000"); //value sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getmoneyback")); //参数倒序入 sb.EmitAppCall(reg_sc); script = sb.ToArray(); } tran = Helper.makeTran(dir[Nep55_1.id_GAS], address, new ThinNeo.Hash256(Nep55_1.id_GAS), 0); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); byte[] postdata; var url = Helper.MakeRpcUrlPost(Nep55_1.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); }
IEnumerator HTTP_nel_post_sgas_gas(decimal num) { var nep55_shash = new ThinNeo.Hash160(global.id_sgas); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash); WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(nep55_address)); WWW www = new WWW(global.api, www_getuxo_form); yield return(www); Dictionary <string, List <Utxo> > _dir = get_utxo(www.text); if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); } List <Utxo> newlist = new List <Utxo>(_dir[global.id_GAS]); for (var i = newlist.Count - 1; i >= 0; i--) { Debug.Log(newlist[i].txid.ToString()); byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + newlist[i].txid.ToString()); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getRefundTarget")); //参数倒序入 var shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } if (newlist[i].n > 0) { continue; } WWWForm www_getTarget_form = Helper.GetWWWFormPost("invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script))); WWW www_form = new WWW(global.api, www_getTarget_form); yield return(www_form); var jsonCU = MyJson.Parse(www_form.text); var stack = jsonCU.AsDict()["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict(); var value = stack["value"].AsString(); if (value.Length > 0)//已经标记的UTXO,不能使用 { newlist.RemoveAt(i); } } string paparms; ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + ThinNeo.Helper.Bytes2HexString(roleInfo.getInstance().scripthash)); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入 var shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); //拼接发给后台做验证的json字符串 MyJson.JsonNode_Object account = new MyJson.JsonNode_Object(); account["sbParamJson"] = array; account["sbPushString"] = new MyJson.JsonNode_ValueString("refund"); account["nnc"] = new MyJson.JsonNode_ValueString(global.id_sgas); account["cnts"] = new MyJson.JsonNode_ValueString(num.ToString()); paparms = account.ToString(); } //生成交易 tran = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(global.id_GAS), num); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; //附加鉴证 tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script; tran.attributes[0].data = roleInfo.getInstance().scripthash; } //sign and broadcast {//做智能合约的签名 byte[] n55contract = null; { WWWForm www_getState = Helper.GetWWWFormPost("getcontractstate", new MyJson.JsonNode_ValueString(global.id_sgas)); WWW www_state = new WWW(global.api, www_getState); yield return(www_state); var _json = MyJson.Parse(www_state.text).AsDict(); var _resultv = _json["result"].AsList()[0].AsDict(); n55contract = ThinNeo.Helper.HexString2Bytes(_resultv["script"].AsString()); } byte[] iscript = null; using (var sb = new ThinNeo.ScriptBuilder()) { sb.EmitPushString("whatever"); sb.EmitPushNumber(250); iscript = sb.ToArray(); } tran.AddWitnessScript(n55contract, iscript); } {//做提款人的签名 var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); WWWForm www_transaction = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata)); WWW www_tran = new WWW(global.api, www_transaction); yield return(www_tran); Debug.Log(www_tran.text); var json = MyJson.Parse(www_tran.text).AsDict(); ThinNeo.Hash256 lasttxid = null; if (json.ContainsKey("result")) { bool bSucc = false; if (json["result"].type == MyJson.jsontype.Value_Number) { bSucc = json["result"].AsBool(); Debug.Log("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; } if (bSucc) { lasttxid = tran.GetHash(); Debug.Log("你可以从这个UTXO拿走GAS了 txid=" + lasttxid.ToString() + "[0]"); api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, lasttxid.ToString(), "0", num.ToString(), "2", paparms, global.netType, "0", (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); testtool.panel_main.on_refresh_plat_NotifyList(); }); } else { lasttxid = null; } } }
IEnumerator HTTP_nel_post_gas_sgas(decimal num) { WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(roleInfo.getInstance().address)); WWW www = new WWW(global.api, www_getuxo_form); yield return(www); Dictionary <string, List <Utxo> > _dir = get_utxo(www.text); if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); } string paparms; ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)mintTokens")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(global.id_sgas); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); //拼接发给后台做验证的json字符串 MyJson.JsonNode_Object account = new MyJson.JsonNode_Object(); account["sbParamJson"] = array; account["sbPushString"] = new MyJson.JsonNode_ValueString("mintTokens"); account["nnc"] = new MyJson.JsonNode_ValueString(global.id_sgas); account["cnts"] = new MyJson.JsonNode_ValueString(num.ToString()); paparms = account.ToString(); Debug.Log(paparms); } var nep5scripthash = new ThinNeo.Hash160(global.id_sgas); var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(nep5scripthash); Debug.Log("contract address=" + targetaddr);//往合约地址转账 //生成交易 tran = Helper.makeTran(_dir[global.id_GAS], targetaddr, new ThinNeo.Hash256(global.id_GAS), num); tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); WWWForm www_form_sendraw = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata)); WWW sendraw = new WWW(global.api, www_form_sendraw); yield return(sendraw); var json = MyJson.Parse(sendraw.text).AsDict(); if (json.ContainsKey("result")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); if (txid.Length > 0) { Debug.Log("txid=" + txid); api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, txid, "0", num.ToString(), "1", paparms, global.netType, "0", (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); }); } else { Debug.Log("交易失败"); } } else { Debug.Log("交易失败"); } }
IEnumerator HTTP_nel_post_tan_gas(string toaddr, decimal num) { WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(roleInfo.getInstance().address)); WWW www = new WWW(global.api, www_getuxo_form); yield return(www); Dictionary <string, List <Utxo> > _dir = get_utxo(www.text); if (_dir.ContainsKey(global.id_GAS) == false) { Debug.Log("no gas"); yield return(null); } string paparms; //拼接发给后台做验证的json字符串 MyJson.JsonNode_Object account = new MyJson.JsonNode_Object(); account["sbPushString"] = new MyJson.JsonNode_ValueString("transfer"); account["cnts"] = new MyJson.JsonNode_ValueString(num.ToString()); paparms = account.ToString(); Debug.Log(paparms); ThinNeo.Transaction tran = null; { tran = Helper.makeTran(_dir[global.id_GAS], toaddr, new ThinNeo.Hash256(global.id_GAS), num); } //sign and broadcast var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey); tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address); var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); WWWForm www_form_sendraw = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata)); WWW sendraw = new WWW(global.api, www_form_sendraw); yield return(sendraw); var json = MyJson.Parse(sendraw.text).AsDict(); if (json.ContainsKey("result")) { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); if (txid.Length > 0) { //Nep55_1.lastNep5Tran = tran.GetHash(); } Debug.Log("txid=" + txid); api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, txid, "0", num.ToString(), "6", paparms, global.netType, "0", (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); }); } else { Debug.Log("交易失败"); //testtool.showNotice("交易失败"); } }
private void SendReturn(string txid, ReturnInfo ret) { //L2CmHCqgeNHL1i9XFhTLzUXsdr5LGjag4d56YY98FqEi4j5d83Mv var prikey = ThinNeo.Helper.GetPrivateKeyFromWIF("L1WHHa4zmudqzRTYQiF4wbw9duiqEvcz7QY93GG1rzvCVxFVSDud"); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var scripthash = ThinNeo.Helper.GetScriptHashFromPublicKey(pubkey); var addres = ThinNeo.Helper.GetAddressFromScriptHash(scripthash); ThinNeo.Transaction tx = new ThinNeo.Transaction(); tx.inputs = new ThinNeo.TransactionInput[0]; tx.outputs = new ThinNeo.TransactionOutput[0]; tx.type = ThinNeo.TransactionType.InvocationTransaction; tx.version = 1; //附加一个见证人 ///要他签名 tx.attributes = new ThinNeo.Attribute[1]; tx.attributes[0] = new ThinNeo.Attribute(); tx.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script; tx.attributes[0].data = scripthash; //拼接调用脚本 var invokedata = new ThinNeo.InvokeTransData(); tx.extdata = invokedata; invokedata.gas = 0; var sb = new ThinNeo.ScriptBuilder(); MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + txid); //txid array.AddArrayValue("(int)" + ret.returnvalue); //returnvalue var _params = new MyJson.JsonNode_Array(); array.Add(_params);//params Random r = new Random(); _params.AddArrayValue("(int)" + r.Next()); sb.EmitParamJson(array); sb.EmitPushString("returnvalue"); ThinNeo.Hash160 contractaddr = new ThinNeo.Hash160("0x24192c2a72e0ce8d069232f345aea4db032faf72"); sb.EmitAppCall(contractaddr); invokedata.script = sb.ToArray(); //签名 (谁来签名) var msg = tx.GetMessage(); var data = ThinNeo.Helper.Sign(msg, prikey); tx.AddWitness(data, pubkey, addres); System.Net.WebClient wc = new System.Net.WebClient(); wc.Headers["content-type"] = "text/plain;charset=UTF-8"; Newtonsoft.Json.Linq.JObject upparam = new Newtonsoft.Json.Linq.JObject(); upparam["jsonrpc"] = "2.0"; upparam["id"] = 1; upparam["method"] = "sendrawtransaction"; var _vparams = new Newtonsoft.Json.Linq.JArray(); var vdata = tx.GetRawData(); _vparams.Add(ThinNeo.Helper.Bytes2HexString(vdata)); upparam["params"] = _vparams; var strdata = upparam.ToString(Formatting.None); var info = wc.UploadString(url, strdata); //Console.WriteLine(info); var this_txid = tx.GetHash(); ret.txid = this_txid.ToString(); ret.rsp = info; this.list2.Items.Add(ret); }