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 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc authored Mar 20, 2018
1 parent ec2c97d commit 7ae5250
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
29 changes: 28 additions & 1 deletion 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 Expand Up @@ -697,9 +718,15 @@ Client.prototype.publishersInfo = function (publishers, callback) {
}

Client.prototype.memo = function (who, args) {
let what
if (!this.memos) this.memos = []
if (this.memos.length > 5) this.memos.splice(0, this.memos.length - 5)
this.memos.push(JSON.stringify({ who: who, what: args || {}, when: underscore.now() }))
if (typeof args !== 'object') {
what = {reason: args}
} else {
what = args
}
this.memos.push(JSON.stringify({ who: who, what: what || {}, when: underscore.now() }))

this._log(who, args)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bat-client",
"version": "2.1.0",
"version": "2.1.1",
"description": "An example of client code for the BAT.",
"main": "index.js",
"scripts": {
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(5)

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 7ae5250

Please sign in to comment.