nis-ruby 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +43 -33
  3. data/examples/account/generate.rb +29 -0
  4. data/examples/account/get.rb +26 -0
  5. data/examples/account/harvests.rb +8 -0
  6. data/examples/account/historical.rb +13 -0
  7. data/examples/account/importances.rb +8 -0
  8. data/examples/account/lock.rb +8 -0
  9. data/examples/account/mosaic.rb +9 -0
  10. data/examples/account/namespace.rb +8 -0
  11. data/examples/account/status.rb +8 -0
  12. data/examples/account/transfers.rb +16 -0
  13. data/examples/account/unconfirmed_transactions.rb +8 -0
  14. data/examples/account/unlock.rb +8 -0
  15. data/examples/account/unlocked.rb +8 -0
  16. data/examples/block/at_public.rb +4 -0
  17. data/examples/block/get.rb +4 -0
  18. data/examples/debug/connections.rb +13 -0
  19. data/examples/debug/time_synchronization.rb +6 -0
  20. data/examples/local/account_transfers.rb +20 -0
  21. data/examples/local/chain_blocks_after.rb +8 -0
  22. data/examples/namespace/mosaic_definition.rb +6 -0
  23. data/examples/namespace/root.rb +6 -0
  24. data/examples/nis.rb +11 -7
  25. data/examples/node/active_peers.rb +6 -0
  26. data/examples/node/boot.rb +25 -0
  27. data/examples/node/info.rb +8 -0
  28. data/examples/node/peerlist.rb +14 -0
  29. data/examples/time_sync/network_time.rb +6 -0
  30. data/examples/transactions/get.rb +42 -0
  31. data/examples/transactions/importance_transfer.rb +6 -4
  32. data/examples/transactions/mosaic_definition_creation.rb +6 -4
  33. data/examples/transactions/mosaic_supply_change.rb +6 -4
  34. data/examples/transactions/multisig.rb +6 -4
  35. data/examples/transactions/multisig_add_cosignatory.rb +3 -1
  36. data/examples/transactions/multisig_aggregate_modification.rb +6 -4
  37. data/examples/transactions/multisig_signature.rb +6 -3
  38. data/examples/transactions/provision_namespace.rb +6 -4
  39. data/examples/transactions/transfer.rb +14 -7
  40. data/examples/transactions/transfer_mosaic.rb +5 -3
  41. data/examples/transactions/transfer_remote.rb +6 -4
  42. data/lib/nis.rb +6 -0
  43. data/lib/nis/client.rb +11 -0
  44. data/lib/nis/configuration.rb +23 -0
  45. data/lib/nis/endpoint/account/transfers.rb +24 -0
  46. data/lib/nis/endpoint/debug/connections.rb +17 -0
  47. data/lib/nis/endpoint/local/account/transfers.rb +14 -0
  48. data/lib/nis/endpoint/node/peer_list.rb +20 -0
  49. data/lib/nis/endpoint/time_sync.rb +4 -0
  50. data/lib/nis/endpoint/time_sync/network_time.rb +10 -0
  51. data/lib/nis/endpoint/transaction/get.rb +12 -0
  52. data/lib/nis/keypair.rb +13 -7
  53. data/lib/nis/struct.rb +1 -0
  54. data/lib/nis/struct/importance_transfer_transaction.rb +10 -0
  55. data/lib/nis/struct/message.rb +9 -2
  56. data/lib/nis/struct/mosaic_definition_creation_transaction.rb +15 -0
  57. data/lib/nis/struct/mosaic_supply_change_transaction.rb +12 -0
  58. data/lib/nis/struct/multisig_aggregate_modification_transaction.rb +10 -0
  59. data/lib/nis/struct/multisig_signature_transaction.rb +12 -0
  60. data/lib/nis/struct/multisig_transaction.rb +15 -0
  61. data/lib/nis/struct/network_time.rb +17 -0
  62. data/lib/nis/struct/provision_namespace_transaction.rb +16 -0
  63. data/lib/nis/struct/transaction.rb +4 -81
  64. data/lib/nis/struct/transaction_meta_data_pair.rb +17 -1
  65. data/lib/nis/struct/transfer_transaction.rb +11 -0
  66. data/lib/nis/unit/address.rb +11 -4
  67. data/lib/nis/util.rb +3 -1
  68. data/lib/nis/util/convert.rb +44 -51
  69. data/lib/nis/util/deserializer.rb +62 -77
  70. data/lib/nis/util/ed25519.rb +10 -12
  71. data/lib/nis/util/serializer.rb +117 -185
  72. data/lib/nis/version.rb +1 -1
  73. data/nis.gemspec +0 -3
  74. metadata +43 -26
  75. data/bin/nis +0 -60
  76. data/examples/account.rb +0 -44
  77. data/examples/block.rb +0 -9
  78. data/examples/debug.rb +0 -33
  79. data/examples/local.rb +0 -19
  80. data/examples/namespace.rb +0 -9
  81. data/examples/node.rb +0 -44
  82. data/examples/shutdown.rb +0 -6
