add ability to get and set the hash of a BitcoinAddress

This commit is contained in:
Stephen Pair 2013-07-07 14:46:59 -04:00
parent 16ce63ca80
commit 47cb4c330c
1 changed files with 19 additions and 5 deletions

View File

@ -8,18 +8,24 @@ function ClassSpec(b) {
this.__proto__ = encodings[encoding || 'base58'];
};
// return the bitcoin address version (the first byte of the address)
// get or set the bitcoin address version (the first byte of the address)
BitcoinAddress.prototype.version = function(num) {
if(num || (num === 0)) {
var oldEncoding = this.encoding();
this.encoding('binary');
this.data.writeUInt8(num, 0);
this.encoding(oldEncoding);
this.doAsBinary(function() {this.data.writeUInt8(num, 0);});
return num;
}
return this.as('binary').readUInt8(0);
};
// get or set the hash data (as a Buffer object)
BitcoinAddress.prototype.hash = function(data) {
if(data) {
this.doAsBinary(function() {data.copy(this.data,1);});
return data;
}
return this.as('binary').slice(1);
};
// get or set the encoding used (transforms data)
BitcoinAddress.prototype.encoding = function(encoding) {
if(encoding && (encoding != this._encoding)) {
@ -50,6 +56,14 @@ function ClassSpec(b) {
return this.as('base58');
};
// utility
BitcoinAddress.prototype.doAsBinary = function(callback) {
var oldEncoding = this.encoding();
this.encoding('binary');
callback.apply(this);
this.encoding(oldEncoding);
};
// Setup support for various address encodings. The object for
// each encoding inherits from the BitcoinAddress prototype. This
// allows any encoding to override any method...changing the encoding