mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Create basic keyring-controller unit test file
This commit is contained in:
parent
c3e1c5c57f
commit
44aa1be277
@ -17,6 +17,7 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
super()
|
super()
|
||||||
this.configManager = opts.configManager
|
this.configManager = opts.configManager
|
||||||
this.ethStore = opts.ethStore
|
this.ethStore = opts.ethStore
|
||||||
|
this.encryptor = encryptor
|
||||||
this.keyrings = []
|
this.keyrings = []
|
||||||
this.identities = {} // Essentially a name hash
|
this.identities = {} // Essentially a name hash
|
||||||
}
|
}
|
||||||
@ -46,11 +47,11 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
createNewVault(password, entropy, cb) {
|
createNewVault(password, entropy, cb) {
|
||||||
const salt = generateSalt()
|
const salt = this.encryptor.generateSalt()
|
||||||
this.configManager.setSalt(salt)
|
this.configManager.setSalt(salt)
|
||||||
this.loadKey(password)
|
this.loadKey(password)
|
||||||
.then((key) => {
|
.then((key) => {
|
||||||
return encryptor.encryptWithKey(key, [])
|
return this.encryptor.encryptWithKey(key, [])
|
||||||
})
|
})
|
||||||
.then((encryptedString) => {
|
.then((encryptedString) => {
|
||||||
this.configManager.setVault(encryptedString)
|
this.configManager.setVault(encryptedString)
|
||||||
@ -75,8 +76,8 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
loadKey(password) {
|
loadKey(password) {
|
||||||
const salt = this.configManager.getSalt() || generateSalt()
|
const salt = this.configManager.getSalt() || this.encryptor.generateSalt()
|
||||||
return encryptor.keyFromPassword(password + salt)
|
return this.encryptor.keyFromPassword(password + salt)
|
||||||
.then((key) => {
|
.then((key) => {
|
||||||
this.key = key
|
this.key = key
|
||||||
return key
|
return key
|
||||||
@ -134,7 +135,7 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
data: k.serialize(),
|
data: k.serialize(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return encryptor.encryptWithKey(this.key, serialized)
|
return this.encryptor.encryptWithKey(this.key, serialized)
|
||||||
.then((encryptedString) => {
|
.then((encryptedString) => {
|
||||||
this.configManager.setVault(encryptedString)
|
this.configManager.setVault(encryptedString)
|
||||||
return true
|
return true
|
||||||
@ -146,7 +147,7 @@ module.exports = class KeyringController extends EventEmitter {
|
|||||||
|
|
||||||
unlockKeyrings(key) {
|
unlockKeyrings(key) {
|
||||||
const encryptedVault = this.configManager.getVault()
|
const encryptedVault = this.configManager.getVault()
|
||||||
return encryptor.decryptWithKey(key, encryptedVault)
|
return this.encryptor.decryptWithKey(key, encryptedVault)
|
||||||
.then((vault) => {
|
.then((vault) => {
|
||||||
this.keyrings = vault.map(this.restoreKeyring.bind(this, 0))
|
this.keyrings = vault.map(this.restoreKeyring.bind(this, 0))
|
||||||
return this.keyrings
|
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
|
|
||||||
}
|
|
||||||
|
@ -22,6 +22,8 @@ module.exports = {
|
|||||||
// Buffer <-> base64 string methods
|
// Buffer <-> base64 string methods
|
||||||
encodeBufferToBase64,
|
encodeBufferToBase64,
|
||||||
decodeBase64ToBuffer,
|
decodeBase64ToBuffer,
|
||||||
|
|
||||||
|
generateSalt,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Takes a Pojo, returns encrypted text.
|
// Takes a Pojo, returns encrypted text.
|
||||||
@ -135,3 +137,10 @@ function decodeBase64ToBuffer (base64) {
|
|||||||
}))
|
}))
|
||||||
return buf
|
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
|
||||||
|
}
|
||||||
|
32
test/lib/mock-encryptor.js
Normal file
32
test/lib/mock-encryptor.js
Normal file
@ -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!'
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
@ -3,10 +3,10 @@ var KeyringController = require('../../app/scripts/keyring-controller')
|
|||||||
var configManagerGen = require('../lib/mock-config-manager')
|
var configManagerGen = require('../lib/mock-config-manager')
|
||||||
const ethUtil = require('ethereumjs-util')
|
const ethUtil = require('ethereumjs-util')
|
||||||
const async = require('async')
|
const async = require('async')
|
||||||
|
const mockEncryptor = require('../lib/mock-encryptor')
|
||||||
|
|
||||||
describe('KeyringController', function() {
|
describe('KeyringController', function() {
|
||||||
|
|
||||||
describe('#createNewVault', function () {
|
|
||||||
let keyringController
|
let keyringController
|
||||||
let password = 'password123'
|
let password = 'password123'
|
||||||
let entropy = 'entripppppyy duuude'
|
let entropy = 'entripppppyy duuude'
|
||||||
@ -14,7 +14,7 @@ describe('KeyringController', function() {
|
|||||||
let accounts = []
|
let accounts = []
|
||||||
let originalKeystore
|
let originalKeystore
|
||||||
|
|
||||||
before(function(done) {
|
beforeEach(function() {
|
||||||
window.localStorage = {} // Hacking localStorage support into JSDom
|
window.localStorage = {} // Hacking localStorage support into JSDom
|
||||||
|
|
||||||
keyringController = new KeyringController({
|
keyringController = new KeyringController({
|
||||||
@ -24,12 +24,27 @@ describe('KeyringController', function() {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
keyringController.createNewVault(password, entropy, (err, seeds) => {
|
// Stub out the browser crypto for a mock encryptor.
|
||||||
assert.ifError(err, 'createNewVault threw error')
|
// Browser crypto is tested in the integration test suite.
|
||||||
seedWords = seeds
|
keyringController.encryptor = mockEncryptor
|
||||||
originalKeystore = keyringController._idmgmt.keyStore
|
})
|
||||||
|
|
||||||
|
describe('#createNewVault', function () {
|
||||||
|
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')
|
||||||
|
|
||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user