UNPKG

bch-slpjs

Version:

Simple Ledger Protocol (SLP) JavaScript Library

1 lines 982 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.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";Object.defineProperty(exports,"__esModule",{value:true});const slpjs=require("./lib/slpjs");exports.slpjs=slpjs},{"./lib/slpjs":7}],2:[function(require,module,exports){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){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):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:true});const bignumber_js_1=require("bignumber.js");const _=require("lodash");const bchaddr=require("bchaddrjs-slp");const bitcore=require("bitcore-lib-cash");const slp_1=require("./slp");const axios_1=require("axios");const utils_1=require("./utils");const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));class BitboxNetwork{constructor(BITBOX,validator){this.BITBOX=BITBOX;this.slp=new slp_1.Slp(BITBOX);if(validator)this.validator=validator;else{this.validatorUrl=BITBOX.restURL.replace("v1","v2");this.validatorUrl=this.validatorUrl.concat("/slp/validate");this.validatorUrl=this.validatorUrl.replace("//slp","/slp")}}getTokenInformation(txid){return __awaiter(this,void 0,void 0,function*(){let txhex=(yield this.BITBOX.RawTransactions.getRawTransaction([txid]))[0];let txn=new bitcore.Transaction(txhex);return this.slp.parseSlpOutputScript(txn.outputs[0]._scriptBuffer)})}getUtxos(address){return __awaiter(this,void 0,void 0,function*(){let res;if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address))throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");res=(yield this.BITBOX.Address.utxo([address]))[0];return res})}getAllSlpBalancesAndUtxos(address){return __awaiter(this,void 0,void 0,function*(){address=bchaddr.toCashAddress(address);let result=yield this.getUtxoWithTxDetails(address);return yield this.processUtxosForSlp(result)})}simpleTokenSend(tokenId,sendAmount,inputUtxos,tokenReceiverAddress,changeReceiverAddress){return __awaiter(this,void 0,void 0,function*(){let totalTokenInputAmount=inputUtxos.filter(txo=>{return slp_1.Slp.preSendSlpJudgementCheck(txo,tokenId)}).reduce((tot,txo)=>{return tot.plus(txo.slpUtxoJudgementAmount)},new bignumber_js_1.default(0));let tokenChangeAmount=totalTokenInputAmount.minus(sendAmount);let txHex;if(tokenChangeAmount.isGreaterThan(new bignumber_js_1.default(0))){let sendOpReturn=this.slp.buildSendOpReturn({tokenIdHex:tokenId,outputQtyArray:[sendAmount,tokenChangeAmount]});txHex=this.slp.buildRawSendTx({slpSendOpReturn:sendOpReturn,input_token_utxos:utils_1.Utils.mapToUtxoArray(inputUtxos),tokenReceiverAddressArray:[tokenReceiverAddress,changeReceiverAddress],bchChangeReceiverAddress:changeReceiverAddress})}else if(tokenChangeAmount.isEqualTo(new bignumber_js_1.default(0))){let sendOpReturn=this.slp.buildSendOpReturn({tokenIdHex:tokenId,outputQtyArray:[sendAmount]});txHex=this.slp.buildRawSendTx({slpSendOpReturn:sendOpReturn,input_token_utxos:utils_1.Utils.mapToUtxoArray(inputUtxos),tokenReceiverAddressArray:[tokenReceiverAddress],bchChangeReceiverAddress:changeReceiverAddress})}else{throw Error("Token inputs less than the token outputs")}return yield this.sendTx(txHex)})}simpleTokenGenesis(tokenName,tokenTicker,tokenAmount,documentUri,documentHash,decimals,tokenReceiverAddress,batonReceiverAddress,bchChangeReceiverAddress,inputUtxos){return __awaiter(this,void 0,void 0,function*(){let genesisOpReturn=this.slp.buildGenesisOpReturn({ticker:tokenTicker,name:tokenName,documentUri:documentUri,hash:documentHash,decimals:decimals,batonVout:2,initialQuantity:tokenAmount});let genesisTxHex=this.slp.buildRawGenesisTx({slpGenesisOpReturn:genesisOpReturn,mintReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:bchChangeReceiverAddress,input_utxos:utils_1.Utils.mapToUtxoArray(inputUtxos)});return yield this.sendTx(genesisTxHex)})}simpleTokenMint(tokenId,mintAmount,inputUtxos,tokenReceiverAddress,batonReceiverAddress,changeReceiverAddress){return __awaiter(this,void 0,void 0,function*(){let mintOpReturn=this.slp.buildMintOpReturn({tokenIdHex:tokenId,mintQuantity:mintAmount,batonVout:2});let txHex=this.slp.buildRawMintTx({input_baton_utxos:utils_1.Utils.mapToUtxoArray(inputUtxos),slpMintOpReturn:mintOpReturn,mintReceiverAddress:tokenReceiverAddress,batonReceiverAddress:batonReceiverAddress,bchChangeReceiverAddress:changeReceiverAddress});return yield this.sendTx(txHex)})}getUtxoWithRetry(address,retries=40){return __awaiter(this,void 0,void 0,function*(){let result;let count=0;while(result===undefined){result=yield this.getUtxos(address);count++;if(count>retries)throw new Error("this.BITBOX.Address.utxo endpoint experienced a problem");yield sleep(250)}return result})}getUtxoWithTxDetails(address){return __awaiter(this,void 0,void 0,function*(){let utxos=utils_1.Utils.mapToSlpAddressUtxoResultArray(yield this.getUtxoWithRetry(address));let txIds=utxos.map(i=>i.txid);if(txIds.length===0)return[];let txDetails=yield Promise.all(_.chunk(txIds,20).map(txids=>{return this.getTransactionDetailsWithRetry([...new Set(txids)])}));txDetails=[].concat(...txDetails);utxos=utxos.map(i=>{i.tx=txDetails.find(d=>d.txid===i.txid);return i});return utxos})}getTransactionDetailsWithRetry(txids,retries=40){return __awaiter(this,void 0,void 0,function*(){let result;let count=0;while(result===undefined){result=yield this.BITBOX.Transaction.details(txids);count++;if(count>retries)throw new Error("this.BITBOX.Address.details endpoint experienced a problem");yield sleep(500)}return result})}getAddressDetailsWithRetry(address,retries=40){return __awaiter(this,void 0,void 0,function*(){if(!bchaddr.isCashAddress(address)&&!bchaddr.isLegacyAddress(address))throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");let result;let count=0;while(result===undefined){result=yield this.BITBOX.Address.details([address]);count++;if(count>retries)throw new Error("this.BITBOX.Address.details endpoint experienced a problem");yield sleep(250)}return result})}sendTx(hex){return __awaiter(this,void 0,void 0,function*(){let res=yield this.BITBOX.RawTransactions.sendRawTransaction(hex);return res})}monitorForPayment(paymentAddress,fee,onPaymentCB){return __awaiter(this,void 0,void 0,function*(){let utxo;if(!bchaddr.isCashAddress(paymentAddress)&&!bchaddr.isLegacyAddress(paymentAddress))throw new Error("Not an a valid address format, must be cashAddr or Legacy address format.");while(true){try{utxo=yield this.getUtxos(paymentAddress);if(utxo)if(utxo.utxos[0].satoshis>=fee)break}catch(ex){console.log(ex)}yield sleep(2e3)}onPaymentCB()})}isValidSlpTxid(txid){throw new Error("Method not implemented.")}validateSlpTransactions(txids){return __awaiter(this,void 0,void 0,function*(){const result=yield axios_1.default({method:"post",url:this.validatorUrl,data:{txids:txids}});if(result&&result.data){return result.data}else{return[]}})}getRawTransactions(txid){throw Error("Method not implemented.")}processUtxosForSlp(utxos){return __awaiter(this,void 0,void 0,function*(){if(this.validator)return yield this.slp.processUtxosForSlpAbstract(utxos,this.validator);return yield this.slp.processUtxosForSlpAbstract(utxos,this)})}}exports.BitboxNetwork=BitboxNetwork},{"./slp":6,"./utils":9,axios:25,"bchaddrjs-slp":52,"bignumber.js":54,"bitcore-lib-cash":55,lodash:198}],3:[function(require,module,exports){(function(Buffer){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){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):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:true});const axios_1=require("axios");const bignumber_js_1=require("bignumber.js");const slpjs_1=require("./slpjs");class BitdbNetwork{constructor(bitdbUrl="https://bitdb.bch.sx/q/"){this.bitdbUrl=bitdbUrl}getTokenInformation(tokenId){return __awaiter(this,void 0,void 0,function*(){let 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)" } ]'}};const data=Buffer.from(JSON.stringify(query)).toString("base64");let config={method:"GET",url:this.bitdbUrl+data};const response=(yield axios_1.default(config)).data;const list=[];if(response.c){list.push(...response.c)}if(response.u){list.push(...response.u)}if(list.length===0){throw new Error("Token not found")}let tokenDetails={transactionType:slpjs_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:Buffer.from(list[0].document_sha256),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 tokenDetails})}}exports.BitdbNetwork=BitdbNetwork}).call(this,require("buffer").Buffer)},{"./slpjs":7,axios:25,"bignumber.js":54,buffer:139}],4:[function(require,module,exports){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){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):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:true});const axios_1=require("axios");const slp_1=require("./slp");class JsonRpcProxyValidator{constructor(BITBOX,validatorUrl){this.validatorUrl=validatorUrl;this.slp=new slp_1.Slp(BITBOX)}isValidSlpTxid(txid){return __awaiter(this,void 0,void 0,function*(){let data={jsonrpc:"2.0",id:"slpvalidate",method:"slpvalidate",params:[txid,false,false]};const result=yield axios_1.default({method:"post",url:this.validatorUrl,data:data});if(result&&result.data&&result.data.result==="Valid"){return true}else{return false}})}getRawTransactions(txid){return __awaiter(this,void 0,void 0,function*(){throw new Error("Not implemented.")})}validateSlpTransactions(txids){return __awaiter(this,void 0,void 0,function*(){const validatePromises=txids.map(txid=>__awaiter(this,void 0,void 0,function*(){const isValid=yield this.isValidSlpTxid(txid);return isValid?txid:""}));const validateResults=yield axios_1.default.all(validatePromises);return validateResults.filter(result=>result.length>0)})}}exports.JsonRpcProxyValidator=JsonRpcProxyValidator},{"./slp":6,axios:25}],5:[function(require,module,exports){(function(Buffer){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){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):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:true});const slp_1=require("./slp");const slpjs_1=require("./slpjs");const bitcore=require("bitcore-lib-cash");const bignumber_js_1=require("bignumber.js");const sleep=ms=>new Promise(resolve=>setTimeout(resolve,ms));class LocalValidator{constructor(BITBOX,getRawTransactions){this.BITBOX=BITBOX;this.getRawTransactions=getRawTransactions;this.slp=new slp_1.Slp(BITBOX);this.cachedValidations={};this.cachedRawTransactions={}}addValidationFromStore(hex,isValid){let id=this.BITBOX.Crypto.sha256(this.BITBOX.Crypto.sha256(Buffer.from(hex,"hex"))).reverse().toString("hex");if(!this.cachedValidations[id])this.cachedValidations[id]={hex:hex,validity:isValid,parents:[],details:null,invalidReason:null};if(!this.cachedRawTransactions[id])this.cachedRawTransactions[id]=hex}waitForCurrentValidationProcessing(txid){return __awaiter(this,void 0,void 0,function*(){let cached=this.cachedValidations[txid];while(true){if(typeof cached.validity==="boolean")break;yield sleep(10)}})}waitForTransactionPreProcessing(txid){return __awaiter(this,void 0,void 0,function*(){while(true){if(this.cachedValidations[txid].hex&&(this.cachedValidations[txid].details||typeof this.cachedValidations.validity==="boolean"))break;yield sleep(10)}return})}getRawTransaction(txid){return __awaiter(this,void 0,void 0,function*(){if(this.cachedRawTransactions[txid])return this.cachedRawTransactions[txid];this.cachedRawTransactions[txid]=(yield this.getRawTransactions([txid]))[0];if(this.cachedRawTransactions[txid])return this.cachedRawTransactions[txid];return null})}isValidSlpTxid(txid){return __awaiter(this,void 0,void 0,function*(){if(txid&&!this.cachedValidations[txid]){this.cachedValidations[txid]={hex:null,validity:null,parents:[],details:null,invalidReason:null};this.cachedValidations[txid].hex=yield this.getRawTransaction(txid)}if(!this.cachedValidations[txid].hex)yield this.waitForTransactionPreProcessing(txid);if(typeof this.cachedValidations[txid].validity==="boolean")return this.cachedValidations[txid].validity;if(this.cachedValidations[txid].details)yield this.waitForCurrentValidationProcessing(txid);let txn=new bitcore.Transaction(this.cachedValidations[txid].hex);let slpmsg;try{slpmsg=this.cachedValidations[txid].details=this.slp.parseSlpOutputScript(txn.outputs[0]._scriptBuffer)}catch(e){this.cachedValidations[txid].invalidReason="SLP OP_RETURN parsing error ("+e.message+").";return this.cachedValidations[txid].validity=false}if(slpmsg.transactionType===slpjs_1.SlpTransactionType.GENESIS){return this.cachedValidations[txid].validity=true}else if(slpmsg.transactionType===slpjs_1.SlpTransactionType.MINT){for(let i=0;i<txn.inputs.length;i++){let input_txid=txn.inputs[i].prevTxId.toString("hex");let input_txhex=yield this.getRawTransaction(input_txid);if(input_txhex){let input_tx=new bitcore.Transaction(input_txhex);try{let input_slpmsg=this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer);if(input_slpmsg.transactionType===slpjs_1.SlpTransactionType.GENESIS)input_slpmsg.tokenIdHex=input_txid;if(input_slpmsg.tokenIdHex===slpmsg.tokenIdHex){if(input_slpmsg.transactionType===slpjs_1.SlpTransactionType.GENESIS||input_slpmsg.transactionType===slpjs_1.SlpTransactionType.MINT){if(txn.inputs[i].outputIndex===input_slpmsg.batonVout)this.cachedValidations[txid].parents.push({txid:txn.inputs[i].prevTxId.toString("hex"),versionType:input_slpmsg.versionType,valid:null,inputQty:null})}}}catch(_){}}}if(this.cachedValidations[txid].parents.length!==1){this.cachedValidations[txid].invalidReason="MINT transaction must have 1 valid baton parent.";return this.cachedValidations[txid].validity=false}}else if(slpmsg.transactionType===slpjs_1.SlpTransactionType.SEND){let tokenOutQty=slpmsg.sendOutputs.reduce((t,v)=>{return t.plus(v)},new bignumber_js_1.default(0));let tokenInQty=new bignumber_js_1.default(0);for(let i=0;i<txn.inputs.length;i++){let input_txid=txn.inputs[i].prevTxId.toString("hex");let input_txhex=yield this.getRawTransaction(input_txid);if(input_txhex){let input_tx=new bitcore.Transaction(input_txhex);try{let input_slpmsg=this.slp.parseSlpOutputScript(input_tx.outputs[0]._scriptBuffer);if(input_slpmsg.transactionType===slpjs_1.SlpTransactionType.GENESIS)input_slpmsg.tokenIdHex=input_txid;if(input_slpmsg.tokenIdHex===slpmsg.tokenIdHex){if(input_slpmsg.transactionType===slpjs_1.SlpTransactionType.SEND){tokenInQty=tokenInQty.plus(input_slpmsg.sendOutputs[txn.inputs[i].outputIndex]);this.cachedValidations[txid].parents.push({txid:txn.inputs[i].prevTxId.toString("hex"),versionType:input_slpmsg.versionType,valid:null,inputQty:input_slpmsg.sendOutputs[txn.inputs[i].outputIndex]})}else if(input_slpmsg.transactionType===slpjs_1.SlpTransactionType.GENESIS||input_slpmsg.transactionType===slpjs_1.SlpTransactionType.MINT){if(txn.inputs[i].outputIndex===1)tokenInQty=tokenInQty.plus(input_slpmsg.genesisOrMintQuantity);this.cachedValidations[txid].parents.push({txid:txn.inputs[i].prevTxId.toString("hex"),versionType:input_slpmsg.versionType,valid:null,inputQty:input_slpmsg.genesisOrMintQuantity})}}}catch(_){}}}if(tokenOutQty.isGreaterThan(tokenInQty)){this.cachedValidations[txid].invalidReason="Token outputs are greater than possible token inputs.";return this.cachedValidations[txid].validity=false}}for(let i=0;i<this.cachedValidations[txid].parents.length;i++){let valid=yield this.isValidSlpTxid(this.cachedValidations[txid].parents[i].txid);this.cachedValidations[txid].parents.find(p=>p.txid===this.cachedValidations[txid].parents[i].txid).valid=valid;if(this.cachedValidations[txid].details.transactionType===slpjs_1.SlpTransactionType.MINT&&!valid){this.cachedValidations[txid].invalidReason="MINT transaction with invalid baton parent.";return this.cachedValidations[txid].validity=false}}if(this.cachedValidations[txid].details.transactionType===slpjs_1.SlpTransactionType.SEND){let validInputQty=this.cachedValidations[txid].parents.reduce((t,v)=>{return v.valid?t.plus(v.inputQty):t},new bignumber_js_1.default(0));let tokenOutQty=slpmsg.sendOutputs.reduce((t,v)=>{return t.plus(v)},new bignumber_js_1.default(0));if(tokenOutQty.isGreaterThan(validInputQty)){this.cachedValidations[txid].invalidReason="Token outputs are greater than valid token inputs.";return this.cachedValidations[txid].validity=false}}if(this.cachedValidations[txid].parents.filter(p=>p.valid).length>0){let validVersionType=this.cachedValidations[txid].parents.find(p=>p.valid).versionType;if(this.cachedValidations[txid].details.versionType!==validVersionType){this.cachedValidations[txid].invalidReason="SLP version/type mismatch from valid parent.";return this.cachedValidations[txid].validity=false}}else if(this.cachedValidations[txid].details.transactionType===slpjs_1.SlpTransactionType.SEND){let slpmsg=this.cachedValidations[txid].details;let valid=yield this.isValidSlpTxid(slpmsg.tokenIdHex);if(valid){let genesisTxn=new bitcore.Transaction(this.cachedValidations[slpmsg.tokenIdHex].hex);let genesisMsg=this.slp.parseSlpOutputScript(genesisTxn.outputs[0]._scriptBuffer);if(genesisMsg.versionType!==slpmsg.versionType){this.cachedValidations[txid].invalidReason="SLP version/type mismatch from valid GENESIS.";return this.cachedValidations[txid].validity=false}}else{this.cachedValidations[txid].invalidReason="SEND has 0 outputs, but has invalid token GENESIS.";console.log(this.cachedValidations[slpmsg.tokenIdHex].invalidReason);return this.cachedValidations[txid].validity=false}}return this.cachedValidations[txid].validity=true})}validateSlpTransactions(txids){return __awaiter(this,void 0,void 0,function*(){let res=[];for(let i=0;i<txids.length;i++){res.push((yield this.isValidSlpTxid)?txids[i]:"")}return res.filter(id=>id.length>0)})}}exports.LocalValidator=LocalValidator}).call(this,require("buffer").Buffer)},{"./slp":6,"./slpjs":7,"bignumber.js":54,"bitcore-lib-cash":55,buffer:139}],6:[function(require,module,exports){(function(Buffer){"use strict";var __awaiter=this&&this.__awaiter||function(thisArg,_arguments,P,generator){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):new P(function(resolve){resolve(result.value)}).then(fulfilled,rejected)}step((generator=generator.apply(thisArg,_arguments||[])).next())})};Object.defineProperty(exports,"__esModule",{value:true});const bchaddr=require("bchaddrjs-slp");const bignumber_js_1=require("bignumber.js");const slpjs_1=require("./slpjs");const slptokentype1_1=require("./slptokentype1");const utils_1=require("./utils");class Slp{constructor(BITBOX){this.BITBOX=BITBOX}get lokadIdHex(){return"534c5000"}buildGenesisOpReturn(config,type=1){let hash;try{hash=config.hash.toString("hex")}catch(_){hash=null}return slptokentype1_1.SlpTokenType1.buildGenesisOpReturn(config.ticker,config.name,config.documentUri,hash,config.decimals,config.batonVout,config.initialQuantity)}buildMintOpReturn(config,type=1){return slptokentype1_1.SlpTokenType1.buildMintOpReturn(config.tokenIdHex,config.batonVout,config.mintQuantity)}buildSendOpReturn(config,type=1){return slptokentype1_1.SlpTokenType1.buildSendOpReturn(config.tokenIdHex,config.outputQtyArray)}buildRawGenesisTx(config,type=1){if(config.mintReceiverSatoshis===undefined)config.mintReceiverSatoshis=new bignumber_js_1.default(546);if(config.batonReceiverSatoshis===undefined)config.batonReceiverSatoshis=new bignumber_js_1.default(546);config.input_utxos.forEach(txo=>{if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.NOT_SLP)return;if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN){throw Error("Input UTXOs included a token for another tokenId.")}if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON)throw Error("Cannot spend a minting baton.");if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_TOKEN_DAG||txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_BATON_DAG)throw Error("Cannot currently spend tokens and baton with invalid DAGs.");throw Error("Cannot spend utxo with no SLP judgement.")});if(!bchaddr.isSlpAddress(config.mintReceiverAddress))throw new Error("Not an SLP address.");if(config.batonReceiverAddress!=null&&!bchaddr.isSlpAddress(config.batonReceiverAddress))throw new Error("Not an SLP address.");config.mintReceiverAddress=bchaddr.toCashAddress(config.mintReceiverAddress);let transactionBuilder=new this.BITBOX.TransactionBuilder(utils_1.Utils.txnBuilderString(config.mintReceiverAddress));let satoshis=new bignumber_js_1.default(0);config.input_utxos.forEach(token_utxo=>{transactionBuilder.addInput(token_utxo.txid,token_utxo.vout);satoshis=satoshis.plus(token_utxo.satoshis)});let genesisCost=this.calculateGenesisCost(config.slpGenesisOpReturn.length,config.input_utxos.length,config.batonReceiverAddress,config.bchChangeReceiverAddress);let bchChangeAfterFeeSatoshis=satoshis.minus(genesisCost);transactionBuilder.addOutput(config.slpGenesisOpReturn,0);transactionBuilder.addOutput(config.mintReceiverAddress,config.mintReceiverSatoshis.toNumber());if(config.batonReceiverAddress!=null){config.batonReceiverAddress=bchaddr.toCashAddress(config.batonReceiverAddress);if(this.parseSlpOutputScript(config.slpGenesisOpReturn).batonVout!==2)throw Error("batonVout in transaction does not match OP_RETURN data.");transactionBuilder.addOutput(config.batonReceiverAddress,config.batonReceiverSatoshis.toNumber())}if(config.bchChangeReceiverAddress!=null&&bchChangeAfterFeeSatoshis.isGreaterThan(new bignumber_js_1.default(546))){config.bchChangeReceiverAddress=bchaddr.toCashAddress(config.bchChangeReceiverAddress);transactionBuilder.addOutput(config.bchChangeReceiverAddress,bchChangeAfterFeeSatoshis.toNumber())}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.toNumber());i++}let tx=transactionBuilder.build().toHex();let outValue=transactionBuilder.transaction.tx.outs.reduce((v,o)=>v+=o.value,0);let inValue=config.input_utxos.reduce((v,i)=>v=v.plus(i.satoshis),new bignumber_js_1.default(0));if(inValue.minus(outValue).isLessThanOrEqualTo(tx.length/2))throw Error("Transaction fee is not high enough.");return tx}buildRawSendTx(config,type=1){const sendMsg=this.parseSlpOutputScript(config.slpSendOpReturn);config.tokenReceiverAddressArray.forEach(outputAddress=>{if(!bchaddr.isSlpAddress(outputAddress))throw new Error("Token receiver address not in SLP format.")});let tokenInputQty=new bignumber_js_1.default(0);config.input_token_utxos.forEach(txo=>{if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.NOT_SLP)return;if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN){if(txo.slpTransactionDetails.tokenIdHex!==sendMsg.tokenIdHex)throw Error("Input UTXOs included a token for another tokenId.");tokenInputQty=tokenInputQty.plus(txo.slpUtxoJudgementAmount);return}if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON)throw Error("Cannot spend a minting baton.");if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_TOKEN_DAG||txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_BATON_DAG)throw Error("Cannot currently spend UTXOs with invalid DAGs.");throw Error("Cannot spend utxo with no SLP judgement.")});let chgAddr=config.bchChangeReceiverAddress?1:0;if(config.tokenReceiverAddressArray.length+chgAddr!==sendMsg.sendOutputs.length)throw Error("Number of token receivers in config does not match the OP_RETURN outputs");let outputTokenQty=sendMsg.sendOutputs.reduce((v,o)=>v=v.plus(o),new bignumber_js_1.default(0));if(!tokenInputQty.isEqualTo(outputTokenQty))throw Error("Token input quantity does not match token outputs.");let transactionBuilder=new this.BITBOX.TransactionBuilder(utils_1.Utils.txnBuilderString(config.tokenReceiverAddressArray[0]));let inputSatoshis=new bignumber_js_1.default(0);config.input_token_utxos.forEach(token_utxo=>{transactionBuilder.addInput(token_utxo.txid,token_utxo.vout);inputSatoshis=inputSatoshis.plus(token_utxo.satoshis)});let sendCost=this.calculateSendCost(config.slpSendOpReturn.length,config.input_token_utxos.length,config.tokenReceiverAddressArray.length,config.bchChangeReceiverAddress);let bchChangeAfterFeeSatoshis=inputSatoshis.minus(sendCost);transactionBuilder.addOutput(config.slpSendOpReturn,0);config.tokenReceiverAddressArray.forEach(outputAddress=>{outputAddress=bchaddr.toCashAddress(outputAddress);transactionBuilder.addOutput(outputAddress,546)});if(config.bchChangeReceiverAddress!=null&&bchChangeAfterFeeSatoshis.isGreaterThan(new bignumber_js_1.default(546))){config.bchChangeReceiverAddress=bchaddr.toCashAddress(config.bchChangeReceiverAddress);transactionBuilder.addOutput(config.bchChangeReceiverAddress,bchChangeAfterFeeSatoshis.toNumber())}let i=0;for(const txo of config.input_token_utxos){let paymentKeyPair=this.BITBOX.ECPair.fromWIF(txo.wif);transactionBuilder.sign(i,paymentKeyPair,null,transactionBuilder.hashTypes.SIGHASH_ALL,txo.satoshis.toNumber());i++}let tx=transactionBuilder.build().toHex();let outValue=transactionBuilder.transaction.tx.outs.reduce((v,o)=>v+=o.value,0);let inValue=config.input_token_utxos.reduce((v,i)=>v=v.plus(i.satoshis),new bignumber_js_1.default(0));if(inValue.minus(outValue).isLessThanOrEqualTo(tx.length/2))throw Error("Transaction fee is not high enough.");return tx}buildRawMintTx(config,type=1){let mintMsg=this.parseSlpOutputScript(config.slpMintOpReturn);if(config.mintReceiverSatoshis===undefined)config.mintReceiverSatoshis=new bignumber_js_1.default(546);if(config.batonReceiverSatoshis===undefined)config.batonReceiverSatoshis=new bignumber_js_1.default(546);if(!bchaddr.isSlpAddress(config.mintReceiverAddress)){throw new Error("Mint receiver address not in SLP format.")}if(config.batonReceiverAddress!=null&&!bchaddr.isSlpAddress(config.batonReceiverAddress)){throw new Error("Baton receiver address not in SLP format.")}config.mintReceiverAddress=bchaddr.toCashAddress(config.mintReceiverAddress);config.batonReceiverAddress=bchaddr.toCashAddress(config.batonReceiverAddress);config.input_baton_utxos.forEach(txo=>{if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.NOT_SLP)return;if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN)throw Error("Input UTXOs should not include any tokens.");if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON){if(txo.slpTransactionDetails.tokenIdHex!==mintMsg.tokenIdHex)throw Error("Cannot spend a minting baton.");return}if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_TOKEN_DAG||txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_BATON_DAG)throw Error("Cannot currently spend UTXOs with invalid DAGs.");throw Error("Cannot spend utxo with no SLP judgement.")});if(!config.input_baton_utxos.find(o=>o.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON))Error("There is no baton included with the input UTXOs.");let transactionBuilder=new this.BITBOX.TransactionBuilder(utils_1.Utils.txnBuilderString(config.mintReceiverAddress));let satoshis=new bignumber_js_1.default(0);config.input_baton_utxos.forEach(baton_utxo=>{transactionBuilder.addInput(baton_utxo.txid,baton_utxo.vout);satoshis=satoshis.plus(baton_utxo.satoshis)});let mintCost=this.calculateGenesisCost(config.slpMintOpReturn.length,config.input_baton_utxos.length,config.batonReceiverAddress,config.bchChangeReceiverAddress);let bchChangeAfterFeeSatoshis=satoshis.minus(mintCost);transactionBuilder.addOutput(config.slpMintOpReturn,0);transactionBuilder.addOutput(config.mintReceiverAddress,config.mintReceiverSatoshis.toNumber());if(config.batonReceiverAddress!==null){config.batonReceiverAddress=bchaddr.toCashAddress(config.batonReceiverAddress);if(this.parseSlpOutputScript(config.slpMintOpReturn).batonVout!==2)throw Error("batonVout in transaction does not match OP_RETURN data.");transactionBuilder.addOutput(config.batonReceiverAddress,config.batonReceiverSatoshis.toNumber())}if(config.bchChangeReceiverAddress!==null&&bchChangeAfterFeeSatoshis.isGreaterThan(new bignumber_js_1.default(546))){config.bchChangeReceiverAddress=bchaddr.toCashAddress(config.bchChangeReceiverAddress);transactionBuilder.addOutput(config.bchChangeReceiverAddress,bchChangeAfterFeeSatoshis.toNumber())}let i=0;for(const txo of config.input_baton_utxos){let paymentKeyPair=this.BITBOX.ECPair.fromWIF(txo.wif);transactionBuilder.sign(i,paymentKeyPair,null,transactionBuilder.hashTypes.SIGHASH_ALL,txo.satoshis.toNumber());i++}let tx=transactionBuilder.build().toHex();let outValue=transactionBuilder.transaction.tx.outs.reduce((v,o)=>v+=o.value,0);let inValue=config.input_baton_utxos.reduce((v,i)=>v=v.plus(i.satoshis),new bignumber_js_1.default(0));if(inValue.minus(outValue).isLessThanOrEqualTo(tx.length/2))throw Error("Transaction fee is not high enough.");return tx}parseSlpOutputScript(outputScript){let slpMsg={};let chunks;try{chunks=this.parseOpReturnToChunks(outputScript)}catch(e){throw Error("Bad OP_RETURN")}if(chunks.length===0)throw Error("Empty OP_RETURN");if(!chunks[0].equals(Buffer.from(this.lokadIdHex,"hex")))throw Error("No SLP");if(chunks.length===1)throw Error("Missing token_type");slpMsg.versionType=Slp.parseChunkToInt(chunks[1],1,2,true);if(chunks.length===2)throw Error("Missing SLP transaction type");try{slpMsg.transactionType=slpjs_1.SlpTransactionType[chunks[2].toString("ascii")]}catch(_){throw Error("Bad transaction type")}if(slpMsg.transactionType===slpjs_1.SlpTransactionType.GENESIS){if(chunks.length!==10)throw Error("GENESIS with incorrect number of parameters");slpMsg.symbol=chunks[3]?chunks[3].toString("utf8"):"";slpMsg.name=chunks[4]?chunks[4].toString("utf8"):"";slpMsg.documentUri=chunks[5]?chunks[5].toString("utf8"):"";slpMsg.documentSha256=chunks[6]?chunks[6]:null;if(slpMsg.documentSha256){if(slpMsg.documentSha256.length!==0&&slpMsg.documentSha256.length!==32)throw Error("Token document hash is incorrect length")}slpMsg.decimals=Slp.parseChunkToInt(chunks[7],1,1,true);if(slpMsg.decimals>9)throw Error("Too many decimals");slpMsg.batonVout=chunks[8]?Slp.parseChunkToInt(chunks[8],1,1):null;if(slpMsg.batonVout!==null){if(slpMsg.batonVout<2)throw Error("Mint baton cannot be on vout=0 or 1");slpMsg.containsBaton=true}slpMsg.genesisOrMintQuantity=new bignumber_js_1.default(chunks[9].readUInt32BE(0).toString()).multipliedBy(Math.pow(2,32)).plus(chunks[9].readUInt32BE(4).toString())}else if(slpMsg.transactionType===slpjs_1.SlpTransactionType.SEND){if(chunks.length<4)throw Error("SEND with too few parameters");if(chunks[3].length!==32)throw Error("token_id is wrong length");slpMsg.tokenIdHex=chunks[3].toString("hex");slpMsg.sendOutputs=[];slpMsg.sendOutputs.push(new bignumber_js_1.default(0));chunks.slice(4).forEach(chunk=>{if(chunk.length!==8)throw Error("SEND quantities must be 8-bytes each.");slpMsg.sendOutputs.push(new bignumber_js_1.default(chunk.readUInt32BE(0).toString()).multipliedBy(Math.pow(2,32)).plus(new bignumber_js_1.default(chunk.readUInt32BE(4).toString())))});if(slpMsg.sendOutputs.length<2)throw Error("Missing output amounts");if(slpMsg.sendOutputs.length>20)throw Error("More than 19 output amounts")}else if(slpMsg.transactionType===slpjs_1.SlpTransactionType.MINT){if(chunks.length!=6)throw Error("MINT with incorrect number of parameters");if(chunks[3].length!=32)throw Error("token_id is wrong length");slpMsg.tokenIdHex=chunks[3].toString("hex");slpMsg.batonVout=chunks[4]?Slp.parseChunkToInt(chunks[4],1,1):null;if(slpMsg.batonVout!==null){if(slpMsg.batonVout<2)throw Error("Mint baton cannot be on vout=0 or 1");slpMsg.containsBaton=true}slpMsg.genesisOrMintQuantity=new bignumber_js_1.default(chunks[5].readUInt32BE(0).toString()).multipliedBy(Math.pow(2,32)).plus(new bignumber_js_1.default(chunks[5].readUInt32BE(4).toString()))}else throw Error("Bad transaction type");return slpMsg}static parseChunkToInt(intBytes,minByteLen,maxByteLen,raise_on_Null=false){if(intBytes.length>=minByteLen&&intBytes.length<=maxByteLen)return intBytes.readUIntBE(0,intBytes.length);if(intBytes.length===0&&!raise_on_Null)return null;throw Error("Field has wrong length")}parseOpReturnToChunks(script,allow_op_0=false,allow_op_number=false){let ops;try{ops=this.getScriptOperations(script)}catch(e){throw Error("Script error")}if(ops[0].opcode!=this.BITBOX.Script.opcodes.OP_RETURN)throw Error("No OP_RETURN");let chunks=[];ops.slice(1).forEach(opitem=>{if(opitem.opcode>this.BITBOX.Script.opcodes.OP_16)throw Error("Non-push opcode");if(opitem.opcode>this.BITBOX.Script.opcodes.OP_PUSHDATA4){if(opitem.opcode===80)throw Error("Non-push opcode");if(!allow_op_number)throw Error("OP_1NEGATE to OP_16 not allowed");if(opitem.opcode===this.BITBOX.Script.opcodes.OP_1NEGATE)opitem.data=Buffer.from([129]);else opitem.data=Buffer.from([opitem.opcode-80])}if(opitem.opcode===this.BITBOX.Script.opcodes.OP_0&&!allow_op_0){throw Error("OP_0 not allowed")}chunks.push(opitem.data)});return chunks}getScriptOperations(script){let ops=[];try{let n=0;let dlen;while(n<script.length){let op={opcode:script[n],data:null};n+=1;if(op.opcode<=this.BITBOX.Script.opcodes.OP_PUSHDATA4){if(op.opcode<this.BITBOX.Script.opcodes.OP_PUSHDATA1)dlen=op.opcode;else if(op.opcode===this.BITBOX.Script.opcodes.OP_PUSHDATA1){dlen=script[n];n+=1}else if(op.opcode===this.BITBOX.Script.opcodes.OP_PUSHDATA2){dlen=script.slice(n,n+2).readUIntLE(0,2);n+=2}else{dlen=script.slice(n,n+4).readUIntLE(0,4);n+=4}if(n+dlen>script.length){throw Error("IndexError")}if(dlen>0)op.data=script.slice(n,n+dlen);n+=dlen}ops.push(op)}}catch(e){throw Error("truncated script")}return ops}calculateGenesisCost(genesisOpReturnLength,inputUtxoSize,batonAddress,bchChangeAddress,feeRate=1){return this.calculateMintOrGenesisCost(genesisOpReturnLength,inputUtxoSize,batonAddress,bchChangeAddress,feeRate)}calculateMintCost(mintOpReturnLength,inputUtxoSize,batonAddress,bchChangeAddress,feeRate=1){return this.calculateMintOrGenesisCost(mintOpReturnLength,inputUtxoSize,batonAddress,bchChangeAddress,feeRate)}calculateMintOrGenesisCost(mintOpReturnLength,inputUtxoSize,batonAddress,bchChangeAddress,feeRate=1){let outputs=1;let nonfeeoutputs=546;if(batonAddress!==null&&batonAddress!==undefined){nonfeeoutputs+=546;outputs+=1}if(bchChangeAddress!==null&&bchChangeAddress!==undefined){outputs+=1}let fee=this.BITBOX.BitcoinCash.getByteCount({P2PKH:inputUtxoSize},{P2PKH:outputs});fee+=mintOpReturnLength;fee+=10;fee*=feeRate;fee+=nonfeeoutputs;return fee}calculateSendCost(sendOpReturnLength,inputUtxoSize,outputAddressArraySize,bchChangeAddress,feeRate=1){let outputs=outputAddressArraySize;let nonfeeoutputs=outputAddressArraySize*546;if(bchChangeAddress!=null){outputs+=1}let fee=this.BITBOX.BitcoinCash.getByteCount({P2PKH:inputUtxoSize},{P2PKH:outputs});fee+=sendOpReturnLength;fee+=10;fee*=feeRate;fee+=nonfeeoutputs;return fee}static preSendSlpJudgementCheck(txo,tokenId){if(txo.slpUtxoJudgement===undefined||txo.slpUtxoJudgement===null||txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.UNKNOWN)throw Error("There at least one input UTXO that does not have a proper SLP judgement");if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON)throw Error("There is at least one input UTXO that is a baton. You can only spend batons in a MINT transaction.");if(txo.slpTransactionDetails){if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN){if(!txo.slpUtxoJudgementAmount)throw Error("There is at least one input token that does not have the 'slpUtxoJudgementAmount' property set.");if(txo.slpTransactionDetails.tokenIdHex!==tokenId)throw Error("There is at least one input UTXO that is a different SLP token than the one specified.");return txo.slpTransactionDetails.tokenIdHex===tokenId}}return false}processUtxosForSlpAbstract(utxos,asyncSlpValidator){return __awaiter(this,void 0,void 0,function*(){for(let txo of utxos){this.applyInitialSlpJudgement(txo);if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.UNKNOWN||txo.slpUtxoJudgement===undefined)throw Error("Utxo SLP judgement has not been set, unknown error.")}yield this.applyFinalSlpJudgement(asyncSlpValidator,utxos);const result=this.computeSlpBalances(utxos);let tokenTxoCount=0;for(let id in result.slpTokenUtxos)tokenTxoCount+=result.slpTokenUtxos[id].length;let batonTxoCount=0;for(let id in result.slpBatonUtxos)batonTxoCount+=result.slpBatonUtxos[id].length;if(utxos.length!==tokenTxoCount+batonTxoCount+result.nonSlpUtxos.length+result.invalidBatonUtxos.length+result.invalidTokenUtxos.length)throw Error("Not all UTXOs have been categorized. Unknown Error.");return result})}computeSlpBalances(utxos){const result={satoshis_available_bch:0,satoshis_in_slp_baton:0,satoshis_in_slp_token:0,satoshis_in_invalid_token_dag:0,satoshis_in_invalid_baton_dag:0,slpTokenBalances:{},slpTokenUtxos:{},slpBatonUtxos:{},nonSlpUtxos:[],invalidTokenUtxos:[],invalidBatonUtxos:[]};for(const txo of utxos){if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN){if(!(txo.slpTransactionDetails.tokenIdHex in result.slpTokenBalances))result.slpTokenBalances[txo.slpTransactionDetails.tokenIdHex]=new bignumber_js_1.default(0);if(txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.GENESIS||txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.MINT){result.slpTokenBalances[txo.slpTransactionDetails.tokenIdHex]=result.slpTokenBalances[txo.slpTransactionDetails.tokenIdHex].plus(txo.slpTransactionDetails.genesisOrMintQuantity)}else if(txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.SEND&&txo.slpTransactionDetails.sendOutputs){let qty=txo.slpTransactionDetails.sendOutputs[txo.vout];result.slpTokenBalances[txo.slpTransactionDetails.tokenIdHex]=result.slpTokenBalances[txo.slpTransactionDetails.tokenIdHex].plus(qty)}else{throw Error("Unknown Error: cannot have an SLP_TOKEN that is not from GENESIS, MINT, or SEND.")}result.satoshis_in_slp_token+=txo.satoshis;if(!(txo.slpTransactionDetails.tokenIdHex in result.slpTokenUtxos))result.slpTokenUtxos[txo.slpTransactionDetails.tokenIdHex]=[];result.slpTokenUtxos[txo.slpTransactionDetails.tokenIdHex].push(txo)}else if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON){result.satoshis_in_slp_baton+=txo.satoshis;if(!(txo.slpTransactionDetails.tokenIdHex in result.slpBatonUtxos))result.slpBatonUtxos[txo.slpTransactionDetails.tokenIdHex]=[];result.slpBatonUtxos[txo.slpTransactionDetails.tokenIdHex].push(txo)}else if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_TOKEN_DAG){result.satoshis_in_invalid_token_dag+=txo.satoshis;result.invalidTokenUtxos.push(txo)}else if(txo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.INVALID_BATON_DAG){result.satoshis_in_invalid_baton_dag+=txo.satoshis;result.invalidBatonUtxos.push(txo)}else{result.satoshis_available_bch+=txo.satoshis;result.nonSlpUtxos.push(txo)}}return result}applyInitialSlpJudgement(txo){try{let vout=txo.tx.vout.find(vout=>vout.n===0);if(!vout)throw"Utxo contains no Vout!";let vout0script=Buffer.from(vout.scriptPubKey.hex,"hex");txo.slpTransactionDetails=this.parseSlpOutputScript(vout0script);if(txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.GENESIS)txo.slpTransactionDetails.tokenIdHex=txo.txid;if(txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.GENESIS||txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.MINT){if(txo.slpTransactionDetails.containsBaton&&txo.slpTransactionDetails.batonVout===txo.vout){txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.SLP_BATON}else if(txo.vout===1&&txo.slpTransactionDetails.genesisOrMintQuantity.isGreaterThan(0)){txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.SLP_TOKEN;txo.slpUtxoJudgementAmount=txo.slpTransactionDetails.genesisOrMintQuantity}else txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.NOT_SLP}else if(txo.slpTransactionDetails.transactionType===slpjs_1.SlpTransactionType.SEND&&txo.slpTransactionDetails.sendOutputs){if(txo.vout>0&&txo.vout<txo.slpTransactionDetails.sendOutputs.length){txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.SLP_TOKEN;txo.slpUtxoJudgementAmount=txo.slpTransactionDetails.sendOutputs[txo.vout]}else txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.NOT_SLP}else{txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.NOT_SLP}}catch(e){txo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.NOT_SLP}}applyFinalSlpJudgement(asyncSlpValidator,utxos){return __awaiter(this,void 0,void 0,function*(){let validSLPTx=yield asyncSlpValidator.validateSlpTransactions([...new Set(utxos.filter(txOut=>{if(txOut.slpTransactionDetails&&txOut.slpUtxoJudgement!==slpjs_1.SlpUtxoJudgement.UNKNOWN&&txOut.slpUtxoJudgement!==slpjs_1.SlpUtxoJudgement.NOT_SLP)return true;return false}).map(txOut=>txOut.txid))]);utxos.forEach(utxo=>{if(!validSLPTx.includes(utxo.txid)){if(utxo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_TOKEN){utxo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.INVALID_TOKEN_DAG}else if(utxo.slpUtxoJudgement===slpjs_1.SlpUtxoJudgement.SLP_BATON){utxo.slpUtxoJudgement=slpjs_1.SlpUtxoJudgement.INVALID_BATON_DAG}}})})}}exports.Slp=Slp}).call(this,require("buffer").Buffer)},{"./slpjs":7,"./slptokentype1":8,"./utils":9,"bchaddrjs-slp":52,"bignumber.js":54,buffer:139}],7:[function(require,module,exports){"use strict";Object.defineProperty(exports,"__esModule",{value:true});const slp_1=require("./slp");const utils_1=require("./utils");const bitdbnetwork_1=require("./bitdbnetwork");const jsonrpcvalidator_1=require("./jsonrpcvalidator");const localvalidator_1=require("./localvalidator");const bitboxnetwork_1=require("./bitboxnetwork");exports.Slp=slp_1.Slp;exports.Utils=utils_1.Utils;exports.BitboxNetwork=bitboxnetwork_1.BitboxNetwork;exports.BitdbNetwork=bitdbnetwork_1.BitdbNetwork;exports.JsonRpcProxyValidator=jsonrpcvalidator_1.JsonRpcProxyValidator;exports.LocalValidator=localvalidator_1.LocalValidator;var SlpTransactionType;(function(SlpTransactionType){SlpTransactionType[SlpTransactionType["GENESIS"]=0]="GENESIS";SlpTransactionType[SlpTransactionType["MINT"]=1]="MINT";SlpTransactionType[SlpTransactionType["SEND"]=2]="SEND"})(SlpTransactionType=exports.SlpTransactionType||(exports.SlpTransactionType={}));var SlpVersionType;(function(SlpVersionType){SlpVersionType[SlpVersionType["TokenVersionType1"]=1]="TokenVersionType1"})(SlpVersionType=exports.SlpVersionType||(exports.SlpVersionType={}));var SlpUtxoJudgement;(function(SlpUtxoJudgement){SlpUtxoJudgement[SlpUtxoJudgement["UNKNOWN"]=-3]="UNKNOWN";SlpUtxoJudgement[SlpUtxoJudgement["INVALID_BATON_DAG"]=-2]="INVALID_BATON_DAG";SlpUtxoJudgement[SlpUtxoJudgement["INVALID_TOKEN_DAG"]=-1]="INVALID_TOKEN_DAG";SlpUtxoJudgement[SlpUtxoJudgement["NOT_SLP"]=0]="NOT_SLP";SlpUtxoJudgement[SlpUtxoJudgement["SLP_TOKEN"]=1]="SLP_TOKEN";SlpUtxoJudgement[SlpUtxoJudgement["SLP_BATON"]=2]="SLP_BATON"})(SlpUtxoJudgement=exports.SlpUtxoJudgement||(exports.SlpUtxoJudgement={}));class SlpAddressUtxoResult{constructor(){this.slpUtxoJudgement=SlpUtxoJudgement.UNKNOWN}}exports.SlpAddressUtxoResult=SlpAddressUtxoResult},{"./bitboxnetwork":2,"./bitdbnetwork":3,"./jsonrpcvalidator":4,"./localvalidator":5,"./slp":6,"./utils":9}],8:[function(require,module,exports){(function(Buffer){"use strict";Object.defineProperty(exports,"__esModule",{value:true});const utils_1=require("./utils");const bignumber_js_1=require("bignumber.js");class SlpTokenType1{static get lokadIdHex(){return"534c5000"}static buildGenesisOpReturn(ticker,name,documentUri,documentHashHex,decimals,batonVout,initialQuantity){let script=[];script.push(106);let lokadId=Buffer.from(this.lokadIdHex,"hex");script.push(utils_1.Utils.getPushDataOpcode(lokadId));lokadId.forEach(item=>script.push(item));let tokenVersionType=1;script.push(utils_1.Utils.getPushDataOpcode([tokenVersionType]));script.push(tokenVersionType);let transactionType=Buffer.from("GENESIS");script.push(utils_1.Utils.getPushDataOpcode(transactionType));transactionType.forEach(item=>script.push(item));if(ticker!==null&&typeof ticker!=="string"){throw Error("ticker must be a string")}else if(ticker===null||ticker.length===0){[76,0].forEach(item=>script.push(item))}else{let tickerBuf=Buffer.from(ticker,"utf8");script.push(utils_1.Utils.getPushDataOpcode(tickerBuf));tickerBuf.forEach(item=>script.push(item))}if(name!==null&&typeof name!=="string"){throw Error("name must be a string")}else if(name==null||name.length===0){[76,0].forEach(item=>script.push(item))}else{let nameBuf=Buffer.from(name,"utf8");script.push(utils_1.Utils.getPushDataOpcode(nameBuf));nameBuf.forEach(item=>script.push(item))}if(documentUri!==null&&typeof documentUri!=="string"){throw Error("documentUri must be a string")}else if(documentUri==null||documentUri.length===0){[76,0].forEach(item=>script.push(item))}else{let documentUriBuf=Buffer.from(documentUri,"ascii");script.push(utils_1.Utils.getPushDataOpcode(documentUriBuf));documentUriBuf.forEach(item=>script.push(item))}var re=/^[0-9a-fA-F]+$/;if(documentHashHex==null||documentHashHex.length===0){[76,0].forEach(item=>script.push(item))}else if(documentHashHex.length===64&&re.test(documentHashHex)){let documentHashBuf=Buffer.from(documentHashHex,"hex");script.push(utils_1.Utils.getPushDataOpcode(documentHashBuf));documentHashBuf.forEach(item=>script.push(item))}else{throw Error("Document hash must be provided as a 64 character hex string")}if(decimals===null||decimals<0||decimals>9){throw Error("Decimals property must be in range 0 to 9")}else{script.push(utils_1.Utils.getPushDataOpcode([decimals]));script.push(decimals)}if(batonVout==null){[76,0].forEach(item=>script.push(item))}else{if(batonVout<2||batonVout>255||!(typeof batonVout=="number"))throw Error("Baton vout must a number and greater than 1 and less than 256.");script.push(utils_1.Utils.getPushDataOpcode([batonVout]));script.push(batonVout)}let MAX_QTY=new bignumber_js_1.default("18446744073709551615");try{initialQuantity.absoluteValue()}catch(_){throw Error("Amount must be an instance of BigNumber")}if(initialQuantity.isGreaterThan(MAX_QTY))throw new Error("Maximum genesis value exceeded. Reduce input quantity below 18446744073709551615.");if(initialQuantity.isLessThan(0))throw Error("Genesis quantity must be greater than 0.");if(!initialQuantity.modulo(1).isEqualTo(new bignumber_js_1.default(0)))throw Error("Genesis quantity must be a whole number.");let initialQuantityBuf=utils_1.Utils.int2FixedBuffer(initialQuantity);script.push(utils_1.Utils.getPushDataOpcode(initialQuantityBuf));initialQuantityBuf.forEach(item=>script.push(item));let encodedScript=utils_1.Utils.encodeScript(script);if(encodedScript.length>223){throw Error("Script too long, must be less than 223 bytes.")}return encodedScript}static buildSendOpReturn(tokenIdHex,outputQtyArray){let script=[];script.push(106);let lokadId=Buffer.from(this.lokadIdHex,"hex");script.push(utils_1.Utils.getPushDataOpcode(lokadId));lokadId.forEach(item=>script.push(item));let tokenVersionType=1;script.push(utils_1.Utils.getPushDataOpcode([tokenVersionType]));script.push(tokenVersionType);le