@@ -44,5 +44,29 @@ module Nis::Endpoint
44
44
  res[:data].map { |tmdp| Nis::Struct::TransactionMetaDataPair.build(tmdp) }
45
45
  end
46
46
  end
47
+
48
+ # @param [Symbol] dir direction(:in, :out, :all)
49
+ # @param [String] address
50
+ # @param [String] hash
51
+ # @param [String] id
52
+ # @return [Array <Nis::Struct::TransactionMetaDataPair>]
53
+ # @see https://nemproject.github.io/#requesting-transaction-data-for-an-account
54
+ def account_transfers(dir = :all, address:, hash: nil, id: nil)
55
+ request!(:get, "/account/transfers/#{account_transfers_direction(dir)}",
56
+ address: address,
57
+ hash: hash,
58
+ id: id
59
+ ) do |res|
60
+ res[:data].map { |tmdp| Nis::Struct::TransactionMetaDataPair.build(tmdp) }
61
+ end
62
+ end
63
+
64
+ def account_transfers_direction(dir)
65
+ case dir.to_s
66
+ when /\Ai/ then :incoming
67
+ when /\Ao/ then :outgoing
68
+ else :all
69
+ end
70
+ end
47
71
  end
48
72
  end
@@ -29,5 +29,22 @@ module Nis::Endpoint
29
29
  res[:data].map { |natv| Nis::Struct::NemAsyncTimerVisitor.build(natv) }
30
30
  end
31
31
  end
32
+
33
+ def debug_connections(dir)
34
+ request!(:get, "/debug/connections/#{debug_connections_direction(dir)}") do |res|
35
+ Nis::Struct::AuditCollection.build(
36
+ outstanding: res[:outstanding],
37
+ most_recent: res[:'most-recent']
38
+ )
39
+ end
40
+ end
41
+
42
+ def debug_connections_direction(dir)
43
+ case dir.to_s
44
+ when /\Ai/ then :incoming
45
+ when /\Ao/ then :outgoing
46
+ else raise "Undefined direction: #{dir}"
47
+ end
48
+ end
32
49
  end
33
50
  end
@@ -28,6 +28,20 @@ module Nis::Endpoint
28
28
  res[:data].map { |tmdp| Nis::Struct::TransactionMetaDataPair.build(tmdp) }
29
29
  end
30
30
  end
31
+
32
+ def local_account_transfers(dir = :all, page:)
33
+ request!(:post, "/local/account/transfers/#{local_account_transfers_direction(dir)}", page) do |res|
34
+ res[:data].map { |tmdp| Nis::Struct::TransactionMetaDataPair.build(tmdp) }
35
+ end
36
+ end
37
+
38
+ def local_account_transfers_direction(dir)
39
+ case dir.to_s
40
+ when /\Ai/ then :incoming
41
+ when /\Ao/ then :outgoing
42
+ else :all
43
+ end
44
+ end
31
45
  end
32
46
  end
33
47
  end
@@ -21,5 +21,25 @@ module Nis::Endpoint
21
21
  res[:data].map { |n| Nis::Struct::Node.build(n) }
22
22
  end
23
23
  end
24
+
25
+ # @return [Array <Nis::Struct::Node>]
26
+ # @see https://nemproject.github.io/#active-neighborhood
27
+ def node_peerlist(state = :all)
28
+ if node_peerlist_state(state) == :all
29
+ node_peerlist_all
30
+ else
31
+ request!(:get, "/node/peer-list/#{node_peerlist_state(state)}") do |res|
32
+ res[:data].map { |n| Nis::Struct::Node.build(n) }
33
+ end
34
+ end
35
+ end
36
+
37
+ def node_peerlist_state(state)
38
+ case state.to_s
39
+ when 'active' then :active
40
+ when 'reachable' then :reachable
41
+ else :all
42
+ end
43
+ end
24
44
  end
