Bitcoin Forum

Economy => Services => Topic started by: b!z on August 05, 2013, 08:24:16 AM



Title: How do I generate Bitcoin addresses on my website? $$
Post by: b!z on August 05, 2013, 08:24:16 AM
I need an address for me to receive coins to be generated and displayed when a user clicks a button. That's it. I don't need QR codes or any fancy stuff. What's the easiest way to do so (with code examples, since I'm quite clueless)?

Will tip btc.


Title: Re: How do I generate Bitcoin addresses on my website?
Post by: kseistrup on August 05, 2013, 08:30:36 AM
The code used on https://www.bitaddress.org/ can be downloaded from https://github.com/pointbiz/bitaddress.org


Title: Re: How do I generate Bitcoin addresses on my website?
Post by: Kiwi7 on August 05, 2013, 08:37:56 AM
Or, you can install bitcoind on the server and:
<?php
echo exec("bitcoind getnewaddress");
?>


Title: Re: How do I generate Bitcoin addresses on my website?
Post by: MA5H3D on August 05, 2013, 09:39:38 AM
It kinda depends what you want the address for..

Kseistrup mentioned bitaddress, but that will generate the address clientside with javascript. That might be ok, but it's no good for generating payment addresses because the client can get the private key.

Kiwi17 recommended installing bitcoind, this might be a bit overkill for just generating addresses.

If you just want to generate addresses so that people can pay you, the best option would be to use a serverside script like "PHPCoinAddress". That generates addresses for almost all coins, so just rip out the code for the coins you don't need and you're ready to go. You might want to build on it a bit to generate QR codes, but there are loads of scripts for that on github.
https://github.com/zamgo/PHPCoinAddress


Title: Re: How do I generate Bitcoin addresses on my website?
Post by: b!z on August 05, 2013, 09:40:15 AM
Would it be easier to use the coinbase or blockchain api? I am not the ownerof the server so it might not be wise to run bitaddress or bitcoind. How can i use the apis.? Or is there a better way.


Title: Re: How do I generate Bitcoin addresses on my website?
Post by: CIYAM on August 05, 2013, 09:48:36 AM
If you are not the owner of the server and can't be 100% of its security then you would not want to be generating key pairs on it and even APIs are going to be problematic in regards to keeping your "API keys" safe (although I think many APIs might have different keys for creating addresses vs. spending BTC so you should be fairly safe with those).

Another approach might be to generate 1000's of keypairs (or more depending upon the expected frequency of usage) from an offline computer using bitcoind or vanitygen and then import just the "public" keys of the key pairs into a DB (as a public key pool).

When the pool gets near to running out you would just replenish with more.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: Cyberdyne on August 05, 2013, 09:57:10 AM
Maybe the OP was too specific in his wording when he said 'generate' and doesn't need to generate them on the fly, but rather, just display btc addresses to his customers.

In that case, pre-generating a list of addresses using bitaddress.org, and then loading those public addresses into a table on your server is a perfect solution. Then keep the private keys offline for later spending.

Incidentally this is how my kamikaze games have been set up - Players' deposits never get sent to the game server at any point.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: CIYAM on August 05, 2013, 10:02:19 AM
In that case, pre-generating a list of addresses using bitaddress.org, and then loading those public addresses into a table on your server is a perfect solution. Then keep the private keys offline for later spending.

Yup - pretty much what I said - I think whether you would use bitaddress or vanitygen probably comes down to just how many addresses you want to generate (under 1000 probably bitaddress is easiest but over 10,000 and I think vanitygen would be better - it would also depend upon what sort of tools the OP is comfortable working with).

BTW - QR codes can easily be generated in javascript (so that shouldn't be your major concern either way) - code to do so is in both bitaddress.org and brainwallet.org (and probably plenty of other websites).


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: b!z on August 05, 2013, 10:14:17 AM
I need an address for me to receive coins to be generated and displayed when a user clicks a button. That's it. I don't need QR codes or any fancy stuff. What's the easiest way to do so (with code examples, since I'm quite clueless)?

