From ae8927b78e1a3b9ad642d2307c2281811f331ead 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 | 21 +++++++++++++++++++++ package.json | 2 +- test/indexTest.js | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f7a8ef0..f3c7d87 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 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..22f9a9f 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(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') +})