From 286d1809edea4ce04006ce824c783c179ddbfd4b Mon Sep 17 00:00:00 2001 From: thomasRalee Date: Thu, 18 Jul 2024 03:25:30 +0800 Subject: [PATCH] chore: add connect wallet via private key functionality --- layer/store/wallet.ts | 45 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/layer/store/wallet.ts b/layer/store/wallet.ts index 53b5801..fc620ac 100644 --- a/layer/store/wallet.ts +++ b/layer/store/wallet.ts @@ -64,6 +64,7 @@ type WalletStoreState = { } autoSign?: AutoSign + privateKey: string } const initialStateFactory = (): WalletStoreState => ({ @@ -94,7 +95,9 @@ const initialStateFactory = (): WalletStoreState => ({ injectiveAddress: '', expiration: 0, duration: 0 - } + }, + + privateKey: '' }) export const useSharedWalletStore = defineStore('sharedWallet', { @@ -238,6 +241,17 @@ export const useSharedWalletStore = defineStore('sharedWallet', { privateKey: walletStore.autoSign?.privateKey as string, isAutoSign: true }) + + return + } + + if (walletStore.privateKey) { + console.log('connecting wallet!') + + walletStore.connectWallet(Wallet.PrivateKey, { + privateKey: walletStore.privateKey, + isAutoSign: false + }) } }, @@ -307,7 +321,7 @@ export const useSharedWalletStore = defineStore('sharedWallet', { walletStrategy.setOptions({ privateKey: options.privateKey }) } - if (!options?.isAutoSign) { + if (!options?.isAutoSign && wallet !== Wallet.PrivateKey) { walletStore.$patch({ walletConnectStatus: WalletConnectStatus.connecting, wallet @@ -672,6 +686,33 @@ export const useSharedWalletStore = defineStore('sharedWallet', { await walletStore.onConnect() }, + async connectPrivateKey(privateKeyHash: string) { + const walletStore = useSharedWalletStore() + + const pk = PrivateKey.fromHex(privateKeyHash) + const injectiveAddress = pk.toBech32() + + await walletStore.connectWallet(Wallet.PrivateKey, { + privateKey: privateKeyHash, + isAutoSign: false + }) + + const address = getEthereumAddress(injectiveAddress) + const session = await walletStrategy.getSessionOrConfirm(address) + + walletStore.$patch({ + address, + session, + injectiveAddress, + addresses: [address], + addressConfirmation: await walletStrategy.getSessionOrConfirm(address), + wallet: Wallet.PrivateKey, + privateKey: privateKeyHash + }) + + await walletStore.onConnect() + }, + async prepareBroadcastMessages(messages: Msgs | Msgs[], memo?: string) { const walletStore = useSharedWalletStore() const msgs = Array.isArray(messages) ? messages : [messages]