Browse Source

Merge pull request #379 from ryanxcharles/feature/address-throw-error

throw error when using invalid length hash in Address constructor
patch-2
Ryan X. Charles 11 years ago
parent
commit
83aada8648
  1. 8
      browser/bundle.js
  2. 4
      lib/Address.js
  3. 22
      test/test.Address.js

8
browser/bundle.js

@ -66,11 +66,11 @@ module.exports=require('4itQ50');
module.exports=require('f08cvL');
},{}],"f08cvL":[function(require,module,exports){
MSG={TX:1,BLOCK:2,FILTERED_BLOCK:3},MSG.to_str=function(t){switch(t){case MSG.TX:return"transaction";case MSG.BLOCK:return"block";case MSG.FILTERED_BLOCK:return"filtered block";default:return"unknown"}},exports.MSG=MSG;
},{}],"G+CcXD":[function(require,module,exports){
(function(r){"use strict";function e(){e.super(this,arguments)}var i=require("soop").imports(),t=i.coinUtil||require("../util"),n=i.parent||require("../util/VersionedData"),s=i.networks||require("../networks"),o=i.Script||require("./Script");e.parent=n,n.applyEncodingsTo(e),e.fromPubKey=function(r,i){if(i||(i="livenet"),33!==r.length&&65!==r.length)throw new Error("Invalid public key");var n=s[i].addressVersion,o=t.sha256ripe160(r);return new e(n,o)},e.fromKey=function(r,i){return e.fromPubKey(r.public,i)},e.fromPubKeys=function(r,i,t,n){t||(t="livenet");for(var s in i){var a=i[s];if(33!=a.length&&65!=a.length)throw new Error("Invalid public key")}var u=o.createMultisig(r,i,n);return e.fromScript(u,t)},e.fromScript=function(i,n){n||(n="livenet"),"string"==typeof i&&(i=new o(new r(i,"hex")));var a=s[n].P2SHVersion,u=i.getBuffer(),f=t.sha256ripe160(u);return new e(a,f)},e.fromScriptPubKey=function(i,n){"string"==typeof i&&(i=new o(new r(i,"hex"))),n||(n="livenet");var a,u=[],f=i.capture();if(f){var p=i.classify();switch(p){case o.TX_PUBKEY:f[0]=t.sha256ripe160(f[0]),a=s[n].addressVersion;break;case o.TX_PUBKEYHASH:a=s[n].addressVersion;break;case o.TX_MULTISIG:a=s[n].addressVersion;for(var c in f)f[c]=t.sha256ripe160(f[c]);break;case o.TX_SCRIPTHASH:a=s[n].P2SHVersion}for(var c in f)u.push(new e(a,f[c]))}return u},e.prototype.validate=function(){if(this.doAsBinary(function(){if(e.super(this,"validate",arguments),21!==this.data.length)throw new Error("invalid data length")}),"undefined"==typeof this.network())throw new Error("invalid network")},e.prototype.isValid=function(){var r=e.super(this,"isValid",arguments);return r},e.prototype.network=function(){var r,e=this.version(),i=s.livenet,t=s.testnet;return e===i.addressVersion||e===i.P2SHVersion?r=i:(e===t.addressVersion||e===t.P2SHVersion)&&(r=t),r},e.prototype.isScript=function(){return this.isValid()&&this.version()===this.network().P2SHVersion},module.exports=require("soop")(e)}).call(this,require("buffer").Buffer);
},{"../networks":"ULNIu2","../util":143,"../util/VersionedData":"QLzNQg","./Script":"hQ0t76","buffer":85,"soop":129}],"./lib/Address":[function(require,module,exports){
},{}],"./lib/Address":[function(require,module,exports){
module.exports=require('G+CcXD');
},{}],"./lib/Armory":[function(require,module,exports){
},{}],"G+CcXD":[function(require,module,exports){
(function(r){"use strict";function e(r,i){if(i&&i.length&&20!=i.length)throw new Error("Hash must be 20 bytes");e.super(this,arguments)}var i=require("soop").imports(),t=i.coinUtil||require("../util"),n=i.parent||require("../util/VersionedData"),s=i.networks||require("../networks"),o=i.Script||require("./Script");e.parent=n,n.applyEncodingsTo(e),e.fromPubKey=function(r,i){if(i||(i="livenet"),33!==r.length&&65!==r.length)throw new Error("Invalid public key");var n=s[i].addressVersion,o=t.sha256ripe160(r);return new e(n,o)},e.fromKey=function(r,i){return e.fromPubKey(r.public,i)},e.fromPubKeys=function(r,i,t,n){t||(t="livenet");for(var s in i){var a=i[s];if(33!=a.length&&65!=a.length)throw new Error("Invalid public key")}var u=o.createMultisig(r,i,n);return e.fromScript(u,t)},e.fromScript=function(i,n){n||(n="livenet"),"string"==typeof i&&(i=new o(new r(i,"hex")));var a=s[n].P2SHVersion,u=i.getBuffer(),f=t.sha256ripe160(u);return new e(a,f)},e.fromScriptPubKey=function(i,n){"string"==typeof i&&(i=new o(new r(i,"hex"))),n||(n="livenet");var a,u=[],f=i.capture();if(f){var p=i.classify();switch(p){case o.TX_PUBKEY:f[0]=t.sha256ripe160(f[0]),a=s[n].addressVersion;break;case o.TX_PUBKEYHASH:a=s[n].addressVersion;break;case o.TX_MULTISIG:a=s[n].addressVersion;for(var l in f)f[l]=t.sha256ripe160(f[l]);break;case o.TX_SCRIPTHASH:a=s[n].P2SHVersion}for(var l in f)u.push(new e(a,f[l]))}return u},e.prototype.validate=function(){if(this.doAsBinary(function(){if(e.super(this,"validate",arguments),21!==this.data.length)throw new Error("invalid data length")}),"undefined"==typeof this.network())throw new Error("invalid network")},e.prototype.isValid=function(){var r=e.super(this,"isValid",arguments);return r},e.prototype.network=function(){var r,e=this.version(),i=s.livenet,t=s.testnet;return e===i.addressVersion||e===i.P2SHVersion?r=i:(e===t.addressVersion||e===t.P2SHVersion)&&(r=t),r},e.prototype.isScript=function(){return this.isValid()&&this.version()===this.network().P2SHVersion},module.exports=require("soop")(e)}).call(this,require("buffer").Buffer);
},{"../networks":"ULNIu2","../util":143,"../util/VersionedData":"QLzNQg","./Script":"hQ0t76","buffer":85,"soop":129}],"./lib/Armory":[function(require,module,exports){
module.exports=require('YL/05i');
},{}],"YL/05i":[function(require,module,exports){
(function(e){function r(r,n){this.chaincode=new e(r,"hex"),this.pubkey=new e(n,"hex")}function n(e){for(var r="0123456789abcdef",n="asdfghjkwertuion",t="",i=0;i<e.length;i++)t+=r.charAt(n.indexOf(e.charAt(i)));return t}var t=require("./Point"),i=require("./Key"),o=require("../util").sha256,c=require("../util").twoSha256;r.prototype.generatePubKey=function(){for(var e=this.pubkey,r=this.chaincode,n=c(e),i=0;32>i;i++)n[i]^=r[i];var o=t.fromUncompressedPubKey(e);o=t.multiply(o,n);var u=o.toUncompressedPubKey();return u},r.prototype.next=function(){var e=this.generatePubKey();return new r(this.chaincode,e)},r.fromMasterPublicKey=function(e){var n=e.substr(0,130),t=e.substr(130,e.length);return new r(t,n)},r.decodeSeed=function(t){for(var i=t.trim().split("\n"),o=[],c=0;c<i.length;c++){var u=i[c].replace(" ",""),a=new e(n(u),"hex"),h=a.slice(0,16);o.push(h)}var s=e.concat([o[0],o[1]]),f=4==o.length?e.concat([o[2],o[3]]):r.deriveChaincode(s);return{privKey:s,chainCode:f}},r.fromSeed=function(e){var n=r.decodeSeed(e),t=new i;return t.private=n.privKey,t.compressed=!1,t.regenerateSync(),new r(n.chainCode,t.public)},r.deriveChaincode=function(r){for(var n="Derive Chaincode from Root Key",t=c(r),i=[],u=[],a=0;a<t.length;a++)i.push(92^t[a]),u.push(54^t[a]);i=new e(i),u=new e(u);var h=new e(n,"utf8"),s=o(e.concat([u,h])),f=o(e.concat([i,s]));return f},module.exports=r}).call(this,require("buffer").Buffer);

4
lib/Address.js

@ -36,7 +36,9 @@ var parent = imports.parent || require('../util/VersionedData');
var networks = imports.networks || require('../networks');
var Script = imports.Script || require('./Script');
function Address() {
function Address(version, hash) {
if (hash && hash.length && hash.length != 20)
throw new Error('Hash must be 20 bytes');
Address.super(this, arguments);
}

22
test/test.Address.js

@ -4,6 +4,7 @@ var chai = chai || require('chai');
var bitcore = bitcore || require('../bitcore');
var should = chai.should();
var expect = chai.expect;
var Address = bitcore.Address;
var Key = bitcore.Key;
@ -79,6 +80,27 @@ describe('Address', function() {
new Address('2NBSBcf2KfjPEEqVusmrWdmUeNHRiUTS3Li').isScript().should.equal(true);
});
describe('#Address constructor', function() {
it('should produce a valid address from a hash', function() {
var privkey = bitcore.util.sha256('test');
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var hash = bitcore.util.sha256ripe160(key.public);
var addr = new bitcore.Address(0, hash);
addr.isValid().should.equal(true);
});
it('should throw an error if you try to use a public key instead of a hash', function() {
var privkey = bitcore.util.sha256('test');
var key = new bitcore.Key();
key.private = privkey;
key.regenerateSync();
var f = function() {new bitcore.Address(0, key.public);};
expect(f).to.throw(Error);
});
});
describe('#fromPubKey', function() {
it('should make pubkeyhash address from an uncompressed public key', function() {
var pubkey = new Buffer('04fa05ce8b25010cb6e17a30e0b66668bf083c40687547748ec330ee77adf53a42abd3d26148cbacfcf79c907ddefeb2c37f8bebc0a695ba79d634449d871de218', 'hex');

Loading…
Cancel
Save