public void NeoTransfer() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(wif); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); using (var sb = new ThinNeo.ScriptBuilder()) { var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + from); array.AddArrayValue("(addr)" + "AMjCDmrbfcBxGPitHcdrUYRyPXD7DfC52c");//NeoBank address AMjCDmrbfcBxGPitHcdrUYRyPXD7DfC52c array.AddArrayValue("(int)" + value); 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); sb.EmitParamJson(array); //参数倒序入 sb.EmitPushString("transfer"); //参数倒序入 sb.EmitAppCall(new Hash160("0x04e31cee0443bb916534dad2adf508458920e66d")); //nep5脚本 script = sb.ToArray(); } ThinNeo.Transaction tran = new Transaction(); tran.inputs = new ThinNeo.TransactionInput[0]; tran.outputs = new 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(address); tran.version = 1; tran.type = ThinNeo.TransactionType.InvocationTransaction; var idata = new ThinNeo.InvokeTransData(); tran.extdata = idata; idata.script = script; idata.gas = 0; byte[] msg = tran.GetMessage(); string msgstr = ThinNeo.Helper.Bytes2HexString(msg); 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 = MakeRpcUrlPost(NeoRpcUrl, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(rawdata)); var result = HttpPost(url, postdata); Console.WriteLine(result + " txid: " + txid); return(result); }
static async void DelContract(string avmPath, string strurl, string rpcurl, string assetId, string wif, string change_address) { 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); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes(avmPath); //这里填你的合约所在地址 //Console.WriteLine("合约脚本:"+ThinNeo.Helper.Bytes2HexString(script)); Console.WriteLine("合约脚本hash:" + ThinNeo.Helper.GetScriptHashFromScript(script)); using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { //倒叙插入数据 sb.EmitPushBytes(script); sb.EmitSysCall("Neo.Contract.Destroy"); 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); //string result = http.Post(api, "invokescript", new MyJson.JsonNode_Array() { new MyJson.JsonNode_ValueString(scriptPublish) },Encoding.UTF8); var resultObj = MyJson.Parse(result) as MyJson.JsonNode_Object; var consume = resultObj["result"].AsDict()["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 = (gas_consumed >= 10) ? Math.Ceiling(gas_consumed - 10) : gas_consumed; //拼装交易体 ThinNeo.Transaction tran = makeTran(dir, null, new ThinNeo.Hash256(assetId), extdata.gas, change_address); 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 = Helper.MakeRpcUrlPost(rpcurl, "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 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 void ZoroTransfer() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(wif); var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); var address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); ScriptBuilder sb = new ScriptBuilder(); MyJson.JsonNode_Array array = new MyJson.JsonNode_Array(); array.AddArrayValue("(addr)" + from); array.AddArrayValue("(addr)" + "AUB7tMoKTzN33iVVqhz98vnT3KiG4bqx3f"); //ZoroBank address AUB7tMoKTzN33iVVqhz98vnT3KiG4bqx3f array.AddArrayValue("(int)" + value); 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); sb.EmitParamJson(array); sb.EmitPushString("transfer"); sb.EmitAppCall(new Hash160("0x67147557c0b6431e9b9297de26b46d9889434e49")); Hash160 scripthash = ThinNeo.Helper.GetPublicKeyHashFromAddress(address); string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); byte[] postdata; ThinNeo.InvokeTransData extdata = new ThinNeo.InvokeTransData(); extdata.script = sb.ToArray(); extdata.gas = 0; ThinNeo.Transaction tran = Helper.makeTran(null, null, new ThinNeo.Hash256(Config.getValue("id_GAS")), extdata.gas); tran.version = 1; tran.extdata = extdata; tran.type = ThinNeo.TransactionType.InvocationTransaction; //附加鉴证 tran.attributes = new ThinNeo.Attribute[1]; tran.attributes[0] = new ThinNeo.Attribute(); tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script; tran.attributes[0].data = scripthash; 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); MyJson.JsonNode_Array postRawArray = new MyJson.JsonNode_Array(); postRawArray.AddArrayValue(chainHash); //跟链 postRawArray.AddArrayValue(rawdata); var url = Helper.MakeRpcUrlPost(ZoroRpcUrl, "sendrawtransaction", out postdata, postRawArray.ToArray()); var result = Helper.HttpPost(url, postdata).Result; Console.WriteLine(result + " txid: " + txid); }
async public Task Demo() { Console.WriteLine("请输入你的wif"); string wif = Console.ReadLine(); if (string.IsNullOrEmpty(wif)) { wif = ""; //这里填你用于支付发布合约消耗的私钥 } 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("Nep5.5gas_Contract.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 = 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 | need_nep4 | need_canCharge; //倒叙插入数据 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 = Helper.MakeRpcUrlPost(api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = await Helper.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(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); //Console.WriteLine("scripthash:"+scripthash); 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()); } }
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); }
ThinNeo.Transaction GenTran() { if (this.listInput.Items.Count == 0) { MessageBox.Show("no input"); return(null); } if (this.listOutput.Items.Count == 0) { MessageBox.Show("no output"); return(null); } if (this.listWitness.Items.Count == 0) { MessageBox.Show("no witness"); return(null); } ThinNeo.Transaction trans = new ThinNeo.Transaction(); trans.attributes = new ThinNeo.Attribute[0]; var invokedata = new ThinNeo.InvokeTransData(); if (tabCType.SelectedIndex == 0) { trans.type = ThinNeo.TransactionType.ContractTransaction; } else if (tabCType.SelectedIndex == 1) { if (lastScript == null) { throw new Exception("need script"); } if (lastFee.HasValue == false) { throw new Exception("need test script"); } trans.type = ThinNeo.TransactionType.InvocationTransaction; trans.version = 1; trans.extdata = new ThinNeo.InvokeTransData(); invokedata = (trans.extdata as ThinNeo.InvokeTransData); invokedata.script = lastScript; //gas 系統費衹收整數? decimal gas = lastFee.Value - (decimal)10.0; if (gas < 0) { gas = 0; } gas = Math.Ceiling(gas); //invokedata.gas = gas; //(trans.extdata as ThinNeo.InvokeTransData).gas = lastFee.Value; } trans.inputs = new ThinNeo.TransactionInput[this.listInput.Items.Count]; var _listOutput = new List <ThinNeo.TransactionOutput>(); for (var i = 0; i < listInput.Items.Count; i++) { var item = listInput.Items[i] as Tools.Input; var input = new ThinNeo.TransactionInput(); input.index = (ushort)item.Coin.fromN; var checkutxo = item.Coin.fromN + "_" + item.Coin.fromID; foreach (var v in Tools.CoinTool.UtxoHistory) { if (v == checkutxo) { if (MessageBox.Show("use a input with spented money. Realy?", "", MessageBoxButton.YesNo) == MessageBoxResult.No) { return(null); } } } input.hash = ThinNeo.Helper.HexString2Bytes(item.Coin.fromID).Reverse().ToArray();//反转 trans.inputs[i] = input; } for (var i = 0; i < listOutput.Items.Count; i++) { var item = listOutput.Items[i] as Tools.Output; if (string.IsNullOrEmpty(item.Target)) {//扔钱 invokedata.gas += item.Fix8; continue; } var output = new ThinNeo.TransactionOutput(); output.assetId = ThinNeo.Helper.HexString2Bytes(item.assetID).Reverse().ToArray();//反转 output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(item.Target); output.value = item.Fix8; _listOutput.Add(output); } trans.outputs = _listOutput.ToArray(); return(trans); }
/// <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("请填写正确的数据"); } }
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()); } }
public async Task Demo() { byte[] prikey = ThinNeo.Helper.GetPrivateKeyFromWIF(Config.test_wif); byte[] pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey); string address = ThinNeo.Helper.GetAddressFromPublicKey(pubkey); Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, address); //从文件中读取合约脚本 byte[] script = System.IO.File.ReadAllBytes("dapp_sgas_migrate.avm"); //这里填你的合约所在地址 string str_script = ThinNeo.Helper.Bytes2HexString(script); byte[] aa = ThinNeo.Helper.HexString2Bytes(str_script); using (ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder()) { //倒叙插入数据 var array = new MyJson.JsonNode_Array(); array.AddArrayValue("(bytes)" + str_script); array.AddArrayValue("(bytes)0710"); array.AddArrayValue("(bytes)05"); array.AddArrayValue("(int)" + 5); array.AddArrayValue("(str)合约测试"); //name array.AddArrayValue("(str)1"); //version array.AddArrayValue("(str)ss"); //author array.AddArrayValue("(str)1"); //email array.AddArrayValue("(str)sssss"); //desc sb.EmitParamJson(array); //参数倒序入 sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)upgrade")); //参数倒序入 var shash = Config.dapp_sgas; sb.EmitAppCall(shash); string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray()); byte[] postdata; var url = Helper.MakeRpcUrlPost(Config.api, "invokescript", out postdata, new MyJson.JsonNode_ValueString(scriptPublish)); var result = await Helper.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.gas = 500;// Math.Ceiling(gas_consumed - 10); extdata.script = sb.ToArray(); //拼装交易体 ThinNeo.Transaction tran = Helper.makeTran(dir[Config.id_GAS], null, new ThinNeo.Hash256(Config.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(Config.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()); } }
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); } }