Bitcoin Forum
November 18, 2024, 04:51:36 PM *
News: Check out the artwork 1Dq created to commemorate this forum's 15th anniversary
 
   Home   Help Search Login Register More  
Pages: « 1 [2] 3 »  All
  Print  
Author Topic: Command Line and JSON-RPC  (Read 34350 times)
The Madhatter
Hero Member
*****
Offline Offline

Activity: 490
Merit: 511


My avatar pic says it all


View Profile
March 02, 2010, 03:46:42 PM
 #21

That wchar thing was what I was stuck on for the OSX build. Cheesy

Glad to see it is ironed out. I'll keep hacking away now.
satoshi (OP)
Founder
Sr. Member
*
qt
Offline Offline

Activity: 364
Merit: 7248


View Profile
March 05, 2010, 01:46:25 AM
 #22

This is strange... When I start Bitcoin as a daemon on my 64 bit Linux server, it eats up all the 250MB of remaining RAM, 700MB of swap and eventually crashes. On my 32 bit Ubuntu desktop, it works fine and stays at 15MB of memory usage. The server is running a 64 bit build of Bitcoin. Maybe there's something wrong with the build or something.
sirius-m debugged this, it was 64-bit related. 

The fix is now available on SVN, file util.cpp.
dwdollar
Full Member
***
Offline Offline

Activity: 202
Merit: 109


GCC - Global cryptocurrency


View Profile WWW
April 06, 2010, 07:07:05 AM
Last edit: April 06, 2010, 11:51:05 AM by dwdollar
 #23

Here is code for a simple Python API.  Each method connects to the server, sends a request, gets a response, then returns a Python dictionary equivalent of the JSON.  It uses standard Python modules.  No error checking is done and only three functions from rpc.cpp are implemented.  If there's interest I can write more.

To use it, the Python code should be something like...
access =  BitcoinAPI()
access.getInfo()
access.getAmountReceived("1JyEmxiMso2RsFVfBcCa616npBvGgxiBX")
access.sendToAddress("1JyEmxiMso2RsFVfBcCa616npBvGgxiBX", 100.00)    # Send 100 Bitcoins to my Address  Grin

This will be the base for automatic transactions on my site.  If there are any questions or concerns let me know.  If there is something severely wrong, feel free to school me.

Code:
import httplib, simplejson


class BitcoinAPI(object):

    def __init__(self, host = "127.0.0.1", port = 8332, timeout = 3):
        self.host = host
        self.port = port
        self.timeout = timeout
        self.httpHeader = {"Content-type": "application/json"}    # I don't know what needs to be in the header, but this works
        return

    def connect(self):
        self.connection = httplib.HTTPConnection(self.host, self.port, timeout = self.timeout)
        return

    def disconnect(self):
        self.connection.close()
        return

    # Functions return a python dictionary which should be equivalent to the JSON syntax received from the server
    # ident or "id" is a constant 1, but can be overridden when calling.  E.g. getAmountReceived(address, ident = 23)
    def getInfo(self, ident = 1):
        self.connect()
        params = simplejson.dumps({"method": "getinfo", "params": [], "id": ident})
        self.connection.request("POST", "/", params, self.httpHeader)
        response = self.connection.getresponse()
        #print response.status, response.reason    # Use for troubleshooting
        dictionary = simplejson.loads(response.read())
        self.disconnect()
        return dictionary

    def getAmountReceived(self, address, ident = 1):
        self.connect()
        params = simplejson.dumps({"method": "getamountreceived", "params": [address], "id": ident})
        self.connection.request("POST", "/", params, self.httpHeader)
        response = self.connection.getresponse()
        #print response.status, response.reason    # Use for troubleshooting
        dictionary = simplejson.loads(response.read())
        self.disconnect()
        return dictionary

    def sendToAddress(self, address, amount, ident = 1):
        self.connect()
        params = simplejson.dumps({"method": "sendtoaddress", "params": [address, amount], "id": ident})
        self.connection.request("POST", "/", params, self.httpHeader)
        response = self.connection.getresponse()
        #print response.status, response.reason    # Use for troubleshooting
        dictionary = simplejson.loads(response.read())
        self.disconnect()
        return dictionary

