Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/bitcoinjs-lib.util.js
248 views
1
//https://raw.github.com/bitcoinjs/bitcoinjs-lib/09e8c6e184d6501a0c2c59d73ca64db5c0d3eb95/src/util.js
2
// Bitcoin utility functions
3
Bitcoin.Util = {
4
/**
5
* Cross-browser compatibility version of Array.isArray.
6
*/
7
isArray: Array.isArray || function (o) {
8
return Object.prototype.toString.call(o) === '[object Array]';
9
},
10
/**
11
* Create an array of a certain length filled with a specific value.
12
*/
13
makeFilledArray: function (len, val) {
14
var array = [];
15
var i = 0;
16
while (i < len) {
17
array[i++] = val;
18
}
19
return array;
20
},
21
/**
22
* Turn an integer into a "var_int".
23
*
24
* "var_int" is a variable length integer used by Bitcoin's binary format.
25
*
26
* Returns a byte array.
27
*/
28
numToVarInt: function (i) {
29
if (i < 0xfd) {
30
// unsigned char
31
return [i];
32
} else if (i <= 1 << 16) {
33
// unsigned short (LE)
34
return [0xfd, i >>> 8, i & 255];
35
} else if (i <= 1 << 32) {
36
// unsigned int (LE)
37
return [0xfe].concat(Crypto.util.wordsToBytes([i]));
38
} else {
39
// unsigned long long (LE)
40
return [0xff].concat(Crypto.util.wordsToBytes([i >>> 32, i]));
41
}
42
},
43
/**
44
* Parse a Bitcoin value byte array, returning a BigInteger.
45
*/
46
valueToBigInt: function (valueBuffer) {
47
if (valueBuffer instanceof BigInteger) return valueBuffer;
48
49
// Prepend zero byte to prevent interpretation as negative integer
50
return BigInteger.fromByteArrayUnsigned(valueBuffer);
51
},
52
/**
53
* Format a Bitcoin value as a string.
54
*
55
* Takes a BigInteger or byte-array and returns that amount of Bitcoins in a
56
* nice standard formatting.
57
*
58
* Examples:
59
* 12.3555
60
* 0.1234
61
* 900.99998888
62
* 34.00
63
*/
64
formatValue: function (valueBuffer) {
65
var value = this.valueToBigInt(valueBuffer).toString();
66
var integerPart = value.length > 8 ? value.substr(0, value.length - 8) : '0';
67
var decimalPart = value.length > 8 ? value.substr(value.length - 8) : value;
68
while (decimalPart.length < 8) decimalPart = "0" + decimalPart;
69
decimalPart = decimalPart.replace(/0*$/, '');
70
while (decimalPart.length < 2) decimalPart += "0";
71
return integerPart + "." + decimalPart;
72
},
73
/**
74
* Parse a floating point string as a Bitcoin value.
75
*
76
* Keep in mind that parsing user input is messy. You should always display
77
* the parsed value back to the user to make sure we understood his input
78
* correctly.
79
*/
80
parseValue: function (valueString) {
81
// TODO: Detect other number formats (e.g. comma as decimal separator)
82
var valueComp = valueString.split('.');
83
var integralPart = valueComp[0];
84
var fractionalPart = valueComp[1] || "0";
85
while (fractionalPart.length < 8) fractionalPart += "0";
86
fractionalPart = fractionalPart.replace(/^0+/g, '');
87
var value = BigInteger.valueOf(parseInt(integralPart));
88
value = value.multiply(BigInteger.valueOf(100000000));
89
value = value.add(BigInteger.valueOf(parseInt(fractionalPart)));
90
return value;
91
},
92
/**
93
* Calculate RIPEMD160(SHA256(data)).
94
*
95
* Takes an arbitrary byte array as inputs and returns the hash as a byte
96
* array.
97
*/
98
sha256ripe160: function (data) {
99
return Crypto.RIPEMD160(Crypto.SHA256(data, { asBytes: true }), { asBytes: true });
100
},
101
// double sha256
102
dsha256: function (data) {
103
return Crypto.SHA256(Crypto.SHA256(data, { asBytes: true }), { asBytes: true });
104
},
105
// duck typing method
106
hasMethods: function(obj /*, method list as strings */){
107
var i = 1, methodName;
108
while((methodName = arguments[i++])){
109
if(typeof obj[methodName] != 'function') {
110
return false;
111
}
112
}
113
return true;
114
}
115
};
116