add getAddresses to return all wallet\'s addresses
This commit is contained in:
parent
6a2e0d82d0
commit
cf1732fc44
2 changed files with 57 additions and 11 deletions
|
|
@ -1,15 +1,16 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
var imports = require('soop').imports();
|
var imports = require('soop').imports();
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var BIP32 = bitcore.BIP32;
|
var BIP32 = bitcore.BIP32;
|
||||||
var Address = bitcore.Address;
|
var Address = bitcore.Address;
|
||||||
var Script = bitcore.Script;
|
var Script = bitcore.Script;
|
||||||
var coinUtil= bitcore.util;
|
var coinUtil = bitcore.util;
|
||||||
|
var Transaction = bitcore.Transaction;
|
||||||
|
|
||||||
var Storage = imports.Storage || require('./Storage');
|
var Storage = imports.Storage || require('./Storage');
|
||||||
var log = imports.log || console.log;
|
var log = imports.log || console.log;
|
||||||
|
|
||||||
var storage = Storage.default();
|
var storage = Storage.default();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This follow Electrum convetion, as described on
|
* This follow Electrum convetion, as described on
|
||||||
|
|
@ -220,6 +221,12 @@ Wallet.prototype.getCosignersSortedPubKeys = function(index, isChange) {
|
||||||
|
|
||||||
Wallet.prototype.getAddress = 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 pubKeys = this.getCosignersSortedPubKeys(index, isChange);
|
||||||
|
|
||||||
var version = this.network.addressScript;
|
var version = this.network.addressScript;
|
||||||
|
|
@ -244,7 +251,26 @@ Wallet.prototype.createAddress = function(isChange) {
|
||||||
return ret;
|
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
|
// Input: Bitcore's Transaction, sign with ownPK
|
||||||
// return partially signed or fully signed tx
|
// return partially signed or fully signed tx
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ describe('Wallet model', function() {
|
||||||
|
|
||||||
for(var isChange=0; isChange<2; isChange++) {
|
for(var isChange=0; isChange<2; isChange++) {
|
||||||
for(var i=0; i<5; i++) {
|
for(var i=0; i<5; i++) {
|
||||||
var addr = w.getAddress(i,isChange);
|
var addr = w.createAddress(isChange);
|
||||||
var a = new Address(addr);
|
var a = new Address(addr);
|
||||||
a.isValid().should.equal(true);
|
a.isValid().should.equal(true);
|
||||||
a.isScript().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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue