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

Adds validation function for passphrase #45

Merged
merged 1 commit into from
Mar 20, 2018
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
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')
})