Bitcoin Forum
December 04, 2016, 04:40:40 PM *
News: To be able to use the next phase of the beta forum software, please ensure that your email address is correct/functional.
 
   Home   Help Search Donate Login Register  
Pages: [1]
  Print  
Author Topic: Sample account system using JSON-RPC needed  (Read 3487 times)
satoshi
Founder
Sr. Member
*
qt
Offline Offline

Activity: 364


View Profile
July 16, 2010, 07:45:10 PM
 #1

We need someone to write sample code, preferably Python or Java, showing the recommended way to use the JSON-RPC interface to create an account system.  Most sites that sell things will need something like this.  Someone who's kept up on the JSON-RPC threads here should have some idea how it should work.

When a user is logged in to their account, you show the bitcoin address they can send to to add funds.  Before showing it, you check if it's been used, if it has then you replace it with a new one (getnewaddress <username>).  You only need to keep the latest bitcoin address for the account in your database.  (I posted a sample code fragment for this in an earlier thread somewhere, search on getnewaddress)

You use getreceivedbylabel <username> with the username as the label to get the "credit" amount of the account.  You need to keep a "debit" amount in your database.  The current balance of the account is (credit - debit).  When the user spends money, you increase debit.

If you're requiring more than 0 confirmations, it's nice if you show the current balance (0 confirmations) and the available balance (1 or more confirmations), so they can immediately see that their payment is acknowledged.  Not all sites need to wait for confirmations, so the dual current & available should be optional.  Most sites selling digital goods are fine to accept 0 confirmations.

A nice sample app for this would be a simple bank site, which would have the above, plus the option to send a payment to a bitcoin address.  The sample code should be the simplest possible with the minimum extra stuff to make it a working site.

vekja.net is an example of a site like this.
1480869640
Hero Member
*
Offline Offline

Posts: 1480869640

View Profile Personal Message (Offline)

Ignore
1480869640
Reply with quote  #2

1480869640
Report to moderator
1480869640
Hero Member
*
Offline Offline

Posts: 1480869640

View Profile Personal Message (Offline)

Ignore
1480869640
Reply with quote  #2

1480869640
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
RudeDude
Newbie
*
Offline Offline

Activity: 11


View Profile
July 16, 2010, 08:18:45 PM
 #2

bitcoinmarket.com is doing all of these things now. perhaps the author of that site can share some code as well.

19sM3BSaoGByh3t6Hasr8YbApibXoPtW5Z
RudeDude
Newbie
*
Offline Offline

Activity: 11


View Profile
July 16, 2010, 08:23:48 PM
 #3

I should also point out that this site has some JSON code in Python.
http://www.alloscomp.com/bitcoin/

Specifically this code that checks the balance and sends it to a static address.
Code:
#!/usr/bin/python
import jsonrpc
MyBCAddress = 'YOUR BITCOIN ADDRESS HERE'

b = jsonrpc.ServiceProxy("http://localhost:8332/")
bal = float(b.getbalance())

if bal > 0.01:
    print "We have a balance of {0:.2f}BC. Sending...".format(bal)
    b.sendtoaddress(MyBCAddress,bal)
else:
    print "No coins here."

19sM3BSaoGByh3t6Hasr8YbApibXoPtW5Z
dwdollar
Full Member
***
Offline Offline

Activity: 166


View Profile WWW
July 16, 2010, 10:22:22 PM
 #4

I've been meaning to post this for some time.  I wanted to clean it up into a simple example, so it would be easy to understand.  I don't know when I'll have a chance now.  So... I'll just go ahead and post it as is.  Maybe it will help somebody.

It probably isn't the best approach (after talking to Satoshi a while back).  In fact, I haven't tried applying the new functions in the API, that were added later on and now in Version .3.  But, it is battle tested.  Why fix what isn't broken (yet)?  It runs as a separate process on the machine with the Bitcoin server.  The only thing I left out was a break handler for the while loop.  Anytime you see "list(something_inside)", realize it's a query to the database using SQLObject.

When I go through it again, I'll post a revision here.

Code:
#! /usr/bin/python


import jsonrpc
import time
import datetime

from database import *


