eth-twc-sdk-react 0.1.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 +279 -0
- package/hooks/clients.ts +12 -0
- package/hooks/configInfo.ts +33 -0
- package/hooks/index.ts +36 -0
- package/hooks/sdkConfig.ts +7 -0
- package/hooks/selfTransfer.ts +83 -0
- package/hooks/sign.ts +133 -0
- package/hooks/signatureTransfer.ts +110 -0
- package/hooks/verify.ts +67 -0
- package/index.ts +1 -0
- package/package.json +40 -0
- package/test/clients.test.ts +33 -0
- package/test/fixtures.ts +11 -0
- package/test/hooks.configInfo.test.ts +94 -0
- package/test/hooks.selfTransfer.test.ts +173 -0
- package/test/hooks.sign.test.ts +231 -0
- package/test/hooks.signatureTransfer.test.ts +198 -0
- package/test/hooks.verify.test.ts +230 -0
- package/test/preload.ts +81 -0
- package/test/queryClient.tsx +17 -0
- package/test/sdkConfigState.ts +33 -0
- package/test/stubs.ts +257 -0
- package/test/tsconfig.json +8 -0
- package/test/wagmiState.ts +18 -0
- package/tsconfig.json +31 -0
package/README.md
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
# eth-twc-sdk-react
|
|
2
|
+
|
|
3
|
+
[`eth-twc-sdk-js`](../sdk_js) を **wagmi v3** と **TanStack Query v5** 上で使うための React フック群です。**Vite / Next.js など、フロントのバンドラ+ React 18/19** を想定しています。
|
|
4
|
+
[docsはこちら](https://bunsan-pay.github.io/TransferWithCommitment/sdk-react)。
|
|
5
|
+
|
|
6
|
+
## 前提
|
|
7
|
+
|
|
8
|
+
アプリ側で次を用意してください。
|
|
9
|
+
|
|
10
|
+
1. **`WagmiProvider`**(チェーン・トランスポート・コネクタの設定)
|
|
11
|
+
2. **`QueryClientProvider`**(`@tanstack/react-query`)
|
|
12
|
+
3. **`commitment`(`bytes32`)** — `eth-twc-sdk-js` と同様、**コミットメントを導出するヘルパは提供しない**(外部プロトコルとのスキーマの相互運用性と、ハッシュ選択の単一障害点回避のため)。利用者は **暗号学的にランダムな `r`** と **安全なハッシュ `H`** を選び、少なくとも **`commitment = H(message || r)`**(`message` はアプリのスキーマに従うバイト列)とすることを推奨する。詳細は [`docs/sdk-js`](https://bunsan-pay.github.io/TransferWithCommitment/sdk-js) の「Commitment」の節を参照。
|
|
13
|
+
|
|
14
|
+
```tsx
|
|
15
|
+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
16
|
+
import { WagmiProvider } from "wagmi";
|
|
17
|
+
import { config } from "./wagmiConfig";
|
|
18
|
+
|
|
19
|
+
const queryClient = new QueryClient();
|
|
20
|
+
|
|
21
|
+
export function Providers({ children }: { children: React.ReactNode }) {
|
|
22
|
+
return (
|
|
23
|
+
<WagmiProvider config={config}>
|
|
24
|
+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
25
|
+
</WagmiProvider>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## インストール
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
bun add eth-twc-sdk-react eth-twc-sdk-js @tanstack/react-query wagmi viem
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
`eth-twc-sdk-react` は **peerDependencies** として `react`・`@tanstack/react-query`・`wagmi`・`viem` を要求します。アプリと同じインスタンスになるよう、バージョンはアプリ側に合わせてください。
|
|
37
|
+
|
|
38
|
+
## エクスポート
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
import {
|
|
42
|
+
useSelfBatchTransfer,
|
|
43
|
+
useSelfTransfer,
|
|
44
|
+
useSelfUnifiedTransfer,
|
|
45
|
+
useSendAuthorizedBatchTransfer,
|
|
46
|
+
useSendAuthorizedSingleTransfer,
|
|
47
|
+
useSendAuthorizedUnifiedTransfer,
|
|
48
|
+
useSendCancelAuthorization,
|
|
49
|
+
useSignBatchTransferWithCommit,
|
|
50
|
+
useSignCancelAuthorization,
|
|
51
|
+
useSignSingleTransfer,
|
|
52
|
+
useSignUniCommitTransfers,
|
|
53
|
+
type VerifyTransferArgs,
|
|
54
|
+
useTransferWithCommitmentSentLogs,
|
|
55
|
+
useVerifyTransfer,
|
|
56
|
+
transferWithCommitmentAddress,
|
|
57
|
+
useIsTransferWithCommitmentDeployed,
|
|
58
|
+
} from "eth-twc-sdk-react";
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## Usage(ユースケース別)
|
|
64
|
+
|
|
65
|
+
### コントラクトアドレス・デプロイ確認
|
|
66
|
+
|
|
67
|
+
- `transferWithCommitmentAddress` — **`eth-twc-sdk-js` のリリースビルドで固定された** CREATE2 アドレス
|
|
68
|
+
|
|
69
|
+
- `useIsTransferWithCommitmentDeployed`(別名 `useIsSupportedChain`)
|
|
70
|
+
|
|
71
|
+
接続中チェーンで canonical TWC に **`getCode` があるか**を示す hook
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import {
|
|
75
|
+
transferWithCommitmentAddress,
|
|
76
|
+
useIsTransferWithCommitmentDeployed,
|
|
77
|
+
} from "eth-twc-sdk-react";
|
|
78
|
+
|
|
79
|
+
export function NetworkBanner() {
|
|
80
|
+
const deployed = useIsTransferWithCommitmentDeployed();
|
|
81
|
+
return (
|
|
82
|
+
<div>
|
|
83
|
+
<p>Contract: {transferWithCommitmentAddress}</p>
|
|
84
|
+
<p>TWC deployed on current chain: {deployed ? "yes" : "no"}</p>
|
|
85
|
+
</div>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Self-Call で送金(mutation)
|
|
91
|
+
|
|
92
|
+
ウォレット接続済み・canonical TWC がデプロイ済みであることが前提です。`mutate` / `mutateAsync` に **`eth-twc-sdk-js/selfTransfer/*`** の入力型オブジェクトを渡します。
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import { useSelfTransfer } from "eth-twc-sdk-react";
|
|
96
|
+
import type { SelfTransferSingleArgs } from "eth-twc-sdk-js/selfTransfer/single";
|
|
97
|
+
|
|
98
|
+
export function SelfSendButton() {
|
|
99
|
+
const selfTransfer = useSelfTransfer();
|
|
100
|
+
|
|
101
|
+
async function onClick() {
|
|
102
|
+
const args: SelfTransferSingleArgs = {
|
|
103
|
+
/* ... */
|
|
104
|
+
};
|
|
105
|
+
const txHash = await selfTransfer.mutateAsync(args);
|
|
106
|
+
console.log("submitted", txHash);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<button
|
|
111
|
+
type="button"
|
|
112
|
+
disabled={selfTransfer.isPending}
|
|
113
|
+
onClick={() => void onClick()}
|
|
114
|
+
>
|
|
115
|
+
{selfTransfer.isPending ? "Sending…" : "Send (self-call)"}
|
|
116
|
+
</button>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 署名 → 署名済みトランザクション送信(2 段 mutation)
|
|
122
|
+
|
|
123
|
+
まず `useSignSingleTransfer` で署名済みペイロードを取得し、続けて `useSendAuthorizedSingleTransfer` で送信します(他パターンは `useSign*` / `useSendAuthorized*` の組み合わせを同様に)。
|
|
124
|
+
|
|
125
|
+
```tsx
|
|
126
|
+
import {
|
|
127
|
+
useSendAuthorizedSingleTransfer,
|
|
128
|
+
useSignSingleTransfer,
|
|
129
|
+
} from "eth-twc-sdk-react";
|
|
130
|
+
import type { SignatureTransferSingleArgs } from "eth-twc-sdk-js/signatureTransfer/single";
|
|
131
|
+
|
|
132
|
+
export function SignAndSend() {
|
|
133
|
+
const sign = useSignSingleTransfer();
|
|
134
|
+
const send = useSendAuthorizedSingleTransfer();
|
|
135
|
+
|
|
136
|
+
async function run() {
|
|
137
|
+
const args: SignatureTransferSingleArgs = {
|
|
138
|
+
/* ... */
|
|
139
|
+
};
|
|
140
|
+
const signed = await sign.mutateAsync(args);
|
|
141
|
+
const txHash = await send.mutateAsync(signed);
|
|
142
|
+
console.log(txHash);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<button
|
|
147
|
+
type="button"
|
|
148
|
+
disabled={sign.isPending || send.isPending}
|
|
149
|
+
onClick={() => void run()}
|
|
150
|
+
>
|
|
151
|
+
Sign and send
|
|
152
|
+
</button>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### トランザクション検証(query)
|
|
158
|
+
|
|
159
|
+
`txHash` と `verify` に渡す第 3 引数が揃ったときだけフェッチします。成功時の **`data` は `null`** で、失敗時のみ `error` が入ります。
|
|
160
|
+
|
|
161
|
+
```tsx
|
|
162
|
+
import { useVerifyTransfer } from "eth-twc-sdk-react";
|
|
163
|
+
import type { Hex } from "viem";
|
|
164
|
+
|
|
165
|
+
export function VerifyPanel({ txHash }: { txHash: Hex | undefined }) {
|
|
166
|
+
const args =
|
|
167
|
+
txHash === undefined
|
|
168
|
+
? undefined
|
|
169
|
+
: {
|
|
170
|
+
from: "0x…",
|
|
171
|
+
to: "0x…",
|
|
172
|
+
token: "0x…",
|
|
173
|
+
value: 1n,
|
|
174
|
+
commitment: "0x…",
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const q = useVerifyTransfer(txHash, args);
|
|
178
|
+
|
|
179
|
+
if (q.isPending) return <p>Verifying…</p>;
|
|
180
|
+
if (q.isError) return <p>Error: {q.error.message}</p>;
|
|
181
|
+
if (q.isSuccess) return <p>Transfer verified.</p>;
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### レシートからイベントログ取得(query)
|
|
187
|
+
|
|
188
|
+
```tsx
|
|
189
|
+
import { useTransferWithCommitmentSentLogs } from "eth-twc-sdk-react";
|
|
190
|
+
import type { Hex } from "viem";
|
|
191
|
+
|
|
192
|
+
export function EventLogs({ txHash }: { txHash: Hex | undefined }) {
|
|
193
|
+
const q = useTransferWithCommitmentSentLogs(txHash);
|
|
194
|
+
|
|
195
|
+
if (q.isPending) return <p>Loading logs…</p>;
|
|
196
|
+
if (q.isError) return <p>{q.error.message}</p>;
|
|
197
|
+
return <pre>{JSON.stringify(q.data, null, 2)}</pre>;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
## フック一覧(引数・戻り値)
|
|
204
|
+
|
|
205
|
+
型のうち `…` は TanStack Query の `UseMutationOptions` / `UseQueryOptions` で上書き可能なオプション(`mutationFn` / `queryFn` / `queryKey` を除く)を指します。
|
|
206
|
+
|
|
207
|
+
### 定数の再エクスポート(`eth-twc-sdk-js/config`)
|
|
208
|
+
|
|
209
|
+
| 名前 | 型 / 値 |
|
|
210
|
+
| ------------------------------- | ----------------------------------------------------------------- |
|
|
211
|
+
| `transferWithCommitmentAddress` | `` `0x${string}` `` — CREATE2 決定論アドレス(`twcConstants.ts`) |
|
|
212
|
+
| CREATE2 / EIP-712 定数 | JS SDK `config` と同じ再エクスポート |
|
|
213
|
+
|
|
214
|
+
### デプロイ確認(hook)
|
|
215
|
+
|
|
216
|
+
| フック | 引数 | 戻り値 |
|
|
217
|
+
| --------------------------------------------------------------------- | ---- | -------------------------------------------------------------------------------------------- |
|
|
218
|
+
| `useIsTransferWithCommitmentDeployed()`(別名 `useIsSupportedChain`) | なし | `boolean` — canonical TWC アドレスで `getCode` が非空なら `true`。クライアントなしは `false` |
|
|
219
|
+
|
|
220
|
+
### Self-Call 送信(`sdk_js/selfTransfer/*`)
|
|
221
|
+
|
|
222
|
+
| フック | 引数 | 戻り値 |
|
|
223
|
+
| ---------------------------------- | ---------- | -------------------------------------------------------- |
|
|
224
|
+
| `useSelfTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SelfTransferSingleArgs>` |
|
|
225
|
+
| `useSelfUnifiedTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SelfTransferUnifiedArgs>` |
|
|
226
|
+
| `useSelfBatchTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SelfTransferBatchArgs>` |
|
|
227
|
+
|
|
228
|
+
引数型は `eth-twc-sdk-js/selfTransfer/single|batch|unified` を参照。
|
|
229
|
+
|
|
230
|
+
### 署名済み送信(`sdk_js/signatureTransfer/*/sendTx`)
|
|
231
|
+
|
|
232
|
+
| フック | 引数 | 戻り値 |
|
|
233
|
+
| -------------------------------------------- | ---------- | ---------------------------------------------------------- |
|
|
234
|
+
| `useSendAuthorizedSingleTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SignedSingleTransfer>` |
|
|
235
|
+
| `useSendAuthorizedUnifiedTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SignedUnifiedTransfer>` |
|
|
236
|
+
| `useSendAuthorizedBatchTransfer(options?)` | `options?` | `UseMutationResult<Hex, Error, SignedBatchTransfer>` |
|
|
237
|
+
| `useSendCancelAuthorization(options?)` | `options?` | `UseMutationResult<Hex, Error, SignedCancelAuthorization>` |
|
|
238
|
+
|
|
239
|
+
署名済み型は **`eth-twc-sdk-js/signatureTransfer/<variant>`** の `Signed*` と `signedDataSchema` を参照。
|
|
240
|
+
|
|
241
|
+
### 署名(`sdk_js/signatureTransfer/*/sign.ts`)
|
|
242
|
+
|
|
243
|
+
| フック | 引数 | 戻り値 |
|
|
244
|
+
| ------------------------------------------ | ---------- | ------------------------------------------------------------------------------- |
|
|
245
|
+
| `useSignSingleTransfer(options?)` | `options?` | `UseMutationResult<SignedSingleTransfer, Error, SignatureTransferSingleArgs>` |
|
|
246
|
+
| `useSignUniCommitTransfers(options?)` | `options?` | `UseMutationResult<SignedUnifiedTransfer, Error, SignatureTransferUnifiedArgs>` |
|
|
247
|
+
| `useSignBatchTransferWithCommit(options?)` | `options?` | `UseMutationResult<SignedBatchTransfer, Error, SignatureTransferBatchArgs>` |
|
|
248
|
+
| `useSignCancelAuthorization(options?)` | `options?` | `UseMutationResult<SignedCancelAuthorization, Error, CancelAuthorizationArgs>` |
|
|
249
|
+
|
|
250
|
+
引数・戻り値の型は `eth-twc-sdk-js/signatureTransfer/<variant>` を参照。バッチでは `SignatureTransferBatchArgs` / `SignedBatchTransfer` に **`batchCommitment`** が含まれます。
|
|
251
|
+
|
|
252
|
+
### 検証・ログ(`sdk_js/verify`)
|
|
253
|
+
|
|
254
|
+
| フック | 引数 | 戻り値 |
|
|
255
|
+
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- |
|
|
256
|
+
| `useVerifyTransfer(txHash, args, options?)` | `txHash: Hex \| undefined` — 検証対象 Tx。`args: VerifyTransferArgs \| undefined` — `verify` の第3引数と同形。`options?` — `UseQueryOptions`(`queryKey` / `queryFn` 除く) | `UseQueryResult<null, Error>` — **成功時の `data` は `null`(検証失敗時のみ `error`)** |
|
|
257
|
+
| `useTransferWithCommitmentSentLogs(txHash, options?)` | `txHash: Hex \| undefined`。`options?` | `UseQueryResult<Log[], Error>` |
|
|
258
|
+
|
|
259
|
+
`VerifyTransferArgs` は `Parameters<typeof verify>[2]` と同じで、`eth-twc-sdk-js/verify` の `verify` に渡すオブジェクトと一致させてください。
|
|
260
|
+
|
|
261
|
+
`txHash` または `args` が欠ける場合、`useVerifyTransfer` はクエリを `enabled: false` にし、`txHash` だけ欠ける `useTransferWithCommitmentSentLogs` も同様です。
|
|
262
|
+
|
|
263
|
+
**書き込み系**(`useMutation`): `mutate(args)` または `mutateAsync(args)`。成功時 `data` はトランザクションハッシュ(`Hex`)。**読み取り系**(`useQuery`): TanStack Query の `data` / `error` / `isPending` などがそのまま使えます。ウォレット未接続・`publicClient` 未取得時は、mutation / 該当 query はエラーまたは `enabled: false` になります(上表および各サンプル参照)。
|
|
264
|
+
|
|
265
|
+
---
|
|
266
|
+
|
|
267
|
+
## 開発(このリポジトリ内)
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
bun install
|
|
271
|
+
bun run typecheck
|
|
272
|
+
bun test
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## 配布形態について
|
|
278
|
+
|
|
279
|
+
Vite / Next での利用を前提としているため、エントリは TypeScript ソース(`.ts`)です。トランスパイルしない環境では利用できません。
|
package/hooks/clients.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Hex, PublicClient, WalletClient } from "viem";
|
|
2
|
+
|
|
3
|
+
export function narrowWriteClients(
|
|
4
|
+
publicClient: PublicClient | undefined,
|
|
5
|
+
walletClient: WalletClient | undefined,
|
|
6
|
+
address: Hex | undefined,
|
|
7
|
+
): [PublicClient, WalletClient, Hex] {
|
|
8
|
+
if (!publicClient) throw new Error("TWC: public client is not available");
|
|
9
|
+
if (!walletClient) throw new Error("TWC: wallet client is not available");
|
|
10
|
+
if (!address) throw new Error("TWC: connect a wallet to send or sign");
|
|
11
|
+
return [publicClient, walletClient, address];
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useQuery } from "@tanstack/react-query";
|
|
4
|
+
import { transferWithCommitmentAddress } from "eth-twc-sdk-js/config";
|
|
5
|
+
import { usePublicClient } from "wagmi";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 接続中チェーンで、canonical TWC アドレスにコントラクトコードがあるか(`eth_getCode`)。
|
|
9
|
+
* クライアント未取得・読み込み中は `false`。
|
|
10
|
+
*/
|
|
11
|
+
export function useIsTransferWithCommitmentDeployed(): boolean {
|
|
12
|
+
const publicClient = usePublicClient();
|
|
13
|
+
const { data } = useQuery({
|
|
14
|
+
queryKey: [
|
|
15
|
+
"twc",
|
|
16
|
+
"deployed",
|
|
17
|
+
publicClient?.chain?.id ?? null,
|
|
18
|
+
transferWithCommitmentAddress,
|
|
19
|
+
],
|
|
20
|
+
queryFn: async () => {
|
|
21
|
+
if (!publicClient) return false;
|
|
22
|
+
const code = await publicClient.getCode({
|
|
23
|
+
address: transferWithCommitmentAddress,
|
|
24
|
+
});
|
|
25
|
+
return code !== undefined && code !== "0x";
|
|
26
|
+
},
|
|
27
|
+
enabled: Boolean(publicClient),
|
|
28
|
+
});
|
|
29
|
+
return data === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @deprecated Use {@link useIsTransferWithCommitmentDeployed}. */
|
|
33
|
+
export const useIsSupportedChain = useIsTransferWithCommitmentDeployed;
|
package/hooks/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export {
|
|
2
|
+
useSelfBatchTransfer,
|
|
3
|
+
useSelfTransfer,
|
|
4
|
+
useSelfUnifiedTransfer,
|
|
5
|
+
} from "./selfTransfer.ts";
|
|
6
|
+
export {
|
|
7
|
+
useSendAuthorizedBatchTransfer,
|
|
8
|
+
useSendAuthorizedSingleTransfer,
|
|
9
|
+
useSendAuthorizedUnifiedTransfer,
|
|
10
|
+
/** 別名: `UniCommitTransfers` 経路と同義 */
|
|
11
|
+
useSendAuthorizedUnifiedTransfer as useSendAuthorizedUniCommitTransfers,
|
|
12
|
+
useSendCancelAuthorization,
|
|
13
|
+
} from "./signatureTransfer.ts";
|
|
14
|
+
export {
|
|
15
|
+
useSignBatchTransferWithCommit,
|
|
16
|
+
useSignCancelAuthorization,
|
|
17
|
+
useSignSingleTransfer,
|
|
18
|
+
useSignUnifiedTransfer,
|
|
19
|
+
/** 別名: `UniCommitTransfers` EIP-712 と同義 */
|
|
20
|
+
useSignUnifiedTransfer as useSignUniCommitTransfers,
|
|
21
|
+
} from "./sign.ts";
|
|
22
|
+
export {
|
|
23
|
+
type VerifyTransferArgs,
|
|
24
|
+
useTransferWithCommitmentSentLogs,
|
|
25
|
+
useVerifyTransfer,
|
|
26
|
+
} from "./verify.ts";
|
|
27
|
+
export {
|
|
28
|
+
EIP712_DOMAIN_NAME,
|
|
29
|
+
EIP712_DOMAIN_VERSION,
|
|
30
|
+
TRANSFER_WITH_COMMITMENT_CREATE2_SALT,
|
|
31
|
+
transferWithCommitmentAddress,
|
|
32
|
+
} from "./sdkConfig.ts";
|
|
33
|
+
export {
|
|
34
|
+
useIsSupportedChain,
|
|
35
|
+
useIsTransferWithCommitmentDeployed,
|
|
36
|
+
} from "./configInfo.ts";
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
|
|
4
|
+
import type { Hex } from "viem";
|
|
5
|
+
import { sendTx as sendSelfBatch } from "eth-twc-sdk-js/selfTransfer/batch";
|
|
6
|
+
import { sendTx as sendSelfSingle } from "eth-twc-sdk-js/selfTransfer/single";
|
|
7
|
+
import { sendTx as sendSelfUnified } from "eth-twc-sdk-js/selfTransfer/unified";
|
|
8
|
+
import type { SelfTransferBatchArgs } from "eth-twc-sdk-js/selfTransfer/batch";
|
|
9
|
+
import type { SelfTransferSingleArgs } from "eth-twc-sdk-js/selfTransfer/single";
|
|
10
|
+
import type { SelfTransferUnifiedArgs } from "eth-twc-sdk-js/selfTransfer/unified";
|
|
11
|
+
import { useConnection, usePublicClient, useWalletClient } from "wagmi";
|
|
12
|
+
|
|
13
|
+
import { narrowWriteClients } from "./clients.ts";
|
|
14
|
+
|
|
15
|
+
export function useSelfTransfer(
|
|
16
|
+
options?: Omit<
|
|
17
|
+
UseMutationOptions<Hex, Error, SelfTransferSingleArgs>,
|
|
18
|
+
"mutationFn"
|
|
19
|
+
>,
|
|
20
|
+
) {
|
|
21
|
+
const publicClient = usePublicClient();
|
|
22
|
+
const { data: walletClient } = useWalletClient();
|
|
23
|
+
const { address } = useConnection();
|
|
24
|
+
|
|
25
|
+
return useMutation({
|
|
26
|
+
...options,
|
|
27
|
+
mutationFn: async (args: SelfTransferSingleArgs) => {
|
|
28
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
29
|
+
publicClient,
|
|
30
|
+
walletClient,
|
|
31
|
+
address,
|
|
32
|
+
);
|
|
33
|
+
return sendSelfSingle(pc, wc, addr, args);
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** UniCommitTransfers 経路と同じ `transfer([details], commitment)` */
|
|
39
|
+
export function useSelfUnifiedTransfer(
|
|
40
|
+
options?: Omit<
|
|
41
|
+
UseMutationOptions<Hex, Error, SelfTransferUnifiedArgs>,
|
|
42
|
+
"mutationFn"
|
|
43
|
+
>,
|
|
44
|
+
) {
|
|
45
|
+
const publicClient = usePublicClient();
|
|
46
|
+
const { data: walletClient } = useWalletClient();
|
|
47
|
+
const { address } = useConnection();
|
|
48
|
+
|
|
49
|
+
return useMutation({
|
|
50
|
+
...options,
|
|
51
|
+
mutationFn: async (args: SelfTransferUnifiedArgs) => {
|
|
52
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
53
|
+
publicClient,
|
|
54
|
+
walletClient,
|
|
55
|
+
address,
|
|
56
|
+
);
|
|
57
|
+
return sendSelfUnified(pc, wc, addr, args);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function useSelfBatchTransfer(
|
|
63
|
+
options?: Omit<
|
|
64
|
+
UseMutationOptions<Hex, Error, SelfTransferBatchArgs>,
|
|
65
|
+
"mutationFn"
|
|
66
|
+
>,
|
|
67
|
+
) {
|
|
68
|
+
const publicClient = usePublicClient();
|
|
69
|
+
const { data: walletClient } = useWalletClient();
|
|
70
|
+
const { address } = useConnection();
|
|
71
|
+
|
|
72
|
+
return useMutation({
|
|
73
|
+
...options,
|
|
74
|
+
mutationFn: async (args: SelfTransferBatchArgs) => {
|
|
75
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
76
|
+
publicClient,
|
|
77
|
+
walletClient,
|
|
78
|
+
address,
|
|
79
|
+
);
|
|
80
|
+
return sendSelfBatch(pc, wc, addr, args);
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
package/hooks/sign.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
|
|
4
|
+
import { sign as signBatch } from "eth-twc-sdk-js/signatureTransfer/batch";
|
|
5
|
+
import { sign as signCancel } from "eth-twc-sdk-js/signatureTransfer/cancelAuthorization";
|
|
6
|
+
import { sign as signSingle } from "eth-twc-sdk-js/signatureTransfer/single";
|
|
7
|
+
import { sign as signUnified } from "eth-twc-sdk-js/signatureTransfer/unified";
|
|
8
|
+
import type {
|
|
9
|
+
SignedBatchTransfer,
|
|
10
|
+
SignatureTransferBatchArgs,
|
|
11
|
+
} from "eth-twc-sdk-js/signatureTransfer/batch";
|
|
12
|
+
import type {
|
|
13
|
+
CancelAuthorizationArgs,
|
|
14
|
+
SignedCancelAuthorization,
|
|
15
|
+
} from "eth-twc-sdk-js/signatureTransfer/cancelAuthorization";
|
|
16
|
+
import type {
|
|
17
|
+
SignatureTransferSingleArgs,
|
|
18
|
+
SignedSingleTransfer,
|
|
19
|
+
} from "eth-twc-sdk-js/signatureTransfer/single";
|
|
20
|
+
import type {
|
|
21
|
+
SignatureTransferUnifiedArgs,
|
|
22
|
+
SignedUnifiedTransfer,
|
|
23
|
+
} from "eth-twc-sdk-js/signatureTransfer/unified";
|
|
24
|
+
import { useConnection, usePublicClient, useWalletClient } from "wagmi";
|
|
25
|
+
|
|
26
|
+
import { narrowWriteClients } from "./clients.ts";
|
|
27
|
+
|
|
28
|
+
export function useSignSingleTransfer(
|
|
29
|
+
options?: Omit<
|
|
30
|
+
UseMutationOptions<SignedSingleTransfer, Error, SignatureTransferSingleArgs>,
|
|
31
|
+
"mutationFn"
|
|
32
|
+
>,
|
|
33
|
+
) {
|
|
34
|
+
const publicClient = usePublicClient();
|
|
35
|
+
const { data: walletClient } = useWalletClient();
|
|
36
|
+
const { address } = useConnection();
|
|
37
|
+
|
|
38
|
+
return useMutation({
|
|
39
|
+
...options,
|
|
40
|
+
mutationFn: async (args: SignatureTransferSingleArgs) => {
|
|
41
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
42
|
+
publicClient,
|
|
43
|
+
walletClient,
|
|
44
|
+
address,
|
|
45
|
+
);
|
|
46
|
+
return signSingle(pc, wc, addr, args);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** UniCommitTransfers EIP-712(公開名 unified)を署名する */
|
|
52
|
+
export function useSignUnifiedTransfer(
|
|
53
|
+
options?: Omit<
|
|
54
|
+
UseMutationOptions<
|
|
55
|
+
SignedUnifiedTransfer,
|
|
56
|
+
Error,
|
|
57
|
+
SignatureTransferUnifiedArgs
|
|
58
|
+
>,
|
|
59
|
+
"mutationFn"
|
|
60
|
+
>,
|
|
61
|
+
) {
|
|
62
|
+
const publicClient = usePublicClient();
|
|
63
|
+
const { data: walletClient } = useWalletClient();
|
|
64
|
+
const { address } = useConnection();
|
|
65
|
+
|
|
66
|
+
return useMutation({
|
|
67
|
+
...options,
|
|
68
|
+
mutationFn: async (args: SignatureTransferUnifiedArgs) => {
|
|
69
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
70
|
+
publicClient,
|
|
71
|
+
walletClient,
|
|
72
|
+
address,
|
|
73
|
+
);
|
|
74
|
+
return signUnified(pc, wc, addr, args);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 別名保持: 旧 hooks 利用者の移行パスとして */
|
|
80
|
+
export const useSignUniCommitTransfers = useSignUnifiedTransfer;
|
|
81
|
+
|
|
82
|
+
export function useSignBatchTransfer(
|
|
83
|
+
options?: Omit<
|
|
84
|
+
UseMutationOptions<SignedBatchTransfer, Error, SignatureTransferBatchArgs>,
|
|
85
|
+
"mutationFn"
|
|
86
|
+
>,
|
|
87
|
+
) {
|
|
88
|
+
const publicClient = usePublicClient();
|
|
89
|
+
const { data: walletClient } = useWalletClient();
|
|
90
|
+
const { address } = useConnection();
|
|
91
|
+
|
|
92
|
+
return useMutation({
|
|
93
|
+
...options,
|
|
94
|
+
mutationFn: async (args: SignatureTransferBatchArgs) => {
|
|
95
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
96
|
+
publicClient,
|
|
97
|
+
walletClient,
|
|
98
|
+
address,
|
|
99
|
+
);
|
|
100
|
+
return signBatch(pc, wc, addr, args);
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** 別名保持: 旧名前 `BatchTransferWithCommit` と対応 */
|
|
106
|
+
export const useSignBatchTransferWithCommit = useSignBatchTransfer;
|
|
107
|
+
|
|
108
|
+
export function useSignCancelAuthorization(
|
|
109
|
+
options?: Omit<
|
|
110
|
+
UseMutationOptions<
|
|
111
|
+
SignedCancelAuthorization,
|
|
112
|
+
Error,
|
|
113
|
+
CancelAuthorizationArgs
|
|
114
|
+
>,
|
|
115
|
+
"mutationFn"
|
|
116
|
+
>,
|
|
117
|
+
) {
|
|
118
|
+
const publicClient = usePublicClient();
|
|
119
|
+
const { data: walletClient } = useWalletClient();
|
|
120
|
+
const { address } = useConnection();
|
|
121
|
+
|
|
122
|
+
return useMutation({
|
|
123
|
+
...options,
|
|
124
|
+
mutationFn: async (args: CancelAuthorizationArgs) => {
|
|
125
|
+
const [pc, wc, addr] = narrowWriteClients(
|
|
126
|
+
publicClient,
|
|
127
|
+
walletClient,
|
|
128
|
+
address,
|
|
129
|
+
);
|
|
130
|
+
return signCancel(pc, wc, addr, args);
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|