nano_rpc 0.3.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d161d743c5ec1f860437f0c90189ed939280bc090f135cbacd7f9c442404000
4
- data.tar.gz: ff48c6fdd7cbf214ff0d967e5fdf924ad6c51aa5a803dfbe3f92c343d453a19e
3
+ metadata.gz: 51594de4d5c25a44368e84aea129bfe04cc01f28654efdd70101a77b86361111
4
+ data.tar.gz: 3dcedfce43a868796ea107807a4a196cd4e78d5416ebaae4289d3911fbbb8a88
5
5
  SHA512:
6
- metadata.gz: a71a9d9558fb9abdca9484158761567edb522cf29322e36a764811c5b669e3eeb086f3d3125ca94d3de862e45b8cf071a1bd3fb48f1534c6251ccd0f0f4f32b4
7
- data.tar.gz: c928cd95666dd0e84e67f71275e6d475947c90da9c89a82cfb1ae6f4193c87a9686c1dec3ba004c722373f20f3dca857c4c285c7fc652c64d61792d8552a4e41
6
+ metadata.gz: 5df2f32a0c02eada1f18af7d460de1d01b1e9cf762091168047855cba46f002dbcda51f42c662ff29a1dbddc31ac14b0dbe1725d9602a57e2726bab4f650f214
7
+ data.tar.gz: d0cdd6a9ba34c543f8cd7039abc6e0cc368ea862e2770cfceb2f49c9cb8af538570eea14d870c7dc1aab1012fe359ee92970df819ac5b9a8c0e59351f61b8191
data/lib/nano_rpc.rb CHANGED
@@ -1,14 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
  require 'nano_rpc/version'
3
+ require 'nano_rpc/helpers/application_helper'
4
+ require 'nano_rpc/helpers/account_helper'
5
+ require 'nano_rpc/helpers/accounts_helper'
6
+ require 'nano_rpc/helpers/node_helper'
7
+ require 'nano_rpc/helpers/wallet_helper'
3
8
  require 'nano_rpc/client'
4
9
  require 'nano_rpc/errors'
5
10
  require 'nano_rpc/proxy'
6
11
  require 'nano_rpc/response'
7
- require 'nano_rpc/helpers/application_helper'
8
- require 'nano_rpc/helpers/account_proxy_helper'
9
- require 'nano_rpc/helpers/accounts_proxy_helper'
10
- require 'nano_rpc/helpers/node_proxy_helper'
11
- require 'nano_rpc/helpers/wallet_proxy_helper'
12
12
  require 'nano_rpc/proxies/account'
13
13
  require 'nano_rpc/proxies/accounts'
14
14
  require 'nano_rpc/proxies/node'
@@ -8,6 +8,8 @@ module Nano
8
8
  end
9
9
 
10
10
  class Client
11
+ include Nano::ApplicationHelper
12
+
11
13
  attr_accessor :host, :port
12
14
 
13
15
  def initialize(host: 'localhost', port: 7076)
@@ -15,6 +17,11 @@ module Nano
15
17
  @port = port
16
18
  end
17
19
 
20
+ # Condense host/port on object inspection
21
+ def inspect
22
+ "#{inspect_prefix}, @url=\"#{@host}:#{port}\">"
23
+ end
24
+
18
25
  def call(action, params = {})
19
26
  args = { action: action }
20
27
  args.merge!(params) if params.is_a?(Hash)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- module Nano::AccountProxyHelper
2
+ module Nano::AccountHelper
3
3
  include Nano::ApplicationHelper
4
4
 
5
5
  def balance
@@ -10,9 +10,9 @@ module Nano::AccountProxyHelper
10
10
  account_block_count.block_count
11
11
  end
12
12
 
13
- def history(opts)
13
+ def history(*args)
14
14
  account_history(
15
- count: opts_pluck(opts, :count)
15
+ pluck_argument(args, :count)
16
16
  ).history
17
17
  end
18
18
 
@@ -26,15 +26,15 @@ module Nano::AccountProxyHelper
26
26
 
