From 81699f43cd15d931b5e0e24666be07dbf4d816a9 Mon Sep 17 00:00:00 2001 From: Cafe137 <77121044+Cafe137@users.noreply.github.com> Date: Fri, 9 Sep 2022 13:05:02 +0200 Subject: [PATCH] fix: read current password when constructing v3 wallet (#261) * fix: read current password when constructing v3 wallet * refactor: add readWalletPasswordOrThrow helper --- jest.config.js | 6 ++++++ jest.config.ts | 6 ------ package.json | 2 +- src/config-yaml.ts | 13 ++++++++++++ src/server.ts | 4 ++-- test/config-yaml.spec.ts | 46 ++++++++++++++++++++++++++++++++++++++++ test/data/.gitkeep | 0 test/path.spec.ts | 12 ----------- 8 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 jest.config.js delete mode 100644 jest.config.ts create mode 100644 test/config-yaml.spec.ts create mode 100644 test/data/.gitkeep delete mode 100644 test/path.spec.ts diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..16ebe2b --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + transform: { '^.+\\.ts?$': 'ts-jest' }, + testEnvironment: 'node', + testRegex: '/test/.*\\.spec\\.ts$', + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], +} diff --git a/jest.config.ts b/jest.config.ts deleted file mode 100644 index 20aa0a8..0000000 --- a/jest.config.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ -export default { - preset: 'ts-jest', - runner: '@kayahr/jest-electron-runner/main', - testEnvironment: 'node', -}; diff --git a/package.json b/package.json index 9d8838b..1702713 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lint": "eslint --fix \"src/**/*.ts\" && cd ui && npm run lint", "lint:check": "eslint \"src/**/*.ts\" && cd ui && npm run lint:check", "check:types": "tsc --project tsconfig.test.json && cd ui && tsc --project tsconfig.test.json", - "test:unit": "jest --verbose -config=jest.config.ts " + "test:unit": "jest --verbose --config=jest.config.js" }, "keywords": [ "bee", diff --git a/src/config-yaml.ts b/src/config-yaml.ts index 0ea836c..62613d5 100644 --- a/src/config-yaml.ts +++ b/src/config-yaml.ts @@ -22,3 +22,16 @@ export function writeConfigYaml(newValues: Record) { } writeFileSync(getPath('config.yaml'), dump(data)) } + +export function readWalletPasswordOrThrow(): string { + if (!configYamlExists()) { + throw Error('Attempted to read password, but config.yaml is not found') + } + const config = readConfigYaml() + + if (!config.password) { + throw Error('Attempted to read password, but config.yaml does not contain it') + } + + return config.password as string +} diff --git a/src/server.ts b/src/server.ts index 588b8e0..43e2bce 100644 --- a/src/server.ts +++ b/src/server.ts @@ -13,7 +13,7 @@ import { URL } from 'url' import PACKAGE_JSON from '../package.json' import { getApiKey } from './api-key' import { sendBzzTransaction, sendNativeTransaction } from './blockchain' -import { readConfigYaml, writeConfigYaml } from './config-yaml' +import { readConfigYaml, readWalletPasswordOrThrow, writeConfigYaml } from './config-yaml' import { rebuildElectronTray } from './electron' import { createConfigFileAndAddress, createInitialTransaction, runLauncher } from './launcher' import { BeeManager } from './lifecycle' @@ -197,7 +197,7 @@ export function runServer() { async function getPrivateKey(): Promise { const v3 = await readFile(getPath(path.join('data-dir', 'keys', 'swarm.key')), 'utf-8') - const wallet = await Wallet.fromV3(v3, 'Test') + const wallet = await Wallet.fromV3(v3, readWalletPasswordOrThrow()) const privateKeyString = wallet.getPrivateKeyString() return privateKeyString diff --git a/test/config-yaml.spec.ts b/test/config-yaml.spec.ts new file mode 100644 index 0000000..3936bfe --- /dev/null +++ b/test/config-yaml.spec.ts @@ -0,0 +1,46 @@ +import { existsSync, unlinkSync, writeFileSync } from 'fs-extra' +import { configYamlExists, readConfigYaml, readWalletPasswordOrThrow, writeConfigYaml } from '../src/config-yaml' + +jest.mock('env-paths', () => + jest.fn().mockImplementation(() => ({ + data: 'test/data', + config: 'test/data', + cache: 'test/data', + log: 'test/data', + temp: 'test/data', + })), +) + +describe('config-yaml module', () => { + beforeEach(cleanUp) + afterAll(cleanUp) + + test('check existence', () => { + expect(configYamlExists()).toBe(false) + writeFileSync('test/data/config.yaml', '') + expect(configYamlExists()).toBe(true) + }) + + test('read and write', () => { + writeFileSync('test/data/config.yaml', 'test: true') + writeConfigYaml({ + abc: 123, + }) + expect(readConfigYaml()).toHaveProperty('test', 'true') + expect(readConfigYaml()).toHaveProperty('abc', '123') + }) + + test('read password', () => { + expect(() => readWalletPasswordOrThrow()).toThrow('Attempted to read password, but config.yaml is not found') + writeFileSync('test/data/config.yaml', 'test: true') + expect(() => readWalletPasswordOrThrow()).toThrow('Attempted to read password, but config.yaml does not contain it') + writeFileSync('test/data/config.yaml', 'password: test') + expect(readWalletPasswordOrThrow()).toBe('test') + }) +}) + +function cleanUp() { + if (existsSync('test/data/config.yaml')) { + unlinkSync('test/data/config.yaml') + } +} diff --git a/test/data/.gitkeep b/test/data/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/path.spec.ts b/test/path.spec.ts deleted file mode 100644 index f5190e3..0000000 --- a/test/path.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { resolve } from 'path' -import { getPath } from '../src/path' - -describe('resolvePath', () => { - it('should read path ', async () => { - const filename = 'test-file.txt' - const expectedPath = resolve(filename) - const path = getPath(filename) - - expect(path).toBe(expectedPath) - }) -})