blockchain0x-x402 0.0.1.alpha.0
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 +7 -0
- data/LICENSE +201 -0
- data/README.md +186 -0
- data/lib/blockchain0x_x402/client.rb +179 -0
- data/lib/blockchain0x_x402/errors.rb +42 -0
- data/lib/blockchain0x_x402/server.rb +221 -0
- data/lib/blockchain0x_x402/version.rb +9 -0
- data/lib/blockchain0x_x402/wire.rb +255 -0
- data/lib/blockchain0x_x402.rb +38 -0
- metadata +92 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: be9ad30aa23e7e4d44c0dd712254925bb187f4f0c78567aec39c7ea3d4f98587
|
|
4
|
+
data.tar.gz: 22a5ab6a9635274a08a72dfe3f6fd0f06ecadbcdaf0b1ef05b849673fe5355df
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 35deca80a3287f50c577ac6935a83be33d39268af3fc008972c3d55154d90041a2e0bcca4082689b13b6e0c1f42e0c8d63c4f5aaba9895afe1ce2fe8fd6694b0
|
|
7
|
+
data.tar.gz: 385941f182db7f338f0cd26b9a63ab8c90a368535f2323ee869cfc1b7d39acfdaed09e08a2cef83fa95c0712403274b15f7f6cff3e083ddd8acce7bcaa3a6592
|
data/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing the Work or
|
|
166
|
+
Derivative Works thereof, You may choose to offer, and charge a
|
|
167
|
+
fee for, acceptance of support, warranty, indemnity, or other
|
|
168
|
+
liability obligations and/or rights consistent with this License.
|
|
169
|
+
However, in accepting such obligations, You may act only on Your
|
|
170
|
+
own behalf and on Your sole responsibility, not on behalf of any
|
|
171
|
+
other Contributor, and only if You agree to indemnify, defend,
|
|
172
|
+
and hold each Contributor harmless for any liability incurred by,
|
|
173
|
+
or claims asserted against, such Contributor by reason of your
|
|
174
|
+
accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Tosh Labs
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# blockchain0x-x402 (Ruby)
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/blockchain0x-x402)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
[](#install)
|
|
6
|
+
|
|
7
|
+
\*\*Official Ruby port of [`@blockchain0x/x402`](https://www.npmjs.com/package/@blockchain0x/x402)
|
|
8
|
+
|
|
9
|
+
- `blockchain0x-x402` (Python) + `blockchain0x-x402-go`.\*\* Ships the
|
|
10
|
+
wire primitives + a 402-aware HTTP client. Sibling gem to
|
|
11
|
+
`blockchain0x`; install only when your service either issues
|
|
12
|
+
x402-aware HTTP calls (a payer) or verifies inbound x402 payments
|
|
13
|
+
(a recipient).
|
|
14
|
+
|
|
15
|
+
> Pre-release: `0.0.1.alpha.0` ships the wire primitives + the
|
|
16
|
+
> 402-aware `Client`. Rack middleware + Sinatra / Rails server
|
|
17
|
+
> adapters land in a follow-up row.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
gem install blockchain0x-x402 --pre
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Or in a `Gemfile`:
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
gem 'blockchain0x-x402', '~> 0.0.1.alpha'
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
For the payer path you also need the main SDK:
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
gem 'blockchain0x', '~> 0.0.1.alpha'
|
|
35
|
+
gem 'blockchain0x-x402', '~> 0.0.1.alpha'
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
For the recipient/verifier path you only need `blockchain0x-x402`.
|
|
39
|
+
|
|
40
|
+
## Verify an inbound x402 payment (recipient)
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
class WebhooksController < ApplicationController
|
|
44
|
+
skip_before_action :verify_authenticity_token, only: :receive
|
|
45
|
+
|
|
46
|
+
def receive
|
|
47
|
+
payment = Blockchain0xX402::Wire.parse_payment_header(
|
|
48
|
+
request.headers['X-Payment'],
|
|
49
|
+
)
|
|
50
|
+
# payment.payment_request_id, payment.tx_hash, payment.network ...
|
|
51
|
+
rescue Blockchain0xX402::WireError => e
|
|
52
|
+
render json: { code: e.code }, status: :bad_request
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The verifier:
|
|
58
|
+
|
|
59
|
+
- Accepts only `exact-usdc:<base64>` scheme; anything else rejects
|
|
60
|
+
with `header.unknown_scheme`.
|
|
61
|
+
- Validates `txHash`, `payerAddress`, `amountUsdc`, and `network`
|
|
62
|
+
shape; any drift rejects with `header.payload_malformed`.
|
|
63
|
+
- Lowercases hex fields so downstream comparisons against on-chain
|
|
64
|
+
transaction logs are deterministic.
|
|
65
|
+
|
|
66
|
+
## Issue x402-aware HTTP calls (payer)
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
require 'blockchain0x'
|
|
70
|
+
require 'blockchain0x_x402'
|
|
71
|
+
|
|
72
|
+
# Adapter glue: the x402 Client takes any object with .network,
|
|
73
|
+
# .payments_create(args), and .transactions_get(id). Wire the
|
|
74
|
+
# main SDK at your boundary.
|
|
75
|
+
sdk_adapter = Struct.new(:sdk) do
|
|
76
|
+
def network = sdk.network
|
|
77
|
+
def payments_create(agent_id:, to:, amount_wei:)
|
|
78
|
+
sdk.payments.create(agent_id:, to:, amount_wei:)
|
|
79
|
+
end
|
|
80
|
+
def transactions_get(id) = sdk.transactions.get(id)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
main = Blockchain0x::Client.new(api_key: ENV.fetch('BLOCKCHAIN0X_API_KEY'))
|
|
84
|
+
x402 = Blockchain0xX402::Client.new(sdk: sdk_adapter.new(main), agent_id: 'agt_...')
|
|
85
|
+
response = x402.post('https://service-b.com/llm-query', body: { ... })
|
|
86
|
+
raise unless response.success?
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The wrapper handles a 402 response transparently:
|
|
90
|
+
|
|
91
|
+
1. Parses the 402 body and picks the requirement matching the SDK's network.
|
|
92
|
+
2. Calls `sdk.payments_create(...)` to settle on-chain. The main SDK
|
|
93
|
+
auto-attaches an `Idempotency-Key` so a flaky retry does not
|
|
94
|
+
double-spend.
|
|
95
|
+
3. Polls `sdk.transactions_get(payment_id)` every 1s for up to 30s
|
|
96
|
+
until the transaction confirms.
|
|
97
|
+
4. Rebuilds the request with the `X-Payment` header and re-issues
|
|
98
|
+
it once. The 200 response is returned to the caller.
|
|
99
|
+
|
|
100
|
+
Failures surface as `Blockchain0xX402::ClientError` with stable codes:
|
|
101
|
+
|
|
102
|
+
- `no_matching_requirement`
|
|
103
|
+
- `settlement_timeout`
|
|
104
|
+
- `chain_failed`
|
|
105
|
+
|
|
106
|
+
## Expose a paid HTTP route (recipient, server-side)
|
|
107
|
+
|
|
108
|
+
The `RackMiddleware` adapter gates routes against a static pricing
|
|
109
|
+
table. It mounts in front of any Rack app (Sinatra, Rails, plain
|
|
110
|
+
Rack) with the standard `use` directive.
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
require 'sinatra'
|
|
114
|
+
require 'blockchain0x_x402/server'
|
|
115
|
+
|
|
116
|
+
server_sdk = ... # an object that responds to payment_requests_settle(...)
|
|
117
|
+
|
|
118
|
+
use Blockchain0xX402::Server::RackMiddleware,
|
|
119
|
+
sdk: server_sdk,
|
|
120
|
+
pricing: {
|
|
121
|
+
'POST /llm-query' => Blockchain0xX402::Server::PricingEntry.new(
|
|
122
|
+
amount_usdc: '0.10',
|
|
123
|
+
pay_to_address: '0xabc...',
|
|
124
|
+
payment_request_id: 'pr_demo',
|
|
125
|
+
),
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
post '/llm-query' do
|
|
129
|
+
payment = request.env['blockchain0x.x402_payment']
|
|
130
|
+
# payment.payment_request_id, payment.tx_hash, ...
|
|
131
|
+
json served: true
|
|
132
|
+
end
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
A miss in the pricing table is a no-op (the route is free). A hit
|
|
136
|
+
with no/invalid `X-Payment` short-circuits the response with HTTP
|
|
137
|
+
402 and the canonical `accepts[]` body. A hit with a valid payment
|
|
138
|
+
calls `sdk.payment_requests_settle(...)` to anchor trust, attaches
|
|
139
|
+
the parsed payment to `env['blockchain0x.x402_payment']`, and
|
|
140
|
+
forwards to the next middleware in the chain.
|
|
141
|
+
|
|
142
|
+
For Rails, add the same directive to `config/application.rb`:
|
|
143
|
+
|
|
144
|
+
```ruby
|
|
145
|
+
config.middleware.use Blockchain0xX402::Server::RackMiddleware,
|
|
146
|
+
sdk: ..., pricing: { ... }
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Wire-format cross-compatibility
|
|
150
|
+
|
|
151
|
+
`Blockchain0xX402::Wire.build_payment_header` produces the same
|
|
152
|
+
base64 string as the Node, Python, and Go implementations for the
|
|
153
|
+
same input. The canonical JSON shape is:
|
|
154
|
+
|
|
155
|
+
```json
|
|
156
|
+
{
|
|
157
|
+
"scheme": "exact-usdc",
|
|
158
|
+
"version": 1,
|
|
159
|
+
"paymentRequestId": "...",
|
|
160
|
+
"txHash": "...",
|
|
161
|
+
"payerAddress": "...",
|
|
162
|
+
"amountUsdc": "...",
|
|
163
|
+
"network": "..."
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Keys are emitted in this exact order. Hex fields (`txHash`,
|
|
168
|
+
`payerAddress`) are lowercased before encoding.
|
|
169
|
+
|
|
170
|
+
## Failure-mode codes
|
|
171
|
+
|
|
172
|
+
| Code | When |
|
|
173
|
+
| -------------------------- | ---------------------------------------------------------- |
|
|
174
|
+
| `response.not_402` | A non-402 response was passed to `parse_402_response`. |
|
|
175
|
+
| `response.body_missing` | 402 response body was empty. |
|
|
176
|
+
| `response.body_malformed` | 402 body failed JSON parse or shape validation. |
|
|
177
|
+
| `header.missing` | X-Payment header was absent. |
|
|
178
|
+
| `header.malformed` | X-Payment header was not `<scheme>:<base64-payload>`. |
|
|
179
|
+
| `header.unknown_scheme` | The scheme prefix was not `exact-usdc`. |
|
|
180
|
+
| `header.payload_malformed` | The decoded payload failed JSON parse or shape validation. |
|
|
181
|
+
|
|
182
|
+
Branch on `e.code` (a Blockchain0xX402::WireError instance).
|
|
183
|
+
|
|
184
|
+
## License
|
|
185
|
+
|
|
186
|
+
Apache-2.0.
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# Sub-plan 21.3 row C-7 (Ruby x402): the 402-aware HTTP client.
|
|
2
|
+
#
|
|
3
|
+
# Flow on the 402 branch:
|
|
4
|
+
#
|
|
5
|
+
# 1. Wire.parse_402_response(resp) -> X402Response
|
|
6
|
+
# 2. Pick the requirement whose `network` matches the SDK's bound
|
|
7
|
+
# key mode (sk_test_* -> testnet, sk_live_* -> mainnet). No
|
|
8
|
+
# match -> ClientError 'no_matching_requirement'.
|
|
9
|
+
# 3. sdk.payments_create(agent_id:, to:, amount_wei:) for the
|
|
10
|
+
# chosen requirement. The SDK auto-attaches an Idempotency-
|
|
11
|
+
# Key so a flaky retry does not double-spend.
|
|
12
|
+
# 4. Poll sdk.transactions_get(payment_id) every 1s up to 30s
|
|
13
|
+
# until status == 'confirmed' AND tx_hash present.
|
|
14
|
+
# Timeout -> ClientError 'settlement_timeout'.
|
|
15
|
+
# Failure status -> ClientError 'chain_failed'.
|
|
16
|
+
# 5. Build the X-Payment header with Wire.build_payment_header
|
|
17
|
+
# and re-issue the original request. Returns the retry's
|
|
18
|
+
# Faraday::Response.
|
|
19
|
+
#
|
|
20
|
+
# Single retry only - if the second hop also returns 402 the
|
|
21
|
+
# wrapper propagates that response unchanged so the caller can
|
|
22
|
+
# decide whether to loop or surface to the user.
|
|
23
|
+
#
|
|
24
|
+
# The `sdk` is duck-typed: any object responding to
|
|
25
|
+
# `network`, `payments_create(args)`, and `transactions_get(id)`
|
|
26
|
+
# works. Tests mock the surface; production passes a
|
|
27
|
+
# Blockchain0x::Client instance with thin adapters.
|
|
28
|
+
|
|
29
|
+
# frozen_string_literal: true
|
|
30
|
+
|
|
31
|
+
require 'faraday'
|
|
32
|
+
require_relative 'errors'
|
|
33
|
+
require_relative 'wire'
|
|
34
|
+
|
|
35
|
+
module Blockchain0xX402
|
|
36
|
+
class Client
|
|
37
|
+
DEFAULT_CONFIRM_TIMEOUT_SECONDS = 30
|
|
38
|
+
DEFAULT_CONFIRM_POLL_SECONDS = 1.0
|
|
39
|
+
|
|
40
|
+
# @param sdk [#network, #payments_create, #transactions_get]
|
|
41
|
+
# @param agent_id [String] wallet that funds the on-chain payment
|
|
42
|
+
# @param confirm_timeout_seconds [Integer]
|
|
43
|
+
# @param confirm_poll_seconds [Float]
|
|
44
|
+
# @param connection [Faraday::Connection, nil] test seam
|
|
45
|
+
# @param sleep_proc [#call, nil] test seam for the poll loop
|
|
46
|
+
def initialize(
|
|
47
|
+
sdk:,
|
|
48
|
+
agent_id:,
|
|
49
|
+
confirm_timeout_seconds: DEFAULT_CONFIRM_TIMEOUT_SECONDS,
|
|
50
|
+
confirm_poll_seconds: DEFAULT_CONFIRM_POLL_SECONDS,
|
|
51
|
+
connection: nil,
|
|
52
|
+
sleep_proc: nil
|
|
53
|
+
)
|
|
54
|
+
@sdk = sdk
|
|
55
|
+
@agent_id = agent_id
|
|
56
|
+
@confirm_timeout = confirm_timeout_seconds
|
|
57
|
+
@confirm_poll = confirm_poll_seconds
|
|
58
|
+
@conn = connection || Faraday.new
|
|
59
|
+
@sleep = sleep_proc || Kernel.method(:sleep)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Perform an HTTP request that handles 402 automatically.
|
|
63
|
+
# @param method [Symbol] :get, :post, :patch, :delete
|
|
64
|
+
# @param url [String]
|
|
65
|
+
# @param body [Object, nil] body the request adapter will encode
|
|
66
|
+
# @param headers [Hash<String, String>]
|
|
67
|
+
# @return [Faraday::Response] the final response (after the retry on the 402 branch)
|
|
68
|
+
def request(method, url, body: nil, headers: {})
|
|
69
|
+
first = perform(method, url, body, headers)
|
|
70
|
+
return first unless first.status == 402
|
|
71
|
+
|
|
72
|
+
spec = Wire.parse_402_response(first)
|
|
73
|
+
requirement = pick_requirement(spec.accepts)
|
|
74
|
+
payment = @sdk.payments_create(
|
|
75
|
+
agent_id: @agent_id,
|
|
76
|
+
to: requirement.pay_to_address,
|
|
77
|
+
amount_wei: requirement.amount_wei_usdc,
|
|
78
|
+
)
|
|
79
|
+
payment_id = payment.is_a?(Hash) ? (payment['id'] || payment[:id]) : payment.id
|
|
80
|
+
raise ClientError.new('chain_failed', 'payments_create did not return an id.') if payment_id.nil?
|
|
81
|
+
|
|
82
|
+
confirmed = wait_for_confirmation(payment_id)
|
|
83
|
+
|
|
84
|
+
header = Wire.build_payment_header(
|
|
85
|
+
Wire::ExactUsdcPayment.new(
|
|
86
|
+
scheme: 'exact-usdc',
|
|
87
|
+
version: 1,
|
|
88
|
+
payment_request_id: requirement.payment_request_id,
|
|
89
|
+
tx_hash: tx_field(confirmed, :tx_hash, 'txHash').to_s,
|
|
90
|
+
payer_address: tx_field(confirmed, :from_address, 'fromAddress').to_s,
|
|
91
|
+
amount_usdc: wei_to_usdc(requirement.amount_wei_usdc),
|
|
92
|
+
network: requirement.network,
|
|
93
|
+
),
|
|
94
|
+
)
|
|
95
|
+
perform(method, url, body, headers.merge('X-Payment' => header))
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Sugar: client.get / .post / .patch / .delete delegate to #request.
|
|
99
|
+
%i[get post patch delete].each do |verb|
|
|
100
|
+
define_method(verb) do |url, body: nil, headers: {}|
|
|
101
|
+
request(verb, url, body: body, headers: headers)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def perform(method, url, body, headers)
|
|
108
|
+
@conn.run_request(method, url, body, headers) do |req|
|
|
109
|
+
# Faraday adapters serialize the body; leave the existing
|
|
110
|
+
# block free for caller customisation in a future row.
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def pick_requirement(accepts)
|
|
115
|
+
target = @sdk.network.to_s
|
|
116
|
+
match = accepts.find { |r| r.network == target }
|
|
117
|
+
return match if match
|
|
118
|
+
|
|
119
|
+
raise ClientError.new(
|
|
120
|
+
'no_matching_requirement',
|
|
121
|
+
"402 accepts list has no entry for network=#{target.inspect}.",
|
|
122
|
+
)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def wait_for_confirmation(payment_id)
|
|
126
|
+
deadline = monotonic_now + @confirm_timeout
|
|
127
|
+
last = nil
|
|
128
|
+
while monotonic_now < deadline
|
|
129
|
+
tx = @sdk.transactions_get(payment_id)
|
|
130
|
+
last = tx
|
|
131
|
+
status = tx_field(tx, :status, 'status').to_s
|
|
132
|
+
tx_hash = tx_field(tx, :tx_hash, 'txHash')
|
|
133
|
+
return tx if status == 'confirmed' && !tx_hash.to_s.empty?
|
|
134
|
+
raise ClientError.new('chain_failed', 'payment status flipped to `failed`.') if status == 'failed'
|
|
135
|
+
|
|
136
|
+
@sleep.call(@confirm_poll)
|
|
137
|
+
end
|
|
138
|
+
raise ClientError.new(
|
|
139
|
+
'settlement_timeout',
|
|
140
|
+
"payment #{payment_id} did not confirm within #{@confirm_timeout}s " \
|
|
141
|
+
"(last status: #{tx_field(last, :status, 'status').inspect}).",
|
|
142
|
+
)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def monotonic_now
|
|
146
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def tx_field(tx, snake, camel)
|
|
150
|
+
return nil if tx.nil?
|
|
151
|
+
|
|
152
|
+
if tx.is_a?(Hash)
|
|
153
|
+
tx[snake] || tx[snake.to_s] || tx[camel]
|
|
154
|
+
elsif tx.respond_to?(snake)
|
|
155
|
+
tx.public_send(snake)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# USDC has 6 decimals on Base. Convert the wire's amount-wei
|
|
160
|
+
# integer string to the human-readable decimal form (e.g.
|
|
161
|
+
# "100000" -> "0.1"). Uses pure string arithmetic so no
|
|
162
|
+
# digits are lost via float conversion.
|
|
163
|
+
def wei_to_usdc(amount_wei)
|
|
164
|
+
raise ClientError.new('chain_failed', "amount_wei is not numeric: #{amount_wei.inspect}") unless amount_wei.match?(/\A[0-9]+\z/)
|
|
165
|
+
|
|
166
|
+
if amount_wei.length <= 6
|
|
167
|
+
padded = amount_wei.rjust(6, '0')
|
|
168
|
+
frac = padded.sub(/0+\z/, '')
|
|
169
|
+
return '0' if frac.empty?
|
|
170
|
+
|
|
171
|
+
"0.#{frac}"
|
|
172
|
+
else
|
|
173
|
+
whole = amount_wei[0...(amount_wei.length - 6)]
|
|
174
|
+
frac = amount_wei[(amount_wei.length - 6)..].sub(/0+\z/, '')
|
|
175
|
+
frac.empty? ? whole : "#{whole}.#{frac}"
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Sub-plan 21.3 C-7 (Ruby x402): error hierarchy.
|
|
2
|
+
#
|
|
3
|
+
# Blockchain0xX402::Error (base)
|
|
4
|
+
# Blockchain0xX402::WireError (wire-format parse / build failures)
|
|
5
|
+
# Blockchain0xX402::ClientError (402-flow runtime failures)
|
|
6
|
+
#
|
|
7
|
+
# Each carries a stable `code` string matching the Node + Python +
|
|
8
|
+
# Go ports so cross-language consumers branch identically.
|
|
9
|
+
|
|
10
|
+
# frozen_string_literal: true
|
|
11
|
+
|
|
12
|
+
module Blockchain0xX402
|
|
13
|
+
class Error < StandardError
|
|
14
|
+
attr_reader :code
|
|
15
|
+
|
|
16
|
+
def initialize(code, message)
|
|
17
|
+
super("#{code}: #{message}")
|
|
18
|
+
@code = code
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Raised on any malformed x402 wire input. Codes:
|
|
23
|
+
#
|
|
24
|
+
# response.not_402
|
|
25
|
+
# response.body_missing
|
|
26
|
+
# response.body_malformed
|
|
27
|
+
# header.missing
|
|
28
|
+
# header.malformed
|
|
29
|
+
# header.unknown_scheme
|
|
30
|
+
# header.payload_malformed
|
|
31
|
+
class WireError < Error
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Raised when the 402-aware client wrapper cannot resolve a
|
|
35
|
+
# challenge. Codes:
|
|
36
|
+
#
|
|
37
|
+
# no_matching_requirement - no accepts entry matched the SDK network
|
|
38
|
+
# settlement_timeout - on-chain payment did not confirm in budget
|
|
39
|
+
# chain_failed - payment status flipped to failed
|
|
40
|
+
class ClientError < Error
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
# x402 Rack middleware (sub-plan 21.3 row C-7 follow-up).
|
|
2
|
+
#
|
|
3
|
+
# Drop in front of any Rack app (Sinatra, Rails, plain Rack) to gate
|
|
4
|
+
# routes against a configured pricing table. Same shape as the
|
|
5
|
+
# Fastify / Express / Starlette / FastAPI / Flask / net-http
|
|
6
|
+
# adapters; the wire format is byte-equivalent so a Ruby server
|
|
7
|
+
# accepts headers from any cross-language payer.
|
|
8
|
+
#
|
|
9
|
+
# Usage (Sinatra / plain Rack):
|
|
10
|
+
#
|
|
11
|
+
# use Blockchain0xX402::Server::RackMiddleware,
|
|
12
|
+
# sdk: server_sdk,
|
|
13
|
+
# pricing: {
|
|
14
|
+
# 'POST /llm-query' => Blockchain0xX402::Server::PricingEntry.new(
|
|
15
|
+
# amount_usdc: '0.10',
|
|
16
|
+
# pay_to_address: '0xabc...',
|
|
17
|
+
# payment_request_id: 'pr_demo',
|
|
18
|
+
# ),
|
|
19
|
+
# }
|
|
20
|
+
#
|
|
21
|
+
# Usage (Rails):
|
|
22
|
+
#
|
|
23
|
+
# # config/application.rb
|
|
24
|
+
# config.middleware.use Blockchain0xX402::Server::RackMiddleware,
|
|
25
|
+
# sdk: server_sdk,
|
|
26
|
+
# pricing: { ... }
|
|
27
|
+
#
|
|
28
|
+
# A miss in the pricing table is a no-op (the route is free). A hit
|
|
29
|
+
# with no/invalid `X-Payment` short-circuits the response with
|
|
30
|
+
# HTTP 402 and the canonical accepts[] body. A hit with a valid
|
|
31
|
+
# payment calls `sdk.payment_requests_settle(...)` to anchor trust,
|
|
32
|
+
# stashes the parsed payment under `env['blockchain0x.x402_payment']`
|
|
33
|
+
# for downstream handlers, and forwards to the next middleware.
|
|
34
|
+
|
|
35
|
+
# frozen_string_literal: true
|
|
36
|
+
|
|
37
|
+
require 'json'
|
|
38
|
+
|
|
39
|
+
require_relative 'errors'
|
|
40
|
+
require_relative 'wire'
|
|
41
|
+
|
|
42
|
+
module Blockchain0xX402
|
|
43
|
+
module Server
|
|
44
|
+
PricingEntry = Struct.new(
|
|
45
|
+
:amount_usdc,
|
|
46
|
+
:pay_to_address,
|
|
47
|
+
:payment_request_id,
|
|
48
|
+
:network,
|
|
49
|
+
keyword_init: true,
|
|
50
|
+
) do
|
|
51
|
+
# Optional `network` defaults to nil so the middleware can fall
|
|
52
|
+
# back to the global DefaultNetwork. Struct's keyword_init
|
|
53
|
+
# already nils unspecified fields; this initialiser only
|
|
54
|
+
# exists to enforce the three required ones.
|
|
55
|
+
def initialize(amount_usdc:, pay_to_address:, payment_request_id:, network: nil)
|
|
56
|
+
super
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
CAIP2_BY_NETWORK = {
|
|
61
|
+
'mainnet' => 'eip155:8453',
|
|
62
|
+
'testnet' => 'eip155:84532',
|
|
63
|
+
}.freeze
|
|
64
|
+
|
|
65
|
+
PAYMENT_ENV_KEY = 'blockchain0x.x402_payment'
|
|
66
|
+
|
|
67
|
+
# Convert a human decimal USDC amount ("0.10") to a 6-decimal
|
|
68
|
+
# wei integer string ("100000"). Mirrors the equivalent helper
|
|
69
|
+
# in @blockchain0x/x402's shared.ts.
|
|
70
|
+
def self.usdc_decimal_to_wei(decimal)
|
|
71
|
+
whole, frac = decimal.split('.', 2)
|
|
72
|
+
whole = '0' if whole.nil? || whole.empty?
|
|
73
|
+
frac ||= ''
|
|
74
|
+
frac = frac.ljust(6, '0')[0, 6]
|
|
75
|
+
(whole.to_i * 1_000_000 + frac.to_i).to_s
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Build a PaymentRequirement struct from a pricing entry +
|
|
79
|
+
# network fallback. Exported so alternative adapters (Rails
|
|
80
|
+
# ActionController::API, Grape) can reuse it.
|
|
81
|
+
def self.build_requirement(entry, default_network)
|
|
82
|
+
network = entry.network || default_network || 'mainnet'
|
|
83
|
+
Wire::PaymentRequirement.new(
|
|
84
|
+
scheme: 'exact-usdc',
|
|
85
|
+
network: network,
|
|
86
|
+
chain_id: CAIP2_BY_NETWORK.fetch(network),
|
|
87
|
+
pay_to_address: entry.pay_to_address,
|
|
88
|
+
amount_wei_usdc: usdc_decimal_to_wei(entry.amount_usdc),
|
|
89
|
+
payment_request_id: entry.payment_request_id,
|
|
90
|
+
max_age_seconds: nil,
|
|
91
|
+
)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Render the canonical 402 body. Used internally by the
|
|
95
|
+
# middleware; exported for alternative adapters.
|
|
96
|
+
def self.build_402_body(entry:, resource:, default_network:, max_age_seconds:)
|
|
97
|
+
req = build_requirement(entry, default_network)
|
|
98
|
+
req.max_age_seconds = max_age_seconds
|
|
99
|
+
Wire::X402Response.new(version: 1, resource: resource, accepts: [req])
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def self.x402_response_to_hash(resp)
|
|
103
|
+
{
|
|
104
|
+
'version' => resp.version,
|
|
105
|
+
'resource' => resp.resource,
|
|
106
|
+
'accepts' => resp.accepts.map do |r|
|
|
107
|
+
h = {
|
|
108
|
+
'scheme' => r.scheme,
|
|
109
|
+
'network' => r.network,
|
|
110
|
+
'chainId' => r.chain_id,
|
|
111
|
+
'payToAddress' => r.pay_to_address,
|
|
112
|
+
'amountWeiUsdc' => r.amount_wei_usdc,
|
|
113
|
+
'paymentRequestId' => r.payment_request_id,
|
|
114
|
+
}
|
|
115
|
+
h['maxAgeSeconds'] = r.max_age_seconds unless r.max_age_seconds.nil?
|
|
116
|
+
h
|
|
117
|
+
end,
|
|
118
|
+
}
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# Typed verify result. Returns one of:
|
|
122
|
+
# { ok: true, payment: ExactUsdcPayment }
|
|
123
|
+
# { ok: false, reason: 'header_missing' | 'header_malformed' |
|
|
124
|
+
# 'requirement_mismatch' | 'settle_rejected', message: String }
|
|
125
|
+
VerifyOk = Struct.new(:payment) do
|
|
126
|
+
def ok?; true; end
|
|
127
|
+
end
|
|
128
|
+
VerifyFailure = Struct.new(:reason, :message) do
|
|
129
|
+
def ok?; false; end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def self.verify_x_payment(sdk:, header:, entry:)
|
|
133
|
+
if header.nil? || header.empty?
|
|
134
|
+
return VerifyFailure.new('header_missing', 'X-Payment header is required.')
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
begin
|
|
138
|
+
payment = Wire.parse_payment_header(header)
|
|
139
|
+
rescue WireError => e
|
|
140
|
+
return VerifyFailure.new('header_malformed', e.message)
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
if payment.payment_request_id != entry.payment_request_id
|
|
144
|
+
return VerifyFailure.new(
|
|
145
|
+
'requirement_mismatch',
|
|
146
|
+
"X-Payment references #{payment.payment_request_id}, " \
|
|
147
|
+
"route quoted #{entry.payment_request_id}.",
|
|
148
|
+
)
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
begin
|
|
152
|
+
sdk.payment_requests_settle(
|
|
153
|
+
payment_request_id: payment.payment_request_id,
|
|
154
|
+
body: {
|
|
155
|
+
'txHash' => payment.tx_hash,
|
|
156
|
+
'payerAddress' => payment.payer_address,
|
|
157
|
+
'amountUsdcVerified' => payment.amount_usdc,
|
|
158
|
+
},
|
|
159
|
+
)
|
|
160
|
+
rescue StandardError => e
|
|
161
|
+
msg = e.message
|
|
162
|
+
msg = 'settle() rejected the proof.' if msg.nil? || msg.empty?
|
|
163
|
+
return VerifyFailure.new('settle_rejected', msg)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
VerifyOk.new(payment)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Rack middleware. Build at process boot, then `use` in front
|
|
170
|
+
# of the application.
|
|
171
|
+
class RackMiddleware
|
|
172
|
+
# @param app [#call] downstream Rack app
|
|
173
|
+
# @param sdk [#payment_requests_settle] backend settle bridge
|
|
174
|
+
# @param pricing [Hash{String=>PricingEntry}] keyed by '<METHOD> <PATH>'
|
|
175
|
+
# @param default_network [String] 'mainnet' (default) or 'testnet'
|
|
176
|
+
# @param max_age_seconds [Integer] how long a chain confirmation
|
|
177
|
+
# may live before the payer's wrapper re-pays
|
|
178
|
+
def initialize(app, sdk:, pricing:, default_network: 'mainnet', max_age_seconds: 60)
|
|
179
|
+
@app = app
|
|
180
|
+
@sdk = sdk
|
|
181
|
+
@pricing = pricing
|
|
182
|
+
@default_network = default_network
|
|
183
|
+
@max_age_seconds = max_age_seconds
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def call(env)
|
|
187
|
+
method = env['REQUEST_METHOD'] || 'GET'
|
|
188
|
+
path = env['PATH_INFO'] || env['REQUEST_PATH'] || ''
|
|
189
|
+
key = "#{method.upcase} #{path}"
|
|
190
|
+
entry = @pricing[key]
|
|
191
|
+
return @app.call(env) if entry.nil?
|
|
192
|
+
|
|
193
|
+
header = env['HTTP_X_PAYMENT']
|
|
194
|
+
outcome = Server.verify_x_payment(sdk: @sdk, header: header, entry: entry)
|
|
195
|
+
|
|
196
|
+
if outcome.ok?
|
|
197
|
+
env[PAYMENT_ENV_KEY] = outcome.payment
|
|
198
|
+
return @app.call(env)
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
body = Server.build_402_body(
|
|
202
|
+
entry: entry,
|
|
203
|
+
resource: "#{method} #{path}",
|
|
204
|
+
default_network: @default_network,
|
|
205
|
+
max_age_seconds: @max_age_seconds,
|
|
206
|
+
)
|
|
207
|
+
payload = Server.x402_response_to_hash(body)
|
|
208
|
+
payload['error'] = { 'reason' => outcome.reason, 'message' => outcome.message }
|
|
209
|
+
raw = JSON.generate(payload)
|
|
210
|
+
[
|
|
211
|
+
402,
|
|
212
|
+
{
|
|
213
|
+
'Content-Type' => 'application/json',
|
|
214
|
+
'Content-Length' => raw.bytesize.to_s,
|
|
215
|
+
},
|
|
216
|
+
[raw],
|
|
217
|
+
]
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# Sub-plan 21.3 row C-7 (Ruby x402). Ports the Node + Python + Go
|
|
2
|
+
# wire primitives byte-for-byte.
|
|
3
|
+
#
|
|
4
|
+
# Three exports:
|
|
5
|
+
#
|
|
6
|
+
# parse_402_response(http_response) -> X402Response
|
|
7
|
+
# build_payment_header(payment_hash_or_struct) -> "exact-usdc:<base64>"
|
|
8
|
+
# parse_payment_header(string) -> ExactUsdcPayment
|
|
9
|
+
#
|
|
10
|
+
# Plus the typed error class Blockchain0xX402::WireError with one of
|
|
11
|
+
# 7 stable codes (response.not_402, response.body_missing,
|
|
12
|
+
# response.body_malformed, header.missing, header.malformed,
|
|
13
|
+
# header.unknown_scheme, header.payload_malformed).
|
|
14
|
+
#
|
|
15
|
+
# The wire form follows Coinbase's x402 reference:
|
|
16
|
+
#
|
|
17
|
+
# X-Payment: <scheme>:<base64(payload)>
|
|
18
|
+
#
|
|
19
|
+
# Wire compatibility: the JSON output of build_payment_header is
|
|
20
|
+
# byte-equivalent with the Node, Python, and Go implementations.
|
|
21
|
+
# The canonical key order (scheme, version, paymentRequestId,
|
|
22
|
+
# txHash, payerAddress, amountUsdc, network) is emitted manually
|
|
23
|
+
# via String#<< so the wire shape does not depend on Ruby Hash
|
|
24
|
+
# insertion-order semantics.
|
|
25
|
+
|
|
26
|
+
# frozen_string_literal: true
|
|
27
|
+
|
|
28
|
+
require 'base64'
|
|
29
|
+
require 'json'
|
|
30
|
+
require_relative 'errors'
|
|
31
|
+
|
|
32
|
+
module Blockchain0xX402
|
|
33
|
+
module Wire
|
|
34
|
+
# Failure-code constants mirror the Node + Python + Go ports.
|
|
35
|
+
RESPONSE_NOT_402 = 'response.not_402'
|
|
36
|
+
RESPONSE_BODY_MISSING = 'response.body_missing'
|
|
37
|
+
RESPONSE_BODY_MALFORMED = 'response.body_malformed'
|
|
38
|
+
HEADER_MISSING = 'header.missing'
|
|
39
|
+
HEADER_MALFORMED = 'header.malformed'
|
|
40
|
+
HEADER_UNKNOWN_SCHEME = 'header.unknown_scheme'
|
|
41
|
+
HEADER_PAYLOAD_MALFORMED = 'header.payload_malformed'
|
|
42
|
+
|
|
43
|
+
VALID_NETWORKS = %w[mainnet testnet].freeze
|
|
44
|
+
RE_TX_HASH = /\A0x[0-9a-fA-F]{64}\z/
|
|
45
|
+
RE_PAYER = /\A0x[0-9a-fA-F]{40}\z/
|
|
46
|
+
RE_AMOUNT = /\A[0-9]+(?:\.[0-9]+)?\z/
|
|
47
|
+
RE_WEI = /\A[0-9]+\z/
|
|
48
|
+
|
|
49
|
+
# @!attribute [r] scheme @return [String] 'exact-usdc'
|
|
50
|
+
# @!attribute [r] network @return [String] 'mainnet' | 'testnet'
|
|
51
|
+
# @!attribute [r] chain_id @return [String] CAIP-2 chain id
|
|
52
|
+
# @!attribute [r] pay_to_address @return [String]
|
|
53
|
+
# @!attribute [r] amount_wei_usdc @return [String] integer text
|
|
54
|
+
# @!attribute [r] payment_request_id @return [String]
|
|
55
|
+
# @!attribute [r] max_age_seconds @return [Integer, nil]
|
|
56
|
+
PaymentRequirement = Struct.new(
|
|
57
|
+
:scheme,
|
|
58
|
+
:network,
|
|
59
|
+
:chain_id,
|
|
60
|
+
:pay_to_address,
|
|
61
|
+
:amount_wei_usdc,
|
|
62
|
+
:payment_request_id,
|
|
63
|
+
:max_age_seconds,
|
|
64
|
+
keyword_init: true,
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
X402Response = Struct.new(:version, :resource, :accepts, keyword_init: true)
|
|
68
|
+
|
|
69
|
+
# Decoded X-Payment header payload (scheme=exact-usdc).
|
|
70
|
+
ExactUsdcPayment = Struct.new(
|
|
71
|
+
:scheme,
|
|
72
|
+
:version,
|
|
73
|
+
:payment_request_id,
|
|
74
|
+
:tx_hash,
|
|
75
|
+
:payer_address,
|
|
76
|
+
:amount_usdc,
|
|
77
|
+
:network,
|
|
78
|
+
keyword_init: true,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
class << self
|
|
82
|
+
# @param response [#status, #body, ...] any HTTP response that
|
|
83
|
+
# exposes a numeric status and a JSON-parseable body. Faraday
|
|
84
|
+
# responses, Net::HTTPResponse via Net::HTTP, and Webmock
|
|
85
|
+
# stubs all work; for raw hashes use {parse_402_body} instead.
|
|
86
|
+
# @return [X402Response]
|
|
87
|
+
# @raise [WireError] on any shape problem
|
|
88
|
+
def parse_402_response(response)
|
|
89
|
+
status = response.respond_to?(:status) ? response.status : response.code.to_i
|
|
90
|
+
if status != 402
|
|
91
|
+
raise WireError.new(RESPONSE_NOT_402, "Expected status 402, got #{status}.")
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
body = response.body
|
|
95
|
+
body = JSON.parse(body) if body.is_a?(String)
|
|
96
|
+
parse_402_body(body)
|
|
97
|
+
rescue JSON::ParserError
|
|
98
|
+
raise WireError.new(RESPONSE_BODY_MALFORMED, '402 response body is not JSON.')
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def parse_402_body(body)
|
|
102
|
+
unless body.is_a?(Hash)
|
|
103
|
+
raise WireError.new(RESPONSE_BODY_MISSING, '402 response body is missing or non-object.')
|
|
104
|
+
end
|
|
105
|
+
unless body['version'] == 1
|
|
106
|
+
raise WireError.new(RESPONSE_BODY_MALFORMED, "Unsupported x402 version: #{body['version'].inspect}.")
|
|
107
|
+
end
|
|
108
|
+
unless body['resource'].is_a?(String) && !body['resource'].empty?
|
|
109
|
+
raise WireError.new(RESPONSE_BODY_MALFORMED, '402 body missing `resource` string.')
|
|
110
|
+
end
|
|
111
|
+
accepts_raw = body['accepts']
|
|
112
|
+
unless accepts_raw.is_a?(Array) && !accepts_raw.empty?
|
|
113
|
+
raise WireError.new(RESPONSE_BODY_MALFORMED, '402 body missing `accepts` array or empty.')
|
|
114
|
+
end
|
|
115
|
+
accepts = accepts_raw.map do |entry|
|
|
116
|
+
unless valid_requirement?(entry)
|
|
117
|
+
raise WireError.new(RESPONSE_BODY_MALFORMED, '402 `accepts` entry is not a recognised payment requirement.')
|
|
118
|
+
end
|
|
119
|
+
PaymentRequirement.new(
|
|
120
|
+
scheme: 'exact-usdc',
|
|
121
|
+
network: entry['network'],
|
|
122
|
+
chain_id: entry['chainId'],
|
|
123
|
+
pay_to_address: entry['payToAddress'],
|
|
124
|
+
amount_wei_usdc: entry['amountWeiUsdc'],
|
|
125
|
+
payment_request_id: entry['paymentRequestId'],
|
|
126
|
+
max_age_seconds: entry['maxAgeSeconds'].is_a?(Numeric) ? entry['maxAgeSeconds'].to_i : nil,
|
|
127
|
+
)
|
|
128
|
+
end
|
|
129
|
+
X402Response.new(version: 1, resource: body['resource'], accepts: accepts.freeze)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Encode a payment payload as an X-Payment header value.
|
|
133
|
+
# Accepts either an ExactUsdcPayment struct OR a Hash with
|
|
134
|
+
# the same keys (snake_case OR camelCase) for caller
|
|
135
|
+
# convenience.
|
|
136
|
+
#
|
|
137
|
+
# @raise [WireError] on unknown scheme
|
|
138
|
+
def build_payment_header(payment)
|
|
139
|
+
scheme, version, payment_request_id, tx_hash, payer_address, amount_usdc, network =
|
|
140
|
+
coerce_payment(payment)
|
|
141
|
+
|
|
142
|
+
if scheme != 'exact-usdc'
|
|
143
|
+
raise WireError.new(HEADER_UNKNOWN_SCHEME, "build_payment_header: unsupported scheme #{scheme.inspect}.")
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Pin the JSON key order explicitly. Ruby >= 1.9 Hashes are
|
|
147
|
+
# insertion-ordered but writing the JSON manually makes the
|
|
148
|
+
# wire contract obvious from this file alone.
|
|
149
|
+
json = String.new
|
|
150
|
+
json << '{"scheme":"exact-usdc","version":' << version.to_s
|
|
151
|
+
json << ',"paymentRequestId":' << JSON.generate(payment_request_id)
|
|
152
|
+
json << ',"txHash":' << JSON.generate(tx_hash.downcase)
|
|
153
|
+
json << ',"payerAddress":' << JSON.generate(payer_address.downcase)
|
|
154
|
+
json << ',"amountUsdc":' << JSON.generate(amount_usdc)
|
|
155
|
+
json << ',"network":' << JSON.generate(network)
|
|
156
|
+
json << '}'
|
|
157
|
+
|
|
158
|
+
"exact-usdc:#{Base64.strict_encode64(json)}"
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def parse_payment_header(value)
|
|
162
|
+
if !value.is_a?(String) || value.empty?
|
|
163
|
+
raise WireError.new(HEADER_MISSING, 'X-Payment header is missing or empty.')
|
|
164
|
+
end
|
|
165
|
+
sep = value.index(':')
|
|
166
|
+
if sep.nil? || sep < 1 || sep == value.length - 1
|
|
167
|
+
raise WireError.new(HEADER_MALFORMED, 'X-Payment header must be `<scheme>:<base64-payload>`.')
|
|
168
|
+
end
|
|
169
|
+
scheme = value[0...sep]
|
|
170
|
+
if scheme != 'exact-usdc'
|
|
171
|
+
raise WireError.new(HEADER_UNKNOWN_SCHEME, "Unsupported X-Payment scheme: #{scheme}.")
|
|
172
|
+
end
|
|
173
|
+
b64 = value[(sep + 1)..]
|
|
174
|
+
text = begin
|
|
175
|
+
Base64.strict_decode64(b64)
|
|
176
|
+
rescue ArgumentError
|
|
177
|
+
raise WireError.new(HEADER_PAYLOAD_MALFORMED, 'X-Payment payload is not valid base64.')
|
|
178
|
+
end
|
|
179
|
+
parsed = begin
|
|
180
|
+
JSON.parse(text)
|
|
181
|
+
rescue JSON::ParserError
|
|
182
|
+
raise WireError.new(HEADER_PAYLOAD_MALFORMED, 'X-Payment payload is not valid JSON.')
|
|
183
|
+
end
|
|
184
|
+
unless parsed.is_a?(Hash)
|
|
185
|
+
raise WireError.new(HEADER_PAYLOAD_MALFORMED, 'X-Payment payload is not an object.')
|
|
186
|
+
end
|
|
187
|
+
unless valid_payload?(parsed)
|
|
188
|
+
raise WireError.new(HEADER_PAYLOAD_MALFORMED, 'X-Payment payload failed shape validation.')
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
ExactUsdcPayment.new(
|
|
192
|
+
scheme: 'exact-usdc',
|
|
193
|
+
version: 1,
|
|
194
|
+
payment_request_id: parsed['paymentRequestId'],
|
|
195
|
+
tx_hash: parsed['txHash'].downcase,
|
|
196
|
+
payer_address: parsed['payerAddress'].downcase,
|
|
197
|
+
amount_usdc: parsed['amountUsdc'],
|
|
198
|
+
network: parsed['network'],
|
|
199
|
+
)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
private
|
|
203
|
+
|
|
204
|
+
def valid_requirement?(entry)
|
|
205
|
+
entry.is_a?(Hash) &&
|
|
206
|
+
entry['scheme'] == 'exact-usdc' &&
|
|
207
|
+
entry['network'].is_a?(String) &&
|
|
208
|
+
VALID_NETWORKS.include?(entry['network']) &&
|
|
209
|
+
entry['chainId'].is_a?(String) &&
|
|
210
|
+
entry['payToAddress'].is_a?(String) &&
|
|
211
|
+
entry['amountWeiUsdc'].is_a?(String) &&
|
|
212
|
+
RE_WEI.match?(entry['amountWeiUsdc']) &&
|
|
213
|
+
entry['paymentRequestId'].is_a?(String)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def valid_payload?(p)
|
|
217
|
+
p['scheme'] == 'exact-usdc' &&
|
|
218
|
+
p['version'] == 1 &&
|
|
219
|
+
p['paymentRequestId'].is_a?(String) &&
|
|
220
|
+
p['txHash'].is_a?(String) && RE_TX_HASH.match?(p['txHash']) &&
|
|
221
|
+
p['payerAddress'].is_a?(String) && RE_PAYER.match?(p['payerAddress']) &&
|
|
222
|
+
p['amountUsdc'].is_a?(String) && RE_AMOUNT.match?(p['amountUsdc']) &&
|
|
223
|
+
p['network'].is_a?(String) && VALID_NETWORKS.include?(p['network'])
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def coerce_payment(payment)
|
|
227
|
+
if payment.is_a?(ExactUsdcPayment)
|
|
228
|
+
[
|
|
229
|
+
payment.scheme,
|
|
230
|
+
payment.version,
|
|
231
|
+
payment.payment_request_id,
|
|
232
|
+
payment.tx_hash,
|
|
233
|
+
payment.payer_address,
|
|
234
|
+
payment.amount_usdc,
|
|
235
|
+
payment.network,
|
|
236
|
+
]
|
|
237
|
+
elsif payment.is_a?(Hash)
|
|
238
|
+
# Accept either snake_case or camelCase keys.
|
|
239
|
+
get = ->(snake, camel = nil) { payment[snake] || payment[snake.to_s] || (camel && (payment[camel] || payment[camel.to_s])) }
|
|
240
|
+
[
|
|
241
|
+
get.call(:scheme),
|
|
242
|
+
get.call(:version) || 1,
|
|
243
|
+
get.call(:payment_request_id, :paymentRequestId),
|
|
244
|
+
(get.call(:tx_hash, :txHash) || '').to_s,
|
|
245
|
+
(get.call(:payer_address, :payerAddress) || '').to_s,
|
|
246
|
+
get.call(:amount_usdc, :amountUsdc),
|
|
247
|
+
get.call(:network),
|
|
248
|
+
]
|
|
249
|
+
else
|
|
250
|
+
raise WireError.new(HEADER_PAYLOAD_MALFORMED, 'build_payment_header expects ExactUsdcPayment or Hash.')
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Sub-plan 21.3 row C-7 (Ruby x402): top-level module.
|
|
2
|
+
#
|
|
3
|
+
# Public surface:
|
|
4
|
+
# Blockchain0xX402::Wire.parse_402_response / parse_402_body /
|
|
5
|
+
# build_payment_header / parse_payment_header
|
|
6
|
+
# Blockchain0xX402::WireError
|
|
7
|
+
# Blockchain0xX402::Client (402-aware Faraday wrapper)
|
|
8
|
+
# Blockchain0xX402::ClientError
|
|
9
|
+
# Blockchain0xX402::Server::RackMiddleware (Rack / Sinatra / Rails gate)
|
|
10
|
+
# Blockchain0xX402::Server::PricingEntry
|
|
11
|
+
# Blockchain0xX402::VERSION
|
|
12
|
+
|
|
13
|
+
# frozen_string_literal: true
|
|
14
|
+
|
|
15
|
+
require_relative 'blockchain0x_x402/version'
|
|
16
|
+
require_relative 'blockchain0x_x402/errors'
|
|
17
|
+
require_relative 'blockchain0x_x402/wire'
|
|
18
|
+
# Client + Server are required lazily so verify-only consumers
|
|
19
|
+
# do not pay the Faraday / Rack load cost. Server has no runtime
|
|
20
|
+
# Rack dependency (the middleware obeys the Rack 1.x calling
|
|
21
|
+
# convention purely on the env-hash + tuple shape), so it can be
|
|
22
|
+
# required even in installs that do not have Rack installed.
|
|
23
|
+
|
|
24
|
+
module Blockchain0xX402
|
|
25
|
+
# @return [Class] Blockchain0xX402::Client (lazy-loaded)
|
|
26
|
+
def self.const_missing(name)
|
|
27
|
+
case name
|
|
28
|
+
when :Client
|
|
29
|
+
require_relative 'blockchain0x_x402/client'
|
|
30
|
+
const_get(:Client)
|
|
31
|
+
when :Server
|
|
32
|
+
require_relative 'blockchain0x_x402/server'
|
|
33
|
+
const_get(:Server)
|
|
34
|
+
else
|
|
35
|
+
super
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: blockchain0x-x402
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1.alpha.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Blockchain0x
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-05-30 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: faraday
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
- - "<"
|
|
21
|
+
- !ruby/object:Gem::Version
|
|
22
|
+
version: '3.0'
|
|
23
|
+
type: :runtime
|
|
24
|
+
prerelease: false
|
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '2.0'
|
|
30
|
+
- - "<"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.0'
|
|
33
|
+
- !ruby/object:Gem::Dependency
|
|
34
|
+
name: rspec
|
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.13'
|
|
40
|
+
type: :development
|
|
41
|
+
prerelease: false
|
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '3.13'
|
|
47
|
+
description: Sibling gem to `blockchain0x`. Verify inbound x402 payments + issue x402-aware
|
|
48
|
+
HTTP calls. Wire-format byte-equivalent with the Node, Python, and Go ports.
|
|
49
|
+
email:
|
|
50
|
+
- support@blockchain0x.com
|
|
51
|
+
executables: []
|
|
52
|
+
extensions: []
|
|
53
|
+
extra_rdoc_files: []
|
|
54
|
+
files:
|
|
55
|
+
- LICENSE
|
|
56
|
+
- README.md
|
|
57
|
+
- lib/blockchain0x_x402.rb
|
|
58
|
+
- lib/blockchain0x_x402/client.rb
|
|
59
|
+
- lib/blockchain0x_x402/errors.rb
|
|
60
|
+
- lib/blockchain0x_x402/server.rb
|
|
61
|
+
- lib/blockchain0x_x402/version.rb
|
|
62
|
+
- lib/blockchain0x_x402/wire.rb
|
|
63
|
+
homepage: https://blockchain0x.com
|
|
64
|
+
licenses:
|
|
65
|
+
- Apache-2.0
|
|
66
|
+
metadata:
|
|
67
|
+
homepage_uri: https://blockchain0x.com
|
|
68
|
+
source_code_uri: https://github.com/Tosh-Labs/blockchain0x-app/tree/dev/packages/sdk-ruby-x402
|
|
69
|
+
bug_tracker_uri: https://github.com/Tosh-Labs/blockchain0x-app/issues
|
|
70
|
+
documentation_uri: https://docs.blockchain0x.com
|
|
71
|
+
changelog_uri: https://github.com/Tosh-Labs/blockchain0x-x402-ruby/blob/main/CHANGELOG.md
|
|
72
|
+
rubygems_mfa_required: 'true'
|
|
73
|
+
post_install_message:
|
|
74
|
+
rdoc_options: []
|
|
75
|
+
require_paths:
|
|
76
|
+
- lib
|
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
78
|
+
requirements:
|
|
79
|
+
- - ">="
|
|
80
|
+
- !ruby/object:Gem::Version
|
|
81
|
+
version: '3.0'
|
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - ">="
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '0'
|
|
87
|
+
requirements: []
|
|
88
|
+
rubygems_version: 3.5.22
|
|
89
|
+
signing_key:
|
|
90
|
+
specification_version: 4
|
|
91
|
+
summary: Official Ruby port of @blockchain0x/x402 - HTTP 402 wire primitives + client
|
|
92
|
+
test_files: []
|