fix tests

This commit is contained in:
Manuel Araoz 2014-05-29 17:18:55 -03:00
parent 4cd90c45a2
commit f13dea2376
6 changed files with 22 additions and 55 deletions

View File

@ -4,6 +4,7 @@ module.exports.PublicKeyRing = require('./js/models/core/PublicKeyRing');
module.exports.TxProposals = require('./js/models/core/TxProposals'); module.exports.TxProposals = require('./js/models/core/TxProposals');
module.exports.PrivateKey = require('./js/models/core/PrivateKey'); module.exports.PrivateKey = require('./js/models/core/PrivateKey');
module.exports.Passphrase = require('./js/models/core/Passphrase'); module.exports.Passphrase = require('./js/models/core/Passphrase');
module.exports.Structure = require('./js/models/core/Structure');
// components // components

View File

@ -3,11 +3,11 @@
var imports = require('soop').imports(); var imports = require('soop').imports();
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey; var HK = bitcore.HierarchicalKey;
var WalletKey = bitcore.WalletKey; var WalletKey = bitcore.WalletKey;
var networks = bitcore.networks; var networks = bitcore.networks;
var util = bitcore.util; var util = bitcore.util;
var PublicKeyRing = require('./PublicKeyRing'); var Structure = require('./Structure');
function PrivateKey(opts) { function PrivateKey(opts) {
opts = opts || {}; opts = opts || {};
@ -20,7 +20,7 @@ function PrivateKey(opts) {
PrivateKey.prototype.getId = function() { PrivateKey.prototype.getId = function() {
if (!this.id) { if (!this.id) {
var path = PublicKeyRing.IdFullBranch(); var path = Structure.IdFullBranch;
var idhk = this.bip.derive(path); var idhk = this.bip.derive(path);
this.id= idhk.eckey.public.toString('hex'); this.id= idhk.eckey.public.toString('hex');
} }
@ -29,7 +29,7 @@ PrivateKey.prototype.getId = function() {
PrivateKey.prototype.deriveBIP45Branch = function() { PrivateKey.prototype.deriveBIP45Branch = function() {
if (!this.bip45Branch) { if (!this.bip45Branch) {
this.bip45Branch = this.bip.derive(PublicKeyRing.BIP45_PUBLIC_PREFIX); this.bip45Branch = this.bip.derive(Structure.BIP45_PUBLIC_PREFIX);
} }
return this.bip45Branch; return this.bip45Branch;
} }
@ -62,8 +62,7 @@ PrivateKey.prototype._getHK = function(path) {
}; };
PrivateKey.prototype.get = function(index,isChange) { PrivateKey.prototype.get = function(index,isChange) {
console.log(PublicKeyRing); var path = Structure.FullBranch(index, isChange);
var path = PublicKeyRing.FullBranch(index, isChange);
var pk = this.privateKeyCache[path]; var pk = this.privateKeyCache[path];
if (!pk) { if (!pk) {
var derivedHK = this._getHK(path); var derivedHK = this._getHK(path);

View File

@ -6,6 +6,7 @@ var imports = require('soop').imports();
var bitcore = require('bitcore'); var bitcore = require('bitcore');
var HK = bitcore.HierarchicalKey; var HK = bitcore.HierarchicalKey;
var PrivateKey = require('./PrivateKey'); var PrivateKey = require('./PrivateKey');
var Structure = require('./Structure');
var Address = bitcore.Address; var Address = bitcore.Address;
var Script = bitcore.Script; var Script = bitcore.Script;
var coinUtil = bitcore.util; var coinUtil = bitcore.util;
@ -37,40 +38,6 @@ function PublicKeyRing(opts) {
this.copayerIds = []; this.copayerIds = [];
} }
/*
* Based on https://github.com/maraoz/bips/blob/master/bip-NNNN.mediawiki
* m / purpose' / cosigner_index / change / address_index
*/
var PURPOSE = 45;
var MAX_NON_HARDENED = 0x8000000 - 1;
var SHARED_INDEX = MAX_NON_HARDENED - 0;
var ID_INDEX = MAX_NON_HARDENED - 1;
var BIP45_PUBLIC_PREFIX = 'm/'+ PURPOSE+'\'';
PublicKeyRing.BIP45_PUBLIC_PREFIX = BIP45_PUBLIC_PREFIX;
PublicKeyRing.Branch = function(address_index, isChange, cosigner_index) {
var ret = 'm/'+
(typeof cosigner_index !== 'undefined'? cosigner_index: SHARED_INDEX)+'/'+
(isChange?1:0)+'/'+
address_index;
return ret;
};
PublicKeyRing.FullBranch = function(address_index, isChange, cosigner_index) {
var sub = PublicKeyRing.Branch(address_index, isChange, cosigner_index);
sub = sub.substring(2);
return BIP45_PUBLIC_PREFIX + '/' + sub;
};
PublicKeyRing.IdFullBranch = function() {
return PublicKeyRing.FullBranch(0, 0, ID_INDEX);
};
PublicKeyRing.fromObj = function (data) { PublicKeyRing.fromObj = function (data) {
if (data instanceof PublicKeyRing) { if (data instanceof PublicKeyRing) {
throw new Error('bad data format: Did you use .toObj()?'); throw new Error('bad data format: Did you use .toObj()?');
@ -134,7 +101,7 @@ PublicKeyRing.prototype._newExtendedPublicKey = function () {
}; };
PublicKeyRing.prototype._updateBip = function (index) { PublicKeyRing.prototype._updateBip = function (index) {
var hk = this.copayersHK[index].derive(PublicKeyRing.Branch(0, 0, ID_INDEX)); var hk = this.copayersHK[index].derive(Structure.IdBranch);
this.copayerIds[index]= hk.eckey.public.toString('hex'); this.copayerIds[index]= hk.eckey.public.toString('hex');
}; };
@ -176,7 +143,7 @@ PublicKeyRing.prototype.addCopayer = function(newEpk, nickname) {
PublicKeyRing.prototype.getPubKeys = function(index, isChange) { PublicKeyRing.prototype.getPubKeys = function(index, isChange) {
this._checkKeys(); this._checkKeys();
var path = PublicKeyRing.Branch(index, isChange); var path = Structure.Branch(index, isChange);
var pubKeys = this.publicKeysCache[path]; var pubKeys = this.publicKeysCache[path];
if (!pubKeys) { if (!pubKeys) {
pubKeys = []; pubKeys = [];

View File

@ -628,7 +628,7 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
var signRet; var signRet;
if (priv) { if (priv) {
b.sign(priv.getAll(pkr.addressIndex, pkr.changeAddressIndex)); var signed = b.sign(priv.getAll(pkr.addressIndex, pkr.changeAddressIndex));
} }
var myId = this.getMyCopayerId(); var myId = this.getMyCopayerId();
var now = Date.now(); var now = Date.now();

View File

@ -28,10 +28,10 @@ describe('PrivateKey model', function() {
}); });
it('should derive priv keys', function () { it('should derive priv keys', function () {
var w = new PrivateKey(config); var pk = new PrivateKey(config);
for(var j=0; j<2; j++) { for(var j=0; j<2; j++) {
for(var i=0; i<3; i++) { for(var i=0; i<3; i++) {
var wk = w.get(i,j); var wk = pk.get(i,j);
should.exist(wk); should.exist(wk);
var o=wk.storeObj(); var o=wk.storeObj();
should.exist(o); should.exist(o);

View File

@ -42,7 +42,8 @@ describe('Wallet model', function() {
requiredCopayers: c.requiredCopayers, requiredCopayers: c.requiredCopayers,
totalCopayers: c.totalCopayers, totalCopayers: c.totalCopayers,
}); });
c.publicKeyRing.addCopayer(c.privateKey.getExtendedPublicKeyString()); var copayerEPK = c.privateKey.deriveBIP45Branch().extendedPublicKeyString()
c.publicKeyRing.addCopayer(copayerEPK);
c.txProposals = new copay.TxProposals({ c.txProposals = new copay.TxProposals({
networkName: c.networkName, networkName: c.networkName,
@ -102,10 +103,10 @@ describe('Wallet model', function() {
for(var i=0; i<4; i++) { for(var i=0; i<4; i++) {
if (privateKeys) { if (privateKeys) {
var k=privateKeys[i]; var k=privateKeys[i];
pkr.addCopayer(k?k.getExtendedPublicKeyString():null); pkr.addCopayer(k?k.deriveBIP45Branch().extendedPublicKeyString():null);
} } else {
else
pkr.addCopayer(); pkr.addCopayer();
}
} }
pkr.generateAddress(true); pkr.generateAddress(true);
pkr.generateAddress(true); pkr.generateAddress(true);
@ -125,20 +126,19 @@ describe('Wallet model', function() {
unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString(); unspentTest[0].address = w.publicKeyRing.getAddress(1, true).toString();
unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true); unspentTest[0].scriptPubKey = w.publicKeyRing.getScriptPubKeyHex(1, true);
w.createTxSync( var ntxid = w.createTxSync(
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt', '15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789', '123456789',
unspentTest unspentTest
); );
var t = w.txProposals; var t = w.txProposals;
console.log(t); var txp = t.txps[ntxid];
var k = Object.keys(t.txps)[0]; var tx = txp.builder.build();
var tx = t.txps[k].builder.build();
should.exist(tx); should.exist(tx);
tx.isComplete().should.equal(false); tx.isComplete().should.equal(false);
Object.keys(t.txps[k].signedBy).length.should.equal(1); Object.keys(txp.seenBy).length.should.equal(1);
Object.keys(t.txps[k].seenBy).length.should.equal(1); Object.keys(txp.signedBy).length.should.equal(1);
}); });
it('#addressIsOwn', function () { it('#addressIsOwn', function () {