sirius
Bitcoiner
Sr. Member
****
Offline Offline

Activity: 429
Merit: 1002



View Profile
April 06, 2010, 10:06:57 PM
 #24

Here is code for a simple Python API.  Each method connects to the server, sends a request, gets a response, then returns a Python dictionary equivalent of the JSON.  It uses standard Python modules.  No error checking is done and only three functions from rpc.cpp are implemented.  If there's interest I can write more.

Well, you could just install python-json-rpc (http://json-rpc.org/wiki/python-json-rpc) and do:

Code:
from jsonrpc import ServiceProxy
s = ServiceProxy("http://127.0.0.1:8332")
s.getinfo()

Returns a python dict. It's that easy Wink

Iris — for better social networks
I'm not a forum admin - please contact theymos instead.
dwdollar
Full Member
***
Offline Offline

Activity: 202
Merit: 109


GCC - Global cryptocurrency


View Profile WWW
April 06, 2010, 10:58:08 PM
 #25


Well, you could just install python-json-rpc (http://json-rpc.org/wiki/python-json-rpc) and do:

Code:
from jsonrpc import ServiceProxy
s = ServiceProxy("http://127.0.0.1:8332")
s.getinfo()

Returns a python dict. It's that easy Wink

D***, haha...  Well... at least I got sirius-m to share some of his mojo.    Tongue

I'll check it out.

ducki2p
Jr. Member
*
Offline Offline

Activity: 34
Merit: 1



View Profile WWW
April 14, 2010, 06:15:08 PM
 #26

Thanks for that! The JSON interface is very easy to use.
Karmicads
Full Member
***
Offline Offline

Activity: 185
Merit: 121



View Profile
April 29, 2010, 10:04:36 AM
Last edit: April 29, 2010, 01:12:38 PM by Karmicads
 #27

This sounds very promising. Is there anyway to carve it out as a stand alone binary? My very rudimentary programming skills might not cut the cheese, but I would like to try to make a Firefox bitcoin wallet plugin. If anybody wants to hack at that and leave me in the dust then I gladly eat dust, otherwise I will plod along and see what I can't do (I refuse to do the impossible, but merely resist the inevitable Cheesy). If there were a way to just get the extra files for the JSON and such like (for web only), working with the bitcoin 0.2.0 version and save having to compile, then the target user would not have to install anything other than the plugin itself, that what I'm fishing for really. Is that a tall order?

I would aim to have bitcoin built into Firefox as a standard. Since no third party is being given preference here, can't it be standard browser code, or even built into the HTML or JavaScript, to parse dedicated scripting code and streamline development of online banking and financial utilities? Perhaps even deposits and withdrawals from banks could then finally be done online without their greedy charges. The plugin would demonstrate the idea for now though and save some user hassle as installing is easy, standard and the plugin repository has wide exposure. Then people may want to email and invite their friends to get the new electronic cash standard. The Paypal tactic of being able to email cash to somebody without an account, and have them login to make an account (to get their money), will translate to, having them load Firefox and/or open the plugin window from the email and install. Guaranteed - satisfaction or your money back, (to the sender of course). Go viral bitcoin. Wink


EDIT: "can't it be standard browser code, or even built into the HTML or JavaScript"  -- And now with a little research, I realize that is just what the JSON stuff does. Yikes! you can't fall asleep for 5 minutes without finding yourself out of the loop.
lachesis
Full Member
***
Offline Offline

Activity: 210
Merit: 105


View Profile
June 11, 2010, 02:54:57 PM
 #28

Is there some way this might be recast as a file socket (I suppose Windows would be an issue) or with some sort of security?

At the moment, the lowliest user on any of my machines can happily make a completely unauthenticated SendToAddress call and empty my wallet.

Bitcoin Calculator | Scallion | GPG Key | WoT Rating | 1QGacAtYA7E8V3BAiM7sgvLg7PZHk5WnYc
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5390
Merit: 13427


View Profile
June 11, 2010, 04:46:09 PM
Last edit: June 11, 2010, 05:08:32 PM by theymos
 #29

You can block it for everyone but your intended users with iptables.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
laszlo
Full Member
***
Offline Offline

Activity: 199
Merit: 2385


View Profile
June 11, 2010, 04:49:36 PM
 #30

I don't think you can do that with iptables.. is there some module that lets you match a locally generated packet against the UID of a process?

BC: 157fRrqAKrDyGHr1Bx3yDxeMv8Rh45aUet
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5390
Merit: 13427


View Profile
June 11, 2010, 05:06:59 PM
 #31

iptables -A OUTPUT -o lo -p tcp --dport 8332 -m owner --uid-owner root -j ACCEPT
iptables -A OUTPUT -o lo -p tcp --dport 8332 -j REJECT

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
lachesis
Full Member
***
Offline Offline

Activity: 210
Merit: 105


View Profile
June 11, 2010, 06:15:40 PM
 #32

iptables -A OUTPUT -o lo -p tcp --dport 8332 -m owner --uid-owner root -j ACCEPT
iptables -A OUTPUT -o lo -p tcp --dport 8332 -j REJECT
Oh sweet. I didn't know you could do that. Setting the UID to my username will block the packet for everyone but me (and root, obviously), right?
Not working for me. It gives the error:
Code:
iptables: No chain/target/match by that name.

Bitcoin Calculator | Scallion | GPG Key | WoT Rating | 1QGacAtYA7E8V3BAiM7sgvLg7PZHk5WnYc
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5390
Merit: 13427


View Profile
June 11, 2010, 06:56:41 PM
 #33

Try loading the "xt_owner" kernel module.

Quote
Setting the UID to my username will block the packet for everyone but me (and root, obviously), right?

Yes. Root will also be blocked unless you add that user as well.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
lachesis
Full Member
***
Offline Offline

Activity: 210
Merit: 105


View Profile
June 11, 2010, 07:22:14 PM
 #34

Code:
FATAL: Module xt_owner not found.
It's not a big problem at the moment. I'd still like to see authentication and wallet encryption in bitcoin in the future. Also, if I were root, couldn't I just
sudo su - <USERNAME>
and evade the owner check in iptables?

Bitcoin Calculator | Scallion | GPG Key | WoT Rating | 1QGacAtYA7E8V3BAiM7sgvLg7PZHk5WnYc
theymos
Administrator
Legendary
*
Offline Offline

Activity: 5390
Merit: 13427


View Profile
June 11, 2010, 07:55:33 PM
 #35

You need to recompile your kernel with support for "owner match support" for netfilter. I was always running into this problem (wanting exotic iptables filters), so I have all of the netfilter modules enabled, even though most of them seem useless to me right now.

I agree that proper authentication would be good, but it's not very important right now. Very few people are running BitCoin on actual multi-user machines, I think.

Quote
if I were root, couldn't I just...

Of course. But packets that originate from the "root" account will still be blocked unless you remove the iptables rule.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
sirius
Bitcoiner
Sr. Member
****
Offline Offline

Activity: 429
Merit: 1002



View Profile
June 14, 2010, 07:16:28 PM
 #36

Note: Running the server/daemon on a computer that you use for Internet browsing is unsafe. A malicious page could host javascript that calls your local API and sends your coins out.

Iris — for better social networks
I'm not a forum admin - please contact theymos instead.
laszlo
Full Member
***
Offline Offline

Activity: 199
Merit: 2385


View Profile
June 14, 2010, 08:14:29 PM
 #37

Is this really true for modern web browsers, or would the web page have to be loaded from localhost as well?  Can you make an HTTP call from javascript to the localhost if the page was loaded from an internet host?

BC: 157fRrqAKrDyGHr1Bx3yDxeMv8Rh45aUet
sirius
Bitcoiner
Sr. Member
****
Offline Offline

Activity: 429
Merit: 1002



View Profile
June 14, 2010, 10:26:45 PM
 #38

Here's an article about cross domain scripting: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

It seems that direct remote calling from javascript might not be possible after all. I wonder if calling localhost:8332 is possible from pages stored on your hard disk.

Iris — for better social networks
I'm not a forum admin - please contact theymos instead.
dwdollar
Full Member
***
Offline Offline

Activity: 202
Merit: 109


GCC - Global cryptocurrency


View Profile WWW
June 15, 2010, 02:25:58 AM
Last edit: June 15, 2010, 02:52:21 AM by dwdollar
 #39

Here's an article about cross domain scripting: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

It seems that direct remote calling from javascript might not be possible after all. I wonder if calling localhost:8332 is possible from pages stored on your hard disk.

I didn't try that, but I did try two servers on the same machine.  One on 127.0.0.1:8332 and the other on 127.0.0.1:8080.  Neither browser script could make a request to the opposite server.  I was using Firefox 3.6

krepta3000
Member
**
Offline Offline

Activity: 92
Merit: 10


View Profile
June 26, 2011, 02:47:21 PM
 #40

Look at rpc.cpp for the list of commands.

Seriously?  So, everyone knows C++ but me?  I don't understand where this list of commands is in this file.  *sigh* I have tried to understand C++ for years, things just are not clicking in my head for some reason.  Do you mean this stuff?
Code:
        //
        // Special case non-string parameter types
        //
        if (strMethod == "setgenerate"            && n > 0) ConvertTo<bool>(params[0]);
        if (strMethod == "setgenerate"            && n > 1) ConvertTo<boost::int64_t>(params[1]);
        if (strMethod == "sendtoaddress"          && n > 1) ConvertTo<double>(params[1]);
        if (strMethod == "settxfee"               && n > 0) ConvertTo<double>(params[0]);
        if (strMethod == "getamountreceived"      && n > 1) ConvertTo<boost::int64_t>(params[1]); // deprecated
        if (strMethod == "getreceivedbyaddress"   && n > 1) ConvertTo<boost::int64_t>(params[1]);
        if (strMethod == "getreceivedbyaccount"   && n > 1) ConvertTo<boost::int64_t>(params[1]);
        if (strMethod == "getreceivedbylabel"     && n > 1) ConvertTo<boost::int64_t>(params[1]); // deprecated
        if (strMethod == "getallreceived"         && n > 0) ConvertTo<boost::int64_t>(params[0]); // deprecated
        if (strMethod == "getallreceived"         && n > 1) ConvertTo<bool>(params[1]);
        if (strMethod == "listreceivedbyaddress"  && n > 0) ConvertTo<boost::int64_t>(params[0]);
        if (strMethod == "listreceivedbyaddress"  && n > 1) ConvertTo<bool>(params[1]);
        if (strMethod == "listreceivedbyaccount"  && n > 0) ConvertTo<boost::int64_t>(params[0]);
        if (strMethod == "listreceivedbyaccount"  && n > 1) ConvertTo<bool>(params[1]);
        if (strMethod == "listreceivedbylabel"    && n > 0) ConvertTo<boost::int64_t>(params[0]); // deprecated
        if (strMethod == "listreceivedbylabel"    && n > 1) ConvertTo<bool>(params[1]); // deprecated
        if (strMethod == "getbalance"             && n > 1) ConvertTo<boost::int64_t>(params[1]);
        if (strMethod == "move"                   && n > 2) ConvertTo<double>(params[2]);
        if (strMethod == "move"                   && n > 3) ConvertTo<boost::int64_t>(params[3]);
        if (strMethod == "sendfrom"               && n > 2) ConvertTo<double>(params[2]);
        if (strMethod == "sendfrom"               && n > 3) ConvertTo<boost::int64_t>(params[3]);
        if (strMethod == "listtransactions"       && n > 1) ConvertTo<boost::int64_t>(params[1]);
        if (strMethod == "listtransactions"       && n > 2) ConvertTo<boost::int64_t>(params[2]);
        if (strMethod == "listaccounts"           && n > 0) ConvertTo<boost::int64_t>(params[0]);
        if (strMethod == "sendmany"               && n > 1)
        {
            string s = params[1].get_str();
            Value v;
            if (!read_string(s, v) || v.type() != obj_type)
                throw runtime_error("type mismatch");
            params[1] = v.get_obj();
        }
        if (strMethod == "sendmany"                && n > 2) ConvertTo<boost::int64_t>(params[2]);

Well, there are comments throughout, so that's good, I really get annoyed by spaghetti code.
Pages: « 1 [2] 3 »  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!