Browse Source

Merge pull request #8 from matiu/feat/createTx2

fix index 0 and adds public keys to addresses
activeAddress
Ivan Socolsky 10 years ago
parent
commit
d8f5f74f53
  1. 15
      lib/model/address.js
  2. 3
      lib/model/addressmanager.js
  3. 4
      test/addressmanager.js
  4. 80
      test/integration.js
  5. 4
      test/wallet.js

15
lib/model/address.js

@ -1,11 +1,14 @@
'use strict'; 'use strict';
var Bitcore = require('bitcore');
function Address(opts) { function Address(opts) {
opts = opts || {}; opts = opts || {};
this.createdOn = Math.floor(Date.now() / 1000); this.createdOn = Math.floor(Date.now() / 1000);
this.address = opts.address; this.address = opts.address;
this.path = opts.path; this.path = opts.path;
this.publicKeys = opts.publicKeys;
}; };
Address.fromObj = function (obj) { Address.fromObj = function (obj) {
@ -14,7 +17,19 @@ Address.fromObj = function (obj) {
x.createdOn = obj.createdOn; x.createdOn = obj.createdOn;
x.address = obj.address; x.address = obj.address;
x.path = obj.path; x.path = obj.path;
x.publicKeys = obj.publicKeys;
return x; return x;
}; };
/**
* getScriptPubKey
*
* @param {number} threshold - amount of required signatures to spend the output
* @return {Script}
*/
Address.prototype.getScriptPubKey = function (threshold) {
return Bitcore.Script.buildMultisigOut(this.publicKeys, threshold)
};
module.exports = Address; module.exports = Address;

3
lib/model/addressmanager.js

@ -31,8 +31,9 @@ AddressManager.prototype.getCurrentAddressPath = function (isChange) {
}; };
AddressManager.prototype.getNewAddressPath = function (isChange) { AddressManager.prototype.getNewAddressPath = function (isChange) {
var ret = this.getCurrentAddressPath(isChange);
this._incrementIndex(isChange); this._incrementIndex(isChange);
return this.getCurrentAddressPath(isChange); return ret;
}; };
module.exports = AddressManager; module.exports = AddressManager;

4
test/addressmanager.js

@ -29,8 +29,8 @@ describe('AddressManager', function() {
var am = new AddressManager({ var am = new AddressManager({
copayerIndex: 2 copayerIndex: 2
}); });
am.getNewAddressPath(false).should.equal('m/2/0/1'); am.getNewAddressPath(false).should.equal('m/2/0/0');
am.getNewAddressPath(true).should.equal('m/2/1/1'); am.getNewAddressPath(true).should.equal('m/2/1/0');
}); });
}); });
}); });

80
test/integration.js

