startcoin-client 0.0.3

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +18 -0
  4. data/Gemfile +13 -0
  5. data/README.rdoc +78 -0
  6. data/Rakefile +8 -0
  7. data/lib/startcoin_client/api.rb +33 -0
  8. data/lib/startcoin_client/client.rb +340 -0
  9. data/lib/startcoin_client/dsl.rb +307 -0
  10. data/lib/startcoin_client/errors.rb +4 -0
  11. data/lib/startcoin_client/request.rb +35 -0
  12. data/lib/startcoin_client/rpc.rb +51 -0
  13. data/lib/startcoin_client/version.rb +12 -0
  14. data/lib/startcoin_client.rb +19 -0
  15. data/spec/fixtures/backupwallet_without_params.json +8 -0
  16. data/spec/fixtures/build_fixture.rb +19 -0
  17. data/spec/fixtures/getbalance.json +8 -0
  18. data/spec/fixtures/getbestblockhash.json +8 -0
  19. data/spec/fixtures/getblock.json +8 -0
  20. data/spec/fixtures/getblockcount.json +8 -0
  21. data/spec/fixtures/getblocknumber.json +8 -0
  22. data/spec/fixtures/getconnectioncount.json +8 -0
  23. data/spec/fixtures/getdifficulty.json +8 -0
  24. data/spec/fixtures/getgenerate.json +8 -0
  25. data/spec/fixtures/gethashespersec.json +8 -0
  26. data/spec/fixtures/getinfo.json +8 -0
  27. data/spec/fixtures/getmininginfo.json +8 -0
  28. data/spec/fixtures/help.json +8 -0
  29. data/spec/fixtures/listreceivedbyaddress_with_minconf_0.json +8 -0
  30. data/spec/fixtures/listreceivedbyaddress_with_minconf_0_and_includeempty_true.json +7 -0
  31. data/spec/fixtures/listreceivedbyaddress_without_params.json +7 -0
  32. data/spec/fixtures/setaccount.json +8 -0
  33. data/spec/fixtures/signmessage_invalid_address.json +8 -0
  34. data/spec/fixtures/signmessage_success.json +8 -0
  35. data/spec/fixtures/verifymessage_failure.json +8 -0
  36. data/spec/fixtures/verifymessage_success.json +8 -0
  37. data/spec/lib/startcoin_client/api_spec.rb +28 -0
  38. data/spec/lib/startcoin_client/client_spec.rb +184 -0
  39. data/spec/lib/startcoin_client/request_spec.rb +19 -0
  40. data/spec/lib/startcoin_client_spec.rb +34 -0
  41. data/spec/spec_helper.rb +18 -0
  42. data/spec/support/fixtures_helper.rb +5 -0
  43. data/spec/support/rpc_service_helper.rb +34 -0
  44. data/startcoin-client.gemspec +32 -0
  45. metadata +205 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 795bc699e065fa6a9ef2b4b24ffdd06448099889
