-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathfarcaster-domain-account-association-dialog.tsx
263 lines (245 loc) · 8.03 KB
/
farcaster-domain-account-association-dialog.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
constructJSONFarcasterSignatureAccountAssociationPaylod,
sign,
type SignResult,
} from "frames.js/farcaster-v2/json-signature";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { useAccount, useSignMessage, useSwitchChain } from "wagmi";
import { FormEvent, useCallback, useRef, useState } from "react";
import { CopyIcon, CopyCheckIcon, CopyXIcon, Loader2Icon } from "lucide-react";
import { z } from "zod";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { useFarcasterIdentity } from "../hooks/useFarcasterIdentity";
import { optimism } from "viem/chains";
import { useToast } from "@/components/ui/use-toast";
import { useCopyToClipboard } from "../hooks/useCopyToClipboad";
type FarcasterDomainAccountAssociationDialogProps = {
onClose: () => void;
};
export function FarcasterDomainAccountAssociationDialog({
onClose,
}: FarcasterDomainAccountAssociationDialogProps) {
const domainInputRef = useRef<HTMLInputElement>(null);
const copyCompact = useCopyToClipboard();
const copyJSON = useCopyToClipboard();
const account = useAccount();
const { toast } = useToast();
const farcasterSigner = useFarcasterIdentity();
const { switchChainAsync } = useSwitchChain();
const { signMessageAsync } = useSignMessage();
const [isGenerating, setIsGenerating] = useState(false);
const [associationResult, setAssociationResult] = useState<SignResult | null>(
null
);
const handleSubmit = useCallback(
async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const data = Object.fromEntries(new FormData(event.currentTarget));
try {
if (farcasterSigner.signer?.status !== "approved") {
throw new Error("Farcaster signer is not approved");
}
if (!account.address) {
throw new Error("Account address is not available");
}
const parser = z.object({
domain: z
.preprocess((val) => {
if (typeof val === "string") {
// prepend with prefix because normally it is the domain but we want to validate
// it is in valid format
return `http://${val}`;
}
return val;
}, z.string().url("Invalid domain"))
// remove the protocol prefix
.transform((val) => val.substring(7)),
});
const parseResult = parser.safeParse(data);
if (!parseResult.success) {
parseResult.error.errors.map((error) => {
domainInputRef.current?.setCustomValidity(error.message);
});
event.currentTarget.reportValidity();
return;
}
domainInputRef.current?.setCustomValidity("");
event.currentTarget.reportValidity();
setIsGenerating(true);
await switchChainAsync({
chainId: optimism.id,
});
const result = await sign({
fid: farcasterSigner.signer.fid,
payload: constructJSONFarcasterSignatureAccountAssociationPaylod(
parseResult.data.domain
),
signer: {
type: "custody",
custodyAddress: account.address,
},
signMessage(message) {
return signMessageAsync({
message,
});
},
});
setAssociationResult(result);
} catch (e) {
console.error(e);
toast({
title: "An error occurred",
description: "Please check the console for more information",
variant: "destructive",
});
} finally {
setIsGenerating(false);
}
},
[
account.address,
farcasterSigner.signer,
signMessageAsync,
switchChainAsync,
toast,
]
);
return (
<Dialog
open
onOpenChange={(isOpen) => {
if (!isOpen) {
onClose();
}
}}
>
<DialogContent>
<DialogHeader>
<DialogTitle>Domain Account Association</DialogTitle>
</DialogHeader>
{!associationResult && (
<form
className="flex flex-col gap-2"
id="domain-account-association-form"
onSubmit={handleSubmit}
noValidate
>
<Label htmlFor="domain">Domain</Label>
<Input
id="domain"
name="domain"
required
type="text"
ref={domainInputRef}
/>
<span className="text-muted-foreground text-sm">
A domain of your frame, e.g. for https://framesjs.org the domain
is framesjs.org, for http://localhost:3000 the domain is
localhost.
</span>
</form>
)}
{associationResult && (
<div className="flex flex-col gap-4">
<div>
<Label htmlFor="domain-account-association-form-compact-signature">
Compact signature
</Label>
<div className="flex w-full items-center space-x-2">
<Input
id="domain-account-association-form-compact-signature"
readOnly
value={associationResult.compact}
/>
<Button
onClick={() => {
copyCompact.copyToClipboard(associationResult.compact);
}}
size="icon"
type="button"
variant="secondary"
>
{copyCompact.copyState === "idle" && <CopyIcon size={14} />}
{copyCompact.copyState === "copied" && (
<CopyCheckIcon size={14} />
)}
{copyCompact.copyState === "failed" && (
<CopyXIcon size={14} />
)}
</Button>
</div>
</div>
<div>
<Label htmlFor="domain-account-association-form-json-signature">
JSON
</Label>
<div className="relative">
<textarea
className="p-2 bg-gray-100 rounded-md font-mono w-full resize-none text-sm"
readOnly
name="json"
rows={5}
id="domain-account-association-form-json-signature"
value={JSON.stringify(associationResult.json, null, 2)}
></textarea>
<Button
className="absolute top-0 right-0 p-2"
onClick={() => {
copyJSON.copyToClipboard(
JSON.stringify(associationResult.json, null, 2)
);
}}
size="icon"
type="button"
variant="ghost"
>
{copyJSON.copyState === "idle" && <CopyIcon size={14} />}
{copyJSON.copyState === "copied" && (
<CopyCheckIcon size={14} />
)}
{copyJSON.copyState === "failed" && <CopyXIcon size={14} />}
</Button>
</div>
</div>
</div>
)}
<DialogFooter>
{associationResult && (
<Button
onClick={() => {
setAssociationResult(null);
}}
type="button"
>
Reset
</Button>
)}
{!associationResult && (
<Button
disabled={isGenerating}
form="domain-account-association-form"
type="submit"
>
{isGenerating ? (
<>
<Loader2Icon className="animate-spin mr-2" />
Generating...
</>
) : (
"Generate"
)}
</Button>
)}
</DialogFooter>
</DialogContent>
</Dialog>
);
}