@Cyberdyne and CIYAM Open, is there any code examples I can use to import public keys I have and display one every time a button is pressed? Thanks a lot.  :)


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: 🏰 TradeFortress 🏰 on August 05, 2013, 10:16:08 AM
I need an address for me to receive coins to be generated and displayed when a user clicks a button. That's it. I don't need QR codes or any fancy stuff. What's the easiest way to do so (with code examples, since I'm quite clueless)?

If you're OK with using Inputs:

Quote
<script src='https://inputs.io/js/buttons.js'></script>
<a href='https://inputs.io/pay?to=yourusername' class='inputsio' data-to='yourusername' >Button Text</a>


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: MA5H3D on August 05, 2013, 10:19:28 AM
Aaah.. if you don't need to actually "generate" the addresses then CIYAM's solution is perfect. But, to be fair the coinbase and blockchain apis are also perfectly safe and probably an easier solution..

just check out: https://coinbase.com/docs/merchant_tools/payment_buttons it's all well documented there.

Example:
<a class="coinbase-button" data-code="4d4b84bbad4508b64b61d372ea394dad" href="https://coinbase.com/checkouts/4d4b84bbad4508b64b61d372ea394dad">Pay With Bitcoin</a>
<script src="https://coinbase.com/assets/button.js" type="text/javascript"></script>


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: Cyberdyne on August 05, 2013, 11:12:05 AM
@Cyberdyne and CIYAM Open, is there any code examples I can use to import public keys I have and display one every time a button is pressed? Thanks a lot.  :)

Are you familiar with PHP/Mysql? Here's something to start with if it helps:

Code:
			
@mysql_query("UPDATE addresses SET used=NOW(), address=(@addr:=address) WHERE used IS NULL LIMIT 1;");
$result = mysql_query(SELECT @addr AS depositaddress;");
$row = mysql_fetch_assoc($result);
$deposit_address = $row['depositaddress'];

The above code will mark an address as used, and then retrieve the one that was just marked.

If you do it the other way around (retrieve one, and then mark it as used), it's technically possible to accidentally give 2 customers the same address, if they both request an address in the exact same microsecond.

Edit: Okay I just re-read your post and see that you probably need more code than that. Perhaps someone can expand on the above. Let us know how you go.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: CIYAM on August 05, 2013, 11:17:57 AM
I think that if you don't already know how to code the use of a pool of unused addresses then you would find it much easier to use an API as some of the other responders have mentioned (just make sure that the key used for the API call to generate an address is not the same one for being able to spend BTC).


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: dudeami on August 05, 2013, 11:23:18 AM
@Cyberdyne and CIYAM Open, is there any code examples I can use to import public keys I have and display one every time a button is pressed? Thanks a lot.  :)

Are you familiar with PHP/Mysql? Here's something to start with if it helps:

Code:
			
