From 71f992138bc9f88316887150a1558c96e45006a2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 01:40:56 -0300 Subject: [PATCH] add check in constructor of Address --- lib/Address.js | 2 +- test/test.Address.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Address.js b/lib/Address.js index 0eb383a..6dafa0c 100644 --- a/lib/Address.js +++ b/lib/Address.js @@ -38,7 +38,7 @@ var Script = require('./Script'); var util = require('util'); function Address(version, hash) { - if (hash && hash.length && hash.length != 20) + if (hash && hash.length && (!Buffer.isBuffer(hash) || hash.length != 20)) throw new Error('Hash must be 20 bytes'); Address.super_.call(this, version, hash); } diff --git a/test/test.Address.js b/test/test.Address.js index e130b4d..5b47929 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -107,6 +107,21 @@ describe('Address', function() { }); }); + describe('constructor, 2 params', function() { + it('should make an address from a version, hash', function() { + var hash = new Buffer('1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex'); + var addr = new Address(0, hash); + addr.toString().should.equal('13SE7uKmnQwGA8X1A8WcZnX2ceQRDEzsAd'); + }); + it('should fail with param version, string', function() { + var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0'; + ( function (){ + var addr = new Address(0, hash); + }).should.throw(); + }); + }); + + describe('#fromPubKey', function() { it('should make pubkeyhash address from an uncompressed public key', function() { var pubkey = new Buffer('04fa05ce8b25010cb6e17a30e0b66668bf083c40687547748ec330ee77adf53a42abd3d26148cbacfcf79c907ddefeb2c37f8bebc0a695ba79d634449d871de218', 'hex');