From 47cb4c330c4456210e40d00bdbd0b962266d8b44 Mon Sep 17 00:00:00 2001 From: Stephen Pair Date: Sun, 7 Jul 2013 14:46:59 -0400 Subject: [PATCH] add ability to get and set the hash of a BitcoinAddress --- BitcoinAddress.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/BitcoinAddress.js b/BitcoinAddress.js index b016570..d15a014 100644 --- a/BitcoinAddress.js +++ b/BitcoinAddress.js @@ -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