Merge remote-tracking branch 'origin/draply/mac-os-support' into draply/mac-os-support

# Conflicts:
#	electron/ipcHandlers.ts
#	src/composables/useNeptuneWallet.ts
#	src/stores/neptuneStore.ts
This commit is contained in:
draplydo 2025-11-11 09:30:40 +07:00
commit 66c93dcc00
26 changed files with 91 additions and 125 deletions

View File

@ -78,7 +78,7 @@ ipcMain.handle('wallet:checkKeystore', async () => {
fs.statSync(path.join(walletDir, a)).mtime.getTime() fs.statSync(path.join(walletDir, a)).mtime.getTime()
)[0] )[0]
if (!newestFile) return { exists: false, filePath: null } if (!newestFile?.length) return { exists: false, filePath: null }
const resolvedPath = path.join(walletDir, newestFile) const resolvedPath = path.join(walletDir, newestFile)
let minBlockHeight: number | null = null let minBlockHeight: number | null = null
@ -88,11 +88,10 @@ ipcMain.handle('wallet:checkKeystore', async () => {
const data = JSON.parse(json) const data = JSON.parse(json)
const height = data?.minBlockHeight const height = data?.minBlockHeight
if (typeof height === 'number' && Number.isFinite(height)) { if (Number.isFinite(height)) minBlockHeight = height
minBlockHeight = height
}
} catch (error) { } catch (error) {
console.warn('Unable to read minBlockHeight from keystore:', error) console.warn('Unable to read minBlockHeight from keystore:', error)
return { exists: true, filePath: resolvedPath }
} }
return { exists: true, filePath: resolvedPath, minBlockHeight } return { exists: true, filePath: resolvedPath, minBlockHeight }
@ -137,37 +136,34 @@ ipcMain.handle(
} }
) )
ipcMain.handle( ipcMain.handle('wallet:getMinBlockHeight', async (_event, filePath: string | null) => {
'wallet:getMinBlockHeight', if (!filePath) {
async (_event, filePath: string | null) => { return { success: false, error: 'No keystore file path provided.', minBlockHeight: null }
if (!filePath) {
return { success: false, error: 'No keystore file path provided.', minBlockHeight: null }
}
try {
const normalizedPath = path.isAbsolute(filePath)
? filePath
: path.join(process.cwd(), filePath)
if (!fs.existsSync(normalizedPath)) {
return { success: false, error: 'Keystore file not found.', minBlockHeight: null }
}
const fileContents = fs.readFileSync(normalizedPath, 'utf-8')
const walletJson = JSON.parse(fileContents)
const height = walletJson?.minBlockHeight
if (typeof height === 'number' && Number.isFinite(height)) {
return { success: true, minBlockHeight: height }
}
return { success: true, minBlockHeight: null }
} catch (error) {
console.error('Error reading min block height:', error)
return { success: false, error: String(error), minBlockHeight: null }
}
} }
)
try {
const normalizedPath = path.isAbsolute(filePath)
? filePath
: path.join(process.cwd(), filePath)
if (!fs.existsSync(normalizedPath)) {
return { success: false, error: 'Keystore file not found.', minBlockHeight: null }
}
const fileContents = fs.readFileSync(normalizedPath, 'utf-8')
const walletJson = JSON.parse(fileContents)
const height = walletJson?.minBlockHeight
if (Number.isFinite(height)) {
return { success: true, minBlockHeight: height }
}
return { success: true, minBlockHeight: null }
} catch (error) {
console.error('Error reading min block height:', error)
return { success: false, error: String(error), minBlockHeight: null }
}
})
ipcMain.handle('wallet:generateKeysFromSeed', async (_event, seedPhrase: string[]) => { ipcMain.handle('wallet:generateKeysFromSeed', async (_event, seedPhrase: string[]) => {
try { try {
@ -188,7 +184,6 @@ ipcMain.handle('wallet:buildTransaction', async (_event, args) => {
import.meta.env.VITE_APP_API, import.meta.env.VITE_APP_API,
spendingKeyHex, spendingKeyHex,
inputAdditionRecords, inputAdditionRecords,
// pass minBlockHeight from args if provided, default 0
typeof args?.minBlockHeight === 'number' && Number.isFinite(args.minBlockHeight) typeof args?.minBlockHeight === 'number' && Number.isFinite(args.minBlockHeight)
? args.minBlockHeight ? args.minBlockHeight
: 0, : 0,

View File

@ -4,7 +4,6 @@ export async function encrypt(seed: string, password: string) {
const salt = crypto.randomBytes(16) const salt = crypto.randomBytes(16)
const iv = crypto.randomBytes(12) const iv = crypto.randomBytes(12)
// derive 32-byte key từ password
const key = await new Promise<Buffer>((resolve, reject) => { const key = await new Promise<Buffer>((resolve, reject) => {
crypto.scrypt(password, salt, 32, { N: 16384, r: 8, p: 1 }, (err, derivedKey) => { crypto.scrypt(password, salt, 32, { N: 16384, r: 8, p: 1 }, (err, derivedKey) => {
if (err) reject(err) if (err) reject(err)

Binary file not shown.

View File

@ -1,6 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import { LayoutVue } from '@/components'
const config = { const config = {
token: { token: {
colorPrimary: '#42A5F5', colorPrimary: '#42A5F5',

View File

@ -2,7 +2,7 @@ import { callJsonRpc } from '@/api/request'
export const getUtxosFromViewKey = async ( export const getUtxosFromViewKey = async (
viewKey: string, viewKey: string,
startBlock: number = 0, startBlock: number | null = 0,
endBlock: number | null = null, endBlock: number | null = null,
maxSearchDepth: number = 1000 maxSearchDepth: number = 1000
): Promise<any> => { ): Promise<any> => {
@ -17,7 +17,7 @@ export const getUtxosFromViewKey = async (
export const getBalance = async ( export const getBalance = async (
viewKey: string, viewKey: string,
startBlock: number = 0, startBlock: number | null = 0,
endBlock: number | null = null, endBlock: number | null = null,
maxSearchDepth: number = 1000 maxSearchDepth: number = 1000
): Promise<any> => { ): Promise<any> => {

View File

@ -1,7 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import type { ButtonProps } from '@/interface' import type { ButtonProps } from '@/interface'
import { computed } from 'vue' import { computed } from 'vue'
import { SpinnerCommon } from '@/components'
const props = withDefaults(defineProps<ButtonProps>(), { const props = withDefaults(defineProps<ButtonProps>(), {
type: 'default', type: 'default',

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
import { ButtonCommon, CardBase, FormCommon } from '@/components'
interface Props { interface Props {
title?: string title?: string

View File

@ -7,6 +7,7 @@ import type {
WalletState, WalletState,
} from '@/interface' } from '@/interface'
import initWasm, { generate_seed, address_from_seed, validate_seed_phrase } from '@neptune/wasm' import initWasm, { generate_seed, address_from_seed, validate_seed_phrase } from '@neptune/wasm'
import { toFiniteNumber } from '@/utils'
let wasmInitialized = false let wasmInitialized = false
let initPromise: Promise<void> | null = null let initPromise: Promise<void> | null = null
@ -171,9 +172,7 @@ export function useNeptuneWallet() {
store.setKeystorePath(keystoreFile.filePath) store.setKeystorePath(keystoreFile.filePath)
if ('minBlockHeight' in keystoreFile) { if ('minBlockHeight' in keystoreFile) {
const height = keystoreFile.minBlockHeight const height = keystoreFile.minBlockHeight
store.setMinBlockHeight( store.setMinBlockHeight(toFiniteNumber(height))
typeof height === 'number' && Number.isFinite(height) ? height : null
)
} }
return true return true
} catch (err) { } catch (err) {
@ -182,82 +181,69 @@ export function useNeptuneWallet() {
} }
} }
// ===== API METHODS =====
const persistMinBlockHeight = async (utxos: any[]) => { const persistMinBlockHeight = async (utxos: any[]) => {
const keystorePath = store.getKeystorePath const keystorePath = store.getKeystorePath
if (!keystorePath) return if (!keystorePath) return
try { try {
const heights = utxos const minBlockHeight = utxos.reduce((min, utxo) => {
.map((utxo) => { const h = +(
const rawHeight = [utxo?.blockHeight, utxo?.block_height, utxo?.height, utxo?.block?.height] utxo?.blockHeight ??
.find((h) => h !== null && h !== undefined) ?? null utxo?.block_height ??
utxo?.height ??
utxo?.block?.height
)
return Number.isFinite(h) && (min === null || h < min) ? h : min
}, null)
const numericHeight = const response = await (window as any).walletApi.updateMinBlockHeight(
typeof rawHeight === 'string' ? Number(rawHeight) : rawHeight keystorePath,
minBlockHeight
return Number.isFinite(numericHeight) ? Number(numericHeight) : null )
}) if (!response.success) throw new Error('Failed to update min block height')
.filter((height): height is number => height !== null)
const minBlockHeight = heights.length ? Math.min(...heights) : null
await (window as any).walletApi.updateMinBlockHeight(keystorePath, minBlockHeight)
store.setMinBlockHeight(minBlockHeight) store.setMinBlockHeight(minBlockHeight)
} catch (err) { } catch (err) {
console.error('Error saving min block height:', err) console.error('Error saving min block height:', err)
throw err
} }
} }
const loadMinBlockHeightFromKeystore = async (): Promise<number | null> => { const loadMinBlockHeightFromKeystore = async (): Promise<number | null> => {
const keystorePath = store.getKeystorePath const keystorePath = store.getKeystorePath
if (!keystorePath) return null if (!keystorePath) return null
try { try {
const response = await (window as any).walletApi.getMinBlockHeight(keystorePath) const response = await (window as any).walletApi.getMinBlockHeight(keystorePath)
if (!response?.success) return null
if (response?.success) { const minBlockHeight = toFiniteNumber(response.minBlockHeight)
const minBlockHeight =
typeof response.minBlockHeight === 'number' && Number.isFinite(response.minBlockHeight)
? response.minBlockHeight
: null
store.setMinBlockHeight(minBlockHeight) store.setMinBlockHeight(minBlockHeight)
return minBlockHeight return minBlockHeight
}
} catch (err) { } catch (err) {
console.error('Error loading min block height:', err) console.error('Error loading min block height:', err)
throw err
} }
return null
} }
// ===== API METHODS =====
const getUtxos = async (): Promise<any> => { const getUtxos = async (): Promise<any> => {
try { try {
if (!store.getViewKey) { if (!store.getViewKey) {
throw new Error('No view key available. Please import or generate a wallet first.') throw new Error('No view key available. Please import or generate a wallet first.')
} }
let startBlock: number | null | undefined = store.getMinBlockHeight let startBlock: number | null = store.getMinBlockHeight
if (startBlock == null) startBlock = await loadMinBlockHeightFromKeystore()
if (startBlock === null || startBlock === undefined) {
startBlock = await loadMinBlockHeightFromKeystore()
}
const response = await API.getUtxosFromViewKey( const response = await API.getUtxosFromViewKey(
store.getViewKey || '', store.getViewKey || '',
typeof startBlock === 'number' && Number.isFinite(startBlock) ? startBlock : 0 toFiniteNumber(startBlock, 0)
) )
const result = response?.result || response const result = response?.result || response
const utxoList = Array.isArray(result?.utxos) const utxos = result?.utxos ?? result
? result.utxos const utxoList = Array.isArray(utxos) ? utxos : []
: Array.isArray(result)
? result
: []
store.setUtxos(utxoList) store.setUtxos(utxoList)
@ -279,9 +265,10 @@ export function useNeptuneWallet() {
const response = await API.getBalance( const response = await API.getBalance(
store.getViewKey || '', store.getViewKey || '',
typeof startBlock === 'number' && Number.isFinite(startBlock) ? startBlock : 0 toFiniteNumber(startBlock, 0)
) )
const result = response?.result || response const result = response?.result || response
store.setBalance(result?.balance || result) store.setBalance(result?.balance || result)
store.setPendingBalance(result?.pendingBalance || result) store.setPendingBalance(result?.pendingBalance || result)
return { return {
@ -318,9 +305,7 @@ export function useNeptuneWallet() {
} }
} }
const buildTransaction = async ( const buildTransaction = async (args: PayloadBuildTransaction): Promise<any> => {
args: PayloadBuildTransaction
): Promise<any> => {
let minBlockHeight: number | null | undefined = store.getMinBlockHeight let minBlockHeight: number | null | undefined = store.getMinBlockHeight
if (minBlockHeight === null || minBlockHeight === undefined) { if (minBlockHeight === null || minBlockHeight === undefined) {
minBlockHeight = await loadMinBlockHeightFromKeystore() minBlockHeight = await loadMinBlockHeightFromKeystore()
@ -329,10 +314,7 @@ export function useNeptuneWallet() {
const payload = { const payload = {
spendingKeyHex: store.getSpendingKey, spendingKeyHex: store.getSpendingKey,
inputAdditionRecords: args.inputAdditionRecords, inputAdditionRecords: args.inputAdditionRecords,
minBlockHeight: minBlockHeight: toFiniteNumber(minBlockHeight, 0),
typeof minBlockHeight === 'number' && Number.isFinite(minBlockHeight)
? minBlockHeight
: 0,
outputAddresses: args.outputAddresses, outputAddresses: args.outputAddresses,
outputAmounts: args.outputAmounts, outputAmounts: args.outputAmounts,
fee: args.fee, fee: args.fee,

View File

@ -1,6 +1,7 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
import type { WalletState } from '@/interface' import type { WalletState } from '@/interface'
import { toFiniteNumber } from '@/utils'
export const useNeptuneStore = defineStore('neptune', () => { export const useNeptuneStore = defineStore('neptune', () => {
const defaultNetwork = (import.meta.env.VITE_NODE_NETWORK || 'mainnet') as 'mainnet' | 'testnet' const defaultNetwork = (import.meta.env.VITE_NODE_NETWORK || 'mainnet') as 'mainnet' | 'testnet'
@ -65,10 +66,7 @@ export const useNeptuneStore = defineStore('neptune', () => {
} }
const setMinBlockHeight = (minBlockHeight: number | null) => { const setMinBlockHeight = (minBlockHeight: number | null) => {
wallet.value.minBlockHeight = wallet.value.minBlockHeight = toFiniteNumber(minBlockHeight)
typeof minBlockHeight === 'number' && Number.isFinite(minBlockHeight)
? minBlockHeight
: null
} }
const setWallet = (walletData: Partial<WalletState>) => { const setWallet = (walletData: Partial<WalletState>) => {

View File

@ -0,0 +1,3 @@
export function toFiniteNumber(value: number | null, defaultValue?: number): number | null {
return Number.isFinite(value) ? value : (defaultValue ?? null)
}

View File

@ -2,3 +2,4 @@ export * from './constants/code'
export * from './constants/constants' export * from './constants/constants'
export * from './helpers/format' export * from './helpers/format'
export * from './helpers/seedPhrase' export * from './helpers/seedPhrase'
export * from './helpers/helpers'

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { ref } from 'vue'
import { PasswordForm } from '@/components'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/authStore' import { useAuthStore } from '@/stores/authStore'

View File

@ -1,6 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import { ButtonCommon } from '@/components'
const emit = defineEmits<{ const emit = defineEmits<{
goToCreate: [] goToCreate: []
goToRecover: [] goToRecover: []

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed } from 'vue'
import { ButtonCommon } from '@/components'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, onMounted, computed } from 'vue' import { ref, onMounted, computed } from 'vue'
import { ButtonCommon } from '@/components'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
import { ButtonCommon, FormCommon } from '@/components'
import { useNeptuneStore } from '@/stores' import { useNeptuneStore } from '@/stores'
const emit = defineEmits<{ const emit = defineEmits<{

View File

@ -4,7 +4,6 @@ import { SeedPhraseDisplayComponent, ConfirmSeedComponent } from '..'
import { CreatePasswordStep, WalletCreatedStep } from '.' import { CreatePasswordStep, WalletCreatedStep } from '.'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import { CardBaseScrollable } from '@/components'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
const emit = defineEmits<{ const emit = defineEmits<{

View File

@ -1,5 +1,4 @@
<script setup lang="ts"> <script setup lang="ts">
import { ButtonCommon } from '@/components'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue' import { ref } from 'vue'
import { ButtonCommon, PasswordForm } from '@/components'
import { RecoverSeedComponent } from '..' import { RecoverSeedComponent } from '..'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import { TabsCommon, TabPaneCommon } from '@/components'
import { WalletTab, NetworkTab, UTXOTab } from './components' import { WalletTab, NetworkTab, UTXOTab } from './components'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'

View File

@ -1,7 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue' import { ref, computed, onMounted, onUnmounted } from 'vue'
import { formatNumberToLocaleString } from '@/utils' import { formatNumberToLocaleString } from '@/utils'
import { CardBase, SpinnerCommon } from '@/components'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'

View File

@ -1,24 +1,18 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted } from 'vue' import { ref, computed, onMounted, inject, watch, type ComputedRef } from 'vue'
import { Table, message } from 'ant-design-vue' import { Table, message } from 'ant-design-vue'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { CardBaseScrollable, SpinnerCommon } from '@/components'
import { columns } from '../utils' import { columns } from '../utils'
const { getUtxos } = useNeptuneWallet() const { getUtxos } = useNeptuneWallet()
const neptuneStore = useNeptuneStore() const neptuneStore = useNeptuneStore()
const activeTabKey = inject<ComputedRef<string>>('activeTabKey')
const loading = ref(false) const loading = ref(false)
const utxosList = computed(() => [ const utxosList = computed(() => [
...(neptuneStore.getUtxos || []), ...(neptuneStore.getUtxos || []),
...Array.from({ length: 18 }, (_, i) => ({
additionRecord: `additionRecord${i}`,
amount: `${i}.00000000`,
blockHeight: `blockHeight${i}`,
utxoHash: `utxoHash${i}`,
})),
]) ])
const inUseUtxosCount = computed(() => (utxosList.value?.length ? utxosList.value.length : 0)) const inUseUtxosCount = computed(() => (utxosList.value?.length ? utxosList.value.length : 0))
@ -55,6 +49,13 @@ const loadUtxos = async () => {
onMounted(() => { onMounted(() => {
loadUtxos() loadUtxos()
}) })
watch(
() => activeTabKey?.value,
(newTab) => {
if (newTab === 'UTXOs') loadUtxos()
}
)
</script> </script>
<template> <template>

View File

@ -1,6 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
import { ButtonCommon, ModalCommon } from '@/components'
interface Props { interface Props {
isLoading?: boolean isLoading?: boolean

View File

@ -27,7 +27,9 @@ const props = defineProps<Props>()
<style lang="scss" scoped> <style lang="scss" scoped>
.balance-section { .balance-section {
text-align: center; display: flex;
flex-direction: column;
align-items: center;
margin-bottom: var(--spacing-xl); margin-bottom: var(--spacing-xl);
padding-bottom: var(--spacing-lg); padding-bottom: var(--spacing-lg);
border-bottom: 2px solid var(--border-color); border-bottom: 2px solid var(--border-color);

View File

@ -1,15 +1,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue' import { ref, computed, onMounted, onUnmounted, inject, watch, type ComputedRef } from 'vue'
import { useNeptuneStore } from '@/stores/neptuneStore' import { useNeptuneStore } from '@/stores/neptuneStore'
import { useNeptuneWallet } from '@/composables/useNeptuneWallet' import { useNeptuneWallet } from '@/composables/useNeptuneWallet'
import { message } from 'ant-design-vue' import { message } from 'ant-design-vue'
import {
ButtonCommon,
CardBaseScrollable,
ModalCommon,
SpinnerCommon,
PasswordForm,
} from '@/components'
import SeedPhraseDisplayComponent from '@/views/Auth/components/SeedPhraseDisplayComponent.vue' import SeedPhraseDisplayComponent from '@/views/Auth/components/SeedPhraseDisplayComponent.vue'
import SendTransactionComponent from './SendTransactionComponent.vue' import SendTransactionComponent from './SendTransactionComponent.vue'
import { WalletAddress, WalletBalance } from '.' import { WalletAddress, WalletBalance } from '.'
@ -23,6 +16,7 @@ const {
broadcastSignedTransaction, broadcastSignedTransaction,
decryptKeystore, decryptKeystore,
} = useNeptuneWallet() } = useNeptuneWallet()
const activeTabKey = inject<ComputedRef<string>>('activeTabKey')
const availableBalance = ref<string>('0.00000000') const availableBalance = ref<string>('0.00000000')
const pendingBalance = ref<string>('0.00000000') const pendingBalance = ref<string>('0.00000000')
@ -172,6 +166,13 @@ onMounted(() => {
onUnmounted(() => { onUnmounted(() => {
window.removeEventListener('resize', handleResize) window.removeEventListener('resize', handleResize)
}) })
watch(
() => activeTabKey?.value,
(newTab) => {
if (newTab === 'WALLET') loadWalletData()
}
)
</script> </script>
<template> <template>