Skip to content

Instantly share code, notes, and snippets.

@osyoyu
Created January 21, 2014 06:26
Show Gist options
  • Save osyoyu/8535279 to your computer and use it in GitHub Desktop.
Save osyoyu/8535279 to your computer and use it in GitHub Desktop.
Bitcoin JSON-RPC sample fails to connect to monacoind (?) https://en.bitcoin.it/wiki/API_reference_%28JSON-RPC%29#Ruby
require 'net/http'
require 'uri'
require 'json'
class BitcoinRPC
def initialize(service_url)
@uri = URI.parse(service_url)
end
def method_missing(name, *args)
post_body = { 'method' => name, 'params' => args, 'id' => 'jsonrpc' }.to_json
resp = JSON.parse( http_post_request(post_body) )
raise JSONRPCError, resp['error'] if resp['error']
resp['result']
end
def http_post_request(post_body)
http = Net::HTTP.new(@uri.host, @uri.port)
http.use_ssl = false
request = Net::HTTP::Post.new(@uri.request_uri)
request.basic_auth( @uri.user, @uri.password)
request.content_type = 'application/json'
request.body = post_body
http.request(request).body
end
class JSONRPCError < RuntimeError; end
end
if $0 == __FILE__
h = BitcoinRPC.new('http://monacoinrpc:DytCKSu2kwwbgWAFQqhV7rEb3KxoT6d31Z7DThpEjt2F@127.0.0.1:9401')
p h.getbalance
p h.getinfo
# also see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
end
/home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': end of file reached (EOFError)
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/protocol.rb:153:in `rbuf_fill'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http/response.rb:39:in `read_status_line'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http/response.rb:28:in `read_new'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1406:in `block in transport_request'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1369:in `block in request'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /home/osyoyu/.rbenv/versions/2.0.0-p353/lib/ruby/2.0.0/net/http.rb:1367:in `request'
from monacoin_rpc_orig.rb:24:in `http_post_request'
from monacoin_rpc_orig.rb:12:in `method_missing'
from monacoin_rpc_orig.rb:32:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment