wallet model and test WIP

This commit is contained in:
Matias Alejo Garcia 2014-03-26 15:26:31 -03:00
commit bd8271d0ec
4 changed files with 186 additions and 1 deletions

95
js/models/wallet.js Normal file
View file

@ -0,0 +1,95 @@
'use strict';
var imports = require('soop').imports();
var bitcore = require('bitcore');
var BIP32 = bitcore.BIP32;
var Storage = imports.Storage || require('./Storage');
/*
* This follow Electrum convetion, as described on
* https://bitcointalk.org/index.php?topic=274182.0
*/
var PUBLIC_BRANCH = 'm/0/';
var CHANGE_BRANCH = 'm/1/';
function Wallet(opts) {
opts = opts || {};
this.network = opts.network === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
this.neededCosigners = opts.neededCosigners || 3;
this.totalCosigners = opts.totalCosigners || 5;
this.dirty = 1;
this.cosignersBIP = [];
this.bip32 = new BIP32(opts.bytes || this.network.name);
}
Wallet.read = function (BIP38password) {
};
Wallet.prototype.registeredCosigners = function () {
// 1 is self.
return 1 + this.cosignersBIP.length;
};
Wallet.prototype.getExtendedPrivKey = function (BIP38password) {
if (!this.bip32)
throw new Error('no priv key defined on the wallet');
return this.bip32.extended_private_key_string();
};
Wallet.prototype.getExtendedPubKey = function () {
return this.bip32.extended_public_key_string();
};
// should receive an array also?
Wallet.prototype.addCosignerExtendedPubKey = function (newEpk) {
if (this.haveAllNeededPubKeys())
throw new Error('already have all needed key:' + this.totalCosigners);
if (this.getExtendedPubKey() === newEpk)
throw new Error('already have that key (self kehy)');
this.cosignersBIP.forEach(function(b){
if (b.getExtendedPubKey() === newEpk)
throw new Error('already have that key');
});
this.cosignersBIP.push(new Wallet({bytes:newEpk, network: this.network.name } ));
};
Wallet.prototype.haveAllNeededPubKeys = function () {
return this.registeredCosigners() === this.totalCosigners;
};
Wallet.prototype.getChangeAddress = function (index) {
//index can be 0, 1, 2, etc.
if (! this.haveAllNeededPubKeys() )
throw new Error('cosigners pub key missing');
};
Wallet.prototype.store = function () {
};
// Input: Bitcore's Transaction, sign with ownPK
// return partially signed or fully signed tx
Wallet.prototype.signTx = function (tx, BIP38password) {
};
module.exports = require('soop')(Wallet);

View file

@ -18,11 +18,16 @@
"url": "https://github.com/bitpay/cosign/issues" "url": "https://github.com/bitpay/cosign/issues"
}, },
"homepage": "https://github.com/bitpay/cosign", "homepage": "https://github.com/bitpay/cosign",
"dependencies": {
"bitcore": "0.1.8",
"soop": "0.1.5"
},
"devDependencies": { "devDependencies": {
"grunt-cli": "~0.1.13", "grunt-cli": "~0.1.13",
"karma": "~0.12.1", "karma": "~0.12.1",
"karma-chrome-launcher": "~0.1.2", "karma-chrome-launcher": "~0.1.2",
"mocha": "~1.18.2", "mocha": "~1.18.2",
"karma-mocha": "~0.1.3" "karma-mocha": "~0.1.3",
"chai": "~1.9.0"
} }
} }

1
test/mocha.opts Normal file
View file

@ -0,0 +1 @@
-R spec

84
test/test.wallet.js Normal file
View file

@ -0,0 +1,84 @@
'use strict';
var chai = chai || require('chai');
var should = chai.should();
var bitcore = bitcore || require('../node_modules/bitcore');
var cosign = cosign || {};
var fakeStorage = {}; // TODO
var Wallet = cosign.Wallet || require('soop').load('../js/models/Wallet', {Storage: fakeStorage});
var config = {
network:'livenet',
};
describe('Wallet model', function() {
it('should create an instance (livenet)', function () {
var w = new Wallet({
network: config.network
});
should.exist(w);
w.network.name.should.equal('livenet');
});
it('should create an instance (testnet)', function () {
var w2 = new Wallet();
should.exist(w2);
w2.network.name.should.equal('testnet');
});
it('should throw master priv key', function () {
var w2 = new Wallet(config);
should.exist(w2);
w2.getExtendedPrivKey.bind().should.throw();
});
it('should create an master priv key', function () {
var w2 = new Wallet(config);
should.exist(w2);
should.exist(w2.getExtendedPrivKey());
});
it('should create an master pub key', function () {
var w2 = new Wallet(config);
should.exist(w2);
should.exist(w2.getExtendedPubKey());
});
it('should fail to generate shared pub keys', function () {
var w2 = new Wallet(config);
should.exist(w2);
w2.getChangeAddress.bind(0).should.throw();
w2.registeredCosigners().should.equal(1);
w2.haveAllNeededPubKeys().should.equal(false);
});
it('should add and check when adding shared pub keys', function () {
var w = new Wallet(config);
should.exist(w);
var cosigners = [];
for(var i=0; i<4; i++) {
var c = new Wallet(config);
w.haveAllNeededPubKeys().should.equal(false);
w.addCosignerExtendedPubKey(c.getExtendedPubKey());
cosigners.push(c);
}
w.haveAllNeededPubKeys().should.equal(true);
w.addCosignerExtendedPubKey.bind(w.getExtendedPubKey()).should.throw();
w.addCosignerExtendedPubKey.bind(cosigners[0].getExtendedPubKey()).should.throw();
w.addCosignerExtendedPubKey.bind((new Wallet(config)).getExtendedPubKey()).should.throw();
});
});