aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/keyrings
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2017-01-17 07:02:24 +0800
committerGitHub <noreply@github.com>2017-01-17 07:02:24 +0800
commitee62a6a3913967a6840d28ca812b94bbfa7a5779 (patch)
treecac32de6cc520917db9621ce93f7f4ad9abd787e /test/unit/keyrings
parente8e06542e47b41668f196b6db54290490003845b (diff)
parent88fabdd2de78d0dd4be86b02c94a64d9748667fa (diff)
downloadtangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.gz
tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.tar.zst
tangerine-wallet-browser-ee62a6a3913967a6840d28ca812b94bbfa7a5779.zip
Merge pull request #1005 from MetaMask/dev
Merge dev into master!
Diffstat (limited to 'test/unit/keyrings')
-rw-r--r--test/unit/keyrings/hd-test.js127
-rw-r--r--test/unit/keyrings/simple-test.js91
2 files changed, 218 insertions, 0 deletions
diff --git a/test/unit/keyrings/hd-test.js b/test/unit/keyrings/hd-test.js
new file mode 100644
index 000000000..dfc0ec908
--- /dev/null
+++ b/test/unit/keyrings/hd-test.js
@@ -0,0 +1,127 @@
+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('hd-keyring', function() {
+
+ let keyring
+ beforeEach(function() {
+ keyring = new HdKeyring()
+ })
+
+ describe('constructor', function(done) {
+ keyring = new HdKeyring({
+ mnemonic: sampleMnemonic,
+ numberOfAccounts: 2,
+ })
+
+ const accounts = keyring.getAccounts()
+ .then((accounts) => {
+ assert.equal(accounts[0], firstAcct)
+ assert.equal(accounts[1], secondAcct)
+ done()
+ })
+ })
+
+ describe('Keyring.type', function() {
+ it('is a class property 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() {
+ keyring.serialize()
+ .then((output) => {
+ assert.equal(output.numberOfAccounts, 0)
+ assert.equal(output.mnemonic, null)
+ })
+ })
+ })
+
+ describe('#deserialize a private key', function() {
+ it('serializes what it deserializes', function(done) {
+ keyring.deserialize({
+ mnemonic: sampleMnemonic,
+ numberOfAccounts: 1
+ })
+ .then(() => {
+ assert.equal(keyring.wallets.length, 1, 'restores two accounts')
+ return keyring.addAccounts(1)
+ }).then(() => {
+ return keyring.getAccounts()
+ }).then((accounts) => {
+ assert.equal(accounts[0], firstAcct)
+ assert.equal(accounts[1], secondAcct)
+ assert.equal(accounts.length, 2)
+
+ return keyring.serialize()
+ }).then((serialized) => {
+ assert.equal(serialized.mnemonic, sampleMnemonic)
+ done()
+ })
+ })
+ })
+
+ describe('#addAccounts', function() {
+ describe('with no arguments', function() {
+ it('creates a single wallet', function(done) {
+ keyring.addAccounts()
+ .then(() => {
+ assert.equal(keyring.wallets.length, 1)
+ done()
+ })
+ })
+ })
+
+ describe('with a numeric argument', function() {
+ it('creates that number of wallets', function(done) {
+ keyring.addAccounts(3)
+ .then(() => {
+ assert.equal(keyring.wallets.length, 3)
+ done()
+ })
+ })
+ })
+ })
+
+ describe('#getAccounts', function() {
+ it('calls getAddress on each wallet', function(done) {
+
+ // Push a mock wallet
+ const desiredOutput = 'foo'
+ keyring.wallets.push({
+ getAddress() {
+ return {
+ toString() {
+ return desiredOutput
+ }
+ }
+ }
+ })
+
+ const output = keyring.getAccounts()
+ .then((output) => {
+ assert.equal(output[0], desiredOutput)
+ assert.equal(output.length, 1)
+ done()
+ })
+ })
+ })
+})
diff --git a/test/unit/keyrings/simple-test.js b/test/unit/keyrings/simple-test.js
new file mode 100644
index 000000000..687318f99
--- /dev/null
+++ b/test/unit/keyrings/simple-test.js
@@ -0,0 +1,91 @@
+const assert = require('assert')
+const extend = require('xtend')
+const ethUtil = require('ethereumjs-util')
+const SimpleKeyring = require('../../../app/scripts/keyrings/simple')
+const TYPE_STR = 'Simple Key Pair'
+
+// Sample account:
+const privKeyHex = 'b8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952'
+
+describe('simple-keyring', function() {
+
+ let keyring
+ beforeEach(function() {
+ keyring = new SimpleKeyring()
+ })
+
+ describe('Keyring.type', function() {
+ it('is a class property that returns the type string.', function() {
+ const type = SimpleKeyring.type
+ assert.equal(type, TYPE_STR)
+ })
+ })
+
+ describe('#type', function() {
+ it('returns the correct value', function() {
+ const type = keyring.type
+ assert.equal(type, TYPE_STR)
+ })
+ })
+
+ describe('#serialize empty wallets.', function() {
+ it('serializes an empty array', function(done) {
+ keyring.serialize()
+ .then((output) => {
+ assert.deepEqual(output, [])
+ done()
+ })
+ })
+ })
+
+ describe('#deserialize a private key', function() {
+ it('serializes what it deserializes', function() {
+ keyring.deserialize([privKeyHex])
+ .then(() => {
+ assert.equal(keyring.wallets.length, 1, 'has one wallet')
+ const serialized = keyring.serialize()
+ assert.equal(serialized[0], privKeyHex)
+ })
+ })
+ })
+
+ describe('#addAccounts', function() {
+ describe('with no arguments', function() {
+ it('creates a single wallet', function() {
+ keyring.addAccounts()
+ .then(() => {
+ assert.equal(keyring.wallets.length, 1)
+ })
+ })
+ })
+
+ describe('with a numeric argument', function() {
+ it('creates that number of wallets', function() {
+ keyring.addAccounts(3)
+ .then(() => {
+ assert.equal(keyring.wallets.length, 3)
+ })
+ })
+ })
+ })
+
+ describe('#getAccounts', function() {
+ it('calls getAddress on each wallet', function(done) {
+
+ // Push a mock wallet
+ const desiredOutput = '0x18a3462427bcc9133bb46e88bcbe39cd7ef0e761'
+ keyring.wallets.push({
+ getAddress() {
+ return ethUtil.toBuffer(desiredOutput)
+ }
+ })
+
+ keyring.getAccounts()
+ .then((output) => {
+ assert.equal(output[0], desiredOutput)
+ assert.equal(output.length, 1)
+ done()
+ })
+ })
+ })
+})