sequence-sdk 1.0.3 → 1.0.4

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.
data/lib/sequence/key.rb CHANGED
@@ -7,30 +7,37 @@ module Sequence
7
7
  # Keys are used to sign transactions.
8
8
  class Key < ResponseObject
9
9
  # @!attribute [r] id
10
- # Unique identifier of the key, based on the public key material itself.
10
+ # Unique identifier of the key, based on the public key material itself.
11
11
  # @return [String]
12
12
  attrib :id
13
13
 
14
14
  # @!attribute [r] alias
15
- # Unique, user-specified identifier of the key.
15
+ # Unique, user-specified identifier of the key.
16
16
  # @return [String]
17
17
  attrib :alias
18
18
 
19
19
  class ClientModule < Sequence::ClientModule
20
-
21
20
  # Creates a key.
22
- # @param [Hash] opts Options hash
23
- # @option opts [String] alias Unique, user-specified identifier of the key.
21
+ # @param [Hash] opts
22
+ # Options hash
23
+ # @option opts [String] alias
24
+ # Unique, user-specified identifier of the key.
24
25
  # @return [Key]
25
26
  def create(opts = {})
27
+ validate_inclusion_of!(opts, :alias)
26
28
  Key.new(client.session.request('create-key', opts))
27
29
  end
28
30
 
29
31
  # Executes a query, returning an enumerable over individual keys.
30
- # @param [Hash] opts Options hash
31
- # @option opts [Array<String>] aliases A list of aliases of keys to retrieve.
32
+ # @param [Hash] opts
33
+ # Options hash
34
+ # @option opts [Array<String>] aliases
35
+ # A list of aliases of keys to retrieve.
36
+ # @option opts [Integer>] page_size
37
+ # The number of items to return in the result set.
32
38
  # @return [Query]
33
39
  def query(opts = {})
40
+ validate_inclusion_of!(opts, :aliases, :page_size, :after)
34
41
  Query.new(client, opts)
35
42
  end
36
43
  end
data/lib/sequence/page.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require_relative './response_object'
2
2
 
3
3
  module Sequence
4
+ # @private
4
5
  class Page < ResponseObject
5
6
  # @!attribute [r] items
6
7
  # List of items.
@@ -4,6 +4,7 @@ module Sequence
4
4
  class Query
5
5
  include ::Enumerable
6
6
 
7
+ # @private
7
8
  # @return [Client]
8
9
  attr_reader :client
9
10
 
@@ -29,23 +30,25 @@ module Sequence
29
30
  end
30
31
  end
31
32
 
32
- def pages
33
- PageQuery.new(client, query, method(:fetch), method(:translate))
34
- end
35
-
36
- # @abstract
33
+ # @private
37
34
  def fetch(query)
38
35
  raise NotImplementedError
39
36
  end
40
37
 
41
38
  # Overwrite to translate API response data to a different Ruby object.
42
- # @abstract
39
+ # @private
43
40
  def translate(response_object)
44
41
  raise NotImplementedError
45
42
  end
46
43
 
47
44
  alias_method :all, :to_a
48
45
 
46
+ # @private
47
+ def pages
48
+ PageQuery.new(client, query, method(:fetch), method(:translate))
49
+ end
50
+
51
+ # @private
49
52
  class PageQuery
50
53
  include ::Enumerable
51
54
 
File without changes
@@ -5,6 +5,7 @@ require_relative './errors'
5
5
  require_relative './version'
6
6
 
7
7
  module Sequence
8
+ # @private
8
9
  class Session
9
10
  def initialize(opts)
10
11
  @opts = opts
@@ -41,33 +42,24 @@ module Sequence
41
42
  end
42
43
 
43
44
  def request(path, body = {})
44
- request_full_resp(path, body)[:parsed_body]
45
+ request_full_resp(nil, path, body)[:parsed_body]
45
46
  end
46
47
 
47
- def request_full_resp(path, body={})
48
- refresh!
49
-
50
- response = @ledger_api.post(ledger_url(path), body)
51
- req_id = response['Chain-Request-ID']
52
- unless req_id.is_a?(String) && req_id.size > 0
53
- raise InvalidRequestIDError.new(response)
54
- end
55
-
56
- status = Integer(response.code)
57
- parsed_body = nil
58
- if status != 204 # No Content
59
- begin
60
- parsed_body = JSON.parse(response.body)
61
- rescue JSON::JSONError
62
- raise JSONError.new(req_id, response)
48
+ def request_full_resp(id, path, body = {})
49
+ refresh!(id)
50
+ id ||= SecureRandom.hex(10)
51
+ @ledger_api.post(id, ledger_url(path), body) do |response|
52
+ # require that the response contains the Chain-Request-ID
53
+ # http header. Since the Sequence API will always set this
54
+ # header, its absence indicates that the request stopped at
55
+ # some intermediary like a proxy on the local network or
56
+ # a Sequence load balancer. This error will be retried by
57
+ # HttpWrapper.post.
58
+ req_id = response['Chain-Request-ID']
59
+ unless req_id.is_a?(String) && req_id.size > 0
60
+ raise InvalidRequestIDError.new(response)
63
61
  end
64
62
  end
65
- if status / 100 != 2
66
- klass = status == 401 ? UnauthorizedError : APIError
67
- raise klass.new(parsed_body, response)
68
- end
69
-
70
- {parsed_body: parsed_body, response: response}
71
63
  end
72
64
 
73
65
  private
@@ -77,14 +69,13 @@ module Sequence
77
69
  "/#{@team_name}/#{@ledger}/#{path}"
78
70
  end
79
71
 
80
- def refresh!
72
+ def refresh!(id)
81
73
  return if @refresh_at > Time.now.to_i
82
74
 
83
75
  result = if @refresh_method
84
76
  @refresh_method.call(@macaroon)
85
77
  else
86
- r = @session_api.post('/sessions/validate', {macaroon: @macaroon})
87
- JSON.parse(r.body)
78
+ @session_api.post(id, '/sessions/validate', macaroon: @macaroon)[:parsed_body]
88
79
  end
89
80
 
90
81
  @team_name = result['team_name']
@@ -4,8 +4,8 @@ require_relative './query'
4
4
 
5
5
  module Sequence
6
6
  # An object describing summary information about a ledger.
7
+ # @private
7
8
  class Stats < ResponseObject
8
-
9
9
  # @!attribute [r] asset_count
10
10
  # The number of assets in the ledger.
11
11
  # @return [Integer]
@@ -28,6 +28,5 @@ module Sequence
28
28
  Stats.new(client.session.request('stats'))
29
29
  end
30
30
  end
31
-
32
31
  end
33
32
  end
@@ -3,47 +3,50 @@ require 'securerandom'
3
3
  require_relative './client_module'
4
4
  require_relative './query'
5
5
  require_relative './response_object'
6
+ require_relative './validations'
6
7
 
7
8
  module Sequence
8
9
  # A transaction is an atomic update to the state of the ledger. Transactions
9
10
  # can issue new asset units, transfer of asset units from one account to
10
11
  # another, and/or the retire asset units from an account.
11
12
  class Transaction < ResponseObject
12
-
13
13
  # @!attribute [r] id
14
- # A unique ID.
14
+ # A unique ID.
15
15
  # @return [String]
16
16
  attrib :id
17
17
 
18
18
  # @!attribute [r] timestamp
19
- # Time of transaction.
19
+ # Time of transaction.
20
20
  # @return [Time]
21
21
  attrib :timestamp, rfc3339_time: true
22
22
 
23
23
  # @!attribute [r] sequence_number
24
- # Sequence number of the transaction.
24
+ # Sequence number of the transaction.
25
25
  # @return [Integer]
26
26
  attrib :sequence_number
27
27
 
28
28
  # @!attribute [r] reference_data
29
- # User-specified key-value data embedded into the transaction.
29
+ # User-specified key-value data embedded into the transaction.
30
30
  # @return [Hash]
31
31
  attrib :reference_data
32
32
 
33
33
  # @!attribute [r] actions
34
- # List of actions taken by the transaction.
34
+ # List of actions taken by the transaction.
35
35
  # @return [Array<Action>]
36
36
  attrib(:actions) { |raw| raw.map { |v| Action.new(v) } }
37
37
 
38
38
  # @!attribute [r] contracts
39
- # List of contracts created by the transaction.
39
+ # List of contracts created by the transaction.
40
40
  # @return [Array<Contract>]
41
41
  attrib(:contracts) { |raw| raw.map { |v| Contract.new(v) } }
42
42
 
43
43
  class ClientModule < Sequence::ClientModule
44
44
  # Builds, signs, and submits a transaction.
45
- # @param [Builder] builder Builder object with actions defined. If provided, overrides block parameter.
46
- # @yield Block defining transaction actions. A {Builder} object is passed as the only parameter.
45
+ # @param [Builder] builder
46
+ # Builder object with actions defined. If provided, overrides block
47
+ # parameter.
48
+ # @yield Block defining transaction actions. A {Builder} object is passed
49
+ # as the only parameter.
47
50
  # @return Transaction
48
51
  def transact(builder = nil, &block)
49
52
  if builder.nil?
@@ -55,18 +58,25 @@ module Sequence
55
58
  '/sign-transaction',
56
59
  transaction: tpl,
57
60
  )
58
- Transaction.new(client.session.request(
59
- 'submit-transaction',
60
- {transaction: tpl}
61
- ))
61
+ Transaction.new(
62
+ client.session.request('submit-transaction', transaction: tpl),
63
+ )
62
64
  end
63
65
 
64
66
  # Executes a query, returning an enumerable over individual transactions.
65
67
  # @param [Hash] opts Options hash
66
- # @option opts [String] filter A filter expression.
67
- # @option opts [Array<String|Integer>] filter_params A list of values that will be interpolated into the filter expression.
68
- # @option opts [Integer] start_time A Unix timestamp in milliseconds of the earliest transaction timestamp to include in the query results.
69
- # @option opts [Integer] end_time A Unix timestamp in milliseconds of the most recent transaction timestamp to include in the query results.
68
+ # @option opts [String] filter
69
+ # A filter expression.
70
+ # @option opts [Array<String|Integer>] filter_params
71
+ # A list of values that will be interpolated into the filter expression.
72
+ # @option opts [Integer] start_time
73
+ # A Unix timestamp in milliseconds of the earliest transaction timestamp
74
+ # to include in the query results.
75
+ # @option opts [Integer] end_time
76
+ # A Unix timestamp in milliseconds of the most recent transaction
77
+ # timestamp to include in the query results.
78
+ # @option opts [Integer>] page_size
79
+ # The number of items to return in the result set.
70
80
  # @return [Query]
71
81
  def query(opts = {})
72
82
  Query.new(client, opts)
@@ -86,72 +96,80 @@ module Sequence
86
96
  # An action taken by a transaction.
87
97
  class Action < ResponseObject
88
98
  # @!attribute [r] type
89
- # The type of the action. Possible values are "issue", "transfer" and "retire".
99
+ # The type of the action. Possible values are "issue", "transfer" and
100
+ # "retire".
90
101
  # @return [String]
91
102
  attrib :type
92
103
 
93
104
  # @!attribute [r] asset_id
94
- # The id of the action's asset.
105
+ # The id of the action's asset.
95
106
  # @return [String]
96
107
  attrib :asset_id
97
108
 
98
109
  # @!attribute [r] asset_alias
99
- # The alias of the action's asset.
110
+ # The alias of the action's asset.
100
111
  # @return [String]
101
112
  attrib :asset_alias
102
113
 
103
114
  # @!attribute [r] asset_tags
104
- # The tags of the action's asset.
115
+ # The tags of the action's asset.
105
116
  # @return [Hash]
106
117
  attrib :asset_tags
107
118
 
108
119
  # @!attribute [r] amount
109
- # The number of asset units issues, transferred, or retired.
120
+ # The number of asset units issues, transferred, or retired.
110
121
  # @return [Integer]
111
122
  attrib :amount
112
123
 
113
124
  # @!attribute [r] source_account_id
114
- # The ID of the account serving as the source of asset units. Null for issuances.
125
+ # The ID of the account serving as the source of asset units. Null for
126
+ # issuances.
115
127
  # @return [String]
116
128
  attrib :source_account_id
117
129
 
118
130
  # @!attribute [r] source_account_alias
119
- # The alias of the account serving as the source of asset units. Null for issuances.
131
+ # The alias of the account serving as the source of asset units. Null
132
+ # for issuances.
120
133
  # @return [String]
121
134
  attrib :source_account_alias
122
135
 
123
136
  # @!attribute [r] source_account_tags
124
- # The tags of the account serving as the source of asset units. Null for issuances.
137
+ # The tags of the account serving as the source of asset units. Null for
138
+ # issuances.
125
139
  # @return [String]
126
140
  attrib :source_account_tags
127
141
 
128
142
  # @!attribute [r] destination_account_id
129
- # The ID of the account receiving the asset units. Null for retirements.
143
+ # The ID of the account receiving the asset units. Null for retirements.
130
144
  # @return [String]
131
145
  attrib :destination_account_id
132
146
 
133
147
  # @!attribute [r] destination_account_alias
134
- # The alias of the account receiving the asset units. Null for retirements.
148
+ # The alias of the account receiving the asset units. Null for
149
+ # retirements.
135
150
  # @return [String]
136
151
  attrib :destination_account_alias
137
152
 
138
153
  # @!attribute [r] destination_account_tags
139
- # The tags of the account receiving the asset units. Null for retirements.
154
+ # The tags of the account receiving the asset units. Null for
155
+ # retirements.
140
156
  # @return [String]
141
157
  attrib :destination_account_tags
142
158
 
143
159
  # @!attribute [r] reference_data
144
- # User-specified, key-value data embedded into the action.
160
+ # User-specified, key-value data embedded into the action.
145
161
  # @return [Hash]
146
162
  attrib :reference_data
147
163
  end
148
164
 
149
165
  # A configuration object for creating and submitting transactions.
150
166
  class Builder
167
+ include Sequence::Validations
168
+
151
169
  attr_accessor :reference_data
152
170
 
153
171
  def initialize(&block)
154
- block.call(self) if block
172
+ yield(self) if block
155
173
  end
156
174
 
157
175
  def actions
@@ -170,78 +188,165 @@ module Sequence
170
188
  end
171
189
 
172
190
  # Adds an action to a transaction builder.
173
- # @param [Hash] params Action parameters.
191
+ # @param [Hash] opts
192
+ # Action parameters.
174
193
  # @return [Builder]
175
- def add_action(params)
176
- # Some actions require an idempotency token, so we'll add it here as a
177
- # generic parameter.
178
- params = {client_token: SecureRandom.uuid}.merge(params)
179
- actions << params
194
+ def add_action(opts = {})
195
+ if opts[:amount].nil?
196
+ raise ArgumentError, ':amount must be provided'
197
+ end
198
+ actions << opts
180
199
  self
181
200
  end
182
201
 
183
202
  # Issues new units of an asset to a destination account.
184
203
  #
185
- # @param [Hash] params Options hash
186
- # @option params [String] :asset_id ID of the asset to be issued.
187
- # You must specify either an ID or an alias.
188
- # @option params [String] :asset_alias Asset alias of the asset to be issued.
189
- # You must specify either an ID or an alias.
190
- # @option params [Integer] :amount amount of the asset to be issued.
191
- # @option params [String] :destination_account_id ID of the account receiving the asset units.
192
- # You must specify a destination account ID or alias.
193
- # @option params [String] :destination_account_alias alias of the account receiving the asset units.
194
- # You must specify a destination account ID or alias.
195
- # @option params [Hash] :reference_data reference data for the action.
204
+ # @param [Hash] opts
205
+ # Options hash
206
+ # @option opts [Integer] :amount
207
+ # Amount of the asset to be issued.
208
+ # @option opts [String] :asset_id
209
+ # ID of the asset to be issued. You must specify either an ID or an
210
+ # alias.
211
+ # @option opts [String] :asset_alias
212
+ # Asset alias of the asset to be issued. You must specify either an ID
213
+ # or an alias.
214
+ # @option opts [String] :destination_account_id
215
+ # ID of the account receiving the asset units. You must specify a
216
+ # destination account ID or alias.
217
+ # @option opts [String] :destination_account_alias
218
+ # Alias of the account receiving the asset units. You must specify a
219
+ # destination account ID or alias.
220
+ # @option opts [Hash] :reference_data
221
+ # Reference data for the action.
196
222
  # @return [Builder]