class BitcoinInterface(object):

    def __init__(self):
        self.access = jsonrpc.ServiceProxy("http://127.0.0.1:8332")
        return

    def issueBitcoinAddresses(self):
        localBitcoinAddresses = list(Address.select(AND(Address.q.type_ == "localBitcoin", Address.q.value == None)))    # Get all unassigned local Bitcoin Addresses
        for localBitcoinAddress in localBitcoinAddresses:
            localBitcoinAddress.value = self.access.getnewaddress()
            client = Client.get(localBitcoinAddress.clientID)
            print "New Address:\nUsername:  %s\nClientID:  %i\nValue:  %s\n" % (client.username, client.id, localBitcoinAddress.value)
        return

    def takeDeposits(self):
        localBitcoinAddresses = list(Address.select(AND(Address.q.type_ == "localBitcoin", Address.q.value != None)))    # Get all assigned local Bitcoin Addresses
        for localBitcoinAddress in localBitcoinAddresses:
            amountReceived = self.access.getamountreceived(localBitcoinAddress.value)
            if amountReceived > localBitcoinAddress.amountReceived:
                client = Client.get(localBitcoinAddress.clientID)
                quantity = amountReceived - localBitcoinAddress.amountReceived
                client.bitcoins = client.bitcoins + quantity
                localBitcoinAddress.amountReceived = amountReceived
                transaction = Transaction(clientID = client.id)
                transaction.datetime = datetime.datetime.now()
                transaction.type_ = "Deposit"
                transaction.quantity = quantity    # Transaction quantity is positive for "Deposit"
                print "Deposit:\nUsername:  %s\nClientID:  %i\nQuantity:  %f\n" % (client.username, client.id, quantity)
        return

    def issueWithdrawals(self):
        clients = list(Client.select(Client.q.bitcoinsToWithdraw > 0))
        for client in clients:
            if client.bitcoinsToWithdraw <= client.bitcoins:    # Redundancy check
                foreignBitcoinAddress = list(Address.select(AND(Address.q.clientID == client.id, Address.q.type_ == "foreignBitcoin")))[0]
                try:
                    quantity = client.bitcoinsToWithdraw
                    self.access.sendtoaddress(foreignBitcoinAddress.value, quantity)
                    client.bitcoins = client.bitcoins - quantity
                    transaction = Transaction(clientID = client.id)
                    transaction.datetime = datetime.datetime.now()
                    transaction.type_ = "Withdrawal"
                    transaction.quantity = -quantity    # Transaction quantity is negative for "Withdrawal"
                    client.bitcoinsToWithdraw = 0
                    print "Withdrawal:\nUsername:  %s\nClientID:  %i\nQuantity:  %f\n" % (client.username, client.id, quantity)
                except:
                    print "Failed Withdrawal:\nUsername:  %s\nClientID:  %i\nQuantity:  %f\n" % (client.username, client.id, client.bitcoinsToWithdraw)              
            else:
                print "Failed Withdrawal, Not Enough Bitcoins:\nUsername:  %s\nClientID:  %i\n" % (client.username, client.id)
        return

bitcoinInterface = BitcoinInterface()
while 1:
    bitcoinInterface.issueBitcoinAddresses()
    bitcoinInterface.takeDeposits()
    bitcoinInterface.issueWithdrawals()
    time.sleep(300)
BitLex
Hero Member
*****
Offline Offline

Activity: 588


View Profile WWW
July 17, 2010, 01:01:59 PM
 #5

not exactly a complete account system (maybe half of it),
but i'd like to share my SAMPLE_JSON.PHP anyway, maybe it's of use for someone.
it's using cURL to connect to the API.
Code:
<?php
// IMPORTANT!
// This is NOT SECURE by default, restrict access to this script,
// password protect it, check the referrer, session, or whatever else you like.
// Anyone able to reach this script AS IS is able to send your balance to another address!


// simply call commands like this 
// ./SAMPLE_JSON.PHP?command=getinfo
// or with parameter
// ./SAMPLE_JSON.PHP?command=getnewaddress&parameter=username
// ./SAMPLE_JSON.PHP?command=getreceivedbylabel&parameter=username


// set the URL to JSON-API
$url 'http://127.0.0.1:8332';  
// set a JSON id
$jsonid 'unique'  