@mysql_query("UPDATE addresses SET used=NOW(), address=(@addr:=address) WHERE used IS NULL LIMIT 1;");
$result = mysql_query(SELECT @addr AS depositaddress;");
$row = mysql_fetch_assoc($result);
$deposit_address = $row['depositaddress'];

The above code will mark an address as used, and then retrieve the one that was just marked.

If you do it the other way around (retrieve one, and then mark it as used), it's technically possible to accidentally give 2 customers the same address, if they both request an address in the exact same microsecond.

Edit: Okay I just re-read your post and see that you probably need more code than that. Perhaps someone can expand on the above. Let us know how you go.

You really shouldn't be using mysql_* functions, at the very least use mysqli_* or PDO :)


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: b!z on August 05, 2013, 12:37:14 PM
I think that if you don't already know how to code the use of a pool of unused addresses then you would find it much easier to use an API as some of the other responders have mentioned (just make sure that the key used for the API call to generate an address is not the same one for being able to spend BTC).


Thank you for the advice. I will look at the blockchain.info receive API php example and see if I can figure out how to make it work.

EDIT: I failed somehow, can someone show me what I did wrong or show me an easier API? setup.php of https://github.com/blockchain/receive_payment_php_demo gives this error:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/a1011100/public_html/receive_payment_php_demo-master/setup.php on line 8
10 Invalid connect: Access denied for user 'root'@'localhost' (using password: YES)


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: johnnyfla123 on August 05, 2013, 01:31:13 PM
use coinbase. i use coinbase for my webhosting stuff it works great. I also use it to offer my ios beta slots. an example can be here for that:

http://aztecgaming.org/l/

It auto calculates the exchange rate. I set the value to USD and it calculates the current exhancge for the bitcoin payment. If you also want to see it on my hosting you can make a dummy order to see the system.

http://codingagency.com

On my hosting site it calls back as paid after 3 orders, on coinbase it provides transaction numbers that both me and the client recieve as well as a confermation number. sort of like a paypal payment setup. Also the exchange rates on coinbase are awsome. When i cash out I ushually get the money in my bank within 24 hours. The fee is a awsome 1% which beats almost any place. buying is also 1% but it takes 4 days to proccess when you buy. You can get a greendot bank account using the moneypak system and use that to handle he transacations. jsut be mindful that coinbase flags major buys and sells. each transaction i make are always less then a network of $500 to make sure that their system doesnt reject the transaction. I read somewhere where people have reported that coinbase rejects transactiosn it deems as unsafe or sketch.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: johnnyfla123 on August 05, 2013, 01:33:10 PM
Also coinbase supplies a new coinbase address per payment which is nice :)


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: CIYAM on August 05, 2013, 01:36:24 PM
EDIT: I failed somehow, can someone show me what I did wrong or show me an easier API? setup.php of https://github.com/blockchain/receive_payment_php_demo gives this error:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/a1011100/public_html/receive_payment_php_demo-master/setup.php on line 8
10 Invalid connect: Access denied for user 'root'@'localhost' (using password: YES)

That just looks as though you didn't pass a correct (or any) password to MySQL.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: stevenh512 on August 06, 2013, 05:07:29 AM
There's also Electrum. Install the Electrum wallet on your own PC and let the server generate a virtually endless supply of addresses from your master public key. No risk of private keys on the server and no third-party risk of having a payment processor handle your money for you (although so far inputs, coinbase and bitpay have all handled a lot of money with no major issues). Electrum comes with a Python script to generate addresses from your master public key, and there's also some working PHP and Ruby code at:
github.com/prusnak/addrgen

That's the way I'd do it, just give the server a single public key and let the money come straight to your wallet


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: cp1 on August 06, 2013, 05:11:50 AM
Armory can generate public addresses in its watching only mode without exposing private keys, you could probably use their scripts to do it.


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: b!z on August 06, 2013, 12:19:41 PM
Also coinbase supplies a new coinbase address per payment which is nice :)

Can you help me setup Coinbase on my site? I might rent your hosting service  :D


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: Cyberdyne on August 07, 2013, 05:07:50 AM
You really shouldn't be using mysql_* functions, at the very least use mysqli_* or PDO :)

Thanks, I hadn't read about that until today.

Personally I use more abstracted methods in a framework, but that might have made my example more complicated instead of less...    $db->query()


Title: Re: How do I generate Bitcoin addresses on my website? $$
Post by: Eric Blair on September 15, 2013, 09:01:52 PM
I think that if you don't already know how to code the use of a pool of unused addresses then you would find it much easier to use an API as some of the other responders have mentioned (just make sure that the key used for the API call to generate an address is not the same one for being able to spend BTC).


Thank you for the advice. I will look at the blockchain.info receive API php example and see if I can figure out how to make it work.

EDIT: I failed somehow, can someone show me what I did wrong or show me an easier API? setup.php of https://github.com/blockchain/receive_payment_php_demo gives this error:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /home/a1011100/public_html/receive_payment_php_demo-master/setup.php on line 8
10 Invalid connect: Access denied for user 'root'@'localhost' (using password: YES)

Go to include.php and change $mysql_password.