Skip to content

Commit

Permalink
fix: read current password when constructing v3 wallet (#261)
Browse files Browse the repository at this point in the history
* fix: read current password when constructing v3 wallet

* refactor: add readWalletPasswordOrThrow helper
  • Loading branch information
Cafe137 authored Sep 9, 2022
1 parent 9d42b8b commit 81699f4
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 21 deletions.
6 changes: 6 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
transform: { '^.+\\.ts?$': 'ts-jest' },
testEnvironment: 'node',
testRegex: '/test/.*\\.spec\\.ts$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}
6 changes: 0 additions & 6 deletions jest.config.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions src/config-yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ export function writeConfigYaml(newValues: Record<string, unknown>) {
}
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
}
4 changes: 2 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -197,7 +197,7 @@ export function runServer() {

async function getPrivateKey(): Promise<string> {
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
Expand Down
46 changes: 46 additions & 0 deletions test/config-yaml.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
}
}
Empty file added test/data/.gitkeep
Empty file.
12 changes: 0 additions & 12 deletions test/path.spec.ts

This file was deleted.

0 comments on commit 81699f4

Please sign in to comment.