satsgate-sdk 0.10.0__tar.gz
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.
- satsgate_sdk-0.10.0/LICENSE +201 -0
- satsgate_sdk-0.10.0/PKG-INFO +151 -0
- satsgate_sdk-0.10.0/README.md +131 -0
- satsgate_sdk-0.10.0/pyproject.toml +34 -0
- satsgate_sdk-0.10.0/setup.cfg +4 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk/__init__.py +22 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk/client.py +684 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk/receipts.py +479 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk.egg-info/PKG-INFO +151 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk.egg-info/SOURCES.txt +11 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk.egg-info/dependency_links.txt +1 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk.egg-info/requires.txt +2 -0
- satsgate_sdk-0.10.0/src/satsgate_sdk.egg-info/top_level.txt +1 -0
|
@@ -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 reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and 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 Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
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 [yyyy] [name of copyright owner]
|
|
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 implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: satsgate-sdk
|
|
3
|
+
Version: 0.10.0
|
|
4
|
+
Summary: Charge AI agents in Lightning and USDC on the same 402. 500 free unlocks per UTC month. Cobrale a agentes de IA en Lightning y USDC, en el mismo 402.
|
|
5
|
+
License-Expression: Apache-2.0
|
|
6
|
+
Project-URL: Homepage, https://github.com/Mike-io-hash/satsgate
|
|
7
|
+
Project-URL: Issues, https://github.com/Mike-io-hash/satsgate/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/Mike-io-hash/satsgate/blob/main/CHANGELOG.md
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: httpx>=0.27.0
|
|
18
|
+
Requires-Dist: pymacaroons>=0.13.0
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# satsgate-sdk (Python)
|
|
22
|
+
|
|
23
|
+
Python client for **satsgate** model B. A vendor opens an account, holds a prepaid balance in milisatoshis, and charges a live unlock from that balance. The balance is not money and is not redeemable for sats, USDC, or fiat. Works with core 0.20.3 or later.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install satsgate-sdk
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
From a checkout: `pip install -e sdk/python`.
|
|
30
|
+
|
|
31
|
+
## USDC payments: Base network only
|
|
32
|
+
|
|
33
|
+
USDC payments use Base only. Ethereum mainnet, Polygon, Solana, Arbitrum, and any other chain are not accepted.
|
|
34
|
+
|
|
35
|
+
## Install (editable, from this repo)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
# from the satsgate repo root
|
|
39
|
+
python3 -m venv .venv
|
|
40
|
+
source .venv/bin/activate
|
|
41
|
+
|
|
42
|
+
pip install -e sdk/python
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Account and top-up
|
|
46
|
+
|
|
47
|
+
`create_account(base_url=...)` calls `POST /v1/accounts` with no API key and an empty body. The response includes `api_key` (shown once), `client_id`, `balance_msat`, `balance_sats`, and `estimated_unlocks_remaining`. A 429 is raised. The client does not retry.
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from satsgate_sdk import SatsgateClient, create_account
|
|
51
|
+
|
|
52
|
+
opened = create_account(base_url="http://127.0.0.1:8000")
|
|
53
|
+
sg = SatsgateClient(base_url="http://127.0.0.1:8000", api_key=opened["api_key"])
|
|
54
|
+
print(sg.balance()["balance_msat"], sg.balance()["estimated_unlocks_remaining"])
|
|
55
|
+
|
|
56
|
+
# Lightning: minimum 1,000 sats. The 402 body includes invoice, macaroon,
|
|
57
|
+
# payment_hash, and msat_credit (principal plus any bonus the server computed).
|
|
58
|
+
invoice = sg.topup_lightning(sats=1000)
|
|
59
|
+
|
|
60
|
+
# USDC: minimum US$1. The 402 body includes PAYMENT-REQUIRED from the response header.
|
|
61
|
+
# Pass payment_signature on a later call to settle. This SDK does not sign.
|
|
62
|
+
offer = sg.topup_usdc(usd="1.00")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
`balance()` and `get_client()` return `balance_msat`, `balance_sats`, and `estimated_unlocks_remaining`, plus `free_unlocks_remaining` and `paid_unlocks_estimate` when the server sends them. `list_plans()` returns the model B document.
|
|
66
|
+
|
|
67
|
+
## Example
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from satsgate_sdk import SatsgateClient
|
|
71
|
+
|
|
72
|
+
sg = SatsgateClient(base_url="http://127.0.0.1:8000", api_key="sg_...")
|
|
73
|
+
|
|
74
|
+
# 1) Register payee (once)
|
|
75
|
+
sg.set_payee("burlybakery53@walletofsatoshi.com")
|
|
76
|
+
|
|
77
|
+
# One-time browser link for a USDC payee. The URL is not logged.
|
|
78
|
+
# link = sg.create_connect_link()
|
|
79
|
+
|
|
80
|
+
# 2) Create a challenge for your resource
|
|
81
|
+
ch = sg.paywall_challenge(resource="demo/test", amount_sats=10, memo="my service")
|
|
82
|
+
|
|
83
|
+
# Your backend should respond to your end user with HTTP 402 + header:
|
|
84
|
+
# WWW-Authenticate: ch.www_authenticate
|
|
85
|
+
# and optionally JSON containing ch.invoice and ch.macaroon.
|
|
86
|
+
|
|
87
|
+
# 3) When your end user retries with Authorization: L402 ...
|
|
88
|
+
# expected_resource is required. The token is v2 and only verifies for the
|
|
89
|
+
# client that created the challenge.
|
|
90
|
+
res = sg.paywall_verify(
|
|
91
|
+
authorization_header="L402 <macaroon>:<preimage>",
|
|
92
|
+
expected_resource="demo/test",
|
|
93
|
+
)
|
|
94
|
+
print(res)
|
|
95
|
+
|
|
96
|
+
# Or, when the payee's wallet advertised LUD-21 verify: the payer pays `ch.invoice`
|
|
97
|
+
# from any wallet and you poll without a preimage.
|
|
98
|
+
# settled = sg.poll_paywall(
|
|
99
|
+
# macaroon=ch.macaroon,
|
|
100
|
+
# payment_hash=ch.payment_hash,
|
|
101
|
+
# expected_resource="demo/test",
|
|
102
|
+
# )
|
|
103
|
+
|
|
104
|
+
# 4) Reporting
|
|
105
|
+
print(sg.usage_forecast(lookback_hours=24, buffer_days=7, trigger_hours=24))
|
|
106
|
+
print(sg.usage_daily(days=30))
|
|
107
|
+
print(sg.ledger(limit=50))
|
|
108
|
+
|
|
109
|
+
# 5) Restricted key for a proxy or MCP server (account key only).
|
|
110
|
+
# The secret is in created["api_key"] and is not shown again.
|
|
111
|
+
# created = sg.create_api_key(label="blog-proxy", resource_prefix="blog/")
|
|
112
|
+
# proxy = SatsgateClient(base_url="http://127.0.0.1:8000", api_key=created["api_key"])
|
|
113
|
+
# proxy.paywall_challenge(resource="blog/post", amount_sats=10)
|
|
114
|
+
# sg.revoke_api_key(created["key"]["id"])
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Buyer IP
|
|
118
|
+
|
|
119
|
+
`paywall_challenge(..., buyer_ip="203.0.113.10")` sends `X-Satsgate-Buyer-Ip`. The server applies per-buyer challenge limits and may reuse a pending invoice only when the TCP peer is a trusted proxy. `buyer_ip_accepted` on the result says whether it did. `reused` is true when the response is an invoice that buyer already has.
|
|
120
|
+
|
|
121
|
+
## Usage policy
|
|
122
|
+
|
|
123
|
+
`paywall_challenge(..., max_uses=1)` or `access_seconds=3600` records what that payment buys. Pass `consume=True` to `paywall_verify` or `poll_paywall` on every buyer request. Each call takes one use, including a retry. The server returns `uses_remaining` and `access_until`. HTTP 401 `token_used_up` or `token_expired` means create a new challenge. The SDK does not cache a challenge that has a policy.
|
|
124
|
+
|
|
125
|
+
## Restricted keys
|
|
126
|
+
|
|
127
|
+
`create_api_key`, `list_api_keys`, and `revoke_api_key` talk to `/v1/api-keys` with the account key (`sg_...`).
|
|
128
|
+
|
|
129
|
+
A restricted key starts with `sgr_`. It can call `paywall_challenge`, `paywall_verify`, `poll_paywall`, and `balance` (including `balance_msat` and `estimated_unlocks_remaining`). It cannot open an account or top up. Payee changes, key management, and other account writes return HTTP 403 `insufficient_scope`. `resource_prefix` limits the paywall calls to resources that start with that prefix (HTTP 403 `resource_not_allowed`). A revoked key returns HTTP 401 `api_key_revoked`.
|
|
130
|
+
|
|
131
|
+
## Receipts (0.7.0)
|
|
132
|
+
|
|
133
|
+
`satsgate_sdk.receipts` checks a hosted-gate receipt without calling Satsgate. The secret is the site `origin_secret`. `verify_receipt` checks the HS256 signature, `aud`, `sub`, `iss`, `exp`, and `uses`. `MemoryReceiptStore` and `SqliteReceiptStore` reserve a use and commit it on a 2xx. `SatsgateReceiptMiddleware` is the ASGI wrapper. `python -m satsgate_sdk.receipts verify` checks one token. Vectors shared with the Express snippet are `tests/fixtures/receipt_vectors.json`.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from satsgate_sdk.receipts import SatsgateReceiptMiddleware, SqliteReceiptStore
|
|
137
|
+
|
|
138
|
+
app = SatsgateReceiptMiddleware(
|
|
139
|
+
app,
|
|
140
|
+
site="my-shop",
|
|
141
|
+
secret=os.environ["SATSGATE_SITE_SECRET"],
|
|
142
|
+
store=SqliteReceiptStore("/data/receipts.sqlite3"),
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The demo seller is `sdk/python/examples/receipt_seller.py`. `x402-test` is mounted at `/live` and `demo-test` at `/test`. Each accepts only its own receipts.
|
|
147
|
+
|
|
148
|
+
## Cache
|
|
149
|
+
|
|
150
|
+
`paywall_verify(..., use_cache=True)` remembers a successful `payment_hash` until `cache_until`.
|
|
151
|
+
Older servers only return `valid_until`, and the SDK falls back to that. The cache is there so a busy route does not call satsgate on every request. It is not how long the buyer may use your product. `valid_until` is the macaroon expiry caveat. `claim_until` is the latest time a first unlock is accepted.
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# satsgate-sdk (Python)
|
|
2
|
+
|
|
3
|
+
Python client for **satsgate** model B. A vendor opens an account, holds a prepaid balance in milisatoshis, and charges a live unlock from that balance. The balance is not money and is not redeemable for sats, USDC, or fiat. Works with core 0.20.3 or later.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pip install satsgate-sdk
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
From a checkout: `pip install -e sdk/python`.
|
|
10
|
+
|
|
11
|
+
## USDC payments: Base network only
|
|
12
|
+
|
|
13
|
+
USDC payments use Base only. Ethereum mainnet, Polygon, Solana, Arbitrum, and any other chain are not accepted.
|
|
14
|
+
|
|
15
|
+
## Install (editable, from this repo)
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# from the satsgate repo root
|
|
19
|
+
python3 -m venv .venv
|
|
20
|
+
source .venv/bin/activate
|
|
21
|
+
|
|
22
|
+
pip install -e sdk/python
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Account and top-up
|
|
26
|
+
|
|
27
|
+
`create_account(base_url=...)` calls `POST /v1/accounts` with no API key and an empty body. The response includes `api_key` (shown once), `client_id`, `balance_msat`, `balance_sats`, and `estimated_unlocks_remaining`. A 429 is raised. The client does not retry.
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from satsgate_sdk import SatsgateClient, create_account
|
|
31
|
+
|
|
32
|
+
opened = create_account(base_url="http://127.0.0.1:8000")
|
|
33
|
+
sg = SatsgateClient(base_url="http://127.0.0.1:8000", api_key=opened["api_key"])
|
|
34
|
+
print(sg.balance()["balance_msat"], sg.balance()["estimated_unlocks_remaining"])
|
|
35
|
+
|
|
36
|
+
# Lightning: minimum 1,000 sats. The 402 body includes invoice, macaroon,
|
|
37
|
+
# payment_hash, and msat_credit (principal plus any bonus the server computed).
|
|
38
|
+
invoice = sg.topup_lightning(sats=1000)
|
|
39
|
+
|
|
40
|
+
# USDC: minimum US$1. The 402 body includes PAYMENT-REQUIRED from the response header.
|
|
41
|
+
# Pass payment_signature on a later call to settle. This SDK does not sign.
|
|
42
|
+
offer = sg.topup_usdc(usd="1.00")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`balance()` and `get_client()` return `balance_msat`, `balance_sats`, and `estimated_unlocks_remaining`, plus `free_unlocks_remaining` and `paid_unlocks_estimate` when the server sends them. `list_plans()` returns the model B document.
|
|
46
|
+
|
|
47
|
+
## Example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from satsgate_sdk import SatsgateClient
|
|
51
|
+
|
|
52
|
+
sg = SatsgateClient(base_url="http://127.0.0.1:8000", api_key="sg_...")
|
|
53
|
+
|
|
54
|
+
# 1) Register payee (once)
|
|
55
|
+
sg.set_payee("burlybakery53@walletofsatoshi.com")
|
|
56
|
+
|
|
57
|
+
# One-time browser link for a USDC payee. The URL is not logged.
|
|
58
|
+
# link = sg.create_connect_link()
|
|
59
|
+
|
|
60
|
+
# 2) Create a challenge for your resource
|
|
61
|
+
ch = sg.paywall_challenge(resource="demo/test", amount_sats=10, memo="my service")
|
|
62
|
+
|
|
63
|
+
# Your backend should respond to your end user with HTTP 402 + header:
|
|
64
|
+
# WWW-Authenticate: ch.www_authenticate
|
|
65
|
+
# and optionally JSON containing ch.invoice and ch.macaroon.
|
|
66
|
+
|
|
67
|
+
# 3) When your end user retries with Authorization: L402 ...
|
|
68
|
+
# expected_resource is required. The token is v2 and only verifies for the
|
|
69
|
+
# client that created the challenge.
|
|
70
|
+
res = sg.paywall_verify(
|
|
71
|
+
authorization_header="L402 <macaroon>:<preimage>",
|
|
72
|
+
expected_resource="demo/test",
|
|
73
|
+
)
|
|
74
|
+
print(res)
|
|
75
|
+
|
|
76
|
+
# Or, when the payee's wallet advertised LUD-21 verify: the payer pays `ch.invoice`
|
|
77
|
+
# from any wallet and you poll without a preimage.
|
|
78
|
+
# settled = sg.poll_paywall(
|
|
79
|
+
# macaroon=ch.macaroon,
|
|
80
|
+
# payment_hash=ch.payment_hash,
|
|
81
|
+
# expected_resource="demo/test",
|
|
82
|
+
# )
|
|
83
|
+
|
|
84
|
+
# 4) Reporting
|
|
85
|
+
print(sg.usage_forecast(lookback_hours=24, buffer_days=7, trigger_hours=24))
|
|
86
|
+
print(sg.usage_daily(days=30))
|
|
87
|
+
print(sg.ledger(limit=50))
|
|
88
|
+
|
|
89
|
+
# 5) Restricted key for a proxy or MCP server (account key only).
|
|
90
|
+
# The secret is in created["api_key"] and is not shown again.
|
|
91
|
+
# created = sg.create_api_key(label="blog-proxy", resource_prefix="blog/")
|
|
92
|
+
# proxy = SatsgateClient(base_url="http://127.0.0.1:8000", api_key=created["api_key"])
|
|
93
|
+
# proxy.paywall_challenge(resource="blog/post", amount_sats=10)
|
|
94
|
+
# sg.revoke_api_key(created["key"]["id"])
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Buyer IP
|
|
98
|
+
|
|
99
|
+
`paywall_challenge(..., buyer_ip="203.0.113.10")` sends `X-Satsgate-Buyer-Ip`. The server applies per-buyer challenge limits and may reuse a pending invoice only when the TCP peer is a trusted proxy. `buyer_ip_accepted` on the result says whether it did. `reused` is true when the response is an invoice that buyer already has.
|
|
100
|
+
|
|
101
|
+
## Usage policy
|
|
102
|
+
|
|
103
|
+
`paywall_challenge(..., max_uses=1)` or `access_seconds=3600` records what that payment buys. Pass `consume=True` to `paywall_verify` or `poll_paywall` on every buyer request. Each call takes one use, including a retry. The server returns `uses_remaining` and `access_until`. HTTP 401 `token_used_up` or `token_expired` means create a new challenge. The SDK does not cache a challenge that has a policy.
|
|
104
|
+
|
|
105
|
+
## Restricted keys
|
|
106
|
+
|
|
107
|
+
`create_api_key`, `list_api_keys`, and `revoke_api_key` talk to `/v1/api-keys` with the account key (`sg_...`).
|
|
108
|
+
|
|
109
|
+
A restricted key starts with `sgr_`. It can call `paywall_challenge`, `paywall_verify`, `poll_paywall`, and `balance` (including `balance_msat` and `estimated_unlocks_remaining`). It cannot open an account or top up. Payee changes, key management, and other account writes return HTTP 403 `insufficient_scope`. `resource_prefix` limits the paywall calls to resources that start with that prefix (HTTP 403 `resource_not_allowed`). A revoked key returns HTTP 401 `api_key_revoked`.
|
|
110
|
+
|
|
111
|
+
## Receipts (0.7.0)
|
|
112
|
+
|
|
113
|
+
`satsgate_sdk.receipts` checks a hosted-gate receipt without calling Satsgate. The secret is the site `origin_secret`. `verify_receipt` checks the HS256 signature, `aud`, `sub`, `iss`, `exp`, and `uses`. `MemoryReceiptStore` and `SqliteReceiptStore` reserve a use and commit it on a 2xx. `SatsgateReceiptMiddleware` is the ASGI wrapper. `python -m satsgate_sdk.receipts verify` checks one token. Vectors shared with the Express snippet are `tests/fixtures/receipt_vectors.json`.
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
from satsgate_sdk.receipts import SatsgateReceiptMiddleware, SqliteReceiptStore
|
|
117
|
+
|
|
118
|
+
app = SatsgateReceiptMiddleware(
|
|
119
|
+
app,
|
|
120
|
+
site="my-shop",
|
|
121
|
+
secret=os.environ["SATSGATE_SITE_SECRET"],
|
|
122
|
+
store=SqliteReceiptStore("/data/receipts.sqlite3"),
|
|
123
|
+
)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
The demo seller is `sdk/python/examples/receipt_seller.py`. `x402-test` is mounted at `/live` and `demo-test` at `/test`. Each accepts only its own receipts.
|
|
127
|
+
|
|
128
|
+
## Cache
|
|
129
|
+
|
|
130
|
+
`paywall_verify(..., use_cache=True)` remembers a successful `payment_hash` until `cache_until`.
|
|
131
|
+
Older servers only return `valid_until`, and the SDK falls back to that. The cache is there so a busy route does not call satsgate on every request. It is not how long the buyer may use your product. `valid_until` is the macaroon expiry caveat. `claim_until` is the latest time a first unlock is accepted.
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "satsgate-sdk"
|
|
3
|
+
version = "0.10.0"
|
|
4
|
+
description = "Charge AI agents in Lightning and USDC on the same 402. 500 free unlocks per UTC month. Cobrale a agentes de IA en Lightning y USDC, en el mismo 402."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
classifiers = [
|
|
10
|
+
"Development Status :: 4 - Beta",
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Programming Language :: Python :: 3.10",
|
|
13
|
+
"Programming Language :: Python :: 3.11",
|
|
14
|
+
"Programming Language :: Python :: 3.12",
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"httpx>=0.27.0",
|
|
18
|
+
"pymacaroons>=0.13.0",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/Mike-io-hash/satsgate"
|
|
23
|
+
Issues = "https://github.com/Mike-io-hash/satsgate/issues"
|
|
24
|
+
Changelog = "https://github.com/Mike-io-hash/satsgate/blob/main/CHANGELOG.md"
|
|
25
|
+
|
|
26
|
+
[build-system]
|
|
27
|
+
requires = ["setuptools>=77", "wheel"]
|
|
28
|
+
build-backend = "setuptools.build_meta"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools]
|
|
31
|
+
package-dir = {"" = "src"}
|
|
32
|
+
|
|
33
|
+
[tool.setuptools.packages.find]
|
|
34
|
+
where = ["src"]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""satsgate-sdk
|
|
2
|
+
|
|
3
|
+
Python client for the satsgate API under model B.
|
|
4
|
+
|
|
5
|
+
- ``create_account`` opens an account. No API key is sent. The ``sg_`` key is returned once.
|
|
6
|
+
- Top up a free amount with ``topup_lightning`` or ``topup_usdc``. The prepaid balance
|
|
7
|
+
is milisatoshis (``balance_msat``). It is not redeemable for sats, USDC, or fiat.
|
|
8
|
+
- A restricted ``sgr_`` key can read the balance. It cannot open an account or top up.
|
|
9
|
+
- The buyer payment still happens on Lightning or USDC. This SDK does not sign and
|
|
10
|
+
does not ask for a seed or a private key.
|
|
11
|
+
- ``SatsgateClient.create_connect_link`` mints a one-time browser link. The URL is not logged.
|
|
12
|
+
- The customer backend submits ``Authorization: L402 ...`` and satsgate verifies it.
|
|
13
|
+
- ``max_uses`` / ``access_seconds`` on a challenge, with ``consume=True``, say what one payment buys.
|
|
14
|
+
- ``paywall_challenge(..., buyer_ip=)`` sends the end buyer's address. The server
|
|
15
|
+
uses it only when the TCP peer is a trusted proxy.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
from .client import SatsgateClient, SatsgateError, create_account
|
|
19
|
+
|
|
20
|
+
__version__ = "0.10.0"
|
|
21
|
+
|
|
22
|
+
__all__ = ["SatsgateClient", "SatsgateError", "create_account"]
|