UNPKG

digibyte

Version:

A pure and powerful JavaScript DigiByte library.

1 lines 686 kB
require=function(){return function t(e,r,n){function i(o,a){if(!r[o]){if(!e[o]){var u="function"==typeof require&&require;if(!a&&u)return u(o,!0);if(s)return s(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var c=r[o]={exports:{}};e[o][0].call(c.exports,function(t){return i(e[o][1][t]||t)},c,c.exports,t,e,r,n)}return r[o].exports}for(var s="function"==typeof require&&require,o=0;o<n.length;o++)i(n[o]);return i}}()({1:[function(t,e,r){(function(r){"use strict";var n=t("lodash"),i=t("./util/preconditions"),s=t("./errors"),o=t("./encoding/bech32"),a=t("./encoding/bech32check"),u=t("./encoding/base58check"),f=t("./networks"),c=t("./crypto/hash"),h=t("./util/js"),d=t("./publickey");function p(t,e,r,s){if(!(this instanceof p))return new p(t,e,r);if(n.isArray(t)&&n.isNumber(e))return p.createMultisig(t,e,r,s);if(t instanceof p)return t;if(i.checkArgument(t,"First argument is required, please include address data.","guide/address.html"),e&&!f.get(e))throw new TypeError('Second argument must be "livenet" or "testnet".');if(r&&r!==p.PayToPublicKeyHash&&r!==p.PayToScriptHash&&r!==p.PayToWitnessPublicKeyHash&&r!==p.PayToWitnessScriptHash)throw new TypeError('Third argument must be "pubkeyhash", "scripthash", "witnesspubkeyhash", or "witnessscripthash".');var o=this._classifyArguments(t,e,r);return o.network=o.network||f.get(e)||f.defaultNetwork,o.type=o.type||r||p.PayToPublicKeyHash,h.defineImmutable(this,{hashBuffer:o.hashBuffer,network:o.network,type:o.type}),this}p.prototype._classifyArguments=function(t,e,i){if((t instanceof r||t instanceof Uint8Array)&&20===t.length)return p._transformHash(t);if(!(t instanceof r||t instanceof Uint8Array)||21!==t.length&&34!==t.length){if(t instanceof d)return p._transformPublicKey(t);if(t instanceof l)return p._transformScript(t,e);if("string"==typeof t)return p._transformString(t,e,i);if(n.isObject(t))return p._transformObject(t);throw new TypeError("First argument is an unrecognized data format.")}return p._transformBuffer(t,e,i)},p.PayToPublicKeyHash="pubkeyhash",p.PayToScriptHash="scripthash",p.PayToWitnessPublicKeyHash="paytowitnesspublickeyhash",p.PayToWitnessScriptHash="paytowitnessscripthash",p.PayToPublicKey="publickey",p._transformHash=function(t){var e={};if(!(t instanceof r||t instanceof Uint8Array))throw new TypeError("Address supplied is not a buffer.");if(20!==t.length)throw new TypeError("Address hashbuffers must be exactly 20 bytes.");return e.hashBuffer=t,e},p._transformObject=function(t){return i.checkArgument(t.hash||t.hashBuffer,"Must provide a `hash` or `hashBuffer` property"),i.checkArgument(t.type,"Must provide a `type` property"),{hashBuffer:t.hash?new r(t.hash,"hex"):t.hashBuffer,network:f.get(t.network)||f.defaultNetwork,type:t.type}},p._classifyFromVersion=function(t,e,r){var n={},i=f.get(t[0],"pubkeyhash"),s=f.get(t[0],"scripthash"),o=f.get(t[0],"scripthashTwo");if(r){if(20===t.length)n.type=p.PayToWitnessPublicKeyHash;else{if(32!==t.length)throw new TypeError("Invalid buffer length for segwit address");n.type=p.PayToWitnessScriptHash}n.network=f.get(r,"prefix")}else i?(n.network=i,n.type=p.PayToPublicKeyHash):(s||o)&&(n.network=s||o,n.type=p.PayToScriptHash);return n},p._transformBuffer=function(t,e,n,i){var s={};if(!(t instanceof r||t instanceof Uint8Array))throw new TypeError("Address supplied is not a buffer.");if(i&&20!==t.length&&32!==t.length)throw new TypeError("Address buffer is incorrect length.");if(!i&&21!==t.length)throw new TypeError("Address buffer is incorrect length.");var o=f.get(e),a=p._classifyFromVersion(t,e,i);if(e&&!o)throw new TypeError("Unknown network");if(!a.network||o&&o!==a.network)throw new TypeError("Address has mismatched network type.");if(!a.type||n&&n!==a.type)throw new TypeError("Address has mismatched type.");return s.hashBuffer=i?t:t.slice(1),s.network=a.network,s.type=a.type,s},p._transformPublicKey=function(t){var e={};if(!(t instanceof d))throw new TypeError("Address must be an instance of PublicKey.");return e.hashBuffer=c.sha256ripemd160(t.toBuffer()),e.type=p.PayToPublicKeyHash,e},p._transformScript=function(t,e){i.checkArgument(t instanceof l,"script must be a Script instance");var r=t.getAddressInfo(e);if(!r)throw new s.Script.CantDeriveAddress(t);return r},p.createMultisig=function(t,e,n,i){n=n||t[0].network||f.defaultNetwork;var s=l.buildMultisigOut(t,e);if(!i){var u=c.sha256(s.toBuffer()),h=o.toWords(u);h.unshift(0);var d={};return d.hashBuffer=r.from(h),d.type=p.PayToWitnessScriptHash,d.network=n,new p(a.encode(r.from(h),n.prefix),n)}return p.payingTo(s,n)},p._transformString=function(t,e,n){if("string"!=typeof t)throw new TypeError("data parameter supplied is not a string.");if(t.length<34)throw new Error("Invalid Address string provided");t=t.trim();try{var i=o.decode(t),s=(i.shift(),o.fromWords(i));return e=f.get(t.split("1")[0],"prefix"),p._transformBuffer(r.from(s),e,n,e.prefix)}catch(t){if(n===p.PayToWitnessPublicKeyHash||n===p.PayToWitnessScriptHash)return t}var a=u.decode(t);return p._transformBuffer(a,e,n)},p.fromPublicKey=function(t,e){var r=p._transformPublicKey(t);return e=e||f.defaultNetwork,new p(r.hashBuffer,e,r.type)},p.fromPublicKeyHash=function(t,e){return new p(p._transformHash(t).hashBuffer,e,p.PayToPublicKeyHash)},p.fromScriptHash=function(t,e){return i.checkArgument(t,"hash parameter is required"),new p(p._transformHash(t).hashBuffer,e,p.PayToScriptHash)},p.payingTo=function(t,e,r){return i.checkArgument(t,"script is required"),i.checkArgument(t instanceof l,"script must be instance of Script"),p.fromScriptHash(c.sha256ripemd160(t.toBuffer()),e)},p.fromScript=function(t,e){i.checkArgument(t instanceof l,"script must be a Script instance");var r=p._transformScript(t,e);return new p(r.hashBuffer,e,r.type)},p.fromBuffer=function(t,e,r){var n=p._transformBuffer(t,e,r);return new p(n.hashBuffer,n.network,n.type)},p.fromString=function(t,e,r){var n=p._transformString(t,e,r);return new p(n.hashBuffer,n.network,n.type)},p.fromObject=function(t){return i.checkState(h.isHexa(t.hash),'Unexpected hash property, "'+t.hash+'", expected to be hex.'),new p(new r(t.hash,"hex"),t.network,t.type)},p.getValidationError=function(t,e,r){var n;try{new p(t,e,r)}catch(t){n=t}return n},p.isValid=function(t,e,r){return!p.getValidationError(t,e,r)},p.prototype.isPayToPublicKeyHash=function(){return this.type===p.PayToPublicKeyHash},p.prototype.isPayToScriptHash=function(){return this.type===p.PayToScriptHash},p.prototype.isPayToWitnessPublicKeyHash=function(){return this.type===p.PayToWitnessPublicKeyHash},p.prototype.isPayToWitnessScriptHash=function(){return this.type===p.PayToWitnessScriptHash},p.prototype.toBuffer=function(){var t=new r([this.network[this.type]]);return r.concat([t,this.hashBuffer])},p.toBech32Address=function(t,e){return new p({hashBuffer:c.sha256ripemd160(t),type:p.PayToWitnessPublicKeyHash,network:e||this.network})},p.prototype.toObject=p.prototype.toJSON=function(){return{hash:this.hashBuffer.toString("hex"),type:this.type,network:this.network.toString()}},p.prototype.toString=function(){if(this.type===p.PayToWitnessPublicKeyHash||this.type===p.PayToWitnessScriptHash){var t=o.toWords(this.hashBuffer);return t.unshift(0),a.encode(r.from(t),this.network.prefix)}return u.encode(this.toBuffer())},p.prototype.inspect=function(){return"<Address: "+this.toString()+", type: "+this.type+", network: "+this.network+">"};e.exports=p;var l=t("./script")}).call(this,t("buffer").Buffer)},{"./crypto/hash":15,"./encoding/base58check":20,"./encoding/bech32":21,"./encoding/bech32check":22,"./errors":26,"./networks":30,"./publickey":33,"./script":34,"./util/js":56,"./util/preconditions":57,buffer:116,lodash:174}],2:[function(t,e,r){(function(r){"use strict";var n=t("lodash"),i=t("node-rsa"),s=t("../util/preconditions"),o=(t("../util/assets"),t("../encoding/base58check")),a=t("../crypto/bn"),u=t("../util/buffer"),f=t("../crypto/hash"),c=t("../address"),h=(t("../opcode"),t("../script")),d=t("../transaction"),p=t("../unit"),l={aggregatable:11831,hybrid:11883,dispersed:11854},b={aggregatable:8398,hybrid:8450,dispersed:8420},g={issuance:{start:0,end:15,encoder:t("./issuanceencoder")},transfer:{start:16,end:31,encoder:t("./transferencoder")},burn:{start:32,end:47,encoder:t("./transferencoder")}},y={};for(var m in g)for(var v=g[m].start;v<=g[m].end;v++)y[v]={},y[v].encoder=g[m].encoder,y[v].type=m;var _=function(t,e){return e||(e=Math.ceil(t.length/2)),t.length===2*e?t:_("0"+t,e)},w=function(t){for(var e=[],r=0,n=0;n<t.length;n++){var i=t[n].burn?{burn:!0}:{range:t[n].range,output:t[n].output};i.input=r,i.percent=t[n].percent,i.amount=t[n].amount,e.push(i),t[n].skip&&(r+=1)}return e};function S(t){if(!(this instanceof S))return new S(t);t&&(this.aggregationPolicy=t.aggregationPolicy||"aggregatable",this.assetId=t.assetId||"",this.type=t.type||void 0,this.lockStatus=t.lockStatus||!0,this.multiSig=t.multiSig||[],this.payments=t.payments||[],this.amount=t.amount,this.issueAddress=t.issueAddress,this.to=t.to,this.from=t.from,this.burn=t.burn,this.protocol=t.protocol||S.ASSET_IDENTIFIER,this.version=t.version||S.DA_TX_VERSION,this.divisibility=t.divisibility||0,this.urls=t.urls||[],this.transfer=t.transfer||[],this.metadata=t.metadata,this.rules=t.rules||[],this.falgs=t.flags,this.fee=t.fee,this.financeOutput=t.financeOutput,this.financeOutputTxid=t.financeOutputTxid,this.sha1=t.sha1,this.sha2=t.sha2,this.ignoreMetadata=t.ignoreMetadata||!1),this.type||this.fromBuffer(t)}S.MIN_FEE=1e3,S.DA_TX_VERSION=2,S.ASSET_IDENTIFIER=17473,S.MAXBYTESIZE=80,S.prototype.fromBuffer=function(t){var e=y[t[3]],r=(new e.encoder).decode(t);return this.protocol=r.protocol,this.version=r.version,this.multiSig=r.multiSig||[],this.payments=w(r.payments),this.type=e.type,"issuance"===this.type&&(this.lockStatus=r.lockStatus,this.aggregationPolicy=r.aggregationPolicy,this.divisibility=r.divisibility,this.amount=r.amount),this},S.prototype.setAmount=function(t,e){s.checkState(t,"Amount must be set"),this.type="issuance",this.divisibility=e||0,this.amount=t},S.prototype.setLockStatus=function(t){this.lockStatus=t,this.type="issuance"},S.prototype.setAggregationPolicy=function(t){this.aggregationPolicy=t||"aggregatable",this.type="issuance"},S.prototype.setHash=function(t,e){if(!t)throw new Error("Can't set hashes without the torrent hash");r.isBuffer(t)||(t=new r(t,"hex")),this.torrentHash=t,e&&(r.isBuffer(e)||(e=new r(e,"hex")),this.sha2=e)},S.prototype.allowRules=function(){this.noRules=!1},S.prototype.getAssetId=function(t){var e,r=t.script,n=t.toObject();return this.lockStatus?(e=b[this.aggregationPolicy],this.createIdFromTxidIndex(n,e)):(e=l[this.aggregationPolicy],n.previousOutput&&n.previousOutput.hex?createIdFromPreviousOutputScriptPubKey(n.previousOutput.hex,e,divisibility):this.createIdFromPubKeyHashInput(r,e))},S.prototype.createIdFromTxidIndex=function(t,e){var n=t.prevTxId+":"+t.outputIndex;return this.assetId=this.hashAndBase58CheckEncode(r.from(n),e),this.assetId},S.prototype.createIdFromPubKeyHashInput=function(e,r){var n=t("../script"),i=new c(f.sha256ripemd160(e.chunks[1].buf)),s=n.buildPublicKeyHashOut(i).toBuffer();return this.assetId=this.hashAndBase58CheckEncode(s,r),this.assetId},S.prototype.hashAndBase58CheckEncode=function(t,e){var n=f.sha256(t),i=f.ripemd160(n);e=new r(_(e.toString(16)),"hex");var s=new r(_(this.divisibility.toString(16),2),"hex"),a=r.concat([e,i,s]);return o.encode(a)},S.prototype.encode=function(){var t=g[this.type];this.payments=function(t){var e=JSON.parse(JSON.stringify(t));e.sort(function(t,e){return t.input-e.input});for(var r=0;r<e.length;r++){var n=!1;e[r+1]&&e[r+1].input>e[r].input&&(n=!0),delete e[r].input,e[r].skip=n}return e}(this.payments);var e=new t.encoder(this).encode(S.MAXBYTESIZE);return this.payments=w(this.payments),e},S.prototype.addPayment=function(t,e,r,n,i){n=n||!1,i=i||!1;this.payments.push({input:t,amount:e,output:r,range:n,percent:i})},S.prototype.addBurn=function(t,e,r){if("issuance"===this.type)throw new Error("Can't add burn payment to an issuance transaction");this.payments.push({input:t,amount:e,percent:r,burn:!0}),this.type="burn"},S.prototype.tryEncryptData=function(t){try{if(t.metadata&&t.metadata.encryptions&&t.metadata.userData){var e=new i({b:1024}),r=!1;return t.metadata.encryptions.forEach(function(n){r=r||!n.pubKey;var s=t.metadata.userData[n.key];if(s){n.type,n.format;var o=(n.pubKey?new i([n.pubKey]):e).encrypt(s,"base64");t.metadata.userData[n.key]=o}}),{privateKey:r?e.exportKey("pkcs8").toString("hex"):""}}}catch(t){return t}},S.prototype.getMetaData=function(){var t={};if(this.metadata){var e=this.tryEncryptData();if(e&&e.error)throw new Error("Encryption error: "+e.error);e&&e.privateKey&&(this.privateKey=e.privateKey),t.data=this.metadata,this.rules&&(t.rules=this.rules)}return t},S.prototype.findBestMatchByNeededAssets=function(t,e,r,i,s){var o,a=this,f=[],c=0,p=this.findBestGreaterOrEqualAmountUtxo(t,e,r);p?f[0]=p:n.sortBy(t,function(t){return-a.getUtxoAssetAmount(t,r)}).some(function(t){return f.push(t),(c+=a.getUtxoAssetAmount(t,r))>=e[r].amount})||(f.length=0);return!!f.length&&(f.some(function(t){return t.assets.forEach(function(r){try{if(e[r.assetId]&&!e[r.assetId].done){var n=i.inputs.length;if(!i.inputs.some(function(e,r){return e.index===t.index&&u.reverse(e.hash).toString("hex")===t.txid&&(n=r,!0)})){var f=new d.UnspentOutput({address:t.address,txid:t.txid,vout:t.index,scriptPubKey:t.scriptPubKey.hex,amount:t.value});i.from(f),s.amount+=Math.round(t.value),a.flags&&a.flags.injectPreviousOutput&&(i.inputs[i.inputs.length-1].script=h.fromHex(t.scriptPubKey.hex))}var c=r.aggregationPolicy||"aggregatable",p=e[r.assetId].inputs.length;if(e[r.assetId].amount<=r.amount){var l=r.amount;"aggregatable"===c&&o===r.assetId&&e[r.assetId].inputs.length?e[r.assetId].inputs[p-1].amount+=e[r.assetId].amount:e[r.assetId].inputs.push({index:n,amount:e[r.assetId].amount}),e[r.assetId].change=l-e[r.assetId].amount,e[r.assetId].done=!0}else"aggregatable"===c&&o===r.assetId&&e[r.assetId].inputs.length?e[r.assetId].inputs[p-1].amount+=r.amount:e[r.assetId].inputs.push({index:n,amount:r.amount}),e[r.assetId].amount-=r.amount}}catch(t){throw t}o=r.assetId}),e[r].done}),!0)},S.prototype.findBestGreaterOrEqualAmountUtxo=function(t,e,r){var i=!1,s=this;return t.forEach(function(t){t.score=0;var n=s.getUtxoAssetAmount(t,r);if(!(n<e[r].amount))for(var o in i=!0,n===e[r].amount?t.score+=1e4:t.score+=1e3,e)o!==r&&((n=s.getUtxoAssetAmount(t,o))===e[o].amount?t.score+=100:n>e[o].amount?t.score+=10:t.score+=n/e[o].amount)}),i&&n.maxBy(t,function(t){return t.score})},S.prototype.insertSatoshiToTransaction=function(t,e,r,n){var i=this,s=new a(r),o=new a(0),u=new a(0);return i.financeOutput&&i.financeOutputTxid?!i.isInputInTx(e,i.financeOutputTxid,i.financeOutput.n)&&((o=new a(i.financeOutput.value)).minus(s)>=0?(e.addInput(i.financeOutputTxid,i.financeOutput.n),n.amount+=o.toNumber(),i.flags&&i.flags.injectPreviousOutput&&(e.inputs[e.inputs.length-1].script=h.fromHex(i.financeOutput.scriptPubKey.hex)),!0):void 0):t.some(function(t){if(t.value=Math.round(t.value),!(i.isInputInTx(e,t.txid,t.index)||t.assets&&t.assets.length)){var r=new d.UnspentOutput({address:t.address,txid:t.txid,vout:t.index,scriptPubKey:t.scriptPubKey.hex,amount:p.fromSatoshis(t.value).toDGB()});e.from(r),n.amount+=t.value,u=u.add(new a(t.value)),i.flags&&i.flags.injectPreviousOutput&&(e.inputs[e.inputs.length-1].script=h.fromHex(t.scriptPubKey.hex))}return u.cmp(s)>=0})},S.prototype.getUtxoAssetAmount=function(t,e){return n(t.assets).filter(function(t){return t.assetId===e}).sumBy("amount")},S.prototype.tryAddingInputsForFee=function(t,e,r,n){return!(n>r.amount&&!this.insertSatoshiToTransaction(e,t,n-r.amount,r))},S.prototype.isInputInTx=function(t,e,r){return t.inputs.some(function(t){return u.reverse(t.prevTxId).toString("hex")===e&&t.index===r})},S.prototype.getTotalIssuenceCost=function(t){var e=t?S.MIN_FEE:0;return this.transfer&&this.transfer.length&&this.transfer.forEach(function(t){e+=d.DUST_AMOUNT}),(this.rules||this.metadata)&&(e+=700),e+=d.DUST_AMOUNT},S.prototype.getIssuenceCost=function(){return this.getTotalIssuenceCost(!0)},S.prototype.comupteCost=function(t){var e=t?this.fee||S.MIN_FEE:0;return this.to&&this.to.length&&this.to.forEach(function(t){e+=d.DUST_AMOUNT}),(this.rules||this.metadata)&&(e+=700),e+=d.DUST_AMOUNT},S.prototype.getChangeAmount=function(t,e){var r=n.sumBy(t.outputs,function(t){return t.toObject().satoshis});return e.amount-(r+this.fee)},S.prototype.getNoneMinDustByScript=function(t,e){return varfee=e||d.FEE_PER_KB,d.FEE_PER_KB*(t.toBuffer().length+148+9)/1e3*3},S.prototype.getInputAmountNeededForTx=function(t,e){var r=e||d.FEE_PER_KB;return t.outputs.forEach(function(t){r+=this.getNoneMinDustByScript(t.script,e)}),r},S.prototype.addInputsForSendTransaction=function(t,e){var r=this,i=[],s={amount:0},o=this.comupteCost(!0),a=[],u=[];for(var f in r.to.forEach(function(t){if(i[t.assetId]||(i[t.assetId]={amount:0,addresses:[],done:!1,change:0,encodeAmount:0,inputs:[]}),i[t.assetId].amount+=t.amount,t.burn)i[t.assetId].addresses.push({address:"burn",amount:t.amount});else if(!t.address&&t.pubKeys&&t.m){var e=generateMultisigAddress(t.pubKeys,t.m);i[t.assetId].addresses.push({address:e.address,amount:t.amount,reedemScript:e.reedemScript})}else i[t.assetId].addresses.push({address:t.address,amount:t.amount})}),i){var c=e.filter(function(t,e,r){return!!t.assets&&t.assets.some(function(t){return t.assetId==f})});if(!(c&&c.length>0))throw new Error("No output with that asset - asset: "+f);var h=f;if(c.forEach(function(t){if(t.used)throw new Error("Output Alreaedy Spent - output: "+t.txid+":"+t.index)}),!r.findBestMatchByNeededAssets(c,i,h,t,s))throw new Error("Not enough assets - asset: "+h)}if(!r.tryAddingInputsForFee(t,e,s,o))throw new Error("Not enough funds");for(f in i){var p=i[f];if(!p.done)return new Error("Not enough Assets - asset: "+f);n.uniqBy(p.addresses,function(t){return t.address}).forEach(function(e){var n=e.amount;p.inputs.some(function(i){return!!i.amount&&(n-i.amount>0?("burn"===e.address?r.addBurn(i.index,i.amount):r.addPayment(i.index,i.amount,t.outputs?t.outputs.length:0),n-=i.amount,i.amount=0,!1):("burn"===e.address?r.addBurn(i.index,n):r.addPayment(i.index,n,t.outputs?t.outputs.length:0),i.amount-=n,n=0,!0))}),"burn"!==e.address&&t.to(e.address,d.DUST_AMOUNT),e.reedemScript&&u.push({index:t.outputs.length-1,reedemScript:e.reedemScript,address:e.address})})}try{if((r.metadata||r.rules)&&!r.ignoreMetadata){if(!r.sha1||!r.sha2)throw new Error("Missing Torrenthash!");r.setHash(r.sha1,r.sha2)}var l=r.encode();if(l.leftover&&l.leftover.length>0)if(r.shiftOutputs(),u.forEach(function(t){t.index+=1}),1==(l=r.encode()).leftover.length)addHashesOutput(t,r.pubKeyReturnMultisigDust,l.leftover[0]);else{if(2!=l.leftover.length)throw new Error("have hashes and enough room we offested inputs for nothing");addHashesOutput(t,r.pubKeyReturnMultisigDust,l.leftover[1],l.leftover[0])}r.payments.forEach(function(t){void 0!==t.output&&a.push(t.output)})}catch(t){throw t}t.addData(l.codeBuffer);var b=r.getChangeAmount(t,s),g=n.keys(i).some(function(t){return i[t].change>0}),y=r.flags&&r.flags.splitChange&&g&&b>=2*d.DUST_AMOUNT?2:1;if(b<y*d.DUST_AMOUNT){if(o=r.getInputAmountNeededForTx(t,r.fee)+y*d.DUST_AMOUNT,!r.tryAddingInputsForFee(t,e,s,o))throw new Error("Not Enough funds");b=r.getChangeAmount(t,s)}return 2===y&&(t.addOutput(Array.isArray(r.from)?r.from[0]:r.from,b-d.DUST_AMOUNT),b=d.DUST_AMOUNT),g&&a.push(t.outputs.length),t.to(r.from,b),{tx:t,multisigOutputs:u,coloredOutputIndexes:n.uniqBy(a)}},S.prototype.addInputsForIssuance=function(t,e){var r=new a(e[0].value);if(e.length>1){var n=e.map(function(t){return t.value});r=new a(n.reduce(function(t,e){return t+e}))}var i=new a(this.getIssuenceCost()),s=e.map(function(t){return new d.UnspentOutput({address:t.address,txid:t.txid,vout:t.index,scriptPubKey:t.scriptPubKey.hex,amount:t.value})});return t.from(s),this.flags&&this.flags.injectPreviousOutput&&(t.inputs[t.inputs.length-1].script=new h(utxo.scriptPubKey)),this.assetId=this.getAssetId(t.inputs[0]),this.change=r-i,this.totalInputs={amount:r},{tx:t,assetData:this,change:r-i,totalInputs:{amount:r}}},S.prototype.createBurnAssetTransaction=function(t){var e=this.to||[],r=this.burn||[];r.forEach(function(t){t.burn=!0}),e.push.apply(e,r),delete this.transfer,this.to=e;this.createSendAssetTransaction(t);return this.createSendAssetTransaction(t)},S.prototype.createSendAssetTransaction=function(t,e){return this.addInputsForSendTransaction(t,e),this},S.prototype.createIssueTransaction=function(t,e){return s.checkState(this.amount,"Amount must be set"),s.checkState(this.issueAddress,"Need an Issue Address"),s.checkState(this.metadata,"no metadata"),this.addInputsForIssuance(t,e),this},S.prototype.encodeDigiAssetScheme=function(t){var e=!1,r=new S({protocol:S.ASSET_IDENTIFIER,version:S.DA_TX_VERSION,type:"issuance",divisibility:this.divisibility,protocol:17473,version:2}),i=[],s=[],o=this.amount;if(this.setLockStatus(!this.reissueable),this.setAmount(this.amount,this.divisibility),this.setAggregationPolicy(this.aggregationPolicy),(this.metadata||this.rules)&&!this.ignoreMetadata){if(!this.sha1||!this.sha2)throw new Error("Missing SHA hash");this.setHash(this.sha1,this.sha2)}if(this.transfer&&this.transfer.forEach(function(e,r){if(this.addPayment(0,e.amount,t.outputs.length),o-=e.amount,e.pubKeys&&e.m){var n=generateMultisigAddress(e.pubKeys,e.m);i.push({index:t.outputs.length,reedemScript:n.reedemScript,address:n.address}),t.addOutput(n.address,d.DUST_AMOUNT)}else t.addOutput(e.address,d.DUST_AMOUNT)}),o<0)throw new Error("transferring more than issued");var a=this.encode();if(a.leftover&&a.leftover.length>0&&(this.shiftOutputs(),a=this.encode(),e=!0,i.forEach(function(t){t.index+=1})),t.addData(a.codeBuffer),r.payments.forEach(function(t){s.push(t.output)}),e)if(a.leftover&&1==a.leftover.length)addHashesOutput(t,this.pubKeyReturnMultisigDust,a.leftover[0]);else{if(!a.leftover||2!=a.leftover.length)throw new Error("have hashes and enough room we offested inputs for nothing");addHashesOutput(t,this.pubKeyReturnMultisigDust,a.leftover[1],a.leftover[0])}var u=n.sumBy(t.outputs,function(t){return t.value}),f=this.totalInputs.amount-(u+this.fee);if(f<d.DUST_AMOUNT){var c=d.DUST_AMOUNT+this.totalInputs.amount.toNumber();throw new errors.NotEnoughFundsError({type:"issuance",fee:metadata.fee,totalCost:c,missing:d.DUST_AMOUNT-f})}if(this.flags&&this.flags.splitChange&&f>=2*d.DUST_AMOUNT&&o>0){var h=f-d.DUST_AMOUNT;f=d.DUST_AMOUNT,t.addOutput(metadata.issueAddress,h)}return o>0&&s.push(t.outputs.length),t.to(this.issueAddress,f?Math.abs(f):Math.abs(this.change)),this},S.prototype.inspect=function(){return"issuance"===this.type?"<DigiAsset: assetId: "+this.assetId+", type: "+this.type+", amount: "+this.amount+", divisibility: "+this.divisibility.toString(16)+", protocol: "+this.protocol+", version: "+this.version+", multisig: "+this.multiSig.length+", payments: "+this.payments.length+">":"<DigiAsset: type: "+this.type+", protocol: "+this.protocol+", version: "+this.version+", multisig: "+this.multiSig.length+", payments: "+this.payments.length+">"},S.prototype.toString=function(){return this.txId+":"+this.outputIndex},S.fromObject=function(t){return new S(t)},S.prototype.toObject=S.prototype.toJSON=function(){return{assetId:this.assetId,type:this.type,amount:this.amount,protocol:this.protocol,version:this.version,type:this.type,multiSig:this.multiSig,payments:this.script.payments()}},e.exports=S}).call(this,t("buffer").Buffer)},{"../address":1,"../crypto/bn":13,"../crypto/hash":15,"../encoding/base58check":20,"../opcode":31,"../script":34,"../transaction":37,"../unit":52,"../util/assets":54,"../util/buffer":55,"../util/preconditions":57,"./issuanceencoder":5,"./transferencoder":8,buffer:116,lodash:174,"node-rsa":179}],3:[function(t,e,r){(function(r){"use strict";t("../util/preconditions"),t("../util/assets");var n=t("./paymentencoder");function i(t){if(!(this instanceof i))return new i(t);this.amount=t.amount,this.output=t.output,this.range=t.range,this.burn=t.burn}i.prototype.encode=function(){if(void 0===this.output&&!this.burn)throw new Error("Needs output value or burn flag");if(void 0!==this.output&&this.burn)throw new Error("Received both burn and output");if(void 0!==this.range&&this.burn)throw new Error("Received both burn and range");if(!this.range&&31===this.output)throw new Error("Received range and output values reserved to represent burn (to indicate burn use burn flag)");var t=this;return this.burn&&((t=new n(t)).output=31,t.range=!1,t.amount=this.amount,delete t.burn),new n(t).encode(t)},i.prototype.decode=function(t){var e=(new n).decode(t);return!e.range&&31===e.output&&(e.burn=!0,delete e.output,delete e.range),new i(e)},i.prototype.encodeBulk=function(t,e){for(var n=new r(0),s=t.length,o=0;o<s;o++){var a=new i(t[o]).encode();n=r.concat([n,a])}return n},i.prototype.decodeBulk=function(t,e){for(e=e||[];;)try{e.push((new i).decode(t)),this.decodeBulk(t,e)}catch(t){return e}},e.exports=i}).call(this,t("buffer").Buffer)},{"../util/assets":54,"../util/preconditions":57,"./paymentencoder":7,buffer:116}],4:[function(t,e,r){e.exports=t("./asset"),e.exports.BurnPaymentEncoder=t("./burnpaymentencoder"),e.exports.IssuanceEncoder=t("./issuanceencoder"),e.exports.IssueFlagsEncoder=t("./issueflagsencoder"),e.exports.PaymentEncoder=t("./paymentencoder"),e.exports.TransferEncoder=t("./transferencoder")},{"./asset":2,"./burnpaymentencoder":3,"./issuanceencoder":5,"./issueflagsencoder":6,"./paymentencoder":7,"./transferencoder":8}],5:[function(t,e,r){(function(r){"use strict";var n=t("../util/preconditions"),i=t("../util/assets"),s=t("./issueflagsencoder.js"),o=t("./paymentencoder"),a=t("sffc-encoder"),u=[new r([0]),new r([1]),new r([2]),new r([3]),new r([4]),new r([5]),new r([6])];function f(t){if(!(this instanceof f))return new f(t);t&&(this.amount=t.amount,this.lockStatus=t.lockStatus,this.divisibility=t.divisibility,this.aggregationPolicy=t.aggregationPolicy,this.protocol=t.protocol,this.version=t.version,this.payments=t.payments||[],this.torrentHash=t.torrentHash,this.sha2=t.sha2,this.payments=t.payments,this.noRules=t.noRules)}f.prototype.encode=function(t){var e;n.checkState(this.amount,"Amount must be set"),n.checkState(this.lockStatus,"lockStatus must be set"),n.checkState(this.aggregationPolicy,"aggregationPolicy must be set"),n.checkState(this.protocol,"protocol must be set"),n.checkState(this.version,"version must be set");var f=new r(0),c=new r(i.padLeadingZeros(this.protocol.toString(16),2),"hex"),h=new r([this.version]),d=r.concat([c,h]),p=a.encode(this.amount),l=new r(0);this.payments&&(l=(new o).encodeBulk(this.payments));var b=new s({divisibility:this.divisibility,lockStatus:this.lockStatus,aggregationPolicy:this.aggregationPolicy}).encode(),g=r.concat([p,l,b]),y=d.length+g.length+1;if(y>t)throw new Error("Data code is bigger then the allowed byte size");if(!this.sha2){if(this.torrentHash){if(y+this.torrentHash.length>t)throw new Error("Can't fit Torrent Hash in byte size");return{codeBuffer:r.concat([d,u[4],this.torrentHash,g]),leftover:[]}}return e=this.noRules?u[5]:u[6],{codeBuffer:r.concat([d,e,f,g]),leftover:[]}}if(!this.torrentHash)throw new Error("Torrent Hash is missing");var m=[this.torrentHash,this.sha2];return e=u[3],(y+=this.torrentHash.length)<=t&&(f=r.concat([f,m.shift()]),e=u[2],y+=this.sha2.length),y<=t&&(f=r.concat([f,m.shift()]),e=u[1]),{codeBuffer:r.concat([d,e,f,g]),leftover:m}},f.prototype.decode=function(t){var e={};r.isBuffer(t)||(t=new r(t,"hex"));var n=t.length,a=t.slice(-1),c=(new s).decode(i.consumer(a));e.divisibility=c.divisibility,e.lockStatus=c.lockStatus,e.aggregationPolicy=c.aggregationPolicy;var h=i.consumer(t.slice(0,n-1));e.protocol=parseInt(h(2).toString("hex"),16),e.version=parseInt(h(1).toString("hex"),16),e.multiSig=[];var d=h(1);if(d[0]===u[1][0])e.torrentHash=h(20),e.sha2=h(32);else if(d[0]===u[2][0])e.torrentHash=h(20),e.multiSig.push({index:1,hashType:"sha2"});else if(d[0]===u[3][0])e.multiSig.push({index:1,hashType:"sha2"}),e.multiSig.push({index:2,hashType:"torrentHash"});else if(d[0]===u[4][0])e.torrentHash=h(20);else if(d[0]===u[5][0])e.noRules=!0;else{if(d[0]!==u[6][0])throw new Error("Unrecognized Code");e.noRules=!1}return e.amount=i.decodeAmountByVersion(e.version,h,e.divisibility),e.payments=(new o).decodeBulk(h),new f(e)},e.exports=f}).call(this,t("buffer").Buffer)},{"../util/assets":54,"../util/preconditions":57,"./issueflagsencoder.js":6,"./paymentencoder":7,buffer:116,"sffc-encoder":238}],6:[function(t,e,r){(function(r){"use strict";t("../util/preconditions");var n=t("../util/assets"),i=["aggregatable","hybrid","dispersed"];function s(t){if(!(this instanceof s))return new s(t);t&&(this.divisibility=t.divisibility,this.lockStatus=t.lockStatus,this.aggregationPolicy=t.aggregationPolicy||i[0])}s.prototype.encode=function(){if(this.divisibility<0||this.divisibility>7)throw new Error("Divisibility not in range");if((this.aggregationPolicy=i.indexOf(this.aggregationPolicy))<0)throw new Error("Invalid aggregation policy");var t=this.divisibility<<1,e=0;return this.lockStatus&&(e=1),t|=e,t<<=2,t|=this.aggregationPolicy,t<<=2,t=n.padLeadingZeros(t.toString(16),1),new r(t,"hex")},s.prototype.decode=function(t){var e=t(1)[0],r=i[3&(e>>=2)],n=!!(1&(e>>=2));return new s({divisibility:7&(e>>=1),lockStatus:n,aggregationPolicy:r})},e.exports=s}).call(this,t("buffer").Buffer)},{"../util/assets":54,"../util/preconditions":57,buffer:116}],7:[function(t,e,r){(function(r){"use strict";var n=t("../util/preconditions"),i=t("../util/assets"),s=t("sffc-encoder");function o(t){if(!(this instanceof o))return new o(t);t&&(this.skip=t.skip||!1,this.range=t.range||!1,this.percent=t.percent||!1,this.output=t.output,this.amount=t.amount)}o.prototype.encode=function(){n.checkState(this.amount,"Needs an amount value");var t=this.output.toString(2).length;if(this.output<0)throw new Error("Output Can't be negative");if(!this.range&&t>5||this.range&&t>13)throw new Error("Output value is out of bounds");var e=i.padLeadingZeros(this.output.toString(16),+this.range+1),o=new r(e,"hex");return this.skip&&(o[0]=128|o[0]),this.range&&(o[0]=64|o[0]),this.percent&&(o[0]=32|o[0]),r.concat([o,s.encode(this.amount)])},o.prototype.decode=function(t){var e=t(1)[0];return n.checkState(e,"No flags found"),this.output=new r([-225&e]),this.flags=224&e,this.skip=!!(128&this.flags),this.range=!!(64&this.flags),this.percent=!!(32&this.flags),this.range&&(this.output=r.concat([this.output,t(1)])),this.amount=s.decode(t),new o({skip:this.skip,range:this.range,percent:this.percent,output:parseInt(this.output.toString("hex"),16),amount:this.amount})},o.prototype.encodeBulk=function(t){for(var e=new r(0),n=t.length,i=0;i<n;i++){var s=new o(t[i]).encode();e=r.concat([e,s])}return e},o.prototype.decodeBulk=function(t,e){for(e=e||[];;)try{var r=new o;e.push(r.decode(t)),this.decodeBulk(t,e)}catch(t){return e}},e.exports=o}).call(this,t("buffer").Buffer)},{"../util/assets":54,"../util/preconditions":57,buffer:116,"sffc-encoder":238}],8:[function(t,e,r){(function(r){"use strict";var n=t("../util/preconditions"),i=t("../util/assets"),s=t("./burnpaymentencoder"),o=t("./paymentencoder"),a=[new r([16]),new r([17]),new r([18]),new r([19]),new r([20]),new r([21])],u=[new r([32]),new r([33]),new r([34]),new r([35]),new r([36]),new r([37])];function f(t){if(!(this instanceof f))return new f(t);this.payments=t.payments,this.type=t.type,this.protocol=t.protocol,this.version=t.version,this.sha2=t.sha2,this.torrentHash=t.torrentHash,this.noRules=t.noRules,this.output=t.output}f.prototype.encode=function(t){var e;n.checkState(this.payments,"Missing Data");var f="burn"===this.type?u:a,c="burn"===this.type?s:o,h=new r(0),d=new r(i.padLeadingZeros(this.protocol.toString(16),2),"hex"),p=new r([this.version]),l=r.concat([d,p]),b=new c(this).encodeBulk(this.payments),g=l.length+b.length+1;if(g>t)throw new Error("Data code is bigger then the allowed byte size");if(!this.sha2){if(this.torrentHash){if(e=this.noRules?f[4]:f[3],g+this.torrentHash.length>t)throw new Error("Can't fit Torrent Hash in byte size");return{codeBuffer:r.concat([l,e,this.torrentHash,b]),leftover:[]}}return{codeBuffer:r.concat([l,f[5],h,b]),leftover:[]}}if(!this.torrentHash)throw new Error("Torrent Hash is missing");var y=[this.torrentHash,this.sha2];return e=f[2],(g+=this.torrentHash.length)<=t&&(h=r.concat([h,y.shift()]),e=f[1],g+=this.sha2.length),g<=t&&(h=r.concat([h,y.shift()]),e=f[0]),{codeBuffer:r.concat([l,e,h,b]),leftover:y}},f.prototype.decode=function(t){var e={},r=i.consumer(t);e.protocol=parseInt(r(2).toString("hex"),16),e.version=parseInt(r(1).toString("hex"),16),e.multiSig=[];var n,c=r(1);if(16==(240&c[0]))n=o;else{if(32!=(240&c[0]))throw new Error("Unrecognized Code");n=s}if(c[0]===a[0][0]||c[0]===u[0][0])e.torrentHash=r(20),e.sha2=r(32);else if(c[0]===a[1][0]||c[0]===u[1][0])e.torrentHash=r(20),e.multiSig.push({index:1,hashType:"sha2"});else if(c[0]===a[2][0]||c[0]===u[2][0])e.multiSig.push({index:1,hashType:"sha2"}),e.multiSig.push({index:2,hashType:"torrentHash"});else if(c[0]===a[3][0]||c[0]===u[3][0])e.torrentHash=r(20),e.noRules=!1;else if(c[0]===a[4][0]||c[0]===u[4][0])e.torrentHash=r(20),e.noRules=!0;else if(c[0]!==a[5][0]&&c[0]!==u[5][0])throw new Error("Unrecognized Code");return e.payments=(new n).decodeBulk(r),new f(e)},e.exports=f}).call(this,t("buffer").Buffer)},{"../util/assets":54,"../util/preconditions":57,"./burnpaymentencoder":3,"./paymentencoder":7,buffer:116}],9:[function(t,e,r){(function(r){"use strict";var n=t("lodash"),i=t("./blockheader"),s=t("../crypto/bn"),o=t("../util/buffer"),a=t("../encoding/bufferreader"),u=t("../encoding/bufferwriter"),f=t("../crypto/hash"),c=t("../transaction"),h=t("../unit"),d=t("../util/preconditions"),p=h.fromSatoshis(1).toDGB();function l(t){return this instanceof l?(n.extend(this,l._from(t)),this):new l(t)}l.MAX_BLOCK_SIZE=1e6,l._from=function(t){var e={};if(o.isBuffer(t))e=l._fromBufferReader(a(t));else{if(!n.isObject(t))throw new TypeError("Unrecognized argument for Block");e=l._fromObject(t)}return e},l._fromObject=function(t){var e=[];return t.transactions.forEach(function(t){t instanceof c?e.push(t):e.push(c().fromObject(t))}),{header:i.fromObject(t.header),transactions:e}},l.fromObject=function(t){return new l(l._fromObject(t))},l._fromBufferReader=function(t){var e={};d.checkState(!t.finished(),"No block data received"),e.header=i.fromBufferReader(t);var r=t.readVarintNum();e.transactions=[];for(var n=0;n<r;n++)e.transactions.push(c().fromBufferReader(t));return e},l.fromBufferReader=function(t){return d.checkArgument(t,"br is required"),new l(l._fromBufferReader(t))},l.fromBuffer=function(t){return l.fromBufferReader(new a(t))},l.fromString=function(t){var e=new r(t,"hex");return l.fromBuffer(e)},l.fromRawBlock=function(t){o.isBuffer(t)||(t=new r(t,"binary"));var e=a(t);return e.pos=l.Values.START_OF_BLOCK,new l(l._fromBufferReader(e))},l.prototype.toObject=l.prototype.toJSON=function(){var t=[];return this.transactions.forEach(function(e){t.push(e.toObject())}),{header:this.header.toObject(),transactions:t}},l.prototype.toBuffer=function(){return this.toBufferWriter().concat()},l.prototype.toString=function(){return this.toBuffer().toString("hex")},l.prototype.toBufferWriter=function(t){t||(t=new u),t.write(this.header.toBuffer()),t.writeVarintNum(this.transactions.length);for(var e=0;e<this.transactions.length;e++)this.transactions[e].toBufferWriter(t);return t},l.prototype.getTransactionHashes=function(){var t=[];if(0===this.transactions.length)return[l.Values.NULL_HASH];for(var e=0;e<this.transactions.length;e++)t.push(this.transactions[e]._getHash());return t},l.prototype.getMerkleTree=function(){for(var t=this.getTransactionHashes(),e=0,n=this.transactions.length;n>1;n=Math.floor((n+1)/2)){for(var i=0;i<n;i+=2){var s=Math.min(i+1,n-1),o=r.concat([t[e+i],t[e+s]]);t.push(f.sha256sha256(o))}e+=n}return t},l.prototype.getMerkleRoot=function(){var t=this.getMerkleTree();return t[t.length-1]},l.prototype.validMerkleRoot=function(){var t=new s(this.header.merkleRoot.toString("hex"),"hex"),e=new s(this.getMerkleRoot().toString("hex"),"hex");return 0===t.cmp(e)},l.prototype._getHash=function(){return this.header._getHash()};var b={configurable:!1,enumerable:!0,get:function(){return this._id||(this._id=this.header.id),this._id},set:n.noop};Object.defineProperty(l.prototype,"id",b),Object.defineProperty(l.prototype,"hash",b),l.prototype.inspect=function(){return"<Block "+this.id+">"},l.Values={START_OF_BLOCK:8,NULL_HASH:new r("0000000000000000000000000000000000000000000000000000000000000000","hex")},l.GetDGBSubsidy=function(t){var e;if(t<4e5){e=h.fromDGB(8e3).toSatoshis();for(var r=(t-67200)/10080,n=0;n<r;n++)e-=e/200}else{e=h.fromDGB(2484).toSatoshis();for(r=(t-4e5)/80160+1,n=0;n<r;n++)e-=e/100}return e},l.getBlockValue=function(t){return t<67200?(p=h.fromDGB(8e3).toSatoshis(),t<1440?p=h.fromDGB(72e3).toSatoshis():t<5760&&(p=h.fromDGB(16e3).toSatoshis())):p=l.GetDGBSubsidy(t),p<h.fromDGB(1).toSatoshis()&&(p=h.fromDGB(1).toSatoshis()),h.fromSatoshis(p).toDGB()},e.exports=l}).call(this,t("buffer").Buffer)},{"../crypto/bn":13,"../crypto/hash":15,"../encoding/bufferreader":23,"../encoding/bufferwriter":24,"../transaction":37,"../unit":52,"../util/buffer":55,"../util/preconditions":57,"./blockheader":10,buffer:116,lodash:174}],10:[function(t,e,r){(function(r){"use strict";var n=t("lodash"),i=t("../crypto/bn"),s=t("../util/buffer"),o=t("../encoding/bufferreader"),a=t("../encoding/bufferwriter"),u=t("../crypto/hash"),f=(t("../util/js"),t("../util/preconditions")),c=function t(e){if(!(this instanceof t))return new t(e);var r=t._from(e);return this.version=r.version,this.prevHash=r.prevHash,this.merkleRoot=r.merkleRoot,this.time=r.time,this.timestamp=r.time,this.bits=r.bits,this.nonce=r.nonce,r.hash&&f.checkState(this.hash===r.hash,"Argument object hash property does not match block hash."),this};c._from=function(t){var e={};if(s.isBuffer(t))e=c._fromBufferReader(o(t));else{if(!n.isObject(t))throw new TypeError("Unrecognized argument for BlockHeader");e=c._fromObject(t)}return e},c._fromObject=function(t){f.checkArgument(t,"data is required");var e=t.prevHash,i=t.merkleRoot;return n.isString(t.prevHash)&&(e=s.reverse(new r(t.prevHash,"hex"))),n.isString(t.merkleRoot)&&(i=s.reverse(new r(t.merkleRoot,"hex"))),{hash:t.hash,version:t.version,prevHash:e,merkleRoot:i,time:t.time,timestamp:t.time,bits:t.bits,nonce:t.nonce}},c.fromObject=function(t){var e=c._fromObject(t);return new c(e)},c.fromRawBlock=function(t){s.isBuffer(t)||(t=new r(t,"binary"));var e=o(t);e.pos=c.Constants.START_OF_HEADER;var n=c._fromBufferReader(e);return new c(n)},c.fromBuffer=function(t){var e=c._fromBufferReader(o(t));return new c(e)},c.fromString=function(t){var e=new r(t,"hex");return c.fromBuffer(e)},c._fromBufferReader=function(t){var e={};return e.version=t.readInt32LE(),e.prevHash=t.read(32),e.merkleRoot=t.read(32),e.time=t.readUInt32LE(),e.bits=t.readUInt32LE(),e.nonce=t.readUInt32LE(),e},c.fromBufferReader=function(t){var e=c._fromBufferReader(t);return new c(e)},c.prototype.toObject=c.prototype.toJSON=function(){return{hash:this.hash,version:this.version,prevHash:s.reverse(this.prevHash).toString("hex"),merkleRoot:s.reverse(this.merkleRoot).toString("hex"),time:this.time,bits:this.bits,nonce:this.nonce}},c.prototype.toBuffer=function(){return this.toBufferWriter().concat()},c.prototype.toString=function(){return this.toBuffer().toString("hex")},c.prototype.toBufferWriter=function(t){return t||(t=new a),t.writeInt32LE(this.version),t.write(this.prevHash),t.write(this.merkleRoot),t.writeUInt32LE(this.time),t.writeUInt32LE(this.bits),t.writeUInt32LE(this.nonce),t},c.prototype.getTargetDifficulty=function(t){t=t||this.bits;for(var e=new i(16777215&t),r=8*((t>>>24)-3);r-- >0;)e=e.mul(new i(2));return e},c.prototype.getDifficulty=function(){var t=this.getTargetDifficulty(486604799).mul(new i(Math.pow(10,8))),e=this.getTargetDifficulty(),r=t.div(e).toString(10),n=r.length-8;return r=r.slice(0,n)+"."+r.slice(n),parseFloat(r)},c.prototype._getHash=function(){var t=this.toBuffer();return u.sha256sha256(t)};var h={configurable:!1,enumerable:!0,get:function(){return this._id||(this._id=o(this._getHash()).readReverse().toString("hex")),this._id},set:n.noop};Object.defineProperty(c.prototype,"id",h),Object.defineProperty(c.prototype,"hash",h),c.prototype.validTimestamp=function(){var t=Math.round((new Date).getTime()/1e3);return!(this.time>t+c.Constants.MAX_TIME_OFFSET)},c.prototype.validProofOfWork=function(){var t=new i(this.id,"hex"),e=this.getTargetDifficulty();return!(t.cmp(e)>0)},c.prototype.inspect=function(){return"<BlockHeader "+this.id+">"},c.Constants={START_OF_HEADER:8,MAX_TIME_OFFSET:7200,LARGEST_HASH:new i("10000000000000000000000000000000000000000000000000000000000000000","hex")},e.exports=c}).call(this,t("buffer").Buffer)},{"../crypto/bn":13,"../crypto/hash":15,"../encoding/bufferreader":23,"../encoding/bufferwriter":24,"../util/buffer":55,"../util/js":56,"../util/preconditions":57,buffer:116,lodash:174}],11:[function(t,e,r){e.exports=t("./block"),e.exports.BlockHeader=t("./blockheader"),e.exports.MerkleBlock=t("./merkleblock")},{"./block":9,"./blockheader":10,"./merkleblock":12}],12:[function(t,e,r){(function(r){"use strict";var n=t("lodash"),i=t("./blockheader"),s=t("../util/buffer"),o=t("../encoding/bufferreader"),a=t("../encoding/bufferwriter"),u=t("../crypto/hash"),f=(t("../util/js"),t("../transaction")),c=t("../util/preconditions");function h(t){if(!(this instanceof h))return new h(t);var e={};if(s.isBuffer(t))e=h._fromBufferReader(o(t));else{if(!n.isObject(t))throw new TypeError("Unrecognized argument for MerkleBlock");e={header:t.header instanceof i?t.header:i.fromObject(t.header),numTransactions:t.numTransactions,hashes:t.hashes,flags:t.flags}}return n.extend(this,e),this._flagBitsUsed=0,this._hashesUsed=0,this}h.fromBuffer=function(t){return h.fromBufferReader(o(t))},h.fromBufferReader=function(t){return new h(h._fromBufferReader(t))},h.prototype.toBuffer=function(){return this.toBufferWriter().concat()},h.prototype.toBufferWriter=function(t){t||(t=new a),t.write(this.header.toBuffer()),t.writeUInt32LE(this.numTransactions),t.writeVarintNum(this.hashes.length);for(var e=0;e<this.hashes.length;e++)t.write(new r(this.hashes[e],"hex"));for(t.writeVarintNum(this.flags.length),e=0;e<this.flags.length;e++)t.writeUInt8(this.flags[e]);return t},h.prototype.toObject=h.prototype.toJSON=function(){return{header:this.header.toObject(),numTransactions:this.numTransactions,hashes:this.hashes,flags:this.flags}},h.prototype.validMerkleTree=function(){if(c.checkState(n.isArray(this.flags),"MerkleBlock flags is not an array"),c.checkState(n.isArray(this.hashes),"MerkleBlock hashes is not an array"),this.hashes.length>this.numTransactions)return!1;if(8*this.flags.length<this.hashes.length)return!1;var t=this._calcTreeHeight(),e={hashesUsed:0,flagBitsUsed:0},r=this._traverseMerkleTree(t,0,e);return e.hashesUsed===this.hashes.length&&s.equals(r,this.header.merkleRoot)},h.prototype._traverseMerkleTree=function(t,e,n){if((n=n||{}).txs=n.txs||[],n.flagBitsUsed=n.flagBitsUsed||0,n.hashesUsed=n.hashesUsed||0,n.flagBitsUsed>8*this.flags.length)return null;var i=this.flags[n.flagBitsUsed>>3]>>>(7&n.flagBitsUsed++)&1;if(0!==t&&i){var s=this._traverseMerkleTree(t-1,2*e,n),o=s;return 2*e+1<this._calcTreeWidth(t-1)&&(o=this._traverseMerkleTree(t-1,2*e+1,n)),u.sha256sha256(new r.concat([s,o]))}if(n.hashesUsed>=this.hashes.length)return null;var a=this.hashes[n.hashesUsed++];return 0===t&&i&&n.txs.push(a),new r(a,"hex")},h.prototype._calcTreeWidth=function(t){return this.numTransactions+(1<<t)-1>>t},h.prototype._calcTreeHeight=function(){for(var t=0;this._calcTreeWidth(t)>1;)t++;return t},h.prototype.hasTransaction=function(t){c.checkArgument(!n.isUndefined(t),"tx cannot be undefined"),c.checkArgument(t instanceof f||"string"==typeof t,'Invalid tx given, tx must be a "string" or "Transaction"');var e=t;t instanceof f&&(e=s.reverse(new r(t.id,"hex")).toString("hex"));var i=[],o=this._calcTreeHeight();return this._traverseMerkleTree(o,0,{txs:i}),-1!==i.indexOf(e)},h._fromBufferReader=function(t){c.checkState(!t.finished(),"No merkleblock data received");var e={};e.header=i.fromBufferReader(t),e.numTransactions=t.readUInt32LE();var r=t.readVarintNum();e.hashes=[];for(var n=0;n<r;n++)e.hashes.push(t.read(32).toString("hex"));var s=t.readVarintNum();for(e.flags=[],n=0;n<s;n++)e.flags.push(t.readUInt8());return e},h.fromObject=function(t){return new h(t)},e.exports=h}).call(this,t("buffer").Buffer)},{"../crypto/hash":15,"../encoding/bufferreader":23,"../encoding/bufferwriter":24,"../transaction":37,"../util/buffer":55,"../util/js":56,"../util/preconditions":57,"./blockheader":10,buffer:116,lodash:174}],13:[function(t,e,r){(function(r){"use strict";var n=t("bn.js"),i=t("../util/preconditions"),s=t("lodash"),o=function(t){for(var e=new r(t.length),n=0;n<t.length;n++)e[n]=t[t.length-1-n];return e};n.Zero=new n(0),n.One=new n(1),n.Minus1=new n(-1),n.fromNumber=function(t){return i.checkArgument(s.isNumber(t)),new n(t)},n.fromString=function(t,e){return i.checkArgument(s.isString(t)),new n(t,e)},n.fromBuffer=function(t,e){void 0!==e&&"little"===e.endian&&(t=o(t));var r=t.toString("hex");return new n(r,16)},n.fromSM=function(t,e){var i;if(0===t.length)return n.fromBuffer(new r([0]));var s="big";return e&&(s=e.endian),"little"===s&&(t=o(t)),128&t[0]?(t[0]=127&t[0],(i=n.fromBuffer(t)).neg().copy(i)):i=n.fromBuffer(t),i},n.prototype.toNumber=function(){return parseInt(this.toString(10),10)},n.prototype.toBuffer=function(t){var e,i;if(t&&t.size){var s=(i=this.toString(16,2)).length/2;e=new r(i,"hex"),s===t.size?e=e:s>t.size?e=n.trim(e,s):s<t.size&&(e=n.pad(e,s,t.size))}else i=this.toString(16,2),e=new r(i,"hex");return void 0!==t&&"little"===t.endian&&(e=o(e)),e},n.prototype.toSMBigEndian=function(){var t;return-1===this.cmp(n.Zero)?128&(t=this.neg().toBuffer())[0]?t=r.concat([new r([128]),t]):t[0]=128|t[0]:128&(t=this.toBuffer())[0]&&(t=r.concat([new r([0]),t])),1===t.length&0===t[0]&&(t=new r([])),t},n.prototype.toSM=function(t){var e=t?t.endian:"big",r=this.toSMBigEndian();return"little"===e&&(r=o(r)),r},n.fromScriptNumBuffer=function(t,e,r){var s=r||4;if(i.checkArgument(t.length<=s,new Error("script number overflow")),e&&t.length>0&&0==(127&t[t.length-1])&&(t.length<=1||0==(128&t[t.length-2])))throw new Error("non-minimally encoded script number");return n.fromSM(t,{endian:"little"})},n.prototype.toScriptNumBuffer=function(){return this.toSM({endian:"little"})},n.prototype.gt=function(t){return this.cmp(t)>0},n.prototype.gte=function(t){return this.cmp(t)>=0},n.prototype.lt=function(t){return this.cmp(t)<0},n.trim=function(t,e){return t.slice(e-t.length,t.length)},n.pad=function(t,e,n){for(var i=new r(n),s=0;s<t.length;s++)i[i.length-1-s]=t[t.length-1-s];for(s=0;s<n-e;s++)i[s]=0;return i},e.exports=n}).call(this,t("buffer").Buffer)},{"../util/preconditions":57,"bn.js":83,buffer:116,lodash:174}],14:[function(t,e,r){(function(r){"use strict";var n=t("./bn"),i=t("./point"),s=t("./signature"),o=t("../publickey"),a=t("./random"),u=t("./hash"),f=t("../util/buffer"),c=t("lodash"),h=t("../util/preconditions"),d=function t(e){if(!(this instanceof t))return new t(e);e&&this.set(e)};d.prototype.set=function(t){return this.hashbuf=t.hashbuf||this.hashbuf,this.endian=t.endian||this.endian,this.privkey=t.privkey||this.privkey,this.pubkey=t.pubkey||(this.privkey?this.privkey.publicKey:this.pubkey),this.sig=t.sig||this.sig,this.k=t.k||this.k,this.verified=t.verified||this.verified,this},d.prototype.privkey2pubkey=function(){this.pubkey=this.privkey.toPublicKey()},d.prototype.calci=function(){for(var t=0;t<4;t++){var e;this.sig.i=t;try{e=this.toPublicKey()}catch(t){continue}if(e.point.eq(this.pubkey.point))return this.sig.compressed=this.pubkey.compressed,this}throw this.sig.i=void 0,new Error("Unable to find valid recovery factor")},d.fromString=function(t){var e=JSON.parse(t);return new d(e)},d.prototype.randomK=function(){var t,e=i.getN();do{t=n.fromBuffer(a.getRandomBuffer(32))}while(!t.lt(e)||!t.gt(n.Zero));return this.k=t,this},d.prototype.deterministicK=function(t){c.isUndefined(t)&&(t=0);var e=new r(32);e.fill(1);var s=new r(32);s.fill(0);var o=this.privkey.bn.toBuffer({size:32}),a="little"===this.endian?f.reverse(this.hashbuf):this.hashbuf;s=u.sha256hmac(r.concat([e,new r([0]),o,a]),s),e=u.sha256hmac(e,s),s=u.sha256hmac(r.concat([e,new r([1]),o,a]),s),e=u.sha256hmac(e,s),e=u.sha256hmac(e,s);for(var h=n.fromBuffer(e),d=i.getN(),p=0;p<t||!h.lt(d)||!h.gt(n.Zero);p++)s=u.sha256hmac(r.concat([e,new r([0])]),s),e=u.sha256hmac(e,s),e=u.sha256hmac(e,s),h=n.fromBuffer(e);return this.k=h,this},d.prototype.toPublicKey=function(){var t=this.sig.i;h.checkArgument(0===t||1===t||2===t||3===t,new Error("i must be equal to 0, 1, 2, or 3"));var e=n.fromBuffer(this.hashbuf),r=this.sig.r,s=this.sig.s,a=1&t,u=t>>1,f=i.getN(),c=i.getG(),d=u?r.add(f):r,p=i.fromX(a,d);if(!p.mul(f).isInfinity())throw new Error("nR is not a valid curve point");var l=e.neg().umod(f),b=r.invm(f),g=p.mul(s).add(c.mul(l)).mul(b);return o.fromPoint(g,this.sig.compressed)},d.prototype.sigError=function(){if(!f.isBuffer(this.hashbuf)||32!==this.hashbuf.length)return"hashbuf must be a 32 byte buffer";var t=this.sig.r,e=this.sig.s;if(!(t.gt(n.Zero)&&t.lt(i.getN())&&e.gt(n.Zero)&&e.lt(i.getN())))return"r and s not in range";var r=n.fromBuffer(this.hashbuf,this.endian?{endian:this.endian}:void 0),s=i.getN(),o=e.invm(s),a=o.mul(r).umod(s),u=o.mul(t).umod(s),c=i.getG().mulAdd(a,this.pubkey.point,u);return c.isInfinity()?"p is infinity":0!==c.getX().umod(s).cmp(t)&&"Invalid signature"},d.toLowS=function(t){return t.gt(n.fromBuffer(new r("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576