rail0-sdk 1.3.0 → 1.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1304e38f4530a6bfdbfe8f3196ed562b4869b9e6ab7d455cae744afb21ad21ed
4
- data.tar.gz: 87090219ed6aef2133815ca4a704a5c7a8e926cead00e59688a42aac25f29297
3
+ metadata.gz: 23d06a854473333eb6779af7bcec4ea5d370ab9c24106cef8a1db9809ca29803
4
+ data.tar.gz: b6a28727e25233378a5c39feceb00f7a638d44590a8f9f01f356e1e6e0d35f91
5
5
  SHA512:
6
- metadata.gz: 77f22e52615cd7cac1b1535084a388111e20850e01a89a39628322bf825f8ea023c1db7e9d185bbb9c0616fa1827add2ebbdb2d8749cbf60fbfe099f6f0719f8
7
- data.tar.gz: ae3d34f5cceffb1ea59f33114b53c5308eb115b929bfa581717e7e3357fedbb38a527ef0a35136943eadbc4fa0e93a589ebbacff4f13c880f885c95869644a4a
6
+ metadata.gz: fdb399b1895e43ef062221da06357db402bb59fc746a36d6a21499d9ac82625723db5dc454cb8c46cb1e77e4889d05b00f8497da9d858a0e8f470e286c29f4a4
7
+ data.tar.gz: 7f329e33476a42b6d3304c3a1978dab10425740a460d2f6a066660ce9f67e24399c81d783372d65172b5247e670362970f82ddbf16e7fb7d5c7a5172e8fb9e8b
data/README.md CHANGED
@@ -770,6 +770,17 @@ RAIL0 itself:
770
770
  A failed transaction carries the same triple as `error_code`, `error_title` and
771
771
  `error_detail`, whether it reverted on-chain or was refused before broadcast.
772
772
 
773
+ ### Ids in request paths
774
+
775
+ Every id (and operation name) a method interpolates into a request path is
776
+ percent-encoded as ONE path segment, so a value carrying `/`, `?`, `#`, a space or
777
+ non-ASCII characters can never address a different route than the method names
778
+ (`payments.get("../webhooks/x")` requests `/payments/..%2Fwebhooks%2Fx`, not
779
+ `/webhooks/x`). The ids the gateway hands out — UUIDs, `0x` hex — are unchanged by it.
780
+ A value that is `nil`, empty, `"."` or `".."` is never a valid id, and would still
781
+ re-target the request once encoded, so the method raises `ArgumentError` before
782
+ sending anything.
783
+
773
784
  ## Configuration
774
785
 
775
786
  ```ruby
@@ -854,7 +865,7 @@ lib/rail0/
854
865
  accounts.rb the caller's own account profile, read and update (JWT)
855
866
  webhooks.rb webhook subscription management (JWT)
856
867
  analytics.rb account-scoped payment analytics (JWT)
857
- query.rb shared query-string helper
868
+ query.rb shared query-string and path-segment helpers
858
869
  ```
859
870
 
860
871
  ## Development
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "query"
4
+
3
5
  module Rail0
4
6
  module Resources
5
7
  # The merchant account itself (requires JWT).
@@ -17,6 +19,8 @@ module Rail0
17
19
  # {Resources::Wallets}; buyer-facing discovery of what a merchant accepts lives on
18
20
  # {Resources::PaymentMethods}.
19
21
  class Accounts
22
+ include Query
23
+
20
24
  attr_reader :http
21
25
 
22
26
  def initialize(http)
@@ -29,7 +33,7 @@ module Rail0
29
33
  # @return [Hash] `id`, `name`, `email`, `created_at`, `updated_at`. `email` is part of
30
34
  # the response because only the account's owner, or an operator, can read it.
31
35
  def get(account_id)
32
- http.get("/accounts/#{account_id}")
36
+ http.get("/accounts/#{segment(account_id)}")
33
37
  end
34
38
 
35
39
  # Update the account's own profile (PATCH /accounts/:account_id).
@@ -55,7 +59,7 @@ module Rail0
55
59
  body[:email] = email unless email.nil?
