question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create Account BadRequest

See original GitHub issue

Hi,

any idea why this doesn’t work? The documentation on creating accounts seems to use different values for the PublicKey depending on the SDK.

  public class Crypto
  {
    public static void Main ()
    {
      KeyPair keyPair = CreateKeyPair();
      CreateAccount(keyPair);
    }

    private static string CreateAccount(KeyPair keyPair)
    {
      HttpClient client = new HttpClient();
      var publicKey = Convert.ToBase64String(keyPair.PublicKey);
      var response = client.GetAsync($"https://horizon-testnet.stellar.org/friendbot?addr={publicKey}");
      Task.WaitAll(response);
      var account = response.Result;
      return account.Content.ToString();
    }

    public static KeyPair CreateKeyPair()
    {
      KeyPair keypair = KeyPair.Random();
      return keypair;
    }
  }
}

I receive an Error 400 “Bad Request” when GETting this.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
michaeljmontecommented, Dec 19, 2017

David,

No, I do not think you are. It appears friendbot sometimes returns a tx_bad_seq and they are working on a fix. https://github.com/stellar/go/issues/134

That being said try using this code. It will work most of the time and should work all the time once Stellar implements the fix for friendbot.

    public class Program
    {
        //For testing use the following account info, this only exists on test network and may be wiped at any time...
        //Public: GAZHWW2NBPDVJ6PEEOZ2X43QV5JUDYS3XN4OWOTBR6WUACTUML2CCJLI
        //Secret: SCD74D46TJYXOUXFC5YOA72UTPCCVHK2GRSLKSPRB66VK6UJHQX2Y3R3

        public static async Task Main(string[] args)
        {
            Network.UseTestNetwork();
            var server = new Server("https://horizon-testnet.stellar.org");

            CreateNewAccountUsingFriendbot(server);
        }

        private static void CreateNewAccountUsingFriendbot(Server server)
        {
            //Create a Random Key Pair.
            var keypair = KeyPair.Random();

            Console.WriteLine("Trying to create new account using friendbot: " + keypair.AccountId);

            //Ask friendbot to create a new account for us and to credit the account some stellar.
            //This only works like this on the testnet. On the real net an already created account needs 
            //create a transaction sending 20 lumens to the new account.
            Server friendBotServer = new Server("https://horizon-testnet.stellar.org");
            var url = $"https://horizon-testnet.stellar.org/friendbot?addr={keypair.AccountId}";
            var response = friendBotServer.HttpClient.GetAsync(url);
            Task.WaitAll(response);
            var accountContent = response.Result;

            if (!accountContent.IsSuccessStatusCode)
            {
                Console.WriteLine("Something went wrong! :(");
                Console.WriteLine("ResultCode: " + accountContent.ReasonPhrase);
                Console.Read();

            }
            else
            {

                //See our newly created account.
                var account = server.Accounts.Account(keypair);
                var accountResult = account.Result;
                Console.WriteLine("Account Created!");
                Console.WriteLine("Stellar Account: " + accountResult.KeyPair.AccountId);
                Console.WriteLine("Private Key: " + keypair.SecretSeed);

                foreach (var balance in accountResult.Balances)
                {
                    Console.WriteLine($"Type: {balance.AssetType}, Code: {balance.AssetCode}, Balance: {balance.BalanceString}");
                }
                Console.Read();
            }
        }
    }

That being said me and Eric are thinking about adding extra support for testnet specifically friendbot into the SDK so you don’t have to create the httpClient call manually.

Hope this helps.

0reactions
michaeljmontecommented, May 26, 2018

Oops I was mistaken, the first server instantiation in needed as well, because we use it later to get our account info after friendbot gives us money. However we could just probably use just one server over again, like I said quick code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure AD Create User Bad Request
I have created a flow to automate new hire processes, with the main part being the "Create User" from Azure AD.
Read more >
Getting a "Bad Request" Message for signup and sign in
The sign up route for the website I'm working on isn't working fine. I get this once I click on sign up it...
Read more >
How to Fix a 400 Bad Request Error (Causes and Fixes)
The 400 Bad Request error indicates that the server cannot or process the request due to a client error. Read about the common...
Read more >
Solved: Re: Create User API giving 400 Bad Request Error
Hello, all, I am trying to create a new user using the "Create User" API. I'm sending the following and getting a 400...
Read more >
Bad Request when creating a user without password #30241
A Bad Request occures. The error comes from UserManager which does not have a valid password for the account. It is because the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found