Created
August 3, 2020 01:22
-
-
Save nnzo/b67c31dbde8bd847c84a54947cdcbaa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
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://user:[email protected]:8332') | |
p h.getbalance | |
p h.getblockchaininfo | |
p h.getnewaddress | |
p h.dumpprivkey( h.getnewaddress ) | |
# also see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment