diff --git a/lib/node/config.js b/lib/node/config.js index 5c39749b..88cda889 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -416,7 +416,7 @@ Config.prototype.fixed = function fixed(key, exp, fallback) { return fallback; try { - return util.fromFixed(value.toString(10), exp || 0); + return util.fromDouble(value, exp || 0); } catch (e) { throw new Error(`${fmt(key)} must be a fixed number.`); } diff --git a/lib/utils/util.js b/lib/utils/util.js index bb76ac70..ae295dd9 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -863,6 +863,20 @@ util.toFixed = function toFixed(num, exp) { return `${sign}${hi}.${lo}`; }; +/** + * Parse a double float number and multiply by a + * power of ten (uses no floating point arithmetic). + * @param {Number} num + * @param {Number} exp - Number of decimal places. + * @returns {Number} + */ + +util.fromDouble = function fromDouble(num, exp) { + assert(typeof num === 'number' && isFinite(num)); + assert(Number.isSafeInteger(exp)); + return util.fromFixed(num.toFixed(exp), exp); +}; + /** * Parse a fixed number string and multiply by a * power of ten (uses no floating point arithmetic). diff --git a/lib/utils/validator.js b/lib/utils/validator.js index 3603eb20..9f0afa99 100644 --- a/lib/utils/validator.js +++ b/lib/utils/validator.js @@ -273,7 +273,7 @@ Validator.prototype.fixed = function fixed(key, exp, fallback) { return fallback; try { - return util.fromFixed(value.toString(10), exp || 0); + return util.fromDouble(value, exp || 0); } catch (e) { throw new ValidationError(key, 'fixed number'); } diff --git a/test/script-test.js b/test/script-test.js index 8936fd71..8f6530a4 100644 --- a/test/script-test.js +++ b/test/script-test.js @@ -8,6 +8,7 @@ const Script = require('../lib/script/script'); const Witness = require('../lib/script/witness'); const Stack = require('../lib/script/stack'); const TX = require('../lib/primitives/tx'); +const util = require('../lib/utils/util'); const encoding = require('../lib/utils/encoding'); const opcodes = Script.opcodes; @@ -39,9 +40,9 @@ function parseScriptTest(data) { comments += ` (${expected})`; - let amount = 0; - if (witArr.length !== 0) - amount = witArr.pop() * 1e8; + let value = 0; + if (witArr.length > 0) + value = util.fromDouble(witArr.pop(), 8); const witness = Witness.fromString(witArr); const input = Script.fromString(inpHex); @@ -58,7 +59,7 @@ function parseScriptTest(data) { witness: witness, input: input, output: output, - amount: amount, + value: value, flags: flags, expected: expected, comments: comments @@ -279,7 +280,7 @@ describe('Script', function() { const test = parseScriptTest(data); const {witness, input, output} = test; - const {amount, flags} = test; + const {value, flags} = test; const {expected, comments} = test; for (const noCache of [false, true]) { @@ -300,7 +301,7 @@ describe('Script', function() { }], outputs: [{ script: output, - value: amount + value: value }], locktime: 0 }); @@ -319,7 +320,7 @@ describe('Script', function() { }], outputs: [{ script: [], - value: amount + value: value }], locktime: 0 }); @@ -331,7 +332,7 @@ describe('Script', function() { let err, res; try { - res = Script.verify(input, witness, output, tx, 0, amount, flags); + res = Script.verify(input, witness, output, tx, 0, value, flags); } catch (e) { err = e; }