Exemplo n.º 1
0
        public JObject sendTxPlusSign(string neoCliJsonRPCUrl, string txScriptHex, string signHex, string publicKeyHex)
        {
            byte[] txScript = txScriptHex.HexString2Bytes();
            byte[] sign     = signHex.HexString2Bytes();
            byte[] pubkey   = publicKeyHex.HexString2Bytes();
            //byte[] prikey = privateKeyHex.HexToBytes();

            //byte[] sign = null;

            //sign = ThinNeo.Helper.Sign(txScript, prikey);

            //var pubkey = ThinNeo.Helper.GetPublicKeyFromPrivateKey(prikey);

            var addr = ThinNeo.Helper.GetAddressFromPublicKey(pubkey);

            ThinNeo.Transaction lastTran = new ThinNeo.Transaction();
            lastTran.Deserialize(new MemoryStream(txScript));
            lastTran.witnesses = null;
            lastTran.AddWitness(sign, pubkey, addr);

            string TxPlusSignStr = string.Empty;

            using (var ms = new System.IO.MemoryStream())
            {
                lastTran.Serialize(ms);
                TxPlusSignStr = ms.ToArray().ToHexString();
            }

            return(sendrawtransaction(neoCliJsonRPCUrl, TxPlusSignStr));
        }
Exemplo n.º 2
0
        public JObject sendrawtransaction(string neoCliJsonRPCUrl, string txSigned)
        {
            httpHelper hh   = new httpHelper();
            var        resp = hh.Post(neoCliJsonRPCUrl, "{'jsonrpc':'2.0','method':'sendrawtransaction','params':['" + txSigned + "'],'id':1}", System.Text.Encoding.UTF8, 1);

            JObject Jresult       = new JObject();
            bool    isSendSuccess = false;
            var     res           = JObject.Parse(resp);

            if (res["error"] != null && res["error"]["message"] != null)
            {
                isSendSuccess = false;
                Jresult.Add("errorMessage", res["error"]["message"]);
            }
            else
            {
                isSendSuccess = (bool)JObject.Parse(resp)["result"];
            }
            //bool isSendSuccess = (bool)JObject.Parse(resp)["result"];
            //JObject Jresult = new JObject();
            Jresult.Add("sendrawtransactionresult", isSendSuccess);
            if (isSendSuccess)
            {
                ThinNeo.Transaction lastTran = new ThinNeo.Transaction();
                lastTran.Deserialize(new MemoryStream(txSigned.HexString2Bytes()));
                string txid = lastTran.GetHash().ToString();

                ////从已签名交易体分析出未签名交易体,并做Hash获得txid
                //byte[] txUnsigned = txSigned.Split("014140")[0].HexString2Bytes();
                //string txid = ThinNeo.Helper.Sha256(ThinNeo.Helper.Sha256(txUnsigned)).Reverse().ToArray().ToHexString();

                Jresult.Add("txid", txid);
            }
            else
            {
                //上链失败则返回空txid
                Jresult.Add("txid", string.Empty);
            }

            return(Jresult);
        }
Exemplo n.º 3
0
        async Task test_claimStep2()
        {
            byte[] postdata;

            var url    = Helper.MakeRpcUrlPost(Config.api, "getclaimtxhex", out postdata, new MyJson.JsonNode_ValueString(this.address));
            var result = await Helper.HttpPost(url, postdata);

            Console.WriteLine("得到的结果是:" + result);

            var json       = MyJson.Parse(result).AsDict();
            var claimtxhex = "";

            if (json.ContainsKey("result"))
            {
                claimtxhex = json["result"].AsList()[0].AsDict()["claimtxhex"].AsString();

                byte[] buf = ThinNeo.Helper.HexString2Bytes(claimtxhex);

                ThinNeo.Transaction tran = new ThinNeo.Transaction();
                tran.Deserialize(new System.IO.MemoryStream(buf));

                byte[] msg      = tran.GetMessage();
                byte[] signdata = ThinNeo.Helper.Sign(msg, this.prikey);

                tran.AddWitness(signdata, pubkey, this.address);

                var trandata    = tran.GetRawData();
                var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata);

                byte[] postdata2;
                url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata2, new MyJson.JsonNode_ValueString(strtrandata));

                var result2 = await Helper.HttpPost(url, postdata2);

                Console.WriteLine("得到的结果是:" + result2);
            }
        }
