add ability to get and set the hash of a BitcoinAddress
This commit is contained in:
parent
16ce63ca80
commit
47cb4c330c
|
@ -8,18 +8,24 @@ function ClassSpec(b) {
|
||||||
this.__proto__ = encodings[encoding || 'base58'];
|
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) {
|
BitcoinAddress.prototype.version = function(num) {
|
||||||
if(num || (num === 0)) {
|
if(num || (num === 0)) {
|
||||||
var oldEncoding = this.encoding();
|
this.doAsBinary(function() {this.data.writeUInt8(num, 0);});
|
||||||
this.encoding('binary');
|
|
||||||
this.data.writeUInt8(num, 0);
|
|
||||||
this.encoding(oldEncoding);
|
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
return this.as('binary').readUInt8(0);
|
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)
|
// get or set the encoding used (transforms data)
|
||||||
BitcoinAddress.prototype.encoding = function(encoding) {
|
BitcoinAddress.prototype.encoding = function(encoding) {
|
||||||
if(encoding && (encoding != this._encoding)) {
|
if(encoding && (encoding != this._encoding)) {
|
||||||
|
@ -50,6 +56,14 @@ function ClassSpec(b) {
|
||||||
return this.as('base58');
|
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
|
// Setup support for various address encodings. The object for
|
||||||
// each encoding inherits from the BitcoinAddress prototype. This
|
// each encoding inherits from the BitcoinAddress prototype. This
|
||||||
// allows any encoding to override any method...changing the encoding
|
// allows any encoding to override any method...changing the encoding
|
||||||
|
|
Loading…
Reference in New Issue