27
27
  def move(from:, to:)
28
28
  account_move(
29
- wallet: wallet_seed(to),
30
- source: wallet_seed(from),
29
+ wallet: object_to_value(to),
30
+ source: object_to_value(from),
31
31
  accounts: [address]
32
32
  ).moved == 1
33
33
  end
34
34
 
35
35
  def wallet_work_set(wallet:, work:)
36
36
  work_set(
37
- wallet: wallet_seed(wallet),
37
+ wallet: object_to_value(wallet),
38
38
  work: work
39
39
  ).success == ''
40
40
  end
@@ -53,9 +53,9 @@ module Nano::AccountProxyHelper
53
53
  end
54
54
  alias blocks_pending pending_blocks
55
55
 
56
- def remove(opts)
56
+ def remove(*args)
57
57
  account_remove(
58
- wallet: wallet_seed(opts_pluck(opts, :wallet))
58
+ pluck_argument(args, :wallet)
59
59
  ).removed == 1
60
60
  end
61
61
 
@@ -65,7 +65,7 @@ module Nano::AccountProxyHelper
65
65
 
66
66
  def representative_set(wallet:, representative:)
67
67
  account_representative_set(
68
- wallet: wallet_seed(wallet),
68
+ wallet: object_to_value(wallet),
69
69
  representative: representative
70
70
  ).set == 1
71
71
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- module Nano::AccountsProxyHelper
2
+ module Nano::AccountsHelper
3
3
  include Nano::ApplicationHelper
4
4
 
5
5
  def balances
@@ -25,8 +25,8 @@ module Nano::AccountsProxyHelper
25
25
 
26
26
  def move(from:, to:)
27
27
  account_move(
28
- source: wallet_seed(from),
29
- wallet: wallet_seed(to)
28
+ source: object_to_value(from),
29
+ wallet: object_to_value(to)
30
30
  ).moved == 1
31
31
  end
32
32
 
@@ -2,19 +2,26 @@
2
2
  module Nano::ApplicationHelper
3
3
  private
4
4
 
5
- def opts_pluck(opts, key)
6
- opts.is_a?(Hash) ? opts[key] : opts
5
+ def pluck_argument(args, key, arg_key = nil)
6
+ k = arg_key || key
7
+ arg = args.first
8
+ v = arg.is_a?(Hash) ? arg[key] : arg
9
+ { k => object_to_value(v) }
7
10
  end
8
11
 
9
- def wallet_seed(wallet)
10
- wallet.is_a?(Nano::Wallet) ? wallet.seed : wallet
12
+ def object_to_value(arg)
13
+ if arg.is_a?(Nano::Wallet)
14
+ arg.seed
15
+ elsif arg.is_a?(Nano::Account)
16
+ arg.address
17
+ elsif arg.is_a?(Nano::Accounts)
18
+ arg.addresses
19
+ else
20
+ arg
21
+ end
11
22
  end
12
23
 
13
- def account_address(account)
14
- account.is_a?(Nano::Account) ? account.address : account
15
- end
16
-
17
- def accounts_addresses(accounts)
18
- accounts.is_a?(Nano::Accounts) ? accounts.addresses : accounts
24
+ def inspect_prefix
25
+ "#<#{self.class}:#{format('0x00%x', object_id << 1)}"
19
26
  end
