2025-11-05 04:14:37 +07:00
|
|
|
import { ipcMain, dialog, app } from 'electron'
|
2025-10-31 21:56:47 +07:00
|
|
|
import fs from 'fs'
|
|
|
|
|
import path from 'path'
|
2025-11-05 04:14:37 +07:00
|
|
|
import { encrypt, fromEncryptedJson } from './utils/keystore'
|
2025-10-31 01:22:35 +07:00
|
|
|
|
2025-11-07 18:27:37 +07:00
|
|
|
const neptuneNative = require('@neptune/native')
|
|
|
|
|
|
2025-11-05 04:14:37 +07:00
|
|
|
// Create keystore into default wallets directory
|
2025-10-31 01:22:35 +07:00
|
|
|
ipcMain.handle('wallet:createKeystore', async (_event, seed, password) => {
|
2025-10-31 21:56:47 +07:00
|
|
|
try {
|
2025-11-05 04:14:37 +07:00
|
|
|
const keystore = await encrypt(seed, password)
|
2025-10-31 01:22:35 +07:00
|
|
|
|
2025-10-31 21:56:47 +07:00
|
|
|
const savePath = path.join(process.cwd(), 'wallets')
|
|
|
|
|
fs.mkdirSync(savePath, { recursive: true })
|
2025-10-31 01:22:35 +07:00
|
|
|
|
2025-11-05 04:14:37 +07:00
|
|
|
// Use timestamp for filename
|
|
|
|
|
const timestamp = Date.now()
|
|
|
|
|
const fileName = `neptune-wallet-${timestamp}.json`
|
|
|
|
|
const filePath = path.join(savePath, fileName)
|
2025-10-31 21:56:47 +07:00
|
|
|
fs.writeFileSync(filePath, keystore)
|
2025-10-31 01:22:35 +07:00
|
|
|
|
2025-11-05 04:14:37 +07:00
|
|
|
return { filePath }
|
2025-10-31 21:56:47 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error creating keystore:', error)
|
2025-11-05 04:14:37 +07:00
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// New handler: let user choose folder and filename to save keystore
|
|
|
|
|
ipcMain.handle('wallet:saveKeystoreAs', async (_event, seed: string, password: string) => {
|
|
|
|
|
try {
|
|
|
|
|
const keystore = await encrypt(seed, password)
|
|
|
|
|
|
|
|
|
|
// Use timestamp for default filename
|
|
|
|
|
const timestamp = Date.now()
|
|
|
|
|
const defaultName = `neptune-wallet-${timestamp}.json`
|
|
|
|
|
const { canceled, filePath } = await dialog.showSaveDialog({
|
|
|
|
|
title: 'Save Keystore File',
|
|
|
|
|
defaultPath: path.join(app.getPath('documents'), defaultName),
|
|
|
|
|
filters: [{ name: 'JSON', extensions: ['json'] }],
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (canceled || !filePath) return { filePath: null }
|
|
|
|
|
|
|
|
|
|
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
|
|
|
|
fs.writeFileSync(filePath, keystore)
|
|
|
|
|
|
|
|
|
|
return { filePath }
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error saving keystore (Save As):', error)
|
|
|
|
|
throw error
|
2025-10-31 21:56:47 +07:00
|
|
|
}
|
|
|
|
|
})
|
2025-10-31 01:22:35 +07:00
|
|
|
|
|
|
|
|
ipcMain.handle('wallet:decryptKeystore', async (_event, filePath, password) => {
|
2025-10-31 21:56:47 +07:00
|
|
|
try {
|
|
|
|
|
const json = fs.readFileSync(filePath, 'utf-8')
|
2025-11-05 04:14:37 +07:00
|
|
|
const phrase = await fromEncryptedJson(json, password)
|
2025-10-31 21:56:47 +07:00
|
|
|
|
2025-11-05 04:14:37 +07:00
|
|
|
return { phrase }
|
2025-10-31 21:56:47 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error decrypting keystore ipc:', error)
|
2025-11-05 04:14:37 +07:00
|
|
|
throw error
|
2025-10-31 21:56:47 +07:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ipcMain.handle('wallet:checkKeystore', async () => {
|
|
|
|
|
try {
|
|
|
|
|
const walletDir = path.join(process.cwd(), 'wallets')
|
2025-11-05 04:14:37 +07:00
|
|
|
if (!fs.existsSync(walletDir)) return { exists: false, filePath: null }
|
2025-10-31 21:56:47 +07:00
|
|
|
|
2025-11-07 18:27:37 +07:00
|
|
|
const newestFile = fs
|
|
|
|
|
.readdirSync(walletDir)
|
|
|
|
|
.filter((f) => f.endsWith('.json'))
|
|
|
|
|
.sort(
|
|
|
|
|
(a, b) =>
|
|
|
|
|
fs.statSync(path.join(walletDir, b)).mtime.getTime() -
|
|
|
|
|
fs.statSync(path.join(walletDir, a)).mtime.getTime()
|
|
|
|
|
)[0]
|
|
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
if (!newestFile?.length) return { exists: false, filePath: null }
|
2025-10-31 21:56:47 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
const resolvedPath = path.join(walletDir, newestFile)
|
|
|
|
|
let minBlockHeight: number | null = null
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const json = fs.readFileSync(resolvedPath, 'utf-8')
|
|
|
|
|
const data = JSON.parse(json)
|
|
|
|
|
const height = data?.minBlockHeight
|
|
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
if (Number.isFinite(height)) minBlockHeight = height
|
2025-11-10 15:50:35 +07:00
|
|
|
} catch (error) {
|
|
|
|
|
console.warn('Unable to read minBlockHeight from keystore:', error)
|
2025-11-10 15:50:35 +07:00
|
|
|
return { exists: true, filePath: resolvedPath }
|
2025-11-10 15:50:35 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { exists: true, filePath: resolvedPath, minBlockHeight }
|
2025-10-31 21:56:47 +07:00
|
|
|
} catch (error) {
|
2025-11-07 18:27:37 +07:00
|
|
|
console.error('Error checking keystore ipc:', error)
|
2025-11-10 15:50:35 +07:00
|
|
|
return { exists: false, filePath: null, minBlockHeight: null, error: String(error) }
|
2025-10-31 21:56:47 +07:00
|
|
|
}
|
|
|
|
|
})
|
2025-11-07 18:27:37 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
ipcMain.handle(
|
|
|
|
|
'wallet:updateMinBlockHeight',
|
|
|
|
|
async (_event, filePath: string | null, minBlockHeight: number | null) => {
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
return { success: false, error: 'No keystore file path provided.' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const normalizedPath = path.isAbsolute(filePath)
|
|
|
|
|
? filePath
|
|
|
|
|
: path.join(process.cwd(), filePath)
|
|
|
|
|
|
|
|
|
|
if (!fs.existsSync(normalizedPath)) {
|
|
|
|
|
return { success: false, error: 'Keystore file not found.' }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fileContents = fs.readFileSync(normalizedPath, 'utf-8')
|
|
|
|
|
const walletJson = JSON.parse(fileContents)
|
|
|
|
|
|
|
|
|
|
if (minBlockHeight === null || Number.isNaN(minBlockHeight)) {
|
|
|
|
|
walletJson.minBlockHeight = null
|
|
|
|
|
} else {
|
|
|
|
|
walletJson.minBlockHeight = minBlockHeight
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(normalizedPath, JSON.stringify(walletJson, null, 2), 'utf-8')
|
|
|
|
|
|
|
|
|
|
return { success: true, minBlockHeight }
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error updating min block height:', error)
|
|
|
|
|
return { success: false, error: String(error) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
ipcMain.handle('wallet:getMinBlockHeight', async (_event, filePath: string | null) => {
|
|
|
|
|
if (!filePath) {
|
|
|
|
|
return { success: false, error: 'No keystore file path provided.', minBlockHeight: null }
|
|
|
|
|
}
|
2025-11-10 15:50:35 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
try {
|
|
|
|
|
const normalizedPath = path.isAbsolute(filePath)
|
|
|
|
|
? filePath
|
|
|
|
|
: path.join(process.cwd(), filePath)
|
2025-11-10 15:50:35 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
if (!fs.existsSync(normalizedPath)) {
|
|
|
|
|
return { success: false, error: 'Keystore file not found.', minBlockHeight: null }
|
|
|
|
|
}
|
2025-11-10 15:50:35 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
const fileContents = fs.readFileSync(normalizedPath, 'utf-8')
|
|
|
|
|
const walletJson = JSON.parse(fileContents)
|
|
|
|
|
const height = walletJson?.minBlockHeight
|
2025-11-10 15:50:35 +07:00
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
if (Number.isFinite(height)) {
|
|
|
|
|
return { success: true, minBlockHeight: height }
|
2025-11-10 15:50:35 +07:00
|
|
|
}
|
2025-11-10 15:50:35 +07:00
|
|
|
|
|
|
|
|
return { success: true, minBlockHeight: null }
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error reading min block height:', error)
|
|
|
|
|
return { success: false, error: String(error), minBlockHeight: null }
|
2025-11-10 15:50:35 +07:00
|
|
|
}
|
2025-11-10 15:50:35 +07:00
|
|
|
})
|
2025-11-10 15:50:35 +07:00
|
|
|
|
2025-11-07 18:27:37 +07:00
|
|
|
ipcMain.handle('wallet:generateKeysFromSeed', async (_event, seedPhrase: string[]) => {
|
|
|
|
|
try {
|
|
|
|
|
const wallet = new neptuneNative.WalletManager()
|
|
|
|
|
return wallet.generateKeysFromSeed(seedPhrase)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error generating keys from seed ipc:', error)
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-11-10 15:50:35 +07:00
|
|
|
ipcMain.handle('wallet:buildTransaction', async (_event, args) => {
|
2025-11-07 18:27:37 +07:00
|
|
|
const { spendingKeyHex, inputAdditionRecords, outputAddresses, outputAmounts, fee } = args
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const builder = new neptuneNative.SimpleTransactionBuilder()
|
2025-11-10 15:50:35 +07:00
|
|
|
const result = await builder.buildTransaction(
|
2025-11-07 18:27:37 +07:00
|
|
|
import.meta.env.VITE_APP_API,
|
|
|
|
|
spendingKeyHex,
|
|
|
|
|
inputAdditionRecords,
|
2025-11-10 15:50:35 +07:00
|
|
|
typeof args?.minBlockHeight === 'number' && Number.isFinite(args.minBlockHeight)
|
|
|
|
|
? args.minBlockHeight
|
|
|
|
|
: 0,
|
2025-11-07 18:27:37 +07:00
|
|
|
outputAddresses,
|
|
|
|
|
outputAmounts,
|
|
|
|
|
fee
|
|
|
|
|
)
|
|
|
|
|
return JSON.parse(result)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error building transaction with primitive proof ipc:', error)
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
})
|