static async void invokescript(string avmPath, string rpcurl) { //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes(avmPath); //这里填你的合约所在地址 byte[] parameter__list = new byte[0]; using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { //倒叙插入数据 //sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)totalSupply")); var nnc = ThinNeo.Helper.GetScriptHashFromScript(script); var scriptaddress = new ThinNeo.Hash160(nnc); sb.EmitAppCall(scriptaddress); //sb.EmitPushBytes(script); //ThinNeo.Hash160 shash = new ThinNeo.Hash160(scripthash.Replace("0x", "")); //sb.EmitAppCall(script);//nep5脚本 string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); //用ivokescript试运行并得到消耗 byte[] postdata; var url = Helper.MakeRpcUrlPost(rpcurl, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict()["result"].AsDict(); printByteArray(json); } }
private void Button_Click_2(object sender, RoutedEventArgs e) { int need_storage = iStorage.IsChecked == true ? 1 : 0; int need_nep4 = iDyncall.IsChecked == true ? 2 : 0; int can_charge = iCharge.IsChecked == true ? 4 : 0; ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder(); //倒叙插入数据 var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + this.asmBinText.Text); array.AddArrayValue("(bytes)" + iPList.Text); array.AddArrayValue("(bytes)" + iRType.Text); array.AddArrayValue("(int)" + (need_storage | need_nep4 | can_charge)); array.AddArrayValue("(str)" + iName.Text); //name array.AddArrayValue("(str)" + iVersion.Text); //version array.AddArrayValue("(str)" + iAuthor.Text); //author array.AddArrayValue("(str)" + iEmail.Text); //email array.AddArrayValue("(str)" + iDescription.Text); //desc sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)upgrade")); //参数倒序入 var shash = new ThinNeo.Hash160(iOldScHash.Text);; sb.EmitAppCall(shash); this.script = sb.ToArray(); this.DialogResult = true; }
//async public static Task<MyJson.JsonNode_Object> callcontractfortest(string url,string method) //{ // var gstr = MakeRpcUrlPost("getfullloginfo", txid); // try // { // var str = await postData(url, gstr); // var json = MyJson.Parse(str); // var result = json.AsDict().GetDictItem("result") as MyJson.JsonNode_Object; // return result; // } // catch (Exception err) // { // var errorMsg = "failed to call contract for test.Info:" + err.ToString(); // Console.WriteLine(errorMsg); // Log.WriteLog(errorMsg); // return null; // } //} async public static Task <MyJson.JsonNode_Object> invokescript(string url, string scripthash, string method) { var sb = new ThinNeo.ScriptBuilder(); ThinNeo.Hash160 shash = new ThinNeo.Hash160(scripthash); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)" + method)); //调用的方法 sb.EmitAppCall(shash); //nep5脚本 var data = sb.ToArray(); var script = ThinNeo.Helper.Bytes2HexString(data); var urldata = MakeRpcUrlPost("invokescript", script); try { var str = await postData(url, urldata); var json = MyJson.Parse(str); if (json.AsDict().ContainsKey("error")) { return(json.AsDict()["error"] as MyJson.JsonNode_Object); } var result = json.AsDict().GetDictItem("result") as MyJson.JsonNode_Object; return(result); } catch (Exception err) { var errorMsg = "failed to invokescript method:" + method + ".Info:" + err.ToString(); Console.WriteLine(errorMsg); return(null); } }
void threadCheckTx() { while (true) { foreach (var item in mapTxState) { var txid = item.Key; var sb = new ThinNeo.ScriptBuilder(); MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + txid); //array.AddArrayValue(new MyJson.JsonNode_Array()); sb.EmitParamJson(array); sb.EmitPushString("getcallstate"); ThinNeo.Hash160 contractaddr = new ThinNeo.Hash160("0x24192c2a72e0ce8d069232f345aea4db032faf72"); sb.EmitAppCall(contractaddr); var script = sb.ToArray(); var result = CallScript(script); if (!string.IsNullOrEmpty(result[0]["value"].ToString())) { item.Value.state = (int)result[0]["value"][0]["value"]; } } System.Threading.Thread.Sleep(1000); } }
public JArray getnep5decimals(string nep5Hash) { //Console.WriteLine("assetid:" + nep5Hash); string script = null; using (var sb = new ThinNeo.ScriptBuilder()) { ThinNeo.Hash160 shash = new ThinNeo.Hash160(nep5Hash); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)decimals")); sb.EmitAppCall(shash); var data = sb.ToArray(); script = ThinNeo.Helper.Bytes2HexString(data); } var res = Rpc.invokescript(this.neoCliJsonRPCUrl, script).Result; var arr = res.GetDictItem("stack").AsList().ToArray(); //Console.WriteLine("rpc info:"+ arr[0].ToString()); var decimalString = arr[0].AsDict()["value"].AsString(); int decimals = int.Parse(decimalString); return(new JArray(new JObject() { { "value", decimals } })); }
private void Button_Click(object sender, RoutedEventArgs e) { var prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(""); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var scripthash = ThinNeo.Helper.GetScriptHashFromPublicKey(pubkey); var addres = ThinNeo.Helper.GetAddressFromScriptHash(scripthash); //AbN2K2trYzgx8WMg2H7U7JHH6RQVzz2fnx var vscripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress("AdsNmzKPPG7HfmQpacZ4ixbv9XJHJs2ACz"); 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("(hex160)" + scripthash.ToString()); //witnesscall array.AddArrayValue("(hex160)" + vscripthash.ToString()); //witnessreturn array.AddArrayValue("(str)" + vscripthash.ToString()); //callscript array.AddArrayValue("(str)" + methodCombox.Text); //callmethod var _params = new MyJson.JsonNode_Array(); _params.AddArrayValue(int.Parse(textbox1.Text)); _params.AddArrayValue(int.Parse(textbox2.Text)); array.Add(_params);//params _params.AddArrayValue("(int)" + r.Next()); sb.EmitParamJson(array); sb.EmitPushString("outcall"); 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); DoCallTran(tx); //CallScript(invokedata.script); }
static async void nep53(string strurl, string rpcurl, string avmPath, string wif, string toaddr, string asset) { //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes(avmPath); //这里填你的合约所在地址 var nnc = ThinNeo.Helper.GetScriptHashFromScript(script); 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(strurl, address); string targeraddr = address; //Transfer it to yourself. ThinNeo.Transaction tran = Helper.makeTran(dir[asset], targeraddr, new ThinNeo.Hash256(asset), decimal.Zero); tran.type = ThinNeo.TransactionType.InvocationTransaction; ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder(); var scriptaddress = new ThinNeo.Hash160(nnc); //Parameter inversion MyJson.JsonNode_Array JAParams = new MyJson.JsonNode_Array(); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + address)); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + toaddr)); JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + 1)); sb.EmitParamJson(JAParams); //Parameter list sb.EmitPushString("transfer"); //Method sb.EmitAppCall(scriptaddress); //Asset contract ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = 1; tran.extdata = extdata; 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); byte[] postdata; var url = Helper.MakeRpcUrlPost(rpcurl, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); var response = await Helper.HttpPost(url, postdata); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(response); if (resJO.ContainsKey("result")) { Console.WriteLine(resJO["result"].ToString()); } }
async public Task Demo() { string nnc = "0x3fccdb91c9bb66ef2446010796feb6ca4ed96b05"; byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF("L3tDHnEAvwnnPE4sY4oXpTvNtNhsVhbkY4gmEmWmWWf1ebJhVPVW"); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); string toaddr = "APwCdakS1NpJsiq6j9SfvkQFS9ubt347a2"; string id_GAS = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(api, address); string targeraddr = address; //Transfer it to yourself. ThinNeo.Transaction tran = Helper.makeTran(dir[id_GAS], targeraddr, new ThinNeo.Hash256(id_GAS), decimal.Zero); tran.type = ThinNeo.TransactionType.InvocationTransaction; ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder(); var scriptaddress = new ThinNeo.Hash160(nnc); //Parameter inversion MyJson.JsonNode_Array JAParams = new MyJson.JsonNode_Array(); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + address)); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + toaddr)); JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + 1)); sb.EmitParamJson(JAParams); //Parameter list sb.EmitPushString("transfer"); //Method sb.EmitAppCall(scriptaddress); //Asset contract ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = 1; tran.extdata = extdata; 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 scripthash = ThinNeo.Helper.Bytes2HexString(data); string response = await Helper.HttpGet(api + "?method=sendrawtransaction&id=1¶ms=[\"" + scripthash + "\"]"); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(response); Console.WriteLine(resJO["result"].ToString()); }
public JArray getAddressNep5Asset(string address, string nep5Hash) { string script = null; using (var sb = new ThinNeo.ScriptBuilder()) { ThinNeo.Hash160 shash = new ThinNeo.Hash160(nep5Hash); MyJson.JsonNode_Array JAParams = new MyJson.JsonNode_Array(); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + address)); sb.EmitParamJson(JAParams); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)balanceOf")); sb.EmitAppCall(shash); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)decimals")); sb.EmitAppCall(shash); var data = sb.ToArray(); script = ThinNeo.Helper.Bytes2HexString(data); } var res = Rpc.invokescript(this.neoCliJsonRPCUrl, script).Result; var arr = res.GetDictItem("stack").AsList().ToArray(); var valueString = arr[0].AsDict()["value"].AsString(); var decimalString = arr[1].AsDict()["value"].AsString(); int decimals = int.Parse(decimalString); //-----------精度 //string findStr = "{{assetid:'{0}'}}"; //findStr = string.Format(findStr, nep5Hash); //JArray utxoArr=mh.GetData(Block_mongodbConnStr, Block_mongodbDatabase, "NEP5asset", findStr); //int decimals=(int)((JObject)utxoArr[0])["decimals"]; decimal value = decimal.Parse(Number.getNumStrFromHexStr(valueString, decimals)); return(new JArray(new JObject() { { "value", value } })); }
async public Task Demo() { string nnc = "0x3fccdb91c9bb66ef2446010796feb6ca4ed96b05".Replace("0x", ""); string script = null; using (var sb = new ThinNeo.ScriptBuilder()) { sb.EmitParamJson(new MyJson.JsonNode_Array()); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)name")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(nnc); sb.EmitAppCall(shash); //nep5脚本 sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)symbol")); sb.EmitAppCall(shash); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)decimals")); sb.EmitAppCall(shash); sb.EmitParamJson(new MyJson.JsonNode_Array()); sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)totalSupply")); sb.EmitAppCall(shash); var data = sb.ToArray(); script = ThinNeo.Helper.Bytes2HexString(data); } //var url = Helper.MakeRpcUrl(api, "invokescript", new MyJson.JsonNode_ValueString(script)); //string result = await Helper.HttpGet(url); byte[] postdata; var url = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(script)); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); }
async public Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(testwif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); byte[] scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); Console.WriteLine("address=" + address); {//查balance string script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + ThinNeo.Helper.Bytes2HexString(scripthash)); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)balanceOf")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(nep55); sb.EmitAppCall(shash); //nep5脚本 var data = sb.ToArray(); script = ThinNeo.Helper.Bytes2HexString(data); } //var url = Helper.MakeRpcUrl(api, "invokescript", new MyJson.JsonNode_ValueString(script)); //string result = await Helper.HttpGet(url); byte[] postdata; var url = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(script)); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { var resultv = json["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict(); var rtype = resultv["type"].AsString(); var rvalue = resultv["value"].AsString(); Console.WriteLine("type=" + rtype + " value=" + rvalue); var n = new System.Numerics.BigInteger(ThinNeo.Helper.HexString2Bytes(rvalue)); Console.WriteLine("value dec=" + n.ToString()); } } if (lastNep5Tran != null) { string script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + lastNep5Tran.ToString()); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getTXInfo")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(nep55); sb.EmitAppCall(shash); //nep5脚本 var data = sb.ToArray(); script = ThinNeo.Helper.Bytes2HexString(data); } byte[] postdata; var url = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(script)); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); } }
async public Task Demo() { var lasthash = "0x0963614006fda9ebb3ecbc1a7bd604a393cfe66cf9234882abc860cdb4694970"; if (lasthash == null) { Console.WriteLine("你还没有正确执行N5 3"); return; } byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Nep55_1.testwif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); var scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); var nep55_shash = new ThinNeo.Hash160(Nep55_1.nep55); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash); Console.WriteLine("address=" + address); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Nep55_1.api, nep55_address); if (dir.ContainsKey(Nep55_1.id_GAS) == false) { Console.WriteLine("no gas"); return; } List <Utxo> newlist = new List <Utxo>(); foreach (var utxo in dir[Nep55_1.id_GAS]) { if (utxo.n == 0 && utxo.txid.ToString().Equals(lasthash)) { newlist.Add(utxo); } } if (newlist.Count == 0) { Console.WriteLine("找不到要使用的UTXO"); return; } {//检查utxo byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(hex256)" + newlist[0].txid.ToString()); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getRefundTarget")); //参数倒序入 var shash = new ThinNeo.Hash160(Nep55_1.nep55); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var urlCheckUTXO = Helper.MakeRpcUrl(Nep55_1.api, "invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script))); string resultCheckUTXO = await Helper.HttpGet(urlCheckUTXO); var jsonCU = MyJson.Parse(resultCheckUTXO); var stack = jsonCU.AsDict()["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict(); var value = stack["value"].AsString(); if (value.Length == 0)//未标记的UTXO,不能使用 { Console.WriteLine("这个utxo没有标记"); return; } var hash = new ThinNeo.Hash160(ThinNeo.Helper.HexString2Bytes(value)); //if (hash.ToString()!= scripthash.ToString()) //{ // Console.WriteLine("这个utxo不是标记给你用的"); // return; //} } ThinNeo.Transaction tran = Helper.makeTran(newlist, address, new ThinNeo.Hash256(Nep55_1.id_GAS), newlist[0].value); tran.type = ThinNeo.TransactionType.ContractTransaction; tran.version = 0; //sign and broadcast {//做智能合约的签名 byte[] n55contract = null; { var urlgetscript = Helper.MakeRpcUrl(Nep55_1.api, "getcontractstate", new MyJson.JsonNode_ValueString(Nep55_1.nep55)); var resultgetscript = await Helper.HttpGet(urlgetscript); var _json = MyJson.Parse(resultgetscript).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.EmitPushNumber(0); sb.EmitPushNumber(0); iscript = sb.ToArray(); } tran.AddWitnessScript(n55contract, iscript); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); byte[] postdata; var url = Helper.MakeRpcUrlPost(Nep55_1.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); //bug //sendraw api 有bug,所以先加这个 //url = "http://localhost:20332"; string poststr = System.Text.Encoding.UTF8.GetString(postdata); Console.WriteLine("-----post info begin----"); Console.WriteLine(poststr); Console.WriteLine("-----post info end----"); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { bool bSucc = false; if (json["result"].type == MyJson.jsontype.Value_Number) { bSucc = json["result"].AsBool(); Console.WriteLine("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; Console.WriteLine("txid=" + txid); } if (bSucc) { Nep55_1.lastNep5Tran = tran.GetHash(); Console.WriteLine("besucc txid=" + tran.GetHash().ToString()); } } }
async public Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Nep55_1.testwif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); byte[] scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); Console.WriteLine("address=" + address); //获取地址的资产列表 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; } 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(Nep55_1.nep55); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } var nep5scripthash = new ThinNeo.Hash160(Nep55_1.nep55); var targetaddr = ThinNeo.Helper.GetAddressFromScriptHash(nep5scripthash); Console.WriteLine("contract address=" + targetaddr);//往合约地址转账 //生成交易 tran = Helper.makeTran(dir[Nep55_1.id_GAS], targetaddr, new ThinNeo.Hash256(Nep55_1.id_GAS), 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 = Helper.MakeRpcUrlPost(Nep55_1.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); var result = await Helper.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) { Nep55_1.lastNep5Tran = tran.GetHash(); } Console.WriteLine("txid=" + txid); } }
async public Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Nep55_1.testwif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); var scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); var nep55_shash = new ThinNeo.Hash160(Nep55_1.nep55); string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash); Console.WriteLine("address=" + address); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Nep55_1.api, nep55_address); if (dir.ContainsKey(Nep55_1.id_GAS) == false) { Console.WriteLine("no gas"); return; } List <Utxo> newlist = new List <Utxo>(dir[Nep55_1.id_GAS]); for (var i = newlist.Count - 1; i >= 0; i--) { 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(Nep55_1.nep55); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } if (newlist[i].n > 0) { continue; } var urlCheckUTXO = Helper.MakeRpcUrl(Nep55_1.api, "invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script))); string resultCheckUTXO = await Helper.HttpGet(urlCheckUTXO); var jsonCU = MyJson.Parse(resultCheckUTXO); 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); } } 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(scripthash)); sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入 var shash = new ThinNeo.Hash160(Nep55_1.nep55); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } Console.WriteLine("contract address=" + nep55_address);//往合约地址转账 //生成交易 tran = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(Nep55_1.id_GAS), (decimal)2.1); 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 = scripthash; } //sign and broadcast {//做智能合约的签名 byte[] n55contract = null; { var urlgetscript = Helper.MakeRpcUrl(Nep55_1.api, "getcontractstate", new MyJson.JsonNode_ValueString(Nep55_1.nep55)); var resultgetscript = await Helper.HttpGet(urlgetscript); var _json = MyJson.Parse(resultgetscript).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(), prikey); tran.AddWitness(signdata, pubkey, address); } var trandata = tran.GetRawData(); var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata); ThinNeo.Transaction testde = new ThinNeo.Transaction(); testde.Deserialize(new System.IO.MemoryStream(trandata)); byte[] postdata; var url = Helper.MakeRpcUrlPost(Nep55_1.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata)); //bug //sendraw api 有bug,所以先加这个 //url = "http://localhost:20332"; string poststr = System.Text.Encoding.UTF8.GetString(postdata); Console.WriteLine("-----post info begin----"); Console.WriteLine(poststr); Console.WriteLine("-----post info end----"); var result = await Helper.HttpPost(url, postdata); Console.WriteLine("得到的结果是:" + result); var json = MyJson.Parse(result).AsDict(); if (json.ContainsKey("result")) { bool bSucc = false; if (json["result"].type == MyJson.jsontype.Value_Number) { bSucc = json["result"].AsBool(); Console.WriteLine("cli=" + json["result"].ToString()); } else { var resultv = json["result"].AsList()[0].AsDict(); var txid = resultv["txid"].AsString(); bSucc = txid.Length > 0; Console.WriteLine("txid=" + txid); } if (bSucc) { lasttxid = tran.GetHash(); Nep55_1.lastNep5Tran = tran.GetHash(); Console.WriteLine("你可以从这个UTXO拿走GAS了 txid=" + lasttxid.ToString() + "[0]"); } else { lasttxid = null; } } }
async public Task <string> Operation(string script, string prikeyStr, string method, List <string> args) { // string script = "0x342c8b1242c195929b109079da947b1e973fe2be"; // string script = "0x96babb04e415a402b973350df71e10d24c725f78"; // string script = "0x55b300f8468d9e2bb9c3a4b8ff870c8c3b6ddf7c";//测试网 // byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(prikeyStr); // byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF("L3tDHnEAvwnnPE4sY4oXpTvNtNhsVhbkY4gmEmWmWWf1ebJhVPVW");//测试网 byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(prikeyStr); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); // string toaddr = "APwCdakS1NpJsiq6j9SfvkQFS9ubt347a2"; string id_GAS = "0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"; // Console.WriteLine("prikey:" + prikeyStr); //获取地址的资产列表 Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(api, address); string targeraddr = address; //Transfer it to yourself. ThinNeo.Transaction tran = Helper.makeTran(dir[id_GAS], targeraddr, new ThinNeo.Hash256(id_GAS), decimal.Zero); tran.type = ThinNeo.TransactionType.InvocationTransaction; ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder(); var scriptaddress = new ThinNeo.Hash160(script); //Parameter inversion MyJson.JsonNode_Array JAParams = new MyJson.JsonNode_Array(); var uuidN = ""; //加入循环记录参数 if ("saveValidateInfo".Equals(method) || "savePayInfo".Equals(method)) { uuidN = Guid.NewGuid().ToString("N"); JAParams.Add(new MyJson.JsonNode_ValueString("(str)" + uuidN)); JAParams.Add(new MyJson.JsonNode_ValueString("(str)" + args[0])); } else if ("saveResult".Equals(method)) { uuidN = args[0]; JAParams.Add(new MyJson.JsonNode_ValueString("(str)" + uuidN)); JAParams.Add(new MyJson.JsonNode_ValueString("(str)" + args[1])); } else if ("transfer".Equals(method)) { JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + args[0])); JAParams.Add(new MyJson.JsonNode_ValueString("(address)" + args[1])); JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + args[2])); } else if ("allotTKY".Equals(method)) { uuidN = Guid.NewGuid().ToString("N"); JAParams.Add(new MyJson.JsonNode_ValueString("(str)" + uuidN)); JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + args[0])); JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + args[1])); } else { } sb.EmitParamJson(JAParams);//Parameter list if ("saveValidateInfo".Equals(method)) { sb.EmitPushString("save"); //Method } sb.EmitPushString(method); //Method sb.EmitAppCall(scriptaddress); //Asset contract //转账 //加入循环记录参数 //JAParams.Add(new MyJson.JsonNode_ValueString("(addr)AeP5KqSJxPEpWCGRfoDiCr3Xtg6jJbRbbH")); //JAParams.Add(new MyJson.JsonNode_ValueString("(addr)AMZiiAnBg5uFLXGVMSt4hPEA2ubRCiNZg5")); //JAParams.Add(new MyJson.JsonNode_ValueString("(integer)9")); //// JAParams.Add(new MyJson.JsonNode_ValueString("(integer)" + 1)); //sb.EmitParamJson(JAParams);//Parameter list ////方法名 //sb.EmitPushString("transfer");//Method ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = 1; tran.extdata = extdata; 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 scripthash = ThinNeo.Helper.Bytes2HexString(data); //String url2 = "http://47.96.168.8:20332";//测试网 String url2 = "http://192.168.70.25:10332"; // Console.WriteLine("txid:" + txid); // Console.WriteLine("签名:"+scripthash); // return scripthash; string response = await Helper.HttpGet(url2 + "?method=sendrawtransaction&id=1¶ms=[\"" + scripthash + "\"]"); MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(response); // Console.WriteLine(resJO["result"].ToString()); string resultStr = ""; if ("saveValidateInfo".Equals(method) || "savePayInfo".Equals(method)) { resultStr = "sendrawtransactionResult:" + resJO["result"].ToString() + ",chainKey:" + uuidN + ",txid:" + txid;; } else if ("saveResult".Equals(method)) { resultStr = "sendrawtransactionResult:" + resJO["result"].ToString() + ",chainKey:" + args[0] + "rs,txid:" + txid; } else if ("transfer".Equals(method)) { resultStr = "sendrawtransactionResult:" + resJO["result"].ToString(); } else if ("allotTKY".Equals(method)) { resultStr = "sendrawtransactionResult:" + resJO["result"].ToString() + ",chainKey:" + uuidN + ",txid:" + txid; } else { } // Console.WriteLine(resultStr); return(resultStr); }
public async Task <byte[]> MakeScript(bool sign) { var payaddr = label_addr.Text; var paygas = 0.001; var utxos = await Demo.Helper.GetUtxosToPay(api, payaddr, id_GAS, paygas); if (utxos.Count == 0) { MessageBox.Show("you do not have gas for sendraw."); return(null); } var asset = listAsset.SelectedItem as Asset; if (asset == null || asset.isnep5 == false) { MessageBox.Show("this is not a nep5"); return(null); } var targetaddr = txt_get_addr.Text; var targetbalance = double.Parse(txt_get_balance.Text); for (var i = 0; i < asset.decimals; i++) { targetbalance *= 10; } if (sign) { var paywif = txt_wifGetter.Text; var prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(paywif); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); if (address != payaddr) { MessageBox.Show("error wif for pubkey in lock contract."); return(null); } } //MakeTran ThinNeo.Transaction tran = null; { byte[] script = null; using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + address_LockContract); //from array.AddArrayValue("(addr)" + targetaddr); //to array.AddArrayValue("(int)" + (ulong)targetbalance); //value sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)transfer")); //参数倒序入 ThinNeo.Hash160 shash = new ThinNeo.Hash160(asset.assetID); sb.EmitAppCall(shash); //nep5脚本 script = sb.ToArray(); } tran = Demo.Helper.makeTran(null, payaddr, new ThinNeo.Hash256(id_GAS), 0, (decimal)0.001, utxos, payaddr); 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 = scripthash_LockContract; } if (sign) { var paywif = txt_wifGetter.Text; var prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(paywif); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); ////sign var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey); tran.AddWitness(signdata, pubkey, address); var sb2 = new ThinNeo.ScriptBuilder(); sb2.EmitPushBytes(signdata); var iscript = sb2.ToArray(); tran.AddWitnessScript(script_LockContract, iscript); return(tran.GetRawData()); } else { return(tran.GetMessage()); } }
/// <summary> /// gen nep5 by 印玮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Button_Click(object sender, RoutedEventArgs e) { try { int gas = int.Parse(this.textConsume.Text); var tran = new ThinNeo.Transaction(); tran.type = ThinNeo.TransactionType.InvocationTransaction; if (gas > 0)//0 or 1 { tran.version = 1; } else { tran.version = 0; } var json = MyJson.Parse(this.textParams.Text).AsList(); using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { var list = json.AsList(); for (int i = list.Count - 1; i >= 0; i--) { sb.EmitParamJson(list[i]); } var schash = new ThinNeo.Hash160(this.textContractHash.Text); sb.EmitAppCall(schash); var extdata = new ThinNeo.InvokeTransData(); tran.extdata = extdata; extdata.script = sb.ToArray(); extdata.gas = gas; if (gas > 0) { Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(url, this.textAddr.Text); tran = Helper.makeTran(tran, dir["0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"], null, new ThinNeo.Hash256("0x602c79718b16e442de58778e148d0b1084e3b2dffd5de6b7b16cee7969282de7"), extdata.gas); } else { 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 = ThinNeo.TransactionAttributeUsage.Script; var scripthashfrom = ThinNeo.Helper.GetPublicKeyHashFromAddress(this.textAddr.Text); tran.attributes[0].data = scripthashfrom; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { tran.SerializeUnsigned(ms); var hexstr = ThinNeo.Helper.Bytes2HexString(ms.ToArray()); this.textTran.Text = hexstr; } } catch { MessageBox.Show("请填写正确的数据"); } }