mocha test working again

This commit is contained in:
Matias Alejo Garcia 2014-04-15 12:17:28 -03:00
commit eff20ec49a
12 changed files with 243 additions and 304 deletions

View file

@ -17,6 +17,8 @@ var storage = Storage.default();
function PublicKeyRing(opts) {
opts = opts || {};
this.walletId = opts.walletId;
this.network = opts.networkName === 'livenet' ?
bitcore.networks.livenet : bitcore.networks.testnet;
@ -46,16 +48,6 @@ PublicKeyRing.ChangeBranch = function (index) {
return 'm/1/'+index;
};
PublicKeyRing.decrypt = function (passphrase, encPayload) {
console.log('[wallet.js.35] TODO READ: passphrase IGNORED');
return encPayload;
};
PublicKeyRing.encrypt = function (passphrase, payload) {
console.log('[wallet.js.92] TODO: passphrase IGNORED');
return payload;
};
PublicKeyRing.fromObj = function (data) {
if (!data.ts) {
throw new Error('bad data format: Did you use .toObj()?');
@ -64,13 +56,12 @@ PublicKeyRing.fromObj = function (data) {
var w = new PublicKeyRing(config);
w.id = data.id;
w.requiredCopayers = data.requiredCopayers;
w.totalCopayers = data.totalCopayers;
w.addressIndex = data.addressIndex;
w.walletId = data.walletId;
w.requiredCopayers = data.requiredCopayers;
w.totalCopayers = data.totalCopayers;
w.addressIndex = data.addressIndex;
w.changeAddressIndex = data.changeAddressIndex;
w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
w.copayersBIP32 = data.copayersExtPubKeys.map( function (pk) {
return new BIP32(pk);
});
@ -78,24 +69,9 @@ PublicKeyRing.fromObj = function (data) {
return w;
};
PublicKeyRing.read = function (encPayload, id, passphrase) {
if (!encPayload)
throw new Error('Could not find wallet data');
var data;
try {
data = JSON.parse( PublicKeyRing.decrypt( passphrase, encPayload ));
} catch (e) {
throw new Error('error in read: '+ e.toString());
}
if (data.id !== id)
throw new Error('Wrong id in data');
return PublicKeyRing.fromObj(data);
};
PublicKeyRing.prototype.toObj = function() {
return {
id: this.id,
walletId: this.walletId,
networkName: this.network.name,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
@ -114,13 +90,6 @@ PublicKeyRing.prototype.serialize = function () {
};
PublicKeyRing.prototype.toStore = function (passphrase) {
if (!this.id)
throw new Error('wallet has no id');
return PublicKeyRing.encrypt(passphrase,this.serialize());
};
PublicKeyRing.prototype.registeredCopayers = function () {
return this.copayersBIP32.length;
};
@ -255,8 +224,8 @@ PublicKeyRing.prototype.getRedeemScriptMap = function () {
PublicKeyRing.prototype._checkInPRK = function(inPKR, ignoreId) {
if (!ignoreId && this.id !== inPKR.id) {
throw new Error('inPRK id mismatch');
if (!ignoreId && this.walletId !== inPKR.walletId) {
throw new Error('inPRK walletId mismatch');
}
if (this.network.name !== inPKR.network.name)

View file

@ -52,7 +52,18 @@ Wallet.prototype._createNew = function(config, opts) {
};
Wallet.prototype._checkLoad = function(config, walletId) {
return (
this.storage.get(this.id, 'publicKeyRing') &&
this.storage.get(this.id, 'txProposals') &&
this.storage.get(this.id, 'privateKey')
);
}
Wallet.prototype._load = function(config, walletId) {
if (! this._checkLoad(config,walletId)) return;
this.id = walletId;
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
this.storage.get(this.id, 'publicKeyRing')
@ -77,16 +88,46 @@ Wallet.prototype._load = function(config, walletId) {
Wallet.prototype.store = function() {
Wallet.factory.addWalletId(this.id);
this.storage.set(this.id,'publicKeyRing', this.publicKeyRing.toObj());
this.storage.set(this.id,'txProposals', this.txProposals.toObj());
this.storage.set(this.id,'privateKey', this.privateKey.toObj());
};
Wallet.prototype.sendTxProposals = function(recipients) {
console.log('### SENDING txProposals TO:', recipients||'All', this.txProposals);
this.network.send( recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(),
walletId: this.id,
});
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
console.log('### SENDING publicKeyRing TO:', recipients||'All');
console.log('[Wallet.js.100]', this.publicKeyRing.toObj()); //TODO
this.network.send(recipients, {
type: 'publicKeyRing',
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id,
});
};
// // HERE? not sure
// Wallet.prototype.cleanPeers = function() {
// this.storage.remove('peerData');
// };
//
// CONSTRUCTORS
Wallet.read = function(config, walletId) {
var w = new Wallet(config);
w._load(walletId);
w._load(config, walletId);
return w;
};
@ -97,25 +138,24 @@ Wallet.create = function(config, opts) {
return w;
};
<<<<<<< HEAD
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
return r;
};
Wallet.prototype.store = function() {
// TODO store each variable
};
/*
* WalletFactory
*
*/
var WalletFactory = function() {
this.storage = Storage.
default ();
this.storage = Storage.default();
};
WalletFactory.prototype.create = function(config, opts) {
var w = new Wallet.create(config, opts);
w.store();
this._addWalletId(w.id);
this.addWalletId(w.id);
return w;
};
@ -127,25 +167,22 @@ WalletFactory.prototype.remove = function(walletId) {
// TODO remove wallet contents, not only the id (Wallet.remove?)
this._delWalletId(walletId);
};
=======
module.exports = require('soop')(Wallet);
>>>>>>> WIP wallet working again
WalletFactory.prototype._addWalletId = function(walletId) {
var ids = this._getWalletIds();
WalletFactory.prototype.addWalletId = function(walletId) {
var ids = this.getWalletIds();
if (ids.indexOf(walletId) == -1) return;
storage.set('walletIds', (ids ? ids + ',' : '') + walletId);
};
WalletFactory.prototype._delWalletId = function(walletId) {
var ids = this._getWalletIds();
var ids = this.getWalletIds();
var index = ids.indexOf(walletId);
if (index == -1) return;
ids.splice(index, 1); // removes walletId
this.storage.set('walletIds', ids.join(','));
};
WalletFactory.prototype._getWalletIds = function() {
WalletFactory.prototype.getWalletIds = function() {
var ids = this.storage.get('walletIds');
return ids ? ids.split(',') : [];
};

View file

@ -273,6 +273,7 @@ Network.prototype.connectTo = function(peerId, openCallback, closeCallback ) {
self._setupConnectionHandlers(dataConn, false, openCallback, closeCallback);
};
Network.prototype.disconnect = function(peerId, cb) {
console.log('[Network.js.268:disconnect:]'); //TODO
var self = this;

View file

@ -16,12 +16,12 @@ Storage.prototype.get = function(walletId, k) {
// set value for key
Storage.prototype.set = function(k,v) {
Storage.prototype.set = function(walletId, k,v) {
localStorage.setItem(this._key(walletId,k), JSON.stringify(v));
};
// remove value for key
Storage.prototype.remove = function(k) {
Storage.prototype.remove = function(walletId, k) {
localStorage.removeItem(this._key(walletId,k));
};