From f4ef4df13976daa9392bd14dc5e07a9ea2170de4 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Mon, 19 Mar 2018 16:10:03 -0700 Subject: [PATCH] Adds validation function for passphrase --- index.js | 29 ++++++++++++++++++++++++++++- package.json | 2 +- test/indexTest.js | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f7a8ef0..a1d4aaf 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) } diff --git a/package.json b/package.json index 020a98c..5e0a232 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/indexTest.js b/test/indexTest.js index 12751fd..7963aca 100644 --- a/test/indexTest.js +++ b/test/indexTest.js @@ -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') +})