Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCH Wallet for C# : how to creating public key and address from a Random String (mnemonics) , Getting BHC Balance from address , sending BCH transaction to BCH blockchain. #40

Open
Wgroup-Zohaib opened this issue Sep 14, 2020 · 2 comments

Comments

@Wgroup-Zohaib
Copy link

No description provided.

@Wgroup-Zohaib
Copy link
Author

Wgroup-Zohaib commented Sep 14, 2020

Sample code for Steller Wallet ,

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stellar.Implementation.Common;
using Stellar.Implementation.Interface;
using stellar_dotnet_sdk;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Stellar.Implementation.Implementation
{
    public class StellarWallet : IStellarWallet
    {
        public string GenerateWalletAddress(string mnemonic)
        {
            var wallet = Wallet.FromMnemonic(mnemonic);
            var WalletAddress = wallet.GetPublicKey(0);
          
            return WalletAddress;
        }

        public decimal GetBalance(string PublicAddress)
        {
            decimal balance = 0.0M;
            //String server = String.Format("https://horizon-testnet.stellar.org/accounts/{0}", PublicAddress);
            String server = String.Format("https://horizon.stellar.org/accounts/{0}", PublicAddress);
            var body = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(server);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        body = reader.ReadToEnd();
                    }
                }
            }
            var obj = JObject.Parse(body);
            var balancesArray = (JArray)obj.SelectToken("balances");
            foreach (var item in balancesArray)
            {
                if(item.SelectToken("asset_type").ToString() == "native")
                {
                    balance = Convert.ToDecimal(item.SelectToken("balance").ToString());
                }
            }
            //var b = new Balance();
            //var getBalance = JsonConvert.DeserializeObject<JArray>(body);
            //foreach (var item in getBalance["balances"])
            //{
            //    var amount = (double)item["balance"];
            //    var asset_type = (string)item["asset_type"];
            //    if (asset_type == "native")
            //    {
            //        b.balance = amount;
            //        b.asset_type = asset_type;
            //    }
            //}
            //var json = JsonConvert.SerializeObject(b);
            return balance;
        }
        public async Task<string> MakePayment(string amount, string mnemonic, string destAccountId)
        {
            var PrivateKey = Wallet.FromMnemonic(mnemonic).GetPrivateKey(0);

            Network.UseTestNetwork();
            //Server server = new Server("https://horizon-testnet.stellar.org");
            Server server = new Server("https://horizon.stellar.org");
            var sourceKeypair = KeyPair.FromSecretSeed(PrivateKey);
            var destinationKeyPair = KeyPair.FromAccountId(destAccountId);
            var destAccount = server.Accounts.Account(destinationKeyPair);
            //var sourceAccountResp = await server.Accounts.Account(sourceKeypair);

            //String getAccount = String.Format("https://horizon-testnet.stellar.org/accounts/{0}", sourceKeypair.AccountId);
            String getAccount = String.Format("https://horizon.stellar.org/accounts/{0}", sourceKeypair.AccountId);
            var body = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getAccount);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        body = reader.ReadToEnd();
                    }
                }
            }
            var obj = JObject.Parse(body);
            var sequenceNo = (long)obj.SelectToken("sequence");
            //Console.WriteLine(sequenceNo);

Zohaib Khalid, [14.09.20 17:25]
var sourceAccount = new Account(sourceKeypair, sequenceNo);
            var operation = new PaymentOperation.Builder(destinationKeyPair, new AssetTypeNative(), amount).Build();
            var transaction = new Transaction.Builder(sourceAccount).AddOperation(operation).AddMemo(Memo.Text("sample payment")).Build();
            transaction.Sign(sourceKeypair);

            try
            {
                var resp = await server.SubmitTransaction(transaction);
                if (resp.IsSuccess())
                {
                    //Console.WriteLine("\ntransaction completed successfully!\n");
                    //Console.WriteLine("Remaining Balance of Sender Account\n");
                    //await GetAccountBalanceTest(sourceKeypair.AccountId);
                    //var tx = JsonConvert.SerializeObject(transaction);
                    return transaction.SequenceNumber.ToString();
                }
                else
                {
                    throw new Exception("Transaction Not Completed");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public string GetTestnetCoins(string address)
        {
            String friendBotUrl = String.Format("https://friendbot.stellar.org/?addr={0}", address);
            var body = "";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(friendBotUrl);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        body = reader.ReadToEnd();
                    }
                }
            }
            return body;
        }
    }
}

@Wgroup-Zohaib
Copy link
Author

StellarWallet.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant