diff --git a/resources/js/components/SignTransaction.vue b/resources/js/components/SignTransaction.vue
index a8a3a52..6b7732b 100644
--- a/resources/js/components/SignTransaction.vue
+++ b/resources/js/components/SignTransaction.vue
@@ -21,7 +21,7 @@
+
No accounts found. Please connect your wallet.
@@ -51,13 +51,13 @@ import { DialogTitle } from '@headlessui/vue';
import Btn from './Btn.vue';
import Modal from './Modal.vue';
import { addressShortHex } from '~/util/address';
-import { useAppStore } from '~/store';
import { useTransactionStore } from '~/store/transaction';
import { ref } from 'vue';
import LoadingCircle from './LoadingCircle.vue';
import snackbar from '~/util/snackbar';
import Identicon from './Identicon.vue';
import { formatPriceFromENJ } from '~/util';
+import { useConnectionStore } from '~/store/connection';
const props = defineProps<{
transaction: any;
@@ -71,19 +71,19 @@ const feeCost = ref();
const loadingApi = ref(false);
const signing = ref(false);
-const appStore = useAppStore();
const transactionStore = useTransactionStore();
+const connectionStore = useConnectionStore();
const signTransaction = async () => {
try {
- if (!appStore.provider) {
+ if (!connectionStore.provider) {
snackbar.error({ title: 'Please connect your wallet to sign' });
return;
}
showAccountsModal.value = true;
loadingApi.value = true;
- appStore.getAccounts();
+ connectionStore.getAccounts();
await transactionStore.init();
feeCost.value = formatPriceFromENJ(props.transaction.fee)?.toFixed(5);
loadingApi.value = false;
@@ -100,7 +100,7 @@ const closeModal = () => {
const selectAccount = async (account) => {
try {
isLoading.value = true;
- await appStore.setAccount(account);
+ await connectionStore.setAccount(account);
signing.value = true;
const res = await transactionStore.signTransaction(props.transaction);
if (res) {
diff --git a/resources/js/components/WalletConnectButton.vue b/resources/js/components/WalletConnectButton.vue
index 9c5c86d..52d7279 100644
--- a/resources/js/components/WalletConnectButton.vue
+++ b/resources/js/components/WalletConnectButton.vue
@@ -57,25 +57,25 @@
diff --git a/resources/js/store/connection.ts b/resources/js/store/connection.ts
new file mode 100644
index 0000000..05ad305
--- /dev/null
+++ b/resources/js/store/connection.ts
@@ -0,0 +1,171 @@
+import { defineStore } from 'pinia';
+import { ConnectionState } from '~/types/types.interface';
+import { wcOptions } from '~/util';
+import { wcRequiredNamespaces } from '~/util';
+import { getAppMetadata, getSdkError } from '@walletconnect/utils';
+import { PolkadotjsWallet, Wallet } from '@talismn/connect-wallets';
+import SignClient from '@walletconnect/sign-client';
+import { HexString } from '@polkadot/util/types';
+import { wcNamespaces, wcProjectId } from '~/util/constants';
+import { useAppStore } from '.';
+import { WalletConnectModalSign } from '@walletconnect/modal-sign-html';
+
+export const useConnectionStore = defineStore('connection', {
+ state: (): ConnectionState => ({
+ provider: '',
+ wallet: false,
+ walletClient: null,
+ walletSession: null,
+ account: null,
+ accounts: null,
+ }),
+ persist: {
+ paths: ['provider'],
+ },
+ actions: {
+ getWeb3Modal() {
+ return new WalletConnectModalSign(wcOptions);
+ },
+ async connectWallet(provider) {
+ if (provider === 'wc') {
+ await this.connectWC();
+ }
+
+ if (provider === 'polkadot.js') {
+ await this.connectPolkadotJS();
+ }
+ },
+ async initWalletClient() {
+ this.walletClient = await SignClient.init({
+ projectId: wcProjectId,
+ metadata: getAppMetadata(),
+ });
+
+ if (this.walletClient) {
+ this.walletClient.on('session_delete', () => {
+ this.disconnectWallet();
+ });
+ this.walletClient.on('session_expire', () => {
+ this.disconnectWallet();
+ });
+ }
+
+ this.walletClient.session.getAll({});
+ if (this.walletClient.session.length) {
+ const lastKeyIndex = this.walletClient.session.keys.length - 1;
+ this.walletSession = this.walletClient.session.get(this.walletClient.session.keys[lastKeyIndex]);
+ }
+ },
+ async connectWC() {
+ const walletConnect = this.getWeb3Modal();
+ await this.initWalletClient();
+
+ this.walletSession = await walletConnect.connect({
+ requiredNamespaces: wcRequiredNamespaces(useAppStore().config.network),
+ });
+
+ if (this.walletSession && this.walletSession.acknowledged) {
+ this.provider = 'wc';
+ this.wallet = true;
+ await this.initWalletClient();
+
+ return;
+ }
+
+ await walletConnect!.disconnect({
+ topic: this.walletSession.topic,
+ reason: getSdkError('USER_REJECTED'),
+ });
+
+ this.account = null;
+ },
+ async connectPolkadotJS() {
+ const pkjs = new PolkadotjsWallet();
+ if (pkjs.installed) {
+ await pkjs.enable('Platform');
+ this.wallet = true;
+ this.provider = 'polkadot.js';
+ this.walletSession = pkjs;
+ }
+ },
+
+ async getSession(): Promise