// which command has been called?
if ($_GET['command'])
$command $_GET['command'];
// and if what parameter?
if ($_GET['parameter'])
$para $_GET['parameter'];
if (
$para$parb = array($para);
// if none, we create an empty array
else $parb = array();
// and encode all of it to JSON
$arr = array (
 'id'=>$jsonid,
 'method'=>$command,
 'params'=>$parb
);
$data json_encode($arr);
// run cURL to transfer our data
  
$ch curl_init();
  
curl_setopt($chCURLOPT_HTTPHEADER, array('Content-Type: application/json')); 
  
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  
curl_setopt($chCURLOPT_POST1);
  
curl_setopt($chCURLOPT_URL$url);
  
curl_setopt($chCURLOPT_POSTFIELDS$data);
  
$output curl_exec($ch); 
// debug - to check the response we can just view our JSON output here
echo $output."<br>\n\r";  
// if $output looks ok, we decode it here and close our cURL session
  
$obj json_decode($output);
  
curl_close($ch); 

// and that's it, just handle the $obj and show it, add/compare it to DBs, or whatever else is needed.

// below  is just some basic samples 
   
if ($obj->error != "null")
     
echo $obj->error;   
     

if (
$obj->id == $jsonid) {    
 
if (is_object($obj->result))
$info get_object_vars($obj->result);
     
   
if ($command == "getinfo") {
    
 if ($info) {
            echo 
"Balance: "$info['balance']."<br>\n\r";
            echo 
"Blocks: "$info['blocks']."<br>\n\r";
            echo 
"Connections: "$info['connections']."<br>\n\r";
            if (
$info['generate'])
            
$gen "yes";
            else
            
$gen "no";
            echo 
"Generating: "$gen."<br>\n\r";
            echo 
"Difficulty: "$info['difficulty']."<br>\n\r";
         }
     } 
     if (
$command == "getreceivedbylabel") {
     
echo $obj->result."\n";
     }
     if (
$command == "getnewaddress") {
     
echo $obj->result."\n";
     }
}   
?>


be sure to restrict access somehow!

I guess there's easier, or simpler ways to do this, maybe you know of one.

denger
Newbie
*
Offline Offline

Activity: 14


View Profile
July 18, 2010, 03:12:14 AM
 #6

Anyone done one in perl?
Thanks!
ArtemZ
Newbie
*
Offline Offline

Activity: 26


View Profile
July 18, 2010, 03:28:33 AM
 #7

Anyone done one in perl?
Thanks!
its quite easy to do same in perl if there are any json-rpc library for perl.
melvster
Sr. Member
****
Offline Offline

Activity: 341


View Profile
July 26, 2010, 07:18:38 PM
 #8

Anyone done one in perl?
Thanks!

http://goddamn.co.uk/viewvc/perlmods/Finance-Bitcoin/lib/Finance/Bitcoin/
vual
Hero Member
*****
Offline Offline

Activity: 658



View Profile
February 06, 2012, 04:35:34 AM
 #9

worth a bump, its needed to be up the top:
https://en.bitcoin.it/wiki/API_tutorial_%28JSON-RPC%29
https://en.bitcoin.it/wiki/Proper_Money_Handling_%28JSON-RPC%29

genjix
Legendary
*
expert
Offline Offline

Activity: 1232


View Profile
February 06, 2012, 11:31:50 AM
 #10


I wrote those a long time ago and they're old. This is the updated version:

http://bitcoinmedia.com/merchant-developer-tutorial/
vual
Hero Member
*****
Offline Offline

Activity: 658



View Profile
February 06, 2012, 11:59:22 AM
 #11


wow... thanks kindly!!

akipfer
Sr. Member
****
Offline Offline

Activity: 294



View Profile WWW
March 08, 2014, 08:49:19 AM
 #12

I don't get it yet...

Its like this -> I'm making some kind of game using bitcoins as payable coin, but how do i know if the player has payd let's say 0.0001BTC, Player doesn't do LOGIN, just select diff and play, BUT to been able to play, he need to pay a little low amout, let's say-> 0.0001BTC....

How do i know (my system, PHP only..) if he has payed...causa i will to send him an email with tx and all, saying that has confirmed(when =>6 conf just to be sure that has actually payed...



Some thing that i see on here -> https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

Oh and one more thing..
 not right..assume this ->
<?php

$RoundValue = 0.12345678;
echo 'BTC 0,12345678 = "' .round($RoundValue * 1e8).'"';

?>

THIS will turn out to be \/

 
BTC 0,12345678 = "12345678"
That just not right!
and without that " round($RoundValue * 1e8)  " Its goes all FINE, the way is mean to be...or i forgetting something really bad ..?

So..What the hell?

Without that i think is okay...if not, why not? Explain it to me!

Thx4All till now!

BTC 1LDZwmLNULuEKPANry5DTbH5seLKwph8ua         | [WebSite off] coopersite.com.br | Aceitaremos Moedas como pgto | Procurando franqueados!
LTC  LYMQwZbBHK2kdD7SJnxCnn8NcwbiFAmgnV        | [Twitter] https://twitter.com/allankipfer
[M.B.] Rules - [M.B.]-[Mercado Bitcoin] Esta da hora!   | [Facebook] http://www.facebook.com/allan.kipfer.tbo
12648430
Full Member
***
Offline Offline

Activity: 144


View Profile
March 08, 2014, 09:18:13 AM
 #13

I don't get it yet...

Its like this -> I some kind of game using bitcoins, but how do i know if the player has payd let's say 0.0001BTC, Player doesn't do LOGIN, just select diff and play, BUT to been able to play, he need to pay a  low amout of 0.0001BTC....

How do i know (my system, PHP only..) if he has payed...

Thx4All till now!


Some thing that i see on here -> https://en.bitcoin.it/wiki/Proper_Money_Handling_(JSON-RPC)

Oh and one more thing..
 not right..assume this ->
<?php

$RoundValue = 0.12345678;
echo 'BTC 0,12345678 = "' .round($RoundValue * 1e8).'"';

?>

THIS WILL TURNS OUT TO BE

 
BTC 0,12345678 = "12345678"
That just not right!
and without that " round($RoundValue * 1e8)  " Its goes all FINE, the way is mean to be...or i forgetting something really bad ..?

So..What the hell?

Please don't write software that handles money.
akipfer
Sr. Member
****
Offline Offline

Activity: 294



View Profile WWW
March 08, 2014, 09:19:34 AM
 #14

What do you mean?

BTC 1LDZwmLNULuEKPANry5DTbH5seLKwph8ua         | [WebSite off] coopersite.com.br | Aceitaremos Moedas como pgto | Procurando franqueados!
LTC  LYMQwZbBHK2kdD7SJnxCnn8NcwbiFAmgnV        | [Twitter] https://twitter.com/allankipfer
[M.B.] Rules - [M.B.]-[Mercado Bitcoin] Esta da hora!   | [Facebook] http://www.facebook.com/allan.kipfer.tbo
12648430
Full Member
***
Offline Offline

Activity: 144


View Profile
March 08, 2014, 09:27:48 AM
 #15

What do you mean?

It is difficult to write software that is suitable for handling money, especially when it's other peoples' and your software is online for anyone to interact with. Your current programming knowledge is not up to the task. You can solve some problems like the one you posted by asking people to help you with them, but though you may end up with something that "runs" you can't write secure software without really knowing what you're doing.
akipfer
Sr. Member
****
Offline Offline

Activity: 294



View Profile WWW
March 08, 2014, 09:33:38 AM
 #16

To be honest, i'm not new with PHP, i'm with Bitcoin it self(json-rpc)

the game it self is pretty simple, can't get hacked by any means, seriously!

i only need to know how is that my scrypt can know if when the player send money, to tell me(or auto send an email)

I can't figure it out how, just that, i mean, i use getnewaddress every time some one click on pay? if so...hell that would make like so, so many addresses listed on my bitcoin(not qt for now, but still..)


Ps: I get that, on a high level i'm sure i need to learn a lot about json-rpc to do something like a coin trader or something 'BAD ASS' like that.. but i won't be abble to 'write secure software ' if i never do try...

just saying...Won't u agree with me?

BTC 1LDZwmLNULuEKPANry5DTbH5seLKwph8ua         | [WebSite off] coopersite.com.br | Aceitaremos Moedas como pgto | Procurando franqueados!
LTC  LYMQwZbBHK2kdD7SJnxCnn8NcwbiFAmgnV        | [Twitter] https://twitter.com/allankipfer
[M.B.] Rules - [M.B.]-[Mercado Bitcoin] Esta da hora!   | [Facebook] http://www.facebook.com/allan.kipfer.tbo
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!