Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e) //记录
        {
            string data      = textBox3.Text;
            string judgedata = data.Trim();

            if (!string.IsNullOrEmpty(judgedata))
            {
                string WifPrivateKeyStr = PrivateKey;
                //string WifPrivateKeyStr = "cUvazeu9ucqD4trygt8xMEQKZfR3SZ5BdiAWb3eEwbQ48iPwYKSB";
                string uri     = bsvConfiguration_class.RestApiUri;
                string network = bsvConfiguration_class.testNetwork;
                byte[] encryptedBytes;
                //对数据先进行AES加密,再进行base58编码
                encryptedBytes = AES_class.AesEncrypt(data, WifPrivateKeyStr);
                Base58Encoder base58Encoder = new Base58Encoder();
                string        base58Str     = base58Encoder.EncodeData(encryptedBytes);
                Console.WriteLine(encryptedBytes);
                Console.WriteLine(base58Str);
                Dictionary <string, string> response;
                //发送加密数据到链上
                Task <long> gets = Task <long> .Run(() =>
                {
                    long fee, donationFee;
                    Transaction tx;
                    //response = bsvTransaction_class.send(WifPrivateKeyStr, 0, network, null, null, base58Str, 1, 0);
                    response = bsvTransaction_class.send(WifPrivateKeyStr, 0, network, out tx, out fee, out donationFee, null, null, base58Str, 1, 0);
                    Console.WriteLine("tx fee: " + fee);
                    return(fee);
                });

                textBox2.Text += data + System.Environment.NewLine;
                textBox2.Text += "\r\n";
                textBox2.Text += "------------------------------------------------------------------------------------";
                textBox2.Text += "\r\n";
                textBox2.Text += System.Environment.NewLine;
                //改变余额
                ssbalance    = ssbalance - gets.Result;
                label3.Text  = "账户余额:";
                label3.Text += ssbalance;
                label3.Text += " sat";
            }
            else
            {
                MessageBox.Show("记录不能为空!");
            }
            textBox3.Text = "";
        }
Exemplo n.º 2
0
        //注册
        private void button1_Click(object sender, EventArgs e)
        {
            label10.Text = mes;
            string user     = textBox1.Text;
            string pwd      = textBox2.Text;
            string pwd2     = textBox3.Text;
            string realname = textBox4.Text;
            string realnum  = textBox5.Text;
            string lovename = textBox6.Text;

            byte[] pk = new byte[32];
            if (user.Length != 0 && realname.Length != 0 && realnum.Length != 0) //用户名、真实姓名、证件号不能为空
            {
                if (pwd == pwd2)                                                 //确认密码
                {
                    //产生私钥
                    string AESkey = user + pwd;
                    while (AESkey.Length < 32)
                    {
                        AESkey += "1";
                    }
                    string sStr = realnum;
                    while (sStr.Length < 32)
                    {
                        sStr += "3";
                    }
                    string s = realname + realnum + lovename;
                    Console.WriteLine(AESkey);
                    Console.WriteLine(s);
                    byte[] p;
                    p = AES_class.AesEncrypt(s, sStr);
                    int n = p.Length;
                    if (n < 32)
                    {
                        for (int i = 0; i < n; i++)
                        {
                            pk[i] = p[i];
                        }
                        for (int i = n; i < 32; i++)
                        {
                            pk[i] = 1;
                        }
                    }
                    else if (n > 31)
                    {
                        for (int i = 0; i < 32; i++)
                        {
                            pk[i] = p[i];
                        }
                    }
                    Key           key        = new Key(pk);
                    BitcoinSecret privateKey = key.GetBitcoinSecret(Network.TestNet);

                    //加密私钥后写入文件
                    //string path = user+".txt";
                    string path          = user;
                    string privateKeyStr = "[[[";
                    privateKeyStr += privateKey.ToString();
                    privateKeyStr += "]]]";
                    Console.WriteLine("私钥:" + privateKeyStr);
                    byte[] APkey;
                    APkey = AES_class.AesEncrypt(privateKeyStr, AESkey);
                    Base58Encoder base58Encoder = new Base58Encoder();
                    string        base58Str     = base58Encoder.EncodeData(APkey);
                    Console.WriteLine("写入数据:" + base58Str);
                    File.WriteAllText(path, base58Str);


                    //获取地址
                    PubKey         pubKey  = privateKey.PubKey;
                    KeyId          pkhash  = pubKey.Hash;
                    BitcoinAddress addres  = pkhash.GetAddress(Network.TestNet);
                    string         address = addres.ToString();
                    MessageBox.Show("注册成功!\n请使用地址:" + address + "前往https://faucet.bitcoincloud.net/网站申领比特币。");

                    //跳转页面
                    string pkey = privateKey.ToString();
                    this.Hide();
                    Form1 ff = new Form1(pkey);
                    ff.ShowDialog();
                }
                else
                {
                    MessageBox.Show("密码输入错误!");
                }
            }
            else
            {
                MessageBox.Show("请正确输入相关信息!");
            }
        }
