2014-02-27 04:54:54 +01:00
|
|
|
var Wallet = require('../src/wallet.js')
|
2014-03-11 16:42:01 +01:00
|
|
|
var HDNode = require('../src/hdwallet.js')
|
|
|
|
var convert = require('../src/convert.js')
|
2014-02-27 04:54:54 +01:00
|
|
|
var assert = require('assert')
|
2014-03-11 16:42:01 +01:00
|
|
|
var SHA256 = require('crypto-js/sha256')
|
|
|
|
var Crypto = require('crypto-js')
|
2014-02-27 04:54:54 +01:00
|
|
|
|
|
|
|
describe('Wallet', function() {
|
2014-03-11 16:42:01 +01:00
|
|
|
var seed;
|
|
|
|
beforeEach(function(){
|
|
|
|
seed = convert.wordArrayToBytes(SHA256("don't use a string seed like this in real life"))
|
|
|
|
})
|
2014-02-27 04:54:54 +01:00
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('constructor', function() {
|
2014-02-27 04:54:54 +01:00
|
|
|
var wallet;
|
|
|
|
beforeEach(function() {
|
|
|
|
wallet = new Wallet(seed)
|
|
|
|
})
|
|
|
|
|
2014-02-28 07:27:31 +01:00
|
|
|
it('defaults to Bitcoin mainnet', function() {
|
2014-02-28 09:57:44 +01:00
|
|
|
assert.equal(wallet.getMasterKey().network, 'mainnet')
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0' as the main account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var mainAccount = wallet.getAccountZero()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(mainAccount.index, 0 + HDNode.HIGHEST_BIT)
|
|
|
|
assert.equal(mainAccount.depth, 1)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0'/0 as the external account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var account = wallet.getExternalAccount()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(account.index, 0)
|
|
|
|
assert.equal(account.depth, 2)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
it("generates m/0'/1 as the internal account", function() {
|
2014-03-14 03:31:26 +01:00
|
|
|
var account = wallet.getInternalAccount()
|
2014-03-11 16:42:01 +01:00
|
|
|
assert.equal(account.index, 1)
|
|
|
|
assert.equal(account.depth, 2)
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
|
2014-03-15 03:18:59 +01:00
|
|
|
describe('when seed is not specified', function(){
|
|
|
|
it('generates a seed', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
assert.ok(wallet.getMasterKey())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('constructor options', function() {
|
|
|
|
var wallet;
|
|
|
|
beforeEach(function() {
|
|
|
|
wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('uses the network if specified', function() {
|
|
|
|
assert.equal(wallet.getMasterKey().network, 'testnet')
|
|
|
|
})
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|
|
|
|
})
|
2014-02-28 05:05:48 +01:00
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
describe('newMasterKey', function(){
|
|
|
|
it('resets accounts', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
var oldAccountZero = wallet.getAccountZero()
|
|
|
|
var oldExternalAccount = wallet.getExternalAccount()
|
|
|
|
var oldInternalAccount = wallet.getInternalAccount()
|
|
|
|
|
|
|
|
wallet.newMasterKey(seed)
|
|
|
|
assertNotEqual(wallet.getAccountZero(), oldAccountZero)
|
|
|
|
assertNotEqual(wallet.getExternalAccount(), oldExternalAccount)
|
|
|
|
assertNotEqual(wallet.getInternalAccount(), oldInternalAccount)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('resets addresses', function(){
|
|
|
|
var wallet = new Wallet()
|
|
|
|
wallet.generateAddress()
|
|
|
|
wallet.generateChangeAddress()
|
|
|
|
var oldAddresses = wallet.addresses
|
|
|
|
var oldChangeAddresses = wallet.changeAddresses
|
|
|
|
assert.notDeepEqual(oldAddresses, [])
|
|
|
|
assert.notDeepEqual(oldChangeAddresses, [])
|
|
|
|
|
|
|
|
wallet.newMasterKey(seed)
|
|
|
|
assert.deepEqual(wallet.addresses, [])
|
|
|
|
assert.deepEqual(wallet.changeAddresses, [])
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-11 16:42:01 +01:00
|
|
|
describe('generateAddress', function(){
|
2014-03-13 10:43:35 +01:00
|
|
|
it('generate receiving addresses', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
var expectedAddresses = [
|
|
|
|
"n1GyUANZand9Kw6hGSV9837cCC9FFUQzQa",
|
|
|
|
"n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"
|
|
|
|
]
|
|
|
|
|
|
|
|
assert.equal(wallet.generateAddress(), expectedAddresses[0])
|
|
|
|
assert.equal(wallet.generateAddress(), expectedAddresses[1])
|
|
|
|
assert.deepEqual(wallet.addresses, expectedAddresses)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('generateChangeAddress', function(){
|
|
|
|
it('generates change addresses', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
var expectedAddresses = ["mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"]
|
2014-03-11 16:42:01 +01:00
|
|
|
|
2014-03-13 10:43:35 +01:00
|
|
|
assert.equal(wallet.generateChangeAddress(), expectedAddresses[0])
|
|
|
|
assert.deepEqual(wallet.changeAddresses, expectedAddresses)
|
2014-03-11 16:42:01 +01:00
|
|
|
})
|
|
|
|
})
|
2014-03-13 12:05:55 +01:00
|
|
|
|
|
|
|
describe('getPrivateKey', function(){
|
|
|
|
it('returns the private key at the given index of external account', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
|
|
|
|
assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getInternalPrivateKey', function(){
|
|
|
|
it('returns the private key at the given index of internal account', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
|
|
|
|
assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('getPrivateKeyForAddress', function(){
|
|
|
|
it('returns the private key for the given address', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
wallet.generateChangeAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
wallet.generateAddress()
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
2014-03-14 03:31:26 +01:00
|
|
|
wallet.getExternalAccount().derive(1).priv)
|
2014-03-16 06:31:46 +01:00
|
|
|
assertEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
2014-03-14 03:31:26 +01:00
|
|
|
wallet.getInternalAccount().derive(0).priv)
|
2014-03-13 12:05:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('raises an error when address is not found', function(){
|
|
|
|
var wallet = new Wallet(seed, {network: 'testnet'})
|
|
|
|
assert.throws(function() {
|
|
|
|
wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X")
|
|
|
|
}, Error, 'Unknown address. Make sure the address is from the keychain and has been generated.')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2014-03-16 06:31:46 +01:00
|
|
|
function assertEqual(obj1, obj2){
|
|
|
|
assert.equal(obj1.toString(), obj2.toString())
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertNotEqual(obj1, obj2){
|
|
|
|
assert.notEqual(obj1.toString(), obj2.toString())
|
2014-03-13 12:05:55 +01:00
|
|
|
}
|
2014-02-27 04:54:54 +01:00
|
|
|
})
|