public void makeRawTransaction(Action <bool, WWW> api_cb_cmd, MyJson.JsonNode_Array pararms) { if (pararms.Count > 2) { Debug.Log("不支持大于两部的操作"); return; } byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { Debug.Log(pararms.AsList()[0].ToString()); sb.EmitParamJson(pararms.AsList()[0].AsDict()["sbParamJson"]); //参数倒序入 sb.EmitPushString(pararms.AsList()[0].AsDict()["sbPushString"].ToString()); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(pararms.AsList()[0].AsDict()["nnc"].ToString()); sb.EmitAppCall(shash); //nep5脚本 if (pararms.Count > 1) { //这个方法是为了在同一笔交易中转账并充值 //当然你也可以分为两笔交易 //插入下述两条语句,能得到txid sb.EmitSysCall("System.ExecutionEngine.GetScriptContainer"); sb.EmitSysCall("Neo.Transaction.GetHash"); sb.EmitPushBytes(roleInfo.getInstance().scripthash); sb.EmitPushNumber(2); sb.Emit(ThinNeo.VM.OpCode.PACK); sb.EmitPushString(pararms.AsList()[1].AsDict()["sbPushString"].ToString()); //参数倒序入 ThinNeo.Hash160 shash_2 = new ThinNeo.Hash160(pararms.AsList()[1].AsDict()["nnc"].ToString()); sb.EmitAppCall(shash_2); //nep5脚本 } script = sb.ToArray(); } StartCoroutine(HTTP_nel_post_tan(script, pararms, api_cb_cmd)); }
public async Task publishContract() { string wif = "L1PSC3LRShi51xHAX2KN9oCFqETrZQhnzhKVu5zbrzdDpxF1LQz3"; byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(api, address); byte[] script = System.IO.File.ReadAllBytes("0xccd651a5e7d9f4dc698353970df7b7180139cbbe.avm"); Console.WriteLine("合约脚本:" + ThinNeo.Helper.Bytes2HexString(script)); Console.WriteLine("合约脚本Hash:" + ThinNeo.Helper.Bytes2HexString(ThinNeo.Helper.GetScriptHashFromScript(script).data.ToArray().Reverse().ToArray())); byte[] parameter__list = ThinNeo.Helper.HexString2Bytes("0710"); byte[] return_type = ThinNeo.Helper.HexString2Bytes("05"); int need_storage = 1; int need_nep4 = 0; int need_canCharge = 4; string name = "mygas"; string version = "1.0"; string auther = "LZ"; string email = "0"; string description = "0"; using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { var ss = need_storage | need_nep4 | need_canCharge; sb.EmitPushString(description); sb.EmitPushString(email); sb.EmitPushString(auther); sb.EmitPushString(version); sb.EmitPushString(name); sb.EmitPushNumber(ss); sb.EmitPushBytes(return_type); sb.EmitPushBytes(parameter__list); sb.EmitPushBytes(script); sb.EmitSysCall("Neo.Contract.Create"); string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); byte[] postdata; var url = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = await Helper.HttpPost(url, postdata); //return; var consume = (((MyJson.Parse(result) as MyJson.JsonNode_Object)["result"] as MyJson.JsonNode_Array)[0] as MyJson.JsonNode_Object)["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(id_GAS), extdata.gas); tran.version = 1; tran.extdata = extdata; tran.type = ThinNeo.TransactionType.InvocationTransaction; byte[] msg = tran.GetMessage(); byte[] signdata = ThinNeo.Helper.Sign(msg, prikey); tran.AddWitness(signdata, pubkey, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(data); url = Helper.MakeRpcUrlPost(api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); result = await Helper.HttpPost(url, postdata); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result); Console.WriteLine(resJO.ToString()); } }
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()); } }
private static void PubScDemo(string wif) { string assetid = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; string api = "https://api.nel.group/api/testnet"; byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); Dictionary <string, List <Utxo> > dir = GetBalanceByAddress(api, address); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes("dapp_nnc4.0.avm"); //这里填你的合约所在地址 Console.WriteLine("合约脚本:" + ThinNeo.Helper.Bytes2HexString(script)); Console.WriteLine("合约脚本hash:" + ThinNeo.Helper.Bytes2HexString(ThinNeo.Helper.GetScriptHashFromScript(script).data.ToArray().Reverse().ToArray())); byte[] parameter__list = ThinNeo.Helper.HexString2Bytes("0710"); //这里填合约入参 例:0610代表(string,[]) byte[] return_type = ThinNeo.Helper.HexString2Bytes("05"); //这里填合约的出参 int need_storage = 1; int need_nep4 = 2; int need_canCharge = 4; string name = "XC"; string version = "1.0"; string auther = "Zhang"; string email = "0"; string description = "0"; using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { var ss = need_storage; //倒叙插入数据 sb.EmitPushString(description); sb.EmitPushString(email); sb.EmitPushString(auther); sb.EmitPushString(version); sb.EmitPushString(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试运行并得到消耗 byte[] postdata; var url = MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = HttpPost(url, postdata); //string result = http.Post(api, "invokescript", new MyJson.JsonNode_Array() { new MyJson.JsonNode_ValueString(scriptPublish) },Encoding.UTF8); var consume = (((MyJson.Parse(result) as MyJson.JsonNode_Object)["result"] as MyJson.JsonNode_Array)[0] as MyJson.JsonNode_Object)["gas_consumed"].ToString(); decimal gas_consumed = decimal.Parse(consume); ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); //Console.WriteLine(ThinNeo.Helper.Bytes2HexString(extdata.script)); 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.Sign(msg, prikey); tran.AddWitness(signdata, pubkey, address); string txid = tran.GetHash().ToString(); byte[] data = tran.GetRawData(); string rawdata = ThinNeo.Helper.Bytes2HexString(data); //Console.WriteLine("scripthash:"+scripthash); url = MakeRpcUrlPost(api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); result = HttpPost(url, postdata); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result); Console.WriteLine(resJO.ToString()); } ////byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(wif); //byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); //string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); //Dictionary<string, List<UTXO>> dic_UTXO = new Dictionary<string, List<UTXO>>(); ////读取合约avm文件 //byte[] script = System.IO.File.ReadAllBytes("NeoContracts.avm"); //Console.WriteLine("合约脚本:" + ThinNeo.Helper.Bytes2HexString(script)); //Console.WriteLine("合约脚本哈希:" + ThinNeo.Helper.Bytes2HexString(ThinNeo.Helper.GetScriptHashFromScript(script) // .data.ToArray().Reverse().ToArray())); //byte[] parameter__list = ThinNeo.Helper.HexString2Bytes("0710");//合约入参 //byte[] return_type = ThinNeo.Helper.HexString2Bytes("05");//合约出参 //int need_storage = 1; ////int need_nep4 = 0; ////int need_canCharge = 4; //string name = "sgas"; //string version = "1.0"; //string auther = "NEL"; //string email = "0"; //string description = "0"; //using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) //{ // var ss = need_storage ; // sb.EmitPushString(description); // sb.EmitPushString(email); // sb.EmitPushString(auther); // sb.EmitPushString(version); // sb.EmitPushString(name); // sb.EmitPushNumber(need_storage ); // sb.EmitPushBytes(return_type); // sb.EmitPushBytes(parameter__list); // sb.EmitPushBytes(script); // sb.EmitSysCall("Neo.Contract.Create"); // string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); // byte[] postdata; // var method = "invokescript"; // var url = Helper.MakeRpcUrlPost(api, method, out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); // var result = await Helper.HttpPost(url, postdata); // var consume = // (((MyJson.Parse(result) as MyJson.JsonNode_Object)["result"] as MyJson.JsonNode_Array)[0] as // MyJson.JsonNode_Object)["gas_consumed"].ToString(); // decimal gas_consume = decimal.Parse(consume); // ThinNeo.InvokeTransData extdata = new InvokeTransData(); // extdata.script = sb.ToArray(); // extdata.gas = Math.Ceiling(gas_consume - 10); // ThinNeo.Transaction tran = MakeTran(dic_UTXO, 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.Sign(msg, prikey); // tran.AddWitness(signdata, pubkey, address); // string txid = tran.GetHash().ToString(); // byte[] data = tran.GetRawData(); // string rawdata = ThinNeo.Helper.Bytes2HexString(data); // url = Helper.MakeRpcUrlPost(api, "sendrawtransaction", out postdata, // new MyJson.JsonNode_ValueString(rawdata)); // result = await Helper.HttpPost(url, postdata); // MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object) MyJson.Parse(result); // Console.WriteLine(resJO.ToString()); //} }