bitcoinjs-lib/src/address.js

64 lines
1.2 KiB
JavaScript
Raw Normal View History

var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var conv = require('./convert');
var address_types = {
prod: 0,
testnet: 111
};
var p2sh_types = {
prod: 5,
testnet: 196
};
var Address = function (bytes) {
if (typeof bytes === 'string') {
this.hash = base58.decode(bytes);
this.version = this.hash.version;
}
else {
this.hash = bytes;
this.version = 0x00;
}
2011-05-04 17:02:56 +01:00
};
2012-01-11 10:41:52 +01:00
/**
* Serialize this object as a standard Bitcoin address.
*
* Returns the address as a base58-encoded string in the standardized format.
*/
Address.prototype.toString = function () {
2012-01-11 02:40:45 +01:00
// Get a copy of the hash
var hash = this.hash.slice(0);
2011-05-04 17:02:56 +01:00
return base58.checkEncode(hash,this.version);
};
Address.prototype.getHashBase64 = function () {
return conv.bytesToBase64(this.hash);
2011-05-04 17:02:56 +01:00
};
Address.getVersion = function(string) {
return base58.decode(string)[0];
}
// TODO(shtylman) isValid?
Address.validate = function(string) {
try {
base58.checkDecode(string);
} catch (e) {
return false;
}
return true;
2011-05-04 17:02:56 +01:00
};
2012-01-11 10:41:52 +01:00
/**
* Parse a Bitcoin address contained in a string.
*/
Address.decodeString = function (string) {
return base58.checkDecode(string);
2011-05-04 17:02:56 +01:00
};
module.exports = Address;