@ -95,16 +95,40 @@ helpers.createAndJoinWallet = function(id, m, n, cb) {
}); });
}); });
}; };
helpers.createUtxos = function(wallet, amounts) {
helpers.randomTXID = function() {
var ret = '';
for (var i = 0; i < 64 / 4; i++)
ret += Math.floor(Math.random() * 255 * 255).toString(16);
return ret;
};
helpers.createUtxos = function(server, wallet, amounts, cb) {
var addresses = [];
async.each(amounts, function(a, next) {
server.createAddress({
walletId: wallet.id,
isChange: false,
}, function(err, address) {
addresses.push(address);
console.log('[integration.js.115:address:]', address); //TODO
next(err);
});
},
function(err) {
amounts = [].concat(amounts); amounts = [].concat(amounts);
return _.map(amounts, function(amount) { var i = 0;
return cb(_.map(amounts, function(amount) {
return { return {
txid: SignUtils.hash(Math.random().toString()).toString('hex'), txid: helpers.randomTXID(),
vout: Math.floor((Math.random() * 10) + 1), vout: Math.floor((Math.random() * 10) + 1),
amount: amount, amount: amount,
scriptPubKey: new Bitcore.Script.buildMultisigOut(wallet.publicKeyRing, wallet.n), scriptPubKey: addresses[i].getScriptPubKey(wallet.m),
}; };
}));
}); });
}; };
@ -258,14 +282,25 @@ describe('Copay server', function() {
}); });
it('should fail to create wallet with invalid copayer pairs', function(done) { it('should fail to create wallet with invalid copayer pairs', function(done) {
var invalidPairs = [ var invalidPairs = [{
{ m: 0, n: 0 }, m: 0,
{ m: 0, n: 2 }, n: 0
{ m: 2, n: 1 }, }, {
{ m: 0, n: 10 }, m: 0,
{ m: 1, n: 20 }, n: 2
{ m: 10, n: 10 }, }, {
]; m: 2,
n: 1
}, {
m: 0,
n: 10
}, {
m: 1,
n: 20
}, {
m: 10,
n: 10
}, ];
var opts = { var opts = {
id: '123', id: '123',
name: 'my wallet', name: 'my wallet',
@ -563,8 +598,8 @@ describe('Copay server', function() {
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
address.should.exist; address.should.exist;
address.address.should.equal('3H4pNP6J4PW4NnvdrTg37VvZ7h2QWuAwtA'); address.address.should.equal('36JdLEUDa6UwCfMhhkdZ2VFnDrGUoLedsR');
address.path.should.equal('m/2147483647/0/1'); address.path.should.equal('m/2147483647/0/0');
done(); done();
}); });
}); });
@ -579,8 +614,8 @@ describe('Copay server', function() {
}, function(err, address) { }, function(err, address) {
should.not.exist(err); should.not.exist(err);
address.should.exist; address.should.exist;
address.address.should.equal('3GesnvqTsw3PQbyZwf4D96ZZiFrhVkYsJn'); address.address.should.equal('3CauZ5JUFfmSAx2yANvCRoNXccZ3YSUjXH');
address.path.should.equal('m/2147483647/1/1'); address.path.should.equal('m/2147483647/1/0');
done(); done();
}); });
}); });
@ -595,15 +630,14 @@ describe('Copay server', function() {
}, cb); }, cb);
}, function (err, addresses) { }, function (err, addresses) {
addresses.length.should.equal(10); addresses.length.should.equal(10);
addresses[0].path.should.equal('m/2147483647/0/1'); addresses[0].path.should.equal('m/2147483647/0/0');
addresses[9].path.should.equal('m/2147483647/0/10'); addresses[9].path.should.equal('m/2147483647/0/9');
// No two identical addresses // No two identical addresses
_.keys(_.groupBy(addresses, 'address')).length.should.equal(10); _.keys(_.groupBy(addresses, 'address')).length.should.equal(10);
done(); done();
}); });
}); });
}); });
}); });
describe('#createTx', function() { describe('#createTx', function() {
@ -624,10 +658,13 @@ describe('Copay server', function() {
}); });
it.skip('should create tx', function(done) { it.skip('should create tx', function(done) {
helpers.createUtxos(server, wallet, [100, 200], function(utxos) {
console.log('[integration.js.670:utxos:]', utxos); //TODO
var bc = sinon.stub(); var bc = sinon.stub();
bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, helpers.createUtxos(wallet, [100, 200])); bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos);
server._getBlockExplorer = sinon.stub().returns(bc); server._getBlockExplorer = sinon.stub().returns(bc);
//server._createRawTx = sinon.stub().returns('raw'); //server._createRawTx = sinon.stub().returns('raw');
var txOpts = { var txOpts = {
@ -662,6 +699,7 @@ describe('Copay server', function() {
}); });
}); });
}); });
});
it.skip('should fail to create tx when insufficient funds', function(done) {}); it.skip('should fail to create tx when insufficient funds', function(done) {});

4
test/wallet.js

@ -19,8 +19,8 @@ describe('Wallet', function() {
it('create an address', function() { it('create an address', function() {
var w = Wallet.fromObj(testWallet); var w = Wallet.fromObj(testWallet);
var a = w.createAddress(false); var a = w.createAddress(false);
a.address.should.equal('35Du8JgkFoiN5znoETnkGZAv99v6eCwGMB'); a.address.should.equal('3HPJYvQZuTVY6pPBz17fFVz2YPoMBVT34i');
a.path.should.equal('m/2147483647/0/1'); a.path.should.equal('m/2147483647/0/0');
a.createdOn.should.be.above(1); a.createdOn.should.be.above(1);
}); });
}); });

Loading…
Cancel
Save