UNPKG

blocktrail-sdk

Version:

BlockTrail's Developer Friendly API binding for NodeJS

1 lines 946 kB
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).blocktrailSDK=e()}}(function(){var define,module,exports;return function(){return function e(t,r,n){function i(s,a){if(!r[s]){if(!t[s]){var c="function"==typeof require&&require;if(!a&&c)return c(s,!0);if(o)return o(s,!0);var f=new Error("Cannot find module '"+s+"'");throw f.code="MODULE_NOT_FOUND",f}var u=r[s]={exports:{}};t[s][0].call(u.exports,function(e){return i(t[s][1][e]||e)},u,u.exports,e,t,r,n)}return r[s].exports}for(var o="function"==typeof require&&require,s=0;s<n.length;s++)i(n[s]);return i}}()({1:[function(e,t,r){(function(r,Buffer){var n=e("lodash"),i=e("q"),o=e("bitcoinjs-lib"),s=e("bitcoinjs-message"),a=e("bip39"),c=e("./wallet"),f=e("./btccom.convert"),u=e("./blocktrail.convert"),h=e("./rest_client"),l=e("./encryption"),d=e("./keyderivation"),p=e("./encryption_mnemonic"),b=e("./blocktrail"),y=e("randombytes"),v=e("crypto-js"),m=e("./webworkifier");var g=e("./use-webworker")();function w(e,t){return t&&e.then(function(e){i.nextTick(function(){t(null,e)})},function(e){i.nextTick(function(){t(e,null)})}),e}var _=function(e){if(!(this instanceof _))return new _(e);var t,r=_.normalizeNetworkFromOptions(e);e.network=r[0],e.testnet=r[1],e.regtest=r[2],e.apiNetwork=e.apiNetwork||r[3],this.bitcoinCash="BCC"===e.network,this.regtest=e.regtest,this.testnet=e.testnet,this.network=(t=this).bitcoinCash?t.regtest?o.networks.bitcoincashregtest:t.testnet?o.networks.bitcoincashtestnet:o.networks.bitcoincash:t.regtest?o.networks.regtest:t.testnet?o.networks.testnet:o.networks.bitcoin,this.feeSanityCheck=void 0===e.feeSanityCheck||e.feeSanityCheck,this.feeSanityCheckBaseFeeMultiplier=e.feeSanityCheckBaseFeeMultiplier||200,e.apiNetwork=e.apiNetwork||(this.testnet?"t":"")+(e.network||"BTC").toUpperCase(),void 0===e.btccom&&(e.btccom=!0);var i=n.omit(e,"host");this.dataClient=_.initRestClient(i),this.blocktrailClient=_.initRestClient(n.merge({},e,{btccom:!1})),e.btccom?this.converter=new f(this.network,!0):this.converter=new u};_.normalizeNetworkFromOptions=function(e){var t,r="BTC",n=!1,i=!1,o=!1;if(e.network){var s=e.network.toLowerCase().match(/^([rt])?(btc|bch|bcc)$/);if(!s)throw new Error("Invalid network ["+e.network+"]");r="btc"===s[2]?"BTC":"BCC",(t=s[1])&&(o=!0,"r"===t?(n=!0,i=!0):"t"===t&&(n=!0))}return o||(e.regtest?(n=!0,i=!0,t="r"):e.testnet&&(n=!0,t="t")),[r,n,i,(t||"")+r]},_.updateHostOptions=function(e){return!e.btccom&&r.env.BLOCKTRAIL_SDK_API_ENDPOINT&&(e.host=r.env.BLOCKTRAIL_SDK_API_ENDPOINT),e.btccom&&r.env.BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT&&(e.host=r.env.BLOCKTRAIL_SDK_BTCCOM_API_ENDPOINT),e.btccom&&r.env.BLOCKTRAIL_SDK_THROTTLE_BTCCOM&&(e.throttleRequestsTimeout=r.env.BLOCKTRAIL_SDK_THROTTLE_BTCCOM),e.btccom?(e.host||(e.host=e.btccomhost||("BCC"===e.network?"bch-chain.api.btc.com":"chain.api.btc.com")),e.testnet&&!e.host.match(/tchain/)&&(e.host=e.host.replace(/chain/,"tchain")),e.endpoint||(e.endpoint=e.btccomendpoint||"/"+(e.apiVersion||"v3"))):(e.host||(e.host="wallet-api.btc.com"),e.endpoint||(e.endpoint="/"+(e.apiVersion||"v1")+(e.apiNetwork?"/"+e.apiNetwork:""))),e.host&&0===e.host.indexOf("https://")?(e.https=!0,e.host=e.host.substr(8)):e.host&&0===e.host.indexOf("http://")&&(e.https=!1,e.host=e.host.substr(7)),void 0===e.https&&(e.https=!0),e.port||(e.port=e.https?443:80),e},_.initRestClient=function(e){return e=_.updateHostOptions(e),new h(e)};var E=function(e){return i.when(e).then(function(e){return e.storePrimaryMnemonic&&(e.storeDataOnServer=e.storePrimaryMnemonic),void 0===e.storeDataOnServer&&(e.storeDataOnServer=!e.primarySeed),e})};_.prototype.promisedEncrypt=function(t,r,n){if(g&&"function"==typeof onLoadWorkerLoadAsmCrypto){var o=l.generateSalt(),s=l.generateIV();return m.workify(_.prototype.promisedEncrypt,function(){return e("./webworker")},onLoadWorkerLoadAsmCrypto,{method:"Encryption.encryptWithSaltAndIV",pt:t,pw:r,saltBuf:o,iv:s,iterations:n}).then(function(e){return Buffer.from(e.cipherText.buffer)})}try{return i.when(l.encrypt(t,r,n))}catch(e){return i.reject(e)}},_.prototype.promisedDecrypt=function(t,r){if(g&&"function"==typeof onLoadWorkerLoadAsmCrypto)return m.workify(_.prototype.promisedDecrypt,function(){return e("./webworker")},onLoadWorkerLoadAsmCrypto,{method:"Encryption.decrypt",ct:t,pw:r}).then(function(e){return Buffer.from(e.plainText.buffer)});try{return i.when(l.decrypt(t,r))}catch(e){return i.reject(e)}},_.prototype.produceEncryptedDataV3=function(e,t){var r=this;return i.when(e).then(function(e){return e.storeDataOnServer?i.when().then(function(){if(!e.secret){if(!e.passphrase)throw new b.WalletCreateError("Can't encrypt data without a passphrase");return t(_.CREATE_WALLET_PROGRESS_ENCRYPT_SECRET),e.secret=y(c.WALLET_ENTROPY_BITS/8),r.promisedEncrypt(e.secret,new Buffer(e.passphrase),d.defaultIterations).then(function(t){e.encryptedSecret=t})}if(!(e.secret instanceof Buffer))throw new Error("Secret must be a buffer")}).then(function(){return t(_.CREATE_WALLET_PROGRESS_ENCRYPT_PRIMARY),r.promisedEncrypt(e.primarySeed,e.secret,d.subkeyIterations).then(function(t){e.encryptedPrimarySeed=t})}).then(function(){if(!1!==e.recoverySecret)return t(_.CREATE_WALLET_PROGRESS_ENCRYPT_RECOVERY),e.recoverySecret||(e.recoverySecret=y(c.WALLET_ENTROPY_BITS/8)),r.promisedEncrypt(e.secret,e.recoverySecret,d.defaultIterations).then(function(t){e.recoveryEncryptedSecret=t})}).then(function(){return e}):e})};var S=function(e,t,r){return i.when(e).then(function(e){return e.backupPublicKey||(e.backupSeed=e.backupSeed||y(c.WALLET_ENTROPY_BITS/8)),r(_.CREATE_WALLET_PROGRESS_PRIMARY),e.primaryPrivateKey=o.HDNode.fromSeedBuffer(e.primarySeed,t),r(_.CREATE_WALLET_PROGRESS_BACKUP),e.backupPublicKey||(e.backupPrivateKey=o.HDNode.fromSeedBuffer(e.backupSeed,t),e.backupPublicKey=e.backupPrivateKey.neutered()),e.primaryPublicKey=e.primaryPrivateKey.deriveHardened(e.keyIndex).neutered(),r(_.CREATE_WALLET_PROGRESS_SUBMIT),e})};function k(e,t){if(void 0!==o.HDNode.fromBase58(e[0],t).keyPair.d)throw new Error("BIP32Key contained private key material - abort");if("M"!==e[1].slice(0,1))throw new Error("BIP32Key contained non-public path - abort")}function A(e,t){k(e.primary_public_key,t),k(e.backup_public_key,t)}_.prototype.mnemonicToPrivateKey=function(e,t,r){var n=this,s=i.defer();return s.promise.spreadNodeify(r),s.resolve(i.fcall(function(){return n.mnemonicToSeedHex(e,t).then(function(e){return o.HDNode.fromSeedHex(e,n.network)})})),s.promise},_.prototype.mnemonicToSeedHex=function(t,r){if(g)return m.workify(this.mnemonicToSeedHex,function(){return e("./webworker")},{method:"mnemonicToSeedHex",mnemonic:t,passphrase:r}).then(function(e){return e.seed});try{return i.when(a.mnemonicToSeedHex(t,r))}catch(e){return i.reject(e)}},_.prototype.resolvePrimaryPrivateKeyFromOptions=function(e,t){var r=this,n=i.defer();n.promise.nodeify(t);try{if(e.passphrase&&e.password)throw new b.WalletCreateError("Can't specify passphrase and password");if(e.passphrase=e.passphrase||e.password,delete e.password,e.primaryMnemonic&&e.primarySeed)throw new b.WalletInitError("Can only specify one of; Primary Mnemonic or Primary Seed");if(e.primaryPrivateKey)throw new b.WalletInitError("Can't specify; Primary PrivateKey");if(!e.primaryMnemonic&&!e.primarySeed)throw new b.WalletInitError("Need to specify at least one of; Primary Mnemonic or Primary Seed");if(e.primarySeed)r.primarySeed=e.primarySeed,e.primaryPrivateKey=o.HDNode.fromSeedBuffer(r.primarySeed,r.network),n.resolve(e);else{if(!e.passphrase)throw new b.WalletInitError("Can't init wallet with Primary Mnemonic without a passphrase");r.mnemonicToSeedHex(e.primaryMnemonic,e.passphrase).then(function(t){try{e.primarySeed=new Buffer(t,"hex"),e.primaryPrivateKey=o.HDNode.fromSeedBuffer(e.primarySeed,r.network),n.resolve(e)}catch(e){n.reject(e)}},function(e){n.reject(e)})}}catch(e){n.reject(e)}return n.promise},_.prototype.resolveBackupPublicKeyFromOptions=function(e,t){var r=i.defer();r.promise.nodeify(t);try{if(e.backupMnemonic&&e.backupPublicKey)throw new b.WalletInitError("Can only specify one of; Backup Mnemonic or Backup PublicKey");if(!e.backupMnemonic&&!e.backupPublicKey)throw new b.WalletInitError("Need to specify at least one of; Backup Mnemonic or Backup PublicKey");e.backupPublicKey?e.backupPublicKey instanceof o.HDNode?r.resolve(e):(e.backupPublicKey=o.HDNode.fromBase58(e.backupPublicKey,this.network),r.resolve(e)):this.mnemonicToPrivateKey(e.backupMnemonic,"").then(function(t){e.backupPublicKey=t.neutered(),r.resolve(e)},function(e){r.reject(e)})}catch(e){r.reject(e)}return r.promise},_.prototype.debugAuth=function(e){return this.dataClient.get("/debug/http-signature",null,!0,e)},_.prototype.address=function(e,t){var r=this;return w(r.dataClient.get(r.converter.getUrlForAddress(e),null).then(function(e){return r.converter.handleErrors(r,e)}).then(function(e){return null===e?e:r.converter.convertAddress(e)}),t)},_.prototype.addresses=function(e,t){return w(this.dataClient.post("/address",null,{addresses:e}),t)},_.prototype.addressTransactions=function(e,t,r){var n=this;return"function"==typeof t&&(r=t,t=null),w(n.dataClient.get(n.converter.getUrlForAddressTransactions(e),n.converter.paginationParams(t)).then(function(e){return n.converter.handleErrors(n,e)}).then(function(e){return null===e.data?e:n.converter.convertAddressTxs(e)}),r)},_.prototype.batchAddressHasTransactions=function(e,t,r){var n=this;"function"==typeof t&&(r=t,t=null);var o=i.defer(),s=i();return e.forEach(function(e){s=s.then(function(r){return r||i(e).then(function(e){return console.log(e),n.addressTransactions(e,t).then(function(e){if(1===e.err_no)return!1;if(e.err_no)throw new Error("err: "+e.err_msg);return e.data&&e.data.length>0})})})}),s.then(function(e){o.resolve({has_transactions:e})},function(e){o.reject(e)}),w(o.promise,r)},_.prototype.addressUnconfirmedTransactions=function(e,t,r){var n=this;return"function"==typeof t&&(r=t,t=null),w(n.dataClient.get(n.converter.getUrlForAddressTransactions(e),n.converter.paginationParams(t)).then(function(e){return n.converter.handleErrors(n,e)}).then(function(e){if(null===e.data)return e;var t=n.converter.convertAddressTxs(e);return t.data=t.data.filter(function(e){return!e.confirmations}),t}),r)},_.prototype.addressUnspentOutputs=function(e,t,r){var n=this;return"function"==typeof t&&(r=t,t=null),w(n.dataClient.get(n.converter.getUrlForAddressUnspent(e),n.converter.paginationParams(t)).then(function(e){return n.converter.handleErrors(n,e)}).then(function(t){return null===t.data?t:n.converter.convertAddressUnspentOutputs(t,e)}),r)},_.prototype.batchAddressUnspentOutputs=function(e,t,r){var n=this;return n.converter instanceof f?w(n.dataClient.get(n.converter.getUrlForBatchAddressUnspent(e),n.converter.paginationParams(t)).then(function(e){return n.converter.handleErrors(n,e)}).then(function(e){return null===e.data?e:n.converter.convertBatchAddressUnspentOutputs(e)}),r):("function"==typeof t&&(r=t,t=null),w(n.dataClient.post("/address/unspent-outputs",t,{addresses:e}),r))},_.prototype.verifyAddress=function(e,t,r){return this.verifyMessage(e,e,t,r)},_.prototype.allBlocks=function(e,t){var r=this;return"function"==typeof e&&(t=e,e=null),w(r.dataClient.get(r.converter.getUrlForAllBlocks(),r.converter.paginationParams(e)).then(function(e){return r.converter.handleErrors(r,e)}).then(function(e){return null===e.data?e:r.converter.convertBlocks(e)}),t)},_.prototype.block=function(e,t){var r=this;return w(r.dataClient.get(r.converter.getUrlForBlock(e),null).then(function(e){return r.converter.handleErrors(r,e)}).then(function(e){return null===e.data?e:r.converter.convertBlock(e.data)}),t)},_.prototype.blockLatest=function(e){var t=this;return w(t.dataClient.get(t.converter.getUrlForBlock("latest"),null).then(function(e){return t.converter.handleErrors(t,e)}).then(function(e){return null===e.data?e:t.converter.convertBlock(e.data)}),e)},_.prototype.blockTransactions=function(e,t,r){var n=this;return"function"==typeof t&&(r=t,t=null),w(n.dataClient.get(n.converter.getUrlForBlockTransaction(e),n.converter.paginationParams(t)).then(function(e){return n.converter.handleErrors(n,e)}).then(function(e){return null===e.data?e:n.converter.convertBlockTxs(e)}),r)},_.prototype.transaction=function(e,t){var r=this;return w(r.dataClient.get(r.converter.getUrlForTransaction(e),null).then(function(e){return r.converter.handleErrors(r,e)}).then(function(t){return null===t.data?t:r.converter instanceof f?r.dataClient.get(r.converter.getUrlForRawTransaction(e),null).then(function(e){return[t,e.data]}).then(function(e){if(null!==e){var t=e[0],n=e[1];return r.converter.convertTx(t,n)}return e}):r.converter.convertTx(t)}),t)},_.prototype.transactions=function(e,t){var r=this;return r.converter instanceof f?w(r.dataClient.get(r.converter.getUrlForTransactions(e),null).then(function(e){return r.converter.handleErrors(r,e)}).then(function(e){return null===e.data?e:r.converter.convertTxs(e)}),t):w(r.dataClient.post("/transactions",null,e,null,!1),t)},_.prototype.allWebhooks=function(e,t){return"function"==typeof e&&(t=e,e=null),this.blocktrailClient.get("/webhooks",e,t)},_.prototype.setupWebhook=function(e,t,r){return"function"==typeof t&&(r=t,t=null),this.blocktrailClient.post("/webhook",null,{url:e,identifier:t},r)},_.prototype.getLegacyBitcoinCashAddress=function(e){if(this.network===o.networks.bitcoincash||this.network===o.networks.bitcoincashtestnet||this.network===o.networks.bitcoincashregtest){var t,r;try{return o.address.fromBase58Check(e,this.network),e}catch(e){}if((t=o.address.fromCashAddress(e,this.network)).version===o.script.types.P2PKH)r=this.network.pubKeyHash;else{if(t.version!==o.script.types.P2SH)throw new Error("Unsupported address type");r=this.network.scriptHash}return o.address.toBase58Check(t.hash,r)}throw new Error("Cash addresses only work on bitcoin cash")},_.prototype.getCashAddressFromLegacyAddress=function(e){if(this.network===o.networks.bitcoincash||this.network===o.networks.bitcoincashtestnet||this.network===o.networks.bitcoincashregtest){var t,r;try{return o.address.fromCashAddress(e,this.network),e}catch(e){}if((t=o.address.fromBase58Check(e,this.network)).version===this.network.pubKeyHash)r=o.script.types.P2PKH;else{if(t.version!==this.network.scriptHash)throw new Error("Unsupported address type");r=o.script.types.P2SH}return o.address.toCashAddress(t.hash,r,this.network.cashAddrPrefix)}throw new Error("Cash addresses only work on bitcoin cash")},_.prototype.getWebhook=function(e,t){return this.blocktrailClient.get("/webhook/"+e,null,t)},_.prototype.updateWebhook=function(e,t,r){return this.blocktrailClient.put("/webhook/"+e,null,t,r)},_.prototype.deleteWebhook=function(e,t){return this.blocktrailClient.delete("/webhook/"+e,null,null,t)},_.prototype.getWebhookEvents=function(e,t,r){return"function"==typeof t&&(r=t,t=null),this.blocktrailClient.get("/webhook/"+e+"/events",t,r)},_.prototype.subscribeTransaction=function(e,t,r,n){var i={event_type:"transaction",transaction:t,confirmations:r};return this.blocktrailClient.post("/webhook/"+e+"/events",null,i,n)},_.prototype.subscribeAddressTransactions=function(e,t,r,n){var i={event_type:"address-transactions",address:t,confirmations:r};return this.blocktrailClient.post("/webhook/"+e+"/events",null,i,n)},_.prototype.batchSubscribeAddressTransactions=function(e,t,r){return t.forEach(function(e){e.event_type="address-transactions"}),this.blocktrailClient.post("/webhook/"+e+"/events/batch",null,t,r)},_.prototype.subscribeNewBlocks=function(e,t){return this.blocktrailClient.post("/webhook/"+e+"/events",null,{event_type:"block"},t)},_.prototype.unsubscribeAddressTransactions=function(e,t,r){return this.blocktrailClient.delete("/webhook/"+e+"/address-transactions/"+t,null,null,r)},_.prototype.unsubscribeTransaction=function(e,t,r){return this.blocktrailClient.delete("/webhook/"+e+"/transaction/"+t,null,null,r)},_.prototype.unsubscribeNewBlocks=function(e,t){return this.blocktrailClient.delete("/webhook/"+e+"/block",null,null,t)},_.prototype.getWalletLatestBlock=function(e){return this.blocktrailClient.get("/block/latest",null,e)},_.prototype.initWallet=function(e,t){var r=this;if("object"!=typeof e&&(e={identifier:arguments[0],passphrase:arguments[1]},t=arguments[2]),e.check_backup_key&&"string"!=typeof e.check_backup_key)throw new Error("Invalid input, must provide the backup key as a string (the xpub)");var s=i.defer();s.promise.spreadNodeify(t);var a=e.identifier;return a?(s.resolve(r.blocktrailClient.get("/wallet/"+a,null,!0).then(function(t){var i=e.keyIndex||t.key_index;if(e.walletVersion=t.wallet_version,e.check_backup_key&&e.check_backup_key!==t.backup_public_key[0])throw new Error("Backup key returned from server didn't match our own copy");var s=o.HDNode.fromBase58(t.backup_public_key[0],r.network),f=n.mapValues(t.blocktrail_public_keys,function(e){return o.HDNode.fromBase58(e[0],r.network)}),u=n.mapValues(t.primary_public_keys,function(e){return o.HDNode.fromBase58(e[0],r.network)}),h=new c(r,a,e.walletVersion,t.primary_mnemonic,t.encrypted_primary_seed,t.encrypted_secret,u,s,f,i,t.segwit||0,r.testnet,r.regtest,t.checksum,t.upgrade_key_index,e.useCashAddress,e.bypassNewAddressCheck);return h.recoverySecret=t.recovery_secret,e.readOnly?h:h.unlock(e).then(function(){return h})})),s.promise):(s.reject(new b.WalletInitError("Identifier is required")),s.promise)},_.CREATE_WALLET_PROGRESS_START=0,_.CREATE_WALLET_PROGRESS_ENCRYPT_SECRET=4,_.CREATE_WALLET_PROGRESS_ENCRYPT_PRIMARY=5,_.CREATE_WALLET_PROGRESS_ENCRYPT_RECOVERY=6,_.CREATE_WALLET_PROGRESS_PRIMARY=10,_.CREATE_WALLET_PROGRESS_BACKUP=20,_.CREATE_WALLET_PROGRESS_SUBMIT=30,_.CREATE_WALLET_PROGRESS_INIT=40,_.CREATE_WALLET_PROGRESS_DONE=100,_.prototype.createNewWallet=function(e,t){var r=this;if("object"!=typeof e){var n=arguments[0],o=arguments[1],s=arguments[2];t=arguments[3],"function"==typeof s&&(t=s,s=null),e={identifier:n,passphrase:o,keyIndex:s}}e.walletVersion=e.walletVersion||c.WALLET_VERSION_V3;var a=i.defer();return a.promise.spreadNodeify(t),i.nextTick(function(){if(a.notify(_.CREATE_WALLET_PROGRESS_START),e.keyIndex=e.keyIndex||0,e.passphrase=e.passphrase||e.password,delete e.password,!e.identifier)return a.reject(new b.WalletCreateError("Identifier is required")),a.promise;e.walletVersion===c.WALLET_VERSION_V1?r._createNewWalletV1(e).progress(function(e){a.notify(e)}).then(function(e){a.resolve(e)},function(e){a.reject(e)}):e.walletVersion===c.WALLET_VERSION_V2?r._createNewWalletV2(e).progress(function(e){a.notify(e)}).then(function(e){a.resolve(e)},function(e){a.reject(e)}):e.walletVersion===c.WALLET_VERSION_V3?r._createNewWalletV3(e).progress(function(e){a.notify(e)}).then(function(e){a.resolve(e)},function(e){a.reject(e)}):a.reject(new b.WalletCreateError("Invalid wallet version!"))}),a.promise},_.prototype._createNewWalletV1=function(e){var t=this,r=i.defer();return i.nextTick(function(){if(!e.primaryMnemonic&&!e.primarySeed){if(!e.passphrase&&!e.password)return r.reject(new b.WalletCreateError("Can't generate Primary Mnemonic without a passphrase")),r.promise;e.primaryMnemonic=a.generateMnemonic(c.WALLET_ENTROPY_BITS),!1!==e.storePrimaryMnemonic&&(e.storePrimaryMnemonic=!0)}e.backupMnemonic||e.backupPublicKey||(e.backupMnemonic=a.generateMnemonic(c.WALLET_ENTROPY_BITS)),r.notify(_.CREATE_WALLET_PROGRESS_PRIMARY),t.resolvePrimaryPrivateKeyFromOptions(e).then(function(e){return r.notify(_.CREATE_WALLET_PROGRESS_BACKUP),t.resolveBackupPublicKeyFromOptions(e).then(function(e){r.notify(_.CREATE_WALLET_PROGRESS_SUBMIT);var i=o.crypto.hash160(e.primaryPrivateKey.getPublicKeyBuffer()),s=o.address.toBase58Check(i,t.network.pubKeyHash),a=e.keyIndex,f=e.primaryPrivateKey.deriveHardened(a).neutered();return t.storeNewWalletV1(e.identifier,[f.toBase58(),"M/"+a+"'"],[e.backupPublicKey.toBase58(),"M"],!!e.storePrimaryMnemonic&&e.primaryMnemonic,s,a,e.segwit||null).then(function(i){r.notify(_.CREATE_WALLET_PROGRESS_INIT);var u=n.mapValues(i.blocktrail_public_keys,function(e){return o.HDNode.fromBase58(e[0],t.network)}),h=new c(t,e.identifier,c.WALLET_VERSION_V1,e.primaryMnemonic,null,null,{keyIndex:f},e.backupPublicKey,u,a,i.segwit||0,t.testnet,t.regtest,s,i.upgrade_key_index,e.useCashAddress,e.bypassNewAddressCheck);return h.unlock({walletVersion:c.WALLET_VERSION_V1,passphrase:e.passphrase,primarySeed:e.primarySeed,primaryMnemonic:null}).then(function(){return r.notify(_.CREATE_WALLET_PROGRESS_DONE),[h,{walletVersion:h.walletVersion,primaryMnemonic:e.primaryMnemonic,backupMnemonic:e.backupMnemonic,blocktrailPublicKeys:u}]})})})}).then(function(e){r.resolve(e)},function(e){r.reject(e)})}),r.promise},_.prototype._createNewWalletV2=function(e){var t=this,r=i.defer();return e=n.merge({},e),E(e).then(function(e){if(e.passphrase=e.passphrase||e.password,delete e.password,e.primaryPrivateKey)throw new b.WalletInitError("Can't specify; Primary PrivateKey");return e.primarySeed=e.primarySeed||y(c.WALLET_ENTROPY_BITS/8),e}).then(function(e){return function(e,t){return i.when(e).then(function(e){if(e.storeDataOnServer){if(!e.secret){if(!e.passphrase)throw new b.WalletCreateError("Can't encrypt data without a passphrase");t(_.CREATE_WALLET_PROGRESS_ENCRYPT_SECRET),e.secret=y(c.WALLET_ENTROPY_BITS/8).toString("hex"),e.encryptedSecret=v.AES.encrypt(e.secret,e.passphrase).toString(v.format.OpenSSL)}t(_.CREATE_WALLET_PROGRESS_ENCRYPT_PRIMARY),e.encryptedPrimarySeed=v.AES.encrypt(e.primarySeed.toString("base64"),e.secret).toString(v.format.OpenSSL),e.recoverySecret=y(c.WALLET_ENTROPY_BITS/8).toString("hex"),t(_.CREATE_WALLET_PROGRESS_ENCRYPT_RECOVERY),e.recoveryEncryptedSecret=v.AES.encrypt(e.secret,e.recoverySecret).toString(v.format.OpenSSL)}return e})}(e,r.notify.bind(r))}).then(function(e){return S(e,t.network,r.notify.bind(r))}).then(function(e){var i=o.crypto.hash160(e.primaryPrivateKey.getPublicKeyBuffer()),s=o.address.toBase58Check(i,t.network.pubKeyHash),f=e.keyIndex;return t.storeNewWalletV2(e.identifier,[e.primaryPublicKey.toBase58(),"M/"+f+"'"],[e.backupPublicKey.toBase58(),"M"],!!e.storeDataOnServer&&e.encryptedPrimarySeed,!!e.storeDataOnServer&&e.encryptedSecret,!!e.storeDataOnServer&&e.recoverySecret,s,f,e.support_secret||null,e.segwit||null).then(function(i){r.notify(_.CREATE_WALLET_PROGRESS_INIT);var u=n.mapValues(i.blocktrail_public_keys,function(e){return o.HDNode.fromBase58(e[0],t.network)}),h=new c(t,e.identifier,c.WALLET_VERSION_V2,null,e.storeDataOnServer?e.encryptedPrimarySeed:null,e.storeDataOnServer?e.encryptedSecret:null,{keyIndex:e.primaryPublicKey},e.backupPublicKey,u,f,i.segwit||0,t.testnet,t.regtest,s,i.upgrade_key_index,e.useCashAddress,e.bypassNewAddressCheck);return h.unlock({walletVersion:c.WALLET_VERSION_V2,passphrase:e.passphrase,primarySeed:e.primarySeed,secret:e.secret}).then(function(){return r.notify(_.CREATE_WALLET_PROGRESS_DONE),[h,{walletVersion:h.walletVersion,encryptedPrimarySeed:e.encryptedPrimarySeed?a.entropyToMnemonic(b.convert(e.encryptedPrimarySeed,"base64","hex")):null,backupSeed:e.backupSeed?a.entropyToMnemonic(e.backupSeed.toString("hex")):null,recoveryEncryptedSecret:e.recoveryEncryptedSecret?a.entropyToMnemonic(b.convert(e.recoveryEncryptedSecret,"base64","hex")):null,encryptedSecret:e.encryptedSecret?a.entropyToMnemonic(b.convert(e.encryptedSecret,"base64","hex")):null,blocktrailPublicKeys:u}]})})}).then(function(e){r.resolve(e)},function(e){r.reject(e)}),r.promise},_.prototype._createNewWalletV3=function(e){var t=this,r=i.defer();return e=n.merge({},e),E(e).then(function(e){if(e.passphrase=e.passphrase||e.password,delete e.password,e.primaryPrivateKey)throw new b.WalletInitError("Can't specify; Primary PrivateKey");return e.primarySeed=e.primarySeed||y(c.WALLET_ENTROPY_BITS/8),e}).then(function(e){return t.produceEncryptedDataV3(e,r.notify.bind(r))}).then(function(e){return S(e,t.network,r.notify.bind(r))}).then(function(e){var i=o.crypto.hash160(e.primaryPrivateKey.getPublicKeyBuffer()),s=o.address.toBase58Check(i,t.network.pubKeyHash),f=e.keyIndex;return t.storeNewWalletV3(e.identifier,[e.primaryPublicKey.toBase58(),"M/"+f+"'"],[e.backupPublicKey.toBase58(),"M"],!!e.storeDataOnServer&&e.encryptedPrimarySeed,!!e.storeDataOnServer&&e.encryptedSecret,!!e.storeDataOnServer&&e.recoverySecret,s,f,e.support_secret||null,e.segwit||null).then(function(i){r.notify(_.CREATE_WALLET_PROGRESS_INIT);var u=n.mapValues(i.blocktrail_public_keys,function(e){return o.HDNode.fromBase58(e[0],t.network)}),h=new c(t,e.identifier,c.WALLET_VERSION_V3,null,e.storeDataOnServer?e.encryptedPrimarySeed:null,e.storeDataOnServer?e.encryptedSecret:null,{keyIndex:e.primaryPublicKey},e.backupPublicKey,u,f,i.segwit||0,t.testnet,t.regtest,s,i.upgrade_key_index,e.useCashAddress,e.bypassNewAddressCheck);return h.unlock({walletVersion:c.WALLET_VERSION_V3,passphrase:e.passphrase,primarySeed:e.primarySeed,secret:e.secret}).then(function(){return r.notify(_.CREATE_WALLET_PROGRESS_DONE),[h,{walletVersion:h.walletVersion,encryptedPrimarySeed:e.encryptedPrimarySeed?p.encode(e.encryptedPrimarySeed):null,backupSeed:e.backupSeed?a.entropyToMnemonic(e.backupSeed):null,recoveryEncryptedSecret:e.recoveryEncryptedSecret?p.encode(e.recoveryEncryptedSecret):null,encryptedSecret:e.encryptedSecret?p.encode(e.encryptedSecret):null,blocktrailPublicKeys:u}]})})}).then(function(e){r.resolve(e)},function(e){r.reject(e)}),r.promise},_.prototype.storeNewWalletV1=function(e,t,r,n,i,o,s){var a={identifier:e,wallet_version:c.WALLET_VERSION_V1,primary_public_key:t,backup_public_key:r,primary_mnemonic:n,checksum:i,key_index:o,segwit:s};return A(a,this.network),this.blocktrailClient.post("/wallet",null,a)},_.prototype.storeNewWalletV2=function(e,t,r,n,i,o,s,a,f,u){var h={identifier:e,wallet_version:c.WALLET_VERSION_V2,primary_public_key:t,backup_public_key:r,encrypted_primary_seed:n,encrypted_secret:i,recovery_secret:o,checksum:s,key_index:a,support_secret:f||null,segwit:u};return A(h,this.network),this.blocktrailClient.post("/wallet",null,h)},_.prototype.storeNewWalletV3=function(e,t,r,n,i,o,s,a,f,u){var h={identifier:e,wallet_version:c.WALLET_VERSION_V3,primary_public_key:t,backup_public_key:r,encrypted_primary_seed:n.toString("base64"),encrypted_secret:i.toString("base64"),recovery_secret:o.toString("hex"),checksum:s,key_index:a,support_secret:f||null,segwit:u};return A(h,this.network),this.blocktrailClient.post("/wallet",null,h)},_.prototype.updateWallet=function(e,t,r){return this.blocktrailClient.post("/wallet/"+e,null,t,r)},_.prototype.upgradeKeyIndex=function(e,t,r,n){return this.blocktrailClient.post("/wallet/"+e+"/upgrade",null,{key_index:t,primary_public_key:r},n)},_.prototype.getWalletBalance=function(e,t){return this.blocktrailClient.get("/wallet/"+e+"/balance",null,!0,t)},_.prototype.doWalletDiscovery=function(e,t,r){return this.blocktrailClient.get("/wallet/"+e+"/discovery",{gap:t},!0,r)},_.prototype.getNewDerivation=function(e,t,r){return this.blocktrailClient.post("/wallet/"+e+"/path",null,{path:t},r)},_.prototype.deleteWallet=function(e,t,r,n,i){return"function"==typeof n&&(i=n,n=!1),this.blocktrailClient.delete("/wallet/"+e,{force:n},{checksum:t,signature:r},i)},_.prototype.coinSelection=function(e,t,r,n,o,s,a){"function"==typeof o?(a=o,o=null,s={}):"function"==typeof s&&(a=s,s={}),o=o||c.FEE_STRATEGY_OPTIMAL,s=s||{};var f=i.defer();f.promise.spreadNodeify(a);var u={lock:r,zeroconf:n?1:0,zeroconfself:void 0===s.allowZeroConfSelf||s.allowZeroConfSelf?1:0,fee_strategy:o};return s.forcefee&&(u.forcefee=s.forcefee),f.resolve(this.blocktrailClient.post("/wallet/"+e+"/coin-selection",u,t).then(function(e){return[e.utxos,e.fee,e.change,e]},function(e){if(e.message.match(/too low to pay the fee/))throw b.WalletFeeError(e);throw e})),f.promise},_.prototype.feePerKB=function(e){var t=i.defer();return t.promise.spreadNodeify(e),t.resolve(this.blocktrailClient.get("/fee-per-kb")),t.promise},_.prototype.sendTransaction=function(e,t,r,n,i,o,s,a){"function"==typeof i?(a=i,i=null,o=!1):"function"==typeof o?(a=o,o=!1):"function"==typeof s&&(a=s,s={});var c={paths:r,two_factor_token:i};"string"==typeof t?c.raw_transaction=t:"object"==typeof t&&Object.keys(t).map(function(e){c[e]=t[e]});var f={check_fee:n?1:0,prioboost:o?1:0};return s.bip70PaymentUrl&&(f.bip70PaymentUrl=s.bip70PaymentUrl,s.bip70MerchantData&&s.bip70MerchantData instanceof Uint8Array&&(f.bip70MerchantData=Buffer.from(Array.prototype.map.call(s.bip70MerchantData,function(e){return String.fromCharCode(e)}).join("")).toString("base64"))),this.blocktrailClient.post("/wallet/"+e+"/send",f,c,a)},_.prototype.setupWalletWebhook=function(e,t,r,n){return this.blocktrailClient.post("/wallet/"+e+"/webhook",null,{url:r,identifier:t},n)},_.prototype.deleteWalletWebhook=function(e,t,r){return this.blocktrailClient.delete("/wallet/"+e+"/webhook/"+t,null,null,r)},_.prototype.walletTransactions=function(e,t,r){return"function"==typeof t&&(r=t,t=null),this.blocktrailClient.get("/wallet/"+e+"/transactions",t,!0,r)},_.prototype.walletAddresses=function(e,t,r){return"function"==typeof t&&(r=t,t=null),this.blocktrailClient.get("/wallet/"+e+"/addresses",t,!0,r)},_.prototype.labelWalletAddress=function(e,t,r,n){return this.blocktrailClient.post("/wallet/"+e+"/address/"+t+"/label",null,{label:r},n)},_.prototype.walletMaxSpendable=function(e,t,r,n,i){"function"==typeof r?(i=r,r=null):"function"==typeof n&&(i=n,n={}),r=r||c.FEE_STRATEGY_OPTIMAL;var o={outputs:(n=n||{}).outputs?n.outputs:1,zeroconf:t?1:0,zeroconfself:void 0===n.allowZeroConfSelf||n.allowZeroConfSelf?1:0,fee_strategy:r};return n.forcefee&&(o.forcefee=n.forcefee),this.blocktrailClient.get("/wallet/"+e+"/max-spendable",o,!0,i)},_.prototype.walletUTXOs=function(e,t,r){return"function"==typeof t&&(r=t,t=null),this.blocktrailClient.get("/wallet/"+e+"/utxos",t,!0,r)},_.prototype.allWallets=function(e,t){return"function"==typeof e&&(t=e,e=null),this.blocktrailClient.get("/wallets",e,!0,t)},_.prototype.verifyMessage=function(e,t,r,n){var o=i.defer();o.promise.nodeify(n);try{var a=s.verify(t,this.network.messagePrefix,e,new Buffer(r,"base64"));o.resolve(a)}catch(e){o.reject(e)}return o.promise},_.prototype.faucetWithdrawl=function(e,t,r){return this.blocktrailClient.post("/faucet/withdrawl",null,{address:e,amount:t},r)},_.prototype.sendRawTransaction=function(e,t){return this.blocktrailClient.post("/send-raw-tx",null,e,t)},_.prototype.price=function(e){return this.blocktrailClient.get("/price",null,!1,e)},t.exports=_}).call(this,e("_process"),e("buffer").Buffer)},{"./blocktrail":3,"./blocktrail.convert":2,"./btccom.convert":4,"./encryption":5,"./encryption_mnemonic":6,"./keyderivation":7,"./rest_client":11,"./use-webworker":17,"./wallet":18,"./webworker":20,"./webworkifier":21,_process:250,bip39:34,"bitcoinjs-lib":49,"bitcoinjs-message":78,buffer:111,"crypto-js":158,lodash:241,q:278,randombytes:282}],2:[function(e,t,r){var n=function(){};n.prototype.paginationParams=function(e){return e},n.prototype.getUrlForBlock=function(e){return"/block/"+e},n.prototype.getUrlForTransaction=function(e){return"/transaction/"+e},n.prototype.getUrlForBlockTransaction=function(e){return"/block/"+e+"/transactions"},n.prototype.getUrlForAddress=function(e){return"/address/"+e},n.prototype.getUrlForAddressTransactions=function(e){return"/address/"+e+"/transactions"},n.prototype.getUrlForAddressUnspent=function(e){return"/address/"+e+"/unspent-outputs"},n.prototype.convertBlock=function(e){return e},n.prototype.convertBlockTxs=function(e){return e},n.prototype.convertTx=function(e){return e},n.prototype.convertAddressTxs=function(e){return e},n.prototype.convertAddress=function(e){return e},n.prototype.convertAddressUnspentOutputs=function(e){return e},n.prototype.convertBatchAddressUnspentOutputs=function(e){return e},n.prototype.getUrlForAllBlocks=function(){return"/all-blocks"},n.prototype.handleErrors=function(e,t){return t},t.exports=n},{}],3:[function(e,t,r){(function(Buffer){var r=e("util"),n=e("assert"),i=e("crypto-js"),o=e("bip39"),s={COIN:1e8,PRECISION:8,DUST:2730,BASE_FEE:1e4,LOCK_TIME_TIMESTAMP_THRESHOLD:5e6},a=function(e,t,r){return new Buffer(e,t).toString(r)},c=function(e,t){return function(e,t){var r=o.mnemonicToEntropy(e),n=a(r,"hex","base64"),c=i.AES.decrypt(n,t).toString(i.enc.Utf8);if(!c.length)throw new s.WalletDecryptError;return c}(e,t).toString(i.enc.Utf8)},f=function(e,t){return a(c(e,t),"base64","hex")},u=function(e,t){var r=i.AES.encrypt(e,t).toString(i.format.OpenSSL),n=a(r,"base64","hex");return o.entropyToMnemonic(n)};s.convert=a,s.aesDecryptMnemonicToSeed=c,s.aesDecryptMnemonicToSeedBuffer=function(e,t){return new Buffer(f(e,t),"hex")},s.aesDecryptMnemonicToSeedHex=f,s.aesEncryptSeedToMnemonic=u,s.aesEncryptSeedHexToMnemonic=function(e,t){return u(a(e,"hex","base64"),t)},s.aesEncryptSeedBufferToMnemonic=function(e,t){return u(e.toString("base64"),t)},s.V3Crypt={KeyDerivation:e("./keyderivation"),Encryption:e("./encryption"),EncryptionMnemonic:e("./encryption_mnemonic")},s.toSatoshi=function(e){return parseInt((e*s.COIN).toFixed(0),10)},s.toBTC=function(e){return(e/s.COIN).toFixed(s.PRECISION)},s.patchQ=function(e){e.spreadNodeify&&e.spreadDone||(e.spreadDone=function(t,r,n){return e(t).spreadDone(r,n)},e.makePromise.prototype.spreadDone=function(e,t){return this.all().done(function(t){return e.apply(void 0,t)},t)},e.spreadNodeify=function(t,r){return e(t).spreadNodeify(r)},e.makePromise.prototype.spreadNodeify=function(t){if(!t)return this;this.then(function(r){e.nextTick(function(){t.apply(void 0,[null].concat(r))})},function(r){e.nextTick(function(){t(r)})})})},Error.extend=function(e,t){n(e,"subTypeName is required");var i=function(r){if(!(this instanceof i))return new i(r);this.name=e,this.code=t,this.message=r&&(r.message||r)||"",Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)};return r.inherits(i,this),i.prototype.toString=function(){return this.name+": "+r.inspect(this.message)},i.extend=this.extend,i},"function"!=typeof Uint8Array.prototype.reverse&&(Buffer.prototype.reverse=function(){for(var e,t=0,r=this.length-1;t<=r;++t,--r)e=this[t],this[t]=this[r],this[r]=e;return this}),s.WalletInitError=Error.extend("WalletInitError",400),s.WalletCreateError=Error.extend("WalletCreateError",400),s.WalletUpgradeError=Error.extend("WalletUpgradeError",400),s.WalletChecksumError=Error.extend("WalletChecksumError",400),s.WalletDeleteError=Error.extend("WalletDeleteError",400),s.WalletDecryptError=Error.extend("WalletDecryptError",400),s.WalletAddressError=Error.extend("WalletAddressError",500),s.WalletSendError=Error.extend("WalletSendError",400),s.WalletLockedError=Error.extend("WalletLockedError",500),s.WalletFeeError=Error.extend("WalletFeeError",500),s.WalletInvalid2FAError=Error.extend("WalletInvalid2FAError",401),s.WalletMissing2FAError=Error.extend("WalletMissing2FAError",401),s.WalletMissingEmail2FAError=Error.extend("WalletMissingEmail2FAError",401),s.TransactionSignError=Error.extend("TransactionSignError",500),s.TransactionInputError=Error.extend("TransactionInputError",400),s.TransactionOutputError=Error.extend("TransactionOutputError",400),s.KeyPathError=Error.extend("KeyPathError",400),s.InvalidAddressError=Error.extend("InvalidAddressError",400),s.Error=Error.extend("Error",500),s.FEE_STRATEGY_FORCE_FEE="force_fee",s.FEE_STRATEGY_BASE_FEE="base_fee",s.FEE_STRATEGY_HIGH_PRIORITY="high_priority",s.FEE_STRATEGY_OPTIMAL="optimal",s.FEE_STRATEGY_LOW_PRIORITY="low_priority",s.FEE_STRATEGY_MIN_RELAY_FEE="min_relay_fee",s.patchQ(e("q")),t.exports=s}).call(this,e("buffer").Buffer)},{"./encryption":5,"./encryption_mnemonic":6,"./keyderivation":7,assert:23,bip39:34,buffer:111,"crypto-js":158,q:278,util:335}],4:[function(e,t,r){var n=e("./wallet"),i=e("./blocktrail"),o=e("bitcoinjs-lib"),s=function(e,t){this.network=e,this.useNewCashAddr=t};function a(e,t,r){var n;try{n=o.address.toOutputScript(e,t,r)}catch(e){n=null}return n}function c(e){var t;try{t=o.script.toASM(e)}catch(e){t=null}return t}function f(asm){return asm?asm.replace(/^0 /,"OP_0 "):asm}function u(e){var t;try{t=o.script.classifyOutput(e)}catch(e){t=null}return t}function h(e){switch(e){case"P2PKH_PUBKEY":return"pubkey";case"P2PKH":return"pubkeyhash";case"P2SH":return"scripthash";case"P2WSH_V0":return"witnessscripthash";case"P2WPKH_V0":return"witnesspubkeyhash";case"NULL_DATA":return"op_return";case"coinbase":return"coinbase";default:throw new Error("Not implemented yet, script type: "+e)}}function l(e){return new Date(1e3*e).toISOString().replace(/\.000Z$/,"+0000")}function d(e){return e&&1===e.length?e[0]:e}function p(e){var t,r={};for(var n in r.size=e.vsize,r.hash=e.hash,r.block_height=e.block_height,r.time=r.block_time=l(e.block_time),r.block_hash=e.block_hash,r.confirmations=e.confirmations,r.is_coinbase=e.is_coinbase,t=r.is_coinbase?e.outputs[0].value-e.fee:e.inputs_value,r.total_input_value=t,r.total_output_value=e.outputs.reduce(function(e,t){return e+t.value},0),r.total_fee=e.fee,r.inputs=[],r.outputs=[],r.opt_in_rbf=!1,e.inputs){var s,a,c,u,p=e.inputs[n];p.sequence<o.Transaction.DEFAULT_SEQUENCE-1&&(r.opt_in_rbf=!0),r.is_coinbase&&-1===p.prev_position&&"0000000000000000000000000000000000000000000000000000000000000000"===p.prev_tx_hash?(s="coinbase",c=null,a=t,u=4294967295):(s=p.prev_type,a=p.prev_value,c=p.prev_tx_hash,u=p.prev_position),r.inputs.push({index:parseInt(n,10),output_hash:c,output_index:u,value:a,sequence:p.sequence,address:d(p.prev_addresses),type:h(s),script_signature:p.script_hex})}for(var b in e.outputs){var y=e.outputs[b];r.outputs.push({index:parseInt(b,10),value:y.value,address:d(y.addresses),type:h(y.type),script:f(y.script_asm),script_hex:y.script_hex,spent_hash:y.spent_by_tx,spent_index:y.spent_by_tx_position})}return r.size=e.size,r.is_double_spend=e.is_double_spend,r.lock_time_timestamp=null,r.lock_time_block_height=null,e.lock_time&&(e.lock_time<i.LOCK_TIME_TIMESTAMP_THRESHOLD?r.lock_time_block_height=e.lock_time:r.lock_time_timestamp=e.lock_time),r.is_sw_tx=e.is_sw_tx,r.weight=e.weight,r.witness_hash=e.witness_hash,r.lock_time=e.lock_time,r.sigops=e.sigops,r.version=e.version,r}s.prototype.paginationParams=function(e){return e?(void 0!==e.limit&&(e.pagesize=e.limit,delete e.limit),e):e},s.prototype.getUrlForBlock=function(e){return"/block/"+e},s.prototype.getUrlForTransaction=function(e){return"/tx/"+e+"?verbose=3"},s.prototype.getUrlForRawTransaction=function(e){return"/tx/"+e+"/raw"},s.prototype.getUrlForTransactions=function(e){return"/tx/"+e.join(",")+"?verbose=3"},s.prototype.getUrlForBlockTransaction=function(e){return"/block/"+e+"/tx?verbose=3"},s.prototype.getUrlForAddress=function(e){return"/address/"+e},s.prototype.getUrlForAddressTransactions=function(e){return"/address/"+e+"/tx?verbose=3"},s.prototype.getUrlForAddressUnspent=function(e){return"/address/"+e+"/unspent"},s.prototype.getUrlForBatchAddressUnspent=function(e){return"/multi-address/"+e.join(",")+"/unspent"},s.prototype.getUrlForAllBlocks=function(){return"/block/list"},s.prototype.handleErrors=function(e,t){return 0===t.err_no||null!==t.data?t:{err_no:t.err_no,err_msg:t.err_msg,data:t.data}},s.prototype.convertBlock=function(e){return{hash:e.hash,version:e.version,height:e.height,block_time:l(e.timestamp),arrival_time:l(e.timestamp),bits:e.bits,nonce:e.nonce,merkleroot:e.mrkl_root,prev_block:e.prev_block_hash,next_block:e.next_block_hash,byte_size:e.stripped_size,difficulty:Math.floor(e.difficulty),transactions:e.tx_count,reward_block:e.reward_block,reward_fees:e.reward_fees,created_at:e.created_at,confirmations:e.confirmations,is_orphan:e.is_orphan,is_sw_block:e.is_sw_block,weight:e.weight,miningpool_name:e.miningpool_name||null,miningpool_url:e.miningpool_url||null,miningpool_slug:e.miningpool_slug||null}},s.prototype.convertBlocks=function(e){return{data:e.data.list,current_page:e.data.page,per_page:e.data.pagesize,total:e.data.total_count}},s.prototype.convertBlockTxs=function(e){var t=[];return e.data.list.forEach(function(e){var r=p(e);t.push(r)}),{data:t,current_page:e.data.page,per_page:e.data.pagesize,total:e.data.total_count}},s.prototype.convertTx=function(e,t){var r=p(e.data);return r.raw=t,r},s.prototype.convertTxs=function(e){var t={};return e.data.filter(function(e){return!!e}).forEach(function(e){var r=p(e);t[r.hash]=r}),{data:t}},s.prototype.convertAddressTxs=function(e){return{data:e.data.list.map(function(e){var t={};return t.hash=e.hash,t.time=l(e.block_time),t.confirmations=e.confirmations,t.block_height=e.block_height,t.block_hash=e.block_hash,t.is_coinbase=e.is_coinbase,t.total_input_value=e.inputs_value,t.total_output_value=e.outputs_value,t.total_fee=e.fee,t.inputs=e.inputs.map(function(e,r){return{index:r,output_hash:e.prev_tx_hash,output_index:e.prev_position,value:e.prev_value,address:d(e.prev_addresses),type:t.is_coinbase?t.is_coinbase:h(e.prev_type),script_signature:e.script_hex}}),t.outputs=e.outputs.map(function(e,t){return{index:t,value:e.value,address:d(e.addresses),type:h(e.type),script:f(e.script_asm),spent_hash:e.spent_by_tx||null,script_hex:e.script_hex,spent_index:e.spent_by_tx_position}}),t.is_double_spend=e.is_double_spend,t.is_sw_tx=e.is_sw_tx,t.weight=e.weight,t.witness_hash=e.witness_hash,t.version=e.version,t}),current_page:e.data.page,per_page:e.data.pagesize,total:e.data.total_count}},s.prototype.convertAddress=function(e){var t={};return t.address=e.data.address,t.hash160=function(e,t,r){var i;try{i=n.getAddressAndType(e,t,r)}catch(e){return null}return"base58"===i.type?i.decoded.hash:"bech32"===i.type?20===i.data.length?i.decoded.hash:null:""===i.type?i.decoded.hash:null}(e.data.address,this.network,this.useNewCashAddr).toString("hex").toUpperCase(),t.balance=e.data.balance,t.received=e.data.received,t.sent=e.data.sent,t.transactions=e.data.tx_count,t.utxos=e.data.unspent_tx_count,t.unconfirmed_received=e.data.unconfirmed_received,t.unconfirmed_sent=e.data.unconfirmed_sent,t.unconfirmed_transactions=e.data.unconfirmed_tx_count,t.first_tx=e.data.first_tx,t.last_tx=e.data.last_tx,t},s.prototype.convertAddressUnspentOutputs=function(e,t){var r=a(t,this.network,this.useNewCashAddr),n=r.toString("hex"),i=c(r),o=u(r);return{data:e.data.list.map(function(e){return{hash:e.tx_hash,confirmations:e.confirmations,value:e.value,index:e.tx_output_n,address:t,type:o,script:i,script_hex:n}}),current_page:e.data.page,total:e.data.total_count}},s.prototype.convertBatchAddressUnspentOutputs=function(e){var t=[],r=0;return e.data.forEach(function(e){var n=a(e.address,this.network,this.useNewCashAddr),i=n.toString("hex"),o=c(n),s=u(n);e.list.forEach(function(n){r++,t.push({hash:n.tx_hash,index:n.tx_output_n,value:n.value,confirmations:n.confirmations,address:e.address,script:o,script_hex:i,type:s})})}),{data:t,current_page:null,per_page:null,total:r}},t.exports=s},{"./blocktrail":3,"./wallet":18,"bitcoinjs-lib":49}],5:[function(e,t,r){(function(Buffer){var r=e("assert"),n=e("sjcl"),i=e("./keyderivation"),o=e("randombytes"),s={defaultSaltLen:10,tagLenBits:128,ivLenBits:128,ivLenWords:4,generateSalt:function(){return o(this.defaultSaltLen)},generateIV:function(){return o(this.ivLenBits/8)},encrypt:function(e,t,r){var n=this.generateSalt(),o=this.generateIV();return r=void 0===r?i.defaultIterations:r,this.encryptWithSaltAndIV(e,t,n,o,r)},encryptWithSaltAndIV:function(e,t,o,s,a){r(e instanceof Buffer,"pt must be provided as a buffer"),r(t instanceof Buffer,"pw must be provided as a buffer"),r(s instanceof Buffer,"IV must be provided as a buffer"),r(o instanceof Buffer,"saltBuff must be provided as a buffer"),r(16===s.length,"IV must be exactly 16 bytes");var c=new Buffer(1),f=o,u=new Buffer(4);c.writeUInt8(o.length),u.writeUInt32LE(a);var h=c.toString("hex")+f.toString("hex")+u.toString("hex"),l=n.codec.hex.toBits(i.compute(t,o,a).toString("hex")),d=n.mode.gcm.encrypt(new n.cipher.aes(l),n.codec.hex.toBits(e.toString("hex")),n.codec.hex.toBits(s.toString("hex")),n.codec.hex.toBits(h),this.tagLenBits);return new Buffer([h,s.toString("hex"),n.codec.hex.fromBits(d)].join(""),"hex")},decrypt:function(e,t){r(e instanceof Buffer,"cipherText must be provided as a Buffer"),r(t instanceof Buffer,"password must be provided as a Buffer");var o=new Buffer(e,"hex"),s=0,a=o.readUInt8(s);s+=1;var c=o.slice(1,s+a);s+=a;var f=o.readUInt32LE(s);s+=4;var u=o.slice(0,s),h=o.slice(s,16+s);s+=16;var l=o.slice(s),d=i.compute(t,c,f),p=n.mode.gcm.decrypt(new n.cipher.aes(n.codec.hex.toBits(d.toString("hex"))),n.codec.hex.toBits(l.toString("hex")),n.codec.hex.toBits(h.toString("hex")),n.codec.hex.toBits(u.toString("hex")),this.tagLenBits);return new Buffer(n.codec.hex.fromBits(p),"hex")}};t.exports=s}).call(this,e("buffer").Buffer)},{"./keyderivation":7,assert:23,buffer:111,randombytes:282,sjcl:313}],6:[function(e,t,r){(function(Buffer){var r=e("assert"),n=e("bip39");String.prototype.repeat||(String.prototype.repeat=function(e){"use strict";if(null===this)throw new TypeError("can't convert "+this+" to object");var t=""+this;if((e=+e)!=e&&(e=0),e<0)throw new RangeError("repeat count must be non-negative");if(e===1/0)throw new RangeError("repeat count must be less than infinity");if(e=Math.floor(e),0===t.length||0===e)return"";if(t.length*e>=1<<28)throw new RangeError("repeat count must not overflow maximum string size");for(var r="";1==(1&e)&&(r+=t),0!==(e>>>=1);)t+=t;return r});var i={chunkSize:4,paddingDummy:129},o=function(e){if(e[0]>128)throw new Error("Mnemonic sanity check - first byte can never be above 0x80");return i.paddingDummy.toString(16).repeat(i.chunkSize-e.length%i.chunkSize)};i.encode=function(e){r(e instanceof Buffer,"Data must be provided as a Buffer");var t=o(e),i=n.entropyToMnemonic(t+e.toString("hex"));try{n.mnemonicToEntropy(i)}catch(e){throw new Error("BIP39 library produced an invalid mnemonic")}return i},i.decode=function(e){r("string"==typeof e,"Mnemonic must be provided as a string");for(var t=new Buffer(n.mnemonicToEntropy(e),"hex"),i=0;t[i]===this.paddingDummy;)i++;var s=t.slice(i,t.length);if(o(s)!==t.slice(0,i).toString("hex"))throw new Error("There is only one way to pad a string");return s},t.exports=i}).call(this,e("buffer").Buffer)},{assert:23,bip39:34,buffer:111}],7:[function(e,t,r){(function(Buffer){var r=e("assert"),n=e("./pbkdf2_sha512"),i={defaultIterations:35e3,subkeyIterations:1,keySizeBits:256,compute:function(e,t,i){if(i=i||this.defaultIterations,r(e instanceof Buffer,"Password must be provided as a Buffer"),r(t instanceof Buffer,"Salt must be provided as a Buffer"),r(t.length>0,"Salt must not be empty"),r("number"==typeof i,"Iterations must be a number"),r(i>0,"Iteration count should be at least 1"),t.length>128)throw new Error("Sanity check: Invalid salt, length can never be greater than 128");return n.digest(e,t,i,this.keySizeBits/8)}};t.exports=i}).call(this,e("buffer").Buffer)},{"./pbkdf2_sha512":8,assert:23,buffer:111}],8:[function(e,t,r){(function(Buffer){t.exports={digest:function(e,t,r,n){var i="undefined"!=typeof window?window.asmCrypto:self.asmCrypto;return new Buffer(new i.PBKDF2_HMAC_SHA512.bytes(e,t,r,n).buffer)}}}).call(this,e("buffer").Buffer)},{buffer:111}],9:[function(e,t,r){t.exports={VERSION:"3.7.22"}},{}],10:[function(e,t,r){(function(r){var n=e("./blocktrail"),i=e("lodash"),o=e("url"),s=e("querystring"),a=e("q"),c=e("create-hash"),f=e("superagent"),u=e("superagent-http-signature/index-hmac-only"),h=e("debug")("blocktrail-sdk:request"),l=!r.browser,d=function(){};function p(e){this.https=e.https,this.host=e.host,this.endpoint=e.endpoint,this.auth=e.auth,this.port=e.port,this.apiKey=e.apiKey,this.apiSecret=e.apiSecret,this.contentMd5=void 0===e.contentMd5||e.contentMd5,this.params=i.defaults({},e.params),this.headers=i.defaults({},e.headers)}p.qs=function(e){var t=[],r=Object.keys(e);return r.sort(),r.forEach(function(r){var n={};n[r]=e[r],t.push(s.stringify(n))}),t.join("&")},p.prototype.request=function(e,t,r,n,s){this.deferred=a.defer(),this.callback=s||d;var f=o.parse(t,!0),u=p.qs(i.defaults({},r||{},f.query||{},this.params||{}));this.path="".concat(this.endpoint,f.pathname),u&&(this.path=this.path.concat("?",u)),n?(this.payload=JSON.stringify(n),this.headers["Content-Type"]="application/json"):this.payload="",l&&(this.headers["Content-Length"]=this.payload?this.payload.length:0),!0===this.contentMd5&&(this.headers["Content-MD5"]="GET"===e||"DELETE"===e?c("md5").update(this.path).digest().toString("hex"):c("md5").update(this.payload).digest().toString("hex")),h("%s %s %s",e,this.host,this.path);var b={hostname:this.host,path:this.path,port:this.port,method:e,headers:this.headers,auth:this.auth,agent:!1,withCredentials:!1};return this.performRequest(b),this.deferred.promise},p.prototype.performRequest=function(e){var t=this,r=e.method,n=!1;"http-signature"===e.auth&&(n=!0,delete e.auth);var o=(t.https?"https://":"http://")+e.hostname+e.path,s=f(r,o);if(!t.payload||"DELETE"!==r&&"POST"!==r&&"PUT"!==r&&"PATCH"!==r||s.send(t.payload),i.forEach(e.headers,function(e,t){s.set(t,e)}),n){if(!t.apiSecret){var a=new Error("Missing apiSecret! required to sign POST requests!");return t.deferred.reject(a),t.callback(a)}s.use(u({headers:["(request-target)","content-md5"],algorithm:"hmac-sha256",key:t.apiSecret,keyId:t.apiKey}))}return s.end(function(e,r){var n;if(e){var i=p.handleFailure(e.response&&e.response.body,e.status);return t.deferred.reject(i),t.callback(i,e.response&&e.response.body)}if(h("response status code: %s content type: %s",r.status,r.headers["content-type"]),!e&&r.headers["content-type"].indexOf("application/json")>=0)try{n=JSON.parse(r.text)}catch(t){e=t}return n||(n=r.text),e||200===r.status||(e=p.handleFailure(r.text,r.statusCode)),e?t.deferred.reject(e):t.deferred.resolve(n),t.callback(e,n)}),t.deferred},p.handleFailure=function(e,t){var r,n;if("object"==typeof e)r=e;else try{r=JSON.parse(e)}catch(e){}if(r){var i=r.msg||"";i||429===t&&(i="Too Many Requ