From c2e5a14eed9ab92569d746d56fa680205bd8e60a Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 11 Jun 2014 18:13:39 -0700 Subject: [PATCH] throw error when using invalid length hash in Address I have often made the error of using a public key rather than the hash of the public key when creating an address, leading to invalid addresses. I'm sure I'm not the only one. This commit follows the principle of "fail early, fail often" and simply throws an error if you try to insert something other than 20 bytes long when creating an address, which would be the case when using a public key. This way that common mistake should be reduced. --- browser/bundle.js | 18 +++++++++--------- lib/Address.js | 4 +++- test/test.Address.js | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/browser/bundle.js b/browser/bundle.js index f2502d6..6f49f97 100644 --- a/browser/bundle.js +++ b/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;ii;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;cs;s++)i.push(new t(e.pubkeys[s],"hex"));return n.createMultisig(e.nreq,i)},e._scriptForOut=function(t){var e;if(t.address)e=this.scriptForAddress(t.address);else{if(!(t.pubkeys||t.nreq||t.nreq>1))throw new Error("unknown out type");e=this._scriptForPubkeys(t)}return e},e.infoForP2sh=function(t,e){var r=this._scriptForOut(t),n=s.sha256ripe160(r.getBuffer()),a="testnet"===e?o.testnet.P2SHVersion:o.livenet.P2SHVersion,u=new i(a,n),h=u.as("base58");return{script:r,scriptBufHex:r.getBuffer().toString("hex"),hash:n,address:h}},e.prototype.setUnspent=function(t){return this.utxos=t,this},e.prototype._setInputMap=function(){for(var e=[],r=this.selectedUtxos.length,i=0;r>i;i++){var s=this.selectedUtxos[i],a=new t(s.scriptPubKey,"hex"),u=new n(a),o=u.classify();if(o===n.TX_UNKNOWN)throw new Error("unkown output type at:"+i+" Type:"+u.getRawOutType());e.push({address:s.address,scriptPubKey:u,scriptType:o,i:i})}return this.inputMap=e,this},e.prototype.getSelectedUnspent=function(){return this.selectedUtxos},e.prototype._selectUnspent=function(t){if(!this.utxos||!this.utxos.length)throw new Error("unspent not set");var e=[6,1];this.spendUnconfirmed&&e.push(0);var r=[],i=a(0),n=!1,u=null,o=this.utxos.length;do{for(var h=e.shift(),p=0;o>p;p++){var c=this.utxos[p],f=c.confirmations||0;if(!(h>f||u&&f>=u)){var d=c.amountSat||s.parseValue(c.amount);if(i=i.add(d),r.push(c),i.cmp(t)>=0){n=!0;break}}}u=h}while(!n&&e.length);if(!n)throw new Error("no enough unspent to fulfill totalNeededAmount [SAT]:"+t);return this.selectedUtxos=r,this._setInputMap(),this},e.prototype._setInputs=function(e){var r=this.selectedUtxos,i=r.length,n=a(0);e.ins=[];for(var o=0;i>o;o++){n=n.add(s.parseValue(r[o].amount));var h={};h.s=s.EMPTY_BUFFER,h.q=4294967295;var p=new t(r[o].txid,"hex"),c=u.reverse(p),f=parseInt(r[o].vout),d=new t(4);d.writeUInt32LE(f,0),h.o=t.concat([c,d]),e.ins.push(h)}return this.valueInSat=n,this},e.prototype._setFee=function(t){if("undefined"==typeof this.valueOutSat)throw new Error("valueOutSat undefined");var e=this.valueOutSat.add(t);if(this.valueInSat.cmp(e)<0){var r=this.valueInSat.toString(),i=e.toString();throw new Error("transaction input amount is less than outputs: "+r+" < "+i+" [SAT]")}return this.feeSat=t,this},e.prototype._setRemainder=function(t,r){if("undefined"==typeof this.valueInSat||"undefined"==typeof this.valueOutSat)throw new Error("valueInSat / valueOutSat undefined");var i=this.valueInSat.sub(this.valueOutSat).sub(this.feeSat),n=t.outs.length;if(this.remainderSat=a(0),n>r&&t.outs.pop(),i.cmp(0)>0){var u=this.remainderOut||this.selectedUtxos[0],o=s.bigIntToValue(i),h=e._scriptForOut(u),p={v:o,s:h.getBuffer()};t.outs.push(p),this.remainderSat=i}return this},e.prototype._setFeeAndRemainder=function(t){var e,r=500,i=t.outs.length;do{e=parseInt(r/1e3)+1;var n=this.givenFeeSat?this.givenFeeSat:e*g,s=this.valueOutSat.add(n);this._selectUnspent(s)._setInputs(t)._setFee(n)._setRemainder(t,i),r=new d(t).getSize()}while(r>1e3*(e+1));return this},e.prototype.setOutputs=function(t){var r=a(0),i={};i.version=1,i.lock_time=this.lockTime||0,i.ins=[],i.outs=[];for(var n=t.length,u=0;n>u;u++){var o=t[u].amountSat||s.parseValue(t[u].amount),h=s.bigIntToValue(o),p=e._scriptForOut(t[u]),c={v:h,s:p.getBuffer()};i.outs.push(c);var f=t[u].amountSat||s.parseValue(t[u].amount);r=r.add(f)}return this.valueOutSat=r,this._setFeeAndRemainder(i),this.tx=new d(i),this},e._mapKeys=function(t){for(var e,r={},i=t.length,n=0;i>n;n++){var s=t[n];if("string"==typeof s){var a=new p(s);e=new h({network:a.network()}),e.fromObj({priv:s})}else{if(!(s instanceof h))throw new Error("argument must be an array of strings (WIF format) or WalletKey objects");e=s}r[e.storeObj().addr]=e}return r},e._signHashAndVerify=function(t,e){var r,i=10;do r=t.privKey.signSync(e);while(t.privKey.verifySignatureSync(e,r)===!1&&i--);if(0>i)throw new Error("could not sign input: verification failed");return r},e.prototype._checkTx=function(){if(!this.tx||!this.tx.ins.length||!this.tx.outs.length)throw new Error("tx is not defined")},e.prototype._multiFindKey=function(t,e){var r;return[o.livenet,o.testnet].forEach(function(n){[n.addressVersion,n.P2SHVersion].forEach(function(n){var s=new i(n,e);!r&&t[s]&&(r=t[s])})}),r},e.prototype._findWalletKey=function(t,e){var r;if(e.address)r=t[e.address];else if(e.pubKeyHash)r=this._multiFindKey(t,e.pubKeyHash);else{if(!e.pubKeyBuf)throw new Error("no infomation at input to find keys");var i=s.sha256ripe160(e.pubKeyBuf);r=this._multiFindKey(t,i)}return r},e.prototype._signPubKey=function(r,i,s){if(this.tx.ins[i.i].s.length>0)return{};var a=this._findWalletKey(r,i);if(a){var u=e._signHashAndVerify(a,s),o=new t(1);o[0]=this.signhash;var h=t.concat([u,o]),p=new n;return p.chunks.push(h),p.updateBuffer(),{inputFullySigned:!0,signaturesAdded:1,script:p.getBuffer()}}},e.prototype._signPubKeyHash=function(r,i,s){if(this.tx.ins[i.i].s.length>0)return{};var a=this._findWalletKey(r,i);if(a){var u=e._signHashAndVerify(a,s),o=new t(1);o[0]=this.signhash;var h=t.concat([u,o]),p=new n;return p.chunks.push(h),p.chunks.push(a.privKey.public),p.updateBuffer(),{inputFullySigned:!0,signaturesAdded:1,script:p.getBuffer()}}},e.prototype._chunkSignedWithKey=function(e,r,i){var n,s=new c;s.public=i;for(var a=1;a<=e.countSignatures();a++){var u=e.chunks[a],o=new t(u.slice(0,u.length-1));s.verifySignatureSync(r,o)&&(n=u)}return n},e.prototype._getSignatureOrder=function(e,r,i,n){for(var s=n.length,a=0;s>a;a++){var u=new c;if(u.public=new t(n[a],"hex"),u.verifySignatureSync(i,r))break}return a},e.prototype._getNewSignatureOrder=function(e,r,i,n){for(var s,a=1;a<=r.countSignatures();a++){var u=r.chunks[a],o=new t(u.slice(0,u.length-1));if(s=this._getSignatureOrder(e,o,i,n),s>=e)break}return e===s?-1:a-1},e.prototype._chunkIsEmpty=function(t){return 0===t||0===u.compare(t,s.EMPTY_BUFFER)},e.prototype._initMultiSig=function(t){var e=!1;return 0!==t.chunks[0]&&(t.prependOp0(),e=!0),e},e.prototype._updateMultiSig=function(r,i,n,s,a){var u=this._initMultiSig(n);if(this._chunkSignedWithKey(n,s,i.privKey.public))return null;var o=e._signHashAndVerify(i,s),h=new t(1);h[0]=this.signhash;var p=t.concat([o,h]),c=this._getNewSignatureOrder(r,n,s,a);return n.chunks.splice(c+1,0,p),n.updateBuffer(),u=!0,u?n:null},e.prototype._signMultiSig=function(t,e,r){for(var i=e.scriptPubKey.capture(),s=e.scriptPubKey.chunks[0]-80,a=i.length,u=this.tx.ins[e.i].s,o=new n(u),h=0,p=0;a>p&&o.countSignatures()a;a++){var u=this.inputMap[a],o=this.tx.hashForSignature(u.scriptPubKey,a,this.signhash),h=l[u.scriptType].call(this,s,u,o);h&&h.script&&(r.ins[a].s=h.script,h.inputFullySigned&&this.inputsSigned++,h.signaturesAdded&&(this.signaturesAdded+=h.signaturesAdded))}return this},e.prototype.setHashToScriptMap=function(t){return this.hashToScriptMap=t,this},e.prototype.isFullySigned=function(){return this.inputsSigned===this.tx.ins.length},e.prototype.build=function(){return this._checkTx(),this.tx},e.prototype.toObj=function(){var t={valueInSat:this.valueInSat.toString(),valueOutSat:this.valueOutSat.toString(),feeSat:this.feeSat.toString(),remainderSat:this.remainderSat.toString(),hashToScriptMap:this.hashToScriptMap,selectedUtxos:this.selectedUtxos,inputsSigned:this.inputsSigned,signaturesAdded:this.signaturesAdded,signhash:this.signhash,spendUnconfirmed:this.spendUnconfirmed};return this.tx&&(t.tx=this.tx.serialize().toString("hex")),t},e.fromObj=function(r){var i=new e;if(i.valueInSat=r.valueInSat.toString(),i.valueOutSat=r.valueOutSat.toString(),i.feeSat=r.feeSat.toString(),i.remainderSat=r.remainderSat.toString(),i.hashToScriptMap=r.hashToScriptMap,i.selectedUtxos=r.selectedUtxos,i.inputsSigned=r.inputsSigned,i.signaturesAdded=r.signaturesAdded,i.signhash=r.signhash,i.spendUnconfirmed=r.spendUnconfirmed,i._setInputMap(),r.tx){var n=new d;n.parse(new t(r.tx,"hex")),i.tx=n}return i},e.prototype._checkMergeability=function(t){var e=this;if(["valueInSat","valueOutSat","feeSat","remainderSat","signhash","spendUnconfirmed"].forEach(function(r){if(e[r].toString()!==t[r].toString())throw new Error("mismatch at TransactionBuilder match: "+r+": "+e[r]+" vs. "+t[r])}),e.hashToScriptMap){var r=0;if(t.hashToScriptMap||(r=1),Object.keys(e.hashToScriptMap).forEach(function(i){t.hashToScriptMap[i]||(r=1),e.hashToScriptMap[i]!==t.hashToScriptMap[i]&&(r=1)}),r)throw new Error("mismatch at TransactionBuilder hashToScriptMap")}var r=0,i=0;if(e.selectedUtxos.forEach(function(e){if(!r){var n=t.selectedUtxos[i++];n||(r=1),["address","hash","scriptPubKey","vout","amount"].forEach(function(t){e[t]!==n[t]&&(r=t)})}}),r)throw new Error("mismatch at TransactionBuilder selectedUtxos #"+i-1+" Key:"+r);if(r=0,i=0,e.inputMap.forEach(function(e){if(!r){var n=t.inputMap[i++];n||(r=1),["address","scriptType","scriptPubKey","i"].forEach(function(t){e[t].toString()!==n[t].toString()&&(r=t)})}}),r)throw new Error("mismatch at TransactionBuilder inputMap #"+i-1+" Key:"+r)},e.prototype._mergeInputSigP2sh=function(t,e,r){for(var i=this._p2shInput(t),s=new n(i.scriptBuf),a=s.capture(),u={},o=a.length,h=0;o>h;h++)this._chunkSignedWithKey(e,i.txSigHash,a[h])&&(u[a[h].toString("hex")]=1);for(var p=[],h=0;o>h;h++){var c=this._chunkSignedWithKey(r,i.txSigHash,a[h]),f=a[h].toString("hex");c&&!u[f]&&p.push({prio:h,chunk:c,pubHex:f})}for(var h in p){var d=p[h],g=this._getNewSignatureOrder(d.prio,e,i.txSigHash,a);e.chunks.splice(g+1,0,d.chunk),this.signaturesAdded++}return e.updateBuffer(),e.getBuffer()},e.prototype._mergeInputSig=function(t,e,r){if(0===u.compare(e,r))return e;var i=new n(e),s=new n(r),a=i.chunks.length,o=s.chunks.length;if(a&&o&&(2>a&&o>2||2>o&&a>2))throw new Error("TX sig types mismatch in merge");if(!a&&!o||a&&!o||!a&&o)return r;var h=this.inputMap[t],p=h.scriptPubKey.classify();if(p===n.TX_PUBKEYHASH||p===n.TX_PUBKEY)return f.debug("Merging two signed inputs type:"+h.scriptPubKey.getRawOutType()+". Signatures differs. Using the first version."),e;if(p!==n.TX_SCRIPTHASH)throw new Error("Script type:"+h.scriptPubKey.getRawOutType()+"not supported at #merge");return this._mergeInputSigP2sh(h,i,s)},e.prototype._mergeTx=function(t){var e=this.tx,r=t,i=e.ins.length;if(i!==r.ins.length)throw new Error("TX in length mismatch in merge");this.inputsSigned=0;for(var n=0;i>n;n++){var s=e.ins[n],a=r.ins[n];if(s.q!==a.q)throw new Error("TX sequence ins mismatch in merge. Input:",n);if(0!==u.compare(s.o,a.o))throw new Error("TX .o in mismatch in merge. Input:",n);s.s=this._mergeInputSig(n,s.s,a.s),e.isInputComplete(n)&&this.inputsSigned++}},e.prototype.merge=function(t){if(this._checkMergeability(t),this.tx||t.tx){if(this.tx.getNormalizedHash().toString("hex")!==t.tx.getNormalizedHash().toString("hex"))throw new Error("mismatch at TransactionBuilder NTXID");this._mergeTx(t.tx)}},module.exports=require("soop")(e)}).call(this,require("buffer").Buffer); +(function(t){"use strict";function e(t){return t=t||{},this.lockTime=t.lockTime||0,this.spendUnconfirmed=t.spendUnconfirmed||!1,(t.fee||t.feeSat)&&(this.givenFeeSat=t.fee?t.fee*s.COIN:t.feeSat),this.remainderOut=t.remainderOut,this.signhash=t.signhash||d.SIGHASH_ALL,this.tx={},this.inputsSigned=0,this.signaturesAdded=0,this}var r=require("soop").imports(),i=r.Address||require("./Address"),n=r.Script||require("./Script"),s=r.util||require("../util"),a=r.bignum||require("bignum"),u=r.buffertools||require("buffertools"),o=r.networks||require("../networks"),h=r.WalletKey||require("./WalletKey"),p=r.PrivateKey||require("./PrivateKey"),c=r.Key||require("./Key"),f=r.log||require("../util/log"),d=r.Transaction||require("./Transaction"),g=parseInt(1e-4*s.COIN);e.FEE_PER_1000B_SAT=g,e.scriptForAddress=function(t){var e,r=o.livenet,s=o.testnet,a=new i(t),u=a.version();if(u===r.addressVersion||u===s.addressVersion)e=n.createPubKeyHashOut(a.payload());else{if(u!==r.P2SHVersion&&u!==s.P2SHVersion)throw new Error("invalid output address");e=n.createP2SH(a.payload())}return e},e._scriptForPubkeys=function(e){for(var r=e.pubkeys.length,i=[],s=0;r>s;s++)i.push(new t(e.pubkeys[s],"hex"));return n.createMultisig(e.nreq,i)},e._scriptForOut=function(t){var e;if(t.address)e=this.scriptForAddress(t.address);else{if(!(t.pubkeys||t.nreq||t.nreq>1))throw new Error("unknown out type");e=this._scriptForPubkeys(t)}return e},e.infoForP2sh=function(t,e){var r=this._scriptForOut(t),n=s.sha256ripe160(r.getBuffer()),a="testnet"===e?o.testnet.P2SHVersion:o.livenet.P2SHVersion,u=new i(a,n),h=u.as("base58");return{script:r,scriptBufHex:r.getBuffer().toString("hex"),hash:n,address:h}},e.prototype.setUnspent=function(t){return this.utxos=t,this},e.prototype._setInputMap=function(){for(var e=[],r=this.selectedUtxos.length,i=0;r>i;i++){var s=this.selectedUtxos[i],a=new t(s.scriptPubKey,"hex"),u=new n(a),o=u.classify();if(o===n.TX_UNKNOWN)throw new Error("unkown output type at:"+i+" Type:"+u.getRawOutType());e.push({address:s.address,scriptPubKey:u,scriptType:o,i:i})}return this.inputMap=e,this},e.prototype.getSelectedUnspent=function(){return this.selectedUtxos},e.prototype._selectUnspent=function(t){if(!this.utxos||!this.utxos.length)throw new Error("unspent not set");var e=[6,1];this.spendUnconfirmed&&e.push(0);var r=[],i=a(0),n=!1,u=null,o=this.utxos.length;do{for(var h=e.shift(),p=0;o>p;p++){var c=this.utxos[p],f=c.confirmations||0;if(!(h>f||u&&f>=u)){var d=c.amountSat||s.parseValue(c.amount);if(i=i.add(d),r.push(c),i.cmp(t)>=0){n=!0;break}}}u=h}while(!n&&e.length);if(!n)throw new Error("no enough unspent to fulfill totalNeededAmount [SAT]:"+t);return this.selectedUtxos=r,this._setInputMap(),this},e.prototype._setInputs=function(e){var r=this.selectedUtxos,i=r.length,n=a(0);e.ins=[];for(var o=0;i>o;o++){n=n.add(s.parseValue(r[o].amount));var h={};h.s=s.EMPTY_BUFFER,h.q=4294967295;var p=new t(r[o].txid,"hex"),c=u.reverse(p),f=parseInt(r[o].vout),d=new t(4);d.writeUInt32LE(f,0),h.o=t.concat([c,d]),e.ins.push(h)}return this.valueInSat=n,this},e.prototype._setFee=function(t){if("undefined"==typeof this.valueOutSat)throw new Error("valueOutSat undefined");var e=this.valueOutSat.add(t);if(this.valueInSat.cmp(e)<0){var r=this.valueInSat.toString(),i=e.toString();throw new Error("transaction input amount is less than outputs: "+r+" < "+i+" [SAT]")}return this.feeSat=t,this},e.prototype._setRemainder=function(t,r){if("undefined"==typeof this.valueInSat||"undefined"==typeof this.valueOutSat)throw new Error("valueInSat / valueOutSat undefined");var i=this.valueInSat.sub(this.valueOutSat).sub(this.feeSat),n=t.outs.length;if(this.remainderSat=a(0),n>r&&t.outs.pop(),i.cmp(0)>0){var u=this.remainderOut||this.selectedUtxos[0],o=s.bigIntToValue(i),h=e._scriptForOut(u),p={v:o,s:h.getBuffer()};t.outs.push(p),this.remainderSat=i}return this},e.prototype._setFeeAndRemainder=function(t){var e,r=500,i=t.outs.length;do{e=parseInt(r/1e3)+1;var n=this.givenFeeSat?this.givenFeeSat:e*g,s=this.valueOutSat.add(n);this._selectUnspent(s)._setInputs(t)._setFee(n)._setRemainder(t,i),r=new d(t).getSize()}while(r>1e3*(e+1));return this},e.prototype.setOutputs=function(t){var r=a(0),i={};i.version=1,i.lock_time=this.lockTime||0,i.ins=[],i.outs=[];for(var n=t.length,u=0;n>u;u++){var o=t[u].amountSat||s.parseValue(t[u].amount),h=s.bigIntToValue(o),p=e._scriptForOut(t[u]),c={v:h,s:p.getBuffer()};i.outs.push(c);var f=t[u].amountSat||s.parseValue(t[u].amount);r=r.add(f)}return this.valueOutSat=r,this._setFeeAndRemainder(i),this.tx=new d(i),this},e._mapKeys=function(t){for(var e,r={},i=t.length,n=0;i>n;n++){var s=t[n];if("string"==typeof s){var a=new p(s);e=new h({network:a.network()}),e.fromObj({priv:s})}else{if(!(s instanceof h))throw new Error("argument must be an array of strings (WIF format) or WalletKey objects");e=s}r[e.storeObj().addr]=e}return r},e._signHashAndVerify=function(t,e){var r,i=10;do r=t.privKey.signSync(e);while(t.privKey.verifySignatureSync(e,r)===!1&&i--);if(0>i)throw new Error("could not sign input: verification failed");return r},e.prototype._checkTx=function(){if(!this.tx||!this.tx.ins.length||!this.tx.outs.length)throw new Error("tx is not defined")},e.prototype._multiFindKey=function(t,e){var r;return[o.livenet,o.testnet].forEach(function(n){[n.addressVersion,n.P2SHVersion].forEach(function(n){var s=new i(n,e);!r&&t[s]&&(r=t[s])})}),r},e.prototype._findWalletKey=function(t,e){var r;if(e.address)r=t[e.address];else if(e.pubKeyHash)r=this._multiFindKey(t,e.pubKeyHash);else{if(!e.pubKeyBuf)throw new Error("no infomation at input to find keys");var i=s.sha256ripe160(e.pubKeyBuf);r=this._multiFindKey(t,i)}return r},e.prototype._signPubKey=function(r,i,s){if(this.tx.ins[i.i].s.length>0)return{};var a=this._findWalletKey(r,i);if(a){var u=e._signHashAndVerify(a,s),o=new t(1);o[0]=this.signhash;var h=t.concat([u,o]),p=new n;return p.chunks.push(h),p.updateBuffer(),{inputFullySigned:!0,signaturesAdded:1,script:p.getBuffer()}}},e.prototype._signPubKeyHash=function(r,i,s){if(this.tx.ins[i.i].s.length>0)return{};var a=this._findWalletKey(r,i);if(a){var u=e._signHashAndVerify(a,s),o=new t(1);o[0]=this.signhash;var h=t.concat([u,o]),p=new n;return p.chunks.push(h),p.chunks.push(a.privKey.public),p.updateBuffer(),{inputFullySigned:!0,signaturesAdded:1,script:p.getBuffer()}}},e.prototype._chunkSignedWithKey=function(e,r,i){var n,s=new c;s.public=i;for(var a=1;a<=e.countSignatures();a++){var u=e.chunks[a],o=new t(u.slice(0,u.length-1));s.verifySignatureSync(r,o)&&(n=u)}return n},e.prototype._getSignatureOrder=function(e,r,i,n){for(var s=n.length,a=0;s>a;a++){var u=new c;if(u.public=new t(n[a],"hex"),u.verifySignatureSync(i,r))break}return a},e.prototype._getNewSignatureOrder=function(e,r,i,n){for(var s,a=1;a<=r.countSignatures();a++){var u=r.chunks[a],o=new t(u.slice(0,u.length-1));if(s=this._getSignatureOrder(e,o,i,n),s>=e)break}return e===s?-1:a-1},e.prototype._chunkIsEmpty=function(t){return 0===t||0===u.compare(t,s.EMPTY_BUFFER)},e.prototype._initMultiSig=function(t){var e=!1;return 0!==t.chunks[0]&&(t.prependOp0(),e=!0),e},e.prototype._updateMultiSig=function(r,i,n,s,a){var u=this._initMultiSig(n);if(this._chunkSignedWithKey(n,s,i.privKey.public))return null;var o=e._signHashAndVerify(i,s),h=new t(1);h[0]=this.signhash;var p=t.concat([o,h]),c=this._getNewSignatureOrder(r,n,s,a);return n.chunks.splice(c+1,0,p),n.updateBuffer(),u=!0,u?n:null},e.prototype._signMultiSig=function(t,e,r){for(var i=e.scriptPubKey.capture(),s=e.scriptPubKey.chunks[0]-80,a=i.length,u=this.tx.ins[e.i].s,o=new n(u),h=0,p=0;a>p&&o.countSignatures()a;a++){var u=this.inputMap[a],o=this.tx.hashForSignature(u.scriptPubKey,a,this.signhash),h=l[u.scriptType].call(this,s,u,o);h&&h.script&&(r.ins[a].s=h.script,h.inputFullySigned&&this.inputsSigned++,h.signaturesAdded&&(this.signaturesAdded+=h.signaturesAdded))}return this},e.prototype.setHashToScriptMap=function(t){return this.hashToScriptMap=t,this},e.prototype.isFullySigned=function(){return this.inputsSigned===this.tx.ins.length},e.prototype.build=function(){return this._checkTx(),this.tx},e.prototype.toObj=function(){var t={valueInSat:this.valueInSat.toString(),valueOutSat:this.valueOutSat.toString(),feeSat:this.feeSat.toString(),remainderSat:this.remainderSat.toString(),hashToScriptMap:this.hashToScriptMap,selectedUtxos:this.selectedUtxos,inputsSigned:this.inputsSigned,signaturesAdded:this.signaturesAdded,signhash:this.signhash,spendUnconfirmed:this.spendUnconfirmed};return this.tx&&(t.tx=this.tx.serialize().toString("hex")),t},e.fromObj=function(r){var i=new e;if(i.valueInSat=r.valueInSat.toString(),i.valueOutSat=r.valueOutSat.toString(),i.feeSat=r.feeSat.toString(),i.remainderSat=r.remainderSat.toString(),i.hashToScriptMap=r.hashToScriptMap,i.selectedUtxos=r.selectedUtxos,i.inputsSigned=r.inputsSigned,i.signaturesAdded=r.signaturesAdded,i.signhash=r.signhash,i.spendUnconfirmed=r.spendUnconfirmed,i._setInputMap(),r.tx){var n=new d;n.parse(new t(r.tx,"hex")),i.tx=n}return i},e.prototype._checkMergeability=function(t){var e=this;if(["valueInSat","valueOutSat","feeSat","remainderSat","signhash","spendUnconfirmed"].forEach(function(r){if(e[r].toString()!==t[r].toString())throw new Error("mismatch at TransactionBuilder match: "+r+": "+e[r]+" vs. "+t[r])}),e.hashToScriptMap){var r=0;if(t.hashToScriptMap||(r=1),Object.keys(e.hashToScriptMap).forEach(function(i){t.hashToScriptMap[i]||(r=1),e.hashToScriptMap[i]!==t.hashToScriptMap[i]&&(r=1)}),r)throw new Error("mismatch at TransactionBuilder hashToScriptMap")}var r=0,i=0;if(e.selectedUtxos.forEach(function(e){if(!r){var n=t.selectedUtxos[i++];n||(r=1),["address","hash","scriptPubKey","vout","amount"].forEach(function(t){e[t]!==n[t]&&(r=t)})}}),r)throw new Error("mismatch at TransactionBuilder selectedUtxos #"+i-1+" Key:"+r);if(r=0,i=0,e.inputMap.forEach(function(e){if(!r){var n=t.inputMap[i++];n||(r=1),["address","scriptType","scriptPubKey","i"].forEach(function(t){e[t].toString()!==n[t].toString()&&(r=t)})}}),r)throw new Error("mismatch at TransactionBuilder inputMap #"+i-1+" Key:"+r)},e.prototype._mergeInputSigP2sh=function(t,e,r){for(var i=this._p2shInput(t),s=new n(i.scriptBuf),a=s.capture(),u={},o=a.length,h=0;o>h;h++)this._chunkSignedWithKey(e,i.txSigHash,a[h])&&(u[a[h].toString("hex")]=1);for(var p=[],h=0;o>h;h++){var c=this._chunkSignedWithKey(r,i.txSigHash,a[h]),f=a[h].toString("hex");c&&!u[f]&&p.push({prio:h,chunk:c,pubHex:f})}for(var h in p){var d=p[h],g=this._getNewSignatureOrder(d.prio,e,i.txSigHash,a);e.chunks.splice(g+1,0,d.chunk),this.signaturesAdded++}return e.updateBuffer(),e.getBuffer()},e.prototype._mergeInputSig=function(t,e,r){if(0===u.compare(e,r))return e;var i=new n(e),s=new n(r),a=i.chunks.length,o=s.chunks.length;if(a&&o&&(2>a&&o>2||2>o&&a>2))throw new Error("TX sig types mismatch in merge");if(!a&&!o||a&&!o||!a&&o)return r;var h=this.inputMap[t],p=h.scriptPubKey.classify();if(p===n.TX_PUBKEYHASH||p===n.TX_PUBKEY)return f.debug("Merging two signed inputs type:"+h.scriptPubKey.getRawOutType()+". Signatures differs. Using the first version."),e;if(p!==n.TX_SCRIPTHASH)throw new Error("Script type:"+h.scriptPubKey.getRawOutType()+"not supported at #merge");return this._mergeInputSigP2sh(h,i,s)},e.prototype._mergeTx=function(t){var e=this.tx,r=t,i=e.ins.length;if(i!==r.ins.length)throw new Error("TX in length mismatch in merge");this.inputsSigned=0;for(var n=0;i>n;n++){var s=e.ins[n],a=r.ins[n];if(s.q!==a.q)throw new Error("TX sequence ins mismatch in merge. Input:",n);if(0!==u.compare(s.o,a.o))throw new Error("TX .o in mismatch in merge. Input:",n);s.s=this._mergeInputSig(n,s.s,a.s),e.isInputComplete(n)&&this.inputsSigned++}},e.prototype.merge=function(t){if(this._checkMergeability(t),this.tx||t.tx){if(this.tx.getNormalizedHash().toString("hex")!==t.tx.getNormalizedHash().toString("hex"))throw new Error("mismatch at TransactionBuilder NTXID");this._mergeTx(t.tx)}},module.exports=require("soop")(e)}).call(this,require("buffer").Buffer); },{"../networks":"ULNIu2","../util":143,"../util/log":"AdF7pF","./Address":"G+CcXD","./Key":"ALJ4PS","./PrivateKey":"izTl9z","./Script":"hQ0t76","./Transaction":"LJhYtm","./WalletKey":"wWje7g","bignum":58,"buffer":85,"buffertools":"fugeBw","soop":129}],"yUY4WV":[function(require,module,exports){ (function(t){function e(t){"object"!=typeof t&&(t={}),this.datastore=JSON.parse(t.datastore?JSON.stringify(t.datastore):JSON.stringify(p)),this.network=void 0,this.dirty=t.dirty||!0}var r=require("soop").imports(),i=function(e){return new t(e,"hex")},n=require("fs"),s=require("../util/EncFile"),o=require("./Address"),a=require("../networks"),d=r.util||require("../util"),h="aes-256-cbc",p={client:"libcoin",client_version:"0.0.1",network:"testnet",version:1,best_hash:null,best_height:-1,keys:[],sin:{},scripts:{}};e.prototype.readSync=function(t,e){this.datastore=s.readJFileSync(h,e,t),this.dirty=!1},e.prototype.writeSync=function(t,e){var r=t+".tmp";s.writeJFileSync(h,e,r,this.datastore),n.renameSync(r,t),this.dirty=!1},e.prototype.setNetwork=function(t){switch(t||(t=this.datastore.network),t){case"mainnet":case"livenet":this.network=a.livenet;break;case"testnet":this.network=a.testnet;break;default:throw new Error("Unsupported network")}this.datastore.network=this.network.name,this.dirty=!0},e.prototype.addKey=function(t){this.datastore.keys.push(t),this.dirty=!0},e.prototype.addSIN=function(t){this.datastore.sin[t.sin]=t,this.dirty=!0},e.prototype.findKeyHash=function(t){for(var e=t.toString(),r=0;r>>5]>>>24-s%32&255);return new r(f,"hex")}return new r(i.createHash("rmd160").update(e).digest("binary"),"binary")}),g=(exports.sha1=function(e){return new r(i.createHash("sha1").update(e).digest("binary"),"binary")},exports.twoSha256=function(e){return v(v(e))},exports.sha256ripe160=function(e){return d(v(e))},exports.formatHash=function(e){var t=new r(10);return e.copy(t,0,22,32),l.reverse(t).toString("hex")},exports.formatHashFull=function(e){var t=new r(e.length);e.copy(t);var n=l.toHex(l.reverse(t));return n}),w=(exports.formatHashAlt=function(e){var r=g(e);return r=r.replace(/^0*/,""),r.substr(0,10)},exports.formatBuffer=function(e,t){null===t&&(t=10),(t>e.length||0===t)&&(t=e.length);var n=new r(t);e.copy(n,0,0,t);var o=l.toHex(n);return n.length=0&&(t[n]+=1,t[n]>=256&&(t[n]-=256),0===t[n]);n--);return t},exports.intToBuffer2C=function(e){var r=bytesNeededToStore(e),t=new c,n=e.toString(16),o="-"===n[0];n=n.replace("-","");for(var f=0;r>f;f++){var u=n.substring(n.length-2*(f+1),n.length-2*f);1===u.lenght&&(u="0"+u);var i=parseInt(u,16);t.word8(i)}var s=t.buffer();return o&&(s=l.reverse(s),s=negativeBuffer(s),s=l.reverse(s)),s};var x=function(e){var t;return 128&e[0]?(t=new r(e.length+1),e.copy(t,1),t[0]=0):t=e,t};exports.intToBufferSM=function(e){"number"==typeof e&&(e=s(e));var t,n,o=e.cmp(0);return o>0?(t=e.toBuffer(),n=x(t),n=l.reverse(n)):0==o?n=new r([]):(t=e.neg().toBuffer(),n=x(t),n[0]|=128,n=l.reverse(n)),n},exports.bufferSMToInt=function(e){if(!e.length)return s(0);if(e.length>4)throw new Error("Bigint cast overflow (> 4 bytes)");var t=new r(e.length);e.copy(t),t=l.reverse(t);var n=128&t[0];return n?(t[0]&=127,s.fromBuffer(t).neg()):s.fromBuffer(t)};var b=(exports.formatValue=function(e){for(var r=w(e).toString(),t=r.length>8?r.substr(0,r.length-8):"0",n=r.length>8?r.substr(r.length-8):r;n.length<8;)n="0"+n;for(n=n.replace(/0*$/,"");n.length<2;)n+="0";return t+"."+n},/^\s*(\d+)\.(\d+)/),y=/^\s*\.(\d+)/,B=/^\s*(\d+)/;exports.parseValue=function(e){"string"!=typeof e&&(e=e.toString());var r=e.match(b);return r?n(r):(r=e.match(y))?o(r):(r=e.match(B),r?f(r):void 0)};{var m=(exports.createSynchrotron=function(e){var r={};return function(t){var n=Array.prototype.slice.call(arguments),o=function(){n[0]=function(){r[t]&&(r[t].length?r[t].shift()():delete r[t])},e.apply(null,n)};r[t]?r[t].push(o):(r[t]=[],o())}},exports.decodeDiffBits=function(e,t){e=+e;for(var n=s(16777215&e),o=8*((e>>>24)-3);o-->0;)n=n.mul(2);if(t)return n;var f=n.toBuffer(),u=new r(32);return l.fill(u,0),f.copy(u,32-f.length),u}),I=(exports.encodeDiffBits=function(e){if(r.isBuffer(e))e=s.fromBuffer(e);else if("function"!=typeof e.toBuffer)throw new Error("Incorrect variable type for difficulty");var t=e.toBuffer("mpint"),n=t.length-4,o=n<<24;return n>=1&&(o|=t[4]<<16),n>=2&&(o|=t[5]<<8),n>=3&&(o|=t[6]),o},exports.calcDifficulty=function(e){r.isBuffer(e)||(e=m(e));var t=s.fromBuffer(e,{order:"forward"}),n=s.fromBuffer(E,{order:"forward"});return n.div(t).toNumber()},exports.reverseBytes32=function(e){if(e.length%4)throw new Error("Util.reverseBytes32(): Data length must be multiple of 4");for(var r=new c,t=a.parse(e);!t.eof();){var n=t.word32le("word").vars.word;r.word32be(n)}return r.buffer()},exports.getVarIntSize=function(e){return 253>e?1:65536>e?3:4294967296>e?5:9},exports.varIntBuf=function(e){var t=void 0;return 253>e?(t=new r(1),t.writeUInt8(e,0)):65536>e?(t=new r(3),t.writeUInt8(253,0),t.writeUInt16LE(e,1)):4294967296>e?(t=new r(5),t.writeUInt8(254,0),t.writeUInt32LE(e,1)):(t=new r(9),t.writeUInt8(255,0),t.writeInt32LE(-1&e,1),t.writeUInt32LE(Math.floor(e/4294967296),5)),t});exports.varStrBuf=function(e){return r.concat([I(e.length),e])}}exports.NULL_HASH=l.fill(new r(32),0),exports.EMPTY_BUFFER=new r(0),exports.ZERO_VALUE=l.fill(new r(8),0);var S=new r("ffffffffffffffff","hex");exports.INT64_MAX=S,exports.COIN=1e8;var E=exports.MAX_TARGET=new r("00000000FFFF0000000000000000000000000000000000000000000000000000","hex")}).call(this,require("/Users/ryanxcharles/dev/bitcore/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js"),require("buffer").Buffer); -},{"../browser/vendor-bundle.js":3,"../lib/sjcl":"oLMOpG","/Users/ryanxcharles/dev/bitcore/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":101,"bignum":58,"binary":74,"buffer":85,"bufferput":"aXRuS6","buffertools":"fugeBw","crypto":89}],"./util/util":[function(require,module,exports){ +},{}],"./util/util":[function(require,module,exports){ module.exports=require('ACyo5H'); -},{}]},{},[]) \ No newline at end of file +},{}],"ACyo5H":[function(require,module,exports){ +(function(e,r){function t(e){for(e=e.substr(0,8);e.length<8;)e+="0";return e}function n(e){return s(e[1]).mul("100000000").add(t(e[2]))}function o(e){return s(t(e[1]))}function f(e){return s(e[1]).mul("100000000")}var u,i=require("crypto"),s=require("bignum"),a=require("binary"),c=require("bufferput"),l=require("buffertools"),h=require("../lib/sjcl"),p=!e.versions;p&&(u=require("../browser/vendor-bundle.js"));{var v=exports.sha256=function(e){return new r(i.createHash("sha256").update(e).digest("binary"),"binary")},d=(exports.sha512=function(e){if(p){var t=e.toString("hex"),n=h.codec.hex.toBits(t),o=h.hash.sha512.hash(n),f=h.codec.hex.fromBits(o),u=new r(f,"hex");return u}return new r(i.createHash("sha512").update(e).digest("binary"),"binary")},exports.sha512hmac=function(e,t){if(p){var n=h.codec.hex.toBits(t.toString("hex")),o=h.codec.hex.toBits(e.toString("hex")),f=new h.misc.hmac(n,h.hash.sha512),u=f.encrypt(o),s=h.codec.hex.fromBits(u),a=new r(s,"hex");return a}var f=i.createHmac("sha512",t),c=f.update(e).digest();return c},exports.ripe160=function(e){if(!r.isBuffer(e))throw new Error("arg should be a buffer");if(p){for(var t=new u.crypto31.lib.WordArray.init(Crypto.util.bytesToWords(e),e.length),n=u.crypto31.RIPEMD160(t),o=n.words,f=[],s=0;s<32*o.length;s+=8)f.push(o[s>>>5]>>>24-s%32&255);return new r(f,"hex")}return new r(i.createHash("rmd160").update(e).digest("binary"),"binary")}),g=(exports.sha1=function(e){return new r(i.createHash("sha1").update(e).digest("binary"),"binary")},exports.twoSha256=function(e){return v(v(e))},exports.sha256ripe160=function(e){return d(v(e))},exports.formatHash=function(e){var t=new r(10);return e.copy(t,0,22,32),l.reverse(t).toString("hex")},exports.formatHashFull=function(e){var t=new r(e.length);e.copy(t);var n=l.toHex(l.reverse(t));return n}),w=(exports.formatHashAlt=function(e){var r=g(e);return r=r.replace(/^0*/,""),r.substr(0,10)},exports.formatBuffer=function(e,t){null===t&&(t=10),(t>e.length||0===t)&&(t=e.length);var n=new r(t);e.copy(n,0,0,t);var o=l.toHex(n);return n.length=0&&(t[n]+=1,t[n]>=256&&(t[n]-=256),0===t[n]);n--);return t},exports.intToBuffer2C=function(e){var r=bytesNeededToStore(e),t=new c,n=e.toString(16),o="-"===n[0];n=n.replace("-","");for(var f=0;r>f;f++){var u=n.substring(n.length-2*(f+1),n.length-2*f);1===u.lenght&&(u="0"+u);var i=parseInt(u,16);t.word8(i)}var s=t.buffer();return o&&(s=l.reverse(s),s=negativeBuffer(s),s=l.reverse(s)),s};var x=function(e){var t;return 128&e[0]?(t=new r(e.length+1),e.copy(t,1),t[0]=0):t=e,t};exports.intToBufferSM=function(e){"number"==typeof e&&(e=s(e));var t,n,o=e.cmp(0);return o>0?(t=e.toBuffer(),n=x(t),n=l.reverse(n)):0==o?n=new r([]):(t=e.neg().toBuffer(),n=x(t),n[0]|=128,n=l.reverse(n)),n},exports.bufferSMToInt=function(e){if(!e.length)return s(0);if(e.length>4)throw new Error("Bigint cast overflow (> 4 bytes)");var t=new r(e.length);e.copy(t),t=l.reverse(t);var n=128&t[0];return n?(t[0]&=127,s.fromBuffer(t).neg()):s.fromBuffer(t)};var b=(exports.formatValue=function(e){for(var r=w(e).toString(),t=r.length>8?r.substr(0,r.length-8):"0",n=r.length>8?r.substr(r.length-8):r;n.length<8;)n="0"+n;for(n=n.replace(/0*$/,"");n.length<2;)n+="0";return t+"."+n},/^\s*(\d+)\.(\d+)/),y=/^\s*\.(\d+)/,B=/^\s*(\d+)/;exports.parseValue=function(e){"string"!=typeof e&&(e=e.toString());var r=e.match(b);return r?n(r):(r=e.match(y))?o(r):(r=e.match(B),r?f(r):void 0)};{var m=(exports.createSynchrotron=function(e){var r={};return function(t){var n=Array.prototype.slice.call(arguments),o=function(){n[0]=function(){r[t]&&(r[t].length?r[t].shift()():delete r[t])},e.apply(null,n)};r[t]?r[t].push(o):(r[t]=[],o())}},exports.decodeDiffBits=function(e,t){e=+e;for(var n=s(16777215&e),o=8*((e>>>24)-3);o-->0;)n=n.mul(2);if(t)return n;var f=n.toBuffer(),u=new r(32);return l.fill(u,0),f.copy(u,32-f.length),u}),I=(exports.encodeDiffBits=function(e){if(r.isBuffer(e))e=s.fromBuffer(e);else if("function"!=typeof e.toBuffer)throw new Error("Incorrect variable type for difficulty");var t=e.toBuffer("mpint"),n=t.length-4,o=n<<24;return n>=1&&(o|=t[4]<<16),n>=2&&(o|=t[5]<<8),n>=3&&(o|=t[6]),o},exports.calcDifficulty=function(e){r.isBuffer(e)||(e=m(e));var t=s.fromBuffer(e,{order:"forward"}),n=s.fromBuffer(E,{order:"forward"});return n.div(t).toNumber()},exports.reverseBytes32=function(e){if(e.length%4)throw new Error("Util.reverseBytes32(): Data length must be multiple of 4");for(var r=new c,t=a.parse(e);!t.eof();){var n=t.word32le("word").vars.word;r.word32be(n)}return r.buffer()},exports.getVarIntSize=function(e){return 253>e?1:65536>e?3:4294967296>e?5:9},exports.varIntBuf=function(e){var t=void 0;return 253>e?(t=new r(1),t.writeUInt8(e,0)):65536>e?(t=new r(3),t.writeUInt8(253,0),t.writeUInt16LE(e,1)):4294967296>e?(t=new r(5),t.writeUInt8(254,0),t.writeUInt32LE(e,1)):(t=new r(9),t.writeUInt8(255,0),t.writeInt32LE(-1&e,1),t.writeUInt32LE(Math.floor(e/4294967296),5)),t});exports.varStrBuf=function(e){return r.concat([I(e.length),e])}}exports.NULL_HASH=l.fill(new r(32),0),exports.EMPTY_BUFFER=new r(0),exports.ZERO_VALUE=l.fill(new r(8),0);var S=new r("ffffffffffffffff","hex");exports.INT64_MAX=S,exports.COIN=1e8,exports.BIT=100;var E=exports.MAX_TARGET=new r("00000000FFFF0000000000000000000000000000000000000000000000000000","hex")}).call(this,require("/Users/ryanxcharles/dev/bitcore/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js"),require("buffer").Buffer); +},{"../browser/vendor-bundle.js":3,"../lib/sjcl":"oLMOpG","/Users/ryanxcharles/dev/bitcore/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":101,"bignum":58,"binary":74,"buffer":85,"bufferput":"aXRuS6","buffertools":"fugeBw","crypto":89}]},{},[]) \ No newline at end of file diff --git a/lib/Address.js b/lib/Address.js index 6f592ab..d8d6ef6 100644 --- a/lib/Address.js +++ b/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); } diff --git a/test/test.Address.js b/test/test.Address.js index 13f2f18..cbd269d 100644 --- a/test/test.Address.js +++ b/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');