Example #1
0
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder();
            sb.EmitPushString(iDescription.Text);
            sb.EmitPushString(iEmail.Text);
            sb.EmitPushString(iAuthor.Text);
            sb.EmitPushString(iVersion.Text);
            sb.EmitPushString(iName.Text);
            int need_storage = iStorage.IsChecked == true ? 1 : 0;
            int need_nep4    = iDyncall.IsChecked == true ? 2 : 0;

            sb.EmitPushNumber(need_storage | need_nep4);
            var br = ThinNeo.Helper.HexString2Bytes(iRType.Text);
            var bp = ThinNeo.Helper.HexString2Bytes(iPList.Text);

            sb.EmitPushBytes(br);
            sb.EmitPushBytes(bp);
            var _ss = ThinNeo.Helper.HexString2Bytes(this.asmBinText.Text);

            sb.EmitPushBytes(_ss);

            sb.EmitSysCall("Neo.Contract.Create");

            //sb.EmitSysCall("Neo.Contract.Create", script, parameter_list, return_type, need_storage | need_nep4, name, version, author, email, description);
            this.script = sb.ToArray();

            this.DialogResult = true;
        }
Example #2
0
        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());
            }
        }
Example #3
0
        public JObject publishContractForTest(string neoCliJsonRPCUrl, string avmHexstring, JObject infoJ)
        {
            string cName            = (string)infoJ["cName"];
            string cVersion         = (string)infoJ["cVersion"];
            string cAuthor          = (string)infoJ["cAuthor"];
            string cEmail           = (string)infoJ["cEmail"];
            string cDescription     = (string)infoJ["cDescription"];
            bool   iStorage         = (bool)infoJ["iStorage"];
            bool   iDyncall         = (bool)infoJ["iDyncall"];
            string inputParamsType  = (string)infoJ["inputParamsType"];
            string outputParamsType = (string)infoJ["outputParamsType"];

            //实例化脚本构造器
            ThinNeo.ScriptBuilder sb = new ThinNeo.ScriptBuilder();
            //加入合约基本信息
            sb.EmitPushString(cDescription);
            sb.EmitPushString(cEmail);
            sb.EmitPushString(cAuthor);
            sb.EmitPushString(cVersion);
            sb.EmitPushString(cName);

            //加入是否需要私有存储区、是否需要动态合约调用信息
            int need_storage = iStorage == true ? 1 : 0;
            int need_nep4    = iDyncall == true ? 2 : 0;

            sb.EmitPushNumber(need_storage | need_nep4);//二进制或操作

            //加入输入输出参数类型信息
            var outputType = ThinNeo.Helper.HexString2Bytes(outputParamsType);
            var inputType  = ThinNeo.Helper.HexString2Bytes(inputParamsType);

            sb.EmitPushBytes(outputType);
            sb.EmitPushBytes(inputType);

            //加入合约编译后二进制码
            var contractScript = ThinNeo.Helper.HexString2Bytes(avmHexstring);

            sb.EmitPushBytes(contractScript);

            sb.EmitSysCall("Neo.Contract.Create");

            string scriptPublish = ThinNeo.Helper.Bytes2HexString(sb.ToArray());

            //调用cli RPC 用neoVM试运行,并获得费用估算
            return(invokeScript(neoCliJsonRPCUrl, scriptPublish));
        }
Example #4
0
        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());
            }
        }