read/load on Wallet

This commit is contained in:
Matias Alejo Garcia 2014-04-14 17:42:10 -03:00
parent 296c78cdf0
commit 124e1dff19
3 changed files with 52 additions and 7877 deletions

File diff suppressed because it is too large Load Diff

View File

@ -12,67 +12,82 @@ var Blockchain = imports.Blockchain || require('FakeBlockchain');
var copay = copay || require('../../../copay');
function Wallet(opts, config) {
opts = opts || {};
function Wallet(config) {
this._startInterface(config);
}
console.log('### CREATING WALLET.'
+ (opts.walletId ? ' USING ID: ' +opts.walletId : ' NEW ID') );
//
Wallet.prototype._startInterface = function(config) {
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
};
Wallet.prototype._createNew = function(config, opts) {
console.log('### CREATING NEW WALLET.'
+ (opts.walletId ? ' USING ID: ' +opts.walletId : ' NEW ID') );
this.privateKey = new copay.PrivateKey({networkName: config.networkName});
console.log('\t### PrivateKey Initialized');
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
id: opts.walletId,
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
networkName: config.networkName,
});
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
this.txProposals = new copay.TxProposals({
this.txProposals = opts.txProposals || new copay.TxProposals({
walletId: this.publicKeyRing.id,
publicKeyRing: this.publicKeyRing,
networkName: config.networkName,
});
console.log('\t### TxProposals Initialized');
}
//
// var Read = function(walletId) {
// this.storage.read(walletId);
// $rootScope.w = new copay.PublicKeyRing.fromObj(pkr);
// $rootScope.txProposals = new copay.TxProposals.fromObj(txp);
// $rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure
//
//
};
// HERE or in Storage?
// $rootScope.walletId = walletId;
// $rootScope.w = new copay.PublicKeyRing.fromObj(pkr);
// $rootScope.txProposals = new copay.TxProposals.fromObj(txp);
// $rootScope.PrivateKey = new copay.PrivateKey.fromObj(priv); //TODO secure
// // JIC: Add our key
// try {
// $rootScope.publicKeyRing.addCopayer(
// $rootScope.PrivateKey.getBIP32().extendedPublicKeyString()
// );
// } catch (e) {
// console.log('NOT NECCESARY AN ERROR:', e); //TODO
// };
// ret = true;
// }
// return ret;
// };
// };
Wallet.prototype._load = function(config, walletId) {
this.id = walletId;
this.publicKeyRing = new copay.PublicKeyRing.fromObj(
this.storage.get(this.id, 'publicKeyRing')
);
this.txProposals = new copay.TxProposals.fromObj(
this.storage.get(this.id, 'txProposals')
);
this.privateKey = new copay.PrivateKey.fromObj(
this.storage.get(this.id, 'privateKey')
); //TODO secure
// JIC: Add our key
try {
this.publicKeyRing.addCopayer(
this.privateKey.getBIP32().extendedPublicKeyString()
);
} catch (e) {
console.log('NOT NECCESARY AN ERROR:', e); //TODO
};
};
// CONSTRUCTORS
Wallet.read = function(config, walletId) {
var w = new Wallet(config);
w.load(walletId);
return w;
};
Wallet.create = function(config, opts) {
var w = new Wallet(config);
w._createNew(config, opts);
return w;
};
module.exports = require('soop')(Wallet);

View File

@ -18,7 +18,7 @@ describe('Wallet model', function() {
it('should create an instance', function () {
var opts = {};
var w = new Wallet(opts, config);
var w = new Wallet(config, opts);
should.exist(w);
});
});