56
60
  raise ArgumentError, "at least one of name or email is required" if body.empty?
57
61
 
58
- http.patch("/accounts/#{account_id}", body)
62
+ http.patch("/accounts/#{segment(account_id)}", body)
59
63
  end
60
64
  end
61
65
  end
@@ -99,7 +99,7 @@ module Rail0
99
99
  # (Integer or nil — divide `amount`, `capturable_amount` and `refundable_amount` by
100
100
  # 10**decimals to render them) and `in_flight` (Boolean — see {list}).
101
101
  def get(id)
102
- http.get("/payments/#{id}")
102
+ http.get("/payments/#{segment(id)}")
103
103
  end
104
104
 
105
105
  # List on-chain transactions for a payment.
@@ -114,7 +114,7 @@ module Rail0
114
114
  # @return [Hash] { data: Array<Hash>, meta: { page:, per_page:, total: } }
115
115
  def transactions(id, operation: nil, status: nil, sort: nil, page: nil, per_page: nil)
116
116
  query = build_query(operation: operation, status: status, sort: sort, page: page, per_page: per_page)
117
- http.get_list("/payments/#{id}/transactions#{query}")
117
+ http.get_list("/payments/#{segment(id)}/transactions#{query}")
118
118
  end
119
119
 
120
120
  # Fetch ONE of a payment's transactions
@@ -130,7 +130,7 @@ module Rail0
130
130
  # @param transaction_id [String] Transaction UUID.
131
131
  # @return [Hash]
132
132
  def get_transaction(id, transaction_id)
133
- http.get("/payments/#{id}/transactions/#{transaction_id}")
133
+ http.get("/payments/#{segment(id)}/transactions/#{segment(transaction_id)}")
134
134
  end
135
135
 
136
136
  # Re-enqueue a stuck broadcast
@@ -153,7 +153,7 @@ module Rail0
153
153
  # @param transaction_id [String] The transaction row to redrive.
154
154
  # @return [Hash] The transaction, re-enqueued.
155
155
  def redrive(id, transaction_id)
156
- http.post("/payments/#{id}/transactions/#{transaction_id}/redrive", {})
156
+ http.post("/payments/#{segment(id)}/transactions/#{segment(transaction_id)}/redrive", {})
157
157
  end
158
158
 
159
159
  # Submit the payer's EIP-712 signature (PUT /payments/{id}/sign).
@@ -161,7 +161,7 @@ module Rail0
161
161
  # @param params [Hash] { signature: "0x…" } (65-byte 0x-prefixed hex).
162
162
  # @return [Hash]
163
163
  def sign(id, params)
164
- http.put("/payments/#{id}/sign", params)
164
+ http.put("/payments/#{segment(id)}/sign", params)
165
165
  end
166
166
 
167
167
  # List a payment's dispute open/close history.
@@ -173,7 +173,7 @@ module Rail0
173
173
  # @return [Hash] { data: Array<Hash>, meta: { page:, per_page:, total: } }
174
174
  def disputes(id, status: nil, sort: nil, page: nil, per_page: nil)
175
175
  query = build_query(status: status, sort: sort, page: page, per_page: per_page)
176
- http.get_list("/payments/#{id}/disputes#{query}")
176
+ http.get_list("/payments/#{segment(id)}/disputes#{query}")
177
177
  end
178
178
 
179
179
  # Build the unsigned transaction for an operation
@@ -193,7 +193,7 @@ module Rail0
193
193
  # with different terms is refused 422 +idempotency_key_reused+. Scoped to this
194
194
  # payment. (rail0-gateway#331)
195
195
  def prepare(id, operation, body = nil, idempotency_key: nil)
196
- http.post("/payments/#{id}/#{operation}/prepare", body,
196
+ http.post("/payments/#{segment(id)}/#{segment(operation)}/prepare", body,
197
197
  headers: idempotency_headers(idempotency_key))
198
198
  end
199
199
 
@@ -203,7 +203,7 @@ module Rail0
203
203
  # @param params [Hash] { signed_transaction: "0x…" }.
204
204
  # @return [Hash]
205
205
  def submit(id, operation, params)
206
- http.post("/payments/#{id}/#{operation}", params)
206
+ http.post("/payments/#{segment(id)}/#{segment(operation)}", params)
207
207
  end
208
208
 
209
209
  # Record a transaction the caller broadcast themselves (MetaMask/wallet flow)
@@ -213,7 +213,7 @@ module Rail0
213
213
  # @param params [Hash] { transaction_hash: "0x…" }.
214
214
  # @return [Hash]
215
215
  def submit_by_hash(id, operation, params)
216
- http.post("/payments/#{id}/#{operation}/submitted", params)
216
+ http.post("/payments/#{segment(id)}/#{segment(operation)}/submitted", params)
217
217
  end
218
218
 
219
219
  # Phase 1 — build the unsigned authorize() transaction (escrow hold).
@@ -302,7 +302,7 @@ module Rail0
302
302
  # @param params [Hash] { signed_transaction: "0x…" }.
303
303
  # @return [Hash]
304
304
  def dispute(id, params)
305
- http.post("/payments/#{id}/dispute", params)
305
+ http.post("/payments/#{segment(id)}/dispute", params)
306
306
  end
307
307
 
308
308
  # Phase 1 — build the unsigned closeDispute() transaction (payer only).
@@ -318,7 +318,7 @@ module Rail0
318
318
  # @param params [Hash] { signed_transaction: "0x…" }.
319
319
  # @return [Hash]
320
320
  def close_dispute(id, params)
321
- http.post("/payments/#{id}/dispute/close", params)
321
+ http.post("/payments/#{segment(id)}/dispute/close", params)
322
322
  end
323
323
 
324
324
  # The payer's counterpart to {#submit_by_hash}, which covers only the operations
@@ -336,7 +336,7 @@ module Rail0
336
336
  # @param params [Hash] { transaction_hash: "0x…" }.
337
337
  # @return [Hash]
338
338
  def dispute_submit_by_hash(id, params)
339
- http.post("/payments/#{id}/dispute/submitted", params)
339
+ http.post("/payments/#{segment(id)}/dispute/submitted", params)
340
340
  end
341
341
 
342
342
  # Report an already-broadcast close-dispute transaction by hash (payer only); HTTP 202.
@@ -344,14 +344,17 @@ module Rail0
344
344
  # @param params [Hash] { transaction_hash: "0x…" }.
345
345
  # @return [Hash]
346
346
  def close_dispute_submit_by_hash(id, params)
347
- http.post("/payments/#{id}/dispute/close/submitted", params)
347
+ http.post("/payments/#{segment(id)}/dispute/close/submitted", params)
348
348
  end
349
349
 
350
350
  private
351
351
 
352
+ # +path+ is one of this class's own literals ("dispute/prepare",
353
+ # "dispute/close/prepare"), never caller input, so it is interpolated as-is:
354
+ # its `/` is meant to separate segments. Only +id+ goes through {#segment}.
352
355
  def prepare_dispute(path, id, reason, idempotency_key: nil)
353
356
  body = reason ? { reason: reason } : {}
354
- http.post("/payments/#{id}/#{path}", body,
357
+ http.post("/payments/#{segment(id)}/#{path}", body,
355
358
  headers: idempotency_headers(idempotency_key))
356
359
  end
357
360
 
@@ -1,15 +1,59 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "cgi"
4
+ require "erb"
4
5
 
5
6
  module Rail0
6
7
  module Resources
7
- # Shared query-string builder for resource classes. Included so every
8
- # resource turns keyword filters into a URL query the same way: nil values
9
- # are dropped, and booleans/integers are stringified.
8
+ # Shared URL builders for resource classes. Included so every resource turns
9
+ # keyword filters into a URL query the same way (nil values are dropped,
10
+ # booleans/integers are stringified) and escapes every caller-supplied path
11
+ # segment the same way ({#segment}).
10
12
  module Query
13
+ # Segments that would not stay put once interpolated: "." and ".." are dot
14
+ # segments that a URL normaliser (a proxy, a CDN, the server's router) may
15
+ # resolve away, and an empty one collapses the path onto its parent.
16
+ UNSAFE_SEGMENTS = ["", ".", ".."].freeze
17
+ private_constant :UNSAFE_SEGMENTS
18
+
11
19
  private
12
20
 
21
+ # Escapes ONE caller-supplied value for interpolation into a request path:
22
+ #
23
+ # http.get("/payments/#{segment(id)}/transactions/#{segment(transaction_id)}")
24
+ #
25
+ # Paths used to be plain interpolation, so an id was pasted in raw: one
26
+ # containing `/`, `?`, `#` or `..` addressed a different route than the method
27
+ # named — `payments.get("../webhooks/x")` was a GET on /webhooks/x with the
28
+ # caller's token. The ids this SDK is handed normally come from the gateway, but
29
+ # a caller that forwards one from a URL (a proxy route, a CLI argument) passes
30
+ # along whatever it was given.
31
+ #
32
+ # ERB::Util.url_encode percent-encodes everything outside the RFC 3986
33
+ # unreserved set (A-Z a-z 0-9 - . _ ~), UTF-8 byte by byte, with a space as
34
+ # %20 — the path-correct form (CGI.escape, used for queries, would turn a
35
+ # space into `+`, which in a path is a literal plus). For the values legitimately
36
+ # passed (UUIDs, 0x hex, operation names, integer ids) it changes nothing. This
37
+ # matches rail0-ts's `path` tag, which uses encodeURIComponent.
38
+ #
39
+ # A value that is exactly "", "." or ".." raises ArgumentError instead: encoding
40
+ # leaves the dots as they are (they are unreserved), so the segment would still
41
+ # be a dot segment, and an empty one silently re-targets the parent route
42
+ # (`payments.get("")` would list payments). None of them is a valid id, so
43
+ # failing before the request is the only honest answer.
44
+ #
45
+ # @param value [#to_s] The id (or other caller-supplied value) to interpolate.
46
+ # @return [String] The percent-encoded segment.
47
+ # @raise [ArgumentError] If the value is nil, empty, "." or "..".
48
+ def segment(value)
49
+ str = value.to_s
50
+ if value.nil? || UNSAFE_SEGMENTS.include?(str)
51
+ raise ArgumentError, "invalid path segment #{value.inspect}: an id must not be nil, empty, \".\" or \"..\""
52
+ end
53
+
54
+ ERB::Util.url_encode(str)
55
+ end
56
+
13
57
  # Builds "?k=v&…" from keyword filters, or "" when every value is nil.
14
58
  #
15
59
  # An Array value becomes ONE comma-separated pair (`status: %w[a b]` →
@@ -38,7 +38,7 @@ module Rail0
38
38
  query = build_query(chain_id: chain_id, token_symbol: token_symbol, active: active,
39
39
  default: default, token_active: token_active,
40
40
  sort: sort, page: page, per_page: per_page)
41
- http.get_list("/accounts/#{account_id}/wallets#{query}")
41
+ http.get_list("/accounts/#{segment(account_id)}/wallets#{query}")
42
42
  end
43
43
 
44
44
  # Fetch a single wallet by its id or 0x address.
@@ -46,7 +46,7 @@ module Rail0
46
46
  # @param id_or_address [String] Wallet UUID or 0x address.
47
47
  # @return [Hash] id, address, label, active
48
48
  def get(account_id, id_or_address)
49
- http.get("/accounts/#{account_id}/wallets/#{id_or_address}")
49
+ http.get(wallet_path(account_id, id_or_address))
50
50
  end
51
51
 
52
52
  # Add a wallet to the account.
@@ -74,7 +74,7 @@ module Rail0
74
74
  def create(account_id, address:, message:, signature:, label: nil)
75
75
  body = { address: address, message: message, signature: signature }
76
76
  body[:label] = label unless label.nil?
77
- http.post("/accounts/#{account_id}/wallets", body)
77
+ http.post("/accounts/#{segment(account_id)}/wallets", body)
78
78
  end
79
79
 
80
80
  # Update a wallet's label and/or active status.
@@ -87,7 +87,7 @@ module Rail0
87
87
  body = {}
88
88
  body[:label] = label unless label.nil?
89
89
  body[:active] = active unless active.nil?
90
- http.patch("/accounts/#{account_id}/wallets/#{id_or_address}", body)
90
+ http.patch(wallet_path(account_id, id_or_address), body)
91
91
  end
92
92
 
93
93
  # Soft-delete (deactivate) a wallet. Returns HTTP 204.
@@ -95,7 +95,7 @@ module Rail0
95
95
  # @param id_or_address [String] Wallet UUID or 0x address.
96
96
  # @return [nil]
97
97
  def delete(account_id, id_or_address)
98
- http.delete("/accounts/#{account_id}/wallets/#{id_or_address}")
98
+ http.delete(wallet_path(account_id, id_or_address))
99
99
  end
100
100
 
101
101
  # Read a wallet's live on-chain balances across the configured chains. Each
@@ -109,7 +109,7 @@ module Rail0
109
109
  # @return [Hash] wallet_id, address, balances
110
110
  def balances(account_id, id_or_address, chain_id: nil, token_symbol: nil)
111
111
  query = build_query(chain_id: chain_id, token_symbol: token_symbol)
112
- http.get("/accounts/#{account_id}/wallets/#{id_or_address}/balances#{query}")
112
+ http.get("#{wallet_path(account_id, id_or_address)}/balances#{query}")
113
113
  end
114
114
 
115
115
  # ── Token holdings ────────────────────────────────────────────────────────
@@ -133,7 +133,7 @@ module Rail0
133
133
  def add_token(account_id, id_or_address, chain_id:, token:, default: nil)
134
134
  body = { chain_id: chain_id, token: token }
135
135
  body[:default] = default unless default.nil?
136
- http.post("/accounts/#{account_id}/wallets/#{id_or_address}/tokens", body)
136
+ http.post("#{wallet_path(account_id, id_or_address)}/tokens", body)
137
137
  end
138
138
 
139
139
  # Stop accepting a token on this wallet (soft delete). Returns HTTP 204.
@@ -150,13 +150,13 @@ module Rail0
150
150
  #
151
151
  # @return [nil]
152
152
  def remove_token(account_id, id_or_address, token_id)
153
- http.delete("/accounts/#{account_id}/wallets/#{id_or_address}/tokens/#{token_id}")
153
+ http.delete("#{wallet_path(account_id, id_or_address)}/tokens/#{segment(token_id)}")
154
154
  end
155
155
 
156
156
  # Re-enable an existing token holding.
157
157
  # @return [Hash] the token holding
158
158
  def enable_token(account_id, id_or_address, token_id)
159
- http.patch("/accounts/#{account_id}/wallets/#{id_or_address}/tokens/#{token_id}/enable")
159
+ http.patch("#{wallet_path(account_id, id_or_address)}/tokens/#{segment(token_id)}/enable")
160
160
  end
161
161
 
162
162
  # Disable an existing token holding without forgetting it — the holding (and
@@ -166,7 +166,15 @@ module Rail0
166
166
  # active default holding — promote another token to default first.
167
167
  # @return [Hash] the token holding
168
168
  def disable_token(account_id, id_or_address, token_id)
169
- http.patch("/accounts/#{account_id}/wallets/#{id_or_address}/tokens/#{token_id}/disable")
169
+ http.patch("#{wallet_path(account_id, id_or_address)}/tokens/#{segment(token_id)}/disable")
170
+ end
171
+
172
+ private
173
+
174
+ # "/accounts/{account_id}/wallets/{id_or_address}" with both ids escaped by
175
+ # {Query#segment} — the prefix every single-wallet route shares.
176
+ def wallet_path(account_id, id_or_address)
177
+ "/accounts/#{segment(account_id)}/wallets/#{segment(id_or_address)}"
170
178
  end
171
179
  end
172
180
  end
@@ -71,7 +71,7 @@ module Rail0
71
71
  # @param id [String] Webhook UUID.
72
72
  # @return [Hash]
73
73
  def get(id)
74
- http.get("/webhooks/#{id}")
74
+ http.get("/webhooks/#{segment(id)}")
75
75
  end
76
76
 
77
77
  # Update a webhook's name, callback_url, and/or topics.
@@ -87,35 +87,35 @@ module Rail0
87
87
  body[:name] = name unless name.nil?
88
88
  body[:callback_url] = callback_url unless callback_url.nil?
89
89
  body[:topics] = Array(topics) unless topics.nil?
90
- http.patch("/webhooks/#{id}", body)
90
+ http.patch("/webhooks/#{segment(id)}", body)
91
91
  end
92
92
 
93
93
  # Re-enable a disabled webhook.
94
94
  # @param id [String] Webhook UUID.
95
95
  # @return [Hash]
96
96
  def enable(id)
97
- http.put("/webhooks/#{id}/enable")
97
+ http.put("/webhooks/#{segment(id)}/enable")
98
98
  end
99
99
 
100
100
  # Disable a webhook (stops deliveries without deleting it).
101
101
  # @param id [String] Webhook UUID.
102
102
  # @return [Hash]
103
103
  def disable(id)
104
- http.put("/webhooks/#{id}/disable")
104
+ http.put("/webhooks/#{segment(id)}/disable")
105
105
  end
106
106
 
107
107
  # Generate a new shared secret, returned once on the response.
108
108
  # @param id [String] Webhook UUID.
109
109
  # @return [Hash] webhook record including the new shared_secret
110
110
  def rotate_secret(id)
111
- http.put("/webhooks/#{id}/rotate_secret")
111
+ http.put("/webhooks/#{segment(id)}/rotate_secret")
112
112
  end
113
113
 
114
114
  # Reset the delivery circuit breaker and re-enable the webhook.
115
115
  # @param id [String] Webhook UUID.
116
116
  # @return [Hash]
117
117
  def reset_circuit(id)
118
- http.put("/webhooks/#{id}/reset_circuit")
118
+ http.put("/webhooks/#{segment(id)}/reset_circuit")
119
119
  end
120
120
 
121
121
  # List delivery attempts for a webhook.
@@ -138,7 +138,7 @@ module Rail0
138
138
  query = build_query(status: status, topic: topic, payment_id: payment_id,
139
139
  response_code: response_code, since: since, until: until_time,
140
140
  sort: sort, page: page, per_page: per_page)
141
- http.get_list("/webhooks/#{id}/event_callbacks#{query}")
141
+ http.get_list("/webhooks/#{segment(id)}/event_callbacks#{query}")
142
142
  end
143
143
 
144
144
  # Re-deliver one recorded delivery's exact payload
@@ -161,14 +161,14 @@ module Rail0
161
161
  # @param callback_id [String] The event callback's UUID (from {event_callbacks}).
162
162
  # @return [Hash] `{ status: "queued" }` (HTTP 202).
163
163
  def redeliver(id, callback_id)
164
- http.post("/webhooks/#{id}/event_callbacks/#{callback_id}/redeliver", {})
164
+ http.post("/webhooks/#{segment(id)}/event_callbacks/#{segment(callback_id)}/redeliver", {})
165
165
  end
166
166
 
167
167
  # Delete a webhook. Returns HTTP 204.
168
168
  # @param id [String] Webhook UUID.
169
169
  # @return [nil]
170
170
  def delete(id)
171
- http.delete("/webhooks/#{id}")
171
+ http.delete("/webhooks/#{segment(id)}")
172
172
  end
173
173
  end
174
174
  end
data/lib/rail0/version.rb CHANGED
@@ -5,5 +5,5 @@ module Rail0
5
5
  # consumed by path from a sibling checkout, where the number bought nothing: no registry
6
6
  # ever served a 1.x, no tag was ever cut, and the one consumer resolves the working tree.
7
7
  # Publishing from 1.2.0 would have implied two releases nobody can install.
8
- VERSION = "1.3.0"
8
+ VERSION = "1.3.1"
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rail0-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Commerce Layer