From 607408ceb0168d40c16c4baa9b531055df85eba6 Mon Sep 17 00:00:00 2001 From: Stephen Pair Date: Sun, 7 Jul 2013 15:15:05 -0400 Subject: [PATCH] add more constructor variations --- BitcoinAddress.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/BitcoinAddress.js b/BitcoinAddress.js index d15a01464..88d472a48 100644 --- a/BitcoinAddress.js +++ b/BitcoinAddress.js @@ -3,9 +3,26 @@ require('classtool'); function ClassSpec(b) { var base58 = b.base58 || require('base58-native').base58Check; - function BitcoinAddress(data, encoding) { - this.data = data; - this.__proto__ = encodings[encoding || 'base58']; + // Constructor. Takes the following forms: + // new BitcoinAddress(); + // new BitcoinAddress() + // new BitcoinAddress(<21-byte-buffer>) + // new BitcoinAddress(, ) + // new BitcoinAddress(, <20-byte-hash>) + function BitcoinAddress(arg1, arg2) { + if(typeof arg1 == 'number') { + this.data = new Buffer(21); + this.__proto__ = encodings['binary']; + this.version(arg1); + this.hash(arg2); + } else { + this.data = arg1 || new Buffer(21); + if(!arg2 && (typeof arg1 == 'string')) { + this.__proto__ = encodings['base58']; + } else { + this.__proto__ = encodings[arg2 || 'binary']; + } + } }; // get or set the bitcoin address version (the first byte of the address)