Exemplo n.º 4
0
        async Task test_getRefund()
        {
            //if (lasttxid == null)
            //{
            //    Console.WriteLine("你还没有正确执行Refund");
            //    return;
            //}
            Console.WriteLine("refund txid:");
            var lastTxid = Console.ReadLine();

            string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(sneo_common.sc_sneo);

            Console.WriteLine("address=" + nep55_address);

            //获取地址的资产列表
            Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address);

            if (dir.ContainsKey(Config.id_NEO) == false)
            {
                Console.WriteLine("no neo");
                return;
            }
            List <Utxo> newlist = new List <Utxo>();

            foreach (var utxo in dir[Config.id_NEO])
            {
                if (utxo.n == 0 && utxo.txid.ToString().Equals(lastTxid))
                {
                    newlist.Add(utxo);
                }
            }
            if (newlist.Count == 0)
            {
                Console.WriteLine("找不到要使用的UTXO");
                return;
            }
            else
            {
                lastTxid = newlist[0].txid.ToString();
            }


            {//检查是否是前面交易存储的
                var ret = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getRefundTarget", "(hex256)" + lastTxid);

                sneo_common.ResultItem item = ret.value;

                var value = ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160());
                if (value.Length == 0)//未标记的UTXO,不能使用
                {
                    Console.WriteLine("这个utxo没有标记");
                    return;
                }
                if (value != this.address)
                {
                    Console.WriteLine("这个utxo不是标记给你用的");
                    return;
                }
            }

            ThinNeo.Transaction tran = Helper.makeTran(newlist, address, new ThinNeo.Hash256(Config.id_NEO), newlist[0].value);
            tran.type    = ThinNeo.TransactionType.ContractTransaction;
            tran.version = 0;


            //sign and broadcast
            {//做智能合约的签名
                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(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata));

            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());
                }
            }
        }
Exemplo n.º 5
0
        //退款操作
        async Task test_refund()
        {
            Console.WriteLine("Input refund tokens:");
            string refund = Console.ReadLine();

            string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(sneo_common.sc_sneo);

            Console.WriteLine("nep55_address=" + nep55_address);

            //获取地址的资产列表
            Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address);

            if (dir.ContainsKey(Config.id_NEO) == false)
            {
                Console.WriteLine("no neo");
                return;
            }
            List <Utxo> newlist = new List <Utxo>(dir[Config.id_NEO]);

            for (var i = newlist.Count - 1; i >= 0; i--)
            {
                var ret = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getRefundTarget", "(hex256)" + newlist[i].txid.ToString());

                sneo_common.ResultItem item = ret.value;

                if (newlist[i].n > 0)
                {
                    continue;
                }

                var value = item.subItem[0].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")); //参数倒序入
                    sb.EmitAppCall(sneo_common.sc_sneo);                              //nep5脚本
                    script = sb.ToArray();
                }
                Console.WriteLine("contract address=" + nep55_address);//往合约地址转账

                //生成交易
                tran      = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(Config.id_NEO), Decimal.Parse(refund));
                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[] 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(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata));

            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拿走NEO了 txid=" + lasttxid.ToString() + "[0]");
                }
                else
                {
                    lasttxid = null;
                }
            }
        }