Exemplo n.º 3
0
        private void button1_Click(object sender, EventArgs e) //读取
        {
            textBox2.Text = "";
            Console.WriteLine("start get");

            string privateKeyStr = PrivateKey;
            //string privateKeyStr = "cUvazeu9ucqD4trygt8xMEQKZfR3SZ5BdiAWb3eEwbQ48iPwYKSB";
            BitcoinSecret  privateKey = new BitcoinSecret(privateKeyStr);
            Network        network    = privateKey.Network;
            PubKey         pubKey     = privateKey.PubKey;
            string         pubKeyStr  = pubKey.ToHex();
            KeyId          pkhash     = pubKey.Hash;
            string         pkhashStr  = pkhash.ToString();
            BitcoinAddress addres     = pkhash.GetAddress(network);
            string         address    = addres.ToString();
            string         networkStr = bsvConfiguration_class.testNetwork;
            string         uri        = bsvConfiguration_class.RestApiUri;

            //获取链上的交易历史
            Task <RestApiAddressHistoryTx[]> t = Task <RestApiAddressHistoryTx[]> .Run(() =>
            {
                RestApiAddressHistoryTx[] addrHistory = RestApi_class.getAddressHistory(uri, networkStr, address);
                return(addrHistory);
            });

            t.Wait();
            int num = t.Result.Length;

            Console.WriteLine("链上交易数目:" + num);
            //读取链上信息
            Task <RestApiTransaction[]> gettxs = null;

            if (num > 0)
            {
                string[] txHashs = new string[num];
                for (int i = 0; i < num; i++)
                {
                    txHashs[i] = t.Result[i].TxHash;
                }

                gettxs = Task <RestApiTransaction[]> .Run(() =>
                {
                    RestApiTransaction[] txs = RestApi_class.getTransactions(uri, networkStr, txHashs);
                    return(txs);
                });
            }

            for (int i = 0; i < num; i++)
            {
                RestApiTransaction tx = gettxs.Result[i];
                string             s  = RestApi_class.getOpReturnData(tx, bsvConfiguration_class.encoding);
                if (s != null)
                {
                    //解密
                    byte[]        encryptedBytes;
                    Base58Encoder base58Encoder = new Base58Encoder();
                    encryptedBytes = base58Encoder.DecodeData(s);
                    string data = AES_class.AesDecrypt(encryptedBytes, privateKeyStr);
                    textBox2.Text += data;
                    textBox2.Text += "\r\n";
                    textBox2.Text += System.Environment.NewLine;
                    textBox2.Text += "------------------------------------------------------------------------------------";
                    textBox2.Text += "\r\n";
                    textBox2.Text += System.Environment.NewLine;
                    Console.WriteLine("链上内容:" + s);
                }
            }
        }
Exemplo n.º 4
0
        //登录
        private void button1_Click(object sender, EventArgs e)
        {
            string user   = textBox1.Text;
            string pwd    = textBox2.Text;
            string AESkey = user + pwd;

            while (AESkey.Length < 32)
            {
                AESkey += "1";
            }
            //打开文件获取密文

            string c = null;

            try
            {
                c = File.ReadAllText(user);
            }
            catch (Exception ex)
            {
                MessageBox.Show("获取失败!请确认您是否已注册!\n" + ex.Message);
            }

            //对密文解密
            try
            {
                //string c = File.ReadAllText("privatekey.txt");
                byte[]        encryptedBytes;
                Base58Encoder base58Encoder = new Base58Encoder();
                encryptedBytes = base58Encoder.DecodeData(c);
                string plain = AES_class.AesDecrypt(encryptedBytes, AESkey);
                //确认身份
                int    n    = plain.Length;
                int    flag = 0;
                string pkey = null;
                for (int i = 0; i < 3; i++)
                {
                    if (plain[i] != '[')
                    {
                        flag = 1;
                    }
                }
                for (int i = n - 1; i > n - 4; i--)
                {
                    if (plain[i] != ']')
                    {
                        flag = 1;
                    }
                }
                if (flag == 0)  //身份正确
                {
                    for (int i = 3; i < n - 3; i++)
                    {
                        pkey += plain[i];
                    }
                    //Console.WriteLine("私钥:" + pkey);
                    this.Hide();
                    Form1 ff = new Form1(pkey);
                    ff.ShowDialog();
                }
                else
                {
                    MessageBox.Show("登录失败!请确认用户名密码输入正确!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("登录失败!请确认用户名密码输入正确!\n" + ex.Message);
            }
        }