aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2016-10-22 03:11:54 +0800
committerDan Finlay <dan@danfinlay.com>2016-10-22 03:11:54 +0800
commit44aa1be2778a1647c9a607fd02c61bf93704d92d (patch)
treea99d27dbcac5209950ffde79c53519fde2eaa621
parentc3e1c5c57f2062155626647e239c2a760f3e4b8a (diff)
downloadtangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar.gz
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar.bz2
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar.lz
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar.xz
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.tar.zst
tangerine-wallet-browser-44aa1be2778a1647c9a607fd02c61bf93704d92d.zip
Create basic keyring-controller unit test file
-rw-r--r--app/scripts/keyring-controller.js19
-rw-r--r--app/scripts/lib/encryptor.js9
-rw-r--r--test/lib/mock-encryptor.js32
-rw-r--r--test/unit/keyring-controller-test.js55
4 files changed, 83 insertions, 32 deletions
diff --git a/app/scripts/keyring-controller.js b/app/scripts/keyring-controller.js
index 8192ed790..5cf2542cc 100644
--- a/app/scripts/keyring-controller.js
+++ b/app/scripts/keyring-controller.js
@@ -17,6 +17,7 @@ module.exports = class KeyringController extends EventEmitter {
super()
this.configManager = opts.configManager
this.ethStore = opts.ethStore
+ this.encryptor = encryptor
this.keyrings = []
this.identities = {} // Essentially a name hash
}
@@ -46,11 +47,11 @@ module.exports = class KeyringController extends EventEmitter {
}
createNewVault(password, entropy, cb) {
- const salt = generateSalt()
+ const salt = this.encryptor.generateSalt()
this.configManager.setSalt(salt)
this.loadKey(password)
.then((key) => {
- return encryptor.encryptWithKey(key, [])
+ return this.encryptor.encryptWithKey(key, [])
})
.then((encryptedString) => {
this.configManager.setVault(encryptedString)
@@ -75,8 +76,8 @@ module.exports = class KeyringController extends EventEmitter {
}
loadKey(password) {
- const salt = this.configManager.getSalt() || generateSalt()
- return encryptor.keyFromPassword(password + salt)
+ const salt = this.configManager.getSalt() || this.encryptor.generateSalt()
+ return this.encryptor.keyFromPassword(password + salt)
.then((key) => {
this.key = key
return key
@@ -134,7 +135,7 @@ module.exports = class KeyringController extends EventEmitter {
data: k.serialize(),
}
})
- return encryptor.encryptWithKey(this.key, serialized)
+ return this.encryptor.encryptWithKey(this.key, serialized)
.then((encryptedString) => {
this.configManager.setVault(encryptedString)
return true
@@ -146,7 +147,7 @@ module.exports = class KeyringController extends EventEmitter {
unlockKeyrings(key) {
const encryptedVault = this.configManager.getVault()
- return encryptor.decryptWithKey(key, encryptedVault)
+ return this.encryptor.decryptWithKey(key, encryptedVault)
.then((vault) => {
this.keyrings = vault.map(this.restoreKeyring.bind(this, 0))
return this.keyrings
@@ -278,9 +279,3 @@ module.exports = class KeyringController extends EventEmitter {
}
-function generateSalt (byteCount = 32) {
- var view = new Uint8Array(byteCount)
- global.crypto.getRandomValues(view)
- var b64encoded = btoa(String.fromCharCode.apply(null, view))
- return b64encoded
-}
diff --git a/app/scripts/lib/encryptor.js b/app/scripts/lib/encryptor.js
index 3d069ab33..832e6d528 100644
--- a/app/scripts/lib/encryptor.js
+++ b/app/scripts/lib/encryptor.js
@@ -22,6 +22,8 @@ module.exports = {
// Buffer <-> base64 string methods
encodeBufferToBase64,
decodeBase64ToBuffer,
+
+ generateSalt,
}
// Takes a Pojo, returns encrypted text.
@@ -135,3 +137,10 @@ function decodeBase64ToBuffer (base64) {
}))
return buf
}
+
+function generateSalt (byteCount = 32) {
+ var view = new Uint8Array(byteCount)
+ global.crypto.getRandomValues(view)
+ var b64encoded = btoa(String.fromCharCode.apply(null, view))
+ return b64encoded
+}
diff --git a/test/lib/mock-encryptor.js b/test/lib/mock-encryptor.js
new file mode 100644
index 000000000..09bbf7ad5
--- /dev/null
+++ b/test/lib/mock-encryptor.js
@@ -0,0 +1,32 @@
+var mockHex = '0xabcdef0123456789'
+var mockKey = new Buffer(32)
+let cacheVal
+
+module.exports = {
+
+ encrypt(password, dataObj) {
+ cacheVal = dataObj
+ return Promise.resolve(mockHex)
+ },
+
+ decrypt(password, text) {
+ return Promise.resolve(cacheVal || {})
+ },
+
+ encryptWithKey(key, dataObj) {
+ return this.encrypt(key, dataObj)
+ },
+
+ decryptWithKey(key, text) {
+ return this.decrypt(key, text)
+ },
+
+ keyFromPassword(password) {
+ return Promise.resolve(mockKey)
+ },
+
+ generateSalt() {
+ return 'WHADDASALT!'
+ },
+
+}
diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js
index d3685a257..fbcd3ba81 100644
--- a/test/unit/keyring-controller-test.js
+++ b/test/unit/keyring-controller-test.js
@@ -3,33 +3,48 @@ var KeyringController = require('../../app/scripts/keyring-controller')
var configManagerGen = require('../lib/mock-config-manager')
const ethUtil = require('ethereumjs-util')
const async = require('async')
+const mockEncryptor = require('../lib/mock-encryptor')
describe('KeyringController', function() {
+ let keyringController
+ let password = 'password123'
+ let entropy = 'entripppppyy duuude'
+ let seedWords
+ let accounts = []
+ let originalKeystore
+
+ beforeEach(function() {
+ window.localStorage = {} // Hacking localStorage support into JSDom
+
+ keyringController = new KeyringController({
+ configManager: configManagerGen(),
+ ethStore: {
+ addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
+ },
+ })
+
+ // Stub out the browser crypto for a mock encryptor.
+ // Browser crypto is tested in the integration test suite.
+ keyringController.encryptor = mockEncryptor
+ })
+
describe('#createNewVault', function () {
- let keyringController
- let password = 'password123'
- let entropy = 'entripppppyy duuude'
- let seedWords
- let accounts = []
- let originalKeystore
-
- before(function(done) {
- window.localStorage = {} // Hacking localStorage support into JSDom
-
- keyringController = new KeyringController({
- configManager: configManagerGen(),
- ethStore: {
- addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
- },
- })
+ it('should set a vault on the configManager', function(done) {
+ assert(!keyringController.configManager.getVault(), 'no previous vault')
+ keyringController.createNewVault(password, null, function (err, state) {
+ assert.ifError(err)
+ const vault = keyringController.configManager.getVault()
+ assert(vault, 'vault created')
- keyringController.createNewVault(password, entropy, (err, seeds) => {
- assert.ifError(err, 'createNewVault threw error')
- seedWords = seeds
- originalKeystore = keyringController._idmgmt.keyStore
done()
})
})
})
+
})
+
+
+
+
+