Exemplo n.º 1
0
        public void TestToJson()
        {
            var json = uut.ToJson();

            json.ToString().Should().Be("{\"name\":\"noname\",\"version\":\"1.0\",\"scrypt\":{\"n\":2,\"r\":1,\"p\":1},\"accounts\":[],\"extra\":null}");
        }
        public string ExportNEP6Wallet(NEP6Wallet wallet, string password)
        {
            int wallet_count    = 0;
            int account_count   = 0;
            int contract_count  = 0;
            int scrypt_count    = 0;
            int parameter_count = 0;

            try
            {
                conn.Open();

                JObject jWallet = wallet.ToJson();

                int  scrypt_id = 0;
                bool check     = true;
                try
                {
                    // try get scrypt uid
                    string       cmdStr = "SELECT uid FROM neo.scrypt WHERE n=@n and r=@r and p=@p;";
                    MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                    cmd.Parameters.AddWithValue("@n", ReplaceNull(jWallet["scrypt"]["n"]));
                    cmd.Parameters.AddWithValue("@r", ReplaceNull(jWallet["scrypt"]["r"]));
                    cmd.Parameters.AddWithValue("@p", ReplaceNull(jWallet["scrypt"]["n"]));
                    MySqlDataReader rdr = cmd.ExecuteReader();

                    if (rdr.Read())
                    {
                        if (rdr.HasRows)
                        {
                            check     = false;
                            scrypt_id = rdr.GetInt32("uid");
                        }
                    }
                    rdr.Close();

                    scrypt_count++;
                }
                catch (MySqlException e)
                {
                    Console.WriteLine(e.StackTrace);
                    return("Query Failed!");
                }

                if (check)
                {
                    try
                    {
                        // table scrypt
                        string       cmdStr = "INSERT INTO neo.scrypt VALUES(NULL, @n, @r, @p);";
                        MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                        cmd.Parameters.AddWithValue("@n", ReplaceNull(jWallet["scrypt"]["n"]));
                        cmd.Parameters.AddWithValue("@r", ReplaceNull(jWallet["scrypt"]["r"]));
                        cmd.Parameters.AddWithValue("@p", ReplaceNull(jWallet["scrypt"]["n"]));
                        int result = cmd.ExecuteNonQuery();

                        scrypt_id = (int)cmd.LastInsertedId;
                    }
                    catch (MySqlException e)
                    {
                        Console.WriteLine(e.StackTrace);
                        return("Export Failed!");
                    }
                }

                int wallet_id = 0;
                try
                {
                    // table wallet
                    string       cmdStr = "INSERT INTO neo.wallet VALUES(NULL, @name, @version, @scrypt_id, @password, @extra);";
                    MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                    cmd.Parameters.AddWithValue("@name", ReplaceNull(jWallet["name"]));
                    cmd.Parameters.AddWithValue("@version", ReplaceNull(jWallet["version"]));
                    cmd.Parameters.AddWithValue("@scrypt_id", ReplaceNull(scrypt_id));
                    cmd.Parameters.AddWithValue("@password", ReplaceNull(password));
                    cmd.Parameters.AddWithValue("@extra", ReplaceNull(jWallet["extra"]));
                    int result = cmd.ExecuteNonQuery();

                    wallet_id = (int)cmd.LastInsertedId;
                    wallet_count++;
                }
                catch (MySqlException e)
                {
                    Console.WriteLine(e.StackTrace);
                    return("Export Failed!");
                }

                IEnumerable <NEP6Account> accounts = wallet.GetNEP6Accounts();

                foreach (NEP6Account account in accounts)
                {
                    JObject jAccount = account.ToJson();
                    try
                    {
                        // table contract
                        string       cmdStr = "INSERT INTO neo.contract VALUES(@script, @deployed);";
                        MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                        cmd.Parameters.AddWithValue("@script", ReplaceNull(jAccount["contract"]["script"]));
                        cmd.Parameters.AddWithValue("@deployed", ReplaceBoolean(jAccount["contract"]["deployed"].AsBoolean()));
                        int result = cmd.ExecuteNonQuery();

                        contract_count++;
                    }
                    catch (MySqlException e)
                    {
                        Console.WriteLine(e.StackTrace);
                        return("Export Failed!");
                    }

                    try
                    {
                        // table account
                        string       cmdStr = "INSERT INTO neo.account VALUES(@address, @label, @isDefault, @lock, @key, @script, @wallet_id, @extra);";
                        MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                        cmd.Parameters.AddWithValue("@address", ReplaceNull(jAccount["address"]));
                        cmd.Parameters.AddWithValue("@label", ReplaceNull(jAccount["label"]));
                        cmd.Parameters.AddWithValue("@isDefault", ReplaceBoolean(jAccount["isDefault"].AsBoolean()));
                        cmd.Parameters.AddWithValue("@lock", ReplaceBoolean(jAccount["lock"].AsBoolean()));
                        cmd.Parameters.AddWithValue("@key", ReplaceNull(jAccount["key"]));
                        cmd.Parameters.AddWithValue("@script", ReplaceNull(jAccount["contract"]["script"]));
                        cmd.Parameters.AddWithValue("@wallet_id", ReplaceNull(wallet_id));
                        cmd.Parameters.AddWithValue("@extra", ReplaceNull(jAccount["extra"]));
                        int result = cmd.ExecuteNonQuery();

                        account_count++;
                        count = account_count;
                    }
                    catch (MySqlException e)
                    {
                        Console.WriteLine(e.StackTrace);
                        return("Export Failed!");
                    }

                    foreach (JObject parameter in account.Contract.ParameterList.Zip((account.Contract as NEP6Contract).ParameterNames, (type, name) =>
                    {
                        JObject parameter = new JObject();
                        parameter["name"] = name;
                        parameter["type"] = type;
                        return(parameter);
                    }))
                    {
                        try
                        {
                            // table parameter
                            string       cmdStr = "INSERT INTO neo.parameter VALUES(NULL, @script, @name, @type);";
                            MySqlCommand cmd    = new MySqlCommand(cmdStr, conn);
                            cmd.Parameters.AddWithValue("@script", ReplaceNull(jAccount["contract"]["script"]));
                            cmd.Parameters.AddWithValue("@name", ReplaceNull(parameter["name"].AsString()));
                            cmd.Parameters.AddWithValue("@type", ReplaceNull(parameter["type"].AsString()));
                            int result = cmd.ExecuteNonQuery();

                            parameter_count++;
                        }
                        catch (MySqlException e)
                        {
                            Console.WriteLine(e.StackTrace);
                            return("Export Failed!");
                        }
                    }
                }

                conn.Close();

                return($"Export Success! Total: {wallet_count} wallet(s), {scrypt_count} scrypt(s), {account_count} account(s), {contract_count} contract(s), {parameter_count} parameter(s).");
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.StackTrace);
                return("Connect Failed!");
            }
        }