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

Commit

Permalink
Merge pull request #1477 from blockchain/mew-sweep-2
Browse files Browse the repository at this point in the history
Mew sweep 2
  • Loading branch information
plondon authored Feb 26, 2018
2 parents 36e7a98 + a7363fb commit 6758b6f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/partials/eth-mew-sweep.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ form.bc-form(name="EthMewSweepForm" id="EthMewSweepForm" ng-submit="submit()")
button.btn.button-primary-inverse(type="button" ngf-select ng-model="file" ngf-model-invalid="invalidFile" ngf-pattern="")
i.mr-10.ti-upload
span(translate="MEW.select_wallet_file")
span(ng-show="keystore && fileError")
span(ng-show="fileError")
i.ml-10.f-24.icon-alert.state-danger-text
span(ng-show="keystore && !fileError")
i.ml-10.f-24.icon-success.security-green
.group.mt-5(ng-show="keystore && fileError")
.group.mt-5(ng-show="fileError")
span.state-danger-text(translate="File type unsupported. Please use your UTC / JSON file.")
.group.mt-20(ng-show="keystore && !fileError")
.item
Expand Down
67 changes: 45 additions & 22 deletions assets/js/controllers/ethMewSweepController.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,67 @@ function EthMewSweepController ($q, $scope, Alerts, Ethereum, AngularHelper, Upl
AngularHelper.installLock.call($scope);
$scope.form = {};

let parseInput = (input) => {
if (typeof input === 'string') {
return input.toLowerCase();
} else {
throw new Error('Input type not supported');
}
};

$scope.$watch('file', (newVal) => {
reader.onloadend = (evt) => {
if (evt.target.readyState === 2) {
$scope.keystore = evt.target.result;
try {
JSON.parse($scope.keystore);
let input = parseInput(evt.target.result);
$scope.keystore = JSON.parse(input);
$scope.fileError = null;
} catch (e) {
$scope.keystore = null;
$scope.fileError = e;
}
}
};

newVal && reader.readAsText(newVal);
});

let submitTx = (account) => {
if (!account) { throw new Error('Ethereum account needed'); }
if (!account.isCorrectPrivateKey(account.privateKey)) { throw new Error('Incorrect Private Key'); }

$q.all([account.fetchBalance(), Ethereum.fetchFees()]).then(([balance, fees]) => {
$scope.payment = account.createPayment();
$scope.payment.setGasPrice(fees.regular);
$scope.payment.setGasLimit(fees.gasLimit);
$scope.payment.setSweep();
$scope.payment.setTo(Ethereum.defaultAccount.address);
let privateKey = account.privateKey;
$scope.payment.sign(privateKey);
return $scope.payment.publish().then(() => {
$scope.$dismiss();
Wallet.api.incrementEventStat('mew_sweep_success');
Alerts.displaySentBitcoin('ETHER_SEND_SUCCESS_MEW');
}).catch((e) => {
$scope.free();
if (e.message.toLowerCase().indexOf('insufficient funds') > -1) Alerts.displayError('MEW.fund_error');
else Alerts.displayError(e.message);
});
});
};

$scope.submit = () => {
$scope.lock();
if ($scope.fileError) {
Alerts.displayError('There was a problem with your file.');
return false;
}
if (!$scope.keystore) {
Alerts.displayError('File required.');
return false;
}
try {
$scope.account = Ethereum.eth.fromMew($scope.keystore, $scope.password, true);
return $q.all([$scope.account.fetchBalance(), Ethereum.fetchFees()]).then(([balance, fees]) => {
$scope.payment = $scope.account.createPayment();
$scope.payment.setGasPrice(fees.regular);
$scope.payment.setGasLimit(fees.gasLimit);
$scope.payment.setSweep();
$scope.payment.setTo(Ethereum.defaultAccount.address);
let privateKey = $scope.account.privateKey;
$scope.payment.sign(privateKey);
return $scope.payment.publish().then(() => {
$scope.$dismiss();
Wallet.api.incrementEventStat('mew_sweep_success');
Alerts.displaySentBitcoin('ETHER_SEND_SUCCESS_MEW');
}).catch((e) => {
$scope.free();
if (e.message.toLowerCase().indexOf('insufficient funds') > -1) Alerts.displayError('MEW.fund_error');
else Alerts.displayError(e.message);
});
});
let account = Ethereum.eth.fromMew($scope.keystore, $scope.password);
submitTx(account);
} catch (err) {
$scope.free();
Alerts.displayError(err.message);
Expand Down
40 changes: 40 additions & 0 deletions tests/controllers/eth_mew_sweep_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
describe('EthMewSweepController', () => {
let scope;
let $rootScope;
let $controller;
let Ethereum;

beforeEach(angular.mock.module('walletApp'));

beforeEach(function () {
return angular.mock.inject(function ($injector, _$rootScope_, _$controller_, _$state_, $httpBackend) {
$rootScope = _$rootScope_;
$controller = _$controller_;
$httpBackend.whenGET('/Resources/wallet-options.json').respond();

scope = $rootScope.$new();

Ethereum = $injector.get('Ethereum')
Ethereum.eth.fromMew = (file) => file

$controller('EthMewSweepController', {
$scope: scope
});
});
});

describe('.submit()', () => {

it('should not submit if file is invalid', () => {
scope.fileError = 'Invalid'
expect(scope.submit()).toBe(false)
});

it('should submit if file is valid', () => {
spyOn(Ethereum.eth, 'fromMew')
scope.keystore = '{"version":3,"id":"cb22a4b1-31cc-4c67-9982-588102d7b5d8","address":"5d6987a4992f02d014abc98603c19337fb88390c","crypto":{"ciphertext":"3d8131e34f5a613ed00ef4e4b8ebc4e46ddc04b45b31af97141e7cbd74ff7b1c","cipherparams":{"iv":"9b48c1d947065c4e755512294bef381b"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"581e0240a516b7df92d6bf171fbe43a9a5849a29126743b9cf0c65f2d35afd66","n":8192,"r":8,"p":1},"mac":"dd9dc3cfc79d462a36a096e6d46960f47aa7c491cfd3f940678fb76d7a6aec0e"}}';
scope.submit()
expect(Ethereum.eth.fromMew).toHaveBeenCalled()
})
});
});

0 comments on commit 6758b6f

Please sign in to comment.