-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
306 lines (279 loc) · 8.45 KB
/
App.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import "@rainbow-me/rainbowkit/styles.css";
import {
ConnectButton,
getDefaultWallets,
RainbowKitProvider,
} from "@rainbow-me/rainbowkit";
import { EVMAccountAddress, Signature, EChain } from "@snickerdoodlelabs/objects";
import { SnickerdoodleWebIntegration } from "@snickerdoodlelabs/web-integration";
import React, { useState } from "react";
import {
configureChains,
createConfig,
WagmiConfig,
useAccount,
useSignMessage,
useSignTypedData,
} from "wagmi";
import {
arbitrum,
optimism,
mainnet,
polygon,
avalanche,
avalancheFuji,
} from "wagmi/chains";
import { publicProvider } from "wagmi/providers/public";
import { useEthersSigner } from "./ethers";
import snickerdoodle_logo from "./snickerdoodle_logo.png";
import "./App.css";
// ----------------- Check for Wallet Connect Project ID ------------------
console.log(process.env.REACT_APP_WALLET_CONNECT_PROJECT_ID);
if (!process.env.REACT_APP_WALLET_CONNECT_PROJECT_ID) {
throw new Error(
"You need to provide a WalletConnect Project ID env variable",
);
}
const projectId = process.env.REACT_APP_WALLET_CONNECT_PROJECT_ID;
// -------------------------- WAGMI Configuration
const chains = [arbitrum, optimism, mainnet, polygon, avalanche, avalancheFuji];
const { publicClient } = configureChains(chains, [publicProvider()]);
const { connectors } = getDefaultWallets({
appName: "Snickerdoodle Testbed",
projectId: projectId,
chains,
});
const wagmiConfig = createConfig({
autoConnect: true,
connectors,
publicClient,
});
// ----------------------------------------------------------------------------------
// if you choose to rely on default API keys, use this config instead
/*
const webIntegrationConfig = {};
*/
const webIntegrationConfig = {
primaryInfuraKey: process.env.REACT_APP_INFURA_API_KEY!,
ankrApiKey: process.env.REACT_APP_ANKR_API_KEY!,
covalentApiKey: process.env.REACT_APP_COVALENT_API_KEY!,
poapApiKey: process.env.REACT_APP_POAP_API_KEY!,
};
// -------------------------------------------------------------------------------------
function App() {
const [isInit, setIsInit] = useState<boolean>(false);
return (
<>
<WagmiConfig config={wagmiConfig}>
<RainbowKitProvider chains={chains} initialChain={mainnet}>
<div className="App">
<header className="App-header">
<a
className="App-link"
href="https://github.com/SnickerdoodleLabs/Testbed-React/tree/main"
target="_blank"
rel="noopener noreferrer"
>
Snickerdoodle Labs React Testbed
</a>
<img src={snickerdoodle_logo} className="App-logo" alt="logo" />
<ConnectButton />
<LetSnickerdoodleSign isInit={isInit} setIsInit={setIsInit} />
<AskToSimpleSign isInit={isInit} setIsInit={setIsInit} />
<AskToSignTypedData isInit={isInit} setIsInit={setIsInit} />
</header>
</div>
</RainbowKitProvider>
</WagmiConfig>
</>
);
}
function LetSnickerdoodleSign(props: {
isInit: boolean;
setIsInit: React.Dispatch<React.SetStateAction<boolean>>;
}) {
// see /src/ethers.ts for conversion from wagmi signer to ethers signer object
const ethersSigner = useEthersSigner();
const { isConnected } = useAccount();
// This shows how to authenticate the user's account with an Ethers signer
// This option will present the user with a Snickerdoodle controlled personal sign message
function handleOnClick() {
// don't call this logic if the integration has been initialized already in another component
if (!props.isInit) {
props.setIsInit(true);
const webIntegration = new SnickerdoodleWebIntegration(
webIntegrationConfig,
ethersSigner,
);
webIntegration.initialize();
}
}
if (isConnected && ethersSigner && !props.isInit) {
return (
<button className="button-64" onClick={handleOnClick}>
Let Snickerdoodle Sign
</button>
);
} else if (isConnected && props.isInit) {
return <div>Snickerdoodle Initialized!</div>;
} else {
return <></>;
}
}
function AskToSimpleSign(props: {
isInit: boolean;
setIsInit: React.Dispatch<React.SetStateAction<boolean>>;
}) {
const myMessage: string = "Hello Snickerdoodle!"; // you app's login message
const { data, isError, isSuccess, signMessage } = useSignMessage({
message: myMessage,
});
const { address, isConnected } = useAccount();
// This option shows how to authenticate a user account with a custom EIP-191
// compatible message signature that your app may already be asking the user to sign
React.useEffect(() => {
if (isConnected && address && data && !props.isInit) {
// don't call this logic if the integration has been initialized already in another component
props.setIsInit(true);
const webIntegration = new SnickerdoodleWebIntegration(
webIntegrationConfig,
);
webIntegration
.initialize()
.andThen((proxy) => {
return proxy.account.addAccountWithExternalSignature(
EVMAccountAddress(address),
myMessage,
Signature(data),
EChain.EthereumMainnet,
);
})
.mapErr((err) => {
console.log(err);
});
}
}, [isConnected, address, data]);
React.useEffect(() => {
if (data) {
console.log("Full Signature String: " + data);
}
}, [data]);
if (isConnected && !props.isInit) {
return (
<button className="button-64" onClick={() => signMessage()}>
Personal Sign
</button>
);
} else if (isConnected && props.isInit) {
return (
<>
{isSuccess && (
<div>
Signature:{" "}
{data?.slice(0, 12) +
"..." +
data?.slice(data.length - 13, data.length - 1)}
</div>
)}
{isError && <div>Error signing message</div>}
</>
);
} else {
return <></>;
}
}
function AskToSignTypedData(props: {
isInit: boolean;
setIsInit: React.Dispatch<React.SetStateAction<boolean>>;
}) {
const { address, isConnected } = useAccount();
const domain = {
// your app's EIP-712 domain
name: "Snickerdoodle",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
} as const;
const types = {
// your app's EIP-712 custom types
Login: [
{ name: "Contents", type: "string" },
{ name: "Nonce", type: "uint256" },
],
};
const message = {
// your app's EIP-712 custom typed message
Contents: "Hello Snickerdoodle!",
Nonce: BigInt(123),
};
const { data, isError, isLoading, isSuccess, signTypedData } =
useSignTypedData({
domain,
message,
primaryType: "Login",
types,
});
// This option shows how to authenticate a user's account with a EIP-712 signature
// that your application may already be requiring the user to sign
React.useEffect(() => {
if (
isConnected &&
address &&
domain &&
types &&
message &&
data &&
!props.isInit
) {
// don't call this logic if the integration has been initialized already in another component
props.setIsInit(true);
const webIntegration = new SnickerdoodleWebIntegration(
webIntegrationConfig,
);
webIntegration
.initialize()
.andThen((proxy) => {
return proxy.account.addAccountWithExternalTypedDataSignature(
EVMAccountAddress(address),
domain,
types,
message,
Signature(data),
EChain.EthereumMainnet,
);
})
.mapErr((err) => {
console.log(err);
});
}
}, [isConnected, address, domain, types, message, data]);
React.useEffect(() => {
if (data) {
console.log("Full Signature String: " + data);
}
}, [data]);
if (isConnected && !props.isInit) {
return (
<button className="button-64" onClick={() => signTypedData()}>
Sign Typed Data
</button>
);
} else if (isConnected && props.isInit) {
return (
<>
{isSuccess && (
<div>
Signature:{" "}
{data?.slice(0, 12) +
"..." +
data?.slice(data.length - 13, data.length - 1)}
</div>
)}
{isError && <div>Error signing message</div>}
</>
);
} else {
return <></>;
}
}
export default App;