neptune-web-wallet/src/stores/neptuneStore.ts

116 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-10-24 22:49:46 +07:00
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { WalletState } from '@/interface'
export const useNeptuneStore = defineStore('neptune', () => {
const defaultNetwork = (import.meta.env.VITE_NODE_NETWORK || 'testnet') as 'mainnet' | 'testnet'
2025-10-24 22:49:46 +07:00
// ===== STATE =====
const wallet = ref<WalletState>({
seedPhrase: null,
password: null,
2025-10-24 22:49:46 +07:00
receiverId: null,
viewKey: null,
address: null,
network: defaultNetwork,
2025-10-24 22:49:46 +07:00
balance: null,
utxos: [],
})
const keystorePath = ref<null | string>(null)
2025-10-24 22:49:46 +07:00
// ===== SETTERS =====
const setSeedPhrase = (seedPhrase: string[] | null) => {
wallet.value.seedPhrase = seedPhrase
}
const setPassword = (password: string | null) => {
wallet.value.password = password
}
2025-10-24 22:49:46 +07:00
const setReceiverId = (receiverId: string | null) => {
wallet.value.receiverId = receiverId
}
const setViewKey = (viewKey: string | null) => {
wallet.value.viewKey = viewKey
}
const setAddress = (address: string | null) => {
wallet.value.address = address
}
const setNetwork = (network: 'mainnet' | 'testnet') => {
wallet.value.network = network
}
const setBalance = (balance: string | null) => {
wallet.value.balance = balance
}
const setUtxos = (utxos: any[]) => {
wallet.value.utxos = utxos
}
const setWallet = (walletData: Partial<WalletState>) => {
wallet.value = { ...wallet.value, ...walletData }
}
const setKeystorePath = (path: string | null) => {
keystorePath.value = path
}
2025-10-24 22:49:46 +07:00
const clearWallet = () => {
wallet.value = {
seedPhrase: null,
password: null,
2025-10-24 22:49:46 +07:00
receiverId: null,
viewKey: null,
address: null,
network: defaultNetwork,
2025-10-24 22:49:46 +07:00
balance: null,
utxos: [],
}
}
// ===== GETTERS =====
const getWallet = computed(() => wallet.value)
const getSeedPhrase = computed(() => wallet.value.seedPhrase)
const getSeedPhraseString = computed(() => wallet.value.seedPhrase?.join(' ') || '')
const getPassword = computed(() => wallet.value.password)
2025-10-24 22:49:46 +07:00
const getReceiverId = computed(() => wallet.value.receiverId)
const getViewKey = computed(() => wallet.value.viewKey)
const getAddress = computed(() => wallet.value.address)
const getNetwork = computed(() => wallet.value.network)
const getBalance = computed(() => wallet.value.balance)
const getUtxos = computed(() => wallet.value.utxos)
const hasWallet = computed(() => wallet.value.address !== null)
const getKeystorePath = computed(() => keystorePath.value)
2025-10-24 22:49:46 +07:00
return {
getWallet,
getSeedPhrase,
getSeedPhraseString,
getPassword,
2025-10-24 22:49:46 +07:00
getReceiverId,
getViewKey,
getAddress,
getNetwork,
getBalance,
getUtxos,
hasWallet,
getKeystorePath,
2025-10-24 22:49:46 +07:00
setSeedPhrase,
setPassword,
2025-10-24 22:49:46 +07:00
setReceiverId,
setViewKey,
setAddress,
setNetwork,
setBalance,
setUtxos,
setWallet,
setKeystorePath,
2025-10-24 22:49:46 +07:00
clearWallet,
}
})