check precision is consistent with unit

This commit is contained in:
Ivan Socolsky 2015-02-18 15:39:18 -03:00
parent a5a672b473
commit c23f962030
2 changed files with 9 additions and 1 deletions

View File

@ -121,7 +121,10 @@ WalletUtils.parseAmount = function(text) {
var rate = WalletUtils.UNITS[unit];
if (!rate) throw new Error('Invalid unit')
return Utils.strip(amount * rate);
var amountSat = Utils.strip(amount * rate);
if (amountSat != Math.round(amountSat)) throw new Error('Invalid amount');
return amountSat;
};
module.exports = WalletUtils;

View File

@ -80,6 +80,7 @@ describe('WalletUtils', function() {
var texts = {
'1': 1,
'0': 0,
'1.': 1,
'000000.0000': 0,
'123': 123,
'123sat': 123,
@ -113,8 +114,12 @@ describe('WalletUtils', function() {
'-3',
'1 b t c',
'btc1',
'btc 1',
'1,234',
'0.000000001btc',
'0.1sat',
'0.123bit',
'2.000000009btc',
];
_.each(texts, function(text) {
var valid = true;