From 51d682cc85563a0460ebbab1901987db1bda0a00 Mon Sep 17 00:00:00 2001 From: Antonio Morrone Date: Mon, 24 Jul 2023 11:07:30 +0200 Subject: [PATCH] fix(RREL-013): create random seed using randomBytes rather than the private key of a random wallet (#115) --- package.json | 2 +- src/KeyManager.ts | 10 +++++++--- tsconfig.build.json | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 849047e..13c7cc1 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,7 @@ "register": "ts-node src/commands/Register.ts", "start": "ts-node src/commands/Start.ts", "tdd": "npm run test -- --watch --watch-files src,test", - "test": "ALLOW_CONFIG_MUTATIONS=true npx mocha -r ts-node/register --extensions ts 'test/**/*.{test,spec}.ts'" + "test": "ALLOW_CONFIG_MUTATIONS=true npx mocha -r ts-node/register --extensions ts 'test/**/*.{test,spec}.ts'" }, "lint-staged": { "*": "npx embedme \"*.md\"", diff --git a/src/KeyManager.ts b/src/KeyManager.ts index d073f7d..57c05ee 100644 --- a/src/KeyManager.ts +++ b/src/KeyManager.ts @@ -40,7 +40,7 @@ export class KeyManager { ) as keystore; genseed = seedObject.seed; } else { - genseed = Wallet.createRandom().privateKey; + genseed = this.generateRandomSeed(); fs.writeFileSync(keyStorePath, JSON.stringify({ seed: genseed }), { flag: 'w', }); @@ -56,7 +56,7 @@ export class KeyManager { } else { // no workdir: working in-memory if (seed == null) { - seed = Wallet.createRandom().privateKey; + seed = this.generateRandomSeed(); } this._hdkey = utils.HDNode.fromSeed(seed ?? Buffer.from('')); } @@ -64,7 +64,11 @@ export class KeyManager { this.generateKeys(count); } - generateKeys(count: number): void { + private generateRandomSeed() { + return Buffer.from(utils.randomBytes(16).buffer).toString('hex'); + } + + private generateKeys(count: number): void { this._privateKeys = {}; this._nonces = {}; for (let index = 0; index < count; index++) { diff --git a/tsconfig.build.json b/tsconfig.build.json index c61c65b..10e5fbc 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -2,5 +2,5 @@ "extends": "./tsconfig.json", "exclude": [ "test" - ] + ] }