This commit is contained in:
Matias Alejo Garcia 2015-02-03 16:46:28 -03:00
parent 41c561b6cf
commit fc16cfa8af
5 changed files with 116 additions and 62 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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');
}); });
}); });
}); });

View File

@ -95,17 +95,41 @@ helpers.createAndJoinWallet = function(id, m, n, cb) {
}); });
}); });
}; };
helpers.createUtxos = function(wallet, amounts) {
amounts = [].concat(amounts);
return _.map(amounts, function(amount) { helpers.randomTXID = function() {
return { var ret = '';
txid: SignUtils.hash(Math.random().toString()).toString('hex'), for (var i = 0; i < 64 / 4; i++)
vout: Math.floor((Math.random() * 10) + 1), ret += Math.floor(Math.random() * 255 * 255).toString(16);
amount: amount,
scriptPubKey: new Bitcore.Script.buildMultisigOut(wallet.publicKeyRing, wallet.n), 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);
var i = 0;
return cb(_.map(amounts, function(amount) {
return {
txid: helpers.randomTXID(),
vout: Math.floor((Math.random() * 10) + 1),
amount: amount,
scriptPubKey: addresses[i].getScriptPubKey(wallet.m),
};
}));
});
}; };
var db, storage; var db, storage;
@ -258,20 +282,31 @@ 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',
pubKey: aPubKey, pubKey: aPubKey,
}; };
async.each(invalidPairs, function (pair, cb) { async.each(invalidPairs, function(pair, cb) {
opts.m = pair.m; opts.m = pair.m;
opts.n = pair.n; opts.n = pair.n;
server.createWallet(opts, function(err) { server.createWallet(opts, function(err) {
@ -279,10 +314,10 @@ describe('Copay server', function() {
err.should.contain('Invalid m/n combination'); err.should.contain('Invalid m/n combination');
return cb(); return cb();
}); });
}, function (err) { }, function(err) {
done(); done();
}); });
}); });
}); });
describe('#joinWallet', function() { describe('#joinWallet', function() {
@ -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,40 +658,44 @@ describe('Copay server', function() {
}); });
it.skip('should create tx', function(done) { it.skip('should create tx', function(done) {
var bc = sinon.stub();
bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, helpers.createUtxos(wallet, [100, 200]));
server._getBlockExplorer = sinon.stub().returns(bc);
//server._createRawTx = sinon.stub().returns('raw'); helpers.createUtxos(server, wallet, [100, 200], function(utxos) {
var txOpts = { console.log('[integration.js.670:utxos:]', utxos); //TODO
copayerId: '1', var bc = sinon.stub();
walletId: '123', bc.getUnspentUtxos = sinon.stub().callsArgWith(1, null, utxos);
toAddress: 'dummy', server._getBlockExplorer = sinon.stub().returns(bc);
amount: 80, //server._createRawTx = sinon.stub().returns('raw');
message: 'some message',
otToken: 'dummy', var txOpts = {
requestSignature: 'dummy', copayerId: '1',
}; walletId: '123',
server.createTx(txOpts, function(err, tx) { toAddress: 'dummy',
should.not.exist(err); amount: 80,
tx.should.exist; message: 'some message',
console.log(tx); otToken: 'dummy',
//tx.rawTx.should.equal('raw'); requestSignature: 'dummy',
tx.isAccepted().should.equal.false; };
tx.isRejected().should.equal.false; server.createTx(txOpts, function(err, tx) {
server.getPendingTxs({
walletId: '123'
}, function(err, txs) {
should.not.exist(err); should.not.exist(err);
txs.length.should.equal(1); tx.should.exist;
server.getBalance({ console.log(tx);
//tx.rawTx.should.equal('raw');
tx.isAccepted().should.equal.false;
tx.isRejected().should.equal.false;
server.getPendingTxs({
walletId: '123' walletId: '123'
}, function(err, balance) { }, function(err, txs) {
should.not.exist(err); should.not.exist(err);
balance.totalAmount.should.equal(300); txs.length.should.equal(1);
balance.lockedAmount.should.equal(100); server.getBalance({
done(); walletId: '123'
}, function(err, balance) {
should.not.exist(err);
balance.totalAmount.should.equal(300);
balance.lockedAmount.should.equal(100);
done();
});
}); });
}); });
}); });

View File

@ -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);
}); });
}); });