blob: 0ec3de3c7d59e5cc8b703eec61e24cf8eb6ab9f4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
BigInteger.valueOf = nbv;
BigInteger.prototype.toByteArrayUnsigned = function () {
var ba = this.toByteArray();
if (ba.length) {
if (ba[0] == 0)
ba = ba.slice(1);
return ba.map(function (v) {
return (v < 0) ? v + 256 : v;
});
} else
return ba;
};
var Bitcoin = {};
(function () {
var B58 = Bitcoin.Base58 = {
alphabet: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
base: BigInteger.valueOf(58),
decode: function (input) {
bi = BigInteger.valueOf(0);
var leadingZerosNum = 0;
for (var i = input.length - 1; i >= 0; i--) {
var alphaIndex = B58.alphabet.indexOf(input[i]);
if (alphaIndex < 0) {
throw "Invalid character";
}
bi = bi.add(BigInteger.valueOf(alphaIndex)
.multiply(B58.base.pow(input.length - 1 - i)));
if (input[i] == "1") leadingZerosNum++;
else leadingZerosNum = 0;
}
var bytes = bi.toByteArrayUnsigned();
while (leadingZerosNum-- > 0) bytes.unshift(0);
return bytes;
}
};
})();
Bitcoin.Address = function (bytes) {
if ("string" == typeof bytes)
bytes = Bitcoin.Address.decodeString(bytes);
this.hash = bytes;
this.version = Bitcoin.Address.networkVersion;
};
Bitcoin.Address.networkVersion = 0x00; // mainnet
Bitcoin.Address.decodeString = function (string) {
var bytes = Bitcoin.Base58.decode(string);
var hash = bytes.slice(0, 21);
var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
if (checksum[0] != bytes[21] ||
checksum[1] != bytes[22] ||
checksum[2] != bytes[23] ||
checksum[3] != bytes[24])
throw "Checksum validation failed!";
var version = hash.shift();
if (version != 0)
throw "Version " + version + " not supported!";
return hash;
};
function check_address(address) {
try {
Bitcoin.Address(address);
return true;
} catch (err) {
return false;
}
}
|