|
|
@ -10,18 +10,6 @@ module.exports = { |
|
|
|
return Object.prototype.toString.call(o) === '[object Array]'; |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Create an array of a certain length filled with a specific value. |
|
|
|
*/ |
|
|
|
makeFilledArray: function (len, val) |
|
|
|
{ |
|
|
|
var array = []; |
|
|
|
var i = 0; |
|
|
|
while (i < len) { |
|
|
|
array[i++] = val; |
|
|
|
} |
|
|
|
return array; |
|
|
|
}, |
|
|
|
/** |
|
|
|
* Create a byte array representing a number with the given length |
|
|
|
*/ |
|
|
@ -53,65 +41,6 @@ module.exports = { |
|
|
|
else return [253].concat(m.numToBytes(num,8)); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Parse a Bitcoin value byte array, returning a BigInteger. |
|
|
|
*/ |
|
|
|
valueToBigInt: function (valueBuffer) |
|
|
|
{ |
|
|
|
if (valueBuffer instanceof BigInteger) return valueBuffer; |
|
|
|
|
|
|
|
// Prepend zero byte to prevent interpretation as negative integer
|
|
|
|
return BigInteger.fromByteArrayUnsigned(valueBuffer); |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Format a Bitcoin value as a string. |
|
|
|
* |
|
|
|
* Takes a Number, BigInteger or byte-array and returns that amount of Bitcoins in a |
|
|
|
* nice standard formatting. |
|
|
|
* |
|
|
|
* Examples: |
|
|
|
* 12.3555 |
|
|
|
* 0.1234 |
|
|
|
* 900.99998888 |
|
|
|
* 34.00 |
|
|
|
*/ |
|
|
|
formatValue: function (value) { |
|
|
|
if (typeof value == 'number') { |
|
|
|
value = value/100000000; |
|
|
|
// at least two decimal places
|
|
|
|
return Math.floor(value*10)==value*10 ? value.toFixed(2) : ''+value; |
|
|
|
} else { |
|
|
|
value = module.exports.valueToBigInt(value).toString(); |
|
|
|
var integerPart = value.length > 8 ? value.substr(0, value.length-8) : '0'; |
|
|
|
var decimalPart = value.length > 8 ? value.substr(value.length-8) : value; |
|
|
|
while (decimalPart.length < 8) decimalPart = "0"+decimalPart; |
|
|
|
decimalPart = decimalPart.replace(/0*$/, ''); |
|
|
|
while (decimalPart.length < 2) decimalPart += "0"; |
|
|
|
return integerPart+"."+decimalPart; |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
/** |
|
|
|
* Parse a floating point string as a Bitcoin value. |
|
|
|
* |
|
|
|
* Keep in mind that parsing user input is messy. You should always display |
|
|
|
* the parsed value back to the user to make sure we understood his input |
|
|
|
* correctly. |
|
|
|
*/ |
|
|
|
parseValue: function (valueString) { |
|
|
|
// TODO: Detect other number formats (e.g. comma as decimal separator)
|
|
|
|
var valueComp = valueString.split('.'); |
|
|
|
var integralPart = valueComp[0]; |
|
|
|
var fractionalPart = valueComp[1] || "0"; |
|
|
|
while (fractionalPart.length < 8) fractionalPart += "0"; |
|
|
|
fractionalPart = fractionalPart.replace(/^0+/g, ''); |
|
|
|
var value = BigInteger.valueOf(parseInt(integralPart)); |
|
|
|
value = value.multiply(BigInteger.valueOf(100000000)); |
|
|
|
value = value.add(BigInteger.valueOf(parseInt(fractionalPart))); |
|
|
|
return value; |
|
|
|
}, |
|
|
|
|
|
|
|
bytesToWords: function (bytes) { |
|
|
|
var words = []; |
|
|
|
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) { |
|
|
|