This commit is contained in:
Matias Alejo Garcia 2014-04-04 15:58:10 -03:00
commit 1779d9e7c6
36 changed files with 2342 additions and 6060 deletions

View file

@ -1,6 +1,27 @@
'use strict';
angular.module('cosign',[
angular.module('copay',[
'ngRoute',
'ui.bootstrap'
'mm.foundation',
'monospaced.qrcode',
'copay.header',
'copay.home',
'copay.transactions',
'copay.send',
'copay.backup',
'copay.network',
'copay.signin',
'copay.peer',
'copay.storage'
]);
angular.module('copay.header', []);
angular.module('copay.home', []);
angular.module('copay.transactions', []);
angular.module('copay.send', []);
angular.module('copay.backup', []);
angular.module('copay.network', []);
angular.module('copay.signin', []);
angular.module('copay.peer', []);
angular.module('copay.storage', []);

View file

@ -2,21 +2,42 @@
//Setting up route
angular
.module('cosign')
.module('copay')
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'signin.html'
})
.when('/signin', {
templateUrl: 'signin.html'
})
.when('/home', {
templateUrl: 'home.html'
})
.when('/join/:id', {
templateUrl: 'join.html'
})
.when('/peer', {
templateUrl: 'peer.html'
})
.when('/transactions', {
templateUrl: 'transactions.html'
})
.when('/send', {
templateUrl: 'send.html'
})
.when('/backup', {
templateUrl: 'backup.html'
})
.otherwise({
templateUrl: '404.html'
});
});
//Setting HTML5 Location Mode
angular
.module('cosign')
.module('copay')
.config(function($locationProvider) {
$locationProvider
.html5Mode(false);

6
js/controllers/backup.js Normal file
View file

@ -0,0 +1,6 @@
'use strict';
angular.module('copay.backup').controller('BackupController',
function($scope, $rootScope, $location) {
$scope.title = 'Backup';
});

39
js/controllers/header.js Normal file
View file

@ -0,0 +1,39 @@
'use strict';
angular.module('copay.header').controller('HeaderController',
function($scope, $rootScope, $location, Network) {
$scope.menu = [{
'title': 'Home',
'link': '#/home'
}, {
'title': 'Transactions',
'link': '#/transactions'
}, {
'title': 'Send',
'link': '#/send'
}, {
'title': 'Backup',
'link': '#/backup'
}];
if (!$rootScope.peerId) {
$location.path('signin');
}
$scope.isActive = function(item) {
if (item.link.replace('#','') == $location.path()) {
return true;
}
return false;
};
$scope.init = function() {
$rootScope.isLogged = false;
};
$scope.signout = function() {
$rootScope.isLogged = false;
Network.disconnect();
$location.path('signin');
};
});

17
js/controllers/home.js Normal file
View file

@ -0,0 +1,17 @@
'use strict';
angular.module('copay.home').controller('HomeController',
function($scope, $rootScope, $location) {
$scope.title = 'Home';
$scope.oneAtATime = true;
if (!$rootScope.peerId) {
$location.path('signin');
}
$scope.addrs = [
{ addrStr: 'n3zUqNR7Bbbc4zJhPVj1vG2Lx66K3Xhzvb'},
{ addrStr: 'my9wnLwwUrwpNfEgSrWY62ymEGf1edKf4J'}
];
});

10
js/controllers/peer.js Normal file
View file

@ -0,0 +1,10 @@
'use strict';
angular.module('copay.peer').controller('PeerController',
function($scope, $rootScope, $location, $routeParams, Network) {
$scope.init = function() {
//Network.connect($rootScope.masterId);
};
});

11
js/controllers/send.js Normal file
View file

@ -0,0 +1,11 @@
'use strict';
angular.module('copay.send').controller('SendController',
function($scope, $rootScope, $location) {
$scope.title = 'Send';
if (!$rootScope.peerId) {
$location.path('signin');
}
});

43
js/controllers/signin.js Normal file
View file

@ -0,0 +1,43 @@
'use strict';
angular.module('copay.signin').controller('SigninController',
function($scope, $rootScope, $location, Network, Storage) {
var peerData = Storage.get('peerData');
$scope.loading = false;
$rootScope.peerId = peerData ? peerData.peerId : null;
$scope.create = function() {
$scope.loading = true;
Network.init(function() {
$location.path('peer');
});
};
$scope.join = function(cid) {
$scope.loading = true;
if (cid) {
$rootScope.connectedTo.push(cid);
Network.init(function() {
Network.connect(cid, function() {
$location.path('peer');
});
});
}
};
if (peerData && peerData.peerId) {
console.log('------- reloaded ----------');
console.log(peerData);
$rootScope.peerId = peerData.peerId;
$rootScope.connectedPeers = peerData.connectedPeers;
console.log(peerData.connectedTo[0]);
$scope.join(peerData.connectedTo[0]);
}
});

View file

@ -0,0 +1,38 @@
'use strict';
angular.module('copay.transactions').controller('TransactionsController',
function($scope, $rootScope, $location) {
$scope.title = 'Transactions';
$scope.oneAtATime = true;
if (!$rootScope.peerId) {
$location.path('signin');
}
$scope.txsinput = [
{
fromAddr: "n3zUqNR7Bbbc4zJhPVj1vG2Lx66K3Xhzvb",
toAddr: "msvv2mDfE298s7boXwALq4Dqv77K3TWRZ1",
amount: 23.9982
},
{
fromAddr: "my9wnLwwUrwpNfEgSrWY62ymEGf1edKf4J",
toAddr: "monCusNiDuptf68rtr58hEjKpJt6cW6zwS",
amount: 2.22
}
];
$scope.txsoutput = [
{
fromAddr: "n3zUqNR7Bbbc4zJhPVj1vG2Lx66K3Xhzvb",
toAddr: "msvv2mDfE298s7boXwALq4Dqv77K3TWRZ1",
amount: 23.9982
},
{
fromAddr: "my9wnLwwUrwpNfEgSrWY62ymEGf1edKf4J",
toAddr: "monCusNiDuptf68rtr58hEjKpJt6cW6zwS",
amount: 2.22
}
];
});

View file

@ -2,5 +2,5 @@
angular.element(document).ready(function() {
// Init the app
angular.bootstrap(document, ['cosign']);
angular.bootstrap(document, ['copay']);
});

View file

@ -1,7 +0,0 @@
var cosign = angular.module('cosign', []);
cosign.service('network', function() {
this.f = function() {
return 2;
};
});

189
js/services/network.js Normal file
View file

@ -0,0 +1,189 @@
'use strict';
angular.module('copay.network')
.factory('Network', function($rootScope, Storage) {
var peer;
$rootScope.connectedPeers = [];
$rootScope.connectedTo = [];
$rootScope.peerId = null;
var _arrayDiff = function(a, b) {
var seen = [];
var diff = [];
for ( var i = 0; i < b.length; i++)
seen[b[i]] = true;
for ( var i = 0; i < a.length; i++)
if (!seen[a[i]])
diff.push(a[i]);
return diff;
};
var _isInArray = function(i, array) {
return array.indexOf(i) > -1;
};
var _saveDataStorage = function() {
Storage.save('peerData', {
peerId: $rootScope.peerId,
connectedTo: $rootScope.connectedTo,
connectedPeers: $rootScope.connectedPeers
});
};
var _sender = function(pid, data) {
if (pid !== $rootScope.peerId) {
console.log('-------- sending data to: ' + pid + ' --------');
var conns = peer.connections[pid];
if (conns) {
var str = JSON.stringify({
sender: $rootScope.peerId,
data: data
});
for (var i = 0; i < conns.length; i++) {
var conn = conns[i];
conn.send(str);
}
}
}
};
var _onData = function(data) {
console.log('-------- Data received --------');
console.log(data);
var obj = JSON.parse(data);
if (obj.data.type === 'connectToPeers')
_connectToPeers(obj.data.peers);
if (obj.data.type === 'getPeers')
_send(obj.sender, {
type: 'connectToPeers',
peers: $rootScope.connectedPeers
});
};
var _connectToPeers = function(peers) {
var arrayDiff = _arrayDiff(peers, $rootScope.connectedTo);
arrayDiff.forEach(function(pid) {
_connect(pid);
});
};
var _init = function(cb) {
peer = new Peer($rootScope.peerId, {
key: 'lwjd5qra8257b9', // TODO: we need our own PeerServer KEY (http://peerjs.com/peerserver)
debug: 3
});
peer.on('open', function(pid) {
$rootScope.peerId = pid;
if (!_isInArray(pid, $rootScope.connectedPeers))
$rootScope.connectedPeers.push(pid);
_saveDataStorage();
cb();
$rootScope.$digest();
});
peer.on('connection', function(conn) {
if (conn.label === 'wallet') {
conn.on('open', function() {
console.log('<<<<<<<<<<< ' + conn.peer + ' conected to me --------');
var isConnected = $rootScope.connectedTo.indexOf(conn.peer);
if (isConnected === -1) {
var c = peer.connect(conn.peer, {
label: 'wallet',
serialization: 'none',
reliable: false,
metadata: { message: 'hi copayer!' }
});
c.on('open', function() {
console.log('>>>>>>>>> i am connected to ' + conn.peer);
if (!_isInArray(conn.peer, $rootScope.connectedPeers))
$rootScope.connectedPeers.push(conn.peer);
if (!_isInArray(conn.peer, $rootScope.connectedTo))
$rootScope.connectedTo.push(conn.peer);
_saveDataStorage();
$rootScope.$digest();
});
}
});
}
});
peer.on('data', _onData);
peer.on('close', function() {
console.log('------- connection closed ---------');
});
};
var _connect = function(pid, cb) {
if (pid !== $rootScope.peerId) {
console.log('------- conecting to ' + pid + ' ------');
var c = peer.connect(pid, {
label: 'wallet',
serialization: 'none',
reliable: false,
metadata: { message: 'hi copayer!' }
});
c.on('open', function() {
console.log('-------- I\'m connected to ' + pid + ' ------');
$rootScope.connectedPeers.push(pid);
$rootScope.connectedTo.push(pid);
if (typeof cb === 'function') cb();
_send(c.peer, { type: 'getPeers' });
$rootScope.$digest();
});
c.on('data', _onData);
c.on('error', function(err) {
console.error('-------- Error --------')
});
}
};
var _send = function(pids, data) {
if (Array.isArray(pids)) {
pids.forEach(function(pid) {
_sender(pid, data);
});
} else if (typeof pids === 'string') {
_sender(pids, data);
}
};
var _disconnect = function() {
peer.disconnect();
peer.destroy();
Storage.remove('peerData');
console.log('Disconnected and destroyed connection');
}
return {
init: _init,
connect: _connect,
send: _send,
disconnect: _disconnect
}
});

22
js/services/storage.js Normal file
View file

@ -0,0 +1,22 @@
'use strict';
angular.module('copay.storage')
.factory('Storage', function($rootScope) {
return {
get: function(key) {
return JSON.parse(localStorage.getItem(key));
},
save: function(key, data) {
localStorage.setItem(key, JSON.stringify(data));
},
remove: function(key) {
localStorage.removeItem(key);
},
clearAll: function() {
localStorage.clear();
}
};
});