20
27
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ module Nano::NodeHelper
3
+ include Nano::ApplicationHelper
4
+
5
+ def account_containing_block(*args)
6
+ block_account(pluck_argument(args, :hash)).account
7
+ end
8
+
9
+ def create_wallet
10
+ Nano::Wallet.new(wallet_create.wallet)
11
+ end
12
+
13
+ def knano_from_raw(*args)
14
+ krai_from_raw(pluck_argument(args, :amount)).amount
15
+ end
16
+
17
+ def knano_to_raw(*args)
18
+ krai_to_raw(pluck_argument(args, :amount)).amount
19
+ end
20
+
21
+ def mnano_from_raw(*args)
22
+ mrai_from_raw(pluck_argument(args, :amount)).amount
23
+ end
24
+
25
+ def mnano_to_raw(*args)
26
+ mrai_to_raw(pluck_argument(args, :amount)).amount
27
+ end
28
+
29
+ def nano_from_raw(*args)
30
+ rai_from_raw(pluck_argument(args, :amount)).amount
31
+ end
32
+
33
+ def nano_to_raw(*args)
34
+ rai_to_raw(pluck_argument(args, :amount)).amount
35
+ end
36
+
37
+ def num_frontiers
38
+ frontier_count['count']
39
+ end
40
+
41
+ def pending_exists?(*args)
42
+ pending_exists(pluck_argument(args, :hash)).exists == 1
43
+ end
44
+
45
+ def total_supply
46
+ available_supply.available
47
+ end
48
+
49
+ def work_valid?(work:, hash:)
50
+ work_validate(work: work, hash: hash).valid == 1
51
+ end
52
+ end
@@ -1,14 +1,13 @@
1
1
  # frozen_string_literal: true
2
- module Nano::WalletProxyHelper
2
+ module Nano::WalletHelper
3
3
  include Nano::ApplicationHelper
4
4
 
5
- def account_work(opts)
6
- work_get(
7
- account: account_address(opts_pluck(opts, :account))
8
- ).work
5
+ def account_work(*args)
6
+ work_get(pluck_argument(args, :account)).work
9
7
  end
10
8
 
11
9
  def accounts
10
+ return [] unless account_list.accounts.size.positive?
12
11
  Nano::Accounts.new(account_list.accounts)
13
12
  end
14
13
 
@@ -32,22 +31,20 @@ module Nano::WalletProxyHelper
32
31
  payment_begin.account
33
32
  end
34
33
 
35
- def change_password(opts)
34
+ def change_password(*args)
36
35
  password_change(
37
- password: opts_pluck(opts, :new_password)
36
+ pluck_argument(args, :new_password, :password)
38
37
  ).changed == 1
39
38
  end
40
39
 
41
- def change_seed(opts)
40
+ def change_seed(*args)
42
41
  wallet_change_seed(
43
- seed: opts_pluck(opts, :new_seed)
42
+ pluck_argument(args, :new_seed, :seed)
44
43
  ).success == ''
45
44
  end
46
45
 
47
- def contains?(opts)
48
- wallet_contains(
49
- account: account_address(opts_pluck(opts, :account))
50
- ).exists == 1
46
+ def contains?(*args)
47
+ wallet_contains(pluck_argument(args, :account)).exists == 1
51
48
  end
52
49
 
53
50
  def create_account(work: true)
@@ -62,10 +59,8 @@ module Nano::WalletProxyHelper
62
59
  wallet_destroy
63
60
  end
64
61
 
65
- def enter_password(opts)
66
- password_enter(
67
- password: opts_pluck(opts, :password)
68
- ).valid == 1
62
+ def enter_password(*args)
63
+ password_enter(pluck_argument(args, :password)).valid == 1
69
64
  end
70
65
 
71
66
  def export
@@ -86,16 +81,14 @@ module Nano::WalletProxyHelper
86
81
 
87
82
  def move_accounts(to:, accounts:)
88
83
  account_move(
89
- wallet: wallet_seed(to),
84
+ wallet: object_to_value(to),
90
85
  source: seed,
91
- accounts: accounts_addresses(accounts)
86
+ accounts: object_to_value(accounts)
92
87
  ).moved == 1
93
88
  end
94
89
 
95
- def password_valid?(opts)
96
- password_valid(
97
- password: opts_pluck(opts, :password)
98
- ).valid == 1
90
+ def password_valid?(*args)
91
+ password_valid(pluck_argument(args, :password)).valid == 1
99
92
  end
100
93
 
101
94
  def pending_balance
@@ -123,44 +116,42 @@ module Nano::WalletProxyHelper
123
116
 
124
117
  def receive_block(account:, block:)
125
118
  receive(
126
- account: account_address(account),
119
+ account: object_to_value(account),
127
120
  block: block
128
121
  ).block
