Skip to content

Commit

Permalink
feat: encrypt blake160 list in the log (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY authored Aug 12, 2024
1 parent dc6d50c commit dfdae8e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
8 changes: 7 additions & 1 deletion packages/neuron-wallet/src/controllers/export-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { generateRPC } from '../utils/ckb-rpc'
import { CKBLightRunner } from '../services/light-runner'
import { LIGHT_CLIENT_MAINNET, LIGHT_CLIENT_TESTNET } from '../utils/const'
import WalletsService from '../services/wallets'
import LogEncryption from '../services/log-encryption'

export default class ExportDebugController {
#I18N_PATH = 'export-debug-info'
Expand Down Expand Up @@ -158,7 +159,12 @@ export default class ExportDebugController {
csv += row
}
const csvFileName = 'hd_public_key_info.csv'
this.archive.append(csv, { name: csvFileName })
const encryption = LogEncryption.getInstance()
if (encryption.isEnabled) {
this.archive.append(encryption.encrypt(csv), { name: `encrypted_${csvFileName}` })
} else {
this.archive.append(csv, { name: csvFileName })
}
} catch (error) {
logger.error(`Export Debug:\t export public key info error: ${error}`)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/neuron-wallet/src/services/log-encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export default class LogEncryption {
*/
private readonly adminPublicKey: string

public get isEnabled(): boolean {
return !!this.adminPublicKey
}

/**
*
* @param adminPublicKey a PEM-formatted RSA public key
Expand Down
29 changes: 28 additions & 1 deletion packages/neuron-wallet/tests/controllers/export-debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ jest.mock('../../src/services/wallets', () => {
}
})

const encryptionMock = {
isEnabled: false,
encrypt: jest.fn(v => v),
}

jest.mock('../../src/services/log-encryption.ts', () => {
return {
getInstance() {
return encryptionMock
},
}
})

import { dialog } from 'electron'
import logger from '../../src/utils/logger'
import ExportDebugController from '../../src/controllers/export-debug'
Expand Down Expand Up @@ -148,7 +161,7 @@ describe('Test ExportDebugController', () => {
})

it('should call required methods', () => {
expect.assertions(9)
expect.assertions(10)
expect(showSaveDialogMock).toHaveBeenCalled()

expect(addBundledCKBLogMock).toHaveBeenCalled()
Expand All @@ -159,6 +172,7 @@ describe('Test ExportDebugController', () => {
expect(showMessageBoxMock).toHaveBeenCalled()
expect(showErrorBoxMock).not.toHaveBeenCalled()
expect(logger.error).not.toHaveBeenCalled()
expect(encryptionMock.encrypt).not.toHaveBeenCalled()

const csv = ['index,addressType,addressIndex,publicKeyInBlake160\n', '0,0,0,hash1\n', '1,1,1,hash2\n'].join('')
expect(archiveAppendMock).toHaveBeenCalledWith(csv, expect.objectContaining({ name: 'hd_public_key_info.csv' }))
Expand Down Expand Up @@ -200,4 +214,17 @@ describe('Test ExportDebugController', () => {
expect(showErrorBoxMock).toHaveBeenCalled()
})
})

describe('when encryption is enabled', () => {
beforeEach(() => {
encryptionMock.isEnabled = true
showSaveDialogMock.mockResolvedValue({ canceled: false, filePath: 'mock_path' })
return exportDebugController.export()
})

it('encrypt should be called', () => {
expect.assertions(1)
expect(encryptionMock.encrypt).toHaveBeenCalled()
})
})
})

1 comment on commit dfdae8e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10345391011

Please sign in to comment.