UNPKG

slpjs-regtest

Version:

Simple Ledger Protocol (SLP) JavaScript Library

1 lines 2.37 MB
(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.slpjs=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){"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){if(k2===undefined)k2=k;Object.defineProperty(o,k2,{enumerable:true,get:function(){return m[k]}})}:function(o,m,k,k2){if(k2===undefined)k2=k;o[k2]=m[k]});var __setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:true,value:v})}:function(o,v){o["default"]=v});var __exportStar=this&&this.__exportStar||function(m,exports){for(var p in m)if(p!=="default"&&!exports.hasOwnProperty(p))__createBinding(exports,m,p)};var __importStar=this&&this.__importStar||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(k!=="default"&&Object.hasOwnProperty.call(mod,k))__createBinding(result,mod,k);__setModuleDefault(result,mod);return result};Object.defineProperty(exports,"__esModule",{value:true});exports.SlpAddressUtxoResult=exports.SlpUtxoJudgement=exports.SlpVersionType=exports.SlpTransactionType=exports.bitcore=void 0;__exportStar(require("./lib/slp"),exports);__exportStar(require("./lib/utils"),exports);__exportStar(require("./lib/crypto"),exports);__exportStar(require("./lib/primatives"),exports);__exportStar(require("./lib/bitdbnetwork"),exports);__exportStar(require("./lib/localvalidator"),exports);__exportStar(require("./lib/trustedvalidator"),exports);__exportStar(require("./lib/bitboxnetwork"),exports);__exportStar(require("./lib/bchdnetwork"),exports);__exportStar(require("./lib/transactionhelpers"),exports);var bitcore=__importStar(require("bitcore-lib-cash"));exports.bitcore=bitcore;var SlpTransactionType;(function(SlpTransactionType){SlpTransactionType["GENESIS"]="GENESIS";SlpTransactionType["MINT"]="MINT";SlpTransactionType["SEND"]="SEND"})(SlpTransactionType=exports.SlpTransactionType||(exports.SlpTransactionType={}));var SlpVersionType;(function(SlpVersionType){SlpVersionType[SlpVersionType["TokenVersionType1"]=1]="TokenVersionType1";SlpVersionType[SlpVersionType["TokenVersionType1_NFT_Child"]=65]="TokenVersionType1_NFT_Child";SlpVersionType[SlpVersionType["TokenVersionType1_NFT_Parent"]=129]="TokenVersionType1_NFT_Parent"})(SlpVersionType=exports.SlpVersionType||(exports.SlpVersionType={}));var SlpUtxoJudgement;(function(SlpUtxoJudgement){SlpUtxoJudgement["UNKNOWN"]="UNKNOWN";SlpUtxoJudgement["INVALID_BATON_DAG"]="INVALID_BATON_DAG";SlpUtxoJudgement["INVALID_TOKEN_DAG"]="INVALID_TOKEN_DAG";SlpUtxoJudgement["NOT_SLP"]="NOT_SLP";SlpUtxoJudgement["SLP_TOKEN"]="SLP_TOKEN";SlpUtxoJudgement["SLP_BATON"]="SLP_BATON";SlpUtxoJudgement["UNSUPPORTED_TYPE"]="UNSUPPORTED_TYPE"})(SlpUtxoJudgement=exports.SlpUtxoJudgement||(exports.SlpUtxoJudgement={}));var SlpAddressUtxoResult=function(){function SlpAddressUtxoResult(){this.slpUtxoJudgement=SlpUtxoJudgement.UNKNOWN}return SlpAddressUtxoResult}();exports.SlpAddressUtxoResult=SlpAddressUtxoResult},{"./lib/bchdnetwork":2,"./lib/bitboxnetwork":3,"./lib/bitdbnetwork":4,"./lib/crypto":5,"./lib/localvalidator":6,"./lib/primatives":7,"./lib/slp":9,"./lib/transactionhelpers":11,"./lib/trustedvalidator":12,"./lib/utils":13,"bitcore-lib-cash":70}],2:[function(require,module,exports){(function(Buffer){"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){if(k2===undefined)k2=k;Object.defineProperty(o,k2,{enumerable:true,get:function(){return m[k]}})}:function(o,m,k,k2){if(k2===undefined)k2=k;o[k2]=m[k]});var __setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:true,value:v})}:function(o,v){o["default"]=v});var __importStar=this&&this.__importStar||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(k!=="default"&&Object.hasOwnProperty.call(mod,k))__createBinding(result,mod,k);__setModuleDefault(result,mod);return result};var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=this&&this.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]<t[3])){_.label=op[1];break}if(op[0]===6&&_.label<t[1]){_.label=t[1];t=op;break}if(t&&_.label<t[2]){_.label=t[2];_.ops.push(op);break}if(t[2])_.ops.pop();_.trys.pop();continue}op=body.call(thisArg,_)}catch(e){op=[6,e];y=0}finally{f=t=0}if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true}}};var __values=this&&this.__values||function(o){var s=typeof Symbol==="function"&&Symbol.iterator,m=s&&o[s],i=0;if(m)return m.call(o);if(o&&typeof o.length==="number")return{next:function(){if(o&&i>=o.length)o=void 0;return{value:o&&o[i++],done:!o}}};throw new TypeError(s?"Object is not iterable.":"Symbol.iterator is not defined.")};var __read=this&&this.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spread=this&&this.__spread||function(){for(var ar=[],i=0;i<arguments.length;i++)ar=ar.concat(__read(arguments[i]));return ar};Object.defineProperty(exports,"__esModule",{value:true});exports.BchdNetwork=void 0;var bchaddr=__importStar(require("bchaddrjs-slp"));var Bitcore=__importStar(require("bitcore-lib-cash"));var grpc_bchrpc_node_1=require("grpc-bchrpc-node");var _=__importStar(require("lodash"));var index_1=require("../index");var slp_1=require("./slp");var transactionhelpers_1=require("./transactionhelpers");var utils_1=require("./utils");var sleep=function(ms){return new Promise(function(resolve){return setTimeout(resolve,ms)})};var BchdNetwork=function(){function BchdNetwork(_a){var BITBOX=_a.BITBOX,validator=_a.validator,logger=_a.logger,client=_a.client;this.logger={log:function(s){return null}};if(!BITBOX){throw Error("Must provide BITBOX instance to class constructor.")}if(!client||!(client instanceof grpc_bchrpc_node_1.GrpcClient)){throw Error("Must provide instance of GrpClient to class constructor.")}if(logger){this.logger=logger}this.validator=validator;this.BITBOX=BITBOX;this.client=client;this.slp=new slp_1.Slp(BITBOX);this.txnHelpers=new transactionhelpers_1.TransactionHelpers(this.slp)}BchdNetwork.prototype.getTokenInformation=function(txid,decimalConversion){if(decimalConversion===void 0){decimalConversion=false}return __awaiter(this,void 0,void 0,function(){var txhex,_a,_b,txn,slpMsg;return __generator(this,function(_c){switch(_c.label){case 0:_b=(_a=Buffer).from;return[4,this.client.getRawTransaction({hash:txid,reversedHashOrder:true})];case 1:txhex=_b.apply(_a,[_c.sent().getTransaction_asU8()]);txn=new Bitcore.Transaction(txhex);slpMsg=this.slp.parseSlpOutputScript(txn.outputs[0]._scriptBuffer);if(decimalConversion){if([index_1.SlpTransactionType.GENESIS,index_1.SlpTransactionType.MINT].includes(slpMsg.transactionType)){slpMsg.genesisOrMintQuantity=slpMsg.genesisOrMintQuantity.dividedBy(Math.pow(10,slpMsg.decimals))}else{slpMsg.sendOutputs.map(function(o){return o.dividedBy(Math.pow(10,slpMsg.decimals))})}}if(index_1.SlpTransactionType.GENESIS===slpMsg.transactionType){slpMsg.tokenIdHex=txid}return[2,slpMsg]}})})};BchdNetwork.prototype.getTransactionDetails=function(txid){return __awaiter(this,void 0,void 0,function(){var res;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.getTransactionDetailsWithRetry([txid],1)];case 1:res=_a.sent();if(!res){return[2,res]}return[2,res[0]]}})})};BchdNetwork.prototype.getUtxos=function(address){return __awaiter(this,void 0,void 0,function(){var cashAddress,legacyAddress,res,scriptPubKey,bestHeight,utxos;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}cashAddress=bchaddr.toCashAddress(address);legacyAddress=bchaddr.toLegacyAddress(address);return[4,this.client.getAddressUtxos({address:address,includeMempool:true})];case 1:res=_a.sent().getOutputsList();if(res.length===0){return[2,{cashAddress:cashAddress,legacyAddress:legacyAddress,scriptPubKey:null,utxos:[]}]}scriptPubKey=Buffer.from(res[0].getPubkeyScript_asU8()).toString("hex");return[4,this.client.getBlockchainInfo()];case 2:bestHeight=_a.sent().getBestHeight();utxos=[];if(res.length>0){utxos=res.map(function(txo){return{satoshis:txo.getValue(),height:txo.getBlockHeight()<2147483647?txo.getBlockHeight():-1,confirmations:txo.getBlockHeight()<2147483647?bestHeight-txo.getBlockHeight()+1:null,txid:Buffer.from(txo.getOutpoint().getHash_asU8().reverse()).toString("hex"),vout:txo.getOutpoint().getIndex(),amount:txo.getValue()/Math.pow(10,8)}})}return[2,{cashAddress:cashAddress,legacyAddress:legacyAddress,scriptPubKey:scriptPubKey,utxos:utxos}]}})})};BchdNetwork.prototype.getAllSlpBalancesAndUtxos=function(address){return __awaiter(this,void 0,void 0,function(){var result,results,address_1,address_1_1,addr,utxos,_a,_b,_c,e_1_1;var e_1,_d;return __generator(this,function(_e){switch(_e.label){case 0:if(!(typeof address==="string"))return[3,3];address=bchaddr.toCashAddress(address);return[4,this.getUtxoWithTxDetails(address)];case 1:result=_e.sent();return[4,this.processUtxosForSlp(result)];case 2:return[2,_e.sent()];case 3:address=address.map(function(a){return bchaddr.toCashAddress(a)});results=[];_e.label=4;case 4:_e.trys.push([4,10,11,12]);address_1=__values(address),address_1_1=address_1.next();_e.label=5;case 5:if(!!address_1_1.done)return[3,9];addr=address_1_1.value;return[4,this.getUtxoWithTxDetails(addr)];case 6:utxos=_e.sent();_b=(_a=results).push;_c={address:utils_1.Utils.toSlpAddress(addr)};return[4,this.processUtxosForSlp(utxos)];case 7:_b.apply(_a,[(_c.result=_e.sent(),_c)]);_e.label=8;case 8:address_1_1=address_1.next();return[3,5];case 9:return[3,12];case 10:e_1_1=_e.sent();e_1={error:e_1_1};return[3,12];case 11:try{if(address_1_1&&!address_1_1.done&&(_d=address_1.return))_d.call(address_1)}finally{if(e_1)throw e_1.error}return[7];case 12:return[2,results]}})})};BchdNetwork.prototype.simpleTokenSend=function(tokenId,sendAmounts,inputUtxos,tokenReceiverAddresses,changeReceiverAddress,requiredNonTokenOutputs){if(requiredNonTokenOutputs===void 0){requiredNonTokenOutputs=[]}return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenSend({tokenId:tokenId,sendAmounts:sendAmounts,inputUtxos:inputUtxos,tokenReceiverAddresses:tokenReceiverAddresses,changeReceiverAddress:changeReceiverAddress,requiredNonTokenOutputs:requiredNonTokenOutputs});if(!inputUtxos.every(function(i){return typeof i.wif==="string"})){throw Error("The BitboxNetwork version of this method requires a private key WIF be provided with each input."+"If you want more control over the signing process use Slp.simpleTokenSend() to get the unsigned transaction,"+"then after the transaction is signed you can use BitboxNetwork.sendTx()")}return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleBchSend=function(sendAmounts,inputUtxos,bchReceiverAddresses,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleBchSend({sendAmounts:sendAmounts,inputUtxos:inputUtxos,bchReceiverAddresses:bchReceiverAddresses,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleTokenGenesis=function(tokenName,tokenTicker,tokenAmount,documentUri,documentHash,decimals,tokenReceiverAddress,batonReceiverAddress,bchChangeReceiverAddress,inputUtxos){return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleTokenGenesis({tokenName:tokenName,tokenTicker:tokenTicker,tokenAmount:tokenAmount,documentUri:documentUri,documentHash:documentHash,decimals:decimals,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleNFT1ParentGenesis=function(tokenName,tokenTicker,tokenAmount,documentUri,documentHash,tokenReceiverAddress,batonReceiverAddress,bchChangeReceiverAddress,inputUtxos,decimals){if(decimals===void 0){decimals=0}return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleNFT1ParentGenesis({tokenName:tokenName,tokenTicker:tokenTicker,tokenAmount:tokenAmount,documentUri:documentUri,documentHash:documentHash,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos,decimals:decimals});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleNFT1ChildGenesis=function(nft1GroupId,tokenName,tokenTicker,documentUri,documentHash,tokenReceiverAddress,bchChangeReceiverAddress,inputUtxos,allowBurnAnyAmount){if(allowBurnAnyAmount===void 0){allowBurnAnyAmount=false}return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleNFT1ChildGenesis({nft1GroupId:nft1GroupId,tokenName:tokenName,tokenTicker:tokenTicker,documentUri:documentUri,documentHash:documentHash,tokenReceiverAddress:tokenReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos,allowBurnAnyAmount:allowBurnAnyAmount});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleTokenMint=function(tokenId,mintAmount,inputUtxos,tokenReceiverAddress,batonReceiverAddress,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenMint({tokenId:tokenId,mintAmount:mintAmount,inputUtxos:inputUtxos,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.simpleTokenBurn=function(tokenId,burnAmount,inputUtxos,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenBurn({tokenId:tokenId,burnAmount:burnAmount,inputUtxos:inputUtxos,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.getUtxoWithRetry=function(address,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.getUtxos(address)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.getUtxoWithTxDetails=function(address){return __awaiter(this,void 0,void 0,function(){var res,utxos,txIds,txDetails;var _this=this;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.getUtxos(address)];case 1:res=_a.sent();utxos=utils_1.Utils.mapToSlpAddressUtxoResultArray(res);txIds=utxos.map(function(i){return i.txid});if(txIds.length===0){return[2,[]]}return[4,Promise.all(_.chunk(txIds,20).map(function(txids){return _this.getTransactionDetailsWithRetry(__spread(new Set(txids)))}))];case 2:txDetails=_a.sent();txDetails=[].concat.apply([],__spread(txDetails));utxos=utxos.map(function(i){i.tx=txDetails.find(function(d){return d.txid===i.txid});return i});return[2,utxos]}})})};BchdNetwork.prototype.getTransactionDetailsWithRetry=function(txids,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){var results,count,txids_1,txids_1_1,txid,res,e_2_1,txns,txns_1,txns_1_1,txn,_a,_b,_c,_d,e_3_1;var e_2,_e,e_3,_f;return __generator(this,function(_g){switch(_g.label){case 0:results=[];count=0;_g.label=1;case 1:if(!(results.length!==txids.length))return[3,25];_g.label=2;case 2:_g.trys.push([2,7,8,9]);txids_1=(e_2=void 0,__values(txids)),txids_1_1=txids_1.next();_g.label=3;case 3:if(!!txids_1_1.done)return[3,6];txid=txids_1_1.value;return[4,this.client.getTransaction({hash:txid,reversedHashOrder:true})];case 4:res=_g.sent().getTransaction();if(res){results.push(res)}_g.label=5;case 5:txids_1_1=txids_1.next();return[3,3];case 6:return[3,9];case 7:e_2_1=_g.sent();e_2={error:e_2_1};return[3,9];case 8:try{if(txids_1_1&&!txids_1_1.done&&(_e=txids_1.return))_e.call(txids_1)}finally{if(e_2)throw e_2.error}return[7];case 9:if(!(results.length===txids.length))return[3,23];txns=void 0;txns=results.map(function(res){return{txid:Buffer.from(res.getHash_asU8().reverse()).toString("hex"),version:res.getVersion(),locktime:res.getLockTime(),vin:res.getInputsList().map(function(i){return{n:i.getIndex(),sequence:i.getSequence(),coinbase:null}}),vout:res.getOutputsList().map(function(o){return{value:o.getValue(),n:o.getIndex(),scriptPubKey:{hex:Buffer.from(o.getPubkeyScript_asU8()).toString("hex"),asm:o.getDisassembledScript()}}}),time:res.getTimestamp(),blockhash:Buffer.from(res.getBlockHash_asU8().reverse()).toString("hex"),blockheight:res.getBlockHeight(),isCoinBase:false,valueOut:res.getOutputsList().reduce(function(p,o){return p+=o.getValue()},0),size:res.getSize()}});_g.label=10;case 10:_g.trys.push([10,20,21,22]);txns_1=(e_3=void 0,__values(txns)),txns_1_1=txns_1.next();_g.label=11;case 11:if(!!txns_1_1.done)return[3,19];txn=txns_1_1.value;txn.vin.forEach(function(input){try{input.slpAddress=utils_1.Utils.toSlpAddress(input.legacyAddress)}catch(_){}});txn.vout.forEach(function(output){try{output.scriptPubKey.slpAddrs=[utils_1.Utils.toSlpAddress(output.scriptPubKey.cashAddrs[0])]}catch(_){}});_a=txn;return[4,this.getTokenInformation(txn.txid,true)];case 12:_a.tokenInfo=_g.sent();_b=txn;if(!this.validator)return[3,14];return[4,this.validator.isValidSlpTxid(txn.txid,null,null,this.logger)];case 13:_c=_g.sent();return[3,16];case 14:return[4,this.isValidSlpTxid(txn.txid)];case 15:_c=_g.sent();_g.label=16;case 16:_b.tokenIsValid=_c;if(!(txn.tokenIsValid&&txn.tokenInfo.versionType===index_1.SlpVersionType.TokenVersionType1_NFT_Child))return[3,18];console.log("test");_d=txn;return[4,this.getNftParentId(txn.tokenInfo.tokenIdHex)];case 17:_d.tokenNftParentId=_g.sent();_g.label=18;case 18:txns_1_1=txns_1.next();return[3,11];case 19:return[3,22];case 20:e_3_1=_g.sent();e_3={error:e_3_1};return[3,22];case 21:try{if(txns_1_1&&!txns_1_1.done&&(_f=txns_1.return))_f.call(txns_1)}finally{if(e_3)throw e_3.error}return[7];case 22:return[2,txns];case 23:count++;if(count>retries){throw new Error("gRPC client.getTransaction endpoint experienced a problem")}return[4,sleep(250)];case 24:_g.sent();return[3,1];case 25:return[2]}})})};BchdNetwork.prototype.getAddressDetailsWithRetry=function(address,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){var utxos,balance,utxosMempool,mempoolBalance;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}return[4,this.client.getAddressUtxos({address:address,includeMempool:false})];case 1:utxos=_a.sent().getOutputsList();balance=utxos.reduce(function(p,o){return o.getValue()},0);return[4,this.client.getAddressUtxos({address:address,includeMempool:true})];case 2:utxosMempool=_a.sent().getOutputsList();mempoolBalance=utxosMempool.reduce(function(p,o){return o.getValue()},0);return[2,{balance:balance,balanceSat:balance*Math.pow(10,8),totalReceived:null,totalReceivedSat:null,totalSent:null,totalSentSat:null,unconfirmedBalance:mempoolBalance-balance,unconfirmedBalanceSat:mempoolBalance*Math.pow(10,8)-balance*Math.pow(10,8),unconfirmedTxApperances:null,txApperances:null,transactions:null,legacyAddress:bchaddr.toLegacyAddress(address),cashAddress:bchaddr.toCashAddress(address),slpAddress:bchaddr.toSlpAddress(address)}]}})})};BchdNetwork.prototype.sendTx=function(hex){return __awaiter(this,void 0,void 0,function(){var res;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.client.submitTransaction({txnHex:hex})];case 1:res=_a.sent();return[2,Buffer.from(res.getHash_asU8().reverse()).toString("hex")]}})})};BchdNetwork.prototype.monitorForPayment=function(paymentAddress,fee,onPaymentCB){return __awaiter(this,void 0,void 0,function(){var utxo,ex_1;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(paymentAddress)&&!bchaddr.isLegacyAddress(paymentAddress)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}_a.label=1;case 1:if(!true)return[3,7];_a.label=2;case 2:_a.trys.push([2,4,,5]);return[4,this.getUtxos(paymentAddress)];case 3:utxo=_a.sent();if(utxo&&utxo.utxos[0].satoshis>=fee){return[3,7]}return[3,5];case 4:ex_1=_a.sent();console.log(ex_1);return[3,5];case 5:return[4,sleep(2e3)];case 6:_a.sent();return[3,1];case 7:onPaymentCB();return[2]}})})};BchdNetwork.prototype.processUtxosForSlp=function(utxos){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.slp.processUtxosForSlpAbstract(utxos,this)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.getRawTransactions=function(txids){return __awaiter(this,void 0,void 0,function(){var getTxnHex;var _this=this;return __generator(this,function(_a){switch(_a.label){case 0:if(!(this.validator&&this.validator.getRawTransactions))return[3,2];return[4,this.validator.getRawTransactions(txids)];case 1:return[2,_a.sent()];case 2:getTxnHex=function(txid){return __awaiter(_this,void 0,void 0,function(){var _a,_b;return __generator(this,function(_c){switch(_c.label){case 0:_b=(_a=Buffer).from;return[4,this.client.getRawTransaction({hash:txid,reversedHashOrder:true})];case 1:return[2,_b.apply(_a,[_c.sent().getTransaction_asU8()]).toString("hex")]}})})};return[4,Promise.all(txids.map(function(txid){return getTxnHex(txid)}))];case 3:return[2,_a.sent()]}})})};BchdNetwork.prototype.isValidSlpTxid=function(txid){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.validator.isValidSlpTxid(txid,null,null,this.logger)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.validateSlpTransactions=function(txids){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.validator.validateSlpTransactions(txids)];case 1:return[2,_a.sent()]}})})};BchdNetwork.prototype.getNftParentId=function(tokenIdHex){return __awaiter(this,void 0,void 0,function(){var txnhex,tx,nftBurnTxnHex,nftBurnTxn,slp,nftBurnSlp;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.getRawTransactions([tokenIdHex])];case 1:txnhex=_a.sent()[0];tx=index_1.Primatives.Transaction.parseFromBuffer(Buffer.from(txnhex,"hex"));return[4,this.getRawTransactions([tx.inputs[0].previousTxHash])];case 2:nftBurnTxnHex=_a.sent()[0];nftBurnTxn=index_1.Primatives.Transaction.parseFromBuffer(Buffer.from(nftBurnTxnHex,"hex"));slp=new slp_1.Slp(this.BITBOX);nftBurnSlp=slp.parseSlpOutputScript(Buffer.from(nftBurnTxn.outputs[0].scriptPubKey));if(nftBurnSlp.transactionType===index_1.SlpTransactionType.GENESIS){return[2,tx.inputs[0].previousTxHash]}else{return[2,nftBurnSlp.tokenIdHex]}return[2]}})})};return BchdNetwork}();exports.BchdNetwork=BchdNetwork}).call(this,require("buffer").Buffer)},{"../index":1,"./slp":9,"./transactionhelpers":11,"./utils":13,"bchaddrjs-slp":67,"bitcore-lib-cash":70,buffer:193,"grpc-bchrpc-node":297,lodash:351}],3:[function(require,module,exports){(function(Buffer){"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(o,m,k,k2){if(k2===undefined)k2=k;Object.defineProperty(o,k2,{enumerable:true,get:function(){return m[k]}})}:function(o,m,k,k2){if(k2===undefined)k2=k;o[k2]=m[k]});var __setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(o,v){Object.defineProperty(o,"default",{enumerable:true,value:v})}:function(o,v){o["default"]=v});var __importStar=this&&this.__importStar||function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k in mod)if(k!=="default"&&Object.hasOwnProperty.call(mod,k))__createBinding(result,mod,k);__setModuleDefault(result,mod);return result};var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=this&&this.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]<t[3])){_.label=op[1];break}if(op[0]===6&&_.label<t[1]){_.label=t[1];t=op;break}if(t&&_.label<t[2]){_.label=t[2];_.ops.push(op);break}if(t[2])_.ops.pop();_.trys.pop();continue}op=body.call(thisArg,_)}catch(e){op=[6,e];y=0}finally{f=t=0}if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true}}};var __read=this&&this.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spread=this&&this.__spread||function(){for(var ar=[],i=0;i<arguments.length;i++)ar=ar.concat(__read(arguments[i]));return ar};var __importDefault=this&&this.__importDefault||function(mod){return mod&&mod.__esModule?mod:{default:mod}};Object.defineProperty(exports,"__esModule",{value:true});exports.BitboxNetwork=void 0;var axios_1=__importDefault(require("axios"));var bchaddr=__importStar(require("bchaddrjs-slp"));var Bitcore=__importStar(require("bitcore-lib-cash"));var _=__importStar(require("lodash"));var index_1=require("../index");var slp_1=require("./slp");var transactionhelpers_1=require("./transactionhelpers");var utils_1=require("./utils");var sleep=function(ms){return new Promise(function(resolve){return setTimeout(resolve,ms)})};var BitboxNetwork=function(){function BitboxNetwork(BITBOX,validator,logger){this.logger={log:function(s){return null}};if(!BITBOX){throw Error("Must provide BITBOX instance to class constructor.")}if(logger){this.logger=logger}if(validator){this.validator=validator}this.BITBOX=BITBOX;this.slp=new slp_1.Slp(BITBOX);this.txnHelpers=new transactionhelpers_1.TransactionHelpers(this.slp)}BitboxNetwork.prototype.getTokenInformation=function(txid,decimalConversion){if(decimalConversion===void 0){decimalConversion=false}return __awaiter(this,void 0,void 0,function(){var res,e_1,txhex,txn,slpMsg;return __generator(this,function(_a){switch(_a.label){case 0:_a.trys.push([0,2,,3]);return[4,this.BITBOX.RawTransactions.getRawTransaction([txid])];case 1:res=_a.sent();return[3,3];case 2:e_1=_a.sent();throw Error(e_1.error);case 3:if(!Array.isArray(res)||res.length!==1){throw Error("BITBOX response error for 'RawTransactions.getRawTransaction'")}txhex=res[0];txn=new Bitcore.Transaction(txhex);slpMsg=this.slp.parseSlpOutputScript(txn.outputs[0]._scriptBuffer);if(decimalConversion){if([index_1.SlpTransactionType.GENESIS,index_1.SlpTransactionType.MINT].includes(slpMsg.transactionType)){slpMsg.genesisOrMintQuantity=slpMsg.genesisOrMintQuantity.dividedBy(Math.pow(10,slpMsg.decimals))}else{slpMsg.sendOutputs.map(function(o){return o.dividedBy(Math.pow(10,slpMsg.decimals))})}}if(index_1.SlpTransactionType.GENESIS===slpMsg.transactionType){slpMsg.tokenIdHex=txid}return[2,slpMsg]}})})};BitboxNetwork.prototype.getTransactionDetails=function(txid,decimalConversion){if(decimalConversion===void 0){decimalConversion=false}return __awaiter(this,void 0,void 0,function(){var txn,_a,_b,_c,_d;return __generator(this,function(_e){switch(_e.label){case 0:return[4,this.BITBOX.Transaction.details([txid])];case 1:txn=_e.sent()[0];txn.vin.forEach(function(input){try{input.slpAddress=utils_1.Utils.toSlpAddress(input.legacyAddress)}catch(_){}});txn.vout.forEach(function(output){try{output.scriptPubKey.slpAddrs=[utils_1.Utils.toSlpAddress(output.scriptPubKey.cashAddrs[0])]}catch(_){}});_a=txn;return[4,this.getTokenInformation(txid,decimalConversion)];case 2:_a.tokenInfo=_e.sent();_b=txn;if(!this.validator)return[3,4];return[4,this.validator.isValidSlpTxid(txid,null,null,this.logger)];case 3:_c=_e.sent();return[3,6];case 4:return[4,this.isValidSlpTxid(txid)];case 5:_c=_e.sent();_e.label=6;case 6:_b.tokenIsValid=_c;if(!(txn.tokenIsValid&&txn.tokenInfo.versionType===index_1.SlpVersionType.TokenVersionType1_NFT_Child))return[3,8];_d=txn;return[4,this.getNftParentId(txn.tokenInfo.tokenIdHex)];case 7:_d.tokenNftParentId=_e.sent();_e.label=8;case 8:return[2,txn]}})})};BitboxNetwork.prototype.getUtxos=function(address){return __awaiter(this,void 0,void 0,function(){var res;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}return[4,this.BITBOX.Address.utxo([address])];case 1:res=_a.sent()[0];return[2,res]}})})};BitboxNetwork.prototype.getAllSlpBalancesAndUtxos=function(address){return __awaiter(this,void 0,void 0,function(){var result,results,i,utxos,_a,_b,_c;return __generator(this,function(_d){switch(_d.label){case 0:if(!(typeof address==="string"))return[3,3];address=bchaddr.toCashAddress(address);return[4,this.getUtxoWithTxDetails(address)];case 1:result=_d.sent();return[4,this.processUtxosForSlp(result)];case 2:return[2,_d.sent()];case 3:address=address.map(function(a){return bchaddr.toCashAddress(a)});results=[];i=0;_d.label=4;case 4:if(!(i<address.length))return[3,8];return[4,this.getUtxoWithTxDetails(address[i])];case 5:utxos=_d.sent();_b=(_a=results).push;_c={address:utils_1.Utils.toSlpAddress(address[i])};return[4,this.processUtxosForSlp(utxos)];case 6:_b.apply(_a,[(_c.result=_d.sent(),_c)]);_d.label=7;case 7:i++;return[3,4];case 8:return[2,results]}})})};BitboxNetwork.prototype.simpleTokenSend=function(tokenId,sendAmounts,inputUtxos,tokenReceiverAddresses,changeReceiverAddress,requiredNonTokenOutputs){if(requiredNonTokenOutputs===void 0){requiredNonTokenOutputs=[]}return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenSend({tokenId:tokenId,sendAmounts:sendAmounts,inputUtxos:inputUtxos,tokenReceiverAddresses:tokenReceiverAddresses,changeReceiverAddress:changeReceiverAddress,requiredNonTokenOutputs:requiredNonTokenOutputs});if(!inputUtxos.every(function(i){return typeof i.wif==="string"})){throw Error("The BitboxNetwork version of this method requires a private key WIF be provided with each input."+"If you want more control over the signing process use Slp.simpleTokenSend() to get the unsigned transaction,"+"then after the transaction is signed you can use BitboxNetwork.sendTx()")}return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleBchSend=function(sendAmounts,inputUtxos,bchReceiverAddresses,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleBchSend({sendAmounts:sendAmounts,inputUtxos:inputUtxos,bchReceiverAddresses:bchReceiverAddresses,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleTokenGenesis=function(tokenName,tokenTicker,tokenAmount,documentUri,documentHash,decimals,tokenReceiverAddress,batonReceiverAddress,bchChangeReceiverAddress,inputUtxos){return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleTokenGenesis({tokenName:tokenName,tokenTicker:tokenTicker,tokenAmount:tokenAmount,documentUri:documentUri,documentHash:documentHash,decimals:decimals,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleNFT1ParentGenesis=function(tokenName,tokenTicker,tokenAmount,documentUri,documentHash,tokenReceiverAddress,batonReceiverAddress,bchChangeReceiverAddress,inputUtxos,decimals){if(decimals===void 0){decimals=0}return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleNFT1ParentGenesis({tokenName:tokenName,tokenTicker:tokenTicker,tokenAmount:tokenAmount,documentUri:documentUri,documentHash:documentHash,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos,decimals:decimals});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleNFT1ChildGenesis=function(nft1GroupId,tokenName,tokenTicker,documentUri,documentHash,tokenReceiverAddress,bchChangeReceiverAddress,inputUtxos,allowBurnAnyAmount){if(allowBurnAnyAmount===void 0){allowBurnAnyAmount=false}return __awaiter(this,void 0,void 0,function(){var genesisTxHex;return __generator(this,function(_a){switch(_a.label){case 0:genesisTxHex=this.txnHelpers.simpleNFT1ChildGenesis({nft1GroupId:nft1GroupId,tokenName:tokenName,tokenTicker:tokenTicker,documentUri:documentUri,documentHash:documentHash,tokenReceiverAddress:tokenReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,inputUtxos:inputUtxos,allowBurnAnyAmount:allowBurnAnyAmount});return[4,this.sendTx(genesisTxHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleTokenMint=function(tokenId,mintAmount,inputUtxos,tokenReceiverAddress,batonReceiverAddress,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenMint({tokenId:tokenId,mintAmount:mintAmount,inputUtxos:inputUtxos,tokenReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.simpleTokenBurn=function(tokenId,burnAmount,inputUtxos,changeReceiverAddress){return __awaiter(this,void 0,void 0,function(){var txHex;return __generator(this,function(_a){switch(_a.label){case 0:txHex=this.txnHelpers.simpleTokenBurn({tokenId:tokenId,burnAmount:burnAmount,inputUtxos:inputUtxos,changeReceiverAddress:changeReceiverAddress});return[4,this.sendTx(txHex)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.getUtxoWithRetry=function(address,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){var result,count;return __generator(this,function(_a){switch(_a.label){case 0:count=0;_a.label=1;case 1:if(!(result===undefined))return[3,4];return[4,this.getUtxos(address)];case 2:result=_a.sent();count++;if(count>retries){throw new Error("this.BITBOX.Address.utxo endpoint experienced a problem")}return[4,sleep(250)];case 3:_a.sent();return[3,1];case 4:return[2,result]}})})};BitboxNetwork.prototype.getUtxoWithTxDetails=function(address){return __awaiter(this,void 0,void 0,function(){var utxos,_a,_b,txIds,txDetails;var _this=this;return __generator(this,function(_c){switch(_c.label){case 0:_b=(_a=utils_1.Utils).mapToSlpAddressUtxoResultArray;return[4,this.getUtxoWithRetry(address)];case 1:utxos=_b.apply(_a,[_c.sent()]);txIds=utxos.map(function(i){return i.txid});if(txIds.length===0){return[2,[]]}return[4,Promise.all(_.chunk(txIds,20).map(function(txids){return _this.getTransactionDetailsWithRetry(__spread(new Set(txids)))}))];case 2:txDetails=_c.sent();txDetails=[].concat.apply([],__spread(txDetails));utxos=utxos.map(function(i){i.tx=txDetails.find(function(d){return d.txid===i.txid});return i});return[2,utxos]}})})};BitboxNetwork.prototype.getTransactionDetailsWithRetry=function(txids,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){var result,count;return __generator(this,function(_a){switch(_a.label){case 0:count=0;_a.label=1;case 1:if(!(result===undefined))return[3,4];return[4,this.BITBOX.Transaction.details(txids)];case 2:result=_a.sent();if(result){return[2,result]}count++;if(count>retries){throw new Error("this.BITBOX.Address.details endpoint experienced a problem")}return[4,sleep(250)];case 3:_a.sent();return[3,1];case 4:return[2]}})})};BitboxNetwork.prototype.getAddressDetailsWithRetry=function(address,retries){if(retries===void 0){retries=40}return __awaiter(this,void 0,void 0,function(){var result,count;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}count=0;_a.label=1;case 1:if(!(result===undefined))return[3,4];return[4,this.BITBOX.Address.details([address])];case 2:result=_a.sent();if(result){return[2,result[0]]}count++;if(count>retries){throw new Error("this.BITBOX.Address.details endpoint experienced a problem")}return[4,sleep(250)];case 3:_a.sent();return[3,1];case 4:return[2]}})})};BitboxNetwork.prototype.sendTx=function(hex){return __awaiter(this,void 0,void 0,function(){var res;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.BITBOX.RawTransactions.sendRawTransaction([hex])];case 1:res=_a.sent();if(typeof res==="object"){return[2,res[0]]}return[2,res]}})})};BitboxNetwork.prototype.monitorForPayment=function(paymentAddress,fee,onPaymentCB){return __awaiter(this,void 0,void 0,function(){var utxo,ex_1;return __generator(this,function(_a){switch(_a.label){case 0:if(!bchaddr.isCashAddress(paymentAddress)&&!bchaddr.isLegacyAddress(paymentAddress)){throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.")}_a.label=1;case 1:if(!true)return[3,7];_a.label=2;case 2:_a.trys.push([2,4,,5]);return[4,this.getUtxos(paymentAddress)];case 3:utxo=_a.sent();if(utxo&&utxo.utxos[0].satoshis>=fee){return[3,7]}return[3,5];case 4:ex_1=_a.sent();console.log(ex_1);return[3,5];case 5:return[4,sleep(2e3)];case 6:_a.sent();return[3,1];case 7:onPaymentCB();return[2]}})})};BitboxNetwork.prototype.getRawTransactions=function(txids){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:if(!(this.validator&&this.validator.getRawTransactions))return[3,2];return[4,this.validator.getRawTransactions(txids)];case 1:return[2,_a.sent()];case 2:return[4,this.BITBOX.RawTransactions.getRawTransaction(txids)];case 3:return[2,_a.sent()]}})})};BitboxNetwork.prototype.processUtxosForSlp=function(utxos){return __awaiter(this,void 0,void 0,function(){return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.slp.processUtxosForSlpAbstract(utxos,this)];case 1:return[2,_a.sent()]}})})};BitboxNetwork.prototype.isValidSlpTxid=function(txid){return __awaiter(this,void 0,void 0,function(){var validatorUrl,result,res;return __generator(this,function(_a){switch(_a.label){case 0:if(!this.validator)return[3,2];return[4,this.validator.isValidSlpTxid(txid,null,null,this.logger)];case 1:return[2,_a.sent()];case 2:validatorUrl=this.setRemoteValidatorUrl();this.logger.log("SLPJS Validating (remote: "+validatorUrl+"): "+txid);return[4,axios_1.default({method:"post",url:validatorUrl,data:{txids:[txid]}})];case 3:result=_a.sent();res=false;if(result&&result.data){res=result.data.filter(function(i){return i.valid}).length>0?true:false}this.logger.log("SLPJS Validator Result: "+res+" (remote: "+validatorUrl+"): "+txid);return[2,res]}})})};BitboxNetwork.prototype.validateSlpTransactions=function(txids){return __awaiter(this,void 0,void 0,function(){var validatorUrl,promises,results,result;return __generator(this,function(_a){switch(_a.label){case 0:if(!this.validator)return[3,2];return[4,this.validator.validateSlpTransactions(txids)];case 1:return[2,_a.sent()];case 2:validatorUrl=this.setRemoteValidatorUrl();promises=_.chunk(txids,20).map(function(ids){return axios_1.default({method:"post",url:validatorUrl,data:{txids:ids}})});return[4,axios_1.default.all(promises)];case 3:results=_a.sent();result={data:[]};results.forEach(function(res){if(res.data){result.data=result.data.concat(res.data)}});if(result&&result.data){return[2,result.data.filter(function(i){return i.valid}).map(function(i){return i.txid})]}return[2,[]]}})})};BitboxNetwork.prototype.getNftParentId=function(tokenIdHex){return __awaiter(this,void 0,void 0,function(){var txnhex,tx,nftBurnTxnHex,nftBurnTxn,slp,nftBurnSlp;return __generator(this,function(_a){switch(_a.label){case 0:return[4,this.getRawTransactions([tokenIdHex])];case 1:txnhex=_a.sent()[0];tx=index_1.Primatives.Transaction.parseFromBuffer(Buffer.from(txnhex,"hex"));return[4,this.getRawTransactions([tx.inputs[0].previousTxHash])];case 2:nftBurnTxnHex=_a.sent()[0];nftBurnTxn=index_1.Primatives.Transaction.parseFromBuffer(Buffer.from(nftBurnTxnHex,"hex"));slp=new slp_1.Slp(this.BITBOX);nftBurnSlp=slp.parseSlpOutputScript(Buffer.from(nftBurnTxn.outputs[0].scriptPubKey));if(nftBurnSlp.transactionType===index_1.SlpTransactionType.GENESIS){return[2,tx.inputs[0].previousTxHash]}else{return[2,nftBurnSlp.tokenIdHex]}return[2]}})})};BitboxNetwork.prototype.setRemoteValidatorUrl=function(){var validatorUrl=this.BITBOX.restURL.replace("v1","v2");validatorUrl=validatorUrl.concat("/slp/validateTxid");validatorUrl=validatorUrl.replace("//slp","/slp");return validatorUrl};return BitboxNetwork}();exports.BitboxNetwork=BitboxNetwork}).call(this,require("buffer").Buffer)},{"../index":1,"./slp":9,"./transactionhelpers":11,"./utils":13,axios:39,"bchaddrjs-slp":67,"bitcore-lib-cash":70,buffer:193,lodash:351}],4:[function(require,module,exports){(function(Buffer){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){function adopt(value){return value instanceof P?value:new P(function(resolve){resolve(value)})}return new(P||(P=Promise))(function(resolve,reject){function fulfilled(value){try{step(generator.next(value))}catch(e){reject(e)}}function rejected(value){try{step(generator["throw"](value))}catch(e){reject(e)}}function step(result){result.done?resolve(result.value):adopt(result.value).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};var __generator=this&&this.__generator||function(thisArg,body){var _={label:0,sent:function(){if(t[0]&1)throw t[1];return t[1]},trys:[],ops:[]},f,y,t,g;return g={next:verb(0),throw:verb(1),return:verb(2)},typeof Symbol==="function"&&(g[Symbol.iterator]=function(){return this}),g;function verb(n){return function(v){return step([n,v])}}function step(op){if(f)throw new TypeError("Generator is already executing.");while(_)try{if(f=1,y&&(t=op[0]&2?y["return"]:op[0]?y["throw"]||((t=y["return"])&&t.call(y),0):y.next)&&!(t=t.call(y,op[1])).done)return t;if(y=0,t)op=[op[0]&2,t.value];switch(op[0]){case 0:case 1:t=op;break;case 4:_.label++;return{value:op[1],done:false};case 5:_.label++;y=op[1];op=[0];continue;case 7:op=_.ops.pop();_.trys.pop();continue;default:if(!(t=_.trys,t=t.length>0&&t[t.length-1])&&(op[0]===6||op[0]===2)){_=0;continue}if(op[0]===3&&(!t||op[1]>t[0]&&op[1]<t[3])){_.label=op[1];break}if(op[0]===6&&_.label<t[1]){_.label=t[1];t=op;break}if(t&&_.label<t[2]){_.label=t[2];_.ops.push(op);break}if(t[2])_.ops.pop();_.trys.pop();continue}op=body.call(thisArg,_)}catch(e){op=[6,e];y=0}finally{f=t=0}if(op[0]&5)throw op[1];return{value:op[0]?op[1]:void 0,done:true}}};var __read=this&&this.__read||function(o,n){var m=typeof Symbol==="function"&&o[Symbol.iterator];if(!m)return o;var i=m.call(o),r,ar=[],e;try{while((n===void 0||n-- >0)&&!(r=i.next()).done)ar.push(r.value)}catch(error){e={error:error}}finally{try{if(r&&!r.done&&(m=i["return"]))m.call(i)}finally{if(e)throw e.error}}return ar};var __spread=this&&this.__spread||function(){for(var ar=[],i=0;i<arguments.length;i++)ar=ar.concat(__read(arguments[i]));return ar};var __importDefault=this&&this.__importDefault||function(mod){return mod&&mod.__esModule?mod:{default:mod}};Object.defineProperty(exports,"__esModule",{value:true});exports.BitdbNetwork=void 0;var index_1=require("../index");var axios_1=__importDefault(require("axios"));var bignumber_js_1=__importDefault(require("bignumber.js"));var BitdbNetwork=function(){function BitdbNetwork(bitdbUrl){if(bitdbUrl===void 0){bitdbUrl="https://bitdb.bch.sx/q/"}this.bitdbUrl=bitdbUrl}BitdbNetwork.prototype.getTokenInformation=function(tokenId){return __awaiter(this,void 0,void 0,function(){var query,data,config,response,list,tokenDetails;return __generator(this,function(_a){switch(_a.label){case 0:query={v:3,q:{find:{"out.h1":"534c5000","out.s3":"GENESIS","tx.h":tokenId}},r:{f:'[ .[] | { token_type: .out[0].h2, timestamp: (if .blk? then (.blk.t | strftime("%Y-%m-%d %H:%M")) else null end), symbol: .out[0].s4, name: .out[0].s5, document: .out[0].s6, document_sha256: .out[0].h7, decimals: .out[0].h8, baton: .out[0].h9, quantity: .out[0].h10, URI: "https://tokengraph.network/token/\\(.tx.h)" } ]'}};data=Buffer.from(JSON.stringify(query)).toString("base64");config={method:"GET",url:this.bitdbUrl+data};return[4,axios_1.default(config)];case 1:response=_a.sent().data;list=[];if(response.c){list.push.apply(list,__spread(response.c))}if(response.u){list.push.apply(list,__spread(response.u))}if(list.length===0){throw new Error("Token not found")}tokenDetails={transactionType:index_1.SlpTransactionType.GENESIS,tokenIdHex:tokenId,versionType:parseInt(list[0].token_type,16),timestamp:list[0].timestamp,symbol:list[0].symbol,name:list[0].name,documentUri:list[0].document,documentSha256:list[0].document_sha256?Buffer.from(list[0].document_sha256):null,decimals:parseInt(list[0].decimals,16)||0,containsBaton:Buffer.from(list[0].baton,"hex").readUIntBE(0,1)>=2,batonVout:Buffer.from(list[0].baton,"hex").readUIntBE(0,1),genesisOrMintQuantity:new bignumber_js_1.default(list[0].quantity,16).dividedBy(Math.pow(10,parseInt(list[0].decimals,16)))};return[2,tokenDetails]}})})};return BitdbNetwork}();exports.BitdbNetwork=BitdbNetwork}).call(this,require("buffer").Buffer)},{"../index":1,axios:39,"bignumber.js":69,buffer:193}],5