nis-ruby 0.0.7 → 0.0.9

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
  SHA1:
3
- metadata.gz: 061a83269fcef596e746171c60d75f215c1ee99c
4
- data.tar.gz: 943f352e756874a0622196046708e533c2b43a56
3
+ metadata.gz: a3cee816f2425c27c2f6c1e0cc6bf4e08a0b8db1
4
+ data.tar.gz: 893039b8833f10bcb9802e04d1819c73fd1ee662
5
5
  SHA512:
6
- metadata.gz: 430fbd5bebbfd64f3edef98fca026873a7255b710e987af87dc77bd09597f8111aa3c04e15eb2ef6c0b5f7df91d4636cdff685c02f5d02030cfede35c5d53679
7
- data.tar.gz: a32054344daeef7210c1c4dfbadf08629a2fb213d4f209e545ab36dec02bb56e3f308daeaa50f1bfaeabdd37bec75918ea223f5876db1ff4660e4e58aac46542
6
+ metadata.gz: a3f908e45854fca103743aef337b0f87970922e46c0b7c86165c4aa2ffdff6751528044a524e31820907c780bdb3cf5ce9db346bd9b56f563748925a196c4504
7
+ data.tar.gz: 79fe99b46a5c77f8e17998f13a6083716602af36b460a702ce66613126ca9cae1f9d3333e05860cdd569525489c89c079e4ff565b35fc9d742b50b13ecc1cc0c
data/README.md CHANGED
@@ -29,6 +29,10 @@ gem 'nis-ruby'
29
29
 
30
30
  ## Usage
31
31
 
32
+ More specific demonstrations are in samples/ directory.
33
+
34
+ ### Methods
35
+
32
36
  ```ruby
33
37
  nis = Nis.new
34
38
 
@@ -39,6 +43,12 @@ nis.heartbeat
39
43
  nis.status
40
44
  # => {code: 6, type: 4, message: "status"}
41
45
  # See http://bob.nem.ninja/docs/#status-request
46
+ ```
47
+
48
+ ### Requesting
49
+
50
+ ```ruby
51
+ nis = Nis.new
42
52
 
43
53
  nis.request(:get, '/account/get',
44
54
  address: 'TALICELCD3XPH4FFI5STGGNSNSWPOTG5E4DS2TOS'
@@ -52,7 +62,7 @@ nis.request(:post, '/account/unlock',
52
62
  # => Nothing
53
63
  # See http://bob.nem.ninja/docs/#locking-and-unlocking-accounts
54
64
 
55
- t = {
65
+ tx = {
56
66
  amount: 10_000_000,
57
67
  fee: 3_000_000,
58
68
  recipient: 'TALICELCD3XPH4FFI5STGGNSNSWPOTG5E4DS2TOS',
@@ -67,15 +77,13 @@ t = {
67
77
  version: -1_744_830_463 # testnet version 1
68
78
  }
69
79
  puts nis.request! :post, 'transaction/prepare-announce', {
70
- transaction: t,
80
+ transaction: tx,
71
81
  privateKey: '68e4f79f886927de698df4f857de2aada41ccca6617e56bb0d61623b35b08cc0'
72
82
  }
73
83
  # => {innerTransactionHash: {}, code: 1, type: 1, message: "SUCCESS", transactionHash: {data: "9da41fd6c6886740ae6a15c869df0470015d78103e5b216971aa09fdbcce9cde"}}
74
84
  # See http://bob.nem.ninja/docs/#initiating-a-transfer-transaction
75
85
  ```
76
86
 
77
- More specific demonstrations are in demo/ directory.
78
-
79
87
 
80
88
  ## Commandline
81
89
 
@@ -116,8 +124,10 @@ Nis.new(url: 'http://bigalice3.nem.ninja:7890')
116
124
 
117
125
  ## TODO
118
126
 
119
- - Do more improvements.
120
-
127
+ * Do more improvements.
128
+ * Implementation Apostille
129
+ * Implementation Mosaic
130
+ * Implementation Encryption
121
131
 
122
132
  ## Documentation
123
133
 
data/lib/nis/client.rb CHANGED
@@ -14,6 +14,14 @@ class Nis::Client
14
14
  timeout: 5
15
15
  }.freeze
16
16
 
17
+ LOCAL_ONLY_PATHES = [
18
+ '/local/account/transfers/incoming',
19
+ '/local/account/transfers/outgoing',
20
+ '/local/account/transfers/all',
21
+ '/node/boot',
22
+ '/transaction/prepare-announce'
23
+ ]
24
+
17
25
  attr_reader :options