129
122
  end
130
123
 
131
- def remove_account(opts)
132
- account_remove(
133
- account: account_address(opts_pluck(opts, :account))
134
- ).removed == 1
124
+ def remove_account(*args)
125
+ account_remove(pluck_argument(args, :account)).removed == 1
135
126
  end
136
127
 
137
128
  def representative
138
129
  wallet_representative.representative
139
130
  end
140
131
 
141
- def republish(opts)
142
- wallet_republish(count: opts_pluck(opts, :count)).blocks
132
+ def republish(*args)
133
+ wallet_republish(pluck_argument(args, :count)).blocks
143
134
  end
144
135
 
145
136
  def account_work_set(account:, work:)
146
137
  work_set(
147
- account: account_address(account),
138
+ account: object_to_value(account),
148
139
  work: work
149
140
  ).success == ''
150
141
  end
151
142
  alias set_account_work account_work_set
152
143
 
153
- def representative_set(opts)
144
+ def representative_set(*args)
154
145
  wallet_representative_set(
155
- representative: opts_pluck(opts, :representative)
146
+ pluck_argument(args, :representative)
156
147
  ).set == 1
157
148
  end
158
149
  alias set_representative representative_set
159
150
 
160
151
  def send_nano(from:, to:, amount:, work: nil)
161
152
  send_currency(
162
- source: wallet_seed(from),
163
- destination: wallet_seed(to),
153
+ source: object_to_value(from),
154
+ destination: object_to_value(to),
164
155
  amount: amount,
165
156
  work: work
166
157
  ).block
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  class Nano::Account
3
3
  include Nano::Proxy
4
- include Nano::AccountProxyHelper
4
+ include Nano::AccountHelper
5
5
 
6
- attr_accessor :address
6
+ attr_reader :address
7
7
 
8
8
  def initialize(address = nil, opts = {})
9
9
  unless address.is_a?(String)
@@ -31,7 +31,6 @@ class Nano::Account
31
31
  proxy_method :delegators
32
32
  proxy_method :delegators_count
33
33
  proxy_method :frontiers, required: %i[count]
34
- proxy_method :frontier_count
35
34
  proxy_method :ledger,
36
35
  required: %i[count],
37
36
  optional: %i[representative weight pending sorting]
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  class Nano::Accounts
3
3
  include Nano::Proxy
4
- include Nano::AccountsProxyHelper
4
+ include Nano::AccountsHelper
5
5
 
6
- attr_accessor :addresses
6
+ attr_reader :addresses
7
7
 
8
8
  def initialize(addresses = nil, opts = {})
9
9
  unless addresses.is_a?(Array)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  class Nano::Node
3
3
  include Nano::Proxy
4
- include Nano::NodeProxyHelper
4
+ include Nano::NodeHelper
5
5
 
6
6
  proxy_method :available_supply
7
7
  proxy_method :block, required: %i[hash]
@@ -17,6 +17,7 @@ class Nano::Node
17
17
  proxy_method :bootstrap_any
18
18
  proxy_method :chain, required: %i[block count]
19
19
  proxy_method :deterministic_key, required: %i[seed index]
20
+ proxy_method :frontier_count
20
21
  proxy_method :history, required: %i[hash count]
21
22
  proxy_method :keepalive, required: %i[address port]
22
23
  proxy_method :key_create
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  class Nano::Wallet
3
3
  include Nano::Proxy
4
- include Nano::WalletProxyHelper
4
+ include Nano::WalletHelper
5
5
 
6
- attr_accessor :seed
6
+ attr_reader :seed
7
7
 
8
8
  def initialize(seed = nil, opts = {})
9
9
  unless seed.is_a?(String)
@@ -15,6 +15,11 @@ class Nano::Wallet
15
15
  super(opts)
16
16
  end
17
17
 
18
+ # Hide secret seed on object inspection
19
+ def inspect
20
+ "#{inspect_prefix}, @client=#{@client.inspect}>"
21
+ end
22
+
18
23
  proxy_params wallet: :seed
