new master key resets accounts and addresses
This commit is contained in:
parent
a7a7999e3f
commit
31cfb11178
2 changed files with 51 additions and 13 deletions
|
@ -37,8 +37,6 @@ var Wallet = function (seed, options) {
|
|||
rng.nextBytes(seed);
|
||||
}
|
||||
masterkey = new HDNode(seed, network);
|
||||
}
|
||||
this.newMasterKey(seed, network)
|
||||
|
||||
// HD first-level child derivation method should be private
|
||||
// See https://bitcointalk.org/index.php?topic=405179.msg4415254#msg4415254
|
||||
|
@ -46,6 +44,14 @@ var Wallet = function (seed, options) {
|
|||
externalAccount = accountZero.derive(0)
|
||||
internalAccount = accountZero.derive(1)
|
||||
|
||||
me.addresses = [];
|
||||
me.changeAddresses = [];
|
||||
|
||||
me.outputs = {};
|
||||
}
|
||||
this.newMasterKey(seed, network)
|
||||
|
||||
|
||||
this.generateAddress = function() {
|
||||
var key = externalAccount.derive(this.addresses.length)
|
||||
this.addresses.push(key.getBitcoinAddress().toString())
|
||||
|
|
|
@ -58,6 +58,34 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
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, [])
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateAddress', function(){
|
||||
it('generate receiving addresses', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
|
@ -86,8 +114,8 @@ describe('Wallet', function() {
|
|||
it('returns the private key at the given index of external account', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv)
|
||||
assertEqual(wallet.getPrivateKey(0), wallet.getExternalAccount().derive(0).priv)
|
||||
assertEqual(wallet.getPrivateKey(1), wallet.getExternalAccount().derive(1).priv)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -95,8 +123,8 @@ describe('Wallet', function() {
|
|||
it('returns the private key at the given index of internal account', function(){
|
||||
var wallet = new Wallet(seed, {network: 'testnet'})
|
||||
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
|
||||
assertPrivateKeyEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
|
||||
assertEqual(wallet.getInternalPrivateKey(0), wallet.getInternalAccount().derive(0).priv)
|
||||
assertEqual(wallet.getInternalPrivateKey(1), wallet.getInternalAccount().derive(1).priv)
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -107,9 +135,9 @@ describe('Wallet', function() {
|
|||
wallet.generateAddress()
|
||||
wallet.generateAddress()
|
||||
|
||||
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||
assertEqual(wallet.getPrivateKeyForAddress("n2fiWrHqD6GM5GiEqkbWAc6aaZQp3ba93X"),
|
||||
wallet.getExternalAccount().derive(1).priv)
|
||||
assertPrivateKeyEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||
assertEqual(wallet.getPrivateKeyForAddress("mnXiDR4MKsFxcKJEZjx4353oXvo55iuptn"),
|
||||
wallet.getInternalAccount().derive(0).priv)
|
||||
})
|
||||
|
||||
|
@ -121,7 +149,11 @@ describe('Wallet', function() {
|
|||
})
|
||||
})
|
||||
|
||||
function assertPrivateKeyEqual(key1, key2){
|
||||
assert.equal(key1.toString(), key2.toString())
|
||||
function assertEqual(obj1, obj2){
|
||||
assert.equal(obj1.toString(), obj2.toString())
|
||||
}
|
||||
|
||||
function assertNotEqual(obj1, obj2){
|
||||
assert.notEqual(obj1.toString(), obj2.toString())
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue