2016-10-28 02:23:26 +02:00
|
|
|
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'
|
|
|
|
|
2016-11-03 00:35:46 +01:00
|
|
|
describe('hd-keyring', function() {
|
2016-10-28 02:23:26 +02:00
|
|
|
|
|
|
|
let keyring
|
|
|
|
beforeEach(function() {
|
|
|
|
keyring = new HdKeyring()
|
|
|
|
})
|
|
|
|
|
2016-11-23 09:23:41 +01:00
|
|
|
describe('constructor', function(done) {
|
2016-11-03 00:35:46 +01:00
|
|
|
keyring = new HdKeyring({
|
|
|
|
mnemonic: sampleMnemonic,
|
2016-11-12 02:06:37 +01:00
|
|
|
numberOfAccounts: 2,
|
2016-11-03 00:35:46 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const accounts = keyring.getAccounts()
|
2016-11-23 09:23:41 +01:00
|
|
|
.then((accounts) => {
|
|
|
|
assert.equal(accounts[0], firstAcct)
|
|
|
|
assert.equal(accounts[1], secondAcct)
|
|
|
|
done()
|
|
|
|
})
|
2016-11-03 00:35:46 +01:00
|
|
|
})
|
|
|
|
|
2016-11-22 04:40:30 +01:00
|
|
|
describe('Keyring.type', function() {
|
|
|
|
it('is a class property that returns the type string.', function() {
|
|
|
|
const type = HdKeyring.type
|
2016-10-28 02:23:26 +02:00
|
|
|
assert.equal(typeof type, 'string')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#type', function() {
|
|
|
|
it('returns the correct value', function() {
|
|
|
|
const type = keyring.type
|
2016-11-22 04:47:45 +01:00
|
|
|
const correct = HdKeyring.type
|
2016-10-28 02:23:26 +02:00
|
|
|
assert.equal(type, correct)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#serialize empty wallets.', function() {
|
|
|
|
it('serializes a new mnemonic', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
keyring.serialize()
|
|
|
|
.then((output) => {
|
|
|
|
assert.equal(output.numberOfAccounts, 0)
|
|
|
|
assert.equal(output.mnemonic, null)
|
|
|
|
})
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#deserialize a private key', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
it('serializes what it deserializes', function(done) {
|
2016-11-23 23:39:35 +01:00
|
|
|
console.log('deserializing ' + sampleMnemonic)
|
2016-10-28 02:23:26 +02:00
|
|
|
keyring.deserialize({
|
|
|
|
mnemonic: sampleMnemonic,
|
2016-11-12 02:06:37 +01:00
|
|
|
numberOfAccounts: 1
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
2016-11-23 09:23:41 +01:00
|
|
|
.then(() => {
|
2016-11-23 23:39:35 +01:00
|
|
|
console.dir(keyring)
|
2016-11-23 09:23:41 +01:00
|
|
|
assert.equal(keyring.wallets.length, 1, 'restores two accounts')
|
2016-11-23 23:39:35 +01:00
|
|
|
return keyring.addAccounts(1)
|
|
|
|
}).then(() => {
|
|
|
|
return keyring.getAccounts()
|
|
|
|
}).then((accounts) => {
|
2016-11-23 09:23:41 +01:00
|
|
|
assert.equal(accounts[0], firstAcct)
|
|
|
|
assert.equal(accounts[1], secondAcct)
|
|
|
|
assert.equal(accounts.length, 2)
|
|
|
|
|
2016-11-23 23:39:35 +01:00
|
|
|
return keyring.serialize()
|
|
|
|
}).then((serialized) => {
|
|
|
|
assert.equal(serialized.mnemonic, sampleMnemonic)
|
|
|
|
done()
|
2016-11-23 09:23:41 +01:00
|
|
|
})
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#addAccounts', function() {
|
|
|
|
describe('with no arguments', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
it('creates a single wallet', function(done) {
|
2016-10-28 02:23:26 +02:00
|
|
|
keyring.addAccounts()
|
2016-11-23 09:23:41 +01:00
|
|
|
.then(() => {
|
|
|
|
assert.equal(keyring.wallets.length, 1)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('with a numeric argument', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
it('creates that number of wallets', function(done) {
|
2016-10-28 02:23:26 +02:00
|
|
|
keyring.addAccounts(3)
|
2016-11-23 09:23:41 +01:00
|
|
|
.then(() => {
|
|
|
|
assert.equal(keyring.wallets.length, 3)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#getAccounts', function() {
|
2016-11-23 09:23:41 +01:00
|
|
|
it('calls getAddress on each wallet', function(done) {
|
2016-10-28 02:23:26 +02:00
|
|
|
|
|
|
|
// Push a mock wallet
|
|
|
|
const desiredOutput = 'foo'
|
|
|
|
keyring.wallets.push({
|
|
|
|
getAddress() {
|
|
|
|
return {
|
|
|
|
toString() {
|
|
|
|
return desiredOutput
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const output = keyring.getAccounts()
|
2016-11-23 09:23:41 +01:00
|
|
|
.then((output) => {
|
|
|
|
assert.equal(output[0], desiredOutput)
|
|
|
|
assert.equal(output.length, 1)
|
|
|
|
done()
|
|
|
|
})
|
2016-10-28 02:23:26 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|