From e1cc7795ae3ed42cc2703959e8ab6bdd25d825dc Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Mon, 11 Oct 2021 18:37:19 -0400 Subject: [PATCH] Allow hex prefix when creating hot signer from a private key (#597) --- main/signers/hot/index.js | 8 ++++++-- test/signers/ring.test.js | 15 +++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main/signers/hot/index.js b/main/signers/hot/index.js index 4ea66802b..054c2527f 100644 --- a/main/signers/hot/index.js +++ b/main/signers/hot/index.js @@ -10,6 +10,7 @@ const crypt = require('../../crypt') const SeedSigner = require('./SeedSigner') const RingSigner = require('./RingSigner') +const { stripHexPrefix } = require('ethereumjs-util') const USER_DATA = app ? app.getPath('userData') : './test/.userData' const SIGNERS_PATH = path.resolve(USER_DATA, 'signers') @@ -51,12 +52,15 @@ module.exports = { }) }, createFromPrivateKey: (signers, privateKey, password, cb) => { - if (!privateKey) return cb(new Error('Private key required to create hot signer')) + const privateKeyHex = stripHexPrefix(privateKey) + + if (!privateKeyHex) return cb(new Error('Private key required to create hot signer')) if (!password) return cb(new Error('Password required to create hot signer')) if (password.length < 12) return cb(new Error('Hot account password is too short')) if (zxcvbn(password).score < 3) return cb(new Error('Hot account password is too weak')) const signer = new RingSigner() - signer.addPrivateKey(privateKey, password, (err, result) => { + + signer.addPrivateKey(privateKeyHex, password, err => { if (err) { signer.close() return cb(err) diff --git a/test/signers/ring.test.js b/test/signers/ring.test.js index 7e5a86e4e..1df18a819 100644 --- a/test/signers/ring.test.js +++ b/test/signers/ring.test.js @@ -58,14 +58,17 @@ describe('Ring signer', () => { }) test('Create from private key', (done) => { - const privateKey = crypto.randomBytes(32).toString('hex') + const privateKey = '0x' + crypto.randomBytes(32).toString('hex') hot.createFromPrivateKey(signers, privateKey, PASSWORD, (err, result) => { signer = result - expect(err).toBe(null) - expect(signer.status).toBe('locked') - expect(signer.id).not.toBe(undefined) - expect(store(`main.signers.${signer.id}.id`)).toBe(signer.id) - done() + + try { + expect(err).toBe(null) + expect(signer.status).toBe('locked') + expect(signer.id).not.toBe(undefined) + expect(store(`main.signers.${signer.id}.id`)).toBe(signer.id) + done() + } catch(e) { done(e) } }) })