@phala/pay-server 0.0.0 → 0.9.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.
- package/LICENSE +202 -0
- package/README.md +213 -1
- package/THIRD_PARTY_NOTICES +25 -0
- package/dist/js/src/shared/checkout-params.d.ts +7 -0
- package/dist/js/src/shared/checkout-params.d.ts.map +1 -0
- package/dist/js/src/shared/checkout-params.js +2 -0
- package/dist/js/src/shared/checkout-params.js.map +1 -0
- package/dist/js-server/src/addresses.d.ts +76 -0
- package/dist/js-server/src/addresses.d.ts.map +1 -0
- package/dist/js-server/src/addresses.js +117 -0
- package/dist/js-server/src/addresses.js.map +1 -0
- package/dist/js-server/src/browser.d.ts +2 -0
- package/dist/js-server/src/browser.d.ts.map +1 -0
- package/dist/js-server/src/browser.js +3 -0
- package/dist/js-server/src/browser.js.map +1 -0
- package/dist/js-server/src/client.d.ts +54 -0
- package/dist/js-server/src/client.d.ts.map +1 -0
- package/dist/js-server/src/client.js +183 -0
- package/dist/js-server/src/client.js.map +1 -0
- package/dist/js-server/src/errors.d.ts +35 -0
- package/dist/js-server/src/errors.d.ts.map +1 -0
- package/dist/js-server/src/errors.js +56 -0
- package/dist/js-server/src/errors.js.map +1 -0
- package/dist/js-server/src/generated/openapi.d.ts +9482 -0
- package/dist/js-server/src/generated/openapi.d.ts.map +1 -0
- package/dist/js-server/src/generated/openapi.js +6 -0
- package/dist/js-server/src/generated/openapi.js.map +1 -0
- package/dist/js-server/src/helpers.d.ts +15 -0
- package/dist/js-server/src/helpers.d.ts.map +1 -0
- package/dist/js-server/src/helpers.js +15 -0
- package/dist/js-server/src/helpers.js.map +1 -0
- package/dist/js-server/src/index.d.ts +12 -0
- package/dist/js-server/src/index.d.ts.map +1 -0
- package/dist/js-server/src/index.js +13 -0
- package/dist/js-server/src/index.js.map +1 -0
- package/dist/js-server/src/json.d.ts +5 -0
- package/dist/js-server/src/json.d.ts.map +1 -0
- package/dist/js-server/src/json.js +114 -0
- package/dist/js-server/src/json.js.map +1 -0
- package/dist/js-server/src/ledger.d.ts +9 -0
- package/dist/js-server/src/ledger.d.ts.map +1 -0
- package/dist/js-server/src/ledger.js +83 -0
- package/dist/js-server/src/ledger.js.map +1 -0
- package/dist/js-server/src/pins.d.ts +19 -0
- package/dist/js-server/src/pins.d.ts.map +1 -0
- package/dist/js-server/src/pins.js +165 -0
- package/dist/js-server/src/pins.js.map +1 -0
- package/dist/js-server/src/resource.d.ts +14 -0
- package/dist/js-server/src/resource.d.ts.map +1 -0
- package/dist/js-server/src/resource.js +32 -0
- package/dist/js-server/src/resource.js.map +1 -0
- package/dist/js-server/src/resources.d.ts +120 -0
- package/dist/js-server/src/resources.d.ts.map +1 -0
- package/dist/js-server/src/resources.js +266 -0
- package/dist/js-server/src/resources.js.map +1 -0
- package/dist/js-server/src/runtime.d.ts +4 -0
- package/dist/js-server/src/runtime.d.ts.map +1 -0
- package/dist/js-server/src/runtime.js +48 -0
- package/dist/js-server/src/runtime.js.map +1 -0
- package/dist/js-server/src/schemas.d.ts +4 -0
- package/dist/js-server/src/schemas.d.ts.map +1 -0
- package/dist/js-server/src/schemas.js +3091 -0
- package/dist/js-server/src/schemas.js.map +1 -0
- package/dist/js-server/src/sweeps.d.ts +78 -0
- package/dist/js-server/src/sweeps.d.ts.map +1 -0
- package/dist/js-server/src/sweeps.js +117 -0
- package/dist/js-server/src/sweeps.js.map +1 -0
- package/dist/js-server/src/transport.d.ts +26 -0
- package/dist/js-server/src/transport.d.ts.map +1 -0
- package/dist/js-server/src/transport.js +248 -0
- package/dist/js-server/src/transport.js.map +1 -0
- package/dist/js-server/src/types.d.ts +111 -0
- package/dist/js-server/src/types.d.ts.map +1 -0
- package/dist/js-server/src/types.js +2 -0
- package/dist/js-server/src/types.js.map +1 -0
- package/dist/js-server/src/validation.d.ts +20 -0
- package/dist/js-server/src/validation.d.ts.map +1 -0
- package/dist/js-server/src/validation.js +43 -0
- package/dist/js-server/src/validation.js.map +1 -0
- package/dist/js-server/src/webhook.d.ts +59 -0
- package/dist/js-server/src/webhook.d.ts.map +1 -0
- package/dist/js-server/src/webhook.js +164 -0
- package/dist/js-server/src/webhook.js.map +1 -0
- package/package.json +55 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,215 @@
|
|
|
1
1
|
# @phala/pay-server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Merchant server SDK for Node.js >=20.3, Bun, and Deno. It contains the keyed `PhalaPay` client, transport, generated server types, bound and standalone webhook verification, pins, address helpers, ledger helpers, and sweep/Safe builders. It has no React dependency.
|
|
4
|
+
|
|
5
|
+
`viem` is the only runtime dependency because the offline EVM address derivation and Safe/sweep
|
|
6
|
+
builders use its ABI, hashing, and checksum primitives.
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @phala/pay-server
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Merchant server client
|
|
13
|
+
|
|
14
|
+
Import `PhalaPay` from `@phala/pay-server` on Node.js >=20.3, Bun or Deno. Server runtimes
|
|
15
|
+
must provide `fetch`, `AbortSignal.any` and WebCrypto (including `randomUUID`). The `@phala/pay` package remains keyless and browser-safe. The server entry rejects browser-like
|
|
16
|
+
environments before reading credentials; keep this package out of browser bundles.
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { PhalaPay, ApiError, SignatureVerificationError } from "@phala/pay-server";
|
|
20
|
+
import type { CheckoutParams } from "@phala/pay";
|
|
21
|
+
|
|
22
|
+
// Reads exactly PHALA_PAY_API_KEY and PHALA_PAY_PINS from process.env.
|
|
23
|
+
const pay = PhalaPay.fromEnv();
|
|
24
|
+
// Or: new PhalaPay({ apiKey, pins, timeoutMs: 15000, maxAttempts: 4,
|
|
25
|
+
// requestDeadlineMs: 60000, fetch });
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const quote = await pay.quotes.create(
|
|
29
|
+
{ client_reference_id: "team-42", amount: 2500, currency: "usd", chain_id: 11155111, asset: "pha" },
|
|
30
|
+
{ idempotencyKey: "persisted-order-42" },
|
|
31
|
+
);
|
|
32
|
+
const checkout: CheckoutParams = pay.checkoutParams(quote);
|
|
33
|
+
// Return checkout only to this order's authenticated browser; spread it into <Checkout>.
|
|
34
|
+
// Fulfill from the verified webhook, never from browser success.
|
|
35
|
+
const event = await pay.webhooks.constructEvent(originalBodyBytes, requestHeaders);
|
|
36
|
+
// Atomically commit your ledger snapshot and balance delta before acknowledging delivery.
|
|
37
|
+
await applyVerifiedEvent(event);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof SignatureVerificationError) {
|
|
40
|
+
// Return 400 for an invalid webhook, without exposing exception text.
|
|
41
|
+
} else if (error instanceof ApiError) {
|
|
42
|
+
// Use statusCode/code/requestId internally; return a generic payment error to the caller.
|
|
43
|
+
} else {
|
|
44
|
+
// Return 5xx for failures before the ledger transaction commits.
|
|
45
|
+
}
|
|
46
|
+
} finally {
|
|
47
|
+
await pay.close();
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`pins` accepts the versioned `ppay_pins_v1.…` string or a `Pins` value returned by
|
|
52
|
+
`parsePins`. `encodePins` produces its canonical encoding. Pins are recursively frozen and bind
|
|
53
|
+
the normalized API origin, account, mode, forwarder contracts, chain treasuries and public webhook
|
|
54
|
+
keys. Both test and live clients require pins. An explicit `apiBase` must match that origin;
|
|
55
|
+
HTTP is permitted only for explicitly configured test loopback origins. No trust discovery occurs.
|
|
56
|
+
`fromEnv(env, { apiBase })` reads no other variables and loads no files or dotenv configuration.
|
|
57
|
+
|
|
58
|
+
Every open quote and active deposit-address network is checked against the pinned treasury and
|
|
59
|
+
recomputed address, including list pages, actions and expanded objects. Historical objects cannot
|
|
60
|
+
be handed to checkout. `checkoutParams` requires a create/replay quote from this client with its
|
|
61
|
+
original `client_secret`; retrieval cannot manufacture a checkout secret. It rechecks pins and
|
|
62
|
+
payability and returns `{ clientSecret, expectedAddress, apiBase }`.
|
|
63
|
+
|
|
64
|
+
### Resources and types
|
|
65
|
+
|
|
66
|
+
Wire fields remain snake_case. Params and responses, including `QuoteCreateParams`, `Quote` and
|
|
67
|
+
`Deposit`, are aliases of the generated OpenAPI definitions. Response fields and future enums are
|
|
68
|
+
retained; unsafe JSON integers are rejected before rounding.
|
|
69
|
+
|
|
70
|
+
| Resource | Methods |
|
|
71
|
+
| --- | --- |
|
|
72
|
+
| `quotes` | `create`, `retrieve`, `listPage`, `list`, `update`, `cancel` |
|
|
73
|
+
| `depositAddresses` | `create`, `retrieve`, `listPage`, `list`, `update`, `rotate` |
|
|
74
|
+
| `deposits` | `retrieve`, `listPage`, `list`, `update` |
|
|
75
|
+
| `refunds` | `create`, `retrieve`, `listPage`, `list`, `update`, `cancel`, `markPaid` |
|
|
76
|
+
| `paymentSettings` | `retrieve`, `update` |
|
|
77
|
+
| `config`, `balance` | `retrieve` |
|
|
78
|
+
| `account` | `retrieve`, `pauseQuotes`, `resumeQuotes`, `rollWebhookKey` |
|
|
79
|
+
| `treasuries` | `challenge`, `create`, `retrieve`, `listPage`, `list`, `cancel`, `pause`, `resume` |
|
|
80
|
+
| `apiKeys` | `create`, `retrieve`, `listPage`, `list`, `roll`, `revoke` |
|
|
81
|
+
| `webhookEndpoints` | `create`, `retrieve`, `listPage`, `list`, `update`, `delete`, `test` |
|
|
82
|
+
| `events` | `retrieve`, `listPage`, `list`, `resend` |
|
|
83
|
+
| `sweeps`, `forwarders` | `listPage`, `list` |
|
|
84
|
+
|
|
85
|
+
Create/singleton-update/challenge use `(params, options?)`; resource updates and actions with
|
|
86
|
+
bodies use `(id, params, options?)`. Actions without bodies use `(id, options?)`. Retrieve uses
|
|
87
|
+
`(id, params?, options?)` (pass `undefined` for endpoints without query params); singleton retrieve
|
|
88
|
+
uses `(options?)`. Lists accept `(params?, options?)`. `listPage` returns `{ data, has_more, … }`;
|
|
89
|
+
`list` is an `AsyncIterable` that validates every page and rejects empty continuing pages or
|
|
90
|
+
repeated cursors. Omitting `apiKeys.create` permissions creates a secret key; supplying a list
|
|
91
|
+
creates a restricted key.
|
|
92
|
+
|
|
93
|
+
### Transport and errors
|
|
94
|
+
|
|
95
|
+
Per-call options are `{ idempotencyKey?, signal?, requestDeadlineMs?, upgradeTolerance? }`.
|
|
96
|
+
Defaults are a 15-second attempt timeout (including body reads), four total attempts, and a 60-second overall deadline
|
|
97
|
+
(including sleeps). POST requests freeze their body and one automatically generated UUID across
|
|
98
|
+
retries; persist an explicit order key to survive process restarts. Keys have a 255-character limit.
|
|
99
|
+
Retries cover network/timeouts, 429, 500/502/503/504, and only `409 idempotency_key_in_use`, with
|
|
100
|
+
bounded exponential jitter and Retry-After as a minimum. A delay that exceeds the remaining budget
|
|
101
|
+
returns the last error immediately. Replayed errors, DELETE, cancellation and redirects do not
|
|
102
|
+
retry. Redirects are refused. `close()` aborts outstanding requests without closing injected fetch.
|
|
103
|
+
|
|
104
|
+
Set `upgradeTolerance: true` on the client or per call for backend work that can wait through a
|
|
105
|
+
CVM upgrade. A maintenance 503, gateway 502/503/504 (even a non-JSON body), or network/attempt
|
|
106
|
+
timeout allows up to five minutes of retries from the original request start, with backoff capped
|
|
107
|
+
at 10 seconds. This applies only to GET and replayable POST and preserves the same body/key.
|
|
108
|
+
Explicit deadlines, cancellation, and replayed-error handling still take precedence. The option
|
|
109
|
+
defaults to false so interactive requests keep the existing timeout/attempt budget. See
|
|
110
|
+
[planned upgrades](../../docs/integration.md#planned-upgrades-and-reconnecting).
|
|
111
|
+
|
|
112
|
+
Errors inherit `PhalaPayError`: `ApiError`, `TransportError`, `ConfigurationError`,
|
|
113
|
+
`ResponseValidationError`, `AddressMismatchError`, `AttestationError`,
|
|
114
|
+
`SignatureVerificationError` (also exported as `WebhookSignatureError`) and `LedgerSnapshotError`.
|
|
115
|
+
`ApiError` exposes `statusCode`, `code`, `message`, `errorType`, `param`, `docUrl`, `requestId` and
|
|
116
|
+
`retryAfter` in seconds; absent optional fields are null. `TransportError.code` is `network`,
|
|
117
|
+
`timeout` or `cancelled`. Malformed responses expose status/request ID without raw bodies or causes.
|
|
118
|
+
|
|
119
|
+
### Bound webhooks and pure ledger helpers
|
|
120
|
+
|
|
121
|
+
`pay.webhooks.constructEvent(body, headers, { tolerance: 300 })` uses only this client's pinned
|
|
122
|
+
public keys, account and mode. Pass original bytes or exact UTF-8, before JSON middleware. Headers
|
|
123
|
+
are case-insensitive; duplicate signing headers fail. Ed25519 v1a verification, the envelope and ID
|
|
124
|
+
must pass. The inclusive time window is bilateral; tolerance zero requires an exact timestamp.
|
|
125
|
+
Known deposit events expose the typed `event.deposit`; unknown event types retain their raw object
|
|
126
|
+
and should be ignored before accessing it. Change overlapping webhook pins manually after
|
|
127
|
+
attestation; security notices never modify trust.
|
|
128
|
+
|
|
129
|
+
`depositNetAmount(deposit)` returns integer minor units. `balanceDelta(previousSnapshot, deposit)`
|
|
130
|
+
returns `{ snapshot, contribution, delta }` without IO or mutation. Commit the returned snapshot
|
|
131
|
+
and delta together in your own transaction before acknowledging a webhook. Duplicate and reordered
|
|
132
|
+
snapshots converge through monotone statuses and cumulative deductions. Refund events retain
|
|
133
|
+
`status: "credited"`; unknown statuses, identity/valuation conflicts and impossible deductions
|
|
134
|
+
raise `LedgerSnapshotError`. These helpers provide no SQL adapter or authorization policy.
|
|
135
|
+
|
|
136
|
+
## Server helpers
|
|
137
|
+
|
|
138
|
+
Keyless pins, ledger, address, signature and sweep builders live at `@phala/pay-server/helpers`, including for
|
|
139
|
+
WebCrypto-capable non-Node runtimes. Existing exports from `/server` remain available on supported server runtimes;
|
|
140
|
+
browser callers must move to `/server/helpers`:
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { constructEvent, flushTransactions, safeBatch, verifyQuoteAddress } from "@phala/pay-server/helpers";
|
|
144
|
+
|
|
145
|
+
// Your pins, configured on your server and never read from the service. The address is derived
|
|
146
|
+
// from your treasury, not the quote's: a mismatch throws AddressMismatchError, and in live mode a
|
|
147
|
+
// chain without a pinned treasury fails closed.
|
|
148
|
+
const pins = { account: "acct_…", factory: FACTORY, implementation: IMPLEMENTATION, treasuries: { 1: TREASURY } };
|
|
149
|
+
const expectedAddress = verifyQuoteAddress(pins, quote); // pass it to <Checkout expectedAddress>
|
|
150
|
+
|
|
151
|
+
// Standard Webhooks v1a (ed25519, WebCrypto: Node 20.3+, Deno, Bun, edge runtimes). Fails closed
|
|
152
|
+
// unless the signature verifies with a pinned key and the event is your account's in this mode.
|
|
153
|
+
// WEBHOOK_PUBLIC_KEYS: your account's keys in this mode, `whpk_…`, pinned from GET /v1/attestation.
|
|
154
|
+
const event = await constructEvent(rawBody, request.headers, WEBHOOK_PUBLIC_KEYS, {
|
|
155
|
+
expectedAccount: "acct_…",
|
|
156
|
+
expectedLivemode: false,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Sweeping, offline: forwarders from GET /v1/forwarders?sweepable=<token>.
|
|
160
|
+
const calls = flushTransactions(forwarders, PHA);
|
|
161
|
+
const batchFile = safeBatch(1, TREASURY_SAFE, calls); // Safe Transaction Builder JSON
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`verifyDepositAddress(pins, address)` checks every network of a deposit address the same way.
|
|
165
|
+
`quoteAddress`, `depositAddress`, `forwarderAddress`, `quoteSalt`, and `depositAddressSalt`
|
|
166
|
+
recompute an address offline from a treasury you pass, as the Python SDK does.
|
|
167
|
+
|
|
168
|
+
## Chain and token icons
|
|
169
|
+
|
|
170
|
+
`Checkout` and `DepositAddress` include compact inline icons for Ethereum, Base, USDC, USDT,
|
|
171
|
+
PHA, and ETH. Testnets reuse their mainnet family artwork. Unknown chains
|
|
172
|
+
and assets show a neutral first-letter monogram. Symbols are case-insensitive.
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
import { NetworkIcon, AssetIcon } from "@phala/pay-react";
|
|
176
|
+
|
|
177
|
+
<NetworkIcon chainId={8453} size={16} />; // decorative beside visible "Base" text
|
|
178
|
+
<AssetIcon asset="usdc" size={20} decorative={false} />; // accessible when used alone
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Import `@phala/pay-react/styles.css` for alignment. The default
|
|
182
|
+
size is 18px. Icons use SVG width/height attributes and React elements, so they need no inline
|
|
183
|
+
styles, data URI allowance, or extra CSP directives, including with `style-src 'self'`.
|
|
184
|
+
|
|
185
|
+
Framework-free hosts can call `networkIcon(chainId)` or `assetIcon(symbol)` from `@phala/pay`
|
|
186
|
+
for decorative SVG markup. Keep the
|
|
187
|
+
name visible alongside it; when used alone, supply an accessible label on the host wrapper.
|
|
188
|
+
|
|
189
|
+
Only six branded SVGs from `@web3icons/core@4.0.57` are vendored, under the MIT license in
|
|
190
|
+
`THIRD_PARTY_NOTICES`. Regenerate both the markup and JSX modules with
|
|
191
|
+
`npx -y pnpm@12.6.0 run icons:generate`; the script packs the pinned package into a temporary
|
|
192
|
+
directory, rejects unexpected SVG tags/attributes, and cleans up. No web3icons runtime package
|
|
193
|
+
is installed.
|
|
194
|
+
|
|
195
|
+
## Development
|
|
196
|
+
|
|
197
|
+
```sh
|
|
198
|
+
pnpm install
|
|
199
|
+
pnpm run check # typecheck, lint, unit tests, build
|
|
200
|
+
pnpm run e2e # Playwright against Anvil; needs Foundry (anvil, forge) on PATH
|
|
201
|
+
pnpm run e2e:docker # the same, with Chromium from Playwright's image (as CI runs it)
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
The end-to-end tests start Anvil as Sepolia, deploy a test token, and pay a quote from a mocked
|
|
205
|
+
EIP-6963 wallet backed by the node (and again through a viem `WalletClient` passed as
|
|
206
|
+
`walletClient`), then decode the QR code and exercise the manual details.
|
|
207
|
+
|
|
208
|
+
## Releases
|
|
209
|
+
|
|
210
|
+
`@phala/pay` is released with the service: the `v<version>` tag's Release workflow publishes it to
|
|
211
|
+
npm (environment `npm`) with trusted publishing and provenance
|
|
212
|
+
([CONTRIBUTING.md, "Releasing"](../../CONTRIBUTING.md#releasing)). Its changes are in the top-level
|
|
213
|
+
[CHANGELOG.md](https://github.com/Phala-Network/phala-pay/blob/main/CHANGELOG.md), under "JS SDK";
|
|
214
|
+
[its releases before v0.5.0](https://github.com/Phala-Network/phala-pay/blob/main/sdk/js/CHANGELOG.md),
|
|
215
|
+
versioned on their own, stay in `sdk/js`.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
web3icons (@web3icons/core@4.0.57)
|
|
2
|
+
https://github.com/0xa3k5/web3icons
|
|
3
|
+
Vendored branded token and network SVGs.
|
|
4
|
+
|
|
5
|
+
MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2024 0xa3k5
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkout-params.d.ts","sourceRoot":"","sources":["../../../../../js/src/shared/checkout-params.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkout-params.js","sourceRoot":"","sources":["../../../../../js/src/shared/checkout-params.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
/**
|
|
3
|
+
* The forwarder `factory` deploys for `treasury` and `salt`: OpenZeppelin 5.x
|
|
4
|
+
* `Clones.predictDeterministicAddressWithImmutableArgs` with `abi.encodePacked(treasury)`, so the
|
|
5
|
+
* address commits to the factory, the implementation, the treasury, and the salt.
|
|
6
|
+
*/
|
|
7
|
+
export declare function forwarderAddress(factory: string, implementation: string, treasury: string, salt: Hex): Address;
|
|
8
|
+
/** `keccak256(abi.encode(account, client_reference_id, "quote", quote_id))`: a quote's salt. */
|
|
9
|
+
export declare function quoteSalt(account: string, clientReferenceId: string, quoteId: string): Hex;
|
|
10
|
+
/**
|
|
11
|
+
* `keccak256(abi.encode(account, livemode, client_reference_id, "deposit_address", version))`:
|
|
12
|
+
* a deposit address's salt, the same on every chain.
|
|
13
|
+
*/
|
|
14
|
+
export declare function depositAddressSalt(account: string, livemode: boolean, clientReferenceId: string, version: number | bigint): Hex;
|
|
15
|
+
export interface Forwarder {
|
|
16
|
+
factory: string;
|
|
17
|
+
implementation: string;
|
|
18
|
+
}
|
|
19
|
+
/** The address of a quote over `treasury`, which you pin yourself (see `verifyQuoteAddress`). */
|
|
20
|
+
export declare function quoteAddress(forwarder: Forwarder, quote: {
|
|
21
|
+
client_reference_id: string;
|
|
22
|
+
id: string;
|
|
23
|
+
}, treasury: string, account: string): Address;
|
|
24
|
+
/** A deposit address on the network whose treasury is `treasury`, which you pin yourself. */
|
|
25
|
+
export declare function depositAddress(forwarder: Forwarder, address: {
|
|
26
|
+
livemode: boolean;
|
|
27
|
+
client_reference_id: string;
|
|
28
|
+
version: number;
|
|
29
|
+
}, treasury: string, account: string): Address;
|
|
30
|
+
/** An address the service returned is not the one your pins derive: show nothing to pay. */
|
|
31
|
+
export { AddressMismatchError } from "./errors.js";
|
|
32
|
+
/**
|
|
33
|
+
* What every address is recomputed from, configured on your server and never read from the
|
|
34
|
+
* service: your account id (`acct_…`), the forwarder `factory` and `implementation` of the
|
|
35
|
+
* attested deployment, and `treasuries`, your own treasury per chain id as you proved it. A
|
|
36
|
+
* compromised service could return another treasury with the address that really derives from
|
|
37
|
+
* it, so the response's `treasury` is never trusted.
|
|
38
|
+
*
|
|
39
|
+
* Live mode requires `treasuries`: without the chain's treasury the check fails closed. In test
|
|
40
|
+
* mode an unpinned chain falls back to the response's treasury with a console warning.
|
|
41
|
+
*/
|
|
42
|
+
export interface AddressPins extends Forwarder {
|
|
43
|
+
account: string;
|
|
44
|
+
treasuries?: Readonly<Record<number, string>> | undefined;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recomputes an open quote's address from your pins and returns it, the `expectedAddress` to
|
|
48
|
+
* pass to `<Checkout>`. Throws `AddressMismatchError` unless the quote names your pinned
|
|
49
|
+
* treasury of its chain and its address derives from it; in live mode also when the chain has no
|
|
50
|
+
* pinned treasury.
|
|
51
|
+
*/
|
|
52
|
+
export declare function verifyQuoteAddress(pins: AddressPins, quote: {
|
|
53
|
+
id: string;
|
|
54
|
+
livemode: boolean;
|
|
55
|
+
chain_id: number;
|
|
56
|
+
treasury: string;
|
|
57
|
+
address: string;
|
|
58
|
+
client_reference_id: string;
|
|
59
|
+
}): Address;
|
|
60
|
+
/**
|
|
61
|
+
* Recomputes every network of an active deposit address from your pins and returns the address,
|
|
62
|
+
* the same on every network. Throws `AddressMismatchError` as `verifyQuoteAddress` does, for any
|
|
63
|
+
* network.
|
|
64
|
+
*/
|
|
65
|
+
export declare function verifyDepositAddress(pins: AddressPins, address: {
|
|
66
|
+
id: string;
|
|
67
|
+
livemode: boolean;
|
|
68
|
+
client_reference_id: string;
|
|
69
|
+
version: number;
|
|
70
|
+
networks: readonly {
|
|
71
|
+
chain_id: number;
|
|
72
|
+
treasury: string;
|
|
73
|
+
address: string;
|
|
74
|
+
}[];
|
|
75
|
+
}): Address;
|
|
76
|
+
//# sourceMappingURL=addresses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addresses.d.ts","sourceRoot":"","sources":["../../../src/addresses.ts"],"names":[],"mappings":"AAAA,OAAO,EAML,KAAK,OAAO,EACZ,KAAK,GAAG,EACT,MAAM,MAAM,CAAC;AAEd;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,GAAG,GACR,OAAO,CAgBT;AAED,gGAAgG;AAChG,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,GAAG,CAO1F;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,OAAO,EACjB,iBAAiB,EAAE,MAAM,EACzB,OAAO,EAAE,MAAM,GAAG,MAAM,GACvB,GAAG,CAaL;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,iGAAiG;AACjG,wBAAgB,YAAY,CAC1B,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE;IAAE,mBAAmB,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,EAClD,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GACd,OAAO,CAOT;AAED,6FAA6F;AAC7F,wBAAgB,cAAc,CAC5B,SAAS,EAAE,SAAS,EACpB,OAAO,EAAE;IAAE,QAAQ,EAAE,OAAO,CAAC;IAAC,mBAAmB,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAC5E,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,GACd,OAAO,CAOT;AAED,4FAA4F;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAGnD;;;;;;;;;GASG;AACH,MAAM,WAAW,WAAY,SAAQ,SAAS;IAC5C,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,GAAG,SAAS,CAAC;CAC3D;AAwCD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,WAAW,EACjB,KAAK,EAAE;IACL,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,mBAAmB,EAAE,MAAM,CAAC;CAC7B,GACA,OAAO,CAST;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,WAAW,EACjB,OAAO,EAAE;IACP,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,OAAO,CAAC;IAClB,mBAAmB,EAAE,MAAM,CAAC;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,SAAS;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;CAC9E,GACA,OAAO,CAgBT"}
|