UNPKG

bitcoinfiles-node

Version:

Upload and Download files to Bitcoin Cash blockchain with Node and web

1 lines 431 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.bitcoinfiles=f()}})(function(){var define,module,exports;return function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r}()({1:[function(require,module,exports){let bfp=require("./lib/bfp");let utils=require("./lib/utils");let network=require("./lib/network");module.exports={bfp:bfp,utils:utils,network:network}},{"./lib/bfp":2,"./lib/network":4,"./lib/utils":5}],2:[function(require,module,exports){(function(Buffer){let utils=require("./utils");let Network=require("./network");let Bitdb=require("./bitdb");let bchrpc=require("grpc-bchrpc-web");const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));class Bfp{constructor(BITBOX,network="mainnet",grpcUrl=null){this.BITBOX=BITBOX;this.networkstring=network;this.network=new Network(this.BITBOX,grpcUrl);this.bitdb=new Bitdb(network);if(grpcUrl)this.client=new bchrpc.Client(grpcUrl);else this.client=new bchrpc.Client}static get lokadIdHex(){return"42465000"}async uploadHashOnlyObject(type,fundingUtxo,fundingAddress,fundingWif,objectDataArrayBuffer,objectName=null,objectExt=null,prevObjectSha256Hex=null,objectExternalUri=null,objectReceiverAddress=null,signProgressCallback=null,signFinishedCallback=null,uploadProgressCallback=null,uploadFinishedCallback=null){let fileSize=objectDataArrayBuffer.byteLength;let hash=this.BITBOX.Crypto.sha256(new Buffer(objectDataArrayBuffer)).toString("hex");let chunkCount=0;let configEmptyMetaOpReturn={msgType:type,chunkCount:chunkCount,fileName:objectName,fileExt:objectExt,fileSize:fileSize,fileSha256Hex:hash,prevFileSha256Hex:prevObjectSha256Hex,fileUri:objectExternalUri,chunkData:null};let transactions=[];let txid=fundingUtxo.txid;let satoshis=fundingUtxo.satoshis;let vout=fundingUtxo.vout;let metaOpReturn=Bfp.buildMetadataOpReturn(configEmptyMetaOpReturn);let configMetaTx={bfpMetadataOpReturn:metaOpReturn,input_utxo:{txid:txid,vout:vout,satoshis:satoshis,wif:fundingWif},fileReceiverAddress:objectReceiverAddress!=null?objectReceiverAddress:fundingAddress};let metaTx=this.buildMetadataTx(configMetaTx);transactions.push(metaTx);if(signProgressCallback!=null){signProgressCallback(100)}if(signFinishedCallback!=null){signFinishedCallback()}if(uploadProgressCallback!=null){uploadProgressCallback(0)}console.log("transaction: ",transactions[0].toHex());var bfTxId=await this.network.sendTxWithRetry(transactions[0].toHex());if(uploadProgressCallback!=null){uploadProgressCallback(100)}bfTxId="bitcoinfile:"+bfTxId;if(uploadFinishedCallback!=null){uploadFinishedCallback(bfTxId)}return bfTxId}async uploadFolderHashOnly(fundingUtxo,fundingAddress,fundingWif,folderDataArrayBuffer,folderName=null,folderExt=null,prevFolderSha256Hex=null,folderExternalUri=null,folderReceiverAddress=null,signProgressCallback=null,signFinishedCallback=null,uploadProgressCallback=null,uploadFinishedCallback=null){return await this.uploadHashOnlyObject(3,fundingUtxo,fundingAddress,fundingWif,folderDataArrayBuffer,folderName,folderExt,prevFolderSha256Hex,folderExternalUri,folderReceiverAddress,signProgressCallback,signFinishedCallback,uploadProgressCallback,uploadFinishedCallback)}async uploadFileHashOnly(fundingUtxo,fundingAddress,fundingWif,fileDataArrayBuffer,fileName=null,fileExt=null,prevFileSha256Hex=null,fileExternalUri=null,fileReceiverAddress=null,signProgressCallback=null,signFinishedCallback=null,uploadProgressCallback=null,uploadFinishedCallback=null){return await this.uploadHashOnlyObject(1,fundingUtxo,fundingAddress,fundingWif,fileDataArrayBuffer,fileName,fileExt,prevFileSha256Hex,fileExternalUri,fileReceiverAddress,signProgressCallback,signFinishedCallback,uploadProgressCallback,uploadFinishedCallback)}async uploadFile(fundingUtxo,fundingAddress,fundingWif,fileDataArrayBuffer,fileName=null,fileExt=null,prevFileSha256Hex=null,fileExternalUri=null,fileReceiverAddress=null,signProgressCallback=null,signFinishedCallback=null,uploadProgressCallback=null,uploadFinishedCallback=null,delay_ms=500){let fileSize=fileDataArrayBuffer.byteLength;let hash=this.BITBOX.Crypto.sha256(new Buffer(fileDataArrayBuffer)).toString("hex");let chunks=[];let chunkCount=Math.floor(fileSize/220);for(let nId=0;nId<chunkCount;nId++){chunks.push(fileDataArrayBuffer.slice(nId*220,(nId+1)*220))}if(fileSize%220){chunks[chunkCount]=fileDataArrayBuffer.slice(chunkCount*220,fileSize);chunkCount++}let configEmptyMetaOpReturn={msgType:1,chunkCount:chunkCount,fileName:fileName,fileExt:fileExt,fileSize:fileSize,fileSha256Hex:hash,prevFileSha256Hex:prevFileSha256Hex,fileUri:fileExternalUri,chunkData:null};let transactions=[];let nDiff=100/chunkCount;let nCurPos=0;for(let nId=0;nId<chunkCount;nId++){let chunkOpReturn=Bfp.buildDataChunkOpReturn(chunks[nId]);let txid="";let satoshis=0;let vout=1;if(nId===0){txid=fundingUtxo.txid;satoshis=fundingUtxo.satoshis;vout=fundingUtxo.vout}else{txid=transactions[nId-1].getId();satoshis=transactions[nId-1].outs[1].value}let configChunkTx={bfpChunkOpReturn:chunkOpReturn,input_utxo:{address:fundingAddress,txid:txid,vout:vout,satoshis:satoshis,wif:fundingWif}};let chunksTx=this.buildChunkTx(configChunkTx);if(nId===chunkCount-1){let emptyOpReturn=Bfp.buildMetadataOpReturn(configEmptyMetaOpReturn);let capacity=223-emptyOpReturn.length;if(capacity>=chunks[nId].byteLength){let configMetaOpReturn={msgType:1,chunkCount:chunkCount,fileName:fileName,fileExt:fileExt,fileSize:fileSize,fileSha256Hex:hash,prevFileSha256Hex:prevFileSha256Hex,fileUri:fileExternalUri,chunkData:chunks[nId]};let metaOpReturn=Bfp.buildMetadataOpReturn(configMetaOpReturn);let configMetaTx={bfpMetadataOpReturn:metaOpReturn,input_utxo:{txid:txid,vout:vout,satoshis:satoshis,wif:fundingWif},fileReceiverAddress:fileReceiverAddress!=null?fileReceiverAddress:fundingAddress};let metaTx=this.buildMetadataTx(configMetaTx);transactions.push(metaTx)}else{transactions.push(chunksTx);let metaOpReturn=Bfp.buildMetadataOpReturn(configEmptyMetaOpReturn);let configMetaTx={bfpMetadataOpReturn:metaOpReturn,input_utxo:{txid:chunksTx.getId(),vout:vout,satoshis:chunksTx.outs[1].value,wif:fundingWif},fileReceiverAddress:fileReceiverAddress!=null?fileReceiverAddress:fundingAddress};let metaTx=this.buildMetadataTx(configMetaTx);transactions.push(metaTx)}}else{transactions.push(chunksTx)}if(signProgressCallback!=null){signProgressCallback(nCurPos)}nCurPos+=nDiff}if(signFinishedCallback!=null){signFinishedCallback()}nDiff=100/transactions.length;nCurPos=0;if(uploadProgressCallback!=null){uploadProgressCallback(0)}for(let nId=0;nId<transactions.length;nId++){console.log("transaction: ",transactions[nId].toHex());var bfTxId=await this.network.sendTxWithRetry(transactions[nId].toHex());if(uploadProgressCallback!=null){uploadProgressCallback(nCurPos)}nCurPos+=nDiff;await sleep(delay_ms)}bfTxId="bitcoinfile:"+bfTxId;if(uploadFinishedCallback!=null){uploadFinishedCallback(bfTxId)}return bfTxId}async downloadFile(bfpUri,progressCallback=null){let chunks=[];let size=0;let txid=bfpUri.replace("bitcoinfile:","");txid=txid.replace("bitcoinfiles:","");let tx=await this.client.getTransaction(txid,true);let prevHash=Buffer.from(tx.getTransaction().getInputsList()[0].getOutpoint().getHash_asU8()).toString("hex");let metadata_opreturn_hex=Buffer.from(tx.getTransaction().getOutputsList()[0].getPubkeyScript_asU8()).toString("hex");let bfpMsg=this.parsebfpDataOpReturn(metadata_opreturn_hex);let downloadCount=bfpMsg.chunk_count;if(bfpMsg.chunk_count>0&&bfpMsg.chunk!=null){downloadCount=bfpMsg.chunk_count-1;chunks.push(bfpMsg.chunk);size+=bfpMsg.chunk.length}for(let index=0;index<downloadCount;index++){let tx=await this.client.getTransaction(prevHash);prevHash=Buffer.from(tx.getTransaction().getInputsList()[0].getOutpoint().getHash_asU8()).toString("hex");let op_return_hex=Buffer.from(tx.getTransaction().getOutputsList()[0].getPubkeyScript_asU8()).toString("hex");let bfpMsg=this.parsebfpDataOpReturn(op_return_hex);chunks.push(bfpMsg.chunk);size+=bfpMsg.chunk.length;if(progressCallback!=null){progressCallback(index/(downloadCount-1))}}chunks=chunks.reverse();let fileBuf=new Buffer.alloc(size);let index=0;chunks.forEach(chunk=>{chunk.copy(fileBuf,index);index+=chunk.length});let passesHashCheck=false;if(bfpMsg.sha256!=null){let fileSha256=this.BITBOX.Crypto.sha256(fileBuf);let res=Buffer.compare(fileSha256,bfpMsg.sha256);if(res===0){passesHashCheck=true}}return{passesHashCheck:passesHashCheck,fileBuf:fileBuf}}static buildMetadataOpReturn(config){let script=[];script.push(106);let lokadId=Buffer.from(Bfp.lokadIdHex,"hex");script.push(utils.getPushDataOpcode(lokadId));lokadId.forEach(item=>script.push(item));script.push(utils.getPushDataOpcode([config.msgType]));script.push(config.msgType);let chunkCount=utils.int2FixedBuffer(config.chunkCount,1);script.push(utils.getPushDataOpcode(chunkCount));chunkCount.forEach(item=>script.push(item));if(config.fileName==null||config.fileName.length===0||config.fileName==""){[76,0].forEach(item=>script.push(item))}else{let fileName=Buffer.from(config.fileName,"utf8");script.push(utils.getPushDataOpcode(fileName));fileName.forEach(item=>script.push(item))}if(config.fileExt==null||config.fileExt.length===0||config.fileExt==""){[76,0].forEach(item=>script.push(item))}else{let fileExt=Buffer.from(config.fileExt,"utf8");script.push(utils.getPushDataOpcode(fileExt));fileExt.forEach(item=>script.push(item))}let fileSize=utils.int2FixedBuffer(config.fileSize,2);script.push(utils.getPushDataOpcode(fileSize));fileSize.forEach(item=>script.push(item));var re=/^[0-9a-fA-F]+$/;if(config.fileSha256Hex==null||config.fileSha256Hex.length===0||config.fileSha256Hex==""){[76,0].forEach(item=>script.push(item))}else if(config.fileSha256Hex.length===64&&re.test(config.fileSha256Hex)){let fileSha256Buf=Buffer.from(config.fileSha256Hex,"hex");script.push(utils.getPushDataOpcode(fileSha256Buf));fileSha256Buf.forEach(item=>script.push(item))}else{throw Error("File hash must be provided as a 64 character hex string")}if(config.prevFileSha256Hex==null||config.prevFileSha256Hex.length===0||config.prevFileSha256Hex==""){[76,0].forEach(item=>script.push(item))}else if(config.prevFileSha256Hex.length===64&&re.test(config.prevFileSha256Hex)){let prevFileSha256Buf=Buffer.from(config.prevFileSha256Hex,"hex");script.push(utils.getPushDataOpcode(prevFileSha256Buf));prevFileSha256Buf.forEach(item=>script.push(item))}else{throw Error("Previous File hash must be provided as a 64 character hex string")}if(config.fileUri==null||config.fileUri.length===0||config.fileUri==""){[76,0].forEach(item=>script.push(item))}else{let fileUri=Buffer.from(config.fileUri,"utf8");script.push(utils.getPushDataOpcode(fileUri));fileUri.forEach(item=>script.push(item))}if(config.chunkData==null||config.chunkData.length===0){[76,0].forEach(item=>script.push(item))}else{let chunkData=Buffer.from(config.chunkData);script.push(utils.getPushDataOpcode(chunkData));chunkData.forEach(item=>script.push(item))}let encodedScript=utils.encodeScript(script);if(encodedScript.length>223){throw Error("Script too long, must be less than 223 bytes.")}return encodedScript}static buildDataChunkOpReturn(chunkData){let script=[];script.push(106);if(chunkData===undefined||chunkData===null||chunkData.length===0){[76,0].forEach(item=>script.push(item))}else{let chunkDataBuf=Buffer.from(chunkData);script.push(utils.getPushDataOpcode(chunkDataBuf));chunkDataBuf.forEach(item=>script.push(item))}let encodedScript=utils.encodeScript(script);if(encodedScript.length>223){throw Error("Script too long, must be less than 223 bytes.")}return encodedScript}buildFundingTx(config){let transactionBuilder;if(this.networkstring==="mainnet")transactionBuilder=new this.BITBOX.TransactionBuilder("bitcoincash");else transactionBuilder=new this.BITBOX.TransactionBuilder("bchtest");let satoshis=0;config.input_utxos.forEach(token_utxo=>{transactionBuilder.addInput(token_utxo.txid,token_utxo.vout);satoshis+=token_utxo.satoshis});let fundingMinerFee=this.BITBOX.BitcoinCash.getByteCount({P2PKH:config.input_utxos.length},{P2PKH:1});let outputAmount=satoshis-fundingMinerFee;transactionBuilder.addOutput(config.outputAddress,outputAmount);let i=0;for(const txo of config.input_utxos){let paymentKeyPair=this.BITBOX.ECPair.fromWIF(txo.wif);transactionBuilder.sign(i,paymentKeyPair,null,transactionBuilder.hashTypes.SIGHASH_ALL,txo.satoshis);i++}return transactionBuilder.build()}buildChunkTx(config){let transactionBuilder;if(this.networkstring==="mainnet")transactionBuilder=new this.BITBOX.TransactionBuilder("bitcoincash");else transactionBuilder=new this.BITBOX.TransactionBuilder("bchtest");transactionBuilder.addInput(config.input_utxo.txid,config.input_utxo.vout);let chunkTxFee=this.calculateDataChunkMinerFee(config.bfpChunkOpReturn.length);let outputAmount=config.input_utxo.satoshis-chunkTxFee;transactionBuilder.addOutput(config.bfpChunkOpReturn,0);transactionBuilder.addOutput(config.input_utxo.address,outputAmount);let paymentKeyPair=this.BITBOX.ECPair.fromWIF(config.input_utxo.wif);transactionBuilder.sign(0,paymentKeyPair,null,transactionBuilder.hashTypes.SIGHASH_ALL,config.input_utxo.satoshis);return transactionBuilder.build()}buildMetadataTx(config){let transactionBuilder;if(this.networkstring==="mainnet")transactionBuilder=new this.BITBOX.TransactionBuilder("bitcoincash");else transactionBuilder=new this.BITBOX.TransactionBuilder("bchtest");let inputSatoshis=0;transactionBuilder.addInput(config.input_utxo.txid,config.input_utxo.vout);inputSatoshis+=config.input_utxo.satoshis;let metadataFee=this.calculateMetadataMinerFee(config.bfpMetadataOpReturn.length);let output=inputSatoshis-metadataFee;transactionBuilder.addOutput(config.bfpMetadataOpReturn,0);let outputAddress=this.BITBOX.Address.toCashAddress(config.fileReceiverAddress);transactionBuilder.addOutput(outputAddress,output);let paymentKeyPair=this.BITBOX.ECPair.fromWIF(config.input_utxo.wif);transactionBuilder.sign(0,paymentKeyPair,null,transactionBuilder.hashTypes.SIGHASH_ALL,config.input_utxo.satoshis);return transactionBuilder.build()}calculateMetadataMinerFee(genesisOpReturnLength,feeRate=1){let fee=this.BITBOX.BitcoinCash.getByteCount({P2PKH:1},{P2PKH:1});fee+=genesisOpReturnLength;fee+=10;fee*=feeRate;return fee}calculateDataChunkMinerFee(sendOpReturnLength,feeRate=1){let fee=this.BITBOX.BitcoinCash.getByteCount({P2PKH:1},{P2PKH:1});fee+=sendOpReturnLength;fee+=10;fee*=feeRate;return fee}static calculateFileUploadCost(fileSizeBytes,configMetadataOpReturn,fee_rate=1){let byte_count=fileSizeBytes;let whole_chunks_count=Math.floor(fileSizeBytes/220);let last_chunk_size=fileSizeBytes%220;let final_op_return_no_chunk=Bfp.buildMetadataOpReturn(configMetadataOpReturn);byte_count+=final_op_return_no_chunk.length;byte_count+=35;byte_count+=148+1;byte_count+=(whole_chunks_count+1)*3;if(!Bfp.chunk_can_fit_in_final_opreturn(final_op_return_no_chunk.length,last_chunk_size)){byte_count+=149+35;byte_count+=16}byte_count+=35*whole_chunks_count;byte_count+=(148+1)*whole_chunks_count;byte_count+=22*(whole_chunks_count+1);let dust_amount=546;return byte_count*fee_rate+dust_amount}static chunk_can_fit_in_final_opreturn(script_length,chunk_data_length){if(chunk_data_length===0){return true}let op_return_capacity=223-script_length;if(op_return_capacity>=chunk_data_length){return true}return false}parsebfpDataOpReturn(hex){const script=this.BITBOX.Script.toASM(Buffer.from(hex,"hex")).split(" ");let bfpData={};bfpData.type="metadata";if(script.length==2){bfpData.type="chunk";try{bfpData.chunk=Buffer.from(script[1],"hex")}catch(e){bfpData.chunk=null}return bfpData}if(script[0]!="OP_RETURN"){throw new Error("Not an OP_RETURN")}if(script[1]!==Bfp.lokadIdHex){throw new Error("Not a BFP OP_RETURN")}if(script[2]!="OP_1"){throw new Error("Not a BFP file (type 0x01)")}bfpData.chunk_count=parseInt(script[3],16);if(script[3].includes("OP_")){let val=script[3].replace("OP_","");bfpData.chunk_count=parseInt(val)}if(script[4]=="OP_0"){bfpData.filename=null}else{bfpData.filename=Buffer.from(script[4],"hex").toString("utf8")}if(script[5]=="OP_0"){bfpData.fileext=null}else{bfpData.fileext=Buffer.from(script[5],"hex").toString("utf8")}if(script[6]=="OP_0"){bfpData.filesize=null}else{bfpData.filesize=parseInt(script[6],16)}if(script[7]=="OP_0"){bfpData.sha256=null}else{bfpData.sha256=Buffer.from(script[7],"hex")}if(script[8]=="OP_0"){bfpData.prevsha256=null}else{bfpData.prevsha256=Buffer.from(script[8],"hex")}if(script[9]=="OP_0"){bfpData.uri=null}else{bfpData.uri=Buffer.from(script[9],"hex").toString("utf8")}if(script[10]=="OP_0"){bfpData.chunk=null}else{try{bfpData.chunk=Buffer.from(script[10],"hex")}catch(e){bfpData.chunk=null}}return bfpData}}module.exports=Bfp}).call(this,require("buffer").Buffer)},{"./bitdb":3,"./network":4,"./utils":5,buffer:34,"grpc-bchrpc-web":36}],3:[function(require,module,exports){(function(Buffer){const axios=require("axios");module.exports=class BfpBitdb{constructor(network){this.bitDbUrl=network==="mainnet"?"https://bitdb.bitcoin.com/q/":"https://tbitdb.bitcoin.com/q/"}async getFileMetadata(txid,apiKey=null){txid=txid.replace("bitcoinfile:","");txid=txid.replace("bitcoinfiles:","");let query={v:3,q:{find:{"tx.h":txid,"out.h1":"42465000","out.h2":"01"}},r:{f:'[ .[] | { timestamp: (if .blk? then (.blk.t | strftime("%Y-%m-%d %H:%M")) else null end), chunks: .out[0].h3, filename: .out[0].s4, fileext: .out[0].s5, size: .out[0].h6, sha256: .out[0].h7, prev_sha256: .out[0].h8, ext_uri: .out[0].s9, URI: "bitcoinfile:\\(.tx.h)" } ]'}};const json_str=JSON.stringify(query);const data=Buffer.from(json_str).toString("base64");const response=(await axios({method:"GET",url:this.bitDbUrl+data,headers:null,json:true})).data;if(response.status==="error"){throw new Error(response.message||"API error message missing")}const list=[];if(response.c){list.push(...response.c)}if(response.u){list.push(...response.u)}if(list.length===0){throw new Error("File not found")}console.log("bitdb response: ",list[0]);return list[0]}}}).call(this,require("buffer").Buffer)},{axios:7,buffer:34}],4:[function(require,module,exports){let bchrpc=require("grpc-bchrpc-web");const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));class BfpNetwork{constructor(BITBOX,grpcUrl="https://bchd.greyh.at:8335"){this.BITBOX=BITBOX;this.stopPayMonitor=false;this.isMonitoringPayment=false;if(grpcUrl)this.client=new bchrpc.Client(grpcUrl);else this.client=new bchrpc.Client}async getLastUtxoWithRetry(address,retries=40){let result;let count=0;while(result==undefined){result=await this.getLastUtxo(address);console.log(result);count++;if(count>retries)throw new Error("BITBOX.Address.utxo endpoint experienced a problem");await sleep(250)}return result}async getTransactionDetailsWithRetry(txid,retries=40){let result;let count=0;while(result==undefined){result=await this.BITBOX.Transaction.details(txid);count++;if(count>retries)throw new Error("BITBOX.Address.details endpoint experienced a problem");await sleep(250)}return result}async getLastUtxo(address){if(!this.BITBOX.Address.isCashAddress(address)&&!this.BITBOX.Address.isLegacyAddress(address))throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");let res=(await this.BITBOX.Address.utxo([address]))[0];if(res&&res.utxos&&res.utxos.length>0)return res.utxos[0];return res}async sendTx(hex,log=true){let res=await this.BITBOX.RawTransactions.sendRawTransaction(hex);if(res&&res.error)return undefined;if(res==="64: too-long-mempool-chain")throw new Error("Mempool chain too long");if(log)console.log("sendTx() res: ",res);return res}async sendTxWithRetry(hex,retries=40){let res;let count=0;while(res===undefined||res.length!=64){res=await this.sendTx(hex);count++;if(count>retries)break;await sleep(250)}if(res.length!=64)throw new Error("BITBOX network error");return res}async monitorForPayment(paymentAddress,fee,onPaymentCB){if(this.isMonitoringPayment||this.stopPayMonitor)return;this.isMonitoringPayment=true;if(!this.BITBOX.Address.isCashAddress(paymentAddress)&&!this.BITBOX.Address.isLegacyAddress(paymentAddress))throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");while(true){try{var utxo=await this.getLastUtxo(paymentAddress);if(utxo&&utxo&&utxo.satoshis>=fee&&utxo.confirmations===0){break}}catch(ex){console.log("monitorForPayment() error: ",ex)}if(this.stopPayMonitor){this.isMonitoringPayment=false;return}await sleep(2e3)}this.isMonitoringPayment=false;onPaymentCB(utxo)}}module.exports=BfpNetwork},{"grpc-bchrpc-web":36}],5:[function(require,module,exports){(function(Buffer){class BfpUtils{static getPushDataOpcode(data){let length=data.length;if(length===0)return[76,0];else if(length<76)return length;else if(length<256)return[76,length];else throw Error("Pushdata too large")}static int2FixedBuffer(amount,size){let hex=amount.toString(16);hex=hex.padStart(size*2,"0");if(hex.length%2)hex="0"+hex;return Buffer.from(hex,"hex")}static encodeScript(script){const bufferSize=script.reduce((acc,cur)=>{if(Array.isArray(cur))return acc+cur.length;else return acc+1},0);const buffer=Buffer.allocUnsafe(bufferSize);let offset=0;script.forEach(scriptItem=>{if(Array.isArray(scriptItem)){scriptItem.forEach(item=>{buffer.writeUInt8(item,offset);offset+=1})}else{buffer.writeUInt8(scriptItem,offset);offset+=1}});return buffer}}module.exports=BfpUtils}).call(this,require("buffer").Buffer)},{buffer:34}],6:[function(require,module,exports){!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(this,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=11)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(4);t.Metadata=r.BrowserHeaders},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.debug=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];console.debug?console.debug.apply(null,e):console.log.apply(null,e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=null;t.default=function(e){null===r?(r=[e],setTimeout(function(){!function e(){if(r){var t=r;r=null;for(var n=0;n<t.length;n++)try{t[n]()}catch(s){null===r&&(r=[],setTimeout(function(){e()},0));for(var o=t.length-1;o>n;o--)r.unshift(t[o]);throw s}}}()},0)):r.push(e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0),o=n(9),s=n(10),i=n(1),a=n(2),u=n(5),d=n(15);t.client=function(e,t){return new c(e,t)};var c=function(){function e(e,t){this.started=!1,this.sentFirstMessage=!1,this.completed=!1,this.closed=!1,this.finishedSending=!1,this.onHeadersCallbacks=[],this.onMessageCallbacks=[],this.onEndCallbacks=[],this.parser=new o.ChunkParser,this.methodDefinition=e,this.props=t,this.createTransport()}return e.prototype.createTransport=function(){var e=this.props.host+"/"+this.methodDefinition.service.serviceName+"/"+this.methodDefinition.methodName,t={methodDefinition:this.methodDefinition,debug:this.props.debug||!1,url:e,onHeaders:this.onTransportHeaders.bind(this),onChunk:this.onTransportChunk.bind(this),onEnd:this.onTransportEnd.bind(this)};this.props.transport?this.transport=this.props.transport(t):this.transport=u.makeDefaultTransport(t)},e.prototype.onTransportHeaders=function(e,t){if(this.props.debug&&i.debug("onHeaders",e,t),this.closed)this.props.debug&&i.debug("grpc.onHeaders received after request was closed - ignoring");else if(0===t);else{this.responseHeaders=e,this.props.debug&&i.debug("onHeaders.responseHeaders",JSON.stringify(this.responseHeaders,null,2));var n=p(e);this.props.debug&&i.debug("onHeaders.gRPCStatus",n);var r=n&&n>=0?n:s.httpStatusToCode(t);this.props.debug&&i.debug("onHeaders.code",r);var o=e.get("grpc-message")||[];if(this.props.debug&&i.debug("onHeaders.gRPCMessage",o),this.rawOnHeaders(e),r!==s.Code.OK){var a=this.decodeGRPCStatus(o[0]);this.rawOnError(r,a,e)}}},e.prototype.onTransportChunk=function(e){var t=this;if(this.closed)this.props.debug&&i.debug("grpc.onChunk received after request was closed - ignoring");else{var n=[];try{n=this.parser.parse(e)}catch(e){return this.props.debug&&i.debug("onChunk.parsing error",e,e.message),void this.rawOnError(s.Code.Internal,"parsing error: "+e.message)}n.forEach(function(e){if(e.chunkType===o.ChunkType.MESSAGE){var n=t.methodDefinition.responseType.deserializeBinary(e.data);t.rawOnMessage(n)}else e.chunkType===o.ChunkType.TRAILERS&&(t.responseHeaders?(t.responseTrailers=new r.Metadata(e.trailers),t.props.debug&&i.debug("onChunk.trailers",t.responseTrailers)):(t.responseHeaders=new r.Metadata(e.trailers),t.rawOnHeaders(t.responseHeaders)))})}},e.prototype.onTransportEnd=function(){if(this.props.debug&&i.debug("grpc.onEnd"),this.closed)this.props.debug&&i.debug("grpc.onEnd received after request was closed - ignoring");else if(void 0!==this.responseTrailers){var e=p(this.responseTrailers);if(null!==e){var t=this.responseTrailers.get("grpc-message"),n=this.decodeGRPCStatus(t[0]);this.rawOnEnd(e,n,this.responseTrailers)}else this.rawOnError(s.Code.Internal,"Response closed without grpc-status (Trailers provided)")}else{if(void 0===this.responseHeaders)return void this.rawOnError(s.Code.Unknown,"Response closed without headers");var r=p(this.responseHeaders),o=this.responseHeaders.get("grpc-message");if(this.props.debug&&i.debug("grpc.headers only response ",r,o),null===r)return void this.rawOnEnd(s.Code.Unknown,"Response closed without grpc-status (Headers only)",this.responseHeaders);var a=this.decodeGRPCStatus(o[0]);this.rawOnEnd(r,a,this.responseHeaders)}},e.prototype.decodeGRPCStatus=function(e){if(!e)return"";try{return decodeURIComponent(e)}catch(t){return e}},e.prototype.rawOnEnd=function(e,t,n){var r=this;this.props.debug&&i.debug("rawOnEnd",e,t,n),this.completed||(this.completed=!0,this.onEndCallbacks.forEach(function(o){a.default(function(){r.closed||o(e,t,n)})}))},e.prototype.rawOnHeaders=function(e){this.props.debug&&i.debug("rawOnHeaders",e),this.completed||this.onHeadersCallbacks.forEach(function(t){a.default(function(){t(e)})})},e.prototype.rawOnError=function(e,t,n){var o=this;void 0===n&&(n=new r.Metadata),this.props.debug&&i.debug("rawOnError",e,t),this.completed||(this.completed=!0,this.onEndCallbacks.forEach(function(r){a.default(function(){o.closed||r(e,t,n)})}))},e.prototype.rawOnMessage=function(e){var t=this;this.props.debug&&i.debug("rawOnMessage",e.toObject()),this.completed||this.closed||this.onMessageCallbacks.forEach(function(n){a.default(function(){t.closed||n(e)})})},e.prototype.onHeaders=function(e){this.onHeadersCallbacks.push(e)},e.prototype.onMessage=function(e){this.onMessageCallbacks.push(e)},e.prototype.onEnd=function(e){this.onEndCallbacks.push(e)},e.prototype.start=function(e){if(this.started)throw new Error("Client already started - cannot .start()");this.started=!0;var t=new r.Metadata(e||{});t.set("content-type","application/grpc-web+proto"),t.set("x-grpc-web","1"),this.transport.start(t)},e.prototype.send=function(e){if(!this.started)throw new Error("Client not started - .start() must be called before .send()");if(this.closed)throw new Error("Client already closed - cannot .send()");if(this.finishedSending)throw new Error("Client already finished sending - cannot .send()");if(!this.methodDefinition.requestStream&&this.sentFirstMessage)throw new Error("Message already sent for non-client-streaming method - cannot .send()");this.sentFirstMessage=!0;var t=d.frameRequest(e);this.transport.sendMessage(t)},e.prototype.finishSend=function(){if(!this.started)throw new Error("Client not started - .finishSend() must be called before .close()");if(this.closed)throw new Error("Client already closed - cannot .send()");if(this.finishedSending)throw new Error("Client already finished sending - cannot .finishSend()");this.finishedSending=!0,this.transport.finishSend()},e.prototype.close=function(){if(!this.started)throw new Error("Client not started - .start() must be called before .close()");if(this.closed)throw new Error("Client already closed - cannot .close()");this.closed=!0,this.props.debug&&i.debug("request.abort aborting request"),this.transport.cancel()},e}();function p(e){var t=e.get("grpc-status")||[];if(t.length>0)try{var n=t[0];return parseInt(n,10)}catch(e){return null}return null}},function(e,t,n){var r;r=function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=1)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(3);var o=function(){function e(e,t){void 0===e&&(e={}),void 0===t&&(t={splitValues:!1});var n,o=this;if(this.headersMap={},e)if("undefined"!=typeof Headers&&e instanceof Headers)r.getHeaderKeys(e).forEach(function(n){r.getHeaderValues(e,n).forEach(function(e){t.splitValues?o.append(n,r.splitHeaderValue(e)):o.append(n,e)})});else if("object"==typeof(n=e)&&"object"==typeof n.headersMap&&"function"==typeof n.forEach)e.forEach(function(e,t){o.append(e,t)});else if("undefined"!=typeof Map&&e instanceof Map){e.forEach(function(e,t){o.append(t,e)})}else"string"==typeof e?this.appendFromString(e):"object"==typeof e&&Object.getOwnPropertyNames(e).forEach(function(t){var n=e[t];Array.isArray(n)?n.forEach(function(e){o.append(t,e)}):o.append(t,n)})}return e.prototype.appendFromString=function(e){for(var t=e.split("\r\n"),n=0;n<t.length;n++){var r=t[n],o=r.indexOf(":");if(o>0){var s=r.substring(0,o).trim(),i=r.substring(o+1).trim();this.append(s,i)}}},e.prototype.delete=function(e,t){var n=r.normalizeName(e);if(void 0===t)delete this.headersMap[n];else{var o=this.headersMap[n];if(o){var s=o.indexOf(t);s>=0&&o.splice(s,1),0===o.length&&delete this.headersMap[n]}}},e.prototype.append=function(e,t){var n=this,o=r.normalizeName(e);Array.isArray(this.headersMap[o])||(this.headersMap[o]=[]),Array.isArray(t)?t.forEach(function(e){n.headersMap[o].push(r.normalizeValue(e))}):this.headersMap[o].push(r.normalizeValue(t))},e.prototype.set=function(e,t){var n=r.normalizeName(e);if(Array.isArray(t)){var o=[];t.forEach(function(e){o.push(r.normalizeValue(e))}),this.headersMap[n]=o}else this.headersMap[n]=[r.normalizeValue(t)]},e.prototype.has=function(e,t){var n=this.headersMap[r.normalizeName(e)];if(!Array.isArray(n))return!1;if(void 0!==t){var o=r.normalizeValue(t);return n.indexOf(o)>=0}return!0},e.prototype.get=function(e){var t=this.headersMap[r.normalizeName(e)];return void 0!==t?t.concat():[]},e.prototype.forEach=function(e){var t=this;Object.getOwnPropertyNames(this.headersMap).forEach(function(n){e(n,t.headersMap[n])},this)},e.prototype.toHeaders=function(){if("undefined"!=typeof Headers){var e=new Headers;return this.forEach(function(t,n){n.forEach(function(n){e.append(t,n)})}),e}throw new Error("Headers class is not defined")},e}();t.BrowserHeaders=o},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0);t.BrowserHeaders=r.BrowserHeaders},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.iterateHeaders=function(e,t){for(var n=e[Symbol.iterator](),r=n.next();!r.done;)t(r.value[0]),r=n.next()},t.iterateHeadersKeys=function(e,t){for(var n=e.keys(),r=n.next();!r.done;)t(r.value),r=n.next()}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(2);function o(e){return e}t.normalizeName=function(e){if("string"!=typeof e&&(e=String(e)),/[^a-z0-9\-#$%&'*+.\^_`|~]/i.test(e))throw new TypeError("Invalid character in header field name");return e.toLowerCase()},t.normalizeValue=function(e){return"string"!=typeof e&&(e=String(e)),e},t.getHeaderValues=function(e,t){var n=o(e);if(n instanceof Headers&&n.getAll)return n.getAll(t);var r=n.get(t);return r&&"string"==typeof r?[r]:r},t.getHeaderKeys=function(e){var t=o(e),n={},s=[];return t.keys?r.iterateHeadersKeys(t,function(e){n[e]||(n[e]=!0,s.push(e))}):t.forEach?t.forEach(function(e,t){n[t]||(n[t]=!0,s.push(t))}):r.iterateHeaders(t,function(e){var t=e[0];n[t]||(n[t]=!0,s.push(t))}),s},t.splitHeaderValue=function(e){var t=[];return e.split(", ").forEach(function(e){e.split(",").forEach(function(e){t.push(e)})}),t}}])},e.exports=r()},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(6),o=function(e){return r.CrossBrowserHttpTransport({withCredentials:!1})(e)};t.setDefaultTransportFactory=function(e){o=e},t.makeDefaultTransport=function(e){return o(e)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(7),o=n(8);t.CrossBrowserHttpTransport=function(e){if(r.detectFetchSupport()){var t={credentials:e.withCredentials?"include":"same-origin"};return r.FetchReadableStreamTransport(t)}return o.XhrTransport({withCredentials:e.withCredentials})}},function(e,t,n){"use strict";var r=this&&this.__assign||function(){return(r=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};Object.defineProperty(t,"__esModule",{value:!0});var o=n(0),s=n(1),i=n(2);t.FetchReadableStreamTransport=function(e){return function(t){return function(e,t){return e.debug&&s.debug("fetchRequest",e),new a(e,t)}(t,e)}};var a=function(){function e(e,t){this.cancelled=!1,this.controller=self.AbortController&&new AbortController,this.options=e,this.init=t}return e.prototype.pump=function(e,t){var n=this;if(this.reader=e,this.cancelled)return this.options.debug&&s.debug("Fetch.pump.cancel at first pump"),void this.reader.cancel();this.reader.read().then(function(e){if(e.done)return i.default(function(){n.options.onEnd()}),t;i.default(function(){n.options.onChunk(e.value)}),n.pump(n.reader,t)}).catch(function(e){n.cancelled?n.options.debug&&s.debug("Fetch.catch - request cancelled"):(n.cancelled=!0,n.options.debug&&s.debug("Fetch.catch",e.message),i.default(function(){n.options.onEnd(e)}))})},e.prototype.send=function(e){var t=this;fetch(this.options.url,r({},this.init,{headers:this.metadata.toHeaders(),method:"POST",body:e,signal:this.controller&&this.controller.signal})).then(function(e){if(t.options.debug&&s.debug("Fetch.response",e),i.default(function(){t.options.onHeaders(new o.Metadata(e.headers),e.status)}),!e.body)return e;t.pump(e.body.getReader(),e)}).catch(function(e){t.cancelled?t.options.debug&&s.debug("Fetch.catch - request cancelled"):(t.cancelled=!0,t.options.debug&&s.debug("Fetch.catch",e.message),i.default(function(){t.options.onEnd(e)}))})},e.prototype.sendMessage=function(e){this.send(e)},e.prototype.finishSend=function(){},e.prototype.start=function(e){this.metadata=e},e.prototype.cancel=function(){this.cancelled?this.options.debug&&s.debug("Fetch.abort.cancel already cancelled"):(this.cancelled=!0,this.reader?(this.options.debug&&s.debug("Fetch.abort.cancel"),this.reader.cancel()):this.options.debug&&s.debug("Fetch.abort.cancel before reader"),this.controller&&this.controller.abort())},e}();t.detectFetchSupport=function(){return"undefined"!=typeof Response&&Response.prototype.hasOwnProperty("body")&&"function"==typeof Headers}},function(e,t,n){"use strict";var r,o=this&&this.__extends||(r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var n in t)t.hasOwnProperty(n)&&(e[n]=t[n])})(e,t)},function(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)});Object.defineProperty(t,"__esModule",{value:!0});var s=n(0),i=n(1),a=n(2),u=n(12);t.XhrTransport=function(e){return function(t){if(u.detectMozXHRSupport())return new c(t,e);if(u.detectXHROverrideMimeTypeSupport())return new d(t,e);throw new Error("This environment's XHR implementation cannot support binary transfer.")}};var d=function(){function e(e,t){this.options=e,this.init=t}return e.prototype.onProgressEvent=function(){var e=this;this.options.debug&&i.debug("XHR.onProgressEvent.length: ",this.xhr.response.length);var t=this.xhr.response.substr(this.index);this.index=this.xhr.response.length;var n=f(t);a.default(function(){e.options.onChunk(n)})},e.prototype.onLoadEvent=function(){var e=this;this.options.debug&&i.debug("XHR.onLoadEvent"),a.default(function(){e.options.onEnd()})},e.prototype.onStateChange=function(){var e=this;this.options.debug&&i.debug("XHR.onStateChange",this.xhr.readyState),this.xhr.readyState===XMLHttpRequest.HEADERS_RECEIVED&&a.default(function(){e.options.onHeaders(new s.Metadata(e.xhr.getAllResponseHeaders()),e.xhr.status)})},e.prototype.sendMessage=function(e){this.xhr.send(e)},e.prototype.finishSend=function(){},e.prototype.start=function(e){var t=this;this.metadata=e;var n=new XMLHttpRequest;this.xhr=n,n.open("POST",this.options.url),this.configureXhr(),this.metadata.forEach(function(e,t){n.setRequestHeader(e,t.join(", "))}),n.withCredentials=Boolean(this.init.withCredentials),n.addEventListener("readystatechange",this.onStateChange.bind(this)),n.addEventListener("progress",this.onProgressEvent.bind(this)),n.addEventListener("loadend",this.onLoadEvent.bind(this)),n.addEventListener("error",function(e){t.options.debug&&i.debug("XHR.error",e),a.default(function(){t.options.onEnd(e.error)})})},e.prototype.configureXhr=function(){this.xhr.responseType="text",this.xhr.overrideMimeType("text/plain; charset=x-user-defined")},e.prototype.cancel=function(){this.options.debug&&i.debug("XHR.abort"),this.xhr.abort()},e}();t.XHR=d;var c=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return o(t,e),t.prototype.configureXhr=function(){this.options.debug&&i.debug("MozXHR.configureXhr: setting responseType to 'moz-chunked-arraybuffer'"),this.xhr.responseType="moz-chunked-arraybuffer"},t.prototype.onProgressEvent=function(){var e=this,t=this.xhr.response;this.options.debug&&i.debug("MozXHR.onProgressEvent: ",new Uint8Array(t)),a.default(function(){e.options.onChunk(new Uint8Array(t))})},t}(d);function p(e,t){var n=e.charCodeAt(t);if(n>=55296&&n<=56319){var r=e.charCodeAt(t+1);r>=56320&&r<=57343&&(n=65536+(n-55296<<10)+(r-56320))}return n}function f(e){for(var t=new Uint8Array(e.length),n=0,r=0;r<e.length;r++){var o=String.prototype.codePointAt?e.codePointAt(r):p(e,r);t[n++]=255&o}return t}t.MozChunkedArrayBufferXHR=c,t.stringToArrayBuffer=f},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r,o=n(0),s=function(e){return 9===e||10===e||13===e};function i(e){return s(e)||e>=32&&e<=126}function a(e){for(var t=0;t!==e.length;++t)if(!i(e[t]))throw new Error("Metadata is not valid (printable) ASCII");return String.fromCharCode.apply(String,Array.prototype.slice.call(e))}function u(e){return 128==(128&e.getUint8(0))}function d(e){return e.getUint32(1,!1)}function c(e,t,n){return e.byteLength-t>=n}function p(e,t,n){if(e.slice)return e.slice(t,n);var r=e.length;void 0!==n&&(r=n);for(var o=new Uint8Array(r-t),s=0,i=t;i<r;i++)o[s++]=e[i];return o}t.decodeASCII=a,t.encodeASCII=function(e){for(var t=new Uint8Array(e.length),n=0;n!==e.length;++n){var r=e.charCodeAt(n);if(!i(r))throw new Error("Metadata contains invalid ASCII");t[n]=r}return t},function(e){e[e.MESSAGE=1]="MESSAGE",e[e.TRAILERS=2]="TRAILERS"}(r=t.ChunkType||(t.ChunkType={}));var f=function(){function e(){this.buffer=null,this.position=0}return e.prototype.parse=function(e,t){if(0===e.length&&t)return[];var n,s=[];if(null==this.buffer)this.buffer=e,this.position=0;else if(this.position===this.buffer.byteLength)this.buffer=e,this.position=0;else{var i=this.buffer.byteLength-this.position,f=new Uint8Array(i+e.byteLength),h=p(this.buffer,this.position);f.set(h,0);var l=new Uint8Array(e);f.set(l,i),this.buffer=f,this.position=0}for(;;){if(!c(this.buffer,this.position,5))return s;var g=p(this.buffer,this.position,this.position+5),b=new DataView(g.buffer,g.byteOffset,g.byteLength),y=d(b);if(!c(this.buffer,this.position,5+y))return s;var v=p(this.buffer,this.position+5,this.position+5+y);if(this.position+=5+y,u(b))return s.push({chunkType:r.TRAILERS,trailers:(n=v,new o.Metadata(a(n)))}),s;s.push({chunkType:r.MESSAGE,data:v})}},e}();t.ChunkParser=f},function(e,t,n){"use strict";var r;Object.defineProperty(t,"__esModule",{value:!0}),function(e){e[e.OK=0]="OK",e[e.Canceled=1]="Canceled",e[e.Unknown=2]="Unknown",e[e.InvalidArgument=3]="InvalidArgument",e[e.DeadlineExceeded=4]="DeadlineExceeded",e[e.NotFound=5]="NotFound",e[e.AlreadyExists=6]="AlreadyExists",e[e.PermissionDenied=7]="PermissionDenied",e[e.ResourceExhausted=8]="ResourceExhausted",e[e.FailedPrecondition=9]="FailedPrecondition",e[e.Aborted=10]="Aborted",e[e.OutOfRange=11]="OutOfRange",e[e.Unimplemented=12]="Unimplemented",e[e.Internal=13]="Internal",e[e.Unavailable=14]="Unavailable",e[e.DataLoss=15]="DataLoss",e[e.Unauthenticated=16]="Unauthenticated"}(r=t.Code||(t.Code={})),t.httpStatusToCode=function(e){switch(e){case 0:return r.Internal;case 200:return r.OK;case 400:return r.InvalidArgument;case 401:return r.Unauthenticated;case 403:return r.PermissionDenied;case 404:return r.NotFound;case 409:return r.Aborted;case 412:return r.FailedPrecondition;case 429:return r.ResourceExhausted;case 499:return r.Canceled;case 500:return r.Unknown;case 501:return r.Unimplemented;case 503:return r.Unavailable;case 504:return r.DeadlineExceeded;default:return r.Unknown}}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(4),o=n(5),s=n(7),i=n(13),a=n(8),u=n(6),d=n(10),c=n(14),p=n(16),f=n(3);!function(e){e.setDefaultTransport=o.setDefaultTransportFactory,e.CrossBrowserHttpTransport=u.CrossBrowserHttpTransport,e.FetchReadableStreamTransport=s.FetchReadableStreamTransport,e.XhrTransport=a.XhrTransport,e.WebsocketTransport=i.WebsocketTransport,e.Code=d.Code,e.Metadata=r.BrowserHeaders,e.client=function(e,t){return f.client(e,t)},e.invoke=c.invoke,e.unary=p.unary}(t.grpc||(t.grpc={}))},function(e,t,n){"use strict";var r;function o(){if(void 0!==r)return r;if(XMLHttpRequest){r=new XMLHttpRequest;try{r.open("GET","https://localhost")}catch(e){}}return r}function s(e){var t=o();if(!t)return!1;try{return t.responseType=e,t.responseType===e}catch(e){}return!1}Object.defineProperty(t,"__esModule",{value:!0}),t.xhrSupportsResponseType=s,t.detectMozXHRSupport=function(){return"undefined"!=typeof XMLHttpRequest&&s("moz-chunked-arraybuffer")},t.detectXHROverrideMimeTypeSupport=function(){return"undefined"!=typeof XMLHttpRequest&&XMLHttpRequest.prototype.hasOwnProperty("overrideMimeType")}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r,o=n(1),s=n(2),i=n(9);!function(e){e[e.FINISH_SEND=1]="FINISH_SEND"}(r||(r={}));var a=new Uint8Array([1]);t.WebsocketTransport=function(){return function(e){return function(e){e.debug&&o.debug("websocketRequest",e);var t,n=function(e){if("https://"===e.substr(0,8))return"wss://"+e.substr(8);if("http://"===e.substr(0,7))return"ws://"+e.substr(7);throw new Error("Websocket transport constructed with non-https:// or http:// host.")}(e.url),u=[];function d(e){if(e===r.FINISH_SEND)t.send(a);else{var n=e,o=new Int8Array(n.byteLength+1);o.set(new Uint8Array([0])),o.set(n,1),t.send(o)}}return{sendMessage:function(e){t&&t.readyState!==t.CONNECTING?d(e):u.push(e)},finishSend:function(){t&&t.readyState!==t.CONNECTING?d(r.FINISH_SEND):u.push(r.FINISH_SEND)},start:function(r){(t=new WebSocket(n,["grpc-websockets"])).binaryType="arraybuffer",t.onopen=function(){var n;e.debug&&o.debug("websocketRequest.onopen"),t.send((n="",r.forEach(function(e,t){n+=e+": "+t.join(", ")+"\r\n"}),i.encodeASCII(n))),u.forEach(function(e){d(e)})},t.onclose=function(t){e.debug&&o.debug("websocketRequest.onclose",t),s.default(function(){e.onEnd()})},t.onerror=function(t){e.debug&&o.debug("websocketRequest.onerror",t)},t.onmessage=function(t){s.default(function(){e.onChunk(new Uint8Array(t.data))})}},cancel:function(){e.debug&&o.debug("websocket.abort"),s.default(function(){t.close()})}}}(e)}}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(3);t.invoke=function(e,t){if(e.requestStream)throw new Error(".invoke cannot be used with client-streaming methods. Use .client instead.");var n=r.client(e,{host:t.host,transport:t.transport,debug:t.debug});return t.onHeaders&&n.onHeaders(t.onHeaders),t.onMessage&&n.onMessage(t.onMessage),t.onEnd&&n.onEnd(t.onEnd),n.start(t.metadata),n.send(t.request),n.finishSend(),{close:function(){n.close()}}}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.frameRequest=function(e){var t=e.serializeBinary(),n=new ArrayBuffer(t.byteLength+5);return new DataView(n,1,4).setUint32(0,t.length,!1),new Uint8Array(n,5).set(t),new Uint8Array(n)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(0),o=n(3);t.unary=function(e,t){if(e.responseStream)throw new Error(".unary cannot be used with server-streaming methods. Use .invoke or .client instead.");if(e.requestStream)throw new Error(".unary cannot be used with client-streaming methods. Use .client instead.");var n=null,s=null,i=o.client(e,{host:t.host,transport:t.transport,debug:t.debug});return i.onHeaders(function(e){n=e}),i.onMessage(function(e){s=e}),i.onEnd(function(e,o,i){t.onEnd({status:e,statusMessage:o,headers:n||new r.Metadata,message:s,trailers:i})}),i.start(t.metadata),i.send(t.request),i.finishSend(),{close:function(){i.close()}}}}])})},{}],7:[function(require,module,exports){module.exports=require("./lib/axios")},{"./lib/axios":9}],8:[function(require,module,exports){"use strict";var utils=require("./../utils");var settle=require("./../core/settle");var buildURL=require("./../helpers/buildURL");var parseHeaders=require("./../helpers/parseHeaders");var isURLSameOrigin=require("./../helpers/isURLSameOrigin");var createError=require("../core/createError");module.exports=function xhrAdapter(config){return new Promise(function dispatchXhrRequest(resolve,reject){var requestData=config.data;var requestHeaders=config.headers;if(utils.isFormData(requestData)){delete requestHeaders["Content-Type"]}var request=new XMLHttpRequest;if(config.auth){var username=config.auth.username||"";var password=config.auth.password||"";requestHeaders.Authorization="Basic "+btoa(username+":"+password)}request.open(config.method.toUpperCase(),buildURL(config.url,config.params,config.paramsSerializer),true);request.timeout=config.timeout;request.onreadystatechange=function handleLoad(){if(!request||request.readyState!==4){return}if(request.status===0&&!(request.responseURL&&request.responseURL.indexOf("file:")===0)){return}var responseHeaders="getAllResponseHeaders"in request?parseHeaders(request.getAllResponseHeaders()):null;var responseData=!config.responseType||config.responseType==="text"?request.responseText:request.response;var response={data:responseData,status:request.status,statusText:request.statusText,headers:responseHeaders,config:config,request:request};settle(resolve,reject,response);request=null};request.onabort=function handleAbort(){if(!request){return}reject(createError("Request aborted",config,"ECONNABORTED",request));request=null};request.onerror=function handleError(){reject(createError("Network Error",config,null,request));request=null};request.ontimeout=function handleTimeout(){reject(createError("timeout of "+config.timeout+"ms exceeded",config,"ECONNABORTED",request));request=null};if(utils.isStandardBrowserEnv()){var cookies=require("./../helpers/cookies");var xsrfValue=(config.withCredentials||isURLSameOrigin(config.url))&&config.xsrfCookieName?cookies.read(config.xsrfCookieName):undefined;if(xsrfValue){requestHeaders[config.xsrfHeaderName]=xsrfValue}}if("setRequestHeader"in request){utils.forEach(requestHeaders,function setRequestHeader(val,key){if(typeof requestData==="undefined"&&key.toLowerCase()==="content-type"){delete requestHeaders[key]}else{request.setRequestHeader(key,val)}})}if(config.withCredentials){request.withCredentials=true}if(config.responseType){try{request.responseType=config.responseType}catch(e){if(config.responseType!=="json"){throw e}}}if(typeof conf