25
45
  end
@@ -0,0 +1,4 @@
1
+ module Nis::Endpoint
2
+ module TimeSync
3
+ end
4
+ end
@@ -0,0 +1,10 @@
1
+ module Nis::Endpoint
2
+ module TimeSync::NetworkTime
3
+ # @return [Nis::Struct::NetworkTime]
4
+ def time_sync_network_time
5
+ request!(:get, '/time-sync/network-time') do |res|
6
+ Nis::Struct::NetworkTime.build res
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ module Nis::Endpoint
2
+ module Transaction::Get
3
+ # @param [String] hash
4
+ # @return [Nis::Struct::TransactionMetaDataPair]
5
+ def transaction_get(hash:)
6
+ Nis::Struct::TransactionMetaDataPair.build request!(:get,
7
+ '/transaction/get',
8
+ hash: hash
9
+ )
10
+ end
11
+ end
12
+ end
@@ -2,8 +2,8 @@ class Nis
2
2
  class Keypair
3
3
  attr_reader :private, :public
4
4
 
5
- # @param [String] Private Key
6
- # @option options [Strung] :public_key Public Key
5
+ # @param [String] Hex Private Key
6
+ # @option options [Strung] :public_key Hex Public Key
7
7
  def initialize(private_key, public_key: nil)
8
8
  @private = private_key
9
9
  @public = public_key || calc_public_key
@@ -12,8 +12,8 @@ class Nis
12
12
  # @param [String] Hex string
13
13
  # @return [String] Signed hex string
14
14
  def sign(data)
15
- bin_data = data.scan(/../).map(&:hex).pack('C*')
16
- bin_signed = Nis::Util::Ed25519.signature_hash_unsafe(bin_data, @bin_secret, @bin_public)
15
+ bin_data = Nis::Util::Convert.hex2bin(data)
16
+ bin_signed = Nis::Util::Ed25519.signature_hash_unsafe(bin_data, bin_secret, bin_public)
17
17
  bin_signed.unpack('H*').first
18
18
  end
19
19
 
@@ -24,9 +24,15 @@ class Nis
24
24
  end
25
25
 
26
26
  def calc_public_key
27
- @bin_secret = fix_private_key(@private).scan(/../).map(&:hex).reverse.pack('C*')
28
- @bin_public = Nis::Util::Ed25519.publickey_hash_unsafe(@bin_secret)
29
- @bin_public.unpack('H*').first
27
+ bin_public.unpack('H*').first
28
+ end
29
+
30
+ def bin_secret
31
+ @bin_secret ||= Nis::Util::Convert.hex2bin_rev(fix_private_key(@private))
32
+ end
33
+
34
+ def bin_public
35
+ @bin_public ||= Nis::Util::Ed25519.publickey_hash_unsafe(bin_secret)
30
36
  end
31
37
  end
32
38
  end
@@ -1 +1,2 @@
1
+ require File.expand_path('../struct/transaction.rb', __FILE__)
1
2
  Dir[File.expand_path('../struct/*.rb', __FILE__)].each { |f| require f }
@@ -0,0 +1,10 @@
1
+ class Nis::Struct
2
+ # @attr [Integer] mode
3
+ # @attr [String] remote_account
4
+ class ImportanceTransferTransaction < Transaction
5
+ attr_accessor :mode, :remoteAccount
6
+
7
+ alias :remote_account :remoteAccount
8
+ alias :remote_account= :remoteAccount=
9
+ end
10
+ end
@@ -4,11 +4,18 @@ class Nis::Struct
4
4
  # @attr [String] payload
5
5
  # @attr [String] public_key
6
6
  class Message
7
- attr_reader :value, :type, :public_key, :private_key
7
+ attr_reader :value, :type
8
+ attr_accessor :public_key, :private_key
8
9
 
9
10
  TYPE_PLAIN = 1
10
11
  TYPE_ENCRYPTED = 2
11
12
 
13
+ def self.build(attrs)
14
+ if attrs[:type] == TYPE_ENCRYPTED
15
+ new(attrs[:payload], type: :encrypted)
16
+ end
17
+ end
18
+
12
19
  def initialize(value = '', type: :plain, private_key: nil, public_key: nil)
13
20
  @value = value
14
21
  @type = (type == :encrypted) ? TYPE_ENCRYPTED : TYPE_PLAIN
@@ -66,7 +73,7 @@ class Nis::Struct
66
73
  end