197
- def issue(params)
198
- add_action(params.merge(type: :issue))
223
+ def issue(opts = {})
224
+ validate_inclusion_of!(
225
+ opts,
226
+ :amount,
227
+ :asset_id,
228
+ :asset_alias,
229
+ :destination_account_id,
230
+ :destination_account_alias,
231
+ :reference_data,
232
+ )
233
+ validate_either!(opts, :asset_id, :asset_alias)
234
+ validate_either!(
235
+ opts,
236
+ :destination_account_id,
237
+ :destination_account_alias,
238
+ )
239
+ add_action(opts.merge(type: :issue))
199
240
  end
200
241
 
201
- # Moves units of an asset from a source (an account or contract) to a destination account.
242
+ # Moves units of an asset from a source (an account or contract) to a
243
+ # destination account.
202
244
  #
203
- # @param [Hash] params Options hash
204
- # @option params [String] :source_account_id ID of the account serving as the source of asset units.
205
- # You must specify a source account ID, account alias, or contract ID.
206
- # @option params [String] :source_account_alias Alias of the account serving as the source of asset units
207
- # You must specify a source account ID, account alias, or contract ID.
208
- # @option params [String] :source_contract_id ID of the contract serving as the source of asset units.
209
- # You must specify a source account ID, account alias, or contract ID.
210
- # @option params [String] :asset_id ID of the asset to be transferred.
211
- # You must specify either an ID or an alias.
212
- # @option params [String] :asset_alias Asset alias of the asset to be transferred.
213
- # You must specify either an ID or an alias.
214
- # @option params [Integer] :amount amount of the asset to be transferred.
215
- # @option params [String] :destination_account_id ID of the account receiving the asset units.
216
- # You must specify a destination account ID or alias.
217
- # @option params [String] :destination_account_alias alias of the account receiving the asset units.
218
- # You must specify a destination account ID or alias.
219
- # @option params [Hash] :reference_data reference data for the action.
220
- # @option params [Hash] :change_reference_data reference data for the change contract.
245
+ # @param [Hash] opts
246
+ # Options hash
247
+ # @option opts [Integer] :amount
248
+ # Amount of the asset to be transferred.
249
+ # @option opts [String] :asset_id
250
+ # ID of the asset to be transferred. You must specify either an ID or an
251
+ # alias.
252
+ # @option opts [String] :asset_alias
253
+ # Asset alias of the asset to be transferred. You must specify either an
254
+ # ID or an alias.
255
+ # @option opts [String] :source_account_id
256
+ # ID of the account serving as the source of asset units. You must
257
+ # specify a source account ID, account alias, or contract ID.
258
+ # @option opts [String] :source_account_alias
259
+ # Alias of the account serving as the source of asset units You must
260
+ # specify a source account ID, account alias, or contract ID.
261
+ # @option opts [String] :source_contract_id
262
+ # ID of the contract serving as the source of asset units. You must
263
+ # specify a source account ID, account alias, or contract ID.
264
+ # @option opts [String] :destination_account_id
265
+ # ID of the account receiving the asset units. You must specify a
266
+ # destination account ID or alias.
267
+ # @option opts [String] :destination_account_alias
268
+ # Alias of the account receiving the asset units. You must specify a
269
+ # destination account ID or alias.
270
+ # @option opts [Hash] :reference_data
271
+ # reference data for the action.
272
+ # @option opts [Hash] :change_reference_data
273
+ # reference data for the change contract.
221
274
  # @return [Builder]
