nem-ruby 0.0.1

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 (136) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +119 -0
  5. data/.travis.yml +23 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/README.md +81 -0
  9. data/Rakefile +6 -0
  10. data/examples/apostille_audit.rb +22 -0
  11. data/examples/apostille_create.rb +31 -0
  12. data/examples/endpoint/account.rb +73 -0
  13. data/examples/endpoint/account_local.rb +21 -0
  14. data/examples/endpoint/block.rb +14 -0
  15. data/examples/endpoint/chain.rb +15 -0
  16. data/examples/endpoint/debug.rb +17 -0
  17. data/examples/endpoint/local.rb +17 -0
  18. data/examples/endpoint/mosaic.rb +9 -0
  19. data/examples/endpoint/namespace.rb +13 -0
  20. data/examples/endpoint/node.rb +24 -0
  21. data/examples/endpoint/timesync.rb +9 -0
  22. data/examples/endpoint/transaction.rb +42 -0
  23. data/examples/localnode.rb +0 -0
  24. data/examples/nis.rb +34 -0
  25. data/examples/node_pool.rb +23 -0
  26. data/examples/transaction/importance_transfer.rb +23 -0
  27. data/examples/transaction/mosaic_definition_creation.rb +52 -0
  28. data/examples/transaction/mosaic_supply_change.rb +25 -0
  29. data/examples/transaction/multisig_add_cosignatory.rb +31 -0
  30. data/examples/transaction/multisig_aggregate_modification.rb +29 -0
  31. data/examples/transaction/multisig_signature.rb +41 -0
  32. data/examples/transaction/multisig_transfer.rb +35 -0
  33. data/examples/transaction/provision_namespace.rb +20 -0
  34. data/examples/transaction/transfer.rb +32 -0
  35. data/examples/transaction/transfer_mosaic.rb +72 -0
  36. data/examples/transaction/transfer_with_encrypted_message.rb +40 -0
  37. data/examples/transaction/transfer_with_local.rb +34 -0
  38. data/lib/nem.rb +21 -0
  39. data/lib/nem/apostille.rb +104 -0
  40. data/lib/nem/apostille_audit.rb +47 -0
  41. data/lib/nem/client.rb +170 -0
  42. data/lib/nem/configuration.rb +29 -0
  43. data/lib/nem/endpoint.rb +4 -0
  44. data/lib/nem/endpoint/account.rb +264 -0
  45. data/lib/nem/endpoint/base.rb +45 -0
  46. data/lib/nem/endpoint/block.rb +15 -0
  47. data/lib/nem/endpoint/chain.rb +17 -0
  48. data/lib/nem/endpoint/debug.rb +54 -0
  49. data/lib/nem/endpoint/local/account.rb +54 -0
  50. data/lib/nem/endpoint/local/chain.rb +15 -0
  51. data/lib/nem/endpoint/mosaic.rb +13 -0
  52. data/lib/nem/endpoint/namespace.rb +37 -0
  53. data/lib/nem/endpoint/node.rb +83 -0
  54. data/lib/nem/endpoint/timesync.rb +9 -0
  55. data/lib/nem/endpoint/transaction.rb +34 -0
  56. data/lib/nem/error.rb +6 -0
  57. data/lib/nem/fee.rb +2 -0
  58. data/lib/nem/fee/importance_transfer.rb +24 -0
  59. data/lib/nem/fee/mosaic_definition_creation.rb +24 -0
  60. data/lib/nem/fee/mosaic_supply_change_transfer.rb +24 -0
  61. data/lib/nem/fee/multisig.rb +24 -0
  62. data/lib/nem/fee/multisig_aggregation_modification.rb +28 -0
  63. data/lib/nem/fee/provision_namespace.rb +34 -0
  64. data/lib/nem/fee/transfer.rb +66 -0
  65. data/lib/nem/keypair.rb +44 -0
  66. data/lib/nem/mixin.rb +2 -0
  67. data/lib/nem/mixin/assignable.rb +12 -0
  68. data/lib/nem/model.rb +3 -0
  69. data/lib/nem/model/account.rb +67 -0
  70. data/lib/nem/model/account_historical.rb +19 -0
  71. data/lib/nem/model/block.rb +33 -0
  72. data/lib/nem/model/chain.rb +14 -0
  73. data/lib/nem/model/connection.rb +23 -0
  74. data/lib/nem/model/experience.rb +20 -0
  75. data/lib/nem/model/explorer_block.rb +21 -0
  76. data/lib/nem/model/harvest.rb +23 -0
  77. data/lib/nem/model/heartbeat.rb +21 -0
  78. data/lib/nem/model/importance.rb +29 -0
  79. data/lib/nem/model/importance_transfer_transaction.rb +16 -0
  80. data/lib/nem/model/keypair.rb +19 -0
  81. data/lib/nem/model/message.rb +91 -0
  82. data/lib/nem/model/mosaic.rb +21 -0
  83. data/lib/nem/model/mosaic_attachment.rb +22 -0
  84. data/lib/nem/model/mosaic_definition.rb +65 -0
  85. data/lib/nem/model/mosaic_definition_creation_transaction.rb +18 -0
  86. data/lib/nem/model/mosaic_id.rb +27 -0
  87. data/lib/nem/model/mosaic_levy.rb +32 -0
  88. data/lib/nem/model/mosaic_owned.rb +22 -0
  89. data/lib/nem/model/mosaic_properties.rb +44 -0
  90. data/lib/nem/model/mosaic_supply.rb +21 -0
  91. data/lib/nem/model/mosaic_supply_change_transaction.rb +18 -0
  92. data/lib/nem/model/multisig_aggregate_modification_transaction.rb +21 -0
  93. data/lib/nem/model/multisig_info.rb +16 -0
  94. data/lib/nem/model/multisig_signature_transaction.rb +17 -0
  95. data/lib/nem/model/multisig_transaction.rb +21 -0
  96. data/lib/nem/model/namespace.rb +24 -0
  97. data/lib/nem/model/nem_announce_result.rb +19 -0
  98. data/lib/nem/model/network_time.rb +16 -0
  99. data/lib/nem/model/nis_node_info.rb +26 -0
  100. data/lib/nem/model/node.rb +41 -0
  101. data/lib/nem/model/provision_namespace_transaction.rb +18 -0
  102. data/lib/nem/model/status.rb +21 -0
  103. data/lib/nem/model/timer.rb +31 -0
  104. data/lib/nem/model/timesync.rb +19 -0
  105. data/lib/nem/model/transaction.rb +71 -0
  106. data/lib/nem/model/transfer_transaction.rb +24 -0
  107. data/lib/nem/model/unlocked_info.rb +24 -0
  108. data/lib/nem/mosaic.rb +2 -0
  109. data/lib/nem/mosaic/dim_coin.rb +21 -0
  110. data/lib/nem/mosaic/dim_token.rb +21 -0
  111. data/lib/nem/mosaic/ecobit_eco.rb +21 -0
  112. data/lib/nem/mosaic/xem.rb +21 -0
  113. data/lib/nem/node.rb +29 -0
  114. data/lib/nem/node_pool.rb +31 -0
  115. data/lib/nem/request.rb +2 -0
  116. data/lib/nem/request/announce.rb +76 -0
  117. data/lib/nem/transaction.rb +3 -0
  118. data/lib/nem/transaction/base.rb +45 -0
  119. data/lib/nem/transaction/importance_transfer.rb +42 -0
  120. data/lib/nem/transaction/mosaic_definition_creation.rb +45 -0
  121. data/lib/nem/transaction/mosaic_supply_change.rb +45 -0
  122. data/lib/nem/transaction/multisig.rb +29 -0
  123. data/lib/nem/transaction/multisig_aggregate_modification.rb +30 -0
  124. data/lib/nem/transaction/multisig_cosignatory_modification.rb +33 -0
  125. data/lib/nem/transaction/multisig_signature.rb +31 -0
  126. data/lib/nem/transaction/provision_namespace.rb +65 -0
  127. data/lib/nem/transaction/transfer.rb +63 -0
  128. data/lib/nem/util.rb +44 -0
  129. data/lib/nem/util/assignable.rb +51 -0
  130. data/lib/nem/util/convert.rb +112 -0
  131. data/lib/nem/util/deserializer.rb +158 -0
  132. data/lib/nem/util/ed25519.rb +316 -0
  133. data/lib/nem/util/serializer.rb +260 -0
  134. data/lib/nem/version.rb +3 -0
  135. data/nem-ruby.gemspec +46 -0
  136. metadata +350 -0
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ Dir.glob(File.expand_path('mixin/*.rb', __dir__)).each { |f| require f }
@@ -0,0 +1,12 @@
1
+ module Nem
2
+ module Mixin
3
+ module Assignable
4
+ def initialize(attributes = {})
5
+ attributes.each do |k, v|
6
+ instance_variable_set("@#{k.to_s}", v) if respond_to?("#{k.to_s}")
7
+ end if attributes
8
+ yield self if block_given?
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ require File.expand_path('model/transaction.rb', __dir__)
3
+ Dir.glob(File.expand_path('model/*.rb', __dir__)).each { |f| require f }
@@ -0,0 +1,67 @@
1
+ module Nem
2
+ module Model
3
+ class Account
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :address,
7
+ :balance,
8
+ :vested_balance,
9
+ :importance,
10
+ :public_key,
11
+ :label,
12
+ :harvested_blocks,
13
+ :multisig_info,
14
+ :cosignatory_of,
15
+ :cosignatories,
16
+ :status,
17
+ :remote_status
18
+
19
+ def self.new_from_account_meta_data_pair(hash)
20
+ meta = hash[:meta]
21
+ account = hash[:account]
22
+ new(
23
+ address: account[:address],
24
+ balance: account[:balance],
25
+ vested_balance: account[:vestedBalance],
26
+ importance: account[:importance],
27
+ public_key: account[:publicKey],
28
+ label: account[:label],
29
+ harvested_blocks: account[:harvestedBlocks],
30
+ cosignatory_of: meta[:cosignatoryOf],
31
+ cosignatories: meta[:cosignatories],
32
+ status: meta[:status],
33
+ remote_status: meta[:remoteStatus]
34
+ )
35
+ end
36
+
37
+ def self.new_from_account_meta_data(hash)
38
+ cosignatory_of = hash[:cosignatoryOf].map do |account|
39
+ Account.new_from_account_data(account)
40
+ end
41
+ cosignatories = hash[:cosignatories].map do |account|
42
+ Account.new_from_account_data(account)
43
+ end
44
+ new(
45
+ cosignatory_of: cosignatory_of,
46
+ cosignatories: cosignatories,
47
+ status: hash[:status],
48
+ remote_status: hash[:remoteStatus]
49
+ )
50
+ end
51
+
52
+ def self.new_from_account_data(hash)
53
+ multisig_info = MultisigInfo.new_from_multisig_info(hash[:multisigInfo])
54
+ new(
55
+ address: hash[:address],
56
+ balance: hash[:balance],
57
+ vested_balance: hash[:vestedBalance],
58
+ importance: hash[:importance],
59
+ public_key: hash[:publicKey],
60
+ label: hash[:label],
61
+ harvested_blocks: hash[:harvestedBlocks],
62
+ multisig_info: multisig_info
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,19 @@
1
+ module Nem
2
+ module Model
3
+ class AccountHistorical
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :page_rank,
7
+ :address,
8
+ :balance,
9
+ :importance,
10
+ :vested_balance,
11
+ :unvested_balance,
12
+ :height
13
+
14
+ def self.new_from_account_historical(hash)
15
+ new(hash)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ module Nem
2
+ module Model
3
+ class Block
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :timestamp,
7
+ :signature,
8
+ :prev_block_hash,
9
+ :type,
10
+ :transactions,
11
+ :version,
12
+ :signer,
13
+ :height
14
+
15
+ def self.new_from_block(hash)
16
+ txes = hash[:transactions].map do |tx|
17
+ Transaction.new_from_account_transaction(tx)
18
+ end
19
+
20
+ new(
21
+ timestamp: hash[:timeStamp],
22
+ signature: hash[:signature],
23
+ prev_block_hash: hash[:prevBlockHash][:data],
24
+ type: hash[:type],
25
+ transactions: txes,
26
+ version: hash[:version],
27
+ signer: hash[:signer],
28
+ height: hash[:height]
29
+ )
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,14 @@
1
+ module Nem
2
+ module Model
3
+ class Chain
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :timestamp,
7
+
8
+ def self.new_from_hash(hash)
9
+ new(
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ module Nem
2
+ module Model
3
+ class Connection
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader:path,
7
+ :start_time,
8
+ :host,
9
+ :elapsed_time,
10
+ :id
11
+
12
+ def self.new_from_connection(hash)
13
+ new(
14
+ path: hash[:path],
15
+ start_time: hash[:"start-time"],
16
+ host: hash[:host],
17
+ elapsed_time: hash[:"elapsed-time"],
18
+ id: hash[:id]
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module Nem
2
+ module Model
3
+ class Experience
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :node, :syncs, :experience
7
+
8
+ ExperienceData = Struct.new(:s, :f)
9
+
10
+ def self.new_from_experience(hash)
11
+ exp = hash[:experience]
12
+ new(
13
+ node: Nem::Model::Node.new_from_node(hash[:node]),
14
+ syncs: hash[:syncs],
15
+ experience: ExperienceData.new(exp[:s], exp[:f])
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Model
3
+ class ExpelorerBlock
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :difficulty, :txes, :block, :hash
7
+
8
+ def self.new_from_explorer_block(hash)
9
+ txes = hash[:txes].map do |tx|
10
+ Transaction.new_from_transaction(tx)
11
+ end
12
+ new(
13
+ difficulty: hash[:difficulty],
14
+ txes: txes,
15
+ block: Block.new_from_block(hash[:block]),
16
+ hash: hash[:hash]
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module Nem
2
+ module Model
3
+ class Harvest
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :timestamp,
7
+ :difficulty,
8
+ :total_fee,
9
+ :id,
10
+ :height
11
+
12
+ def self.new_from_harvest(hash)
13
+ new(
14
+ timestamp: hash[:timeStamp],
15
+ difficulty: hash[:difficulty],
16
+ total_fee: hash[:totalFee],
17
+ id: hash[:id],
18
+ height: hash[:height]
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Model
3
+ class Heartbeat
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :code,
7
+ :type,
8
+ :message
9
+
10
+ # 0: Unknown status.
11
+ # 1: NIS is stopped.
12
+ # 2: NIS is starting.
13
+ # 3: NIS is running.
14
+ # 4: NIS is booting the local node (implies NIS is running).
15
+ # 5: The local node is booted (implies NIS is running).
16
+ # 6: The local node is synchronized (implies NIS is running and the local node is booted).
17
+ # 7: NIS local node does not see any remote NIS node (implies running and booted).
18
+ # 8: NIS is currently loading the block chain from the database. In this state NIS cannot serve any requests.
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Nem
2
+ module Model
3
+ class Importance
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_accessor :address,
7
+ :score,
8
+ :ev,
9
+ :is_set,
10
+ :height
11
+
12
+ def self.new_from_account_importance_view_model(hash)
13
+ importance = hash[:importance]
14
+ new(
15
+ address: hash[:address],
16
+ score: importance[:score],
17
+ ev: importance[:ev],
18
+ is_set: importance[:isSet],
19
+ height: importance[:height]
20
+ )
21
+ end
22
+
23
+ # @return [Boolean]
24
+ def set?
25
+ @is_set == 1
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ module Nem
2
+ module Model
3
+ class ImportanceTransferTransaction < Transaction
4
+ attr_reader :mode, :remote_account
5
+
6
+ def self.new_from_transaction_meta_data_pair(hash)
7
+ transaction = hash[:transaction]
8
+ attrs = common_part_meta_data_pair(hash).merge(
9
+ mode: transaction[:mode],
10
+ remote_account: transaction[:remoteAccount]
11
+ )
12
+ new(attrs)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Nem
2
+ module Model
3
+ class Keypair
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :private_key,
7
+ :public_key,
8
+ :address
9
+
10
+ def self.new_from_key_pair_view_model(hash)
11
+ new(
12
+ private_key: hash[:privateKey],
13
+ public_key: hash[:publicKey],
14
+ address: hash[:address]
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,91 @@
1
+ module Nem
2
+ module Model
3
+ # @attr [String] value
4
+ # @attr [Integer] type
5
+ # @attr [String] payload
6
+ # @attr [String] public_key
7
+ # @attr [String] private_key
8
+ class Message
9
+ TYPE_PLAIN = 1
10
+ TYPE_ENCRYPTED = 2
11
+
12
+ include Nem::Mixin::Assignable
13
+
14
+ attr_reader :value, :type
15
+ attr_accessor :public_key, :private_key
16
+
17
+ def self.new_from_message(hash)
18
+ new(
19
+ hash[:payload],
20
+ type: (hash[:type] == TYPE_ENCRYPTED) ? :encrypted : :plain
21
+ )
22
+ end
23
+
24
+ def initialize(value = '', type: :plain, private_key: nil, public_key: nil)
25
+ @value = value
26
+ @type = (type == :encrypted) ? TYPE_ENCRYPTED : TYPE_PLAIN
27
+ @private_key = private_key
28
+ @public_key = public_key
29
+ end
30
+
31
+ def encrypt!
32
+ bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
33
+ bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
34
+ @value = Nem::Util::Ed25519.encrypt(bin_sk, bin_pk, value)
35
+ @type = TYPE_ENCRYPTED
36
+ end
37
+
38
+ def decrypt!
39
+ bin_sk = fix_private_key(@private_key).scan(/../).map(&:hex).reverse.pack('C*')
40
+ bin_pk = (public_key || @public_key).scan(/../).map(&:hex).pack('C*')
41
+ @value = Nem::Util::Ed25519.decrypt(bin_sk, bin_pk, payload)
42
+ @type = TYPE_PLAIN
43
+ end
44
+
45
+ # @return [Boolean]
46
+ def encrypted?
47
+ @type == TYPE_ENCRYPTED
48
+ end
49
+
50
+ # @return [Boolean]
51
+ def plain?
52
+ @type == TYPE_PLAIN
53
+ end
54
+
55
+ # @return [Integer]
56
+ def bytesize
57
+ payload.bytesize
58
+ end
59
+
60
+ # @return [Boolean]
61
+ def valid?
62
+ bytesize <= 512
63
+ end
64
+
65
+ # @return [Hash]
66
+ def to_hash
67
+ { payload: payload, type: @type }
68
+ end
69
+
70
+ # @return [String]
71
+ def to_s
72
+ @value.to_s
73
+ end
74
+
75
+ # @return [Boolean]
76
+ def ==(other)
77
+ @value == other.value
78
+ end
79
+
80
+ def payload
81
+ (value =~ /\Afe/ || encrypted?) ? value : value.unpack('H*').first
82
+ end
83
+
84
+ private
85
+
86
+ def fix_private_key(key)
87
+ "#{'0' * 64}#{key.sub(/^00/i, '')}"[-64, 64]
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Model
3
+ class Mosaic
4
+ include Nem::Mixin::Assignable
5
+
6
+ extend Forwardable
7
+
8
+ def_delegators :@mosaic_id, :fqn
9
+
10
+ attr_reader :mosaic_id, :quantity
11
+
12
+ def self.new_from_mosaic(hash)
13
+ mosaic_id = Nem::Model::MosaicId.new_from_mosaic_id(hash[:mosaicId])
14
+ new(
15
+ quantity: hash[:quantity],
16
+ mosaic_id: mosaic_id
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end