nem-ruby 0.0.5 → 0.0.6

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.
@@ -0,0 +1,39 @@
1
+ ## Offline transaction
2
+
3
+ ### Create signed transaction
4
+
5
+ ```ruby
6
+ require 'pp'
7
+ require 'nem'
8
+
9
+ # sender
10
+ A_PRIVATE_KEY = '4ce5c8f9fce571db0d9ac1adf00b8d3ba0f078ed40835fd3d730a2f24b834214'
11
+
12
+ # recipient
13
+ B_ADDRESS = 'TA4TX6U5HG2MROAESH2JE5524T4ZOY2EQKQ6ELHF'
14
+
15
+ kp = Nem::Keypair.new(A_PRIVATE_KEY)
16
+ tx = Nem::Transaction::Transfer.new(B_ADDRESS, 1, 'Good luck! Offline transfer.')
17
+ req = Nem::Request::Announce.new(tx, kp)
18
+
19
+ # signed announce
20
+ pp data = req.to_entity
21
+ pp 'save as tx.json'
22
+ File.write 'tx.json', data.to_json
23
+ ```
24
+
25
+ ### Announce signed transaction
26
+
27
+ ```ruby
28
+ require 'pp'
29
+ require 'nem'
30
+
31
+ node = Nem::Node.new(host: 'bigalice2.nem.ninja')
32
+ tx_endpoint = Nem::Endpoint::Transaction.new(node)
33
+
34
+ req = File.read('tx.json')
35
+ res = tx_endpoint.announce(JSON.parse(req))
36
+
37
+ pp "Message: #{res.message}"
38
+ pp "TransactionHash: #{res.transaction_hash}"
39
+ ```
@@ -0,0 +1,12 @@
1
+ ## Endpoint
2
+
3
+ ```ruby
4
+ node = Nem::Node.new(host: 'bigalice2.nem.ninja')
5
+ endpoint = Nem::Endpoint::Timesync.new(node)
6
+ ```
7
+
8
+ ## Network time
9
+
10
+ ```ruby
11
+ pp endpoint.network_time
12
+ ```
@@ -0,0 +1,218 @@
1
+ ## Endpoint
2
+
3
+ ```ruby
4
+ node = Nem::Node.new(host: 'bigalice2.nem.ninja')
5
+ endpoint = Nem::Endpoint::Transaction.new(node)
6
+ kp = Nem::Keypair.new(A_PRIVATE_KEY)
7
+ ```
8
+
9
+ ## Transfer
10
+
11
+ ### Version1(XEM transfer)
12
+
13
+ ```ruby
14
+ tx = Nem::Transaction::Transfer.new(B_ADDRESS, 1, 'Good luck!')
15
+ pp "Fee: #{tx.fee.to_i}"
16
+
17
+ req = Nem::Request::Announce.new(tx, kp)
18
+ res = endpoint.announce(req)
19
+
20
+ pp "Message: #{res.message}"
21
+ pp "TransactionHash: #{res.transaction_hash}"
22
+ ```
23
+
24
+ ### Version2(mosaic transfer)
25
+
26
+ ```ruby
27
+ tx = Nem::Transaction::Transfer.new(B_ADDRESS, 1, 'Good luck!')
28
+
29
+ # define custom mosaic attachment if you already know mosaic definition
30
+ class KonHeart < Nem::Mosaic::Base
31
+ namespace_id 'kon'
32
+ name 'heart'
33
+ divisibility 3
34
+ initial_supply 100_000_000
35
+ end
36
+ tx.mosaics << KonHeart.new(1)
37
+
38
+ # fetch mosaic definition
39
+ ns_endpoint = Nem::Endpoint::Namespace.new(node)
40
+ mo_def = ns_endpoint.mosaic_definition('kon').first
41
+ moa = Nem::Model::MosaicAttachment.new(
42
+ mosaic_id: mo_def.id,
43
+ properties: mo_def.properties,
44
+ quantity: 1
45
+ )
46
+ tx.mosaics << moa
47
+
48
+ # send xem as mosaic
49
+ tx.mosaics << Nem::Mosaic::Xem.new(1)
50
+
51
+ pp "Fee: #{tx.fee.to_i}"
52
+
53
+ req = Nem::Request::Announce.new(tx, kp)
54
+ res = endpoint.announce(req)
55
+
56
+ pp "Message: #{res.message}"
57
+ pp "TransactionHash: #{res.transaction_hash}"
58
+ ```
59
+
60
+ ## Importance Transfer
61
+
62
+ ```ruby
63
+ tx = Nem::Transaction::ImportanceTransfer.new(B_PUBLIC_KEY, :activate)
64
+ pp "Fee: #{tx.fee.to_i}"
65
+
66
+ req = Nem::Request::Announce.new(tx, kp)
67
+ res = endpoint.announce(req)
68
+
69
+ pp "Message: #{res.message}"
70
+ pp "TransactionHash: #{res.transaction_hash}"
71
+ ```
72
+
73
+ ## Multisig Signature
74
+
75
+ ```ruby
76
+ account_endpoint = Nem::Endpoint::Account.new(node)
77
+ txes = account_endpoint.transfers_unconfirmed(B_ADDRESS)
78
+
79
+ unless txes.size > 0
80
+ puts 'There are no transactions to sign.'
81
+ exit
82
+ end
83
+
84
+ need_sig_tx = txes.first
85
+ pp "Unconfirmed Transaction Hash: #{need_sig_tx.hash}"
86
+
87
+ tx = Nem::Transaction::MultisigSignature.new(need_sig_tx.hash, M_ADDRESS, B_PUBLIC_KEY)
88
+
89
+ req = Nem::Request::Announce.new(tx, kp)
90
+ res = endpoint.announce(req)
91
+
92
+ pp "Message: #{res.message}"
93
+ pp "TransactionHash: #{res.transaction_hash}"
94
+ ```
95
+
96
+ ## Provision Namespace
97
+
98
+ ```ruby
99
+ tx = Nem::Transaction::ProvisionNamespace.new('sushi')
100
+ pp "Fee: #{tx.fee.to_i}"
101
+
102
+ req = Nem::Request::Announce.new(tx, kp)
103
+ res = endpoint.announce(req)
104
+
105
+ pp "Message: #{res.message}"
106
+ pp "TransactionHash: #{res.transaction_hash}"
107
+ ```
108
+
109
+ ## Mosaic
110
+
111
+ ### Creation
112
+
113
+ ```ruby
114
+ mosaic_id = Nem::Model::MosaicId.new(
115
+ namespace_id: 'sushi',
116
+ name: 'maguro'
117
+ )
118
+
119
+ properties = Nem::Model::MosaicProperties.new(
120
+ divisibility: 0,
121
+ initial_supply: 10_000,
122
+ supply_mutable: true,
123
+ transferable: true
124
+ )
125
+
126
+ levy = Nem::Model::MosaicLevy.new(
127
+ type: 1,
128
+ recipient: A_ADDRESS,
129
+ mosaic_id: Nem::Model::MosaicId.new(
130
+ namespace_id: 'nem',
131
+ name: 'xem'
132
+ ),
133
+ fee: 1_000
134
+ )
135
+
136
+ definition = Nem::Model::MosaicDefinition.new(
137
+ creator: A_PUBLIC_KEY,
138
+ id: mosaic_id,
139
+ description: 'Japanese Soul food SHUSHI.',
140
+ properties: properties,
141
+ levy: levy
142
+ )
143
+
144
+ tx = Nem::Transaction::MosaicDefinitionCreation.new(definition)
145
+ pp "Fee: #{tx.fee.to_i}"
146
+
147
+ req = Nem::Request::Announce.new(tx, kp)
148
+ res = endpoint.announce(req)
149
+
150
+ pp "Message: #{res.message}"
151
+ pp "TransactionHash: #{res.transaction_hash}"
152
+ ```
153
+
154
+ ### Supply Change
155
+
156
+ ```ruby
157
+ mosaic_id = Nem::Model::MosaicId.new(
158
+ namespace_id: 'sushi',
159
+ name: 'maguro'
160
+ )
161
+
162
+ tx = Nem::Transaction::MosaicSupplyChange.new(mosaic_id, :increase, 1_000)
163
+ pp "Fee: #{tx.fee.to_i}"
164
+
165
+ req = Nem::Request::Announce.new(tx, kp)
166
+ res = tx_endpoint.announce(req)
167
+
168
+ pp "Message: #{res.message}"
169
+ pp "TransactionHash: #{res.transaction_hash}"
170
+ ```
171
+
172
+ ## Multisig Aggregate Modification
173
+
174
+ ```ruby
175
+ msig_cosignatories = [
176
+ Nem::Transaction::MultisigCosignatoryModification.new(:add, A_PUBLIC_KEY)
177
+ ]
178
+ relative_change = 1
179
+ tx = Nem::Transaction::MultisigAggregateModification.new(msig_cosignatories, relative_change)
180
+ pp "Fee: #{tx.fee.to_i}"
181
+
182
+ req = Nem::Request::Announce.new(tx, kp)
183
+ res = endpoint.prepare_announce(req)
184
+
185
+ pp "Message: #{res.message}"
186
+ pp "TransactionHash: #{res.transaction_hash}"
187
+ ```
188
+
189
+ ### Add cosignatory
190
+
191
+ ```ruby
192
+ msig_cosigratories = [
193
+ Nem::Transaction::MultisigCosignatoryModification.new(:add, B_PUBLIC_KEY)
194
+ ]
195
+ relative_change = 1
196
+ mtx = Nem::Transaction::MultisigAggregateModification.new(msig_cosigratories, relative_change)
197
+ tx = Nem::Transaction::Multisig.new(mtx, M_PUBLIC_KEY)
198
+
199
+ req = Nem::Request::Announce.new(tx, kp)
200
+ res = endpoint.announce(req)
201
+
202
+ pp "Message: #{res.message}"
203
+ pp "TransactionHash: #{res.transaction_hash}"
204
+ ```
205
+
206
+ ### Local node
207
+
208
+ ```ruby
209
+ tx = Nem::Transaction::Transfer.new(B_ADDRESS, 1, 'Good luck!')
210
+ pp "Fee: #{tx.fee.to_i}"
211
+
212
+ req = Nem::Request::Announce.new(tx, kp)
213
+ # use #prepare_announce method for local node without signature
214
+ res = endpoint.prepare_announce(req)
215
+
216
+ pp "Message: #{res.message}"
217
+ pp "TransactionHash: #{res.transaction_hash}"
218
+ ```
@@ -0,0 +1,24 @@
1
+ ## Deserialize transaction
2
+
3
+ ```ruby
4
+ require 'pp'
5
+ require 'nem'
6
+
7
+ # sender
8
+ A_PRIVATE_KEY = '260206d683962350532408e8774fd14870a173b7fba17f6b504da3dbc5f1cc9f'
9
+
10
+ # receiver
11
+ B_ADDRESS = 'TAWKJTUP4DWKLDKKS534TYP6G324CBNMXKBA4X7B'
12
+
13
+ kp = Nem::Keypair.new(A_PRIVATE_KEY)
14
+ tx = Nem::Transaction::Transfer.new(B_ADDRESS, 1, 'Good luck!')
15
+ req = Nem::Request::Announce.new(tx, kp)
16
+
17
+ entity = req.to_entity
18
+
19
+ # You can also announce to nem Network later.
20
+ pp entity.to_json
21
+
22
+ # deserialize data into hash
23
+ pp Nem::Util::Deserializer.deserialize_transaction(req.to_entity[:data])
24
+ ```
data/docs/index.md ADDED
@@ -0,0 +1,153 @@
1
+ ## Introduction
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nem-ruby.svg)](https://badge.fury.io/rb/nem-ruby)
4
+ [![Build Status](https://travis-ci.org/44uk/nem-ruby.svg?branch=master)](https://travis-ci.org/44uk/nem-ruby)
5
+ [![Code Climate](https://codeclimate.com/github/44uk/nem-ruby/badges/gpa.svg)](https://codeclimate.com/github/44uk/nem-ruby)
6
+ [![Join the chat at https://gitter.im/44uk/nem-ruby](https://badges.gitter.im/44uk/nem-ruby.svg)](https://gitter.im/44uk/nem-ruby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
7
+
8
+ <img src="https://cloud.githubusercontent.com/assets/370508/24320282/a332d238-1175-11e7-96dc-75bc30e562d2.png" width="280" height="280" alt="nem" align="right" />
9
+
10
+ Ruby gem for communicating with the nem.
11
+
12
+ For further development of nem with ruby, [feel free to send me your feedback!](/about#feedback-and-contact)
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ $ gem install nem-ruby
18
+ ```
19
+
20
+ or add this line to your application's Gemfile.
21
+
22
+ ```ruby
23
+ gem 'nem-ruby'
24
+ ```
25
+
26
+ ## Connection
27
+
28
+ ```ruby
29
+ require 'pp'
30
+ require 'nem'
31
+
32
+ # connect to remote node
33
+ node = Nem::Node.new(host: 'bigalice2.nem.ninja')
34
+
35
+ # ENV['NEM_URL'] can be used if it set.
36
+ # export NEM_URL=http://bigalice2.nem.ninja:7890
37
+ node = Nem::Node.new
38
+
39
+ # connect to local node if no params
40
+ node = Nem::Node.new
41
+
42
+ # passing options
43
+ node = Nem::Node.new(host: '127.0.0.1', port: 7890)
44
+
45
+ # passing url
46
+ node = Nem::Node.new(url: 'http://127.0.0.1:7890')
47
+ ```
48
+
49
+ You can pick node from [NEM Node Rewards](https://supernodes.nem.io/).
50
+
51
+ ### Node Pool
52
+
53
+ ```ruby
54
+ # create node pool
55
+ node = Nem::NodePool.new([
56
+ Nem::Node.new(host: 'dummy1.example.com'),
57
+ Nem::Node.new(host: 'dummy2.example.com'),
58
+ Nem::Node.new(host: 'bigalice2.nem.ninja')
59
+ ])
60
+ ```
61
+
62
+ ## Endpoint
63
+
64
+ ```ruby
65
+ endpoint = Nem::Endpoint::Account.new(node)
66
+ pp endpoint.find('TBULEAUG2CZQISUR442HWA6UAKGWIXHDABJVIPS4')
67
+ # <Nem::Model::Account:0x007fca1b101070
68
+ # @address="TBULEAUG2CZQISUR442HWA6UAKGWIXHDABJVIPS4",
69
+ # @balance=68000000,
70
+ # @cosignatories=[],
71
+ # @cosignatory_of=[],
72
+ # @harvested_blocks=1,
73
+ # @importance=0.0,
74
+ # @label=nil,
75
+ # @public_key="e59ef184a612d4c3c4d89b5950eb57262c69862b2f96e59c5043bf41765c482f",
76
+ # @remote_status="INACTIVE",
77
+ # @status="LOCKED",
78
+ # @vested_balance=62145647
79
+ # >
80
+ ```
81
+
82
+ ## Node
83
+
84
+ ```ruby
85
+ node.heartbeat
86
+ # <Nem::Model::Heartbeat:0x007f83d3b6eb68 @code=1, @message="ok", @type=2>
87
+ # See https://nemproject.github.io/#heart-beat-request
88
+
89
+ node.status
90
+ # <Nem::Model::Status:0x007f83d3b54920 @code=6, @message="status", @type=4>
91
+ # See https://nemproject.github.io/#status-request
92
+ ```
93
+
94
+ ### calling API Path
95
+
96
+ Also call Nem::Node#request method. The method receive *HTTP Method*, *API Path*, *Parameters*
97
+ It returns hash which converted API JSON response. by API path /heartbeat, /status
98
+
99
+ See [NEM NIS API Documentation](https://nemproject.github.io/).
100
+
101
+ ```ruby
102
+ pp node.request :get, 'heartbeat'
103
+
104
+ pp node.request :get, 'status'
105
+
106
+ pp node.request :get, 'account/get', address: 'TBULEAUG2CZQISUR442HWA6UAKGWIXHDABJVIPS4'
107
+
108
+ pp node.request :post, 'account/unlock', privateKey: '00983bb01d05edecfaef55df9486c111abb6299c754a002069b1d0ef4537441bda'
109
+ ```
110
+
111
+ ## Logging
112
+
113
+ ```ruby
114
+ # custom loggin output (default is STDOUT)
115
+ Nem.logger = Logger.new('/path/to/nem-ruby.log')
116
+ # turn on output request information.
117
+ Nem.logger.level = Logger::DEBUG
118
+
119
+ # also set condig this way.
120
+ Nem.configure do |conf|
121
+ conf.logger = Logger.new('./nem-ruby.log')
122
+ conf.logger.level = Logger::DEBUG
123
+
124
+ # you can set deadline(sec)
125
+ conf.default_deadline = 7200
126
+
127
+ # set :mainnet if you'd like to use on mainnet(default :testnet)
128
+ conf.default_network = :mainnet
129
+ end
130
+ ```
131
+
132
+ ### Output
133
+
134
+ ```
135
+ D, [2017-09-26T08:03:54.752718 #78207] DEBUG -- : host:http://127.0.0.1:7890/ method:post path:/transaction/prepare-announce params:{:transaction=>{:type=>257, :network=>:testnet, :recipient=>"TA4TX6U5HG2MROAESH2JE5524T4ZOY2EQKQ6ELHF", :amount=>1000000, :message=>{:payload=>"476f6f64206c75636b21", :type=>1}, :fee=>100000, :timeStamp=>78793049, :deadline=>78796649, :version=>2550136833, :signer=>"be2ba9cb15a547110d511a4d43c0482fbb584d78781abac01fb053d18f4a0033"}, :privateKey=>"4ce5c8f9fce571db0d9ac1adf00b8d3ba0f078ed40835fd3d730a2f24b834214"}
136
+ ```
137
+
138
+ ## Examples
139
+
140
+ More specific example codes are in **[examples/](https://github.com/44uk/nem-ruby/tree/master/examples)** directory.
141
+
142
+ ## XEM for development
143
+
144
+ You can get Testnet XEM for development / testing from these faucet or thread.
145
+
146
+ * [NEM Testnet Faucet \- You can get Testnet XEM for development / testing.](http://test-nem-faucet.44uk.net/)
147
+ * [NEM TESTNET faucet〜てすとねっと蛇口〜](http://tomotomo9696.xyz/nem/faucet/)
148
+ * [NEM testnet Faucet](http://namuyan.dip.jp/nem/testnet/)
149
+ * [Paste you address here for beta NEM (Testnet XEM) - Technical Discussion - NEM Forum](https://forum.nem.io/t/paste-you-address-here-for-beta-nem-testnet-xem/829)
150
+
151
+ ## Rubydoc
152
+
153
+ * [Documentation for nem-ruby - rubydoc.info](http://www.rubydoc.info/gems/nem-ruby)