Exemplo n.º 1
0
        private static void openWallet(JObject data)
        {
            var walletJson = data["wallet"].Value <string>();

            walletJson = walletJson.Split(',')[1];
            walletJson = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(walletJson));
            string fp = Environment.CurrentDirectory + @"\wallet.json";

            File.WriteAllText(fp, walletJson);

            string PWS = string.Empty;

            using (allowUseWallet dialog = new allowUseWallet((string)data["addrIn"], (string)data["addrOut"], (string)data["assetID"], (string)data["amounts"]))
            {
                if (dialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                PWS = dialog.PSW;

                wallet = new NEP6Wallet(fp);
                try
                {
                    wallet.Unlock(PWS);
                }
                catch (CryptographicException e)
                {
                    //MessageBox.Show(Strings.PasswordIncorrect);
                    //return e.Message;
                }
            }
        }
Exemplo n.º 2
0
        public static JObject ProcessMessage(JObject data)
        {
            JObject resJ = new JObject();

            var message = data["text"].Value <string>();

            switch (message)
            {
            case "namehash":
                try
                {
                    var nns = data["data"].Value <string>();
                    System.IO.File.WriteAllLines(@"log.txt", new string[] { "echo:", nns });
                    var Key    = NameHash(nns);
                    var KeyStr = Key.ToHexString();
                    resJ["key"]  = message;
                    resJ["data"] = KeyStr;
                }
                catch (Exception e)
                {
                    System.IO.File.WriteAllLines(@"err.txt", new string[] { "error:", e.Message });
                    var KeyStr = "请输入符合规则的域名";
                    resJ["key"]  = message;
                    resJ["data"] = KeyStr;
                }
                break;

            case "doTansfar":
                JObject tansfarInfoJ = (JObject)data["tansfarInfo"];
                //var walletJson = tansfarInfoJ["wallet"].Value<string>();
                //walletJson = walletJson.Split(',')[1];
                //walletJson = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(walletJson));
                //JObject Jwallet = JObject.Parse(walletJson);
                var key = (string)tansfarInfoJ["key"];
                //var scrypt = Jwallet["scrypt"];
                string PSW = string.Empty;
                using (allowUseWallet dialog = new allowUseWallet((string)tansfarInfoJ["addrIn"], (string)tansfarInfoJ["addrOut"], (string)tansfarInfoJ["assetID"], (string)tansfarInfoJ["amounts"]))
                {
                    if (dialog.ShowDialog() == DialogResult.OK)
                    {
                        PSW = dialog.PSW;
                    }
                }
                //nep2key 6PYPh1msaJEUvhCkVMxq2Lfu31f3PjAcfQSXQre6vbQguY7ZQeZou5TiB9
                //nep2Key to privateKey
                byte[] privateKey       = neoHelper.GetPrivateKeyFromNEP2(key, PSW);
                string privateKeyHexStr = BitConverter.ToString(privateKey).Replace("-", "").ToLower();
                //privateKeyHexStr 25228cd2b2aeb4fec8065cadab8dc28cb618fba3a789853fcda357c37f0864c1

                //privateKey to publicKey
                byte[] publicKey       = neoHelper.getPublickeyFromPrivateKey(privateKey);
                string publicKeyHexStr = BitConverter.ToString(publicKey).Replace("-", "").ToLower();
                //publicKeyHexStr 029020fd0914c677a1ab7d8b3502ca9a4506efbf7572ea24fb3862f48dd153ee5b

                //调用蓝鲸淘构造转账交易api
                Dictionary <string, string> tansfarInfoDic = new Dictionary <string, string>()
                {
                    { "source", (string)tansfarInfoJ["addrOut"] },
                    { "dests", (string)tansfarInfoJ["addrIn"] },
                    { "amounts", (string)tansfarInfoJ["amounts"] },
                    { "assetId", ((string)tansfarInfoJ["assetID"]).Replace("0x", "") }
                };
                var tansfarInfoDicStr = JsonConvert.SerializeObject(tansfarInfoDic);
                //{"source":"AYX8yqcvQroV9mJ5k4Ez2gro8Kjh7B78kD","dests":"AUb5MeSrTcmqBchWwsDVu8qgZ2Xv9E9ars","amounts":"0.01","assetId":"c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b"}
                string  resp = httpHelper.Post("https://api.otcgo.cn/testnet/transfer", tansfarInfoDic);
                JObject j    = JObject.Parse(resp);
                string  transactionScript = (string)j["transaction"];
                //80000001601715e825765452b25ab16979ce65a6705583dfebaa056c2a011aeda868dd4b0000029b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc540420f00000000008c8ed58be92fd1b01896dd9e02acd45776eb3e8c9b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5c0878b3b00000000b7b0b2d92b970affd9567faf10205ca222e38933

                string txStr = string.Empty;
                //using (ScriptBuilder sb = new ScriptBuilder())
                //{
                //    sb.EmitAppCall(UInt160.Parse(tansfarInfoDic["assetId"]), "transfer",Wallet.ToScriptHash(tansfarInfoDic["dests"]), Wallet.ToScriptHash(tansfarInfoDic["source"]), tansfarInfoDic["amounts"]);
                //    sb.Emit(OpCode.THROWIFNOT);

                //    txStr = sb.ToArray().ToHexString();
                //}

                //string remark = "";
                //Transaction tx = new ContractTransaction();
                //List<TransactionAttribute> attributes = new List<TransactionAttribute>();
                //    attributes.Add(new TransactionAttribute
                //    {
                //        Usage = TransactionAttributeUsage.Remark,
                //        Data = Encoding.UTF8.GetBytes(remark)
                //    });
                //tx.Attributes = attributes.ToArray();
                //tx.Outputs = new TransactionOutput[]{ new TransactionOutput {
                //    AssetId = UInt256.Parse(tansfarInfoDic["assetId"]),
                //    Value = Fixed8.Parse(tansfarInfoDic["amounts"]),
                //    ScriptHash = Wallet.ToScriptHash(tansfarInfoDic["source"])
                //} };
                //tx.Inputs = new CoinReference[] { new CoinReference {
                //    PrevHash = UInt256.Parse("0x2926ca1fa51a54c50951be340a217d83035489da4cb9ea2dd46550e12d2234c3"),
                //    PrevIndex = 0
                //} };
                //var txScript = tx.GetHashData().ToHexString();

                txStr = getTransferTxHex(tansfarInfoDic["source"], tansfarInfoDic["dests"], "0x" + tansfarInfoDic["assetId"], decimal.Parse(tansfarInfoDic["amounts"]));
                string sign   = Sign(txStr.HexToBytes(), privateKey).ToHexString();
                var    txStr2 = getTxSignHex(txStr, sign, publicKeyHexStr);

                string signature = Sign(transactionScript.HexToBytes(), privateKey).ToHexString();

                ////调用蓝鲸淘发送交易api
                //Dictionary<string, string> signInfoDic = new Dictionary<string, string>() {
                //        { "publicKey",publicKeyHexStr},
                //        { "signature",signature },
                //        { "transaction",transactionScript },
                //    };
                //string resp2 = httpHelper.Post("https://api.otcgo.cn/testnet/broadcast", signInfoDic);
                //JObject j2 = JObject.Parse(resp2);
                //string txid = (string)j2["txid"];

                //resJ["key"] = message;
                //resJ["data"] = txid;


                //try
                //{
                //    string publickey = string.Empty;
                //    string signature = string.Empty;
                //    foreach (WalletAccount walletaccount in wallet.GetAccounts())
                //    {
                //        //根据输出地址选择key
                //        if (walletaccount.Address == tansfarInfoDic["source"])
                //        {
                //            KeyPair addrKey = walletaccount.GetKey();

                //            publickey = addrKey.PublicKey.EncodePoint(true).ToHexString();

                //            addrKey.Decrypt();
                //            var pk = addrKey.PrivateKey;

                //            signature = Sign(transactionScript.HexToBytes(), pk).ToHexString();

                //            break;
                //        }
                //    }

                //    Dictionary<string, string> signInfoDic = new Dictionary<string, string>() {
                //        { "publicKey",publickey},
                //        { "signature",signature },
                //        { "transaction",transactionScript },
                //    };
                //    string resp2 = httpHelper.Post("https://api.otcgo.cn/testnet/broadcast", signInfoDic);
                //    JObject j2 = JObject.Parse(resp2);
                //    string txid = (string)j2["txid"];

                //    resJ["key"] = message;
                //    resJ["data"] = txid;
                //}
                //catch { }
                break;
                //case "openWallet":
                //    openWallet(data);
                //    try
                //    {
                //        string addr = wallet.GetAccounts().ToArray()[0].Address;
                //        resJ["key"] = message;
                //        resJ["data"] = addr;
                //    }
                //    catch { }
                //    break;
                //case "test":
                //    resJ["key"]= message;
                //    resJ["data"] = "testing!";
                //    break;
                //case "exit":
                //    resJ["key"] = message;
                //    resJ["data"] = "exit!";
                //    break;


                //default:
                //    try
                //    {
                //        message = httpHelper.Get("https://api.otcgo.cn/testnet/address/" + message, new Dictionary<string, string>());
                //        JObject j = JObject.Parse(message);
                //        message = JsonConvert.SerializeObject(j["balances"]);
                //    }
                //    catch
                //    { }
            }

            return(resJ);
        }