BigNumber support

This commit is contained in:
Marek Kotewicz 2015-01-16 10:47:43 +01:00
parent ec74fc05d4
commit fbcc6d0d25
7 changed files with 39 additions and 29 deletions

26
dist/ethereum.js vendored

@ -27,6 +27,8 @@ if ("build" !== 'build') {/*
var web3 = require('./web3'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
*/} */}
var BigNumber = require('bignumber.js');
// TODO: make these be actually accurate instead of falling back onto JS's doubles. // TODO: make these be actually accurate instead of falling back onto JS's doubles.
var hexToDec = function (hex) { var hexToDec = function (hex) {
return parseInt(hex, 16).toString(); return parseInt(hex, 16).toString();
@ -85,24 +87,26 @@ var namedType = function (name) {
var setupInputTypes = function () { var setupInputTypes = function () {
/// Formats input value to byte representation of int /// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// @returns right-aligned byte representation of int /// @returns right-aligned byte representation of int
var formatInt = function (value) { var formatInt = function (value) {
var padding = 32 * 2; var padding = 32 * 2;
if (typeof value === 'number') { if (value instanceof BigNumber) {
if (value < 0) { if (value.lessThan(0))
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16);
// two's complement else
// TODO: fix big numbers support
value = ((value) >>> 0).toString(16);
return padLeft(value, padding, 'f');
}
value = value.toString(16); value = value.toString(16);
}
else if (typeof value === 'number') {
if (value < 0)
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16);
else
value = new BigNumber(value).toString(16);
} }
else if (value.indexOf('0x') === 0) else if (value.indexOf('0x') === 0)
value = value.substr(2); value = value.substr(2);
else if (typeof value === 'string') else if (typeof value === 'string')
value = value.toHex(value); value = new BigNumber(value).toString(16);
else else
value = (+value).toString(16); value = (+value).toString(16);
return padLeft(value, padding); return padLeft(value, padding);
@ -295,7 +299,7 @@ module.exports = {
}; };
},{}],2:[function(require,module,exports){ },{"bignumber.js":undefined}],2:[function(require,module,exports){
/* /*
This file is part of ethereum.js. This file is part of ethereum.js.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -3,6 +3,7 @@
<head> <head>
<script type="text/javascript" src="js/es6-promise/promise.min.js"></script> <script type="text/javascript" src="js/es6-promise/promise.min.js"></script>
<script type="text/javascript" src="../node_modules/bignumber.js/bignumber.min.js"></script>
<script type="text/javascript" src="../dist/ethereum.js"></script> <script type="text/javascript" src="../dist/ethereum.js"></script>
<script type="text/javascript"> <script type="text/javascript">

@ -90,7 +90,7 @@ gulp.task('uglify', ['build'], function(){
return uglifyFile('ethereum'); return uglifyFile('ethereum');
}); });
gulp.task('uglify', ['buildDev'], function(){ gulp.task('uglifyDev', ['buildDev'], function(){
return uglifyFile('ethereum'); return uglifyFile('ethereum');
}); });
@ -99,6 +99,6 @@ gulp.task('watch', function() {
}); });
gulp.task('release', ['bower', 'lint', 'build', 'uglify']); gulp.task('release', ['bower', 'lint', 'build', 'uglify']);
gulp.task('dev', ['bower', 'lint', 'buildDev', 'uglify']); gulp.task('dev', ['bower', 'lint', 'buildDev', 'uglifyDev']);
gulp.task('default', ['dev']); gulp.task('default', ['dev']);

@ -26,6 +26,8 @@ if (process.env.NODE_ENV !== 'build') {
var web3 = require('./web3'); // jshint ignore:line var web3 = require('./web3'); // jshint ignore:line
} }
var BigNumber = require('bignumber.js');
// TODO: make these be actually accurate instead of falling back onto JS's doubles. // TODO: make these be actually accurate instead of falling back onto JS's doubles.
var hexToDec = function (hex) { var hexToDec = function (hex) {
return parseInt(hex, 16).toString(); return parseInt(hex, 16).toString();
@ -84,24 +86,26 @@ var namedType = function (name) {
var setupInputTypes = function () { var setupInputTypes = function () {
/// Formats input value to byte representation of int /// Formats input value to byte representation of int
/// If value is negative, return it's two's complement
/// @returns right-aligned byte representation of int /// @returns right-aligned byte representation of int
var formatInt = function (value) { var formatInt = function (value) {
var padding = 32 * 2; var padding = 32 * 2;
if (typeof value === 'number') { if (value instanceof BigNumber) {
if (value < 0) { if (value.lessThan(0))
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16);
// two's complement else
// TODO: fix big numbers support
value = ((value) >>> 0).toString(16);
return padLeft(value, padding, 'f');
}
value = value.toString(16); value = value.toString(16);
}
else if (typeof value === 'number') {
if (value < 0)
value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16);
else
value = new BigNumber(value).toString(16);
} }
else if (value.indexOf('0x') === 0) else if (value.indexOf('0x') === 0)
value = value.substr(2); value = value.substr(2);
else if (typeof value === 'string') else if (typeof value === 'string')
value = value.toHex(value); value = new BigNumber(value).toString(16);
else else
value = (+value).toString(16); value = (+value).toString(16);
return padLeft(value, padding); return padLeft(value, padding);

@ -10,7 +10,8 @@
"dependencies": { "dependencies": {
"es6-promise": "*", "es6-promise": "*",
"ws": "*", "ws": "*",
"xmlhttprequest": "*" "xmlhttprequest": "*",
"bignumber.js": ">=2.0.0"
}, },
"devDependencies": { "devDependencies": {
"bower": ">=1.3.0", "bower": ">=1.3.0",