wallet @browser working again

This commit is contained in:
Matias Alejo Garcia 2014-04-15 12:52:28 -03:00
parent eff20ec49a
commit 4dcd18c42f
8 changed files with 55 additions and 53 deletions

View File

@ -2,5 +2,12 @@
angular.module('copay.backup').controller('BackupController',
function($scope, $rootScope, $location) {
if (!$rootScope.wallet.id) {
$location.path('signin');
}
$scope.title = 'Backup';
});

View File

@ -6,15 +6,17 @@ angular.module('copay.home').controller('HomeController',
$scope.oneAtATime = true;
if (!$rootScope.peerId) {
if (!$rootScope.wallet.id) {
$location.path('signin');
}
$scope.addrs = $rootScope.publicKeyRing.getAddresses();
$scope.addrs = $rootScope.wallet.publicKeyRing.getAddresses();
$scope.selectedAddr = $scope.addrs[0];
$scope.newAddr = function() {
var a = $rootScope.publicKeyRing.generateAddress();
var a = $rootScope.wallet.publicKeyRing.generateAddress();
$scope.addrs.push({ addrStr: a.toString('hex') });
};

View File

@ -4,14 +4,15 @@ angular.module('copay.send').controller('SendController',
function($scope, $rootScope, $location, Network) {
$scope.title = 'Send';
if (!$rootScope.peerId) {
if (!$rootScope.wallet.id) {
$location.path('signin');
}
$scope.sendTest = function() {
var pkr = $rootScope.publicKeyRing;
var txp = $rootScope.txProposals;
var w = $rootScope.wallet;
var pkr = w.publicKeyRing;
var txp = w.txProposals;
var opts = {remainderOut: { address: pkr.generateAddress(true).toString() }};
// From @cmgustavo's wallet
@ -31,7 +32,7 @@ console.log('[send.js.29:txp:] BEFORE',txp); //TODO
'15q6HKjWHAksHcH91JW23BJEuzZgFwydBt',
'123456789',
unspentTest,
$rootScope.privateKey,
w.privateKey,
opts
);
console.log('[send.js.29:txp:] READY:',txp); //TODO

View File

@ -23,8 +23,6 @@ angular.module('copay.signin').controller('SigninController',
$scope.open = function(walletId) {
$scope.loading = true;
console.log('[signin.js.28:walletId:]',walletId); //TODO
if (Network.openWallet(walletId)) {
Network.init(function() {
$location.path('peer');

View File

@ -6,7 +6,7 @@ angular.module('copay.transactions').controller('TransactionsController',
$scope.oneAtATime = true;
if (!$rootScope.peerId) {
if (!$rootScope.wallet.id) {
$location.path('signin');
}
@ -23,5 +23,5 @@ angular.module('copay.transactions').controller('TransactionsController',
}
];
$scope.txsoutput = $rootScope.txProposals.txps;
$scope.txsoutput = $rootScope.wallet.txProposals.txps;
});

View File

@ -21,47 +21,52 @@ Wallet.prototype._startInterface = function(config) {
this.storage = new Storage(config.storage);
this.network = new Network(config.network);
this.blockchain = new Blockchain(config.blockchain);
this.networkName = config.networkName;
this.requiredCopayers = config.requiredCopayers;
this.totalCopayers = config.totalCopayers;
};
Wallet.prototype._createNew = function(config, opts) {
Wallet.prototype.create = function(opts) {
this.id = opts.id || Wallet.getRandomId();
console.log('### CREATING NEW WALLET.' + (opts.id ? ' USING ID: ' + opts.id : ' NEW ID'));
this.privateKey = new copay.PrivateKey({
networkName: config.networkName
networkName: this.networkName
});
console.log('\t### PrivateKey Initialized');
this.publicKeyRing = opts.publicKeyRing || new copay.PublicKeyRing({
id: this.id,
requiredCopayers: opts.requiredCopayers || config.wallet.requiredCopayers,
totalCopayers: opts.totalCopayers || config.wallet.totalCopayers,
networkName: config.networkName,
this.publicKeyRing = new copay.PublicKeyRing({
walletId: this.id,
requiredCopayers: opts.requiredCopayers || this.requiredCopayers,
totalCopayers: opts.totalCopayers || this.totalCopayers,
networkName: this.networkName,
});
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.id);
this.txProposals = opts.txProposals || new copay.TxProposals({
this.publicKeyRing.addCopayer(this.privateKey.getBIP32().extendedPublicKeyString());
console.log('\t### PublicKeyRing Initialized WalletID: ' + this.publicKeyRing.walletId);
this.txProposals = new copay.TxProposals({
walletId: this.id,
publicKeyRing: this.publicKeyRing,
networkName: config.networkName,
networkName: this.networkName,
});
console.log('\t### TxProposals Initialized');
};
Wallet.prototype._checkLoad = function(config, walletId) {
Wallet.prototype._checkLoad = function(walletId) {
return (
this.storage.get(this.id, 'publicKeyRing') &&
this.storage.get(this.id, 'txProposals') &&
this.storage.get(this.id, 'privateKey')
this.storage.get(walletId, 'publicKeyRing') &&
this.storage.get(walletId, 'txProposals') &&
this.storage.get(walletId, 'privateKey')
);
}
Wallet.prototype._load = function(config, walletId) {
if (! this._checkLoad(config,walletId)) return;
Wallet.prototype.load = function(walletId) {
if (! this._checkLoad(walletId)) return;
this.id = walletId;
@ -106,10 +111,7 @@ Wallet.prototype.sendTxProposals = function(recipients) {
};
Wallet.prototype.sendPublicKeyRing = function(recipients) {
console.log('### SENDING publicKeyRing TO:', recipients||'All');
console.log('[Wallet.js.100]', this.publicKeyRing.toObj()); //TODO
console.log('### SENDING publicKeyRing TO:', recipients||'All', this.publicKeyRing.toObj());
this.network.send(recipients, {
type: 'publicKeyRing',
@ -124,19 +126,6 @@ console.log('[Wallet.js.100]', this.publicKeyRing.toObj()); //TODO
// this.storage.remove('peerData');
// };
//
// CONSTRUCTORS
Wallet.read = function(config, walletId) {
var w = new Wallet(config);
w._load(config, walletId);
return w;
};
Wallet.create = function(config, opts) {
var w = new Wallet(config);
w._createNew(config, opts);
return w;
};
Wallet.getRandomId = function() {
var r = buffertools.toHex(coinUtil.generateNonce());
@ -153,7 +142,8 @@ var WalletFactory = function() {
};
WalletFactory.prototype.create = function(config, opts) {
var w = new Wallet.create(config, opts);
var w = new Wallet(config);
w.create(opts);
w.store();
this.addWalletId(w.id);
return w;

View File

@ -220,6 +220,8 @@ Network.prototype.start = function(openCallback) {
Network.prototype._sendToOne = function(peerId, data, cb) {
if (peerId !== this.peerId) {
console.log('[WebRTC.js.222:peerId:]',peerId, data); //TODO
var conns = this.peer.connections[peerId];
if (conns) {
@ -240,10 +242,12 @@ Network.prototype._sendToOne = function(peerId, data, cb) {
Network.prototype.send = function(peerIds, data, cb) {
var self=this;
console.log('[WebRTC.js.242:peerIds:]',peerIds); //TODO
if (!peerIds) {
peerIds = this.connectedPeers;
data.isBroadcast = 1;
}
console.log('[WebRTC.js.246:peerIds:]',peerIds, data); //TODO
if (Array.isArray(peerIds)) {
var l = peerIds.length;

View File

@ -39,16 +39,16 @@ angular.module('copay.network')
// TODO -> probably not in network.js
var createWallet = function(walletId) {
console.log('[network.js.41:walletId:]',walletId); //TODO
var w = new copay.Wallet.create(config, {id: walletId});
var w = $rootScope.wallet || new copay.Wallet(config);
w.create({id: walletId});
w.store();
$rootScope.wallet = w;
console.log('createWallet ENDED'); //TODO
};
var openWallet = function (walletId) {
var w = new copay.Wallet.read(config, walletId);
var w = $rootScope.wallet || new copay.Wallet(config);
w.load(walletId);
if (w && w.publicKeyRing && w.privateKey) {
console.log('### WALLET OPENED:', w.walletId);
$rootScope.wallet = w;
@ -58,13 +58,14 @@ console.log('[network.js.41:walletId:]',walletId); //TODO
var closeWallet = function() {
var w = $rootScope.wallet;
w.store();
if (w && w.id)
w.store();
console.log('### CLOSING WALLET');
delete $rootScope['wallet'];
};
var _checkWallet = function(walletId, allowChange) {
var _checkWallet = function(walletId) {
console.log('[network.js.79:_checkWallet:]',walletId); //TODO
if ($rootScope.wallet && $rootScope.wallet.id === walletId)
@ -73,7 +74,6 @@ console.log('[network.js.41:walletId:]',walletId); //TODO
if ($rootScope.wallet && $rootScope.wallet.id && $rootScope.wallet.id !== walletId) {
throw new Error('message to wrong walletID');
}
if (!openWallet(walletId)) {
createWallet(walletId);