From dfdae8e33e5b7a94557e1a9ce0ff794192a3e34b Mon Sep 17 00:00:00 2001 From: Chen Yu Date: Mon, 12 Aug 2024 12:23:06 +0900 Subject: [PATCH] feat: encrypt blake160 list in the log (#3234) --- .../src/controllers/export-debug.ts | 8 ++++- .../src/services/log-encryption.ts | 4 +++ .../tests/controllers/export-debug.test.ts | 29 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/packages/neuron-wallet/src/controllers/export-debug.ts b/packages/neuron-wallet/src/controllers/export-debug.ts index c393984bd3..0504fe8ce7 100644 --- a/packages/neuron-wallet/src/controllers/export-debug.ts +++ b/packages/neuron-wallet/src/controllers/export-debug.ts @@ -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' @@ -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}`) } diff --git a/packages/neuron-wallet/src/services/log-encryption.ts b/packages/neuron-wallet/src/services/log-encryption.ts index 2ae29e9e19..b27dbcfc32 100644 --- a/packages/neuron-wallet/src/services/log-encryption.ts +++ b/packages/neuron-wallet/src/services/log-encryption.ts @@ -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 diff --git a/packages/neuron-wallet/tests/controllers/export-debug.test.ts b/packages/neuron-wallet/tests/controllers/export-debug.test.ts index 9e9f4b797e..f346649c3d 100644 --- a/packages/neuron-wallet/tests/controllers/export-debug.test.ts +++ b/packages/neuron-wallet/tests/controllers/export-debug.test.ts @@ -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' @@ -148,7 +161,7 @@ describe('Test ExportDebugController', () => { }) it('should call required methods', () => { - expect.assertions(9) + expect.assertions(10) expect(showSaveDialogMock).toHaveBeenCalled() expect(addBundledCKBLogMock).toHaveBeenCalled() @@ -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' })) @@ -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() + }) + }) })