Exemplo n.º 6
0
    IEnumerator HTTP_nel_post_sgas_gas2(MyJson.IJsonNode json_complete)
    {
        string txid = json_complete.AsDict()["txid"].ToString();

        var    nep55_shash   = new ThinNeo.Hash160(global.id_sgas);
        string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash);

        WWWForm www_getuxo_form_2 = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(nep55_address));
        WWW     www_utxo_2        = new WWW(global.api, www_getuxo_form_2);

        yield return(www_utxo_2);

        Dictionary <string, List <Utxo> > _dir_2 = get_utxo(www_utxo_2.text);

        if (_dir_2.ContainsKey(global.id_GAS) == false)
        {
            Debug.Log("no gas");
        }

        List <Utxo> newlist_2 = new List <Utxo>();

        foreach (var utxo in _dir_2[global.id_GAS])
        {
            if (utxo.n == 0 && utxo.txid.ToString().Equals(txid))
            {
                newlist_2.Add(utxo);
            }
        }
        if (newlist_2.Count == 0)
        {
            Debug.Log("找不到要使用的UTXO");
            yield return(null);
        }

        {//检查utxo
            byte[] script = null;
            using (var sb = new ThinNeo.ScriptBuilder())
            {
                var array = new MyJson.JsonNode_Array();
                array.AddArrayValue("(hex256)" + newlist_2[0].txid.ToString());
                sb.EmitParamJson(array);                                                   //参数倒序入
                sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getRefundTarget")); //参数倒序入
                var shash = new ThinNeo.Hash160(global.id_sgas);
                sb.EmitAppCall(shash);                                                     //nep5脚本
                script = sb.ToArray();
            }

            WWWForm www_getTarget_form = Helper.GetWWWFormPost("invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script)));
            WWW     www_form           = new WWW(global.api, www_getTarget_form);
            yield return(www_form);

            var jsonCU = MyJson.Parse(www_form.text);
            var stack  = jsonCU.AsDict()["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict();
            var value  = stack["value"].AsString();
            if (value.Length == 0)//未标记的UTXO,不能使用
            {
                Debug.Log("这个utxo没有标记");
                yield return(null);
            }

            var hash = new ThinNeo.Hash160(ThinNeo.Helper.HexString2Bytes(value));
            if (hash.ToString() != ThinNeo.Helper.GetPublicKeyHashFromAddress(roleInfo.getInstance().address).ToString())
            {
                Debug.Log("这个utxo不是标记给你用的");
                Debug.Log(hash.ToString());
                Debug.Log(ThinNeo.Helper.Bytes2HexString(roleInfo.getInstance().scripthash));
                yield return(null);
            }
        }


        ThinNeo.Transaction tran_2 = Helper.makeTran(newlist_2, roleInfo.getInstance().address, new ThinNeo.Hash256(global.id_GAS), newlist_2[0].value);
        tran_2.type    = ThinNeo.TransactionType.ContractTransaction;
        tran_2.version = 0;

        //sign and broadcast
        {//做智能合约的签名
            byte[] n55contract = null;
            {
                WWWForm www_getState = Helper.GetWWWFormPost("getcontractstate", new MyJson.JsonNode_ValueString(global.id_sgas));
                WWW     www_state    = new WWW(global.api, www_getState);
                yield return(www_state);

                var _json    = MyJson.Parse(www_state.text).AsDict();
                var _resultv = _json["result"].AsList()[0].AsDict();
                n55contract = ThinNeo.Helper.HexString2Bytes(_resultv["script"].AsString());
            }
            byte[] iscript = null;
            using (var sb = new ThinNeo.ScriptBuilder())
            {
                sb.EmitPushNumber(0);
                sb.EmitPushNumber(0);
                iscript = sb.ToArray();
            }
            tran_2.AddWitnessScript(n55contract, iscript);
        }


        var trandata_2    = tran_2.GetRawData();
        var strtrandata_2 = ThinNeo.Helper.Bytes2HexString(trandata_2);

        ThinNeo.Transaction testde_2 = new ThinNeo.Transaction();
        testde_2.Deserialize(new System.IO.MemoryStream(trandata_2));

        WWWForm www_transaction_2 = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata_2));
        WWW     www_tran_2        = new WWW(global.api, www_transaction_2);

        yield return(www_tran_2);

        Debug.Log(www_tran_2.text);

        var json_2 = MyJson.Parse(www_tran_2.text).AsDict();

        if (json_2.ContainsKey("result"))
        {
            var resultv = json_2["result"].AsList()[0].AsDict();
            var ext     = resultv["txid"].AsString();
            if (txid.Length > 0)
            {
                Debug.Log("txid=" + ext);
                testtool.panel_main.do_plat_notify(json_complete, ext);
                api_tool._instance.walletNotifyExt(roleInfo.getInstance().uid, roleInfo.getInstance().token, txid, ext, global.netType, null);
            }
            else
            {
                Debug.Log("交易失败");
            }
        }
        else
        {
            Debug.Log("交易失败");
        }
    }
