Skip to content

Commit 8fc5e8d

Browse files
Merge pull request #2163 from Web3Auth/feat/vue-solana-composables
vue solana composables + vue wagmi
2 parents a4bd138 + 7251a25 commit 8fc5e8d

38 files changed

+3763
-417
lines changed

demo/vue-app-new/package-lock.json

Lines changed: 2685 additions & 199 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/vue-app-new/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
},
1212
"dependencies": {
1313
"@solana/web3.js": "^1.98.0",
14+
"@tanstack/vue-query": "^5.74.5",
1415
"@toruslabs/base-controllers": "^8.3.3",
1516
"@toruslabs/bs58": "^1.0.0",
1617
"@toruslabs/ethereum-controllers": "^8.3.4",
1718
"@toruslabs/solana-controllers": "^8.3.3",
1819
"@toruslabs/tweetnacl-js": "^1.0.4",
1920
"@toruslabs/vue-components": "^8.0.6",
2021
"@toruslabs/vue-icons": "^8.0.2",
22+
"@wagmi/vue": "^0.1.15",
2123
"@web3auth/auth": "^10.4.0",
2224
"@web3auth/modal": "file:../../packages/modal",
2325
"@web3auth/no-modal": "file:../../packages/no-modal",

demo/vue-app-new/src/MainView.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "@web3auth/modal";
1818
1919
import { type Web3AuthContextConfig, Web3AuthProvider } from "@web3auth/modal/vue";
20+
import { WagmiProvider } from "@web3auth/modal/vue/wagmi";
2021
import { coinbaseConnector } from "@web3auth/no-modal/connectors/coinbase-connector";
2122
import { computed, onBeforeMount, ref, watch } from "vue";
2223
@@ -164,7 +165,7 @@ const modalParams = computed(() => {
164165
label: "auth",
165166
loginMethods: loginMethodsConfig.value,
166167
},
167-
} as Record<WALLET_CONNECTOR_TYPE, ModalConfig>;
168+
};
168169
return modalConfig;
169170
});
170171
@@ -227,12 +228,14 @@ const configs = computed<Web3AuthContextConfig>(() => {
227228

228229
<template>
229230
<Web3AuthProvider :config="configs">
230-
<AppHeader />
231-
<div class="flex flex-col items-center justify-center">
232-
<main class="relative flex flex-col lg:h-[calc(100dvh_-_110px)]">
231+
<WagmiProvider>
232+
<AppHeader />
233+
<div class="flex flex-col items-center justify-center">
234+
<main class="relative flex flex-col lg:h-[calc(100dvh_-_110px)]">
233235
<AppSettings />
234236
<AppDashboard :chains="options.chains || []" />
235-
</main>
236-
</div>
237+
</main>
238+
</div>
239+
</WagmiProvider>
237240
</Web3AuthProvider>
238241
</template>

demo/vue-app-new/src/components/AppDashboard.vue

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,28 @@
22
import { Button, Card } from "@toruslabs/vue-components";
33
import { CHAIN_NAMESPACES, IProvider, log, WALLET_CONNECTORS, WALLET_PLUGINS } from "@web3auth/modal";
44
import { useCheckout, useEnableMFA, useIdentityToken, useManageMFA, useSwitchChain, useWalletConnectScanner, useWalletUI, useWeb3Auth, useWeb3AuthUser } from "@web3auth/modal/vue";
5-
import { CustomChainConfig, type NFTCheckoutPluginType, type WalletServicesPluginType } from "@web3auth/no-modal";
5+
import { type CustomChainConfig, type NFTCheckoutPluginType } from "@web3auth/no-modal";
66
import { useI18n } from "petite-vue-i18n";
77
8-
import { Connection } from "@solana/web3.js";
8+
import { useSignAndSendTransaction, useSignMessage as useSolanaSignMessage, useSignTransaction, useSolanaWallet } from "@web3auth/modal/vue/solana"
9+
import { useAccount, useBalance, useChainId, useSignMessage, useSignTypedData } from "@wagmi/vue";
10+
11+
import { LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } from "@solana/web3.js";
912
import { ProviderConfig } from "@toruslabs/base-controllers";
1013
import { SUPPORTED_NETWORKS } from "@toruslabs/ethereum-controllers";
1114
import { computed, ref, watch } from "vue";
1215
import { NFT_CHECKOUT_CONTRACT_ID } from "../config";
1316
import {
14-
getAccounts,
15-
getBalance,
16-
getChainId,
1717
getPrivateKey,
1818
sendEth,
19-
signEthMessage,
2019
signTransaction as signEthTransaction,
21-
signPersonalMessage,
22-
signTypedMessage,
2320
} from "../services/ethHandlers";
2421
import {
22+
getConnection,
2523
getBalance as getSolBalance,
2624
getPrivateKey as getSolPrivateKey,
2725
signAllTransactions,
28-
signAndSendTransaction,
29-
signMessage as signSolMessage,
30-
signTransaction as signSolTransaction,
3126
} from "../services/solHandlers";
32-
// import {
33-
// walletSendEth,
34-
// walletSignPersonalMessage,
35-
// walletSignSolanaMessage,
36-
// walletSignSolanaVersionedTransaction,
37-
// walletSignTypedMessage,
38-
// } from "../services/walletServiceHandlers";
3927
import { formDataStore } from "../store/form";
4028
import { SOLANA_SUPPORTED_NETWORKS } from "../utils/constants";
4129
@@ -58,13 +46,22 @@ const { showWalletUI, loading: showWalletUILoading } = useWalletUI();
5846
const { showWalletConnectScanner, loading: showWalletConnectScannerLoading } = useWalletConnectScanner();
5947
const { showCheckout, loading: showCheckoutLoading } = useCheckout();
6048
const { authenticateUser, loading: authenticateUserLoading } = useIdentityToken();
49+
const { status, address } = useAccount();
50+
const { signTypedDataAsync } = useSignTypedData();
51+
const { signMessageAsync } = useSignMessage();
52+
const chainId = useChainId();
53+
const balance = useBalance({
54+
address: address.value,
55+
});
56+
57+
const { accounts: solanaAccounts } = useSolanaWallet()
58+
const { signMessage: signSolanaMessage } = useSolanaSignMessage();
59+
const { signTransaction: signSolTransaction } = useSignTransaction();
60+
const { signAndSendTransaction } = useSignAndSendTransaction();
6161
6262
const currentChainId = ref<string | undefined>(web3Auth.value?.currentChain?.chainId);
6363
const currentChainConfig = computed(() => supportedNetworks[currentChainId.value as keyof typeof supportedNetworks]);
6464
const currentChainNamespace = computed(() => currentChainConfig.value?.chainNamespace);
65-
const connection = computed(() => {
66-
return currentChainConfig?.value ? new Connection(currentChainConfig?.value.rpcTarget) : null;
67-
});
6865
6966
const chainChangedListener = (chainId: string) => {
7067
currentChainId.value = chainId;
@@ -142,28 +139,9 @@ const printToConsole = (...args: unknown[]) => {
142139
}
143140
};
144141
145-
// const onWalletSignPersonalMessage = async () => {
146-
// const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
147-
// await walletSignPersonalMessage(walletPlugin.wsEmbedInstance.provider, printToConsole);
148-
// };
149-
// const onWalletSignTypedData_v4 = async () => {
150-
// const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
151-
// await walletSignTypedMessage(walletPlugin.wsEmbedInstance.provider, printToConsole);
152-
// };
153-
// const onWalletSendEth = async () => {
154-
// const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
155-
// await walletSendEth(walletPlugin.wsEmbedInstance.provider, printToConsole);
156-
// };
157-
158-
// const onWalletSignSolanaMessage = async () => {
159-
// const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
160-
// await walletSignSolanaMessage(walletPlugin.wsEmbedInstance.provider as IProvider, printToConsole);
161-
// };
162-
163-
// const onWalletSignSolanaVersionedTransaction = async () => {
164-
// const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPluginType;
165-
// await walletSignSolanaVersionedTransaction(walletPlugin.wsEmbedInstance.provider as IProvider, connection.value as Connection, printToConsole);
166-
// };
142+
watch(status, (newStatus) => {
143+
console.log("wagmi status", newStatus);
144+
}, { immediate: true });
167145
168146
// NFT Checkout
169147
const showPaidMintNFTCheckout = async () => {
@@ -190,35 +168,66 @@ const onSendEth = async () => {
190168
};
191169
192170
const onSignEthMessage = async () => {
193-
await signEthMessage(provider.value as IProvider, printToConsole);
171+
const result = await signMessageAsync({
172+
message: "Hello, Bob!",
173+
});
174+
printToConsole("result", result);
194175
};
195176
196177
const onGetAccounts = async () => {
197-
await getAccounts(provider.value as IProvider, printToConsole);
178+
printToConsole('account', address.value);
198179
};
199180
200181
const onGetPrivateKey = async () => {
201182
await getPrivateKey(provider.value as IProvider, printToConsole);
202183
};
203184
204185
const getConnectedChainId = async () => {
205-
await getChainId(provider.value as IProvider, printToConsole);
186+
printToConsole('chainId', chainId.value);
206187
};
207188
208189
const onGetBalance = async () => {
209-
await getBalance(provider.value as IProvider, printToConsole);
190+
printToConsole("balance", balance.data.value?.formatted);
210191
};
211192
212193
const onSignEthTransaction = async () => {
213194
await signEthTransaction(provider.value as IProvider, printToConsole);
214195
};
215196
216197
const onSignTypedData_v4 = async () => {
217-
await signTypedMessage(provider.value as IProvider, printToConsole);
198+
const result = await signTypedDataAsync({
199+
types: {
200+
Person: [
201+
{ name: "name", type: "string" },
202+
{ name: 'wallet', type: 'address' },
203+
],
204+
Mail: [
205+
{ name: 'from', type: 'Person' },
206+
{ name: 'to', type: 'Person' },
207+
{ name: 'contents', type: 'string' },
208+
],
209+
},
210+
primaryType: 'Mail',
211+
message: {
212+
from: {
213+
name: "Cow",
214+
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
215+
},
216+
to: {
217+
name: "Bob",
218+
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
219+
},
220+
contents: "Hello, Bob!",
221+
},
222+
});
223+
printToConsole("result", result);
218224
};
219225
220226
const onSignPersonalMsg = async () => {
221-
await signPersonalMessage(provider.value as IProvider, printToConsole);
227+
const result = await signMessageAsync({
228+
message: "Hello, Bob!",
229+
});
230+
printToConsole("result", result);
222231
};
223232
224233
// Solana
@@ -227,15 +236,52 @@ const onGetSolPrivateKey = async () => {
227236
};
228237
229238
const onSignAndSendTransaction = async () => {
230-
await signAndSendTransaction(provider.value as IProvider, printToConsole);
239+
if (!solanaAccounts.value) throw new Error('No account connected');
240+
const conn = await getConnection(provider.value as IProvider);
241+
const block = await conn.getLatestBlockhash("finalized");
242+
const pubKey = solanaAccounts.value[0];
243+
244+
const transactionInstruction = SystemProgram.transfer({
245+
fromPubkey: new PublicKey(pubKey),
246+
toPubkey: new PublicKey(pubKey),
247+
lamports: 0.01 * LAMPORTS_PER_SOL,
248+
});
249+
250+
const transaction = new Transaction({
251+
blockhash: block.blockhash,
252+
lastValidBlockHeight: block.lastValidBlockHeight,
253+
feePayer: new PublicKey(pubKey),
254+
}).add(transactionInstruction);
255+
256+
const data = await signAndSendTransaction(transaction);
257+
printToConsole('result', data);
231258
};
232259
233260
const onSignSolTransaction = async () => {
234-
await signSolTransaction(provider.value as IProvider, printToConsole);
261+
if (!solanaAccounts.value) throw new Error('No account connected');
262+
const conn = await getConnection(provider.value as IProvider);
263+
const block = await conn.getLatestBlockhash("finalized");
264+
const pubKey = solanaAccounts.value[0];
265+
266+
const transactionInstruction = SystemProgram.transfer({
267+
fromPubkey: new PublicKey(pubKey),
268+
toPubkey: new PublicKey(pubKey),
269+
lamports: 0.01 * LAMPORTS_PER_SOL,
270+
});
271+
272+
const transaction = new Transaction({
273+
blockhash: block.blockhash,
274+
lastValidBlockHeight: block.lastValidBlockHeight,
275+
feePayer: new PublicKey(pubKey),
276+
}).add(transactionInstruction);
277+
278+
const result = await signSolTransaction(transaction)
279+
printToConsole('result', result);
235280
};
236281
237282
const onSignSolMessage = async () => {
238-
await signSolMessage(provider.value as IProvider, printToConsole);
283+
const result = await signSolanaMessage("Hello, Bob!");
284+
printToConsole("result", result);
239285
};
240286
241287
const onGetSolBalance = async () => {

demo/vue-app-new/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "./global";
22
import "./style.css";
33

4+
import { VueQueryPlugin } from "@tanstack/vue-query";
45
import { createI18n, PathValue } from "petite-vue-i18n";
56
import { createApp } from "vue";
67

@@ -33,4 +34,5 @@ const app = createApp(App);
3334

3435
app.use(createIcons);
3536
app.use(i18n);
37+
app.use(VueQueryPlugin);
3638
app.mount("#app");

demo/vue-app-new/src/services/solHandlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Connection, LAMPORTS_PER_SOL, PublicKey, SystemProgram, Transaction } f
33
import { CustomChainConfig, IProvider, log, SolanaWallet, TransactionOrVersionedTransaction } from "@web3auth/modal";
44
import { SOLANA_METHOD_TYPES } from "@web3auth/ws-embed";
55

6-
const getConnection = async (provider: IProvider): Promise<Connection> => {
6+
export const getConnection = async (provider: IProvider): Promise<Connection> => {
77
const solanaWallet = new SolanaWallet(provider);
88

99
const connectionConfig = await solanaWallet.request<never, CustomChainConfig>({ method: "solana_provider_config" });

0 commit comments

Comments
 (0)