67
74
 
68
75
  def payload
69
- encrypted? ? value : value.unpack('H*').join
76
+ encrypted? ? value : value.unpack('H*').first
70
77
  end
71
78
 
72
79
  private
@@ -0,0 +1,15 @@
1
+ class Nis::Struct
2
+ # @attr [Hash] mosaicDefinition
3
+ # @attr [String] creationFeeSink
4
+ # @attr [Integer] creationFee
5
+ class MosaicDefinitionCreationTransaction < Transaction
6
+ attr_accessor :mosaicDefinition, :creationFeeSink, :creationFee
7
+
8
+ alias :mosaic_definition :mosaicDefinition
9
+ alias :mosaic_definition= :mosaicDefinition=
10
+ alias :creation_fee_sink :creationFeeSink
11
+ alias :creation_fee_sink= :creationFeeSink=
12
+ alias :creation_fee :creationFee
13
+ alias :creation_fee= :creationFee=
14
+ end
15
+ end
@@ -0,0 +1,12 @@
1
+ class Nis::Struct
2
+ # @attr [String] mosaicId
3
+ # @attr [Integer] supplyType
4
+ # @attr [Integer] delta
5
+ class MosaicSupplyChangeTransaction < Transaction
6
+ attr_accessor :mosaicId, :supplyType, :delta
7
+
8
+ # def self.build
9
+ # TODO: mosaicId
10
+ # end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class Nis::Struct
2
+ # @attr [Array] modifications
3
+ # @attr [Hash] minCosignatories
4
+ class MultisigAggregationModificationTransaction < Transaction
5
+ attr_accessor :modifications, :minCosignatories
6
+
7
+ alias :min_cosignatories :minCosignatories
8
+ alias :min_cosignatories= :minCosignatories=
9
+ end
10
+ end
@@ -0,0 +1,12 @@
1
+ class Nis::Struct
2
+ # @attr [String] other_hash
3
+ # @attr [String] other_account
4
+ class MultisigSignatureTransaction < Transaction
5
+ attr_accessor :otherHash, :otherAccount
6
+
7
+ alias :other_hash :otherHash
8
+ alias :other_hash= :otherHash=
9
+ alias :other_account :otherAccount
10
+ alias :other_account= :otherAccount=
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ class Nis::Struct
2
+ # @attr [String] other_hash
3
+ # @attr [String] other_account
4
+ class MultisigTransaction < Transaction
5
+ attr_accessor :otherTrans
6
+
7
+ alias :other_trans :otherTrans
8
+ alias :other_trans= :otherTrans=
9
+
10
+ def self.build(attrs)
11
+ attrs[:otherTrans] = Nis::Struct::TransactionMetaDataPair.build_transaction_struct(attrs[:otherTrans])
12
+ new(attrs)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ class Nis::Struct
2
+ # @attr [Integer] sendTimeStamp
3
+ # @attr [Integer] receiveTimeStamp
4
+ class NetworkTime
5
+ include Nis::Util::Assignable
6
+ attr_accessor :sendTimeStamp, :receiveTimeStamp
7
+
8
+ alias :send_timestamp :sendTimeStamp
9
+ alias :send_timestamp= :sendTimeStamp=
10
+ alias :receive_timestamp :receiveTimeStamp
11
+ alias :receive_timestamp= :receiveTimeStamp=
12
+
13
+ def self.build(attrs)
14
+ new(attrs)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ class Nis::Struct
2
+ # @attr [String] rentalFeeSink
3
+ # @attr [Integer] rentalFee
4
+ # @attr [String] newPart
5
+ # @attr [String] parent
6
+ class ProvisionNamespaceTransaction < Transaction
7
+ attr_accessor :rentalFeeSink, :rentalFee, :newPart, :parent
8
+
9
+ alias :rental_fee_sink :rentalFeeSink
10
+ alias :rental_fee_sink= :rentalFeeSink=
11
+ alias :rental_fee :rentalFee
12
+ alias :rental_fee= :rentalFee=
13
+ alias :new_part :newPart
14
+ alias :new_part= :newPart=
15
+ end
16
+ end
@@ -1,97 +1,20 @@
1
1
  class Nis::Struct
2
- # @attr [Integer] timestamp
3
- # @attr [Integer] amount
4
- # @attr [Integer] fee
5
- # @attr [String] recipient
6
2
  # @attr [Integer] type