Exemplo n.º 7
0
    IEnumerator HTTP_nel_post_sgas_gas(decimal num)
    {
        var    nep55_shash   = new ThinNeo.Hash160(global.id_sgas);
        string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(nep55_shash);

        WWWForm www_getuxo_form = Helper.GetWWWFormPost("getutxo", new MyJson.JsonNode_ValueString(nep55_address));
        WWW     www             = new WWW(global.api, www_getuxo_form);

        yield return(www);

        Dictionary <string, List <Utxo> > _dir = get_utxo(www.text);

        if (_dir.ContainsKey(global.id_GAS) == false)
        {
            Debug.Log("no gas");
        }

        List <Utxo> newlist = new List <Utxo>(_dir[global.id_GAS]);

        for (var i = newlist.Count - 1; i >= 0; i--)
        {
            Debug.Log(newlist[i].txid.ToString());

            byte[] script = null;
            using (var sb = new ThinNeo.ScriptBuilder())
            {
                var array = new MyJson.JsonNode_Array();
                array.AddArrayValue("(hex256)" + newlist[i].txid.ToString());
                sb.EmitParamJson(array);                                                   //参数倒序入
                sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)getRefundTarget")); //参数倒序入
                var shash = new ThinNeo.Hash160(global.id_sgas);
                sb.EmitAppCall(shash);                                                     //nep5脚本
                script = sb.ToArray();
            }
            if (newlist[i].n > 0)
            {
                continue;
            }

            WWWForm www_getTarget_form = Helper.GetWWWFormPost("invokescript", new MyJson.JsonNode_ValueString(ThinNeo.Helper.Bytes2HexString(script)));
            WWW     www_form           = new WWW(global.api, www_getTarget_form);

            yield return(www_form);

            var jsonCU = MyJson.Parse(www_form.text);
            var stack  = jsonCU.AsDict()["result"].AsList()[0].AsDict()["stack"].AsList()[0].AsDict();
            var value  = stack["value"].AsString();
            if (value.Length > 0)//已经标记的UTXO,不能使用
            {
                newlist.RemoveAt(i);
            }
        }

        string paparms;

        ThinNeo.Transaction tran = null;
        {
            byte[] script = null;
            using (var sb = new ThinNeo.ScriptBuilder())
            {
                var array = new MyJson.JsonNode_Array();
                array.AddArrayValue("(bytes)" + ThinNeo.Helper.Bytes2HexString(roleInfo.getInstance().scripthash));
                sb.EmitParamJson(array);                                          //参数倒序入
                sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入
                var shash = new ThinNeo.Hash160(global.id_sgas);
                sb.EmitAppCall(shash);                                            //nep5脚本
                script = sb.ToArray();


                //拼接发给后台做验证的json字符串
                MyJson.JsonNode_Object account = new MyJson.JsonNode_Object();
                account["sbParamJson"]  = array;
                account["sbPushString"] = new MyJson.JsonNode_ValueString("refund");
                account["nnc"]          = new MyJson.JsonNode_ValueString(global.id_sgas);
                account["cnts"]         = new MyJson.JsonNode_ValueString(num.ToString());
                paparms = account.ToString();
            }

            //生成交易
            tran      = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(global.id_GAS), num);
            tran.type = ThinNeo.TransactionType.InvocationTransaction;
            var idata = new ThinNeo.InvokeTransData();
            tran.extdata = idata;
            idata.script = script;

            //附加鉴证
            tran.attributes          = new ThinNeo.Attribute[1];
            tran.attributes[0]       = new ThinNeo.Attribute();
            tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script;
            tran.attributes[0].data  = roleInfo.getInstance().scripthash;
        }

        //sign and broadcast
        {//做智能合约的签名
            byte[] n55contract = null;
            {
                WWWForm www_getState = Helper.GetWWWFormPost("getcontractstate", new MyJson.JsonNode_ValueString(global.id_sgas));
                WWW     www_state    = new WWW(global.api, www_getState);

                yield return(www_state);

                var _json    = MyJson.Parse(www_state.text).AsDict();
                var _resultv = _json["result"].AsList()[0].AsDict();
                n55contract = ThinNeo.Helper.HexString2Bytes(_resultv["script"].AsString());
            }
            byte[] iscript = null;
            using (var sb = new ThinNeo.ScriptBuilder())
            {
                sb.EmitPushString("whatever");
                sb.EmitPushNumber(250);
                iscript = sb.ToArray();
            }
            tran.AddWitnessScript(n55contract, iscript);
        }
        {//做提款人的签名
            var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), roleInfo.getInstance().prikey);
            tran.AddWitness(signdata, roleInfo.getInstance().pubkey, roleInfo.getInstance().address);
        }
        var trandata    = tran.GetRawData();
        var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata);

        ThinNeo.Transaction testde = new ThinNeo.Transaction();
        testde.Deserialize(new System.IO.MemoryStream(trandata));

        WWWForm www_transaction = Helper.GetWWWFormPost("sendrawtransaction", new MyJson.JsonNode_ValueString(strtrandata));
        WWW     www_tran        = new WWW(global.api, www_transaction);

        yield return(www_tran);


        Debug.Log(www_tran.text);

        var json = MyJson.Parse(www_tran.text).AsDict();

        ThinNeo.Hash256 lasttxid = null;
        if (json.ContainsKey("result"))
        {
            bool bSucc = false;
            if (json["result"].type == MyJson.jsontype.Value_Number)
            {
                bSucc = json["result"].AsBool();
                Debug.Log("cli=" + json["result"].ToString());
            }
            else
            {
                var resultv = json["result"].AsList()[0].AsDict();
                var txid    = resultv["txid"].AsString();
                bSucc = txid.Length > 0;
            }
            if (bSucc)
            {
                lasttxid = tran.GetHash();
                Debug.Log("你可以从这个UTXO拿走GAS了 txid=" + lasttxid.ToString() + "[0]");

                api_tool._instance.addUserWalletLogs(roleInfo.getInstance().uid, roleInfo.getInstance().token, lasttxid.ToString(),
                                                     "0", num.ToString(), "2", paparms, global.netType, "0",
                                                     (bool timeout1, WWW www1) => { testtool.panel_main.on_refresh_WalletListss(); testtool.panel_main.on_refresh_plat_NotifyList(); });
            }
            else
            {
                lasttxid = null;
            }
        }
    }