Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
Adds validation function for passphrase
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Mar 19, 2018
1 parent ec2c97d commit d101e90
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,27 @@ Client.prototype.recoverKeypair = function (passPhrase) {
return this.getKeypair()
}

Client.prototype.isValidPassPhrase = function (passPhrase) {
if (!passPhrase || typeof passPhrase !== 'string') {
return false
}

passPhrase = passPhrase.split(' ')

if (passPhrase.length !== PASSPHRASE_LENGTH) {
this.memo('isValidPassPhrase', `invalid passphrase: must be ${PASSPHRASE_LENGTH} words`)
return false
}
try {
niceware.passphraseToBytes(passPhrase)
} catch (ex) {
this.memo('isValidPassPhrase', ex.toString())
return false
}

return true
}

Client.prototype.recoverWallet = function (recoveryId, passPhrase, callback) {
const self = this
let path, keypair
Expand Down
14 changes: 14 additions & 0 deletions test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,17 @@ test('initWithFixupState', (t) => {
t.equal(crypto.uint8ToHex(signingKey.secretKey), crypto.uint8ToHex(signingKey2.secretKey))
t.equal(crypto.uint8ToHex(signingKey.publicKey), crypto.uint8ToHex(signingKey2.publicKey))
})

test('isValidPassPhrase', (t) => {
t.plan(4)

const client = new Ledger(null, options)

client.generateKeypair()
const passPhrase = client.getWalletPassphrase().join(' ')
t.equal(client.isValidPassPhrase(passPhrase), true, 'Should return true for valid passphrase')
t.equal(client.isValidPassPhrase(123), false, 'Should return false for number')
t.equal(client.isValidPassPhrase(null), false, 'Should return false for null')
t.equal(client.isValidPassPhrase(), false, 'Should return false for empty param')
t.equal(client.isValidPassPhrase('asdfasfsadf'), false, 'Should return false for random string')
})

0 comments on commit d101e90

Please sign in to comment.