7
- # @attr [Integer] deadline
8
- # @attr [Nis::Struct::Message] message
9
3
  # @attr [Integer] version
4
+ # @attr [Integer] timestamp
10
5
  # @attr [String] signer
11
- # @attr [Array <Nis::Struct::MosaicId>] mosaics
6
+ # @attr [Integer] fee
7
+ # @attr [Integer] deadline
12
8
  # @see https://nemproject.github.io/#transaction
13
- # @see https://nemproject.github.io/#initiating-a-transfer-transaction
14
- # @see https://nemproject.github.io/#version-1-transfer-transactions
15
- # @see https://nemproject.github.io/#version-2-transfer-transactions
16
9
  class Transaction
17
10
  include Nis::Util::Assignable
18
- attr_accessor :timeStamp, :amount, :fee, :recipient, :type, :deadline, :message, :version, :signer,
19
- :mosaics
11
+ attr_accessor :type, :version, :timeStamp, :signer, :fee, :deadline
20
12
 
21
13
  alias :timestamp :timeStamp
22
14
  alias :timestamp= :timeStamp=
23
15
 
24
- TRANSFER = 0x0101 # 257 (transfer transaction)
25
- IMPORTANCE_TRANSFER = 0x0801 # 2049 (importance transfer transaction)
26
- MULTISIG_AGGREGATE_MODIFICATION_TRANSFER = 0x1001 # 4097 (multisig aggregate modification transfer transaction)
27
- MULTISIG_SIGNATURE = 0x1002 # 4098 (multisig signature transaction)
28
- MULTISIG = 0x1004 # 4099 (multisig transaction)
29
- PROVISION_NAMESPACE = 0x2001 # 8193 (provision namespace transaction)
30
- MOSAIC_DEFINITION_CREATION = 0x4001 # 16385 (mosaic definition creation transaction)
31
- MOSAIC_SUPPLY_CHANGE = 0x4002 # 16386 (mosaic supply change transaction)
32
-
33
- TESTNET = 0x98000000 # -1744830464
34
- MAINNET = 0x68000000 # 1744830464
35
-
36
- VERSION_1 = 0x00000001 # 1
37
- VERSION_2 = 0x00000002 # 2
38
-
39
- TESTNET_VERSION_1 = TESTNET | VERSION_1 # 0x98000001 = -1744830463
40
- TESTNET_VERSION_2 = TESTNET | VERSION_2 # 0x98000002 = -1744830462
41
- MAINNET_VERSION_1 = MAINNET | VERSION_1 # 0x68000001 = 1744830465
42
- MAINNET_VERSION_2 = MAINNET | VERSION_2 # 0x68000002 = 1744830466
43
-
44
16
  def self.build(attrs)
45
17
  new(attrs)
46
18
  end
47
-
48
- # @return [Integer]
49
- def _version
50
- (0xFFFFFFF0 & @version)
51
- end
52
-
53
- # @return [Boolean]
54
- def testnet?
55
- (0x0000000F & @version) == TESTNET
56
- end
57
-
58
- # @return [Boolean]
59
- def mainnet?
60
- (0x0000000F & @version) == MAINNET
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
96
19
  end
97
20
  end
@@ -9,8 +9,24 @@ class Nis::Struct
9
9
  def self.build(meta:, transaction:)
10
10
  new(
11
11
  meta: TransactionMetaData.build(meta),
12
- transaction: Transaction.build(transaction)
12
+ transaction: build_transaction_struct(transaction)
13
13
  )
14
14
  end
15
+
16
+ def self.build_transaction_struct(data)
17
+ type = data[:type]
18
+ klass = case type
19
+ when 0x0101 then Nis::Struct::TransferTransaction
20
+ when 0x0801 then Nis::Struct::ImportanceTransferTransaction
21
+ when 0x1001 then Nis::Struct::MultisigAggregationModificationTransaction
22
+ when 0x1002 then Nis::Struct::MultisigSignatureTransaction
23
+ when 0x1004 then Nis::Struct::MultisigTransaction
24
+ when 0x2001 then Nis::Struct::ProvisionNamespaceTransaction
25
+ when 0x4001 then Nis::Struct::MosaicDefinitionCreationTransaction
26
+ when 0x4002 then Nis::Struct::MosaicSupplyChangeTransaction
27
+ else raise "Not implemented entity type: #{type}"
28
+ end
29
+ klass.build(data)
30
+ end
15
31
  end
16
32
  end