-
Notifications
You must be signed in to change notification settings - Fork 53
/
App.tsx
170 lines (160 loc) · 4.96 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
import {
ConnectButton,
useAccountBalance,
useWallet,
SuiChainId,
ErrorCode,
} from "@suiet/wallet-kit";
import { TransactionBlock } from "@mysten/sui.js/transactions";
import { useMemo } from "react";
const sampleNft = new Map([
[
"sui:devnet",
"0xe146dbd6d33d7227700328a9421c58ed34546f998acdc42a1d05b4818b49faa2::nft::mint",
],
[
"sui:testnet",
"0x5ea6aafe995ce6506f07335a40942024106a57f6311cb341239abf2c3ac7b82f::nft::mint",
],
[
"sui:mainnet",
"0x5b45da03d42b064f5e051741b6fed3b29eb817c7923b83b92f37a1d2abf4fbab::nft::mint",
],
]);
function App() {
const wallet = useWallet();
const { balance } = useAccountBalance();
const nftContractAddr = useMemo(() => {
if (!wallet.chain) return "";
return sampleNft.get(wallet.chain.id) ?? "";
}, [wallet]);
function uint8arrayToHex(value: Uint8Array | undefined) {
if (!value) return "";
// @ts-ignore
return value.toString("hex");
}
async function handleExecuteMoveCall(target: string | undefined) {
if (!target) return;
try {
const tx = new TransactionBlock();
tx.moveCall({
target: target as any,
arguments: [
tx.pure("Suiet NFT"),
tx.pure("Suiet Sample NFT"),
tx.pure(
"https://xc6fbqjny4wfkgukliockypoutzhcqwjmlw2gigombpp2ynufaxa.arweave.net/uLxQwS3HLFUailocJWHupPJxQsli7aMgzmBe_WG0KC4"
),
],
});
const resData = await wallet.signAndExecuteTransactionBlock({
transactionBlock: tx,
});
console.log("executeMoveCall success", resData);
alert("executeMoveCall succeeded (see response in the console)");
} catch (e) {
console.error("executeMoveCall failed", e);
alert("executeMoveCall failed (see response in the console)");
}
}
async function handleSignMsg() {
if (!wallet.account) return;
try {
const msg = "Hello world!";
const msgBytes = new TextEncoder().encode(msg);
const result = await wallet.signPersonalMessage({
message: msgBytes,
});
const verifyResult = await wallet.verifySignedMessage(
result,
wallet.account.publicKey
);
console.log("verify signedMessage", verifyResult);
if (!verifyResult) {
alert(`signMessage succeed, but verify signedMessage failed`);
} else {
alert(`signMessage succeed, and verify signedMessage succeed!`);
}
} catch (e) {
console.error("signMessage failed", e);
alert("signMessage failed (see response in the console)");
}
}
const chainName = (chainId: string | undefined) => {
switch (chainId) {
case SuiChainId.MAIN_NET:
return "Mainnet";
case SuiChainId.TEST_NET:
return "Testnet";
case SuiChainId.DEV_NET:
return "Devnet";
default:
return "Unknown";
}
};
return (
<div className="App">
<div>
<a href="https://vitejs.dev" target="_blank" rel={"noreferrer"}>
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a
href="https://github.com/suiet/wallet-kit"
target="_blank"
rel={"noreferrer"}
>
<img src={"/suiet-logo.svg"} className="logo" alt="Suiet logo" />
</a>
</div>
<h1>Vite + Suiet Kit</h1>
<div className="card">
<ConnectButton
onConnectError={(error) => {
if (error.code === ErrorCode.WALLET__CONNECT_ERROR__USER_REJECTED) {
console.warn(
"user rejected the connection to " + error.details?.wallet
);
} else {
console.warn("unknown connect error: ", error);
}
}}
/>
{!wallet.connected ? (
<p>Connect DApp with Suiet wallet from now!</p>
) : (
<div>
<div>
<p>current wallet: {wallet.adapter?.name}</p>
<p>
wallet status:{" "}
{wallet.connecting
? "connecting"
: wallet.connected
? "connected"
: "disconnected"}
</p>
<p>wallet address: {wallet.account?.address}</p>
<p>current network: {wallet.chain?.name}</p>
<p>wallet balance: {String(balance)} SUI</p>
<p>
wallet publicKey: {uint8arrayToHex(wallet.account?.publicKey)}
</p>
</div>
<div className={"btn-group"} style={{ margin: "8px 0" }}>
{nftContractAddr && (
<button onClick={() => handleExecuteMoveCall(nftContractAddr)}>
Mint {chainName(wallet.chain?.id)} NFT
</button>
)}
<button onClick={handleSignMsg}>signMessage</button>
</div>
</div>
)}
</div>
<p className="read-the-docs">
Click on the Vite and Suiet logos to learn more
</p>
</div>
);
}
export default App;