Skip to content

Commit

Permalink
Allow hex prefix when creating hot signer from a private key (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholtzman authored Oct 11, 2021
1 parent 44be2c2 commit e1cc779
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions main/signers/hot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions test/signers/ring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
})
})

Expand Down

0 comments on commit e1cc779

Please sign in to comment.