ramontayag-bitcoin-client 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. data/.gitignore +4 -0
  2. data/.travis.yml +8 -0
  3. data/Gemfile +13 -0
  4. data/README.rdoc +88 -0
  5. data/Rakefile +8 -0
  6. data/bitcoind-client.gemspec +28 -0
  7. data/lib/bitcoin-client.rb +1 -0
  8. data/lib/bitcoin.rb +19 -0
  9. data/lib/bitcoin/api.rb +33 -0
  10. data/lib/bitcoin/client.rb +244 -0
  11. data/lib/bitcoin/dsl.rb +259 -0
  12. data/lib/bitcoin/errors.rb +4 -0
  13. data/lib/bitcoin/request.rb +35 -0
  14. data/lib/bitcoin/rpc.rb +50 -0
  15. data/lib/bitcoin/version.rb +12 -0
  16. data/spec/fixtures/backupwallet_without_params.json +8 -0
  17. data/spec/fixtures/build_fixture.rb +19 -0
  18. data/spec/fixtures/getbalance.json +8 -0
  19. data/spec/fixtures/getblockcount.json +8 -0
  20. data/spec/fixtures/getblocknumber.json +8 -0
  21. data/spec/fixtures/getconnectioncount.json +8 -0
  22. data/spec/fixtures/getdifficulty.json +8 -0
  23. data/spec/fixtures/getgenerate.json +8 -0
  24. data/spec/fixtures/gethashespersec.json +8 -0
  25. data/spec/fixtures/getinfo.json +8 -0
  26. data/spec/fixtures/help.json +8 -0
  27. data/spec/fixtures/listreceivedbyaddress_with_minconf_0.json +8 -0
  28. data/spec/fixtures/listreceivedbyaddress_with_minconf_0_and_includeempty_true.json +7 -0
  29. data/spec/fixtures/listreceivedbyaddress_without_params.json +7 -0
  30. data/spec/fixtures/signmessage_invalid_address.json +8 -0
  31. data/spec/fixtures/signmessage_success.json +8 -0
  32. data/spec/fixtures/verifymessage_failure.json +8 -0
  33. data/spec/fixtures/verifymessage_success.json +8 -0
  34. data/spec/lib/bitcoin/api_spec.rb +28 -0
  35. data/spec/lib/bitcoin/client_spec.rb +137 -0
  36. data/spec/lib/bitcoin/request_spec.rb +19 -0
  37. data/spec/lib/bitcoin_spec.rb +34 -0
  38. data/spec/spec_helper.rb +15 -0
  39. data/spec/support/fixtures_helper.rb +5 -0
  40. data/spec/support/rpc_service_helper.rb +34 -0
  41. metadata +182 -0
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
6
+ - jruby
7
+ - ruby-head
8
+ - rbx-2.0
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bitcoind-client.gemspec
4
+ gemspec
5
+
6
+ platforms :ruby_18 do
7
+ gem 'json', '~> 1.5.3'
8
+ end
9
+
10
+ platforms :jruby do
11
+ gem 'json', '~> 1.5.3'
12
+ gem 'jruby-openssl'
13
+ end
@@ -0,0 +1,88 @@
1
+ = bitcoin-client
2
+
3
+ Provides a Ruby library to the complete Bitcoin JSON-RPC API. Implements all methods listed
4
+ at {https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list}[https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list].
5
+ Also supports customizing the host and port number to connect to.
6
+
7
+ == Installation
8
+
9
+ On Ruby 1.9, you can just install the gem and start using it. On 1.8, the 'json' gem is also required, so you'll need to install that first:
10
+
11
+ gem install json
12
+
13
+ Or, if you're using Bundler (and you should be), just add it to the Gemfile:
14
+
15
+ gem 'json', '~> 1.5.3'
16
+
17
+ == Usage
18
+
19
+ As with most Ruby gems, you first need to require the library into your project:
20
+
21
+ require 'bitcoin'
22
+
23
+ After doing this, the simplest possible usage looks like this:
24
+
25
+ Bitcoin('username', 'password').balance
26
+ # => 0.001
27
+
28
+ Or, if you prefer a somewhat more explicit representation, the following code performs the exact
29
+ same task:
30
+
31
+ client = Bitcoin::Client.new('username', 'password')
32
+ client.balance
33
+ # => 0.001
34
+
35
+ The third and final way to use the library is by taking advantage of a simple DSL:
36
+
37
+ include Bitcoin
38
+
39
+ # set up credentials
40
+ username 'username'
41
+ password 'password'
42
+
43
+ balance
44
+ # => 0.001
45
+
46
+ accounts
47
+ # => {"account" => 0.001}
48
+
49
+ The RPC method names available to you are exactly the same as those listed on the Bitcoin wiki
50
+ (again, that's {https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list}[https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list]). Some aliases
51
+ have been added to make them more "ruby-ish," but none of the original names have been changed.
52
+
53
+
54
+ == Host, Port and SSL
55
+
56
+ Here are several examples of how you can change the host information:
57
+
58
+ Bitcoin('username', 'password', :host => 'example.com', :port => 38332, :ssl => true)
59
+
60
+ client = Bitcoin::Client.new('username', 'password', :host => 'example.com')
61
+ client.port = 38332
62
+ client.ssl = true
63
+ client.ssl?
64
+ # => true
65
+
66
+ include Bitcoin
67
+ host 'example.com'
68
+ port 38332
69
+ ssl?
70
+ # => false
71
+ ssl true
72
+ ssl?
73
+ # => true
74
+
75
+ You should see the Bitcoin::Client class documentation if you'd like to see the other options and methods
76
+ that are made available.
77
+
78
+
79
+ == Donations
80
+
81
+ If you found this library useful and feel inclined to compensate me for my trouble, I'm certainly not going to turn you down!
82
+
83
+ Bitcoin donations can be sent to:
84
+
85
+ 1HawYer58J9Vy3iju1w7jsRVci5tzaxkwn
86
+
87
+ Thanks!
88
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run specs.'
5
+ task :default => :spec
6
+
7
+ desc "Run specs"
8
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "bitcoin/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "ramontayag-bitcoin-client"
7
+ s.version = Bitcoin::VERSION
8
+ s.authors = ["Colin MacKenzie IV"]
9
+ s.email = ["sinisterchipmunk@gmail.com"]
10
+ s.homepage = "http://github.com/sinisterchipmunk/bitcoin-client"
11
+ s.summary = %q{Provides a Ruby library to the complete Bitcoin JSON-RPC API.}
12
+ s.description = "Provides a Ruby library to the complete Bitcoin JSON-RPC API. "+
13
+ "Implements all methods listed at "+
14
+ "https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list and "+
15
+ "lets you set options such as the host and port number, and whether to use SSL."
16
+
17
+ s.rubyforge_project = "ramontayag-bitcoin-client"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+
24
+ s.add_development_dependency "rake", '~> 0.8.7'
25
+ s.add_development_dependency "rspec", '~> 2.6.0'
26
+ s.add_development_dependency "fakeweb", '~> 1.3.0'
27
+ s.add_runtime_dependency "rest-client", '~> 1.6.3'
28
+ end
@@ -0,0 +1 @@
1
+ require File.expand_path("bitcoin", File.dirname(__FILE__))
@@ -0,0 +1,19 @@
1
+ module Bitcoin
2
+ autoload :Client, 'bitcoin/client'
3
+ autoload :API, 'bitcoin/api'
4
+ autoload :Request,'bitcoin/request'
5
+ autoload :RPC, 'bitcoin/rpc'
6
+ autoload :Errors, 'bitcoin/errors'
7
+ autoload :Version,'bitcoin/version'
8
+ autoload :VERSION,'bitcoin/version'
9
+ autoload :DSL, 'bitcoin/dsl'
10
+
11
+ def self.included(base)
12
+ base.send(:include, Bitcoin::DSL)
13
+ base.send(:extend, Bitcoin::DSL)
14
+ end
15
+ end
16
+
17
+ def Bitcoin(user, pass, options = {})
18
+ ::Bitcoin::Client.new(user, pass, options)
19
+ end
@@ -0,0 +1,33 @@
1
+ class Bitcoin::API
2
+ attr_reader :options
3
+ attr_reader :params
4
+
5
+ def user; options[:user]; end
6
+ def pass; options[:pass]; end
7
+ def host; options[:host]; end
8
+ def port; options[:port]; end
9
+ def ssl; options[:ssl]; end
10
+ def ssl?; options[:ssl]; end
11
+ def user=(a); options[:user] = a; end
12
+ def pass=(a); options[:pass] = a; end
13
+ def host=(a); options[:host] = a; end
14
+ def port=(a); options[:port] = a; end
15
+ def ssl=(a); options[:ssl] = a; end
16
+
17
+ def initialize(options = {})
18
+ @options = {
19
+ :host => 'localhost',
20
+ :port => 8332,
21
+ :ssl => false
22
+ }.merge(options)
23
+ end
24
+
25
+ def to_hash
26
+ @options.dup
27
+ end
28
+
29
+ def request(service_name, *params)
30
+ req = Bitcoin::Request.new(service_name, params)
31
+ Bitcoin::RPC.new(to_hash).dispatch(req)
32
+ end
33
+ end
@@ -0,0 +1,244 @@
1
+ class Bitcoin::Client
2
+ attr_reader :api
3
+ def user; api.user; end
4
+ def pass; api.pass; end
5
+ def host; api.host; end
6
+ def port; api.port; end
7
+ def ssl; api.ssl; end
8
+ def ssl?; api.ssl?; end
9
+ def user=(a); api.user = a; end
10
+ def pass=(a); api.pass = a; end
11
+ def host=(a); api.host = a; end
12
+ def port=(a); api.port = a; end
13
+ def ssl=(a); api.ssl = a; end
14
+
15
+ def options
16
+ api.options
17
+ end
18
+
19
+ def initialize(user, pass, options = {})
20
+ @api = Bitcoin::API.new({ :user => user, :pass => pass }.merge(options))
21
+ end
22
+
23
+ # Safely copies wallet.dat to destination, which can be a directory or a path with filename.
24
+ def backupwallet(destination)
25
+ @api.request 'backupwallet', destination
26
+ end
27
+
28
+ # Returns the account associated with the given address.
29
+ def getaccount(bitcoinaddress)
30
+ @api.request 'getaccount', bitcoinaddress
31
+ end
32
+
33
+ # Returns the current bitcoin address for receiving payments to this account.
34
+ def getaccountaddress(account)
35
+ @api.request 'getaccountaddress', account
36
+ end
37
+
38
+ # Returns the list of addresses for the given account.
39
+ def getaddressesbyaccount(account)
40
+ @api.request 'getaddressesbyaccount', account
41
+ end
42
+
43
+ # If +account+ is not specified, returns the server's total available balance.
44
+ # If +account+ is specified, returns the balance in the account.
45
+ def getbalance(account = nil, minconf = 1)
46
+ @api.request 'getbalance', account, minconf
47
+ end
48
+
49
+ # Dumps the block existing at specified height.
50
+ # Note: this is not available in the official release
51
+ def getblockbycount(height)
52
+ @api.request 'getblockbycount', height
53
+ end
54
+
55
+ # Returns the number of blocks in the longest block chain.
56
+ def getblockcount
57
+ @api.request 'getblockcount'
58
+ end
59
+
60
+ # Returns the block number of the latest block in the longest block chain.
61
+ def getblocknumber
62
+ @api.request 'getblocknumber'
63
+ end
64
+
65
+ # Returns the number of connections to other nodes.
66
+ def getconnectioncount
67
+ @api.request 'getconnectioncount'
68
+ end
69
+
70
+ # Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
71
+ def getdifficulty
72
+ @api.request 'getdifficulty'
73
+ end
74
+
75
+ # Returns true or false whether bitcoind is currently generating hashes
76
+ def getgenerate
77
+ @api.request 'getgenerate'
78
+ end
79
+
80
+ # Returns a recent hashes per second performance measurement while generating.
81
+ def gethashespersec
82
+ @api.request 'gethashespersec'
83
+ end
84
+
85
+ # Returns an object containing various state info.
86
+ def getinfo
87
+ @api.request 'getinfo'
88
+ end
89
+
90
+ # Returns a new bitcoin address for receiving payments. If +account+ is specified (recommended),
91
+ # it is added to the address book so payments received with the address will be credited to +account+.
92
+ def getnewaddress(account = nil)
93
+ @api.request 'getnewaddress', account
94
+ end
95
+
96
+ # Returns the total amount received by addresses with +account+ in transactions
97
+ # with at least +minconf+ confirmations.
98
+ def getreceivedbyaccount(account, minconf = 1)
99
+ @api.request 'getreceivedbyaccount', account, minconf
100
+ end
101
+
102
+ # Returns the total amount received by +bitcoinaddress+ in transactions with at least +minconf+ confirmations.
103
+ def getreceivedbyaddress(bitcoinaddress, minconf = 1)
104
+ @api.request 'getreceivedbyaddress', bitcoinaddress, minconf
105
+ end
106
+
107
+ # Get detailed information about +txid+
108
+ def gettransaction(txid)
109
+ @api.request 'gettransaction', txid
110
+ end
111
+
112
+ # If +data+ is not specified, returns formatted hash data to work on:
113
+ #
114
+ # :midstate => precomputed hash state after hashing the first half of the data
115
+ # :data => block data
116
+ # :hash1 => formatted hash buffer for second hash
117
+ # :target => little endian hash target
118
+ #
119
+ # If +data+ is specified, tries to solve the block and returns true if it was successful.
120
+ def getwork(data = nil)
121
+ @api.request 'getwork', data
122
+ end
123
+
124
+ # List commands, or get help for a command.
125
+ def help(command = nil)
126
+ @api.request 'help', command
127
+ end
128
+
129
+ # Returns Object that has account names as keys, account balances as values.
130
+ def listaccounts(minconf = 1)
131
+ @api.request 'listaccounts', minconf
132
+ end
133
+
134
+ # Returns an array of objects containing:
135
+ #
136
+ # :account => the account of the receiving addresses
137
+ # :amount => total amount received by addresses with this account
138
+ # :confirmations => number of confirmations of the most recent transaction included
139
+ #
140
+ def listreceivedbyaccount(minconf = 1, includeempty = false)
141
+ @api.request 'listreceivedbyaccount', minconf, includeempty
142
+ end
143
+
144
+ # Returns an array of objects containing:
145
+ #
146
+ # :address => receiving address
147
+ # :account => the account of the receiving address
148
+ # :amount => total amount received by the address
149
+ # :confirmations => number of confirmations of the most recent transaction included
150
+ #
151
+ # To get a list of accounts on the system, execute bitcoind listreceivedbyaddress 0 true
152
+ def listreceivedbyaddress(minconf = 1, includeempty = false)
153
+ @api.request 'listreceivedbyaddress', minconf, includeempty
154
+ end
155
+
156
+ # Returns up to +count+ most recent transactions for account +account+.
157
+ def listtransactions(account, count = 10)
158
+ @api.request 'listtransactions', account, count
159
+ end
160
+
161
+ # Move from one account in your wallet to another.
162
+ def move(fromaccount, toaccount, amount, minconf = 1, comment = nil)
163
+ @api.request 'move', fromaccount, toaccount, amount, minconf, comment
164
+ end
165
+
166
+ # +amount+ is a real and is rounded to 8 decimal places. Returns the transaction ID if successful.
167
+ def sendfrom(fromaccount, tobitcoinaddress, amount, minconf = 1, comment = nil, comment_to = nil)
168
+ @api.request 'sendfrom', fromaccount, tobitcoinaddress, amount, minconf, comment, comment_to
169
+ end
170
+
171
+ # +amount+ is a real and is rounded to 8 decimal places
172
+ def sendtoaddress(bitcoinaddress, amount, comment = nil, comment_to = nil)
173
+ @api.request 'sendtoaddress', bitcoinaddress, amount, comment, comment_to
174
+ end
175
+
176
+ def sendmany(fromaccount, addresses_amounts, minconf = 1, comment = nil)
177
+ @api.request 'sendmany', fromaccount, addresses_amounts, minconf, comment
178
+ end
179
+
180
+ # Sets the account associated with the given address.
181
+ def setaccount(bitcoinaddress, account)
182
+ @api.request 'setaccoint', bitcoinaddress, account
183
+ end
184
+
185
+ # +generate+ is true or false to turn generation on or off.
186
+ # Generation is limited to +genproclimit+ processors, -1 is unlimited.
187
+ def setgenerate(generate, genproclimit = -1)
188
+ @api.request 'setgenerate', generate, genproclimit
189
+ end
190
+
191
+ # Stop bitcoin server.
192
+ def stop
193
+ @api.request 'stop'
194
+ end
195
+
196
+ # Return information about +bitcoinaddress+.
197
+ def validateaddress(bitcoinaddress)
198
+ @api.request 'validateaddress', bitcoinaddress
199
+ end
200
+
201
+ # Sign a message using +bitcoinaddress+.
202
+ def signmessage(bitcoinaddress, message)
203
+ @api.request 'signmessage', bitcoinaddress, message
204
+ end
205
+
206
+ # Verify signature made by +bitcoinaddress+.
207
+ def verifymessage(bitcoinaddress, signature, message)
208
+ @api.request 'verifymessage', bitcoinaddress, signature, message
209
+ end
210
+
211
+ alias account getaccount
212
+ alias account_address getaccountaddress
213
+ alias addresses_by_account getaddressesbyaccount
214
+ alias balance getbalance
215
+ alias block_by_count getblockbycount
216
+ alias block_count getblockcount
217
+ alias block_number getblocknumber
218
+ alias connection_count getconnectioncount
219
+ alias difficulty getdifficulty
220
+ alias generate? getgenerate
221
+ alias hashes_per_sec gethashespersec
222
+ alias info getinfo
223
+ alias new_address getnewaddress
224
+ alias received_by_account getreceivedbyaccount
225
+ alias received_by_address getreceivedbyaddress
226
+ alias transaction gettransaction
227
+ alias work getwork
228
+ alias get_work getwork
229
+ alias accounts listaccounts
230
+ alias list_received_by_account listreceivedbyaccount
231
+ alias list_received_by_address listreceivedbyaddress
232
+ alias transactions listtransactions
233
+ alias list_transactions listtransactions
234
+ alias send_from sendfrom
235
+ alias send_to_address sendtoaddress
236
+ alias send_many sendmany
237
+ alias account= setaccount
238
+ alias set_account setaccount
239
+ alias generate= setgenerate
240
+ alias set_generate setgenerate
241
+ alias validate_address validateaddress
242
+ alias sign_message signmessage
243
+ alias verify_message verifymessage
244
+ end