opalcoin_client 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +418 -0
- data/Rakefile +1 -0
- data/lib/errors/http_error.rb +12 -0
- data/lib/errors/invalid_method_error.rb +11 -0
- data/lib/errors/rpc_error.rb +11 -0
- data/lib/opalcoin_client.rb +36 -0
- data/lib/opalcoin_client/client.rb +73 -0
- data/lib/opalcoin_client/methods.rb +55 -0
- data/lib/opalcoin_client/version.rb +3 -0
- data/opalcoin_client.gemspec +24 -0
- data/spec/client_spec.rb +52 -0
- data/spec/opalcoin_client_spec.rb +38 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ed491d4ea33579c677cab141ea53554c18cc484
|
4
|
+
data.tar.gz: 74bfb755b91b692b9c8c92d164f1bc885a3b4904
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f8d368aa9f24605bcee3975bef99d1a56763d19145e13d3f26b719e41cd0587454d8db3a4b2f4c87c36e982ab2fe0bdc4cb2a9462298290c1cc84714683e91c
|
7
|
+
data.tar.gz: 07952f970a73dc4e669d24a9b80eb68dd88ba484014102aad3d488a70749489d0e88f544a763bf2f83f4c77d034db8314697f1056f7e63e033a517c9070a614a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Whit Jack
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,418 @@
|
|
1
|
+
# OpalcoinClient
|
2
|
+
|
3
|
+
OpalcoinClient is a gem that makes it easy to work with opalcoin in ruby.
|
4
|
+
|
5
|
+
## Dependencies
|
6
|
+
|
7
|
+
The only requirement is a running opalcoin daemon ([opalcoind](https://github.com/opalcoin/opalcoin)). Make sure to check out the [doc section](https://github.com/opalcoin/opalcoin/tree/master/doc) and follow the instructions for your os.
|
8
|
+
NOTICE: by default opalcoind will only allow local connections.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'opalcoin_client'
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install opalcoin_client
|
19
|
+
|
20
|
+
## Configuration
|
21
|
+
|
22
|
+
If you're using rails you can create an initializer. Here are the default settings:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# config/initializers/opalcoin_client.rb
|
26
|
+
OpalcoinClient.configure do |config|
|
27
|
+
config.host = 'localhost'
|
28
|
+
config.port = 51990
|
29
|
+
config.protocol = :http
|
30
|
+
config.user = ''
|
31
|
+
config.password = ''
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
You can also pass config variables as an options hash when creating a new client:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
client = OpalcoinClient.new(user: 'my_opalcoind_username', password: 'my_super_secure_password')
|
39
|
+
```
|
40
|
+
|
41
|
+
## Example Usage
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# create a new instance of the client
|
45
|
+
client = OpalcoinClient.new
|
46
|
+
|
47
|
+
# check that opalcoind is running and that our credentials are correct
|
48
|
+
if client.valid?
|
49
|
+
# get a new wallet address
|
50
|
+
new_wallet_addr = client.get_new_address
|
51
|
+
|
52
|
+
# get the balance of our new wallet
|
53
|
+
my_balance = client.get_balance(new_wallet_addr)
|
54
|
+
puts "I have #{my_balance} opal!"
|
55
|
+
else
|
56
|
+
puts 'Something is wrong...'
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
## Available Methods
|
61
|
+
|
62
|
+
<table>
|
63
|
+
<tr>
|
64
|
+
<th> Method </th>
|
65
|
+
<th> Params </th>
|
66
|
+
<th> Description </th>
|
67
|
+
<th>unlckd wallet req?
|
68
|
+
</th></tr>
|
69
|
+
<tr>
|
70
|
+
<td> add_multi_sig_address </td>
|
71
|
+
<td> [nrequired] ["key","key"] [account] </td>
|
72
|
+
<td> <b>Currently only available on testnet</b> Add a nrequired-to-sign multisignature address to the wallet. Each key is a opalcoin address or hex-encoded public key. If [account] is specified, assign address to [account]. </td>
|
73
|
+
<td> No
|
74
|
+
</td></tr>
|
75
|
+
<tr>
|
76
|
+
<td> backup_wallet </td>
|
77
|
+
<td> [destination] </td>
|
78
|
+
<td> Safely copies wallet.dat to destination, which can be a directory or a path with filename. </td>
|
79
|
+
<td> No
|
80
|
+
</td></tr>
|
81
|
+
<tr>
|
82
|
+
<td> dump_priv_key </td>
|
83
|
+
<td> [opalcoinaddress] </td>
|
84
|
+
<td> Reveals the private key corresponding to <opalcoinaddress< </td>
|
85
|
+
<td> Yes
|
86
|
+
</td></tr>
|
87
|
+
<tr>
|
88
|
+
<td> encrypt_wallet </td>
|
89
|
+
<td> [passphrase] </td>
|
90
|
+
<td> Encrypts the wallet with <passphrase<. </td>
|
91
|
+
<td> No
|
92
|
+
</td></tr>
|
93
|
+
<tr>
|
94
|
+
<td> get_account </td>
|
95
|
+
<td> [opalcoinaddress] </td>
|
96
|
+
<td> Returns the account associated with the given address. </td>
|
97
|
+
<td> No
|
98
|
+
</td></tr>
|
99
|
+
<tr>
|
100
|
+
<td> get_account_address </td>
|
101
|
+
<td> [account] </td>
|
102
|
+
<td> Returns the current opalcoin address for receiving payments to this account. </td>
|
103
|
+
<td> No
|
104
|
+
</td></tr>
|
105
|
+
<tr>
|
106
|
+
<td> get_addresses_by_account </td>
|
107
|
+
<td> [account] </td>
|
108
|
+
<td> Returns the list of addresses for the given account. </td>
|
109
|
+
<td> No
|
110
|
+
</td></tr>
|
111
|
+
<tr>
|
112
|
+
<td> get_balance </td>
|
113
|
+
<td> [account] [minconf=1] </td>
|
114
|
+
<td> If [account] is not specified, returns the server's total available balance.<br />If [account] is specified, returns the balance in the account. </td>
|
115
|
+
<td> No
|
116
|
+
</td></tr>
|
117
|
+
<tr>
|
118
|
+
<td> get_block </td>
|
119
|
+
<td> [hash] </td>
|
120
|
+
<td> Returns information about the given block hash. </td>
|
121
|
+
<td> No
|
122
|
+
</td></tr>
|
123
|
+
<tr>
|
124
|
+
<td> get_block_count </td>
|
125
|
+
<td> </td>
|
126
|
+
<td> Returns the number of blocks in the longest block chain. </td>
|
127
|
+
<td> No
|
128
|
+
</td></tr>
|
129
|
+
<tr>
|
130
|
+
<td> get_block_hash </td>
|
131
|
+
<td> [index] </td>
|
132
|
+
<td> Returns hash of block in best-block-chain at <index< </td>
|
133
|
+
<td> No
|
134
|
+
</td></tr>
|
135
|
+
<tr>
|
136
|
+
<td> get_block_number </td>
|
137
|
+
<td> </td>
|
138
|
+
<td> <b>Deprecated</b>. Use getblockcount. </td>
|
139
|
+
<td> No
|
140
|
+
</td></tr>
|
141
|
+
<tr>
|
142
|
+
<td> get_connection_count </td>
|
143
|
+
<td> </td>
|
144
|
+
<td> Returns the number of connections to other nodes. </td>
|
145
|
+
<td> No
|
146
|
+
</td></tr>
|
147
|
+
<tr>
|
148
|
+
<td> get_difficulty </td>
|
149
|
+
<td> </td>
|
150
|
+
<td> Returns the proof-of-work difficulty as a multiple of the minimum difficulty. </td>
|
151
|
+
<td> No
|
152
|
+
</td></tr>
|
153
|
+
<tr>
|
154
|
+
<td> get_generate </td>
|
155
|
+
<td> </td>
|
156
|
+
<td> Returns true or false whether opalcoind is currently generating hashes </td>
|
157
|
+
<td> No
|
158
|
+
</td></tr>
|
159
|
+
<tr>
|
160
|
+
<td> get_hashes_per_sec </td>
|
161
|
+
<td> </td>
|
162
|
+
<td> Returns a recent hashes per second performance measurement while generating. </td>
|
163
|
+
<td> No
|
164
|
+
</td></tr>
|
165
|
+
<tr>
|
166
|
+
<td> get_info </td>
|
167
|
+
<td> </td>
|
168
|
+
<td> Returns an object containing various state info. </td>
|
169
|
+
<td> No
|
170
|
+
</td></tr>
|
171
|
+
<tr>
|
172
|
+
<td> get_memory_pool </td>
|
173
|
+
<td> [data] </td>
|
174
|
+
<td> If [data] is not specified, returns data needed to construct a block to work on:
|
175
|
+
<ul><li> "version": block version
|
176
|
+
</li><li> "previousblockhash": hash of current highest block
|
177
|
+
</li><li> "transactions": contents of non-coinbase transactions that should be included in the next block
|
178
|
+
</li><li> "coinbasevalue": maximum allowable input to coinbase transaction, including the generation award and transaction fees
|
179
|
+
</li><li> "time": timestamp appropriate for next block
|
180
|
+
</li><li> "bits": compressed target of next block
|
181
|
+
</li></ul>
|
182
|
+
<p>If [data] is specified, tries to solve the block and returns true if it was successful.
|
183
|
+
</p>
|
184
|
+
</td>
|
185
|
+
<td> No
|
186
|
+
</td></tr>
|
187
|
+
<tr>
|
188
|
+
<td> get_mining_info </td>
|
189
|
+
<td> </td>
|
190
|
+
<td> Returns an object containing mining-related information:
|
191
|
+
<ul><li> blocks
|
192
|
+
</li><li> currentblocksize
|
193
|
+
</li><li> currentblocktx
|
194
|
+
</li><li> difficulty
|
195
|
+
</li><li> errors
|
196
|
+
</li><li> generate
|
197
|
+
</li><li> genproclimit
|
198
|
+
</li><li> hashespersec
|
199
|
+
</li><li> pooledtx
|
200
|
+
</li><li> testnet
|
201
|
+
</li></ul>
|
202
|
+
</td>
|
203
|
+
<td> No
|
204
|
+
</td></tr>
|
205
|
+
<tr>
|
206
|
+
<td> get_new_address </td>
|
207
|
+
<td> [account] </td>
|
208
|
+
<td> Returns a new opalcoin address for receiving payments. If [account] is specified (recommended), it is added to the address book so payments received with the address will be credited to [account]. </td>
|
209
|
+
<td> No
|
210
|
+
</td></tr>
|
211
|
+
<tr>
|
212
|
+
<td> get_received_by_account </td>
|
213
|
+
<td> [account] [minconf=1] </td>
|
214
|
+
<td> Returns the total amount received by addresses with [account] in transactions with at least [minconf] confirmations. If [account] not provided return will include all transactions to all accounts. (version 0.3.24-beta) </td>
|
215
|
+
<td> No
|
216
|
+
</td></tr>
|
217
|
+
<tr>
|
218
|
+
<td> get_received_by_address </td>
|
219
|
+
<td> [opalcoinaddress] [minconf=1] </td>
|
220
|
+
<td> Returns the total amount received by <opalcoinaddress< in transactions with at least [minconf] confirmations. While some might consider this obvious, value reported by this only considers *receiving* transactions. It does not check payments that have been made *from* this address. In other words, this is not "getaddressbalance". Works only for addresses in the local wallet, external addresses will always show 0. </td>
|
221
|
+
<td> No
|
222
|
+
</td></tr>
|
223
|
+
<tr>
|
224
|
+
<td> get_transaction </td>
|
225
|
+
<td> [txid] </td>
|
226
|
+
<td> Returns an object about the given transaction containing:
|
227
|
+
<ul><li> "amount": total amount of the transaction
|
228
|
+
</li><li> "confirmations": number of confirmations of the transaction
|
229
|
+
</li><li> "txid": the transaction ID
|
230
|
+
</li><li> "time": time the transaction occurred
|
231
|
+
</li><li> "details" - An array of objects containing:
|
232
|
+
<ul><li> "account"
|
233
|
+
</li><li> "address"
|
234
|
+
</li><li> "category"
|
235
|
+
</li><li> "amount"
|
236
|
+
</li></ul>
|
237
|
+
</li></ul>
|
238
|
+
</td>
|
239
|
+
<td> No
|
240
|
+
</td></tr>
|
241
|
+
<tr>
|
242
|
+
<td> get_work </td>
|
243
|
+
<td> [data] </td>
|
244
|
+
<td> If [data] is not specified, returns formatted hash data to work on:
|
245
|
+
<ul><li> "midstate": precomputed hash state after hashing the first half of the data
|
246
|
+
</li><li> "data": block data
|
247
|
+
</li><li> "hash1": formatted hash buffer for second hash
|
248
|
+
</li><li> "target": little endian hash target
|
249
|
+
</li></ul>
|
250
|
+
<p>If [data] is specified, tries to solve the block and returns true if it was successful.
|
251
|
+
</p>
|
252
|
+
</td>
|
253
|
+
<td> No
|
254
|
+
</td></tr>
|
255
|
+
<tr>
|
256
|
+
<td> help </td>
|
257
|
+
<td> [command] </td>
|
258
|
+
<td> List commands, or get help for a command. </td>
|
259
|
+
<td> No
|
260
|
+
</td></tr>
|
261
|
+
<tr>
|
262
|
+
<td> import_priv_key </td>
|
263
|
+
<td> [opalcoinprivkey] [label] </td>
|
264
|
+
<td> Adds a private key (as returned by dumpprivkey) to your wallet. </td>
|
265
|
+
<td> Yes
|
266
|
+
</td></tr>
|
267
|
+
<tr>
|
268
|
+
<td> key_pool_refill </td>
|
269
|
+
<td> </td>
|
270
|
+
<td> Fills the keypool, requires wallet passphrase to be set. </td>
|
271
|
+
<td> Yes
|
272
|
+
</td></tr>
|
273
|
+
<tr>
|
274
|
+
<td> list_accounts </td>
|
275
|
+
<td> [minconf=1] </td>
|
276
|
+
<td> Returns Object that has account names as keys, account balances as values. </td>
|
277
|
+
<td> No
|
278
|
+
</td></tr>
|
279
|
+
<tr>
|
280
|
+
<td> list_received_by_account </td>
|
281
|
+
<td> [minconf=1] [includeempty=false] </td>
|
282
|
+
<td> Returns an array of objects containing:
|
283
|
+
<ul><li> "account": the account of the receiving addresses
|
284
|
+
</li><li> "amount": total amount received by addresses with this account
|
285
|
+
</li><li> "confirmations": number of confirmations of the most recent transaction included
|
286
|
+
</li></ul>
|
287
|
+
</td>
|
288
|
+
<td> No
|
289
|
+
</td></tr>
|
290
|
+
<tr>
|
291
|
+
<td> list_received_by_address </td>
|
292
|
+
<td> [minconf=1] [includeempty=false] </td>
|
293
|
+
<td> Returns an array of objects containing:
|
294
|
+
<ul><li> "address": receiving address
|
295
|
+
</li><li> "account": the account of the receiving address
|
296
|
+
</li><li> "amount": total amount received by the address
|
297
|
+
</li><li> "confirmations": number of confirmations of the most recent transaction included
|
298
|
+
</li></ul>
|
299
|
+
<p>To get a list of accounts on the system, execute opalcoind listreceivedbyaddress 0 true
|
300
|
+
</p>
|
301
|
+
</td>
|
302
|
+
<td> No
|
303
|
+
</td></tr>
|
304
|
+
<tr>
|
305
|
+
<td> list_since_block</td>
|
306
|
+
<td> [blockhash] [target-confirmations] </td>
|
307
|
+
<td> Get all transactions in blocks since block [blockhash], or all transactions if omitted. </td>
|
308
|
+
<td> No
|
309
|
+
</td></tr>
|
310
|
+
<tr>
|
311
|
+
<td> list_transactions </td>
|
312
|
+
<td> [account] [count=10] [from=0] </td>
|
313
|
+
<td> Returns up to [count] most recent transactions skipping the first [from] transactions for account [account]. If [account] not provided will return recent transaction from all accounts.
|
314
|
+
</td>
|
315
|
+
<td> No
|
316
|
+
</td></tr>
|
317
|
+
<tr>
|
318
|
+
<td> move </td>
|
319
|
+
<td> [fromaccount] [toaccount] [amount] [minconf=1] [comment] </td>
|
320
|
+
<td> Move from one account in your wallet to another </td>
|
321
|
+
<td> No
|
322
|
+
</td></tr>
|
323
|
+
<tr>
|
324
|
+
<td> send_from </td>
|
325
|
+
<td> [fromaccount] [toopalcoinaddress] [amount] [minconf=1] [comment] [comment-to] </td>
|
326
|
+
<td> <amount< is a real and is rounded to 8 decimal places. Will send the given amount to the given address, ensuring the account has a valid balance using [minconf] confirmations. Returns the transaction ID if successful (not in JSON object). </td>
|
327
|
+
<td> Yes
|
328
|
+
</td></tr>
|
329
|
+
<tr>
|
330
|
+
<td> send_many </td>
|
331
|
+
<td> [fromaccount] [address:amount,...] [minconf=1] [comment] </td>
|
332
|
+
<td> amounts are double-precision floating point numbers </td>
|
333
|
+
<td> Yes
|
334
|
+
</td></tr>
|
335
|
+
<tr>
|
336
|
+
<td> send_to_address </td>
|
337
|
+
<td> [opalcoinaddress] [amount] [comment] [comment-to] </td>
|
338
|
+
<td> <amount< is a real and is rounded to 8 decimal places. Returns the transaction ID <txid< if successful. </td>
|
339
|
+
<td> Yes
|
340
|
+
</td></tr>
|
341
|
+
<tr>
|
342
|
+
<td> set_account </td>
|
343
|
+
<td> [opalcoinaddress] [account] </td>
|
344
|
+
<td> Sets the account associated with the given address. Assigning address that is already assigned to the same account will create a new address associated with that account. </td>
|
345
|
+
<td> No
|
346
|
+
</td></tr>
|
347
|
+
<tr>
|
348
|
+
<td> set_generate </td>
|
349
|
+
<td> [generate] [genproclimit] </td>
|
350
|
+
<td> [generate] is true or false to turn generation on or off.
|
351
|
+
|
352
|
+
Generation is limited to [genproclimit] processors, -1 is unlimited. </td>
|
353
|
+
<td> No
|
354
|
+
</td></tr>
|
355
|
+
<tr>
|
356
|
+
<td> sign_message </td>
|
357
|
+
<td> [opalcoinaddress] [message] </td>
|
358
|
+
<td> Sign a message with the private key of an address. </td>
|
359
|
+
<td> Yes
|
360
|
+
</td></tr>
|
361
|
+
<tr>
|
362
|
+
<td> set_tx_fee </td>
|
363
|
+
<td> [amount] </td>
|
364
|
+
<td> [amount] is a real and is rounded to the nearest 0.00000001 </td>
|
365
|
+
<td> No
|
366
|
+
</td></tr>
|
367
|
+
<tr>
|
368
|
+
<td> stop </td>
|
369
|
+
<td> </td>
|
370
|
+
<td> Stop opalcoin server. </td>
|
371
|
+
<td> No
|
372
|
+
</td></tr>
|
373
|
+
<tr>
|
374
|
+
<td> validate_address </td>
|
375
|
+
<td> [opalcoinaddress] </td>
|
376
|
+
<td> Return information about [opalcoinaddress]. </td>
|
377
|
+
<td> No
|
378
|
+
</td></tr>
|
379
|
+
<tr>
|
380
|
+
<td> verify_message </td>
|
381
|
+
<td> [opalcoinaddress] [signature] [message] </td>
|
382
|
+
<td> Verify a signed message. </td>
|
383
|
+
<td> No
|
384
|
+
</td></tr>
|
385
|
+
<tr>
|
386
|
+
<td> wallet_lock </td>
|
387
|
+
<td> </td>
|
388
|
+
<td> Removes the wallet encryption key from memory, locking the wallet. After calling this method, you will need to call walletpassphrase again before being able to call any methods which require the wallet to be unlocked. </td>
|
389
|
+
<td> No
|
390
|
+
</td></tr>
|
391
|
+
<tr>
|
392
|
+
<td> wallet_passphrase </td>
|
393
|
+
<td> [passphrase] [timeout] </td>
|
394
|
+
<td> Stores the wallet decryption key in memory for <timeout< seconds. </td>
|
395
|
+
<td> No
|
396
|
+
</td></tr>
|
397
|
+
<tr>
|
398
|
+
<td> wallet_passphrase_change </td>
|
399
|
+
<td> [oldpassphrase] [newpassphrase] </td>
|
400
|
+
<td> Changes the wallet passphrase from <oldpassphrase< to <newpassphrase<. </td>
|
401
|
+
<td> No
|
402
|
+
</td></tr></table>
|
403
|
+
|
404
|
+
*Table stolen from [node-opalcoin](https://github.com/countable/node-opalcoin)
|
405
|
+
|
406
|
+
## Contributing
|
407
|
+
|
408
|
+
For local testing, make sure to replace the user/password in `spec/client_spec.rb` and `spec/opalcoin_client_spec.rb` with the credentials for your local opalcoind.
|
409
|
+
|
410
|
+
1. Fork it
|
411
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
412
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
413
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
414
|
+
5. Create new Pull Request
|
415
|
+
|
416
|
+
## Can't I just use a client for bitcoin or litecoin?
|
417
|
+
|
418
|
+
Perhaps, but this way you don't need to worry about any current or future api inconsistencies. Plus, why use a tool built for an inferior alt coin?
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'opalcoin_client/version'
|
2
|
+
require 'opalcoin_client/client'
|
3
|
+
|
4
|
+
class OpalcoinClient
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@client = OpalcoinClient::Client.new(options)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Delegate everything to the 'real' Client
|
11
|
+
def method_missing(name, *args)
|
12
|
+
@client.send(name, *args)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
yield(configuration) if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
class Configuration
|
24
|
+
attr_accessor :host, :port, :protocol, :user, :password
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
self.host = 'localhost'
|
28
|
+
self.port = 51990
|
29
|
+
self.protocol = :http
|
30
|
+
self.user = ''
|
31
|
+
self.password = ''
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'uri'
|
3
|
+
require 'json'
|
4
|
+
require 'opalcoin_client'
|
5
|
+
require 'opalcoin_client/methods'
|
6
|
+
require 'errors/http_error'
|
7
|
+
require 'errors/rpc_error'
|
8
|
+
require 'errors/invalid_method_error'
|
9
|
+
|
10
|
+
|
11
|
+
class OpalcoinClient
|
12
|
+
class Client
|
13
|
+
|
14
|
+
attr_accessor :options
|
15
|
+
|
16
|
+
def initialize(options = {})
|
17
|
+
@options = get_defaults.merge(options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid?
|
21
|
+
post_body = { method: 'getinfo', id: Time.now.to_i }.to_json
|
22
|
+
http_post_request(post_body).class == Net::HTTPOK rescue false
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_missing(name, *args)
|
26
|
+
raise OpalcoinClient::InvalidMethodError.new(name) unless OpalcoinClient::METHODS.include?(name.to_s)
|
27
|
+
|
28
|
+
response = http_post_request( get_post_body(name, args) )
|
29
|
+
get_response_data(response)
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_post_request(post_body)
|
33
|
+
url = URI.parse "#{@options[:protocol]}://#{@options[:user]}:#{@options[:password]}@#{@options[:host]}:#{@options[:port]}/"
|
34
|
+
|
35
|
+
http = Net::HTTP.new(url.host, url.port)
|
36
|
+
http.use_ssl = (url.scheme == 'https')
|
37
|
+
|
38
|
+
request = Net::HTTP::Post.new(url.path)
|
39
|
+
request.basic_auth url.user, url.password
|
40
|
+
request.content_type = 'application/json'
|
41
|
+
request.body = post_body
|
42
|
+
|
43
|
+
response = http.request(request)
|
44
|
+
|
45
|
+
return response if response.class == Net::HTTPOK or response.class == Net::HTTPInternalServerError
|
46
|
+
raise OpalcoinClient::HTTPError.new(response)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def get_post_body(name, args)
|
52
|
+
{ method: de_ruby_style(name), params: args, id: Time.now.to_i }.to_json
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_response_data(http_ok_response)
|
56
|
+
resp = JSON.parse( http_ok_response.body )
|
57
|
+
raise OpalcoinClient::RPCError.new(resp['error']['message']) if resp['error'] and http_ok_response.class == Net::HTTPInternalServerError
|
58
|
+
resp['result']
|
59
|
+
end
|
60
|
+
|
61
|
+
def de_ruby_style(method_name)
|
62
|
+
method_name.to_s.tr('_', '').downcase.to_sym
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_defaults
|
66
|
+
OpalcoinClient.configuration.instance_variables.each.inject({}) {|hash, var|
|
67
|
+
hash[var.to_s.delete('@').to_sym] = OpalcoinClient.configuration.instance_variable_get(var);
|
68
|
+
hash
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class OpalcoinClient
|
2
|
+
METHODS = %w(
|
3
|
+
add_multi_sig_address
|
4
|
+
backup_wallet
|
5
|
+
create_raw_transaction
|
6
|
+
decode_raw_transaction
|
7
|
+
dump_priv_key
|
8
|
+
encrypt_wallet
|
9
|
+
get_account
|
10
|
+
get_account_address
|
11
|
+
get_addresses_by_account
|
12
|
+
get_balance
|
13
|
+
get_block
|
14
|
+
get_block_count
|
15
|
+
get_block_hash
|
16
|
+
get_connection_count
|
17
|
+
get_difficulty
|
18
|
+
get_generate
|
19
|
+
get_hashes_per_sec
|
20
|
+
get_info
|
21
|
+
get_memory_pool
|
22
|
+
get_mining_info
|
23
|
+
get_new_address
|
24
|
+
get_raw_transaction
|
25
|
+
get_received_by_account
|
26
|
+
get_received_by_address
|
27
|
+
get_transaction
|
28
|
+
get_work
|
29
|
+
help
|
30
|
+
import_priv_key
|
31
|
+
key_pool_refill
|
32
|
+
list_accounts
|
33
|
+
list_received_by_account
|
34
|
+
list_received_by_address
|
35
|
+
list_since_block
|
36
|
+
list_transactions
|
37
|
+
list_unspent
|
38
|
+
move
|
39
|
+
send_from
|
40
|
+
send_many
|
41
|
+
send_raw_transaction
|
42
|
+
send_to_address
|
43
|
+
set_account
|
44
|
+
set_generate
|
45
|
+
set_tx_fee
|
46
|
+
sign_message
|
47
|
+
sign_raw_transaction
|
48
|
+
stop
|
49
|
+
validate_address
|
50
|
+
verify_message
|
51
|
+
wallet_lock
|
52
|
+
wallet_passphrase
|
53
|
+
wallet_passphrase_change
|
54
|
+
)
|
55
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'opalcoin_client/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "opalcoin_client"
|
8
|
+
spec.version = OpalcoinClient::VERSION
|
9
|
+
spec.authors = ["Whit Jack"]
|
10
|
+
spec.email = ["whit@whitjack.me"]
|
11
|
+
spec.description = %q{A Opalcoin client for ruby. This gem is a ruby wrapper for making remote procedure calls (rpc) to a opalcoin daemon (opalcoind.)}
|
12
|
+
spec.summary = %q{OpalcoinClient is a gem that makes it easy to work with opalcoin in ruby.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
24
|
+
end
|
data/spec/client_spec.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'opalcoin_client/client'
|
3
|
+
require 'errors/http_error'
|
4
|
+
require 'errors/rpc_error'
|
5
|
+
require 'errors/invalid_method_error'
|
6
|
+
|
7
|
+
describe OpalcoinClient::Client do
|
8
|
+
|
9
|
+
def valid_client
|
10
|
+
# For local testing ensure you have opalcoind running correctly and use your own username / password here
|
11
|
+
OpalcoinClient::Client.new(user: 'opalcoinrpc', password: '5d36c07c20a43a281f54c07d72ce78cc')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'rejects bad credentials' do
|
15
|
+
bad_client = OpalcoinClient::Client.new(user: 'bad_username', password: 'bad_password')
|
16
|
+
bad_client.valid?.should eql(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'connects to the rpc server' do
|
20
|
+
valid_client.valid?.should eql(true)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'catches requests with bad credentials' do
|
24
|
+
bad_client = OpalcoinClient::Client.new(user: 'bad_username', password: 'bad_password')
|
25
|
+
expect { bad_client.get_info }.to raise_error(OpalcoinClient::HTTPError)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'catches requests with bad service urls' do
|
29
|
+
bad_client = valid_client
|
30
|
+
bad_client.options[:host] = 'not_localhost'
|
31
|
+
expect { bad_client.get_info }.to raise_error
|
32
|
+
|
33
|
+
bad_client2 = valid_client
|
34
|
+
bad_client2.options[:port] = 100
|
35
|
+
expect { bad_client2.get_info }.to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'works with ruby-style method names' do
|
39
|
+
c = valid_client
|
40
|
+
c.get_info
|
41
|
+
c.get_block_count
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'throws rpc_error when the params are bad' do
|
45
|
+
expect { valid_client.get_account('bad_location') }.to raise_error(OpalcoinClient::RPCError)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'only allows listed methods' do
|
49
|
+
expect { valid_client.not_a_real_method }.to raise_error(OpalcoinClient::InvalidMethodError)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'opalcoin_client/client'
|
2
|
+
|
3
|
+
describe OpalcoinClient do
|
4
|
+
|
5
|
+
def valid_client
|
6
|
+
# make sure to replace these with the credentials from your own opalcoind
|
7
|
+
OpalcoinClient.new(user: 'opalcoinrpc', password: '5d36c07c20a43a281f54c07d72ce78cc')
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'sets up and works with a valid client' do
|
11
|
+
bad_client = OpalcoinClient.new
|
12
|
+
bad_client.valid?.should eql(false)
|
13
|
+
|
14
|
+
valid_client.valid?.should eql(true)
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'calls client methods correctly' do
|
18
|
+
addr = valid_client.get_new_address
|
19
|
+
addr[0].should eql('o')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'configures itself properly' do
|
23
|
+
OpalcoinClient.configure do |config|
|
24
|
+
config.user = 'opalcoinrpc'
|
25
|
+
config.password = '5d36c07c20a43a281f54c07d72ce78cc'
|
26
|
+
end
|
27
|
+
client = OpalcoinClient.new
|
28
|
+
client.valid?.should eql(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'using results as args' do
|
32
|
+
client = valid_client
|
33
|
+
new_wallet_addr = client.get_new_address
|
34
|
+
my_balance = client.get_balance(new_wallet_addr)
|
35
|
+
my_balance.should eql(0.0)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: opalcoin_client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Whit Jack
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.6'
|
55
|
+
description: A Opalcoin client for ruby. This gem is a ruby wrapper for making remote
|
56
|
+
procedure calls (rpc) to a opalcoin daemon (opalcoind.)
|
57
|
+
email:
|
58
|
+
- whit@whitjack.me
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/errors/http_error.rb
|
69
|
+
- lib/errors/invalid_method_error.rb
|
70
|
+
- lib/errors/rpc_error.rb
|
71
|
+
- lib/opalcoin_client.rb
|
72
|
+
- lib/opalcoin_client/client.rb
|
73
|
+
- lib/opalcoin_client/methods.rb
|
74
|
+
- lib/opalcoin_client/version.rb
|
75
|
+
- opalcoin_client.gemspec
|
76
|
+
- spec/client_spec.rb
|
77
|
+
- spec/opalcoin_client_spec.rb
|
78
|
+
homepage: ''
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.2.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: OpalcoinClient is a gem that makes it easy to work with opalcoin in ruby.
|
102
|
+
test_files:
|
103
|
+
- spec/client_spec.rb
|
104
|
+
- spec/opalcoin_client_spec.rb
|