public void GetBalance() { ssbalance = 0; 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; //读取未消费的交易输出utxo Task <RestApiUtxo_class[]> utxo = Task <RestApiUtxo_class[]> .Run(() => { RestApiUtxo_class[] untxo = RestApi_class.getUtxosByAnAddress(uri, networkStr, address); return(untxo); }); utxo.Wait(); int n = utxo.Result.Length; for (int i = 0; i < n; i++) { ssbalance += utxo.Result[i].Value; } //Console.WriteLine(ssbalance); label3.Text = "账户余额:"; label3.Text += ssbalance; label3.Text += " sat"; }
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); } } }