ramontayag-bitcoin-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
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,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 22:25:42 +0000
3
+ Connection: close
4
+ Content-Length: 42
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/0.3.24-beta
7
+
8
+ {"result":0,"error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 20:24:03 +0000
3
+ Connection: close
4
+ Content-Length: 281
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/0.3.24-beta
7
+
8
+ {"result":{"version":32400,"balance":0.00100000,"blocks":141957,"connections":8,"proxy":"","generate":false,"genproclimit":-1,"difficulty":1805700.83619367,"hashespersec":0,"testnet":false,"keypoololdest":1313766189,"paytxfee":0.00000000,"errors":""},"error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 23:20:10 +0000
3
+ Connection: close
4
+ Content-Length: 1078
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/0.3.24-beta
7
+
8
+ {"result":"backupwallet <destination>\ngetaccount <bitcoinaddress>\ngetaccountaddress <account>\ngetaddressesbyaccount <account>\ngetbalance [account] [minconf=1]\ngetblockcount\ngetblocknumber\ngetconnectioncount\ngetdifficulty\ngetgenerate\ngethashespersec\ngetinfo\ngetnewaddress [account]\ngetreceivedbyaccount <account> [minconf=1]\ngetreceivedbyaddress <bitcoinaddress> [minconf=1]\ngettransaction <txid>\ngetwork [data]\nhelp [command]\nlistaccounts [minconf=1]\nlistreceivedbyaccount [minconf=1] [includeempty=false]\nlistreceivedbyaddress [minconf=1] [includeempty=false]\nlisttransactions [account] [count=10] [from=0]\nmove <fromaccount> <toaccount> <amount> [minconf=1] [comment]\nsendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to]\nsendmany <fromaccount> {address:amount,...} [minconf=1] [comment]\nsendtoaddress <bitcoinaddress> <amount> [comment] [comment-to]\nsetaccount <bitcoinaddress> <account>\nsetgenerate <generate> [genproclimit]\nsettxfee <amount>\nstop\nvalidateaddress <bitcoinaddress>","error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 23:04:42 +0000
3
+ Connection: close
4
+ Content-Length: 155
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/0.3.24-beta
7
+
8
+ {"result":[{"address":"1234","account":"","label":"","amount":0.00100000,"confirmations":180}],"error":null,"id":"curltest"}
@@ -0,0 +1,7 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 23:15:11 +0000
3
+ Connection: close
4
+ Content-Type: application/json
5
+ Server: bitcoin-json-rpc/0.3.24-beta
6
+
7
+ {"result":[{"address":"1234","account":"","label":"","amount":0.00100000,"confirmations":180}],"error":null,"id":"curltest"}
@@ -0,0 +1,7 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 21 Aug 2011 22:54:36 +0000
3
+ Connection: close
4
+ Content-Type: application/json
5
+ Server: bitcoin-json-rpc/0.3.24-beta
6
+
7
+ {"result":[{"address":"1234","account":"","label":"","amount":0.00100000,"confirmations":180}],"error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 500 Internal Server Error
2
+ Date: Sun, 26 Aug 2012 23:58:59 +0000
3
+ Connection: close
4
+ Content-Length: 80
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/v0.6.3-beta
7
+
8
+ {"result":null,"error":{"code":-3,"message":"Invalid address"},"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 26 Aug 2012 23:58:30 +0000
3
+ Connection: close
4
+ Content-Length: 131
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/v0.6.3-beta
7
+
8
+ {"result":"Gwz2BAaqdsLTqJsh5a4","error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 26 Aug 2012 23:40:37 +0000
3
+ Connection: close
4
+ Content-Length: 46
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/v0.6.3-beta
7
+
8
+ {"result":false,"error":null,"id":"curltest"}
@@ -0,0 +1,8 @@
1
+ HTTP/1.1 200 OK
2
+ Date: Sun, 26 Aug 2012 23:40:26 +0000
3
+ Connection: close
4
+ Content-Length: 45
5
+ Content-Type: application/json
6
+ Server: bitcoin-json-rpc/v0.6.3-beta
7
+
8
+ {"result":true,"error":null,"id":"curltest"}
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitcoin::API do
4
+ subject { Bitcoin::API.new(:user => $user, :pass => $pass) }
5
+
6
+ it "should have default host, port, ssl" do
7
+ subject.host.should == 'localhost'
8
+ subject.port.should == 8332
9
+ subject.ssl?.should be_false
10
+ end
11
+
12
+ it "should accept host, port, ssl options" do
13
+ req = Bitcoin::API.new(:user => $user, :pass => $pass, :host => 'example.com', :port => 1234, :ssl => true)
14
+ req.host.should == 'example.com'
15
+ req.port.should == 1234
16
+ req.ssl?.should be_true
17
+ end
18
+
19
+ it "should build an options hash" do
20
+ subject.to_hash.should == {
21
+ :user => $user,
22
+ :pass => $pass,
23
+ :host => 'localhost',
24
+ :port => 8332,
25
+ :ssl => false,
26
+ }
27
+ end
28
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitcoin::Client do
4
+ subject { Bitcoin::Client.new($user, $pass) }
5
+
6
+ it "defaults" do
7
+ subject.user.should == $user
8
+ subject.pass.should == $pass
9
+ subject.host.should == 'localhost'
10
+ subject.port.should == 8332
11
+ subject.ssl?.should_not be_true
12
+ end
13
+
14
+ context "RPC" do
15
+ extend RPCServiceHelper
16
+
17
+ service 'getinfo' do
18
+ it "should produce the expected result" do
19
+ result.should == {
20
+ 'version' => 32400,
21
+ 'balance' => 0.001,
22
+ 'blocks' => 141957,
23
+ 'connections' => 8,
24
+ 'proxy' => "",
25
+ 'generate' => false,
26
+ 'genproclimit' => -1,
27
+ 'difficulty' => 1805700.83619367,
28
+ 'hashespersec' => 0,
29
+ 'testnet' => false,
30
+ 'keypoololdest' => 1313766189,
31
+ 'paytxfee' => 0.0,
32
+ 'errors' => ""
33
+ }
34
+ end
35
+ end
36
+
37
+ service 'getblockcount' do
38
+ it "should produce the expected result" do
39
+ result.should == 141972
40
+ end
41
+ end
42
+
43
+ service 'getblocknumber' do
44
+ it "should produce the expected result" do
45
+ result.should == 141972
46
+ end
47
+ end
48
+
49
+ service 'getconnectioncount' do
50
+ it "should produce the expected result" do
51
+ result.should == 8
52
+ end
53
+ end
54
+
55
+ service 'getdifficulty' do
56
+ it "should produce the expected result" do
57
+ result.should == 1805700.83619367
58
+ end
59
+ end
60
+
61
+ service 'getgenerate' do
62
+ it "should produce the expected result" do
63
+ result.should be_false
64
+ end
65
+ end
66
+
67
+ service 'gethashespersec' do
68
+ it "should produce the expected result" do
69
+ result.should == 0
70
+ end
71
+ end
72
+
73
+ service 'listreceivedbyaddress' do
74
+ context 'without params' do
75
+ it "should produce the expected result" do
76
+ result.should == [{
77
+ 'address' => "1234",
78
+ 'account' => "",
79
+ 'label' => "",
80
+ 'amount' => 0.001,
81
+ 'confirmations' => 180}]
82
+ end
83
+ end
84
+
85
+ context 'with minconf 0' do
86
+ it "should produce the expected result" do
87
+ result(0).should == [{
88
+ 'address' => "1234",
89
+ 'account' => "",
90
+ 'label' => "",
91
+ 'amount' => 0.001,
92
+ 'confirmations' => 180}]
93
+ end
94
+ end
95
+
96
+ context 'with minconf 0 and includeempty true' do
97
+ it "should produce the expected result" do
98
+ result(0, true).should == [{
99
+ 'address' => "1234",
100
+ 'account' => "",
101
+ 'label' => "",
102
+ 'amount' => 0.001,
103
+ 'confirmations' => 180}]
104
+ end
105
+ end
106
+ end
107
+
108
+ service 'signmessage' do
109
+ context 'success' do
110
+ it "should produce the expected result" do
111
+ result('valid_address', 'message').should == 'Gwz2BAaqdsLTqJsh5a4'
112
+ end
113
+ end
114
+
115
+ context 'invalid address' do
116
+ it "should produce the expected result" do
117
+ lambda { result('invalid_address', 'message').should }.should \
118
+ raise_error RestClient::InternalServerError
119
+ end
120
+ end
121
+ end
122
+
123
+ service 'verifymessage' do
124
+ context 'success' do
125
+ it "should produce the expected result" do
126
+ result('address', 'message', 'signature').should be_true
127
+ end
128
+ end
129
+
130
+ context 'failure' do
131
+ it "should produce the expected result" do
132
+ result('address', 'message', 'signature').should be_false
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitcoin::Request do
4
+ it "should omit null arguments and everything after them" do
5
+ # bitcoin rejects null values even for optional params. Since
6
+ # even params following those may have default non-nil values,
7
+ # we'll assume the first non-nil value marks a set of optional
8
+ # params, and drop it and everything following it.
9
+
10
+ req = Bitcoin::Request.new('svc', [1, nil, nil, nil])
11
+ req.params.should == [1]
12
+
13
+ req = Bitcoin::Request.new('svc', [nil])
14
+ req.params.should == []
15
+
16
+ req = Bitcoin::Request.new('svc', [1, nil, nil, 1, nil])
17
+ req.params.should == [1]
18
+ end
19
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Bitcoin do
4
+ before :each do
5
+ FakeWeb.register_uri(:post, "http://user:pass@localhost:8332", :response => fixture('getbalance'))
6
+ end
7
+
8
+ it "as a function" do
9
+ cli = Bitcoin($user, $pass)
10
+ cli.balance.should == 0.001
11
+ end
12
+
13
+ it "DSL, included" do
14
+ class << self
15
+ include Bitcoin
16
+ end
17
+
18
+ username $user
19
+ password $pass
20
+
21
+ balance.should == 0.001
22
+ end
23
+
24
+ it "DSL, extended" do
25
+ class << self
26
+ include Bitcoin
27
+
28
+ username $user
29
+ password $pass
30
+
31
+ balance.should == 0.001
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ require 'fakeweb'
2
+
3
+ # bitcoin user settings
4
+ $user = 'user'
5
+ $pass = 'pass'
6
+
7
+ require File.expand_path('../lib/bitcoin-client', File.dirname(__FILE__))
8
+
9
+ Dir[File.expand_path("support/**/*.rb", File.dirname(__FILE__))].each { |f| require f }
10
+
11
+ FakeWeb.allow_net_connect = false
12
+
13
+ RSpec.configure do |c|
14
+ c.include FixturesHelper
15
+ end
@@ -0,0 +1,5 @@
1
+ module FixturesHelper
2
+ def fixture(name)
3
+ File.read(File.expand_path("../fixtures/#{name}.json", File.dirname(__FILE__)))
4
+ end
5
+ end
@@ -0,0 +1,34 @@
1
+ module RPCServiceHelper
2
+ def service(name, &block)
3
+ context "'#{name}'" do
4
+ define_method :fixture_name do
5
+ suffix = self.class.fixture_suffix.gsub(/\s/, '_')
6
+ if suffix.length > 0
7
+ "#{name}_#{suffix}"
8
+ else
9
+ name
10
+ end
11
+ end
12
+
13
+ define_method :result do |*args|
14
+ FakeWeb.register_uri(:post, "http://user:pass@localhost:8332", :response => fixture(fixture_name))
15
+ subject.send(name, *args)
16
+ end
17
+
18
+ class << self
19
+ def fixture_suffix
20
+ @fixture_suffix ||= ""
21
+ end
22
+
23
+ def context(desc, &block)
24
+ super desc do
25
+ fixture_suffix.concat desc
26
+ instance_eval &block
27
+ end
28
+ end
29
+ end
30
+
31
+ instance_eval &block
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ramontayag-bitcoin-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin MacKenzie IV
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.7
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.7
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.6.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: fakeweb
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.3.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: rest-client
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.6.3
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.6.3
78
+ description: Provides a Ruby library to the complete Bitcoin JSON-RPC API. Implements
79
+ all methods listed at https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_Calls_list
80
+ and lets you set options such as the host and port number, and whether to use SSL.
81
+ email:
82
+ - sinisterchipmunk@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .travis.yml
89
+ - Gemfile
90
+ - README.rdoc
91
+ - Rakefile
92
+ - bitcoind-client.gemspec
93
+ - lib/bitcoin-client.rb
94
+ - lib/bitcoin.rb
95
+ - lib/bitcoin/api.rb
96
+ - lib/bitcoin/client.rb
97
+ - lib/bitcoin/dsl.rb
98
+ - lib/bitcoin/errors.rb
99
+ - lib/bitcoin/request.rb
100
+ - lib/bitcoin/rpc.rb
101
+ - lib/bitcoin/version.rb
102
+ - spec/fixtures/backupwallet_without_params.json
103
+ - spec/fixtures/build_fixture.rb
104
+ - spec/fixtures/getbalance.json
105
+ - spec/fixtures/getblockcount.json
106
+ - spec/fixtures/getblocknumber.json
107
+ - spec/fixtures/getconnectioncount.json
108
+ - spec/fixtures/getdifficulty.json
109
+ - spec/fixtures/getgenerate.json
110
+ - spec/fixtures/gethashespersec.json
111
+ - spec/fixtures/getinfo.json
112
+ - spec/fixtures/help.json
113
+ - spec/fixtures/listreceivedbyaddress_with_minconf_0.json
114
+ - spec/fixtures/listreceivedbyaddress_with_minconf_0_and_includeempty_true.json
115
+ - spec/fixtures/listreceivedbyaddress_without_params.json
116
+ - spec/fixtures/signmessage_invalid_address.json
117
+ - spec/fixtures/signmessage_success.json
118
+ - spec/fixtures/verifymessage_failure.json
119
+ - spec/fixtures/verifymessage_success.json
120
+ - spec/lib/bitcoin/api_spec.rb
121
+ - spec/lib/bitcoin/client_spec.rb
122
+ - spec/lib/bitcoin/request_spec.rb
123
+ - spec/lib/bitcoin_spec.rb
124
+ - spec/spec_helper.rb
125
+ - spec/support/fixtures_helper.rb
126
+ - spec/support/rpc_service_helper.rb
127
+ homepage: http://github.com/sinisterchipmunk/bitcoin-client
128
+ licenses: []
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: -2184310591258243336
142
+ required_rubygems_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ! '>='
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ segments:
149
+ - 0
150
+ hash: -2184310591258243336
151
+ requirements: []
152
+ rubyforge_project: ramontayag-bitcoin-client
153
+ rubygems_version: 1.8.25
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: Provides a Ruby library to the complete Bitcoin JSON-RPC API.
157
+ test_files:
158
+ - spec/fixtures/backupwallet_without_params.json
159
+ - spec/fixtures/build_fixture.rb
160
+ - spec/fixtures/getbalance.json
161
+ - spec/fixtures/getblockcount.json
162
+ - spec/fixtures/getblocknumber.json
163
+ - spec/fixtures/getconnectioncount.json
164
+ - spec/fixtures/getdifficulty.json
165
+ - spec/fixtures/getgenerate.json
166
+ - spec/fixtures/gethashespersec.json
167
+ - spec/fixtures/getinfo.json
168
+ - spec/fixtures/help.json
169
+ - spec/fixtures/listreceivedbyaddress_with_minconf_0.json
170
+ - spec/fixtures/listreceivedbyaddress_with_minconf_0_and_includeempty_true.json
171
+ - spec/fixtures/listreceivedbyaddress_without_params.json
172
+ - spec/fixtures/signmessage_invalid_address.json
173
+ - spec/fixtures/signmessage_success.json
174
+ - spec/fixtures/verifymessage_failure.json
175
+ - spec/fixtures/verifymessage_success.json
176
+ - spec/lib/bitcoin/api_spec.rb
177
+ - spec/lib/bitcoin/client_spec.rb
178
+ - spec/lib/bitcoin/request_spec.rb
179
+ - spec/lib/bitcoin_spec.rb
180
+ - spec/spec_helper.rb
181
+ - spec/support/fixtures_helper.rb
182
+ - spec/support/rpc_service_helper.rb