222
- def transfer(params)
223
- add_action(params.merge(type: :transfer))
275
+ def transfer(opts = {})
276
+ validate_inclusion_of!(
277
+ opts,
278
+ :amount,
279
+ :asset_id,
280
+ :asset_alias,
281
+ :source_account_id,
282
+ :source_account_alias,
283
+ :source_contract_id,
284
+ :destination_account_id,
285
+ :destination_account_alias,
286
+ :reference_data,
287
+ :change_reference_data,
288
+ )
289
+ validate_either!(opts, :asset_id, :asset_alias)
290
+ validate_either!(
291
+ opts,
292
+ :source_account_id,
293
+ :source_account_alias,
294
+ :source_contract_id,
295
+ )
296
+ validate_either!(
297
+ opts,
298
+ :destination_account_id,
299
+ :destination_account_alias,
300
+ )
301
+ add_action(opts.merge(type: :transfer))
224
302
  end
225
303
 
226
- # Takes units of an asset from a source (an account or contract) and retires them.
304
+ # Takes units of an asset from a source (an account or contract) and
305
+ # retires them.
227
306
  #
228
- # @param [Hash] params Options hash
229
- # @option params [String] :source_account_id ID of the account serving as the source of asset units.
230
- # You must specify a source account ID, account alias, or contract ID.
231
- # @option params [String] :source_account_alias Alias of the account serving as the source of asset units
232
- # You must specify a source account ID, account alias, or contract ID.
233
- # @option params [String] :source_contract_id ID of the contract serving as the source of asset units.
234
- # You must specify a source account ID, account alias, or contract ID.
235
- # @option params [String] :asset_id ID of the asset to be retired.
236
- # You must specify either an ID or an alias.
237
- # @option params [String] :asset_alias Asset alias of the asset to be retired.
238
- # You must specify either an ID or an alias.
239
- # @option params [Integer] :amount amount of the asset to be retired.
240
- # @option params [Hash] :reference_data reference data for the action.
241
- # @option params [Hash] :change_reference_data reference data for the change contract.
307
+ # @param [Hash] opts Options hash
308
+ # @option opts [Integer] :amount
309
+ # Amount of the asset to be retired.
310
+ # @option opts [String] :asset_id
311
+ # ID of the asset to be retired. You must specify either an ID or an
312
+ # alias.
313
+ # @option opts [String] :asset_alias
314
+ # Asset alias of the asset to be retired. You must specify either an ID
315
+ # or an alias.
316
+ # @option opts [String] :source_account_id
317
+ # ID of the account serving as the source of asset units. You must
318
+ # specify a source account ID, account alias, or contract ID.
319
+ # @option opts [String] :source_account_alias
320
+ # Alias of the account serving as the source of asset units You must
321
+ # specify a source account ID, account alias, or contract ID.
322
+ # @option opts [String] :source_contract_id
323
+ # ID of the contract serving as the source of asset units. You must
324
+ # specify a source account ID, account alias, or contract ID.
325
+ # @option opts [Hash] :reference_data
326
+ # Reference data for the action.
327
+ # @option opts [Hash] :change_reference_data
328
+ # Reference data for the change contract.
242
329
  # @return [Builder]
243
- def retire(params)
244
- add_action(params.merge(type: :retire))
330
+ def retire(opts = {})
331
+ validate_inclusion_of!(
332
+ opts,
333
+ :amount,
334
+ :asset_id,
335
+ :asset_alias,
336
+ :source_account_id,
337
+ :source_account_alias,
338
+ :source_contract_id,
339
+ :reference_data,
340
+ :change_reference_data,
341
+ )
342
+ validate_either!(opts, :asset_id, :asset_alias)
343
+ validate_either!(
344
+ opts,
345
+ :source_account_id,
346
+ :source_account_alias,
347
+ :source_contract_id,
348
+ )
349
+ add_action(opts.merge(type: :retire))
245
350
  end
246
351
  end
247
352
  end