Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow hex prefix when creating hot signer from a private key #597

Merged
merged 1 commit into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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