Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ultraviolet
GitHub Repository: ultraviolet/bitaddress.org
Path: blob/master/src/bitcoinjs-lib.address.js
248 views
1
//https://raw.github.com/bitcoinjs/bitcoinjs-lib/09e8c6e184d6501a0c2c59d73ca64db5c0d3eb95/src/address.js
2
Bitcoin.Address = function (bytes) {
3
if ("string" == typeof bytes) {
4
bytes = Bitcoin.Address.decodeString(bytes);
5
}
6
this.hash = bytes;
7
this.version = Bitcoin.Address.networkVersion;
8
};
9
10
Bitcoin.Address.networkVersion = 0x00; // mainnet
11
12
/**
13
* Serialize this object as a standard Bitcoin address.
14
*
15
* Returns the address as a base58-encoded string in the standardized format.
16
*/
17
Bitcoin.Address.prototype.toString = function () {
18
// Get a copy of the hash
19
var hash = this.hash.slice(0);
20
21
// Version
22
hash.unshift(this.version);
23
var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
24
var bytes = hash.concat(checksum.slice(0, 4));
25
return Bitcoin.Base58.encode(bytes);
26
};
27
28
Bitcoin.Address.prototype.getHashBase64 = function () {
29
return Crypto.util.bytesToBase64(this.hash);
30
};
31
32
/**
33
* Parse a Bitcoin address contained in a string.
34
*/
35
Bitcoin.Address.decodeString = function (string) {
36
var bytes = Bitcoin.Base58.decode(string);
37
var hash = bytes.slice(0, 21);
38
var checksum = Crypto.SHA256(Crypto.SHA256(hash, { asBytes: true }), { asBytes: true });
39
40
if (checksum[0] != bytes[21] ||
41
checksum[1] != bytes[22] ||
42
checksum[2] != bytes[23] ||
43
checksum[3] != bytes[24]) {
44
throw "Checksum validation failed!";
45
}
46
47
var version = hash.shift();
48
49
if (version != 0) {
50
throw "Version " + version + " not supported!";
51
}
52
53
return hash;
54
};
55