diff options
author | Kevin Serrano <kevgagser@gmail.com> | 2016-10-29 07:19:14 +0800 |
---|---|---|
committer | Kevin Serrano <kevgagser@gmail.com> | 2016-10-29 07:19:14 +0800 |
commit | 9b4f3825e78659148ea2c651c2622b3fb01c41b5 (patch) | |
tree | 44d5a0838a6670db7b32ef893c7e1b3fce5dab47 /test | |
parent | 6eb6968037aeacd9c876912634ca5f4b5287957b (diff) | |
parent | 331d9c91ee42f766fe56e2ebef1d1ad283d6c438 (diff) | |
download | tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.gz tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.bz2 tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.lz tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.xz tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.tar.zst tangerine-wallet-browser-9b4f3825e78659148ea2c651c2622b3fb01c41b5.zip |
Merge branch 'i328-MultiVault' of github.com:MetaMask/metamask-plugin into i328-MultiVault
Diffstat (limited to 'test')
-rw-r--r-- | test/unit/idStore-migration-test.js | 91 | ||||
-rw-r--r-- | test/unit/keyrings/hd-test.js | 97 |
2 files changed, 188 insertions, 0 deletions
diff --git a/test/unit/idStore-migration-test.js b/test/unit/idStore-migration-test.js new file mode 100644 index 000000000..df26c7f31 --- /dev/null +++ b/test/unit/idStore-migration-test.js @@ -0,0 +1,91 @@ +const async = require('async') +const assert = require('assert') +const ethUtil = require('ethereumjs-util') +const BN = ethUtil.BN +const configManagerGen = require('../lib/mock-config-manager') +const delegateCallCode = require('../lib/example-code.json').delegateCallCode + +// The old way: +const IdentityStore = require('../../app/scripts/lib/idStore') + +// The new ways: +var KeyringController = require('../../app/scripts/keyring-controller') +const mockEncryptor = require('../lib/mock-encryptor') +const MockSimpleKeychain = require('../lib/mock-simple-keychain') +const sinon = require('sinon') + +const mockVault = { + seed: 'picnic injury awful upper eagle junk alert toss flower renew silly vague', + account: '0x5d8de92c205279c10e5669f797b853ccef4f739a', +} + +describe('IdentityStore to KeyringController migration', function() { + + // The stars of the show: + let idStore, keyringController, seedWords + + let password = 'password123' + let entropy = 'entripppppyy duuude' + let accounts = [] + let newAccounts = [] + let originalKeystore + + // This is a lot of setup, I know! + // We have to create an old style vault, populate it, + // and THEN create a new one, before we can run tests on it. + beforeEach(function(done) { + this.sinon = sinon.sandbox.create() + window.localStorage = {} // Hacking localStorage support into JSDom + var configManager = configManagerGen() + + window.localStorage = {} // Hacking localStorage support into JSDom + + idStore = new IdentityStore({ + configManager: configManagerGen(), + ethStore: { + addAccount(acct) { accounts.push(ethUtil.addHexPrefix(acct)) }, + del(acct) { delete accounts[acct] }, + }, + }) + + idStore._createVault(password, mockVault.seed, null, function (err, seeds) { + assert.ifError(err, 'createNewVault threw error') + originalKeystore = idStore._idmgmt.keyStore + + idStore.setLocked(function(err) { + assert.ifError(err, 'createNewVault threw error') + keyringController = new KeyringController({ + configManager, + ethStore: { + addAccount(acct) { newAccounts.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 + done() + }) + }) + }) + + describe('creating new vault type', function() { + it('should use the password to migrate the old vault', function(done) { + keyringController.createNewVault(password, null, function (err, state) { + assert.ifError(err, 'createNewVault threw error') + + let newAccounts = keyringController.getAccounts() + let newAccount = ethUtil.addHexPrefix(newAccounts[0]) + assert.equal(newAccount, accounts[0], 'restored the account') + assert.equal(newAccount, mockVault.account, 'restored the correct account') + + const newSeed = keyringController.keyrings[0].mnemonic + assert.equal(newSeed, mockVault.seed, 'seed phrase transferred.') + + assert(configManager.getVault(), 'new type of vault is persisted') + done() + }) + }) + }) +}) + diff --git a/test/unit/keyrings/hd-test.js b/test/unit/keyrings/hd-test.js new file mode 100644 index 000000000..a2284995a --- /dev/null +++ b/test/unit/keyrings/hd-test.js @@ -0,0 +1,97 @@ +const assert = require('assert') +const extend = require('xtend') +const HdKeyring = require('../../../app/scripts/keyrings/hd') + +// Sample account: +const privKeyHex = 'b8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952' + +const sampleMnemonic = 'finish oppose decorate face calm tragic certain desk hour urge dinosaur mango' +const firstAcct = '1c96099350f13d558464ec79b9be4445aa0ef579' +const secondAcct = '1b00aed43a693f3a957f9feb5cc08afa031e37a0' + +describe('simple-keyring', function() { + + let keyring + beforeEach(function() { + keyring = new HdKeyring() + }) + + describe('Keyring.type()', function() { + it('is a class method that returns the type string.', function() { + const type = HdKeyring.type() + assert.equal(typeof type, 'string') + }) + }) + + describe('#type', function() { + it('returns the correct value', function() { + const type = keyring.type + const correct = HdKeyring.type() + assert.equal(type, correct) + }) + }) + + describe('#serialize empty wallets.', function() { + it('serializes a new mnemonic', function() { + const output = keyring.serialize() + assert.equal(output.n, 0) + assert.equal(output.mnemonic, null) + }) + }) + + describe('#deserialize a private key', function() { + it('serializes what it deserializes', function() { + keyring.deserialize({ + mnemonic: sampleMnemonic, + n: 1 + }) + assert.equal(keyring.wallets.length, 1, 'restores two accounts') + keyring.addAccounts(1) + + const accounts = keyring.getAccounts() + assert.equal(accounts[0], firstAcct) + assert.equal(accounts[1], secondAcct) + assert.equal(accounts.length, 2) + + const serialized = keyring.serialize() + assert.equal(serialized.mnemonic, sampleMnemonic) + }) + }) + + describe('#addAccounts', function() { + describe('with no arguments', function() { + it('creates a single wallet', function() { + keyring.addAccounts() + assert.equal(keyring.wallets.length, 1) + }) + }) + + describe('with a numeric argument', function() { + it('creates that number of wallets', function() { + keyring.addAccounts(3) + assert.equal(keyring.wallets.length, 3) + }) + }) + }) + + describe('#getAccounts', function() { + it('calls getAddress on each wallet', function() { + + // Push a mock wallet + const desiredOutput = 'foo' + keyring.wallets.push({ + getAddress() { + return { + toString() { + return desiredOutput + } + } + } + }) + + const output = keyring.getAccounts() + assert.equal(output[0], desiredOutput) + assert.equal(output.length, 1) + }) + }) +}) |