4
+ data.tar.gz: 941517fb97e1e0229345cf17c7f36210c2d62230
5
+ SHA512:
6
+ metadata.gz: 05261fec773f80989ffa655f633a446e8a9baaa86890b1cc047bf82b4e30c3d7d0ebc39ab0a7921520280bfd1ed4bfbf60414def3364dacc2d10138380121f28
7
+ data.tar.gz: 76372fc5b7f77eae5fe5684022efbd643044307732d0532900306b968b5b477a4535f45667733e54d439bff7fb8cb66ae8921999bae8ab79c7c00384cd45e30c
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ coverage
6
+ *~
7
+
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - ree
7
+ - jruby
8
+ - ruby-head
9
+ - rbx-2.0
10
+
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: rbx-2.0
14
+ - rvm: jruby
15
+ - rvm: ruby-head
16
+ - rvm: 1.8.7
17
+ - rvm: ree
18
+ - rvm: 1.9.2
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
data/README.rdoc ADDED
@@ -0,0 +1,78 @@
1
+ = startcoin-client
2
+
3
+ Provides a Ruby library to the complete Startcoin 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_Startcoin_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 'bitcon_client'
22
+
23
+ After doing this, the simplest possible usage looks like this:
24
+
25
+ StartcoinClient('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 = StartcoinClient::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 StartcoinClient
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 Startcoin wiki
50
+ (again, that's {https://en.bitcoin.it/wiki/Original_Startcoin_client/API_Calls_list}[https://en.bitcoin.it/wiki/Original_Startcoin_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
+ StartcoinClient('username', 'password', :host => 'example.com', :port => 38332, :ssl => true)
59
+
60
+ client = StartcoinClient::Client.new('username', 'password', :host => 'example.com')
61
+ client.port = 38332
62
+ client.ssl = true
63
+ client.ssl?
64
+ # => true
65
+
66
+ include StartcoinClient
67
+ host 'example.com'
68
+ port 38332
69
+ ssl?
70
+ # => false
71
+ ssl true
72
+ ssl?
73
+ # => true
74
+
75
+ You should see the StartcoinClient::Client class documentation if you'd like to see the other options and methods
76
+ that are made available.
77
+
78
+
data/Rakefile ADDED
@@ -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,33 @@
1
+ class StartcoinClient::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 = StartcoinClient::Request.new(service_name, params)
31
+ StartcoinClient::RPC.new(to_hash).dispatch(req)
32
+ end
33
+ end
@@ -0,0 +1,340 @@
1
+ class StartcoinClient::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 = StartcoinClient::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
+ # Creates a multi-signature address and returns a json object
29
+ def createmultisig(nrequired, keys)
30
+ @api.request 'createmultisig', nrequired, keys
31
+ end
32
+
33
+ # nCreate a transaction spending given inputs
34
+ # (array of objects containing transaction id and output number), sending to given address(es)
35
+ def createrawtransaction(transactionid = nil, address_amount)
36
+ @api.request 'createrawtransaction', transactionid, address_amount
37
+ end
38
+
39
+ # Return a JSON object representing the serialized, hex-encoded transaction.
40
+ def decoderawtransaction(hexstring)
41
+ @api.request 'decoderawtransaction', hexstring
42
+ end
43
+
44
+ # Returns the account associated with the given address.
45
+ def getaccount(bitcoinaddress)
46
+ @api.request 'getaccount', bitcoinaddress
47
+ end
48
+
49
+ # Returns the current bitcoin address for receiving payments to this account.
50
+ def getaccountaddress(account)
51
+ @api.request 'getaccountaddress', account
52
+ end
53
+
54
+ # Returns the list of addresses for the given account.
55
+ def getaddressesbyaccount(account)
56
+ @api.request 'getaddressesbyaccount', account
57
+ end
58
+
59
+ # If +account+ is not specified, returns the server's total available balance.
60
+ # If +account+ is specified, returns the balance in the account.
61
+ def getbalance(account = nil, minconf = 1)
62
+ @api.request 'getbalance', account, minconf
63
+ end
64
+
65
+ def getbestblockhash
66
+ @api.request 'getbestblockhash'
67
+ end
68
+
69
+ # Dumps the block existing at specified height.
70
+ # Note: this is not available in the official release
71
+ def getblockbycount(height)
72
+ @api.request 'getblockbycount', height
73
+ end
74
+
75
+ # Dumps the block existing with specified hash.
76
+ def getblock(hash)
77
+ block = @api.request 'getblock', hash
78
+ block["time"] = Time.at(block["time"]).utc
79
+ block
80
+ end
81
+
82
+ # Returns the number of blocks in the longest block chain.
83
+ def getblockcount
84
+ @api.request 'getblockcount'
85
+ end
86
+
87
+ # Returns the block number of the latest block in the longest block chain.
88
+ def getblocknumber
89
+ @api.request 'getblocknumber'
90
+ end
91
+
92
+ # Returns hash of block in best-block-chain at <index>; index 0 is the genesis block
93
+ def getblockhash(index)
94
+ @api.request 'getblockhash', index
95
+ end
96
+
97
+ # Returns the number of connections to other nodes.
98
+ def getconnectioncount
99
+ @api.request 'getconnectioncount'
100
+ end
101
+
102
+ # Returns the proof-of-work difficulty as a multiple of the minimum difficulty.
103
+ def getdifficulty
104
+ @api.request 'getdifficulty'
105
+ end
106
+
107
+ # Returns true or false whether bitcoind is currently generating hashes
108
+ def getgenerate
109
+ @api.request 'getgenerate'
110
+ end
111
+
112
+ # Returns a recent hashes per second performance measurement while generating.
113
+ def gethashespersec
114
+ @api.request 'gethashespersec'
115
+ end
116
+
117
+ # Returns an object containing various state info.
118
+ def getinfo
119
+ @api.request 'getinfo'
120
+ end
121
+
122
+ # Returns data about each connected network node.
123
+ def getpeerinfo
124
+ @api.request 'getpeerinfo'
125
+ end
126
+
127
+ # Returns an object containing mining info.
128
+ def getmininginfo
129
+ @api.request 'getmininginfo'
130
+ end
131
+
132
+ # Returns a new bitcoin address for receiving payments. If +account+ is specified (recommended),
133
+ # it is added to the address book so payments received with the address will be credited to +account+.
134
+ def getnewaddress(account = nil)
135
+ @api.request 'getnewaddress', account
136
+ end
137
+
138
+ # Returns the total amount received by addresses with +account+ in transactions
139
+ # with at least +minconf+ confirmations.
140
+ def getreceivedbyaccount(account, minconf = 1)
141
+ @api.request 'getreceivedbyaccount', account, minconf
142
+ end
143
+
144
+ # Returns the total amount received by +bitcoinaddress+ in transactions with at least +minconf+ confirmations.
145
+ def getreceivedbyaddress(bitcoinaddress, minconf = 1)
146
+ @api.request 'getreceivedbyaddress', bitcoinaddress, minconf
147
+ end
148
+
149
+ # Get detailed information about +txid+
150
+ def gettransaction(txid)
151
+ @api.request 'gettransaction', txid
152
+ end
153
+
154
+ # Get raw transaction bout +txid+. It outputs the whole transaction chain by default in HEX. If you want JSON, set verbose to 1.
155
+ # When in the bitcoind config is set txindex=1, after reindexing, you can ask about any transaction (not included in your wallet), with this command.
156
+ def getrawtransaction(txid, verbose = 0)
157
+ @api.request 'getrawtransaction', txid, verbose
158
+ end
159
+
160
+ # Gets all mempool txs (pedning/waiting to be added in a block)
161
+ def getrawmempool
162
+ @api.request 'getrawmempool'
163
+ end
164
+ # If +data+ is not specified, returns formatted hash data to work on:
165
+ #
166
+ # :midstate => precomputed hash state after hashing the first half of the data
167
+ # :data => block data
168
+ # :hash1 => formatted hash buffer for second hash
169
+ # :target => little endian hash target
170
+ #
171
+ # If +data+ is specified, tries to solve the block and returns true if it was successful.
172
+ def getwork(data = nil)
173
+ @api.request 'getwork', data
174
+ end
175
+
176
+ # List commands, or get help for a command.
177
+ def help(command = nil)
178
+ @api.request 'help', command
179
+ end
180
+
181
+ # Adds a private key (as returned by dumpprivkey) to your wallet.
182
+ def importprivkey(bitcoinprivkey, label = nil, rescan = true)
183
+ @api.request 'importprivkey', bitcoinprivkey, label, rescan
184
+ end
185
+
186
+ # Returns Object that has account names as keys, account balances as values.
187
+ def listaccounts(minconf = 1)
188
+ @api.request 'listaccounts', minconf
189
+ end
190
+
191
+ # Returns an array of objects containing:
192
+ #
193
+ # :account => the account of the receiving addresses
194
+ # :amount => total amount received by addresses with this account
195
+ # :confirmations => number of confirmations of the most recent transaction included
196
+ #
197
+ def listreceivedbyaccount(minconf = 1, includeempty = false)
198
+ @api.request 'listreceivedbyaccount', minconf, includeempty
199
+ end
200
+
201
+ # Returns an array of objects containing:
202
+ #
203
+ # :address => receiving address
204
+ # :account => the account of the receiving address
205
+ # :amount => total amount received by the address
206
+ # :confirmations => number of confirmations of the most recent transaction included
207
+ #
208
+ # To get a list of accounts on the system, execute bitcoind listreceivedbyaddress 0 true
209
+ def listreceivedbyaddress(minconf = 1, includeempty = false)
210
+ @api.request 'listreceivedbyaddress', minconf, includeempty
211
+ end
212
+
213
+ # Returns up to +count+ most recent transactions for account +account+.
214
+ def listtransactions(account = '' , count = 10, from = 0)
215
+ @api.request 'listtransactions', account, count, from
216
+ end
217
+
218
+ # Returns transactions since <hash> block
219
+ def listsinceblock(hash)
220
+ @api.request 'listsinceblock', hash
221
+ end
222
+
223
+
224
+ # Move from one account in your wallet to another.
225
+ def move(fromaccount, toaccount, amount, minconf = 1, comment = nil)
226
+ @api.request 'move', fromaccount, toaccount, amount, minconf, comment
227
+ end
228
+
229
+ # Return count transactions with <address> present in their scriptSig, skipping skip at the beginning. The ordering is oldest transaction first; if skip is negative the order returned is newest transaction first and skip+1 transactions are skipped. If verbose=0 only txids are returned rather than the full transactions.
230
+ def searchrawtransactions(bitcoinaddress, verbose=1)
231
+ @api.request 'searchrawtransactions', bitcoinaddress, verbose
232
+ end
233
+
234
+ # +amount+ is a real and is rounded to 8 decimal places. Returns the transaction ID if successful.
235
+ def sendfrom(fromaccount, tobitcoinaddress, amount, minconf = 1, comment = nil, comment_to = nil)
236
+ @api.request 'sendfrom', fromaccount, tobitcoinaddress, amount, minconf, comment, comment_to
237
+ end
238
+
239
+ # Submits raw transaction (serialized, hex-encoded) to local node and network.
240
+ def sendrawtransaction(hexstring)
241
+ @api.request 'sendrawtransaction', hexstring
242
+ end
243
+ # +amount+ is a real and is rounded to 8 decimal places
244
+ def sendtoaddress(bitcoinaddress, amount, comment = nil, comment_to = nil)
245
+ @api.request 'sendtoaddress', bitcoinaddress, amount, comment, comment_to
246
+ end
247
+
248
+ def sendmany(fromaccount, addresses_amounts, minconf = 1, comment = nil)
249
+ @api.request 'sendmany', fromaccount, addresses_amounts, minconf, comment
250
+ end
251
+
252
+ # Sets the account associated with the given address.
253
+ def setaccount(bitcoinaddress, account)
254
+ @api.request 'setaccount', bitcoinaddress, account
255
+ end
256
+
257
+ # +generate+ is true or false to turn generation on or off.
258
+ # Generation is limited to +genproclimit+ processors, -1 is unlimited.
259
+ def setgenerate(generate, genproclimit = -1)
260
+ @api.request 'setgenerate', generate, genproclimit
261
+ end
262
+
263
+ # Sign inputs for raw transaction (serialized, hex-encoded).
264
+ def signrawtransaction(hexstring, transaction = nil, privatekey =nil, sighashtype = "ALL")
265
+ @api.request 'signrawtransaction', hexstring, transaction, privatekey, sighashtype
266
+ end
267
+
268
+ # Stop bitcoin server.
269
+ def stop
270
+ @api.request 'stop'
271
+ end
272
+
273
+ # Return information about +bitcoinaddress+.
274
+ def validateaddress(bitcoinaddress)
275
+ @api.request 'validateaddress', bitcoinaddress
276
+ end
277
+
278
+ # Sign a message using +bitcoinaddress+.
279
+ def signmessage(bitcoinaddress, message)
280
+ @api.request 'signmessage', bitcoinaddress, message
281
+ end
282
+
283
+ # Verify signature made by +bitcoinaddress+.
284
+ def verifymessage(bitcoinaddress, signature, message)
285
+ @api.request 'verifymessage', bitcoinaddress, signature, message
286
+ end
287
+
288
+ # Stores the wallet decryption key in memory for +timeout+ seconds.
289
+ def walletpassphrase(passphrase, timeout)
290
+ @api.request 'walletpassphrase', passphrase, timeout
291
+ end
292
+
293
+ # Removes the wallet encryption key from memory, locking the wallet.
294
+ # After calling this method, you will need to call walletpassphrase again
295
+ # before being able to call any methods which require the wallet to be
296
+ # unlocked.
297
+ def walletlock
298
+ @api.request 'walletlock'
299
+ end
300
+
301
+ alias account getaccount
302
+ alias account_address getaccountaddress
303
+ alias addresses_by_account getaddressesbyaccount
304
+ alias balance getbalance
305
+ alias bestblockhash getbestblockhash
306
+ alias block_by_count getblockbycount
307
+ alias block_count getblockcount
308
+ alias block_number getblocknumber
309
+ alias block_hash getblockhash
310
+ alias connection_count getconnectioncount
311
+ alias difficulty getdifficulty
312
+ alias generate? getgenerate
313
+ alias hashes_per_sec gethashespersec
314
+ alias info getinfo
315
+ alias peerinfo getpeerinfo
316
+ alias new_address getnewaddress
317
+ alias received_by_account getreceivedbyaccount
318
+ alias received_by_address getreceivedbyaddress
319
+ alias transaction gettransaction
320
+ alias rawtransaction getrawtransaction
321
+ alias work getwork
322
+ alias get_work getwork
323
+ alias accounts listaccounts
324
+ alias list_received_by_account listreceivedbyaccount
325
+ alias list_received_by_address listreceivedbyaddress
326
+ alias transactions listtransactions
327
+ alias list_transactions listtransactions
328
+ alias send_from sendfrom
329
+ alias send_to_address sendtoaddress
330
+ alias send_many sendmany
331
+ alias account= setaccount
332
+ alias set_account setaccount
333
+ alias generate= setgenerate
334
+ alias set_generate setgenerate
335
+ alias validate_address validateaddress
336
+ alias sign_message signmessage
337
+ alias verify_message verifymessage
338
+ alias search_raw_transactions searchrawtransactions
339
+ alias raw_mempool getrawmempool
340
+ end