@phala/pay 0.8.2 → 0.9.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.
- package/README.md +33 -58
- package/THIRD_PARTY_NOTICES +25 -0
- package/dist/chains.d.ts +2 -0
- package/dist/chains.d.ts.map +1 -1
- package/dist/chains.js +11 -2
- package/dist/chains.js.map +1 -1
- package/dist/checkout.d.ts +4 -2
- package/dist/checkout.d.ts.map +1 -1
- package/dist/checkout.js +18 -3
- package/dist/checkout.js.map +1 -1
- package/dist/deposit-address.js +2 -2
- package/dist/deposit-address.js.map +1 -1
- package/dist/icon-names.d.ts +7 -0
- package/dist/icon-names.d.ts.map +1 -0
- package/dist/icon-names.js +15 -0
- package/dist/icon-names.js.map +1 -0
- package/dist/icon-svg.d.ts +6 -0
- package/dist/icon-svg.d.ts.map +1 -0
- package/dist/icon-svg.js +13 -0
- package/dist/icon-svg.js.map +1 -0
- package/dist/icons.d.ts +5 -0
- package/dist/icons.d.ts.map +1 -0
- package/dist/icons.js +27 -0
- package/dist/icons.js.map +1 -0
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/request.d.ts +2 -0
- package/dist/request.d.ts.map +1 -1
- package/dist/request.js +7 -0
- package/dist/request.js.map +1 -1
- package/dist/shared/checkout-params.d.ts +7 -0
- package/dist/shared/checkout-params.d.ts.map +1 -0
- package/dist/shared/checkout-params.js +2 -0
- package/dist/shared/checkout-params.js.map +1 -0
- package/package.json +18 -31
- package/src/chains.ts +12 -2
- package/src/checkout.ts +21 -4
- package/src/deposit-address.ts +2 -2
- package/src/generated/openapi.d.ts +9482 -0
- package/src/icon-names.ts +20 -0
- package/src/icon-svg.ts +13 -0
- package/src/icons.ts +30 -0
- package/src/index.ts +6 -2
- package/src/request.ts +7 -0
- package/src/shared/checkout-params.ts +6 -0
- package/dist/react/Checkout.d.ts +0 -43
- package/dist/react/Checkout.d.ts.map +0 -1
- package/dist/react/Checkout.js +0 -193
- package/dist/react/Checkout.js.map +0 -1
- package/dist/react/DepositAddress.d.ts +0 -32
- package/dist/react/DepositAddress.d.ts.map +0 -1
- package/dist/react/DepositAddress.js +0 -131
- package/dist/react/DepositAddress.js.map +0 -1
- package/dist/react/Field.d.ts +0 -7
- package/dist/react/Field.d.ts.map +0 -1
- package/dist/react/Field.js +0 -21
- package/dist/react/Field.js.map +0 -1
- package/dist/react/QrCode.d.ts +0 -8
- package/dist/react/QrCode.d.ts.map +0 -1
- package/dist/react/QrCode.js +0 -20
- package/dist/react/QrCode.js.map +0 -1
- package/dist/react/appearance.d.ts +0 -5
- package/dist/react/appearance.d.ts.map +0 -1
- package/dist/react/appearance.js +0 -2
- package/dist/react/appearance.js.map +0 -1
- package/dist/react/index.d.ts +0 -5
- package/dist/react/index.d.ts.map +0 -1
- package/dist/react/index.js +0 -5
- package/dist/react/index.js.map +0 -1
- package/dist/react/useCheckout.d.ts +0 -9
- package/dist/react/useCheckout.d.ts.map +0 -1
- package/dist/react/useCheckout.js +0 -32
- package/dist/react/useCheckout.js.map +0 -1
- package/dist/server/addresses.d.ts +0 -78
- package/dist/server/addresses.d.ts.map +0 -1
- package/dist/server/addresses.js +0 -118
- package/dist/server/addresses.js.map +0 -1
- package/dist/server/index.d.ts +0 -12
- package/dist/server/index.d.ts.map +0 -1
- package/dist/server/index.js +0 -12
- package/dist/server/index.js.map +0 -1
- package/dist/server/sweeps.d.ts +0 -78
- package/dist/server/sweeps.d.ts.map +0 -1
- package/dist/server/sweeps.js +0 -117
- package/dist/server/sweeps.js.map +0 -1
- package/dist/server/webhook.d.ts +0 -61
- package/dist/server/webhook.d.ts.map +0 -1
- package/dist/server/webhook.js +0 -154
- package/dist/server/webhook.js.map +0 -1
- package/src/css.d.ts +0 -1
- package/src/react/Checkout.tsx +0 -469
- package/src/react/DepositAddress.tsx +0 -239
- package/src/react/Field.tsx +0 -46
- package/src/react/QrCode.tsx +0 -39
- package/src/react/appearance.ts +0 -4
- package/src/react/index.ts +0 -6
- package/src/react/useCheckout.ts +0 -58
- package/src/server/addresses.ts +0 -223
- package/src/server/index.ts +0 -38
- package/src/server/sweeps.ts +0 -197
- package/src/server/webhook.ts +0 -208
- package/src/styles.css +0 -76
|
@@ -1,239 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
import { useEffect, useId, useState } from "react";
|
|
4
|
-
import { formatUnits } from "viem";
|
|
5
|
-
import { networkName } from "../chains.js";
|
|
6
|
-
import {
|
|
7
|
-
retrieveDepositAddress,
|
|
8
|
-
type ClientDepositAddress,
|
|
9
|
-
type DepositAddressPayment,
|
|
10
|
-
} from "../deposit-address.js";
|
|
11
|
-
import { CheckoutError, pollDelay } from "../checkout.js";
|
|
12
|
-
import { formatWait } from "../format.js";
|
|
13
|
-
import { depositAddressTransfer, type DepositAddressDetails } from "../payment.js";
|
|
14
|
-
import type { Appearance } from "./appearance.js";
|
|
15
|
-
import { Field } from "./Field.js";
|
|
16
|
-
import { QrCode } from "./QrCode.js";
|
|
17
|
-
|
|
18
|
-
export interface DepositAddressProps {
|
|
19
|
-
/** `address` and `networks` from your backend's `POST /v1/deposit_addresses`. */
|
|
20
|
-
depositAddress: DepositAddressDetails;
|
|
21
|
-
/** The network shown first; the first of `networks` by default. */
|
|
22
|
-
chainId?: number;
|
|
23
|
-
/** The token shown first, for example `pha`; the network's first token by default. */
|
|
24
|
-
asset?: string;
|
|
25
|
-
/**
|
|
26
|
-
* The `client_secret` of the same response. With `apiBase`, the component follows the
|
|
27
|
-
* address's payments and shows each one within about a block of arriving: received with its
|
|
28
|
-
* confirmations, then credited, and states each network's typical credit time. Display only;
|
|
29
|
-
* credit from your `deposit.credited` webhook.
|
|
30
|
-
*/
|
|
31
|
-
clientSecret?: string;
|
|
32
|
-
/** The service origin, for example `https://pay.example.com`; needed with `clientSecret`. */
|
|
33
|
-
apiBase?: string;
|
|
34
|
-
/** Milliseconds between payment reads; default 3000. */
|
|
35
|
-
pollInterval?: number;
|
|
36
|
-
appearance?: Appearance;
|
|
37
|
-
className?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* A customer's persistent deposit address: one address for every supported token on every
|
|
42
|
-
* supported network. The payer picks a network and a token; the component shows a QR code of that
|
|
43
|
-
* EIP-681 transfer request and the address and token contract to copy. Any amount of a supported
|
|
44
|
-
* token sent is credited at the market rate when it arrives; follow it from your backend's
|
|
45
|
-
* `deposit.credited` webhook.
|
|
46
|
-
*/
|
|
47
|
-
export function DepositAddress({
|
|
48
|
-
depositAddress,
|
|
49
|
-
chainId,
|
|
50
|
-
asset,
|
|
51
|
-
clientSecret,
|
|
52
|
-
apiBase,
|
|
53
|
-
pollInterval,
|
|
54
|
-
appearance,
|
|
55
|
-
className,
|
|
56
|
-
}: DepositAddressProps) {
|
|
57
|
-
const { networks } = depositAddress;
|
|
58
|
-
const id = useId();
|
|
59
|
-
const view = useClientView(clientSecret, apiBase, pollInterval ?? 3000);
|
|
60
|
-
const payments = view?.payments ?? [];
|
|
61
|
-
const [selectedChain, setSelectedChain] = useState(chainId ?? networks[0]?.chain_id);
|
|
62
|
-
const [selectedAsset, setSelectedAsset] = useState(asset);
|
|
63
|
-
const network = networks.find((candidate) => candidate.chain_id === selectedChain) ?? networks[0];
|
|
64
|
-
if (network === undefined) {
|
|
65
|
-
throw new TypeError("the deposit address has no network");
|
|
66
|
-
}
|
|
67
|
-
const token =
|
|
68
|
-
network.assets.find((candidate) => candidate.asset === selectedAsset) ?? network.assets[0];
|
|
69
|
-
if (token === undefined) {
|
|
70
|
-
throw new TypeError(`the deposit address takes no token on chain ${network.chain_id}`);
|
|
71
|
-
}
|
|
72
|
-
const { token: contract, to } = depositAddressTransfer(network, token);
|
|
73
|
-
const symbol = token.asset.toUpperCase();
|
|
74
|
-
const name = networkName(network.chain_id);
|
|
75
|
-
const tokens = [...new Set(networks.flatMap((each) => each.assets.map((a) => a.asset.toUpperCase())))];
|
|
76
|
-
return (
|
|
77
|
-
<div
|
|
78
|
-
className={className === undefined ? "pp-root" : `pp-root ${className}`}
|
|
79
|
-
data-theme={appearance?.theme ?? "light"}
|
|
80
|
-
>
|
|
81
|
-
<p className="pp-subtitle">
|
|
82
|
-
{depositAddress.address === null
|
|
83
|
-
? "Your deposit address for every supported token; it differs on some networks"
|
|
84
|
-
: "One address for all supported tokens and networks"}
|
|
85
|
-
</p>
|
|
86
|
-
{networks.length > 1 && (
|
|
87
|
-
<fieldset className="pp-choices">
|
|
88
|
-
<legend>Network</legend>
|
|
89
|
-
{networks.map((each) => (
|
|
90
|
-
<label key={each.chain_id}>
|
|
91
|
-
<input
|
|
92
|
-
type="radio"
|
|
93
|
-
name={`${id}-network`}
|
|
94
|
-
checked={each.chain_id === network.chain_id}
|
|
95
|
-
onChange={() => setSelectedChain(each.chain_id)}
|
|
96
|
-
/>
|
|
97
|
-
{networkName(each.chain_id)}
|
|
98
|
-
</label>
|
|
99
|
-
))}
|
|
100
|
-
</fieldset>
|
|
101
|
-
)}
|
|
102
|
-
{network.assets.length > 1 && (
|
|
103
|
-
<fieldset className="pp-choices">
|
|
104
|
-
<legend>Token</legend>
|
|
105
|
-
{network.assets.map((each) => (
|
|
106
|
-
<label key={each.asset}>
|
|
107
|
-
<input
|
|
108
|
-
type="radio"
|
|
109
|
-
name={`${id}-token`}
|
|
110
|
-
checked={each.asset === token.asset}
|
|
111
|
-
onChange={() => setSelectedAsset(each.asset)}
|
|
112
|
-
/>
|
|
113
|
-
{each.asset.toUpperCase()}
|
|
114
|
-
</label>
|
|
115
|
-
))}
|
|
116
|
-
</fieldset>
|
|
117
|
-
)}
|
|
118
|
-
<div className="pp-qr-panel">
|
|
119
|
-
<QrCode value={token.payment_uri} label={`Deposit address for ${symbol} on ${name}`} />
|
|
120
|
-
</div>
|
|
121
|
-
<dl className="pp-fields">
|
|
122
|
-
<Field label="Network" value={`${name} (chain ID ${network.chain_id})`} />
|
|
123
|
-
<Field label={`Token (${symbol}) contract`} value={contract} copy />
|
|
124
|
-
<Field label="Deposit address" value={to} copy />
|
|
125
|
-
</dl>
|
|
126
|
-
{payments.length > 0 && (
|
|
127
|
-
<ul className="pp-payments" aria-live="polite" aria-label="Payments">
|
|
128
|
-
{payments.map((payment) => (
|
|
129
|
-
<li key={`${payment.chain_id}:${payment.tx_hash}:${payment.created}`}>
|
|
130
|
-
{paymentMessage(payment)}
|
|
131
|
-
</li>
|
|
132
|
-
))}
|
|
133
|
-
</ul>
|
|
134
|
-
)}
|
|
135
|
-
<p className="pp-message">
|
|
136
|
-
Send only {tokens.join(", ")} on {networks.map((each) => networkName(each.chain_id)).join(", ")}
|
|
137
|
-
. {creditMessage(networks.map((each) => each.chain_id), view)} Other tokens and networks are not
|
|
138
|
-
credited. You can reuse this address.
|
|
139
|
-
</p>
|
|
140
|
-
</div>
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/** The address's public view, read every `interval` while mounted; `null` without a secret. */
|
|
145
|
-
function useClientView(
|
|
146
|
-
clientSecret: string | undefined,
|
|
147
|
-
apiBase: string | undefined,
|
|
148
|
-
interval: number,
|
|
149
|
-
): ClientDepositAddress | null {
|
|
150
|
-
const key = clientSecret === undefined || apiBase === undefined ? "" : `${apiBase} ${clientSecret}`;
|
|
151
|
-
const [current, setCurrent] = useState<{ key: string; view: ClientDepositAddress | null }>({
|
|
152
|
-
key: "",
|
|
153
|
-
view: null,
|
|
154
|
-
});
|
|
155
|
-
useEffect(() => {
|
|
156
|
-
if (clientSecret === undefined || apiBase === undefined) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
const controller = new AbortController();
|
|
160
|
-
let stopped = false;
|
|
161
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
162
|
-
let failures = 0;
|
|
163
|
-
const load = async () => {
|
|
164
|
-
let failure: CheckoutError | null = null;
|
|
165
|
-
try {
|
|
166
|
-
const view = await retrieveDepositAddress({ clientSecret, apiBase, signal: controller.signal });
|
|
167
|
-
failures = 0;
|
|
168
|
-
if (!stopped) {
|
|
169
|
-
setCurrent({ key, view });
|
|
170
|
-
}
|
|
171
|
-
} catch (error) {
|
|
172
|
-
failures += 1;
|
|
173
|
-
// A secret that is not valid will not become valid: stop, keep showing the address.
|
|
174
|
-
if (error instanceof CheckoutError && error.code === "invalid_client_secret") {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
failure = error instanceof CheckoutError ? error : null;
|
|
178
|
-
}
|
|
179
|
-
if (!stopped) {
|
|
180
|
-
timer = setTimeout(() => void load(), pollDelay(interval, failures, failure));
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
void load();
|
|
184
|
-
return () => {
|
|
185
|
-
stopped = true;
|
|
186
|
-
controller.abort();
|
|
187
|
-
clearTimeout(timer);
|
|
188
|
-
};
|
|
189
|
-
}, [key, clientSecret, apiBase, interval]);
|
|
190
|
-
// Another address starts without the previous one's view.
|
|
191
|
-
return current.key === key && key !== "" ? current.view : null;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* When a payment on each of `chainIds` is credited, from `view`'s typical credit times, for
|
|
196
|
-
* example "usually in about 30 seconds on Ethereum and about 7 seconds on Base". Before the view
|
|
197
|
-
* is loaded it names no time.
|
|
198
|
-
*/
|
|
199
|
-
function creditMessage(chainIds: number[], view: ClientDepositAddress | null): string {
|
|
200
|
-
const names = new Map<number, string[]>();
|
|
201
|
-
for (const network of view?.networks ?? []) {
|
|
202
|
-
if (chainIds.includes(network.chain_id)) {
|
|
203
|
-
const seconds = network.typical_credit_seconds;
|
|
204
|
-
names.set(seconds, [...(names.get(seconds) ?? []), networkName(network.chain_id)]);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (names.size === 0) {
|
|
208
|
-
return "Any amount is credited at the market rate once it is confirmed on its network.";
|
|
209
|
-
}
|
|
210
|
-
const list = new Intl.ListFormat("en", { type: "conjunction" });
|
|
211
|
-
const [only, ...others] = names.keys();
|
|
212
|
-
const usually =
|
|
213
|
-
only !== undefined && others.length === 0
|
|
214
|
-
? formatWait(only)
|
|
215
|
-
: list.format([...names].map(([seconds, on]) => `${formatWait(seconds)} on ${list.format(on)}`));
|
|
216
|
-
return `Any amount is credited at the market rate when it arrives, usually in ${usually}.`;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
function paymentMessage(payment: DepositAddressPayment): string {
|
|
220
|
-
const amount =
|
|
221
|
-
payment.asset === null || payment.decimals === null
|
|
222
|
-
? "A payment"
|
|
223
|
-
: `${formatUnits(BigInt(payment.amount_atomic), payment.decimals)} ${payment.asset.toUpperCase()}`;
|
|
224
|
-
const on = `on ${networkName(payment.chain_id)}`;
|
|
225
|
-
switch (payment.status) {
|
|
226
|
-
case "seen":
|
|
227
|
-
return payment.confirmations === null
|
|
228
|
-
? `${amount} received ${on}, crediting shortly`
|
|
229
|
-
: `${amount} received ${on}, ${payment.confirmations} confirmation${payment.confirmations === 1 ? "" : "s"}`;
|
|
230
|
-
case "confirming":
|
|
231
|
-
return `${amount} confirmed ${on}, crediting…`;
|
|
232
|
-
case "credited":
|
|
233
|
-
return `${amount} ${on} credited`;
|
|
234
|
-
case "rejected":
|
|
235
|
-
return `${amount} ${on} cannot be credited. Contact support with your transaction.`;
|
|
236
|
-
case "reversed":
|
|
237
|
-
return `${amount} ${on} was reversed: its transaction is no longer on the blockchain.`;
|
|
238
|
-
}
|
|
239
|
-
}
|
package/src/react/Field.tsx
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { useEffect, useState } from "react";
|
|
2
|
-
|
|
3
|
-
/** `copy` adds a copy button, copying `value`, or the given string when it differs from it. */
|
|
4
|
-
export function Field({
|
|
5
|
-
label,
|
|
6
|
-
value,
|
|
7
|
-
copy = false,
|
|
8
|
-
}: {
|
|
9
|
-
label: string;
|
|
10
|
-
value: string;
|
|
11
|
-
copy?: boolean | string;
|
|
12
|
-
}) {
|
|
13
|
-
return (
|
|
14
|
-
<div className="pp-field">
|
|
15
|
-
<dt>{label}</dt>
|
|
16
|
-
<dd>
|
|
17
|
-
<span className={copy === false ? undefined : "pp-value"}>{value}</span>
|
|
18
|
-
{copy !== false && (
|
|
19
|
-
<CopyButton value={typeof copy === "string" ? copy : value} label={label} />
|
|
20
|
-
)}
|
|
21
|
-
</dd>
|
|
22
|
-
</div>
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function CopyButton({ value, label }: { value: string; label: string }) {
|
|
27
|
-
const [copied, setCopied] = useState<boolean | null>(null);
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
if (copied === null) {
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
const timer = setTimeout(() => setCopied(null), 2000);
|
|
33
|
-
return () => clearTimeout(timer);
|
|
34
|
-
}, [copied]);
|
|
35
|
-
const onClick = () => {
|
|
36
|
-
navigator.clipboard.writeText(value).then(
|
|
37
|
-
() => setCopied(true),
|
|
38
|
-
() => setCopied(false),
|
|
39
|
-
);
|
|
40
|
-
};
|
|
41
|
-
return (
|
|
42
|
-
<button type="button" className="pp-copy" onClick={onClick} aria-label={`Copy ${label}`}>
|
|
43
|
-
<span aria-live="polite">{copied === null ? "Copy" : copied ? "Copied" : "Copy failed"}</span>
|
|
44
|
-
</button>
|
|
45
|
-
);
|
|
46
|
-
}
|
package/src/react/QrCode.tsx
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { encode } from "uqr";
|
|
3
|
-
|
|
4
|
-
export interface QrCodeProps {
|
|
5
|
-
value: string;
|
|
6
|
-
label: string;
|
|
7
|
-
size?: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** A QR code as an SVG path, one unit per module. */
|
|
11
|
-
export function QrCode({ value, label, size = 208 }: QrCodeProps) {
|
|
12
|
-
const { path, width } = useMemo(() => {
|
|
13
|
-
const { data, size: modules } = encode(value, { ecc: "M", border: 2 });
|
|
14
|
-
let d = "";
|
|
15
|
-
data.forEach((row, y) => {
|
|
16
|
-
row.forEach((dark, x) => {
|
|
17
|
-
if (dark) {
|
|
18
|
-
d += `M${x} ${y}h1v1h-1z`;
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
return { path: d, width: modules };
|
|
23
|
-
}, [value]);
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<svg
|
|
27
|
-
className="pp-qr"
|
|
28
|
-
role="img"
|
|
29
|
-
aria-label={label}
|
|
30
|
-
width={size}
|
|
31
|
-
height={size}
|
|
32
|
-
viewBox={`0 0 ${width} ${width}`}
|
|
33
|
-
shapeRendering="crispEdges"
|
|
34
|
-
>
|
|
35
|
-
<rect width={width} height={width} fill="#fff" />
|
|
36
|
-
<path d={path} fill="#000" />
|
|
37
|
-
</svg>
|
|
38
|
-
);
|
|
39
|
-
}
|
package/src/react/appearance.ts
DELETED
package/src/react/index.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
|
-
export { Checkout, type CheckoutProps } from "./Checkout.js";
|
|
4
|
-
export { DepositAddress, type DepositAddressProps } from "./DepositAddress.js";
|
|
5
|
-
export { useCheckout, type UseCheckoutOptions, type UseCheckoutResult } from "./useCheckout.js";
|
|
6
|
-
export type { Appearance } from "./appearance.js";
|
package/src/react/useCheckout.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import {
|
|
3
|
-
createCheckout,
|
|
4
|
-
type CheckoutOptions,
|
|
5
|
-
type CheckoutSession,
|
|
6
|
-
type CheckoutState,
|
|
7
|
-
} from "../checkout.js";
|
|
8
|
-
|
|
9
|
-
export type UseCheckoutOptions = Pick<
|
|
10
|
-
CheckoutOptions,
|
|
11
|
-
"clientSecret" | "expectedAddress" | "apiBase" | "pollInterval" | "signal" | "requestTimeout"
|
|
12
|
-
>;
|
|
13
|
-
|
|
14
|
-
export interface UseCheckoutResult extends CheckoutState {
|
|
15
|
-
/** Reads the quote now, for example right after the wallet broadcast the payment. */
|
|
16
|
-
refresh: () => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const LOADING: CheckoutState = { status: "loading", quote: null, error: null };
|
|
20
|
-
|
|
21
|
-
/** Follows a quote's public view for as long as the component is mounted. */
|
|
22
|
-
export function useCheckout({
|
|
23
|
-
clientSecret,
|
|
24
|
-
expectedAddress,
|
|
25
|
-
apiBase,
|
|
26
|
-
pollInterval,
|
|
27
|
-
signal,
|
|
28
|
-
requestTimeout,
|
|
29
|
-
}: UseCheckoutOptions): UseCheckoutResult {
|
|
30
|
-
const [current, setCurrent] = useState({ key: "", state: LOADING });
|
|
31
|
-
const checkout = useRef<CheckoutSession | null>(null);
|
|
32
|
-
const key = `${apiBase} ${clientSecret} ${expectedAddress}`;
|
|
33
|
-
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
const instance = createCheckout({
|
|
36
|
-
clientSecret,
|
|
37
|
-
expectedAddress,
|
|
38
|
-
apiBase,
|
|
39
|
-
...(pollInterval === undefined ? {} : { pollInterval }),
|
|
40
|
-
...(signal === undefined ? {} : { signal }),
|
|
41
|
-
...(requestTimeout === undefined ? {} : { requestTimeout }),
|
|
42
|
-
});
|
|
43
|
-
checkout.current = instance;
|
|
44
|
-
const unsubscribe = instance.subscribe((state) => setCurrent({ key, state }));
|
|
45
|
-
return () => {
|
|
46
|
-
unsubscribe();
|
|
47
|
-
instance.destroy();
|
|
48
|
-
checkout.current = null;
|
|
49
|
-
};
|
|
50
|
-
}, [key, clientSecret, expectedAddress, apiBase, pollInterval, signal, requestTimeout]);
|
|
51
|
-
|
|
52
|
-
const refresh = useCallback(() => {
|
|
53
|
-
void checkout.current?.refresh();
|
|
54
|
-
}, []);
|
|
55
|
-
|
|
56
|
-
// A new client secret starts from `loading`, not from the previous quote's state.
|
|
57
|
-
return { ...(current.key === key ? current.state : LOADING), refresh };
|
|
58
|
-
}
|
package/src/server/addresses.ts
DELETED
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
encodeAbiParameters,
|
|
3
|
-
getAddress,
|
|
4
|
-
getContractAddress,
|
|
5
|
-
keccak256,
|
|
6
|
-
concat,
|
|
7
|
-
type Address,
|
|
8
|
-
type Hex,
|
|
9
|
-
} from "viem";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The forwarder `factory` deploys for `treasury` and `salt`: OpenZeppelin 5.x
|
|
13
|
-
* `Clones.predictDeterministicAddressWithImmutableArgs` with `abi.encodePacked(treasury)`, so the
|
|
14
|
-
* address commits to the factory, the implementation, the treasury, and the salt.
|
|
15
|
-
*/
|
|
16
|
-
export function forwarderAddress(
|
|
17
|
-
factory: string,
|
|
18
|
-
implementation: string,
|
|
19
|
-
treasury: string,
|
|
20
|
-
salt: Hex,
|
|
21
|
-
): Address {
|
|
22
|
-
const initCode = concat([
|
|
23
|
-
"0x61",
|
|
24
|
-
"0x0041", // runtime length: the 45-byte proxy and the 20 treasury bytes
|
|
25
|
-
"0x3d81600a3d39f3",
|
|
26
|
-
"0x363d3d373d3d3d363d73",
|
|
27
|
-
getAddress(implementation),
|
|
28
|
-
"0x5af43d82803e903d91602b57fd5bf3",
|
|
29
|
-
getAddress(treasury),
|
|
30
|
-
]);
|
|
31
|
-
return getContractAddress({
|
|
32
|
-
opcode: "CREATE2",
|
|
33
|
-
from: getAddress(factory),
|
|
34
|
-
salt,
|
|
35
|
-
bytecodeHash: keccak256(initCode),
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** `keccak256(abi.encode(account, client_reference_id, "quote", quote_id))`: a quote's salt. */
|
|
40
|
-
export function quoteSalt(account: string, clientReferenceId: string, quoteId: string): Hex {
|
|
41
|
-
return keccak256(
|
|
42
|
-
encodeAbiParameters(
|
|
43
|
-
[{ type: "string" }, { type: "string" }, { type: "string" }, { type: "string" }],
|
|
44
|
-
[account, clientReferenceId, "quote", quoteId],
|
|
45
|
-
),
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* `keccak256(abi.encode(account, livemode, client_reference_id, "deposit_address", version))`:
|
|
51
|
-
* a deposit address's salt, the same on every chain.
|
|
52
|
-
*/
|
|
53
|
-
export function depositAddressSalt(
|
|
54
|
-
account: string,
|
|
55
|
-
livemode: boolean,
|
|
56
|
-
clientReferenceId: string,
|
|
57
|
-
version: number | bigint,
|
|
58
|
-
): Hex {
|
|
59
|
-
return keccak256(
|
|
60
|
-
encodeAbiParameters(
|
|
61
|
-
[
|
|
62
|
-
{ type: "string" },
|
|
63
|
-
{ type: "bool" },
|
|
64
|
-
{ type: "string" },
|
|
65
|
-
{ type: "string" },
|
|
66
|
-
{ type: "uint256" },
|
|
67
|
-
],
|
|
68
|
-
[account, livemode, clientReferenceId, "deposit_address", BigInt(version)],
|
|
69
|
-
),
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export interface Forwarder {
|
|
74
|
-
factory: string;
|
|
75
|
-
implementation: string;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** The address of a quote over `treasury`, which you pin yourself (see `verifyQuoteAddress`). */
|
|
79
|
-
export function quoteAddress(
|
|
80
|
-
forwarder: Forwarder,
|
|
81
|
-
quote: { client_reference_id: string; id: string },
|
|
82
|
-
treasury: string,
|
|
83
|
-
account: string,
|
|
84
|
-
): Address {
|
|
85
|
-
return forwarderAddress(
|
|
86
|
-
forwarder.factory,
|
|
87
|
-
forwarder.implementation,
|
|
88
|
-
treasury,
|
|
89
|
-
quoteSalt(account, quote.client_reference_id, quote.id),
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** A deposit address on the network whose treasury is `treasury`, which you pin yourself. */
|
|
94
|
-
export function depositAddress(
|
|
95
|
-
forwarder: Forwarder,
|
|
96
|
-
address: { livemode: boolean; client_reference_id: string; version: number },
|
|
97
|
-
treasury: string,
|
|
98
|
-
account: string,
|
|
99
|
-
): Address {
|
|
100
|
-
return forwarderAddress(
|
|
101
|
-
forwarder.factory,
|
|
102
|
-
forwarder.implementation,
|
|
103
|
-
treasury,
|
|
104
|
-
depositAddressSalt(account, address.livemode, address.client_reference_id, address.version),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** An address the service returned is not the one your pins derive: show nothing to pay. */
|
|
109
|
-
export class AddressMismatchError extends Error {
|
|
110
|
-
override readonly name = "AddressMismatchError";
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* What every address is recomputed from, configured on your server and never read from the
|
|
115
|
-
* service: your account id (`acct_…`), the forwarder `factory` and `implementation` of the
|
|
116
|
-
* attested deployment, and `treasuries`, your own treasury per chain id as you proved it. A
|
|
117
|
-
* compromised service could return another treasury with the address that really derives from
|
|
118
|
-
* it, so the response's `treasury` is never trusted.
|
|
119
|
-
*
|
|
120
|
-
* Live mode requires `treasuries`: without the chain's treasury the check fails closed. In test
|
|
121
|
-
* mode an unpinned chain falls back to the response's treasury with a console warning.
|
|
122
|
-
*/
|
|
123
|
-
export interface AddressPins extends Forwarder {
|
|
124
|
-
account: string;
|
|
125
|
-
treasuries?: Readonly<Record<number, string>> | undefined;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* The treasury an address on `chainId` must pay: the pinned one, which the response must name.
|
|
130
|
-
*/
|
|
131
|
-
function pinnedTreasury(
|
|
132
|
-
pins: AddressPins,
|
|
133
|
-
livemode: boolean,
|
|
134
|
-
chainId: number,
|
|
135
|
-
treasury: string,
|
|
136
|
-
where: string,
|
|
137
|
-
): string {
|
|
138
|
-
const pinned = pins.treasuries?.[chainId];
|
|
139
|
-
if (pinned === undefined) {
|
|
140
|
-
if (livemode) {
|
|
141
|
-
throw new AddressMismatchError(`${where}: live mode requires your pinned treasury of chain ${chainId}`);
|
|
142
|
-
}
|
|
143
|
-
console.warn(
|
|
144
|
-
`${where}: no pinned treasury of chain ${chainId}; the service's ${treasury} is trusted (test mode only)`,
|
|
145
|
-
);
|
|
146
|
-
return treasury;
|
|
147
|
-
}
|
|
148
|
-
if (!sameAddress(pinned, treasury)) {
|
|
149
|
-
throw new AddressMismatchError(`${where} pays a treasury that is not the pinned one`);
|
|
150
|
-
}
|
|
151
|
-
return pinned;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function requirePins(pins: AddressPins, where: string): void {
|
|
155
|
-
for (const field of ["account", "factory", "implementation"] as const) {
|
|
156
|
-
if (typeof pins[field] !== "string" || pins[field] === "") {
|
|
157
|
-
throw new AddressMismatchError(`${where}: the pinned ${field} is missing`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function sameAddress(a: string, b: string): boolean {
|
|
163
|
-
return a.toLowerCase() === b.toLowerCase();
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Recomputes an open quote's address from your pins and returns it, the `expectedAddress` to
|
|
168
|
-
* pass to `<Checkout>`. Throws `AddressMismatchError` unless the quote names your pinned
|
|
169
|
-
* treasury of its chain and its address derives from it; in live mode also when the chain has no
|
|
170
|
-
* pinned treasury.
|
|
171
|
-
*/
|
|
172
|
-
export function verifyQuoteAddress(
|
|
173
|
-
pins: AddressPins,
|
|
174
|
-
quote: {
|
|
175
|
-
id: string;
|
|
176
|
-
livemode: boolean;
|
|
177
|
-
chain_id: number;
|
|
178
|
-
treasury: string;
|
|
179
|
-
address: string;
|
|
180
|
-
client_reference_id: string;
|
|
181
|
-
},
|
|
182
|
-
): Address {
|
|
183
|
-
const where = `quote ${quote.id}`;
|
|
184
|
-
requirePins(pins, where);
|
|
185
|
-
const treasury = pinnedTreasury(pins, quote.livemode, quote.chain_id, quote.treasury, where);
|
|
186
|
-
const derived = quoteAddress(pins, quote, treasury, pins.account);
|
|
187
|
-
if (!sameAddress(derived, quote.address)) {
|
|
188
|
-
throw new AddressMismatchError(`${where} has an address the account cannot derive`);
|
|
189
|
-
}
|
|
190
|
-
return derived;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Recomputes every network of an active deposit address from your pins and returns the address,
|
|
195
|
-
* the same on every network. Throws `AddressMismatchError` as `verifyQuoteAddress` does, for any
|
|
196
|
-
* network.
|
|
197
|
-
*/
|
|
198
|
-
export function verifyDepositAddress(
|
|
199
|
-
pins: AddressPins,
|
|
200
|
-
address: {
|
|
201
|
-
id: string;
|
|
202
|
-
livemode: boolean;
|
|
203
|
-
client_reference_id: string;
|
|
204
|
-
version: number;
|
|
205
|
-
networks: readonly { chain_id: number; treasury: string; address: string }[];
|
|
206
|
-
},
|
|
207
|
-
): Address {
|
|
208
|
-
requirePins(pins, `deposit address ${address.id}`);
|
|
209
|
-
let first: Address | undefined;
|
|
210
|
-
for (const network of address.networks) {
|
|
211
|
-
const where = `deposit address ${address.id} on chain ${network.chain_id}`;
|
|
212
|
-
const treasury = pinnedTreasury(pins, address.livemode, network.chain_id, network.treasury, where);
|
|
213
|
-
const derived = depositAddress(pins, address, treasury, pins.account);
|
|
214
|
-
if (!sameAddress(derived, network.address)) {
|
|
215
|
-
throw new AddressMismatchError(`${where} is not one the account can derive`);
|
|
216
|
-
}
|
|
217
|
-
first ??= derived;
|
|
218
|
-
}
|
|
219
|
-
if (first === undefined) {
|
|
220
|
-
throw new AddressMismatchError(`deposit address ${address.id} has no network`);
|
|
221
|
-
}
|
|
222
|
-
return first;
|
|
223
|
-
}
|
package/src/server/index.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Server-side helpers of `@phala/pay`. Nothing here needs or takes an API key: webhook
|
|
3
|
-
* verification uses your account's public webhook key, and the address and sweep helpers run
|
|
4
|
-
* offline. Call the API itself from your backend with a restricted key (the Python SDK, or any
|
|
5
|
-
* HTTP client with `Authorization: Bearer ppay_rk_…`); never ship a key to a browser. Recompute
|
|
6
|
-
* every address with `verifyQuoteAddress` or `verifyDepositAddress` from the pins you configure
|
|
7
|
-
* yourself before you show it.
|
|
8
|
-
*/
|
|
9
|
-
export {
|
|
10
|
-
WebhookSignatureError,
|
|
11
|
-
constructEvent,
|
|
12
|
-
type ConstructEventOptions,
|
|
13
|
-
type WebhookEvent,
|
|
14
|
-
} from "./webhook.js";
|
|
15
|
-
export {
|
|
16
|
-
AddressMismatchError,
|
|
17
|
-
depositAddress,
|
|
18
|
-
depositAddressSalt,
|
|
19
|
-
forwarderAddress,
|
|
20
|
-
quoteSalt,
|
|
21
|
-
quoteAddress,
|
|
22
|
-
verifyDepositAddress,
|
|
23
|
-
verifyQuoteAddress,
|
|
24
|
-
type AddressPins,
|
|
25
|
-
type Forwarder,
|
|
26
|
-
} from "./addresses.js";
|
|
27
|
-
export {
|
|
28
|
-
batchChecksum,
|
|
29
|
-
flushTransaction,
|
|
30
|
-
flushTransactions,
|
|
31
|
-
safeBatch,
|
|
32
|
-
type BatchFile,
|
|
33
|
-
type BatchFileMeta,
|
|
34
|
-
type BatchTransaction,
|
|
35
|
-
type Call,
|
|
36
|
-
type ForwarderObject,
|
|
37
|
-
type SafeBatchOptions,
|
|
38
|
-
} from "./sweeps.js";
|