Skip to content

Commit

Permalink
chore: add connect wallet via private key functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Jul 17, 2024
1 parent 14d72d5 commit 286d180
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions layer/store/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type WalletStoreState = {
}

autoSign?: AutoSign
privateKey: string
}

const initialStateFactory = (): WalletStoreState => ({
Expand Down Expand Up @@ -94,7 +95,9 @@ const initialStateFactory = (): WalletStoreState => ({
injectiveAddress: '',
expiration: 0,
duration: 0
}
},

privateKey: ''
})

export const useSharedWalletStore = defineStore('sharedWallet', {
Expand Down Expand Up @@ -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
})
}
},

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 286d180

Please sign in to comment.