Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Move $peers service account-related methods to Account - Closes #169 #176

Merged
merged 7 commits into from
May 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/components/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ app.component('main', {

this.$rootScope.prelogged = true;

this.$peers.setActive();
this.$peers.setActive(this.account.get());

this.update()
.then(() => {
Expand Down Expand Up @@ -66,7 +66,7 @@ app.component('main', {

update() {
this.$rootScope.reset();
return this.$peers.active.getAccountPromise(this.account.get().address)
return this.account.getAccountPromise(this.account.get().address)
.then((res) => {
this.account.set({ balance: res.balance });
})
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/main/setSecondPassDirective.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import './secondPass.less';

app.directive('setSecondPass', (setSecondPass, $peers, $rootScope, dialog) => {
app.directive('setSecondPass', (setSecondPass, Account, $rootScope, dialog) => {
/* eslint no-param-reassign: ["error", { "props": false }] */
const SetSecondPassLink = function (scope, element, attrs) {
element.bind('click', () => {
setSecondPass.show();
});

scope.passConfirmSubmit = (secondsecret) => {
$peers.active.setSignature(secondsecret, attrs.publicKey, attrs.passphrase)
Account.setSecondSecret(secondsecret, attrs.publicKey, attrs.passphrase)
.then(() => {
dialog.successAlert('Your second passphrase was successfully registered.');
})
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/send/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ app.component('send', {

this.promptSecondPassphrase()
.then((secondPassphrase) => {
this.$peers.active.sendLSKPromise(
this.account.sendLSK(
this.recipient.value,
this.amount.raw,
this.account.get().passphrase,
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/transactions/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ app.component('transactions', {
limit = 10;
}

return this.$peers.listTransactions(this.account.get().address, limit)
return this.account.listTransactions(this.account.get().address, limit)
.then(this._processTransactionsResponse.bind(this))
.catch(() => {
this.transactions = [];
Expand Down
32 changes: 30 additions & 2 deletions src/app/services/account.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lisk from 'lisk-js';

app.factory('Account', function ($rootScope) {
app.factory('Account', function ($rootScope, $peers, $q) {
this.account = {};

const merge = (obj) => {
Expand All @@ -15,7 +15,7 @@ app.factory('Account', function ($rootScope) {
}

// Calling listeners with the list of changes
$rootScope.$broadcast('onAccountChange', {});
$rootScope.$broadcast('onAccountChange', this.account);
});
};

Expand All @@ -33,5 +33,33 @@ app.factory('Account', function ($rootScope) {
});
};

this.getAccountPromise = (address) => {
const deferred = $q.defer();
$peers.active.getAccount(this.account.address, (data) => {
if (data.success) {
deferred.resolve(data.account);
} else {
deferred.resolve({
address,
balance: 0,
});
}
});
return deferred.promise;
};

this.sendLSK = (recipientId, amount, secret, secondSecret) => $peers.sendRequestPromise(
'transactions', { recipientId, amount, secret, secondSecret });

this.listTransactions = (address, limit = 20, offset = 0) => $peers.sendRequestPromise('transactions', {
senderId: address,
recipientId: address,
limit,
offset,
});

this.setSecondSecret = (secondSecret, publicKey, secret) => $peers.sendRequestPromise(
'signatures', { secondSecret, publicKey, secret });

return this;
});
66 changes: 9 additions & 57 deletions src/app/services/peers/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import lisk from 'lisk-js';

const UPDATE_INTERVAL_CHECK = 10000;

app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope, Account) => {
app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope) => {
class $peers {
constructor() {
this.check();

$rootScope.$on('onAccountChange', () => {
this.setActive();
$rootScope.$on('onAccountChange', (event, account) => {
this.setActive(account);
});
}

Expand All @@ -20,9 +20,9 @@ app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope, Account) =
}
}

setActive() {
setActive(account) {
let conf = { };
const network = Account.get().network;
const network = account.network;
if (network) {
conf = network;
if (network.address) {
Expand All @@ -35,7 +35,7 @@ app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope, Account) =
}
}

this.setPeerAPIObject(conf);
this.active = lisk.api(conf);
this.check();
}

Expand All @@ -50,56 +50,8 @@ app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope, Account) =
return deferred.promise;
}

listTransactions(address, limit, offset) {
return this.sendRequestPromise('transactions', {
senderId: address,
recipientId: address,
limit: limit || 20,
offset: offset || 0,
});
}

setPeerAPIObject(config) {
this.active = lisk.api(config);

this.active.getStatusPromise = () => this.sendRequestPromise('loader/status', {});

this.active.getAccountPromise = (address) => {
const deferred = $q.defer();
this.active.getAccount(address, (data) => {
if (data.success) {
deferred.resolve(data.account);
} else {
deferred.resolve({
address,
balance: 0,
});
}
});
return deferred.promise;
};

this.active.sendLSKPromise = (recipient, amount, passphrase, secondPassphrase) => {
const deferred = $q.defer();
this.active.sendLSK(recipient, amount, passphrase, secondPassphrase, (data) => {
if (data.success) {
return deferred.resolve(data);
}
return deferred.reject(data);
});
return deferred.promise;
};

this.active.setSignature = (secondSecret, publicKey, secret) => {
const deferred = $q.defer();
this.active.sendRequest('signatures', { secondSecret, publicKey, secret }, (res) => {
if (res.success) {
deferred.resolve(res);
}
deferred.reject(res);
});
return deferred.promise;
};
getStatusPromise() {
return this.sendRequestPromise('loader/status', {});
}

check() {
Expand All @@ -112,7 +64,7 @@ app.factory('$peers', ($timeout, $cookies, $location, $q, $rootScope, Account) =
return;
}

this.active.getStatusPromise()
this.getStatusPromise()
.then(() => this.online = true)
.catch(() => this.online = false)
.finally(() => next());
Expand Down
7 changes: 0 additions & 7 deletions src/app/services/sign-verify.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
app.factory('signVerify', ($mdDialog, $mdMedia) => ({
openSignMessageDialog() {
return $mdDialog.show({
controllerAs: '$ctrl',
controller: class signMessageDialog {
constructor($scope, Account) {
this.$scope = $scope;
this.account = Account;
}
},
template:
'<md-dialog flex="80">' +
'<sign-message></sign-message>' +
Expand Down
11 changes: 5 additions & 6 deletions src/test/components/main/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,9 @@ describe('main component controller', () => {
balance: '0',
passphrase: 'wagon stock borrow episode laundry kitten salute link globe zero feed marble',
});
controller.$peers.active = {
getAccountPromise() {
return deffered.promise;
},
const mock = sinon.mock(controller.account);
mock.expects('getAccountPromise').returns(deffered.promise);
controller.$peers = {
getStatusPromise() {
return $q.defer().promise;
},
Expand All @@ -167,15 +166,15 @@ describe('main component controller', () => {
account.reset();
});

it('calls this.$peers.active.getAccountPromise(this.address) and then sets balance', () => {
it('calls this.account.getAccountPromise(this.address) and then sets balance', () => {
expect(account.get().balance).to.equal(undefined);
controller.update();
deffered.resolve({ balance: 12345 });
$scope.$apply();
expect(account.get().balance).to.equal(12345);
});

it('calls this.$peers.active.getAccountPromise(this.address) and if it fails, then resets this.account.balance and reject the promise that update() returns', () => {
it('calls this.account.getAccountPromise(this.address) and if it fails, then resets this.account.balance and reject the promise that update() returns', () => {
const spy = sinon.spy(controller.$q, 'reject');
controller.update();
deffered.reject();
Expand Down
35 changes: 15 additions & 20 deletions src/test/components/main/setSecondPassDirective.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('setSecondPass Directive', () => {
let $scope;
let $rootScope;
let element;
let $peers;
let account;
let setSecondPass;
let $q;
let dialog;
Expand All @@ -21,12 +21,12 @@ describe('setSecondPass Directive', () => {

// Store references to $rootScope and $compile
// so they are available to all tests in this describe block
inject((_$compile_, _$rootScope_, _setSecondPass_, _$peers_, _$q_, _dialog_) => {
inject((_$compile_, _$rootScope_, _setSecondPass_, _Account_, _$q_, _dialog_) => {
// The injector unwraps the underscores (_) from around the parameter names when matching
$compile = _$compile_;
$rootScope = _$rootScope_;
setSecondPass = _setSecondPass_;
$peers = _$peers_;
account = _Account_;
$q = _$q_;
dialog = _dialog_;
$scope = $rootScope.$new();
Expand All @@ -39,10 +39,9 @@ describe('setSecondPass Directive', () => {

describe('SetSecondPassLink', () => {
it('listens for an onAfterSignup event', () => {
$peers.active = { setSignature() {} };
const mock = sinon.mock($peers.active);
const mock = sinon.mock(account);
const deffered = $q.defer();
mock.expects('setSignature').returns(deffered.promise);
mock.expects('setSecondSecret').returns(deffered.promise);

const spy = sinon.spy(dialog, 'successAlert');

Expand All @@ -67,11 +66,10 @@ describe('setSecondPass Directive', () => {
});

describe('scope.passConfirmSubmit', () => {
it('should call $peers.active.setSignature', () => {
$peers.active = { setSignature() {} };
const mock = sinon.mock($peers.active);
it('should call account.setSecondSecret', () => {
const mock = sinon.mock(account);
const deffered = $q.defer();
mock.expects('setSignature').returns(deffered.promise);
mock.expects('setSecondSecret').returns(deffered.promise);

const spy = sinon.spy(dialog, 'successAlert');
$scope.passConfirmSubmit();
Expand All @@ -82,11 +80,10 @@ describe('setSecondPass Directive', () => {
expect(spy).to.have.been.calledWith();
});

it('should show error dialog if trying to set second passphrase mulpiple times', () => {
$peers.active = { setSignature() {} };
const mock = sinon.mock($peers.active);
it('should show error dialog if trying to set second passphrase multiple times', () => {
const mock = sinon.mock(account);
const deffered = $q.defer();
mock.expects('setSignature').returns(deffered.promise);
mock.expects('setSecondSecret').returns(deffered.promise);

const spy = sinon.spy(dialog, 'errorAlert');
$scope.passConfirmSubmit();
Expand All @@ -105,10 +102,9 @@ describe('setSecondPass Directive', () => {
});

it('should show error dialog if account does not have enough LSK', () => {
$peers.active = { setSignature() {} };
const mock = sinon.mock($peers.active);
const mock = sinon.mock(account);
const deffered = $q.defer();
mock.expects('setSignature').returns(deffered.promise);
mock.expects('setSecondSecret').returns(deffered.promise);

const spy = sinon.spy(dialog, 'errorAlert');
$scope.passConfirmSubmit();
Expand All @@ -119,10 +115,9 @@ describe('setSecondPass Directive', () => {
});

it('should show error dialog for all the other errors', () => {
$peers.active = { setSignature() {} };
const mock = sinon.mock($peers.active);
const mock = sinon.mock(account);
const deffered = $q.defer();
mock.expects('setSignature').returns(deffered.promise);
mock.expects('setSecondSecret').returns(deffered.promise);

const spy = sinon.spy(dialog, 'errorAlert');
$scope.passConfirmSubmit();
Expand Down
Loading