18
26
 
19
27
  # @param [hash] options HTTP Client connection information
@@ -31,6 +39,9 @@ class Nis::Client
31
39
  # @param [Hash] params API Parameters
32
40
  # @return [Hash] Hash converted API Response
33
41
  def request(method, path, params = {})
42
+ if connection.remote? && local_only?(path)
43
+ raise Nis::Error, "The request (#{method} #{path}) is only permitted to local NIS."
44
+ end
34
45
  if params.is_a?(Hash) && !params.empty?
35
46
  params.reject! { |_, value| value.nil? }
36
47
  end
@@ -53,6 +64,10 @@ class Nis::Client
53
64
 
54
65
  private
55
66
 
67
+ def local_only?(path)
68
+ LOCAL_ONLY_PATHES.include?(path)
69
+ end
70
+
56
71
  def connection
57
72
  @connection ||= Faraday.new(url: @options[:url]) do |f|
58
73
  f.options[:timeout] = @options[:timeout]
@@ -61,7 +76,7 @@ class Nis::Client
61
76
  # logger.filter(/(privateKey=)(\w+)/,'\1[FILTERED]')
62
77
  # end
63
78
  f.adapter Faraday.default_adapter
64
- end
79
+ end.tap { |c| c.extend(Local) }
65
80
  end
66
81
 
67
82
  def parse_body(body)
@@ -103,4 +118,14 @@ class Nis::Client
103
118
 
104
119
  options
105
120
  end
121
+
122
+ module Local
123
+ def local?
124
+ host == '127.0.0.1' || host == 'localhost'
125
+ end
126
+
127
+ def remote?
128
+ !local?
129
+ end
130
+ end
106
131
  end
@@ -1,6 +1,6 @@
1
1
  module Nis::Endpoint
2
2
  module Node::ActivePeers
3
- # @return [Nis::Struct::NodeInfo]
3
+ # @return [Nis::Struct::Node]
4
4
  # @see http://bob.nem.ninja/docs/#maximum-chain-height-in-the-active-neighborhood
5
5
  def node_active_peers_max_chain_height
6
6
  request!(:get, '/node/active-peers/max-chain-height') do |res|
@@ -1,6 +1,6 @@
1
1
  module Nis::Endpoint
2
2
  module Node::ExtendedInfo
3
- # @return [Nis::Struct::NodeInfo]
3
+ # @return [Nis::Struct::Node]
4
4
  # @see http://bob.nem.ninja/docs/#extended-node-information
5
5
  def node_extended_info
6
6
  Nis::Struct::NisNodeInfo.build request!(:get, '/node/extended-info')
@@ -1,9 +1,9 @@
1
1
  module Nis::Endpoint
2
2
  module Node::Info
3
- # @return [Nis::Struct::NodeInfo]
3
+ # @return [Nis::Struct::Node]
4
4
  # @see http://bob.nem.ninja/docs/#basic-node-information
5
5
  def node_info
6
- Nis::Struct::NodeInfo.build request!(:get, '/node/info')
6
+ Nis::Struct::Node.build request!(:get, '/node/info')
7
7
  end
8
8
  end
9
9
  end
@@ -6,19 +6,19 @@ module Nis::Endpoint
6
6
  Nis::Struct::NodeCollection.build request!(:get, '/node/peer-list/all')
7
7
  end
8
8
 
9
- # @return [Array <Nis::Struct::NodeInfo>]
9
+ # @return [Array <Nis::Struct::Node>]
10
10
  # @see http://bob.nem.ninja/docs/#reachable-neighborhood
11
11
  def node_peerlist_reachable
12
12
  request!(:get, '/node/peer-list/reachable') do |res|
13
- res[:data].map { |n| Nis::Struct::NodeInfo.build(n) }
13
+ res[:data].map { |n| Nis::Struct::Node.build(n) }
14
14
  end
15
15
  end
16
16
 
17
- # @return [Array <Nis::Struct::NodeInfo>]
17
+ # @return [Array <Nis::Struct::Node>]
18
18
  # @see http://bob.nem.ninja/docs/#active-neighborhood
19
19
  def node_peerlist_active
20
20
  request!(:get, '/node/peer-list/active') do |res|
21
- res[:data].map { |n| Nis::Struct::NodeInfo.build(n) }
21
+ res[:data].map { |n| Nis::Struct::Node.build(n) }
22
22
  end