19
24
 
20
25
  proxy_method :account_create, optional: %i[work]
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
  module Nano::Proxy
3
+ include Nano::ApplicationHelper
4
+
3
5
  attr_accessor :client
4
6
 
5
7
  def initialize(opts = {})
6
8
  @client = opts[:client] || Nano.client
9
+ self.class.proxy_methods.each { |m| define_proxy_method(m) }
7
10
  end
8
11
 
9
12
  def self.included(base)
@@ -24,42 +27,33 @@ module Nano::Proxy
24
27
  def proxy_methods
25
28
  proxy_method_def.keys.sort
26
29
  end
27
-
28
- def methods
29
- (super + proxy_methods).sort
30
- end
31
30
  end
32
31
 
33
32
  def proxy_methods
34
33
  self.class.proxy_methods
35
34
  end
36
35
 
37
- def methods
38
- (super + proxy_methods).sort
39
- end
40
-
41
36
  private
42
37
 
43
- def method_missing(m, *args, &_block)
44
- return super unless methods.include?(m)
45
- define_proxy_method(m)
46
- send(m, args.first)
47
- end
48
-
49
- def respond_to_missing?(m, include_private = false)
50
- methods.include?(m) || super
51
- end
52
-
53
38
  def define_proxy_method(m)
54
- self.class.send(:define_method, method_alias(m)) do |call_args = {}|
39
+ self.class.send(:define_method, method_alias(m)) do |*args|
55
40
  @m = m
56
- @call_args = call_args
41
+ @call_args = hashify_args(args)
57
42
 
58
43
  validate_params!
59
44
  execute_call
60
45
  end
61
46
  end
62
47
 
48
+ def hashify_args(args)
49
+ if args.first.is_a?(Hash)
50
+ args.first
51
+ else
52
+ (args[1].is_a?(Hash) ? args[1] : {})
53
+ .merge!(pluck_argument(args, required_params.first))
54
+ end
55
+ end
56
+
63
57
  def execute_call
64
58
  expose_nested_data(@client.call(@m, @call_args))
65
59
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Nano
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nano_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Craig-Kuhn (JCK)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-05 00:00:00.000000000 Z
11
+ date: 2018-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -154,11 +154,11 @@ files:
154
154
  - lib/nano_rpc.rb
155
155
  - lib/nano_rpc/client.rb
156
156
  - lib/nano_rpc/errors.rb
157
- - lib/nano_rpc/helpers/account_proxy_helper.rb
158
- - lib/nano_rpc/helpers/accounts_proxy_helper.rb
157
+ - lib/nano_rpc/helpers/account_helper.rb
158
+ - lib/nano_rpc/helpers/accounts_helper.rb
159
159
  - lib/nano_rpc/helpers/application_helper.rb
160
- - lib/nano_rpc/helpers/node_proxy_helper.rb
161
- - lib/nano_rpc/helpers/wallet_proxy_helper.rb
160
+ - lib/nano_rpc/helpers/node_helper.rb
161
+ - lib/nano_rpc/helpers/wallet_helper.rb
162
162
  - lib/nano_rpc/proxies/account.rb
163
163
  - lib/nano_rpc/proxies/accounts.rb
164
164
  - lib/nano_rpc/proxies/node.rb
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- module Nano::NodeProxyHelper
3
- include Nano::ApplicationHelper
4
-
5
- def account_containing_block(opts)
6
- block_account(opts_hash(opts)).account
7
- end
8
-
9
- def available_nano
10
- available_supply.available
11
- end
12
-
13
- def create_wallet
14
- Nano::Wallet.new(wallet_create.wallet)
15
- end
16
-
17
- def pending_exists?(opts)
18
- pending_exists(opts_hash(opts)).exists == 1
19
- end
20
-
21
- def work_valid?(work:, hash:)
22
- work_validate(work: work, hash: hash).valid == 1
23
- end
24
-
25
- private
26
-
27
- def opts_hash(opts)
28
- { hash: opts_pluck(opts, :hash) }
29
- end
30
- end