Skip to content

Commit

Permalink
chore: layer update wallet store folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasRalee committed Jun 25, 2024
1 parent a8853b7 commit ec983b2
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 164 deletions.
168 changes: 140 additions & 28 deletions layer/store/wallet/index.ts → layer/store/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { defineStore } from 'pinia'
import {
Msgs,
MsgGrant,
PrivateKey,
msgsOrMsgExecMsgs,
getEthereumAddress,
getInjectiveAddress,
getDefaultSubaccountId
getDefaultSubaccountId,
getGenericAuthorizationFromMessageType
} from '@injectivelabs/sdk-ts'
import { GeneralException } from '@injectivelabs/exceptions'
import { StatusType } from '@injectivelabs/utils'
Expand All @@ -17,35 +20,25 @@ import {
import {
validateCosmosWallet,
confirmCorrectKeplrAddress
} from './../../wallet/cosmos'
import {
validateOkxWallet,
isOkxWalletInstalled
} from './../../wallet/okx-wallet'
} from '../wallet/cosmos'
import { validateOkxWallet, isOkxWalletInstalled } from '../wallet/okx-wallet'
import {
validateTrustWallet,
isTrustWalletInstalled
} from './../../wallet/trust-wallet'
import { IS_DEVNET } from './../../utils/constant'
import { getAddresses } from './../../wallet/wallet'
import { walletStrategy } from './../../wallet/wallet-strategy'
import { isBitGetInstalled, validateBitGet } from './../../wallet/bitget'
import { validatePhantom, isPhantomInstalled } from './../../wallet/phantom'
import { validateMetamask, isMetamaskInstalled } from './../../wallet/metamask'
import { msgBroadcaster } from '../../WalletService'
} from '../wallet/trust-wallet'
import { IS_DEVNET } from '../utils/constant'
import { getAddresses } from '../wallet/wallet'
import { walletStrategy } from '../wallet/wallet-strategy'
import { isBitGetInstalled, validateBitGet } from '../wallet/bitget'
import { validatePhantom, isPhantomInstalled } from '../wallet/phantom'
import { validateMetamask, isMetamaskInstalled } from '../wallet/metamask'
import { msgBroadcaster } from '../WalletService'
import {
AutoSign,
EventBus,
GrantDirection,
WalletConnectStatus
} from './../../types'
import {
resetAuthZ,
connectAuthZ,
connectAutoSign,
validateAutoSign,
disconnectAutoSign
} from './authz'
} from '../types'

type WalletStoreState = {
walletConnectStatus: WalletConnectStatus
Expand Down Expand Up @@ -163,12 +156,6 @@ export const useSharedWalletStore = defineStore('sharedWallet', {
}
},
actions: {
resetAuthZ,
connectAuthZ,
connectAutoSign,
validateAutoSign,
disconnectAutoSign,

async validate() {
const walletStore = useSharedWalletStore()

Expand Down Expand Up @@ -737,6 +724,131 @@ export const useSharedWalletStore = defineStore('sharedWallet', {
return response
},

connectAuthZ(
injectiveAddress: string,
direction: GrantDirection = GrantDirection.Granter
) {
const walletStore = useSharedWalletStore()

walletStore.$patch({
authZ: {
direction,
injectiveAddress,
address: getEthereumAddress(injectiveAddress),
defaultSubaccountId: getDefaultSubaccountId(injectiveAddress)
}
})

walletStore.onConnect()
},

async connectAutoSign(msgsType: string[]) {
const walletStore = useSharedWalletStore()

const { privateKey } = PrivateKey.generate()
const injectiveAddress = privateKey.toBech32()

const nowInSeconds = Math.floor(Date.now() / 1000)
const expirationInSeconds = 60 * 60 // 1 hour

const authZMsgs = msgsType.map((messageType) =>
MsgGrant.fromJSON({
grantee: injectiveAddress,
granter: walletStore.injectiveAddress,
expiration: nowInSeconds + expirationInSeconds,
authorization: getGenericAuthorizationFromMessageType(messageType)
})
)

await walletStore.broadcastWithFeeDelegation({ messages: authZMsgs })

const autoSign = {
injectiveAddress,
privateKey: privateKey.toPrivateKeyHex(),
expiration: nowInSeconds + expirationInSeconds,
duration: expirationInSeconds
}

walletStore.$patch({
autoSign
})

await walletStore.connectWallet(Wallet.PrivateKey, {
privateKey: autoSign.privateKey,
isAutoSign: true
})
},

async validateAutoSign(msgsType: string[]) {
const walletStore = useSharedWalletStore()

if (!walletStore.isAutoSignEnabled) {
return
}

const autoSign = walletStore.autoSign as AutoSign
const nowInSeconds = Math.floor(Date.now() / 1000)

if (autoSign.expiration > nowInSeconds) {
return
}

const expirationInSeconds = autoSign.duration || 3600

const authZMsgs = msgsType.map((messageType) =>
MsgGrant.fromJSON({
grantee: autoSign.injectiveAddress,
granter: walletStore.injectiveAddress,
expiration: nowInSeconds + expirationInSeconds,
authorization: getGenericAuthorizationFromMessageType(messageType)
})
)

await walletStore.connectWallet(walletStore.wallet)

await walletStore.broadcastWithFeeDelegation({
messages: authZMsgs
})

walletStore.$patch((state) => {
state.autoSign = {
...autoSign,
expiration: expirationInSeconds
}
})

await walletStore.connectWallet(Wallet.PrivateKey, {
privateKey: autoSign.privateKey,
isAutoSign: true
})
},

resetAuthZ() {
const walletStore = useSharedWalletStore()

walletStore.$patch({
authZ: {
address: '',
defaultSubaccountId: '',
direction: GrantDirection.Granter,
injectiveAddress: ''
}
})

walletStore.onConnect()
},

async disconnectAutoSign() {
const walletStore = useSharedWalletStore()

walletStore.$patch({
autoSign: undefined
})

await walletStore.connectWallet(walletStore.wallet)
await walletStore.onConnect()
},

async logout() {
const walletStore = useSharedWalletStore()

Expand Down
136 changes: 0 additions & 136 deletions layer/store/wallet/authz.ts

This file was deleted.

0 comments on commit ec983b2

Please sign in to comment.