@naculus/connect-appkit-react 0.9.0 → 0.11.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 +57 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -88,6 +88,63 @@ authorizations and refreshes after `revoke(entry)`. Inspect the returned
|
|
|
88
88
|
verifies its settlement in the background. Pending verification retries five
|
|
89
89
|
times at four-second intervals by default; pass `verifyRetry` to change that.
|
|
90
90
|
|
|
91
|
+
### Pay x402 from the connected wallet
|
|
92
|
+
|
|
93
|
+
`useX402Signer()` reads the connected session from `Web3ConnectProvider` and
|
|
94
|
+
returns `{ signer, reason }`. Build the paying fetch with
|
|
95
|
+
`createX402Fetch({ signer })` from `@naculus/payments-x402`, then hand it to
|
|
96
|
+
`usePaymentFetch`. `usePaymentFetch` takes a paying fetch, not `null`, so
|
|
97
|
+
render the component that calls it only once `signer` exists:
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import type { PaymentFetch } from "@naculus/connect-appkit-core";
|
|
101
|
+
import { usePaymentFetch, useX402Signer } from "@naculus/connect-appkit-react";
|
|
102
|
+
import { createX402Fetch } from "@naculus/payments-x402";
|
|
103
|
+
import { useMemo } from "react";
|
|
104
|
+
|
|
105
|
+
const REASON_TEXT = {
|
|
106
|
+
"no-session": "Connect a wallet to pay.",
|
|
107
|
+
"not-evm": "Switch to an EVM account to pay with x402.",
|
|
108
|
+
"unsupported-wallet-type":
|
|
109
|
+
"This wallet cannot sign x402 payments here. Connect a browser wallet or use WalletConnect.",
|
|
110
|
+
} as const;
|
|
111
|
+
|
|
112
|
+
function PayWithWallet() {
|
|
113
|
+
const { signer, reason } = useX402Signer();
|
|
114
|
+
const pay = useMemo(
|
|
115
|
+
() => (signer ? createX402Fetch({ signer }) : null),
|
|
116
|
+
[signer],
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
if (!pay) return <p>{reason ? REASON_TEXT[reason] : null}</p>;
|
|
120
|
+
return <PaidResource pay={pay} />;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function PaidResource({ pay }: { pay: PaymentFetch }) {
|
|
124
|
+
const { payFetch, isPending, lastPayment, error } = usePaymentFetch(pay);
|
|
125
|
+
// Call payFetch(url) from an event handler; render isPending, lastPayment
|
|
126
|
+
// and error.
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`reason` is `null` whenever `signer` is set. Otherwise it says why there is no
|
|
132
|
+
signer, and what to show for it:
|
|
133
|
+
|
|
134
|
+
| `reason` | Meaning | Show |
|
|
135
|
+
|----------|---------|------|
|
|
136
|
+
| `"no-session"` | No wallet is connected. | The connect button. |
|
|
137
|
+
| `"not-evm"` | The session has no EVM (`eip155`) account, for example a Solana-only wallet. | Ask for an EVM account or an EVM wallet. |
|
|
138
|
+
| `"unsupported-wallet-type"` | The session is not an injected (EIP-6963) or WalletConnect wallet, or its provider is no longer available. Embedded and passkey EVM wallets land here. | Offer a browser wallet or WalletConnect instead. |
|
|
139
|
+
|
|
140
|
+
The signer switches the wallet to the chain the server asks for before it
|
|
141
|
+
signs, so a wallet on another EVM chain is not a `reason`.
|
|
142
|
+
|
|
143
|
+
WalletConnect sessions are supported: the hook signs through the WalletConnect
|
|
144
|
+
connector. The injected (EIP-6963) path is the one verified end to end with
|
|
145
|
+
real wallets; test WalletConnect against the wallets you target before you rely
|
|
146
|
+
on it.
|
|
147
|
+
|
|
91
148
|
### Ask before executing
|
|
92
149
|
|
|
93
150
|
```tsx
|