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,41 @@
1
+ module Nem
2
+ module Model
3
+ class NodeMetadata
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :features,
7
+ :network,
8
+ :application,
9
+ :version,
10
+ :platform
11
+ end
12
+
13
+ class NodeEndpoint
14
+ include Nem::Mixin::Assignable
15
+
16
+ attr_reader :protocol, :port, :host
17
+ end
18
+
19
+ class NodeIdentity
20
+ include Nem::Mixin::Assignable
21
+
22
+ attr_reader :name, :public_key
23
+ end
24
+
25
+ class Node
26
+ include Nem::Mixin::Assignable
27
+
28
+ attr_reader :metadata,
29
+ :endpoint,
30
+ :identity
31
+
32
+ def self.new_from_node(hash)
33
+ new(
34
+ metadata: NodeMetadata.new(hash[:metaData]),
35
+ endpoint: NodeEndpoint.new(hash[:endpoint]),
36
+ identity: NodeIdentity.new(hash[:identity])
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ module Nem
2
+ module Model
3
+ class ProvisionNamespaceTransaction < Transaction
4
+ attr_reader :rental_fee, :rental_fee_sink, :parent, :new_part
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
+ rental_fee: transaction[:rentalFee],
10
+ rental_fee_sink: transaction[:rentalFeeSink],
11
+ parent: transaction[:parent],
12
+ new_part: transaction[:newPart]
13
+ )
14
+ new(attrs)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Model
3
+ class Status
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,31 @@
1
+ module Nem
2
+ module Model
3
+ class Timer
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :last_delay_time,
7
+ :executions,
8
+ :failures,
9
+ :successes,
10
+ :last_operation_start_time,
11
+ :is_executing,
12
+ :name,
13
+ :average_operation_time,
14
+ :operation_time
15
+
16
+ def self.new_from_timer(hash)
17
+ new(
18
+ last_delay_time: hash[:'last-delay-time'],
19
+ executions: hash[:executions],
20
+ failures: hash[:failures],
21
+ successes: hash[:successes],
22
+ last_operation_start_time: hash[:'last-operation-start-time'],
23
+ is_executing: hash[:'is-executing'],
24
+ name: hash[:name],
25
+ average_operation_time: hash[:'average-operation-time'],
26
+ operation_time: hash[:'operation-time']
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ module Nem
2
+ module Model
3
+ class Timesync
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :datetime,
7
+ :current_time_offset,
8
+ :change
9
+
10
+ def self.new_from_timer(hash)
11
+ new(
12
+ datetime: hash[:datetime],
13
+ current_time_offset: hash[:current_time_offset],
14
+ change: hash[:change]
15
+ )
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,71 @@
1
+ module Nem
2
+ module Model
3
+ class Transaction
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :timestamp,
7
+ :version,
8
+ :type,
9
+ :signer,
10
+ :fee,
11
+ :deadline,
12
+ :id,
13
+ :hash,
14
+ :height
15
+
16
+ def version?(num)
17
+ num == 1 ? true : false # TODO: version
18
+ end
19
+
20
+ def self.new_from_account_transaction(hash)
21
+ new_from_account_transaction_meta_data_pair(
22
+ meta: { data: nil },
23
+ transaction: hash
24
+ )
25
+ end
26
+
27
+ def self.new_from_account_transaction_meta_data_pair(hash)
28
+ type = hash[:transaction][:type]
29
+ klass = case type
30
+ when 0x0101 then TransferTransaction
31
+ when 0x0801 then ImportanceTransferTransaction
32
+ when 0x1001 then MultisigAggregateModificationTransaction
33
+ when 0x1002 then MultisigSignatureTransaction
34
+ when 0x1004 then MultisigTransaction
35
+ when 0x2001 then ProvisionNamespaceTransaction
36
+ when 0x4001 then MosaicDefinitionCreationTransaction
37
+ when 0x4002 then MosaicSupplyChangeTransaction
38
+ else raise "Undefined transaction type: #{type}"
39
+ end
40
+ klass.new_from_transaction_meta_data_pair(hash)
41
+ end
42
+
43
+ def self.common_part(hash)
44
+ {
45
+ timestamp: hash[:timeStamp],
46
+ version: hash[:version],
47
+ type: hash[:type],
48
+ signer: hash[:signer],
49
+ fee: hash[:fee],
50
+ deadline: hash[:deadline]
51
+ }
52
+ end
53
+
54
+ def self.common_part_meta_data_pair(hash)
55
+ meta = hash[:meta]
56
+ transaction = hash[:transaction]
57
+ {
58
+ id: meta[:id],
59
+ hash: meta[:hash] ? meta[:hash][:data] : meta[:data],
60
+ height: meta[:height],
61
+ timestamp: transaction[:timeStamp],
62
+ version: transaction[:version],
63
+ type: transaction[:type],
64
+ signer: transaction[:signer],
65
+ fee: transaction[:fee],
66
+ deadline: transaction[:deadline]
67
+ }
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,24 @@
1
+ module Nem
2
+ module Model
3
+ class TransferTransaction < Transaction
4
+ attr_reader :recipient, :amount, :message, :mosaics
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
+ recipient: transaction[:recipient],
10
+ amount: transaction[:amount],
11
+ )
12
+ unless transaction[:message].empty?
13
+ attrs[:message] = Nem::Model::Message.new_from_message(transaction[:message])
14
+ end
15
+ if transaction[:mosaics]
16
+ attrs[:mosaics] = transaction[:mosaics].map do |mo|
17
+ Mosaic.new(mo)
18
+ end
19
+ end
20
+ new(attrs)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ module Nem
2
+ module Model
3
+ class UnlockedInfo
4
+ include Nem::Mixin::Assignable
5
+
6
+ attr_reader :max_unlocked, :num_unlocked
7
+
8
+ def self.new_from_unlocked_info(hash)
9
+ new(
10
+ max_unlocked: hash[:'max-unlocked'],
11
+ num_unlocked: hash[:'num-unlocked']
12
+ )
13
+ end
14
+
15
+ def has_freeslot?
16
+ ! @max_unlocked == @num_unlocked
17
+ end
18
+
19
+ def freeslot
20
+ @max_unlocked - @num_unlocked
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ Dir.glob(File.expand_path('mosaic/*.rb', __dir__)).each { |f| require f }
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Mosaic
3
+ class DimCoin < Nem::Model::MosaicAttachment
4
+ def initialize(quantity)
5
+ mosaic_id = Nem::Model::MosaicId.new(
6
+ namespace_id: 'dim',
7
+ name: 'coin'
8
+ )
9
+ properties = Nem::Model::MosaicProperties.new(
10
+ divisibility: 6,
11
+ initial_supply: 900_000_000
12
+ )
13
+ super(
14
+ mosaic_id: mosaic_id,
15
+ properties: properties,
16
+ quantity: quantity
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Mosaic
3
+ class DimToken < Nem::Model::MosaicAttachment
4
+ def initialize(quantity)
5
+ mosaic_id = Nem::Model::MosaicId.new(
6
+ namespace_id: 'dim',
7
+ name: 'token'
8
+ )
9
+ properties = Nem::Model::MosaicProperties.new(
10
+ divisibility: 6,
11
+ initial_supply: 1_000_000
12
+ )
13
+ super(
14
+ mosaic_id: mosaic_id,
15
+ properties: properties,
16
+ quantity: quantity
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Mosaic
3
+ class EcobitEco < Nem::Model::MosaicAttachment
4
+ def initialize(quantity)
5
+ mosaic_id = Nem::Model::MosaicId.new(
6
+ namespace_id: 'ecobit',
7
+ name: 'eco'
8
+ )
9
+ properties = Nem::Model::MosaicProperties.new(
10
+ divisibility: 0,
11
+ initial_supply: 888_888_888
12
+ )
13
+ super(
14
+ mosaic_id: mosaic_id,
15
+ properties: properties,
16
+ quantity: quantity
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Nem
2
+ module Mosaic
3
+ class Xem < Nem::Model::MosaicAttachment
4
+ def initialize(quantity)
5
+ mosaic_id = Nem::Model::MosaicId.new(
6
+ namespace_id: 'nem',
7
+ name: 'xem'
8
+ )
9
+ properties = Nem::Model::MosaicProperties.new(
10
+ divisibility: 6,
11
+ initial_supply: 8_999_999_999
12
+ )
13
+ super(
14
+ mosaic_id: mosaic_id,
15
+ properties: properties,
16
+ quantity: quantity
17
+ )
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ module Nem
2
+ class Node
3
+ extend Forwardable
4
+
5
+ def_delegators :@client, :request, :request!
6
+
7
+ # @param [hash] options HTTP Client connection information
8
+ # @option options [String] :url URL
9
+ # @option options [String] :scheme default http
10
+ # @option options [Symbol] :host default 127.0.0.1
11
+ # @option options [Integer] :port default 7890
12
+ # @option options [Integer] :timeout default 5
13
+ def initialize(options = {})
14
+ @client = Nem::Client.new(options)
15
+ end
16
+
17
+ def heartbeat
18
+ Nem::Model::Heartbeat.new request!(:get, '/heartbeat')
19
+ end
20
+
21
+ def status
22
+ Nem::Model::Status.new request!(:get, '/status')
23
+ end
24
+
25
+ def shutdown
26
+ request!(:get, '/shutdown')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module Nem
2
+ class NodePool
3
+ extend Forwardable
4
+
5
+ def_delegators :@nodes, :first, :last, :length
6
+
7
+ attr_reader :nodes, :failed
8
+
9
+ def self.from_hash(definitions)
10
+ new(definitions.map { |d| Nem::Node.new(d) })
11
+ end
12
+
13
+ def initialize(nodes)
14
+ @nodes = nodes
15
+ @failed = []
16
+ end
17
+
18
+ def current
19
+ @nodes.first
20
+ end
21
+
22
+ def next!
23
+ @failed << @nodes.shift
24
+ raise 'Exhausted node pool' if length.zero?
25
+ end
26
+
27
+ def <<(definitioin)
28
+ @nodes << Nem::Node.new(definitioin)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ # frozen_string_literal: true
2
+ Dir.glob(File.expand_path('request/*.rb', __dir__)).each { |f| require f }
@@ -0,0 +1,76 @@
1
+ module Nem
2
+ module Request
3
+ # @attr [Nem::Struct::Transaction] transaction
4
+ # @attr [Nem::Keypair] keypair
5
+ # @attr [String] data
6
+ # @attr [String] signature
7
+ # @see https://nemproject.github.io/#requestAnnounce
8
+ # @see https://nemproject.github.io/#requestPrepareAnnounce
9
+ class Announce
10
+ attr_reader :transaction, :keypair, :data, :signature
11
+
12
+ def initialize(transaction, keypair)
13
+ @transaction = transaction
14
+ @keypair = keypair
15
+ end
16
+
17
+ # @return [Hash] Attribute and value pairs
18
+ def to_entity(state = nil)
19
+ entity = prepare_entity
20
+ if state == :prepare
21
+ { transaction: entity,
22
+ privateKey: keypair.private }
23
+ else
24
+ serialized = serialize(entity)
25
+ hex_serialized = Nem::Util::Convert.ua2hex(serialized)
26
+ { data: Nem::Util::Convert.ua2hex(serialized),
27
+ signature: signature(hex_serialized) }
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def prepare_entity
34
+ specify = if transaction.multisig?
35
+ prepare_multisig(transaction)
36
+ else
37
+ transaction.to_hash
38
+ end
39
+ set_common(specify)
40
+ end
41
+
42
+ def prepare_multisig(tx)
43
+ other_trans = tx.other_trans.to_hash.merge(
44
+ type: tx.other_trans.type,
45
+ fee: tx.other_trans.fee.to_i,
46
+ timeStamp: Nem::Util.timestamp,
47
+ deadline: Nem::Util.deadline(Nem.deadline),
48
+ signer: tx.signer,
49
+ version: tx.other_trans.version
50
+ )
51
+ tx.to_hash.merge(
52
+ otherTrans: other_trans
53
+ )
54
+ end
55
+
56
+ def set_common(hash)
57
+ hash.merge(
58
+ type: transaction.type,
59
+ fee: transaction.fee.to_i,
60
+ timeStamp: Nem::Util.timestamp,
61
+ deadline: Nem::Util.deadline(Nem.deadline),
62
+ signer: keypair.public,
63
+ version: transaction.version
64
+ )
65
+ end
66
+
67
+ def serialize(hash)
68
+ Nem::Util::Serializer.serialize_transaction(hash)
69
+ end
70
+
71
+ def signature(serialized)
72
+ keypair.sign(serialized)
73
+ end
74
+ end
75
+ end
76
+ end