add getAddresses to return all wallet\'s addresses

This commit is contained in:
Matias Alejo Garcia 2014-03-27 01:18:29 -03:00
parent 6a2e0d82d0
commit cf1732fc44
2 changed files with 57 additions and 11 deletions

View File

@ -1,15 +1,16 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil= bitcore.util;
var imports = require('soop').imports();
var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var Address = bitcore.Address;
var Script = bitcore.Script;
var coinUtil = bitcore.util;
var Transaction = bitcore.Transaction;
var Storage = imports.Storage || require('./Storage');
var log = imports.log || console.log;
var Storage = imports.Storage || require('./Storage');
var log = imports.log || console.log;
var storage = Storage.default();
var storage = Storage.default();
/*
* This follow Electrum convetion, as described on
@ -220,6 +221,12 @@ Wallet.prototype.getCosignersSortedPubKeys = function(index, isChange) {
Wallet.prototype.getAddress = function (index, isChange) {
if ( (isChange && index > this.changeAddressIndex)
|| (!isChange && index > this.addressIndex)) {
log('Out of bounds at getAddress: Index %d isChange: %d', index, isChange);
throw new Error('index out of bound');
}
var pubKeys = this.getCosignersSortedPubKeys(index, isChange);
var version = this.network.addressScript;
@ -244,7 +251,26 @@ Wallet.prototype.createAddress = function(isChange) {
return ret;
};
Wallet.prototype.getAddresses = function() {
var ret = [];
for (var i=0; i<this.changeAddressIndex; i++) {
ret.push(this.getAddress(i,true));
}
for (var i=0; i<this.addressIndex; i++) {
ret.push(this.getAddress(i,false));
}
return ret;
};
Wallet.prototype.createTx = function(utxos,outs) {
var opts = {
remainderAddress: this.createAddress(1),
};
return Transaction.create(utxos, outs, opts);
};
// Input: Bitcore's Transaction, sign with ownPK
// return partially signed or fully signed tx

View File

@ -113,7 +113,7 @@ describe('Wallet model', function() {
for(var isChange=0; isChange<2; isChange++) {
for(var i=0; i<5; i++) {
var addr = w.getAddress(i,isChange);
var addr = w.createAddress(isChange);
var a = new Address(addr);
a.isValid().should.equal(true);
a.isScript().should.equal(true);
@ -122,6 +122,26 @@ describe('Wallet model', function() {
}
});
it('should return wallet addresses', function () {
var k = createW();
var w = k.w;
var a = w.getAddresses();
a.length.should.equal(0);
for(var isChange=0; isChange<2; isChange++)
for(var i=0; i<6; i++)
w.createAddress(isChange);
var as = w.getAddresses();
as.length.should.equal(12);
for(var i in as) {
var a = new Address(as[i]);
a.isValid().should.equal(true);
}
});
});