Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: replaces instances of dialog with new bridged version #2421

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,20 @@ export default class GeneratedTransactionModal extends React.Component<

handleSave = async () => {
const { showSuccessNotification, showErrorNotification } = this.props
const { dialog, app } = electron
const { ipcRenderer } = electron
const path = await ipcRenderer.invoke('getPath', 'documents')
try {
dialog.showSaveDialog(
{
defaultPath: `${app.getPath(
'documents',
)}/neon-wallet-transaction-${moment().unix()}`,
ipcRenderer
.invoke('dialog', 'showSaveDialog', {
defaultPath: `${path}/neon-wallet-transaction-${moment().unix()}`,
filters: [
{
name: 'JSON',
extensions: ['json'],
},
],
},
fileName => {
})
.then(fileName => {
if (fileName === undefined) {
return
}
Expand All @@ -82,8 +81,7 @@ export default class GeneratedTransactionModal extends React.Component<
}
},
)
},
)
})
} catch (err) {
console.error(err)
showErrorNotification({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import SaveIcon from '../../../assets/icons/save-icon.svg'
import Button from '../../Button'
import Loading from '../../../containers/App/Loading'

const electron = require('electron')
const { ipcRenderer } = require('electron')

type Tx = {
type: number,
Expand Down Expand Up @@ -113,27 +113,29 @@ export default class GeneratedTransactionModal extends React.Component<

handleImport = async (isSignedRawTx: boolean = false) => {
const { showErrorNotification } = this.props
const { dialog } = electron
try {
dialog.showOpenDialog(fileName => {
if (fileName === undefined) {
return null
}
const rawData = fs.readFileSync(fileName[0])
ipcRenderer
.invoke('dialog', { properties: ['openFile'] })
.then(async fileName => {
if (fileName === undefined) {
return null
}

const data = isSignedRawTx
? rawData.toString('utf8')
: JSON.parse(rawData)
const transaction = isSignedRawTx ? data : JSON.stringify(data)
const rawData = fs.readFileSync(fileName[0])

return isSignedRawTx
? this.setState({
serializedTransactionInput: transaction,
})
: this.setState({
transaction,
})
})
const data = isSignedRawTx
? rawData.toString('utf8')
: JSON.parse(rawData)
const transaction = isSignedRawTx ? data : JSON.stringify(data)

return isSignedRawTx
? this.setState({
serializedTransactionInput: transaction,
})
: this.setState({
transaction,
})
})
} catch (err) {
console.error(err)
showErrorNotification({
Expand All @@ -144,21 +146,20 @@ export default class GeneratedTransactionModal extends React.Component<

handleSave = async (isSignedRawTx: boolean = false) => {
const { showSuccessNotification, showErrorNotification } = this.props
const { dialog, app } = electron

const path = await ipcRenderer.invoke('getPath', 'documents')
try {
dialog.showSaveDialog(
{
defaultPath: `${app.getPath(
'documents',
)}/neon-wallet-signed-transaction-${moment().unix()}`,
ipcRenderer
.invoke('dialog', 'showSaveDialog', {
defaultPath: `${path}}/neon-wallet-signed-transaction-${moment().unix()}`,
filters: [
{
name: isSignedRawTx ? 'TXT' : 'JSON',
extensions: isSignedRawTx ? ['txt'] : ['json'],
},
],
},
fileName => {
})
.then(fileName => {
if (fileName === undefined) {
return
}
Expand All @@ -181,8 +182,7 @@ export default class GeneratedTransactionModal extends React.Component<
}
},
)
},
)
})
} catch (err) {
console.error(err)
showErrorNotification({
Expand Down
28 changes: 18 additions & 10 deletions app/containers/Settings/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import SaveIcon from '../../assets/icons/save-icon.svg'
import pack from '../../../package.json'
import { LanguageSettingsIcon } from '../../components/Inputs/LanguageSelect/LanguageSelect'

const { dialog, shell } = require('electron')
const { ipcRenderer, shell } = require('electron')

type Props = {
setAccounts: (Array<Object>) => any,
Expand Down Expand Up @@ -76,7 +76,11 @@ export const loadWalletRecovery = async (
setN3Accounts: (Array<Object>) => any,
chain: string,
) => {
const { canceled, filePaths } = await dialog.showOpenDialog()
const { canceled, filePaths } = await ipcRenderer.invoke(
'dialog',
'showOpenDialog',
{ properties: ['openFile'] },
)
if (canceled || !filePaths) return

const filepath = filePaths[0]
Expand Down Expand Up @@ -148,14 +152,18 @@ export default class Settings extends Component<Props, State> {
})
return
}
const { filePath, canceled } = await dialog.showSaveDialog({
filters: [
{
name: 'JSON',
extensions: ['json'],
},
],
})
const { filePath, canceled } = await await ipcRenderer.invoke(
'dialog',
'showSaveDialog',
{
filters: [
{
name: 'JSON',
extensions: ['json'],
},
],
},
)

if (filePath && !canceled) {
fs.writeFile(filePath, content, errorWriting => {
Expand Down
10 changes: 5 additions & 5 deletions app/containers/TransactionHistory/TransactionHistory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
parseAbstractData,
} from '../../actions/transactionHistoryActions'

const { dialog, app } = require('electron')
const { ipcRenderer, app } = require('electron')

type Props = {
chain: string,
Expand Down Expand Up @@ -154,10 +154,10 @@ export default class TransactionHistory extends Component<Props, State> {
}),
)
hideNotification(infoNotification)
const result = await dialog.showSaveDialog({
defaultPath: `${app.getPath(
'documents',
)}/neon-wallet-activity-${moment().unix()}.csv`,
const path = await ipcRenderer.invoke('getPath', 'documents')

const result = await ipcRenderer.invoke('dialog', 'showSaveDialog', {
defaultPath: `${path}/neon-wallet-activity-${moment().unix()}.csv`,
filters: [
{
name: 'CSV',
Expand Down
17 changes: 7 additions & 10 deletions app/core/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const set = promisify(storage.set, storage)
const ENCRYPTED_FILES_WHITELIST = ['address']

export const setStorage = async (key, value, encrypt = false) => {
const path = await ipcRenderer.invoke('getPath')
const path = await ipcRenderer.invoke('getStoragePath')
storage.setDataPath(path)
const encryptedValue = await ipcRenderer.invoke(
'safeStorageEncrypt',
Expand All @@ -18,25 +18,22 @@ export const setStorage = async (key, value, encrypt = false) => {
}

export const getStorage = async key => {
const path = await ipcRenderer.invoke('getPath')
const path = await ipcRenderer.invoke('getStoragePath')
storage.setDataPath(path)
const value = await get(key)

const encryptionIsWhitelisted = !!ENCRYPTED_FILES_WHITELIST.find(fileName =>
key.toLowerCase().includes(fileName),
)
// If the file name being requested includes address
// and is NOT encrypted, we encrypt the file.
if (
key &&
!!ENCRYPTED_FILES_WHITELIST.find(fileName =>
key.toLowerCase().includes(fileName),
)
) {
if (key && encryptionIsWhitelisted) {
// if the value is a valid JS object it has not been encrypted
if (typeof value === 'object') {
await setStorage(key, value, true)
}
}
// Only encrypted values get stored as strings
if (typeof value === 'string') {
if (typeof value === 'string' && encryptionIsWhitelisted) {
const decryptedValue = await ipcRenderer.invoke('safeStorageDecrypt', value)
return JSON.parse(decryptedValue)
}
Expand Down
14 changes: 13 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const {
session,
ipcMain,
safeStorage,
dialog,
} = require('electron') // eslint-disable-line import/no-extraneous-dependencies
const path = require('path')
const url = require('url')
const { autoUpdater } = require('electron-updater')
const log = require('electron-log')
const fs = require('fs')

const port = process.env.PORT || 3000

Expand Down Expand Up @@ -261,7 +263,12 @@ ipcMain.on('closed', () => {
app.quit()
})

ipcMain.handle('getPath', async () => `${app.getPath('userData')}/storage`)
ipcMain.handle(
'getStoragePath',
async () => `${app.getPath('userData')}/storage`,
)

ipcMain.handle('getPath', async (event, folder) => app.getPath(folder))

ipcMain.handle('safeStorageEncrypt', async (event, value) => {
const buffer = safeStorage.encryptString(value)
Expand All @@ -275,6 +282,11 @@ ipcMain.handle('safeStorageDecrypt', async (event, value) => {
return plainText
})

ipcMain.handle('dialog', async (event, method, params) => {
const result = await dialog[method](params)
return result
})

autoUpdater.logger = log
autoUpdater.logger.transports.file.level = 'info'

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Neon",
"version": "2.13.0",
"version": "2.13.1",
"main": "./main.js",
"description": "Light wallet for NEO blockchain",
"homepage": "https://github.com/CityOfZion/neon-wallet",
Expand Down