23
23
  end
24
24
  end
@@ -1,7 +1,7 @@
1
1
  module Nis::Endpoint
2
2
  module Transaction::Announce
3
3
  # @param [Nis::Struct::RequestAnnounce] request_announce
4
- # @return [Nis::Struct::NodeInfo]
4
+ # @return [Nis::Struct::Node]
5
5
  # @see http://bob.nem.ninja/docs/#sending-the-data-to-NIS
6
6
  def transaction_announce(request_announce:)
7
7
  Nis::Struct::NemAnnounceResult.build request!(:post,
@@ -1,7 +1,7 @@
1
1
  module Nis::Endpoint
2
2
  module Transaction::PrepareAnnounce
3
3
  # @param [Nis::Struct::RequestPrepareAnnounce] request_prepare_announce
4
- # @return [Nis::Struct::NodeInfo]
4
+ # @return [Nis::Struct::Node]
5
5
  # @see http://bob.nem.ninja/docs/#initiating-a-transaction
6
6
  def transaction_prepare_announce(request_prepare_announce:)
7
7
  Nis::Struct::NemAnnounceResult.build request!(:post,
@@ -1,5 +1,5 @@
1
1
  class Nis::Struct
2
- # @attr [Nis::Struct::NodeInfo] node
2
+ # @attr [Nis::Struct::Node] node
3
3
  # @attr [Integer] syncs
4
4
  # @attr [Nis::Struct::NodeExperience] experience
5
5
  # @see http://bob.nem.ninja/docs/#extendedNodeExperiencePair
@@ -0,0 +1,55 @@
1
+ class Nis::Struct
2
+ # @attr [String] value
3
+ # @attr [Integer] type
4
+ # @attr [String] payload
5
+ class Message
6
+ attr_accessor :value, :type, :payload
7
+
8
+ TYPE_PLAIN = 1
9
+ TYPE_ENCRYPTED = 2
10
+
11
+ def initialize(value = '')
12
+ @value = value
13
+ @type = TYPE_PLAIN
14
+ end
15
+
16
+ # @return [Boolean]
17
+ def encrypted?
18
+ @type == TYPE_ENCRYPTED
19
+ end
20
+
21
+ # @return [Boolean]
22
+ def plain?
23
+ @type == TYPE_PLAIN
24
+ end
25
+
26
+ # @return [Integer]
27
+ def bytesize
28
+ payload.bytesize
29
+ end
30
+
31
+ # @return [Boolean]
32
+ def valid?
33
+ bytesize <= 512
34
+ end
35
+
36
+ # @return [Hash]
37
+ def to_hash
38
+ { payload: payload, type: @type }
39
+ end
40
+
41
+ # @return [String]
42
+ def to_s
43
+ @value
44
+ end
45
+
46
+ # @return [Boolean]
47
+ def ==(other)
48
+ @value == other.value
49
+ end
50
+
51
+ def payload
52
+ @payload ||= value.unpack('H*').join
53
+ end
54
+ end
55
+ end
@@ -13,6 +13,8 @@ class Nis::Struct
13
13
  alias :inner_transaction_hash :innerTransactionHash
14
14
 
15
15
  def self.build(attrs)
16
+ attrs[:transactionHash] = Nis::Unit::Hash.new(attrs[:transactionHash][:data])
17
+ attrs[:innerTransactionHash] = Nis::Unit::Hash.new(attrs[:innerTransactionHash][:data])
16
18
  new(attrs)
17
19
  end
18
20
  end
@@ -1,6 +1,6 @@
1
1
  class Nis::Struct
2
2
  # @attr [Nis::Struct::NodeInfo] node
3
- # @attr [Nis::Struct::NodeInfo] nis_info
3
+ # @attr [Nis::Struct::NodeInfo] nisInfo
4
4
  # @see http://bob.nem.ninja/docs/#nisNodeInfo
5
5
  class NisNodeInfo
6
6
  include Nis::Util::Assignable
@@ -11,8 +11,8 @@ class Nis::Struct
11
11
 
12
12
  def self.build(attrs)
13
13
  new(
14
- node: Nis::Struct::NodeInfo.build(attrs[:node]),
15
- nisInfo: Nis::Struct::NodeInfo.build(attrs[:nisInfo])
14
+ node: Nis::Struct::Node.build(attrs[:node]),
15
+ nisInfo: Nis::Struct::ApplicationMetaData.build(attrs[:nisInfo])
16
16
  )
17
17
  end
18
18
  end
@@ -0,0 +1,16 @@
1
+ class Nis::Struct
2
+ # @attr [String] metaData
3
+ # @attr [String] networkId
4
+ # @attr [String] identity
5
+ # @see http://bob.nem.ninja/docs/#node
6
+ class Node
7
+ include Nis::Util::Assignable
8
+ attr_accessor :metaData, :endpoint, :identity
9
+
10
+ alias :meta_data :metaData
11
+
12
+ def self.build(attrs)
13
+ new(attrs)
14
+ end
15
+ end
16
+ end
@@ -10,10 +10,10 @@ class Nis::Struct
10
10
 
11
11
  def self.build(attrs)
12
12
  new(
13
- inactive: attrs[:inactive].map { |n| NodeInfo.build(n) },
14
- active: attrs[:active].map { |n| NodeInfo.build(n) },
15
- busy: attrs[:busy].map { |n| NodeInfo.build(n) },
16
- failure: attrs[:failure].map { |n| NodeInfo.build(n) }
13
+ inactive: attrs[:inactive].map { |n| Node.build(n) },
14
+ active: attrs[:active].map { |n| Node.build(n) },
15
+ busy: attrs[:busy].map { |n| Node.build(n) },
16
+ failure: attrs[:failure].map { |n| Node.build(n) }
17
17
  )
18
18
  end
19
19
  end
@@ -5,7 +5,7 @@ class Nis::Struct
5
5
  # @attr [String] recipient
6
6
  # @attr [Integer] type
7
7
  # @attr [Integer] deadline
8
- # @attr [String] message
8
+ # @attr [Nis::Struct::Message] message
9
9
  # @attr [Integer] version
10
10
  # @attr [String] signer
11
11
  # @attr [Array <Nis::Struct::MosaicId>] mosaics
@@ -41,8 +41,6 @@ class Nis::Struct
41
41
  MAINNET_VERSION_1 = MAINNET | VERSION_1 # 0x68000001 = 1744830465
42
42
  MAINNET_VERSION_2 = MAINNET | VERSION_2 # 0x68000002 = 1744830466
43
43
 
44
- @mosaics = []
45
-
46
44
  def self.build(attrs)
47
45
  new(attrs)
48
46
  end
@@ -61,5 +59,39 @@ class Nis::Struct
61
59
  def mainnet?
62
60
  (0x0000000F & @version) == MAINNET
63
61
  end
62
+
63
+ # @return [Integer]
64
+ def fee
65
+ @fee ||= calculate_fee
66
+ end
67
+
68
+ def mosaics
69
+ @mosaics ||= []
70
+ end
71
+
72
+ alias to_hash_old to_hash
73
+
74
+ def to_hash
75
+ fee
76
+ to_hash_old
77
+ end
78
+
79
+ # @return [Integer]
80
+ def calculate_fee
81
+ if mosaics.empty?
82
+ tmp_fee = [1, amount / 1_000_000 / 10000].max
83
+ fee = (tmp_fee > 25 ? 25 : tmp_fee)
84
+ else
85
+ # TODO: calc mosaics fee
86
+ raise NotImplementedError, 'not implemented calculation mosaic fee.'
87
+ fee = 25
88
+ end
89
+
90
+ if message.bytesize > 0
91
+ fee += [1, (message.bytesize / 2 / 32) + 1].max
92
+ end
93
+
94
+ fee * 1_000_000
95
+ end
64
96
  end
65
97
  end
data/lib/nis/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Nis
2
- VERSION = '0.0.7'.freeze
2
+ VERSION = '0.0.9'.freeze
3
3
  end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -7,18 +7,15 @@ A_PRIVATE_KEY = '00b4a68d16dc505302e9631b860664ba43a8183f0903bc5782a2403b2f9eb3c
7
7
  A_PUBLIC_KEY = '5aff2e991f85d44eed8f449ede365a920abbefc22f1a2f731d4a002258673519'.freeze
8
8
 
9
9
  # Account B (Dist)
10
- B_ADDRESS = 'TAFPFQOTRYEKMKWWKLLLMYA3I5SCFDGYFACCOFWS'.freeze
10
+ B_ADDRESS = 'TA4TX6U5HG2MROAESH2JE5524T4ZOY2EQKQ6ELHF'.freeze
11
11
 
12
12
  # build Transaction Object
13
- t = Nis::Struct::Transaction.new(
13
+ tx = Nis::Struct::Transaction.new(
14
14
  amount: 1_000_000,
15
- fee: 1_000_000,
15
+ fee: 3_000_000,
16
16
  recipient: B_ADDRESS,
17
17
  signer: A_PUBLIC_KEY,
18
- message: {
19
- payload: '',
20
- type: 1
21
- },
18
+ message: Nis::Struct::Message.new('Hello'),
22
19
  type: Nis::Struct::Transaction::TRANSFER,
23
20
  timeStamp: Nis::Util.timestamp,
24
21
  deadline: Nis::Util.timestamp + 43_200,
@@ -27,7 +24,7 @@ t = Nis::Struct::Transaction.new(
27
24
 
28
25
  # build RequestPrepareAnnounce Object
29
26
  rpa = Nis::Struct::RequestPrepareAnnounce.new(
30
- transaction: t,
27
+ transaction: tx,
31
28
  privateKey: A_PRIVATE_KEY
32
29
  )
33
30
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nis-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshiyuki Ieyama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-11 00:00:00.000000000 Z
11
+ date: 2017-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -167,14 +167,6 @@ files:
167
167
  - README.md
168
168
  - Rakefile
169
169
  - bin/nis
170
- - demo/request_account.rb
171
- - demo/request_block.rb
172
- - demo/request_debug.rb
173
- - demo/request_local.rb
174
- - demo/request_namespace.rb
175
- - demo/request_nis.rb
176
- - demo/request_node.rb
177
- - demo/request_transaction.rb
178
170
  - lib/nis.rb
179
171
  - lib/nis/client.rb
180
172
  - lib/nis/endpoint.rb
@@ -239,6 +231,7 @@ files:
239
231
  - lib/nis/struct/extended_node_experience_pair.rb
240
232
  - lib/nis/struct/harvest_info.rb
241
233
  - lib/nis/struct/key_pair_view_model.rb
234
+ - lib/nis/struct/message.rb
242
235
  - lib/nis/struct/mosaic.rb
243
236
  - lib/nis/struct/mosaic_definition.rb
244
237
  - lib/nis/struct/mosaic_definition_meta_data.rb
@@ -253,9 +246,9 @@ files:
253
246
  - lib/nis/struct/nem_async_timer_visitor.rb
254
247
  - lib/nis/struct/nem_request_result.rb
255
248
  - lib/nis/struct/nis_node_info.rb
249
+ - lib/nis/struct/node.rb
256
250
  - lib/nis/struct/node_collection.rb
257
251
  - lib/nis/struct/node_experience.rb
258
- - lib/nis/struct/node_info.rb
259
252
  - lib/nis/struct/request_announce.rb
260
253
  - lib/nis/struct/request_prepare_announce.rb
261
254
  - lib/nis/struct/time_synchronization_result.rb
@@ -276,6 +269,14 @@ files:
276
269
  - lib/nis/util/assignable.rb
277
270
  - lib/nis/version.rb
278
271
  - nis.gemspec
272
+ - samples/request_account.rb
273
+ - samples/request_block.rb
274
+ - samples/request_debug.rb
275
+ - samples/request_local.rb
276
+ - samples/request_namespace.rb
277
+ - samples/request_nis.rb
278
+ - samples/request_node.rb
279
+ - samples/request_transaction.rb
279
280
  homepage: https://github.com/44uk/nis-ruby
280
281
  licenses:
281
282
  - MIT
@@ -1,28 +0,0 @@
1
- class Nis::Struct
2
- # @attr [String] metaData
3
- # @attr [String] features
4
- # @attr [String] networkId
5
- # @attr [String] application
6
- # @attr [String] version
7
- # @attr [String] platform
8
- # @attr [String] endpoint
9
- # @attr [String] protocol
10
- # @attr [String] port
11
- # @attr [String] host
12
- # @attr [String] identity
13
- # @attr [String] name
14
- # @attr [String] publicKey
15
- # @see http://bob.nem.ninja/docs/#nodeInfo
16
- class NodeInfo
17
- include Nis::Util::Assignable
18
- attr_accessor :metaData, :features, :networkId, :application, :version, :platform, :endpoint, :protocol, :port, :host, :identity, :name, :publicKey
19
-
20
- alias :meta_data :metaData
21
- alias :network_id :networkId
22
- alias :public_key :publicKey
23
-
24
- def self.build(attrs)
25
- new(attrs)
26
- end
27
- end
28
- end